refactor index code

This commit is contained in:
yihaoDeng 2022-07-01 11:50:41 +08:00
parent 3681aacf0e
commit 8d1e2662c7
4 changed files with 15 additions and 17 deletions

View File

@ -41,13 +41,13 @@ typedef struct IFileCtx {
TdFilePtr pFile; TdFilePtr pFile;
bool readOnly; bool readOnly;
char buf[256]; char buf[256];
int size; int64_t size;
#ifdef USE_MMAP #ifdef USE_MMAP
char* ptr; char* ptr;
#endif #endif
} file; } file;
struct { struct {
int32_t capa; int32_t cap;
char* buf; char* buf;
} mem; } mem;
}; };

View File

@ -61,7 +61,7 @@ static int idxFileCtxDoReadFrom(IFileCtx* ctx, uint8_t* buf, int len, int32_t of
} }
return nRead; return nRead;
} }
static int writeCtxGetSize(IFileCtx* ctx) { static int idxFileCtxGetSize(IFileCtx* ctx) {
if (ctx->type == TFile) { if (ctx->type == TFile) {
int64_t file_size = 0; int64_t file_size = 0;
taosStatFile(ctx->file.buf, &file_size, NULL); taosStatFile(ctx->file.buf, &file_size, NULL);
@ -90,38 +90,36 @@ IFileCtx* idxFileCtxCreate(WriterType type, const char* path, bool readOnly, int
if (ctx->type == TFile) { if (ctx->type == TFile) {
// ugly code, refactor later // ugly code, refactor later
ctx->file.readOnly = readOnly; ctx->file.readOnly = readOnly;
memcpy(ctx->file.buf, path, strlen(path));
if (readOnly == false) { if (readOnly == false) {
ctx->file.pFile = taosOpenFile(path, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND); ctx->file.pFile = taosOpenFile(path, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND);
taosFtruncateFile(ctx->file.pFile, 0); taosFtruncateFile(ctx->file.pFile, 0);
int64_t file_size; taosStatFile(path, &ctx->file.size, NULL);
taosStatFile(path, &file_size, NULL); // ctx->file.size = (int)size;
ctx->file.size = (int)file_size;
} else { } else {
// ctx->file.pFile = open(path, O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO);
ctx->file.pFile = taosOpenFile(path, TD_FILE_READ); ctx->file.pFile = taosOpenFile(path, TD_FILE_READ);
int64_t file_size = 0; int64_t size = 0;
taosFStatFile(ctx->file.pFile, &file_size, NULL); taosFStatFile(ctx->file.pFile, &ctx->file.size, NULL);
ctx->file.size = (int)file_size; ctx->file.size = (int)size;
#ifdef USE_MMAP #ifdef USE_MMAP
ctx->file.ptr = (char*)tfMmapReadOnly(ctx->file.pFile, ctx->file.size); ctx->file.ptr = (char*)tfMmapReadOnly(ctx->file.pFile, ctx->file.size);
#endif #endif
} }
memcpy(ctx->file.buf, path, strlen(path));
if (ctx->file.pFile == NULL) { if (ctx->file.pFile == NULL) {
indexError("failed to open file, error %d", errno); indexError("failed to open file, error %d", errno);
goto END; goto END;
} }
} else if (ctx->type == TMemory) { } else if (ctx->type == TMemory) {
ctx->mem.buf = taosMemoryCalloc(1, sizeof(char) * capacity); ctx->mem.buf = taosMemoryCalloc(1, sizeof(char) * capacity);
ctx->mem.capa = capacity; ctx->mem.cap = capacity;
} }
ctx->write = idxFileCtxDoWrite; ctx->write = idxFileCtxDoWrite;
ctx->read = idxFileCtxDoRead; ctx->read = idxFileCtxDoRead;
ctx->flush = idxFileCtxDoFlush; ctx->flush = idxFileCtxDoFlush;
ctx->readFrom = idxFileCtxDoReadFrom; ctx->readFrom = idxFileCtxDoReadFrom;
ctx->size = writeCtxGetSize; ctx->size = idxFileCtxGetSize;
ctx->offset = 0; ctx->offset = 0;
ctx->limit = capacity; ctx->limit = capacity;

View File

@ -75,7 +75,6 @@ CompiledAddr unpackDelta(char* data, uint64_t len, uint64_t nodeAddr) {
} }
// fst slice func // fst slice func
//
FstSlice fstSliceCreate(uint8_t* data, uint64_t len) { FstSlice fstSliceCreate(uint8_t* data, uint64_t len) {
FstString* str = (FstString*)taosMemoryMalloc(sizeof(FstString)); FstString* str = (FstString*)taosMemoryMalloc(sizeof(FstString));

View File

@ -513,7 +513,7 @@ TFileReader* tfileReaderOpen(char* path, uint64_t suid, int64_t version, const c
indexError("failed to open readonly file: %s, reason: %s", fullname, terrstr()); indexError("failed to open readonly file: %s, reason: %s", fullname, terrstr());
return NULL; return NULL;
} }
indexTrace("open read file name:%s, file size: %d", wc->file.buf, wc->file.size); indexTrace("open read file name:%s, file size: %" PRId64 "", wc->file.buf, wc->file.size);
TFileReader* reader = tfileReaderCreate(wc); TFileReader* reader = tfileReaderCreate(wc);
return reader; return reader;
@ -905,8 +905,9 @@ static int tfileReaderLoadFst(TFileReader* reader) {
int64_t ts = taosGetTimestampUs(); int64_t ts = taosGetTimestampUs();
int32_t nread = ctx->readFrom(ctx, buf, fstSize, reader->header.fstOffset); int32_t nread = ctx->readFrom(ctx, buf, fstSize, reader->header.fstOffset);
int64_t cost = taosGetTimestampUs() - ts; int64_t cost = taosGetTimestampUs() - ts;
indexInfo("nread = %d, and fst offset=%d, fst size: %d, filename: %s, file size: %d, time cost: %" PRId64 "us", nread, indexInfo("nread = %d, and fst offset=%d, fst size: %d, filename: %s, file size: %" PRId64 ", time cost: %" PRId64
reader->header.fstOffset, fstSize, ctx->file.buf, ctx->file.size, cost); "us",
nread, reader->header.fstOffset, fstSize, ctx->file.buf, ctx->file.size, cost);
// we assuse fst size less than FST_MAX_SIZE // we assuse fst size less than FST_MAX_SIZE
assert(nread > 0 && nread <= fstSize); assert(nread > 0 && nread <= fstSize);