diff --git a/src/tsdb/inc/tsdbMain.h b/src/tsdb/inc/tsdbMain.h index 45eb25ee154f9e3c91d19fca8b3c27bda39f3799..49275541ad7eab6bc56f11a5121f5b1e9e20c99e 100644 --- a/src/tsdb/inc/tsdbMain.h +++ b/src/tsdb/inc/tsdbMain.h @@ -66,10 +66,8 @@ typedef struct STable { SSkipList* pIndex; // For TSDB_SUPER_TABLE, it is the skiplist index void* eventHandler; // TODO void* streamHandler; // TODO - union { - TSKEY lastKey; - SDataRow lastRow; - }; + TSKEY lastKey; + SDataRow lastRow; char* sql; void* cqhandle; SRWLatch latch; // TODO: implementa latch functions @@ -439,16 +437,9 @@ static FORCE_INLINE STSchema *tsdbGetTableTagSchema(STable *pTable) { } } -static FORCE_INLINE TSKEY tsdbGetTableLastKeyImpl(STable* pTable, bool cacheLastRow) { - if (cacheLastRow) { - if (pTable->lastRow == NULL) { - return TSKEY_INITIAL_VAL; - } else { - return dataRowKey(pTable->lastRow); - } - } else { - return pTable->lastKey; - } +static FORCE_INLINE TSKEY tsdbGetTableLastKeyImpl(STable* pTable) { + ASSERT(pTable->lastRow == NULL || pTable->lastKey == dataRowKey(pTable->lastRow)); + return pTable->lastKey; } // ------------------ tsdbBuffer.c diff --git a/src/tsdb/src/tsdbMain.c b/src/tsdb/src/tsdbMain.c index cc1ea07554ead0d0f26a76b1e0651f577216aa3d..b34b2fa9e6d867527d98036da7972b50c5471c4b 100644 --- a/src/tsdb/src/tsdbMain.c +++ b/src/tsdb/src/tsdbMain.c @@ -722,8 +722,9 @@ static int tsdbRestoreInfo(STsdbRepo *pRepo) { // TODO if (tsdbSetHelperTable(&rhelper, pTable, pRepo) < 0) goto _err; SCompIdx *pIdx = &(rhelper.curCompIdx); - TSKEY lastKey = tsdbGetTableLastKeyImpl(pTable, pCfg->cacheLastRow); + TSKEY lastKey = tsdbGetTableLastKeyImpl(pTable); if (pIdx->offset > 0 && lastKey < pIdx->maxKey) { + pTable->lastKey = pIdx->maxKey; if (pCfg->cacheLastRow) { // load the block of data if (tsdbLoadCompInfo(&rhelper, NULL) < 0) goto _err; @@ -745,8 +746,6 @@ static int tsdbRestoreInfo(STsdbRepo *pRepo) { // TODO tdAppendColVal(pTable->lastRow, tdGetColDataOfRow(pDataCol, pBlock->numOfRows - 1), pCol->type, pCol->bytes, pCol->offset); } - } else { - pTable->lastKey = pIdx->maxKey; } } } diff --git a/src/tsdb/src/tsdbMemTable.c b/src/tsdb/src/tsdbMemTable.c index 9582c0b49e69d7515fe11b5c9519e59b2a9656ac..0c9d450824fd63c300e9d246f4171f2fbb7b3ff1 100644 --- a/src/tsdb/src/tsdbMemTable.c +++ b/src/tsdb/src/tsdbMemTable.c @@ -664,7 +664,7 @@ static int tsdbCopyRowToMem(STsdbRepo *pRepo, SDataRow row, STable *pTable, void return -1; } - TSKEY lastKey = tsdbGetTableLastKeyImpl(pTable, pCfg->cacheLastRow); + TSKEY lastKey = tsdbGetTableLastKeyImpl(pTable); if (key > lastKey) { tsdbTrace("vgId:%d skip to delete row key %" PRId64 " which is larger than table lastKey %" PRId64, REPO_ID(pRepo), key, lastKey); @@ -898,8 +898,9 @@ static void tsdbFreeRows(STsdbRepo *pRepo, void **rows, int rowCounter) { static int tsdbUpdateTableLatestInfo(STsdbRepo *pRepo, STable *pTable, SDataRow row) { STsdbCfg *pCfg = &pRepo->config; - if (tsdbGetTableLastKeyImpl(pTable, pCfg->cacheLastRow) < dataRowKey(row)) { - if (pCfg->cacheLastRow) { + if (tsdbGetTableLastKeyImpl(pTable) < dataRowKey(row)) { + pTable->lastKey = dataRowKey(row); + if (pCfg->cacheLastRow || pTable->lastRow != NULL) { SDataRow nrow = pTable->lastRow; if (taosTSizeof(nrow) < dataRowLen(row)) { SDataRow orow = nrow; @@ -919,8 +920,6 @@ static int tsdbUpdateTableLatestInfo(STsdbRepo *pRepo, STable *pTable, SDataRow dataRowCpy(nrow, row); TSDB_WUNLOCK_TABLE(pTable); } - } else { - pTable->lastKey = dataRowKey(row); } } diff --git a/src/tsdb/src/tsdbMeta.c b/src/tsdb/src/tsdbMeta.c index 17f5e7052e2625d71ba50d7d61baba2a3c32e279..7b08178f4949eff0c4e8802f1d04cf1c85554c5b 100644 --- a/src/tsdb/src/tsdbMeta.c +++ b/src/tsdb/src/tsdbMeta.c @@ -663,7 +663,7 @@ static STable *tsdbNewTable() { return NULL; } - // pTable->lastKey = TSKEY_INITIAL_VAL; + pTable->lastKey = TSKEY_INITIAL_VAL; return pTable; } @@ -775,6 +775,7 @@ static void tsdbFreeTable(STable *pTable) { kvRowFree(pTable->tagVal); tSkipListDestroy(pTable->pIndex); + taosTZfree(pTable->lastRow); tfree(pTable->sql); free(pTable); } @@ -782,13 +783,6 @@ static void tsdbFreeTable(STable *pTable) { static int tsdbAddTableToMeta(STsdbRepo *pRepo, STable *pTable, bool addIdx, bool lock) { STsdbMeta *pMeta = pRepo->tsdbMeta; - STsdbCfg * pCfg = &(pRepo->config); - - if (pCfg->cacheLastRow) { - pTable->lastRow = NULL; - } else { - pTable->lastKey = TSKEY_INITIAL_VAL; - } if (lock && tsdbWLockRepoMeta(pRepo) < 0) { tsdbError("vgId:%d failed to add table %s to meta since %s", REPO_ID(pRepo), TABLE_CHAR_NAME(pTable),