diff --git a/src/tsdb/src/tsdbRead.c b/src/tsdb/src/tsdbRead.c index 4f47d9d7b2de463ed458f11e952585ebbb39df53..e559839e3e48f953bbb2890f56d92b820411bdf5 100644 --- a/src/tsdb/src/tsdbRead.c +++ b/src/tsdb/src/tsdbRead.c @@ -189,6 +189,8 @@ static void* destroyTableCheckInfo(SArray* pTableCheckInfo); static bool tsdbGetExternalRow(TsdbQueryHandleT pHandle); static int32_t tsdbQueryTableList(STable* pTable, SArray* pRes, void* filterInfo); static STableBlockInfo* moveToNextDataBlockInCurrentFile(STsdbQueryHandle* pQueryHandle); +static bool initTableMemIterator(STsdbQueryHandle* pHandle, STableCheckInfo* pCheckInfo); +static SMemRow getSMemRowInTableMem(STableCheckInfo* pCheckInfo, int32_t order, int32_t update, SMemRow* extraRow); static void tsdbInitDataBlockLoadInfo(SDataBlockLoadInfo* pBlockLoadInfo) { pBlockLoadInfo->slot = -1; @@ -632,12 +634,53 @@ static int32_t lazyLoadCacheLast(STsdbQueryHandle* pQueryHandle) { if (pTable->cacheLastConfigVersion == pRepo->cacheLastConfigVersion) { continue; } + + if (!pCheckInfo->initBuf) { + initTableMemIterator(pQueryHandle, pCheckInfo); + } + code = tsdbLoadLastCache(pRepo, pTable); if (code != 0) { tsdbError("%p uid:%" PRId64 ", tid:%d, failed to load last cache since %s", pQueryHandle, pTable->tableId.uid, pTable->tableId.tid, tstrerror(terrno)); break; } + + STsdbCfg *pCfg = &pQueryHandle->pTsdb->config; + bool cacheLastRow = CACHE_LAST_ROW(&(pRepo->config)); + if (!cacheLastRow) continue; + + SMemRow row = getSMemRowInTableMem(pCheckInfo, pQueryHandle->order, pCfg->update, NULL); + if (row == NULL) continue; + TSKEY key = memRowKey(row); + + if (!pTable->lastRow) { + STSchema *pSchema = tsdbGetTableSchema(pTable); + SMemRow lastRow = taosTMalloc(memRowMaxBytesFromSchema(pSchema)); + if (lastRow == NULL) { + return TSDB_CODE_TDB_OUT_OF_MEMORY; + } + + memRowCpy(lastRow, row); + pTable->lastRow = lastRow; + pTable->lastKey = key; + } else { + TSKEY lastRowKey = memRowKey(pTable->lastRow); + if (key <= lastRowKey) continue; + + if (memRowTLen(pTable->lastRow) < memRowTLen(row)) { + SMemRow lastRow = taosTRealloc(pTable->lastRow, memRowTLen(row)); + if (lastRow == NULL) { + taosTZfree(pTable->lastRow); + pTable->lastRow = NULL; + + return TSDB_CODE_TDB_OUT_OF_MEMORY; + } + pTable->lastRow = lastRow; + } + memRowCpy(pTable->lastRow, row); + pTable->lastKey = key; + } } return code;