optimize tfile
This commit is contained in:
parent
4bdac1fdbf
commit
cee5a2ecbe
|
@ -43,7 +43,7 @@ int32_t tfFsync(int64_t tfd);
|
|||
bool tfValid(int64_t tfd);
|
||||
int64_t tfLseek(int64_t tfd, int64_t offset, int32_t whence);
|
||||
int32_t tfFtruncate(int64_t tfd, int64_t length);
|
||||
|
||||
void * tfMmapReadOnly(int64_t tfd, int64_t length);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -22,6 +22,8 @@ extern "C" {
|
|||
|
||||
#include "tfile.h"
|
||||
|
||||
//#define USE_MMAP 1
|
||||
|
||||
#define DefaultMem 1024 * 1024
|
||||
|
||||
static char tmpFile[] = "./index";
|
||||
|
@ -39,6 +41,9 @@ typedef struct WriterCtx {
|
|||
bool readOnly;
|
||||
char buf[256];
|
||||
int size;
|
||||
#ifdef USE_MMAP
|
||||
char* ptr;
|
||||
#endif
|
||||
} file;
|
||||
struct {
|
||||
int32_t capa;
|
||||
|
|
|
@ -31,7 +31,12 @@ static int writeCtxDoWrite(WriterCtx* ctx, uint8_t* buf, int len) {
|
|||
static int writeCtxDoRead(WriterCtx* ctx, uint8_t* buf, int len) {
|
||||
int nRead = 0;
|
||||
if (ctx->type == TFile) {
|
||||
#ifdef USE_MMAP
|
||||
nRead = len < ctx->file.size ? len : ctx->file.size;
|
||||
memcpy(buf, ctx->file.ptr, nRead);
|
||||
#else
|
||||
nRead = tfRead(ctx->file.fd, buf, len);
|
||||
#endif
|
||||
} else {
|
||||
memcpy(buf, ctx->mem.buf + ctx->offset, len);
|
||||
}
|
||||
|
@ -43,7 +48,13 @@ static int writeCtxDoReadFrom(WriterCtx* ctx, uint8_t* buf, int len, int32_t off
|
|||
int nRead = 0;
|
||||
if (ctx->type == TFile) {
|
||||
// tfLseek(ctx->file.fd, offset, 0);
|
||||
#ifdef USE_MMAP
|
||||
int32_t last = ctx->file.size - offset;
|
||||
nRead = last >= len ? len : last;
|
||||
memcpy(buf, ctx->file.ptr + offset, nRead);
|
||||
#else
|
||||
nRead = tfPread(ctx->file.fd, buf, len, offset);
|
||||
#endif
|
||||
} else {
|
||||
// refactor later
|
||||
assert(0);
|
||||
|
@ -83,6 +94,9 @@ WriterCtx* writerCtxCreate(WriterType type, const char* path, bool readOnly, int
|
|||
struct stat fstat;
|
||||
stat(path, &fstat);
|
||||
ctx->file.size = fstat.st_size;
|
||||
#ifdef USE_MMAP
|
||||
ctx->file.ptr = (char*)tfMmapReadOnly(ctx->file.fd, ctx->file.size);
|
||||
#endif
|
||||
}
|
||||
memcpy(ctx->file.buf, path, strlen(path));
|
||||
if (ctx->file.fd < 0) {
|
||||
|
@ -111,8 +125,12 @@ void writerCtxDestroy(WriterCtx* ctx, bool remove) {
|
|||
if (ctx->type == TMemory) {
|
||||
free(ctx->mem.buf);
|
||||
} else {
|
||||
// ctx->flush(ctx);
|
||||
tfClose(ctx->file.fd);
|
||||
if (ctx->file.readOnly) {
|
||||
#ifdef USE_MMAP
|
||||
munmap(ctx->file.ptr, ctx->file.size);
|
||||
#endif
|
||||
}
|
||||
if (remove) { unlink(ctx->file.buf); }
|
||||
}
|
||||
free(ctx);
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
#include "tutil.h"
|
||||
using namespace std;
|
||||
|
||||
#define NUM_OF_THREAD 10
|
||||
#define NUM_OF_THREAD 5
|
||||
|
||||
class DebugInfo {
|
||||
public:
|
||||
|
|
|
@ -158,3 +158,13 @@ int32_t tfFtruncate(int64_t tfd, int64_t length) {
|
|||
taosReleaseRef(tsFileRsetId, tfd);
|
||||
return code;
|
||||
}
|
||||
|
||||
void *tfMmapReadOnly(int64_t tfd, int64_t length) {
|
||||
void *p = taosAcquireRef(tsFileRsetId, tfd);
|
||||
if (p == NULL) return NULL;
|
||||
int32_t fd = (int32_t)(uintptr_t)p;
|
||||
|
||||
void *ptr = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);
|
||||
taosReleaseRef(tsFileRsetId, tfd);
|
||||
return ptr;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue