From 305523f47ab9710f81624590fa3144c6e25a4c0a Mon Sep 17 00:00:00 2001 From: hzcheng Date: Tue, 24 Mar 2020 15:10:41 +0800 Subject: [PATCH] TD-34 --- src/vnode/tsdb/inc/tsdbFile.h | 5 ---- src/vnode/tsdb/src/tsdbMain.c | 50 ++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/vnode/tsdb/inc/tsdbFile.h b/src/vnode/tsdb/inc/tsdbFile.h index 7f3acb6624..385d1f59ea 100644 --- a/src/vnode/tsdb/inc/tsdbFile.h +++ b/src/vnode/tsdb/inc/tsdbFile.h @@ -36,11 +36,6 @@ typedef enum { extern const char *tsdbFileSuffix[]; -typedef struct { - int64_t size; - int64_t tombSize; -} SFileInfo; - typedef struct { int8_t type; char fname[128]; diff --git a/src/vnode/tsdb/src/tsdbMain.c b/src/vnode/tsdb/src/tsdbMain.c index 2df7974844..afd57947b7 100644 --- a/src/vnode/tsdb/src/tsdbMain.c +++ b/src/vnode/tsdb/src/tsdbMain.c @@ -782,6 +782,39 @@ static int tsdbReadRowsFromCache(SSkipListIterator *pIter, TSKEY maxKey, int max return numOfRows; } +static void tsdbDestroyTableIters(SSkipListIterator **iters, int maxTables) { + if (iters == NULL) return; + + for (int tid = 0; tid < maxTables; tid++) { + if (iters[tid] == NULL) continue; + tSkipListDestroy(iters[tid]); + } + + free(iters); +} + +static SSkipListIterator **tsdbCreateTableIters(STsdbMeta *pMeta, int maxTables) { + SSkipListIterator **iters = (SSkipListIterator *)calloc(maxTables, sizeof(SSkipListIterator *)); + if (iters == NULL) return NULL; + + for (int tid = 0; tid < maxTables; tid++) { + STable *pTable = pMeta->tables[tid]; + if (pTable == NULL || pTable->imem == NULL) continue; + + iters[tid] = tSkipListCreateIter(pTable->imem->pData); + if (iters[tid] == NULL) { + tsdbDestroyTableIters(iters, maxTables); + return NULL; + } + + if (!tSkipListIterNext(iters[tid])) { + assert(false); + } + } + + return iters; +} + // Commit to file static void *tsdbCommitToFile(void *arg) { // TODO @@ -791,10 +824,8 @@ static void *tsdbCommitToFile(void *arg) { STsdbCfg * pCfg = &(pRepo->config); if (pCache->imem == NULL) return; - int sfid = tsdbGetKeyFileId(pCache->imem->keyFirst, pCfg->daysPerFile, pCfg->precision); - int efid = tsdbGetKeyFileId(pCache->imem->keyLast, pCfg->daysPerFile, pCfg->precision); - - SSkipListIterator **iters = (SSkipListIterator **)calloc(pCfg->maxTables, sizeof(SSkipListIterator *)); + // Create the iterator to read from cache + SSkipListIterator **iters = tsdbCreateTableIters(pMeta, pCfg->maxTables); if (iters == NULL) { // TODO: deal with the error return NULL; @@ -805,10 +836,15 @@ static void *tsdbCommitToFile(void *arg) { SDataCol **cols = (SDataCol **)malloc(sizeof(SDataCol *) * maxCols); void *buf = malloc((maxBytes + sizeof(SDataCol)) * pCfg->maxRowsPerFileBlock); + int sfid = tsdbGetKeyFileId(pCache->imem->keyFirst, pCfg->daysPerFile, pCfg->precision); + int efid = tsdbGetKeyFileId(pCache->imem->keyLast, pCfg->daysPerFile, pCfg->precision); + for (int fid = sfid; fid <= efid; fid++) { TSKEY minKey = 0, maxKey = 0; tsdbGetKeyRangeOfFileId(pCfg->daysPerFile, pCfg->precision, fid, &minKey, &maxKey); + // tsdbOpenFileForWrite(pRepo, fid); + for (int tid = 0; tid < pCfg->maxTables; tid++) { STable *pTable = pMeta->tables[tid]; if (pTable == NULL || pTable->imem == NULL) continue; @@ -837,14 +873,10 @@ static void *tsdbCommitToFile(void *arg) { } } - // Free the iterator - for (int tid = 0; tid < pCfg->maxTables; tid++) { - if (iters[tid] != NULL) tSkipListDestroyIter(iters[tid]); - } + tsdbDestroyTableIters(iters, pCfg->maxTables); free(buf); free(cols); - free(iters); tsdbLockRepo(arg); tdListMove(pCache->imem->list, pCache->pool.memPool); -- GitLab