tsdbCache.c 33.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "tsdb.h"

int32_t tsdbOpenCache(STsdb *pTsdb) {
H
Hongze Cheng 已提交
19
  int32_t    code = 0;
20
  SLRUCache *pCache = NULL;
21 22
  // TODO: get cfg from vnode config: pTsdb->pVnode->config.lruCapacity
  size_t cfgCapacity = 1024 * 1024;
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

  pCache = taosLRUCacheInit(cfgCapacity, -1, .5);
  if (pCache == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  taosLRUCacheSetStrictCapacity(pCache, true);

_err:
  pTsdb->lruCache = pCache;
  return code;
}

void tsdbCloseCache(SLRUCache *pCache) {
  if (pCache) {
    taosLRUCacheEraseUnrefEntries(pCache);

    taosLRUCacheCleanup(pCache);
  }
}

45
static void getTableCacheKeyS(tb_uid_t uid, const char *cacheType, char *key, int *len) {
H
Hongze Cheng 已提交
46
  snprintf(key, 30, "%" PRIi64 "%s", uid, cacheType);
47 48 49
  *len = strlen(key);
}

50 51 52 53 54 55 56 57 58 59
static void getTableCacheKey(tb_uid_t uid, int cacheType, char *key, int *len) {
  if (cacheType == 0) {  // last_row
    *(uint64_t *)key = (uint64_t)uid;
  } else {  // last
    *(uint64_t *)key = ((uint64_t)uid) | 0x8000000000000000;
  }

  *len = sizeof(uint64_t);
}

H
Hongze Cheng 已提交
60
static void deleteTableCacheLastrow(const void *key, size_t keyLen, void *value) { taosMemoryFree(value); }
61

62 63
static void deleteTableCacheLast(const void *key, size_t keyLen, void *value) { taosArrayDestroy(value); }

64 65 66 67 68
static int32_t tsdbCacheDeleteLastrow(SLRUCache *pCache, tb_uid_t uid, TSKEY eKey) {
  int32_t code = 0;
  char    key[32] = {0};
  int     keyLen = 0;

69 70
  // getTableCacheKey(uid, "lr", key, &keyLen);
  getTableCacheKey(uid, 0, key, &keyLen);
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  LRUHandle *h = taosLRUCacheLookup(pCache, key, keyLen);
  if (h) {
    STSRow *pRow = (STSRow *)taosLRUCacheValue(pCache, h);
    if (pRow->ts <= eKey) {
      taosLRUCacheRelease(pCache, h, true);
    } else {
      taosLRUCacheRelease(pCache, h, false);
    }

    // void taosLRUCacheErase(SLRUCache * cache, const void *key, size_t keyLen);
  }

  return code;
}

static int32_t tsdbCacheDeleteLast(SLRUCache *pCache, tb_uid_t uid, TSKEY eKey) {
  int32_t code = 0;
  char    key[32] = {0};
  int     keyLen = 0;

91 92
  // getTableCacheKey(uid, "l", key, &keyLen);
  getTableCacheKey(uid, 1, key, &keyLen);
93 94 95 96 97 98 99 100 101 102 103
  LRUHandle *h = taosLRUCacheLookup(pCache, key, keyLen);
  if (h) {
    // clear last cache anyway, no matter where eKey ends.
    taosLRUCacheRelease(pCache, h, true);

    // void taosLRUCacheErase(SLRUCache * cache, const void *key, size_t keyLen);
  }

  return code;
}

104
int32_t tsdbCacheInsertLastrow(SLRUCache *pCache, STsdb *pTsdb, tb_uid_t uid, STSRow *row, bool dup) {
105 106
  int32_t code = 0;
  STSRow *cacheRow = NULL;
H
Hongze Cheng 已提交
107 108
  char    key[32] = {0};
  int     keyLen = 0;
109

110 111
  // getTableCacheKey(uid, "lr", key, &keyLen);
  getTableCacheKey(uid, 0, key, &keyLen);
112 113
  LRUHandle *h = taosLRUCacheLookup(pCache, key, keyLen);
  if (h) {
H
Hongze Cheng 已提交
114
    cacheRow = (STSRow *)taosLRUCacheValue(pCache, h);
115
    if (row->ts >= cacheRow->ts) {
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
      if (row->ts == cacheRow->ts) {
        STSRow    *mergedRow;
        SRowMerger merger = {0};
        STSchema  *pTSchema = metaGetTbTSchema(pTsdb->pVnode->pMeta, uid, -1);

        tRowMergerInit(&merger, &tsdbRowFromTSRow(0, cacheRow), pTSchema);

        tRowMerge(&merger, &tsdbRowFromTSRow(1, row));

        tRowMergerGetRow(&merger, &mergedRow);
        tRowMergerClear(&merger);

        taosMemoryFreeClear(pTSchema);

        row = mergedRow;
        dup = false;
      }

134
      if (TD_ROW_LEN(row) <= TD_ROW_LEN(cacheRow)) {
H
Hongze Cheng 已提交
135
        tdRowCpy(cacheRow, row);
136 137 138
        if (!dup) {
          taosMemoryFree(row);
        }
139 140

        taosLRUCacheRelease(pCache, h, false);
141
      } else {
142 143
        taosLRUCacheRelease(pCache, h, true);
        // tsdbCacheDeleteLastrow(pCache, uid, TSKEY_MAX);
M
Minglei Jin 已提交
144 145 146 147 148
        if (dup) {
          cacheRow = tdRowDup(row);
        } else {
          cacheRow = row;
        }
149 150 151 152 153 154 155
        _taos_lru_deleter_t deleter = deleteTableCacheLastrow;
        LRUStatus status = taosLRUCacheInsert(pCache, key, keyLen, cacheRow, TD_ROW_LEN(cacheRow), deleter, NULL,
                                              TAOS_LRU_PRIORITY_LOW);
        if (status != TAOS_LRU_STATUS_OK) {
          code = -1;
        }
        /* tsdbCacheInsertLastrow(pCache, uid, row, dup); */
156 157 158
      }
    }
  } else {
159 160 161 162 163
    if (dup) {
      cacheRow = tdRowDup(row);
    } else {
      cacheRow = row;
    }
164 165

    _taos_lru_deleter_t deleter = deleteTableCacheLastrow;
H
Hongze Cheng 已提交
166 167
    LRUStatus           status =
        taosLRUCacheInsert(pCache, key, keyLen, cacheRow, TD_ROW_LEN(cacheRow), deleter, NULL, TAOS_LRU_PRIORITY_LOW);
168 169 170 171 172 173 174 175
    if (status != TAOS_LRU_STATUS_OK) {
      code = -1;
    }
  }

  return code;
}

176 177 178 179 180 181
int32_t tsdbCacheInsertLast(SLRUCache *pCache, tb_uid_t uid, STSRow *row) {
  int32_t code = 0;
  STSRow *cacheRow = NULL;
  char    key[32] = {0};
  int     keyLen = 0;

182 183
  ((void)(row));

184 185
  // getTableCacheKey(uid, "l", key, &keyLen);
  getTableCacheKey(uid, 1, key, &keyLen);
186 187
  LRUHandle *h = taosLRUCacheLookup(pCache, key, keyLen);
  if (h) {
188 189
    // clear last cache anyway, lazy load when get last lookup
    taosLRUCacheRelease(pCache, h, true);
190 191 192 193 194
  }

  return code;
}

195 196 197 198 199 200
static tb_uid_t getTableSuidByUid(tb_uid_t uid, STsdb *pTsdb) {
  tb_uid_t suid = 0;

  SMetaReader mr = {0};
  metaReaderInit(&mr, pTsdb->pVnode->pMeta, 0);
  if (metaGetTableEntryByUid(&mr, uid) < 0) {
H
Hongze Cheng 已提交
201
    metaReaderClear(&mr);  // table not esist
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    return 0;
  }

  if (mr.me.type == TSDB_CHILD_TABLE) {
    suid = mr.me.ctbEntry.suid;
  } else if (mr.me.type == TSDB_NORMAL_TABLE) {
    suid = 0;
  } else {
    suid = 0;
  }

  metaReaderClear(&mr);

  return suid;
}

static int32_t getTableDelDataFromDelIdx(SDelFReader *pDelReader, SDelIdx *pDelIdx, SArray *aDelData) {
  int32_t code = 0;

221 222
  // SMapData delDataMap;
  // SDelData delData;
223 224

  if (pDelIdx) {
225
    // tMapDataReset(&delDataMap);
226

227
    // code = tsdbReadDelData(pDelReader, pDelIdx, &delDataMap, NULL);
228
    code = tsdbReadDelData(pDelReader, pDelIdx, aDelData, NULL);
229
    if (code) goto _err;
230
    /*
231 232 233 234 235 236
    for (int32_t iDelData = 0; iDelData < delDataMap.nItem; ++iDelData) {
      code = tMapDataGetItemByIdx(&delDataMap, iDelData, &delData, tGetDelData);
      if (code) goto _err;

      taosArrayPush(aDelData, &delData);
    }
237
    */
238 239 240 241 242 243 244
  }

_err:
  return code;
}

static int32_t getTableDelDataFromTbData(STbData *pTbData, SArray *aDelData) {
H
Hongze Cheng 已提交
245
  int32_t   code = 0;
246 247 248 249 250 251 252 253 254
  SDelData *pDelData = pTbData ? pTbData->pHead : NULL;

  for (; pDelData; pDelData = pDelData->pNext) {
    taosArrayPush(aDelData, pDelData);
  }

  return code;
}

H
Hongze Cheng 已提交
255 256
static int32_t getTableDelData(STbData *pMem, STbData *pIMem, SDelFReader *pDelReader, SDelIdx *pDelIdx,
                               SArray *aDelData) {
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
  int32_t code = 0;

  if (pMem) {
    code = getTableDelDataFromTbData(pMem, aDelData);
    if (code) goto _err;
  }

  if (pIMem) {
    code = getTableDelDataFromTbData(pIMem, aDelData);
    if (code) goto _err;
  }

  if (pDelIdx) {
    code = getTableDelDataFromDelIdx(pDelReader, pDelIdx, aDelData);
    if (code) goto _err;
  }

_err:
  return code;
}

H
Hongze Cheng 已提交
278 279
static int32_t getTableDelSkyline(STbData *pMem, STbData *pIMem, SDelFReader *pDelReader, SDelIdx *pDelIdx,
                                  SArray *aSkyline) {
280
  int32_t code = 0;
M
Minglei Jin 已提交
281
  SArray *aDelData = NULL;
282

M
Minglei Jin 已提交
283
  aDelData = taosArrayInit(32, sizeof(SDelData));
284 285 286 287
  code = getTableDelData(pMem, pIMem, pDelReader, pDelIdx, aDelData);
  if (code) goto _err;

  size_t nDelData = taosArrayGetSize(aDelData);
288 289 290 291
  if (nDelData > 0) {
    code = tsdbBuildDeleteSkyline(aDelData, 0, (int32_t)(nDelData - 1), aSkyline);
    if (code) goto _err;
  }
292

M
Minglei Jin 已提交
293 294 295
  if (aDelData) {
    taosArrayDestroy(aDelData);
  }
296 297 298 299 300 301
_err:
  return code;
}

static int32_t getTableDelIdx(SDelFReader *pDelFReader, tb_uid_t suid, tb_uid_t uid, SDelIdx *pDelIdx) {
  int32_t code = 0;
M
Minglei Jin 已提交
302
  SArray *pDelIdxArray = NULL;
303

304
  // SMapData delIdxMap;
M
Minglei Jin 已提交
305
  pDelIdxArray = taosArrayInit(32, sizeof(SDelIdx));
306
  SDelIdx idx = {.suid = suid, .uid = uid};
307

308 309 310
  // tMapDataReset(&delIdxMap);
  //  code = tsdbReadDelIdx(pDelFReader, &delIdxMap, NULL);
  code = tsdbReadDelIdx(pDelFReader, pDelIdxArray, NULL);
311 312
  if (code) goto _err;

313
  // code = tMapDataSearch(&delIdxMap, &idx, tGetDelIdx, tCmprDelIdx, pDelIdx);
314
  SDelIdx *pIdx = taosArraySearch(pDelIdxArray, &idx, tCmprDelIdx, TD_EQ);
315 316
  if (code) goto _err;

317 318
  *pDelIdx = *pIdx;

M
Minglei Jin 已提交
319 320 321
  if (pDelIdxArray) {
    taosArrayDestroy(pDelIdxArray);
  }
322 323 324
_err:
  return code;
}
325

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
typedef enum SFSNEXTROWSTATES {
  SFSNEXTROW_FS,
  SFSNEXTROW_FILESET,
  SFSNEXTROW_BLOCKDATA,
  SFSNEXTROW_BLOCKROW
} SFSNEXTROWSTATES;

typedef struct SFSNextRowIter {
  SFSNEXTROWSTATES state;         // [input]
  STsdb           *pTsdb;         // [input]
  SBlockIdx       *pBlockIdxExp;  // [input]
  int32_t          nFileSet;
  int32_t          iFileSet;
  SArray          *aDFileSet;
  SDataFReader    *pDataFReader;
341 342 343
  SArray          *aBlockIdx;
  // SMapData         blockIdxMap;
  //  SBlockIdx  blockIdx;
344 345 346 347 348 349 350 351 352 353
  SBlockIdx  *pBlockIdx;
  SMapData    blockMap;
  int32_t     nBlock;
  int32_t     iBlock;
  SBlock      block;
  SBlockData  blockData;
  SBlockData *pBlockData;
  int32_t     nRow;
  int32_t     iRow;
  TSDBROW     row;
354 355 356 357 358 359 360 361 362 363
} SFSNextRowIter;

static int32_t getNextRowFromFS(void *iter, TSDBROW **ppRow) {
  SFSNextRowIter *state = (SFSNextRowIter *)iter;
  int32_t         code = 0;

  switch (state->state) {
    case SFSNEXTROW_FS:
      state->aDFileSet = state->pTsdb->fs->cState->aDFileSet;
      state->nFileSet = taosArrayGetSize(state->aDFileSet);
364 365 366
      state->iFileSet = state->nFileSet - 1;

      state->pBlockData = NULL;
367 368 369 370 371 372

    case SFSNEXTROW_FILESET: {
      SDFileSet *pFileSet = NULL;
      if (--state->iFileSet >= 0) {
        pFileSet = (SDFileSet *)taosArrayGet(state->aDFileSet, state->iFileSet);
      } else {
373 374 375 376 377
        // tBlockDataClear(&state->blockData);
        if (state->pBlockData) {
          tBlockDataClear(state->pBlockData);
          state->pBlockData = NULL;
        }
378

379 380 381 382 383 384
        *ppRow = NULL;
        return code;
      }

      code = tsdbDataFReaderOpen(&state->pDataFReader, state->pTsdb, pFileSet);
      if (code) goto _err;
385 386 387 388 389 390 391 392 393

      // tMapDataReset(&state->blockIdxMap);
      // code = tsdbReadBlockIdx(state->pDataFReader, &state->blockIdxMap, NULL);
      if (!state->aBlockIdx) {
        state->aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
      } else {
        taosArrayClear(state->aBlockIdx);
      }
      code = tsdbReadBlockIdx(state->pDataFReader, state->aBlockIdx, NULL);
394 395
      if (code) goto _err;

396 397 398 399
      /* if (state->pBlockIdx) { */
      /*   tBlockIdxReset(state->blockIdx); */
      /* } */
      /* tBlockIdxReset(state->blockIdx); */
400 401
      /* code = tMapDataSearch(&state->blockIdxMap, state->pBlockIdxExp, tGetBlockIdx, tCmprBlockIdx,
       * &state->blockIdx);
402 403
       */
      state->pBlockIdx = taosArraySearch(state->aBlockIdx, state->pBlockIdxExp, tCmprBlockIdx, TD_EQ);
404
      if (code) goto _err;
405

406
      tMapDataReset(&state->blockMap);
407 408
      code = tsdbReadBlock(state->pDataFReader, state->pBlockIdx, &state->blockMap, NULL);
      /* code = tsdbReadBlock(state->pDataFReader, &state->blockIdx, &state->blockMap, NULL); */
409 410 411 412
      if (code) goto _err;

      state->nBlock = state->blockMap.nItem;
      state->iBlock = state->nBlock - 1;
413

414 415 416 417 418
      if (!state->pBlockData) {
        state->pBlockData = &state->blockData;

        tBlockDataInit(&state->blockData);
      }
419 420 421 422 423 424
    }
    case SFSNEXTROW_BLOCKDATA:
      if (state->iBlock >= 0) {
        SBlock block = {0};

        tBlockReset(&block);
425 426
        // tBlockDataReset(&state->blockData);
        tBlockDataReset(state->pBlockData);
427 428

        tMapDataGetItemByIdx(&state->blockMap, state->iBlock, &block, tGetBlock);
429
        /* code = tsdbReadBlockData(state->pDataFReader, &state->blockIdx, &block, &state->blockData, NULL, NULL); */
430
        code = tsdbReadBlockData(state->pDataFReader, state->pBlockIdx, &block, state->pBlockData, NULL, NULL);
431 432 433 434 435 436 437 438 439
        if (code) goto _err;

        state->nRow = state->blockData.nRow;
        state->iRow = state->nRow - 1;

        state->state = SFSNEXTROW_BLOCKROW;
      }
    case SFSNEXTROW_BLOCKROW:
      if (state->iRow >= 0) {
440
        state->row = tsdbRowFromBlockData(state->pBlockData, state->iRow);
441 442 443 444 445 446
        *ppRow = &state->row;

        if (--state->iRow < 0) {
          state->state = SFSNEXTROW_BLOCKDATA;
          if (--state->iBlock < 0) {
            tsdbDataFReaderClose(&state->pDataFReader);
447 448
            state->pDataFReader = NULL;

449 450
            if (state->aBlockIdx) {
              taosArrayDestroy(state->aBlockIdx);
451
              state->aBlockIdx = NULL;
452
            }
453

454 455 456 457 458 459 460 461 462 463 464 465
            state->state = SFSNEXTROW_FILESET;
          }
        }
      }

      return code;
    default:
      ASSERT(0);
      break;
  }

_err:
466 467
  if (state->pDataFReader) {
    tsdbDataFReaderClose(&state->pDataFReader);
468
    state->pDataFReader = NULL;
469 470 471
  }
  if (state->aBlockIdx) {
    taosArrayDestroy(state->aBlockIdx);
472 473 474 475 476 477
    state->aBlockIdx = NULL;
  }
  if (state->pBlockData) {
    // tBlockDataClear(&state->blockData);
    tBlockDataClear(state->pBlockData);
    state->pBlockData = NULL;
478 479
  }

480
  *ppRow = NULL;
481

482 483 484
  return code;
}

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
int32_t clearNextRowFromFS(void *iter) {
  int32_t code = 0;

  SFSNextRowIter *state = (SFSNextRowIter *)iter;
  if (!state) {
    return code;
  }

  if (state->pDataFReader) {
    tsdbDataFReaderClose(&state->pDataFReader);
    state->pDataFReader = NULL;
  }
  if (state->aBlockIdx) {
    taosArrayDestroy(state->aBlockIdx);
    state->aBlockIdx = NULL;
  }
  if (state->pBlockData) {
    // tBlockDataClear(&state->blockData);
    tBlockDataClear(state->pBlockData);
    state->pBlockData = NULL;
  }

  return code;
}

510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
typedef enum SMEMNEXTROWSTATES {
  SMEMNEXTROW_ENTER,
  SMEMNEXTROW_NEXT,
} SMEMNEXTROWSTATES;

typedef struct SMemNextRowIter {
  SMEMNEXTROWSTATES state;
  STbData          *pMem;  // [input]
  STbDataIter       iter;  // mem buffer skip list iterator
} SMemNextRowIter;

static int32_t getNextRowFromMem(void *iter, TSDBROW **ppRow) {
  SMemNextRowIter *state = (SMemNextRowIter *)iter;
  int32_t          code = 0;

  switch (state->state) {
    case SMEMNEXTROW_ENTER: {
      if (state->pMem != NULL) {
        tsdbTbDataIterOpen(state->pMem, NULL, 1, &state->iter);

        TSDBROW *pMemRow = tsdbTbDataIterGet(&state->iter);
        if (pMemRow) {
          *ppRow = pMemRow;
          state->state = SMEMNEXTROW_NEXT;

          return code;
        }
      }

      *ppRow = NULL;

      return code;
    }
    case SMEMNEXTROW_NEXT:
      if (tsdbTbDataIterNext(&state->iter)) {
        *ppRow = tsdbTbDataIterGet(&state->iter);

        return code;
      } else {
        *ppRow = NULL;

        return code;
      }
    default:
      ASSERT(0);
      break;
  }

_err:
  *ppRow = NULL;
  return code;
}

563 564 565 566 567
static int32_t tsRowFromTsdbRow(STSchema *pTSchema, TSDBROW *pRow, STSRow **ppRow) {
  int32_t code = 0;

  SColVal *pColVal = &(SColVal){0};

568
  if (pRow->type == 0) {
569
    *ppRow = tdRowDup(pRow->pTSRow);
570
  } else {
571 572 573 574 575 576
    SArray *pArray = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
    if (pArray == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }

577 578 579 580 581 582 583 584 585
    TSDBKEY   key = TSDBROW_KEY(pRow);
    STColumn *pTColumn = &pTSchema->columns[0];
    *pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = key.ts});

    if (taosArrayPush(pArray, pColVal) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }

586 587 588 589 590 591 592 593 594 595
    for (int16_t iCol = 1; iCol < pTSchema->numOfCols; iCol++) {
      tsdbRowGetColVal(pRow, pTSchema, iCol, pColVal);
      if (taosArrayPush(pArray, pColVal) == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _exit;
      }
    }

    code = tdSTSRowNew(pArray, pTSchema, ppRow);
    if (code) goto _exit;
596 597
  }

598 599
_exit:
  return code;
600 601
}

602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
static bool tsdbKeyDeleted(TSDBKEY *key, SArray *pSkyline, int *iSkyline) {
  bool deleted = false;
  while (*iSkyline > 0) {
    TSDBKEY *pItemBack = (TSDBKEY *)taosArrayGet(pSkyline, *iSkyline);
    TSDBKEY *pItemFront = (TSDBKEY *)taosArrayGet(pSkyline, *iSkyline - 1);

    if (key->ts > pItemBack->ts) {
      return false;
    } else if (key->ts >= pItemFront->ts && key->ts <= pItemBack->ts) {
      if ((key->version <= pItemFront->version || key->ts == pItemBack->ts && key->version <= pItemBack->version)) {
        return true;
      } else {
        return false;
      }
    } else {
      if (*iSkyline > 1) {
        --*iSkyline;
      } else {
        return false;
      }
    }
  }

  return deleted;
}

628
typedef int32_t (*_next_row_fn_t)(void *iter, TSDBROW **ppRow);
629
typedef int32_t (*_next_row_clear_fn_t)(void *iter);
630 631

typedef struct TsdbNextRowState {
632 633 634 635 636 637
  TSDBROW             *pRow;
  bool                 stop;
  bool                 next;
  void                *iter;
  _next_row_fn_t       nextRowFn;
  _next_row_clear_fn_t nextRowClearFn;
638
} TsdbNextRowState;
639

640
static int32_t mergeLastRow(tb_uid_t uid, STsdb *pTsdb, bool *dup, STSRow **ppRow) {
641
  int32_t code = 0;
642
  SArray *pSkyline = NULL;
643

M
Minglei Jin 已提交
644 645 646 647
  STSchema *pTSchema = metaGetTbTSchema(pTsdb->pVnode->pMeta, uid, -1);
  int16_t   nCol = pTSchema->numOfCols;
  SArray   *pColArray = taosArrayInit(nCol, sizeof(SColVal));

648 649
  tb_uid_t suid = getTableSuidByUid(uid, pTsdb);

650
  STbData *pMem = NULL;
651 652 653 654
  if (pTsdb->mem) {
    tsdbGetTbDataFromMemTable(pTsdb->mem, suid, uid, &pMem);
  }

655
  STbData *pIMem = NULL;
656 657 658 659 660 661
  if (pTsdb->imem) {
    tsdbGetTbDataFromMemTable(pTsdb->imem, suid, uid, &pIMem);
  }

  *ppRow = NULL;

662
  pSkyline = taosArrayInit(32, sizeof(TSDBKEY));
663

M
Minglei Jin 已提交
664 665
  SDelIdx delIdx;

666 667 668
  SDelFile *pDelFile = tsdbFSStateGetDelFile(pTsdb->fs->cState);
  if (pDelFile) {
    SDelFReader *pDelFReader;
669

670 671
    code = tsdbDelFReaderOpen(&pDelFReader, pDelFile, pTsdb, NULL);
    if (code) goto _err;
672

673 674 675 676 677
    code = getTableDelIdx(pDelFReader, suid, uid, &delIdx);
    if (code) goto _err;

    code = getTableDelSkyline(pMem, pIMem, pDelFReader, &delIdx, pSkyline);
    if (code) goto _err;
678

H
Hongze Cheng 已提交
679
    tsdbDelFReaderClose(&pDelFReader);
M
Minglei Jin 已提交
680 681 682
  } else {
    code = getTableDelSkyline(pMem, pIMem, NULL, NULL, pSkyline);
    if (code) goto _err;
683 684 685
  }

  int iSkyline = taosArrayGetSize(pSkyline) - 1;
686 687 688 689 690 691 692

  SBlockIdx idx = {.suid = suid, .uid = uid};

  SFSNextRowIter fsState = {0};
  fsState.state = SFSNEXTROW_FS;
  fsState.pTsdb = pTsdb;
  fsState.pBlockIdxExp = &idx;
H
Hongze Cheng 已提交
693

694 695 696 697
  SMemNextRowIter memState = {0};
  SMemNextRowIter imemState = {0};
  TSDBROW         memRow, imemRow, fsRow;

698 699 700
  TsdbNextRowState input[3] = {{&memRow, true, false, &memState, getNextRowFromMem, NULL},
                               {&imemRow, true, false, &imemState, getNextRowFromMem, NULL},
                               {&fsRow, false, true, &fsState, getNextRowFromFS, clearNextRowFromFS}};
701 702 703 704 705 706 707 708 709 710 711 712

  if (pMem) {
    memState.pMem = pMem;
    memState.state = SMEMNEXTROW_ENTER;
    input[0].stop = false;
    input[0].next = true;
  }
  if (pIMem) {
    imemState.pMem = pIMem;
    imemState.state = SMEMNEXTROW_ENTER;
    input[1].stop = false;
    input[1].next = true;
H
Hongze Cheng 已提交
713
  }
714

M
Minglei Jin 已提交
715 716 717
  int16_t nilColCount = nCol - 1;  // count of null & none cols
  int     iCol = 0;                // index of first nil col index from left to right
  bool    setICol = false;
718

719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
  do {
    for (int i = 0; i < 3; ++i) {
      if (input[i].next && !input[i].stop) {
        code = input[i].nextRowFn(input[i].iter, &input[i].pRow);
        if (code) goto _err;

        if (input[i].pRow == NULL) {
          input[i].stop = true;
          input[i].next = false;
        }
      }
    }

    if (input[0].stop && input[1].stop && input[2].stop) {
      break;
    }

    // select maxpoint(s) from mem, imem, fs
    TSDBROW *max[3] = {0};
    int      iMax[3] = {-1, -1, -1};
    int      nMax = 0;
M
Minglei Jin 已提交
740 741
    TSKEY    maxKey = TSKEY_MIN;

742
    for (int i = 0; i < 3; ++i) {
M
Minglei Jin 已提交
743
      if (!input[i].stop && input[i].pRow != NULL) {
744 745 746
        TSDBKEY key = TSDBROW_KEY(input[i].pRow);

        // merging & deduplicating on client side
M
Minglei Jin 已提交
747 748
        if (maxKey <= key.ts) {
          if (maxKey < key.ts) {
749
            nMax = 0;
M
Minglei Jin 已提交
750
            maxKey = key.ts;
751 752 753 754 755 756 757 758 759 760
          }

          iMax[nMax] = i;
          max[nMax++] = input[i].pRow;
        }
      }
    }

    // delete detection
    TSDBROW *merge[3] = {0};
761 762
    // int      iMerge[3] = {-1, -1, -1};
    int nMerge = 0;
763 764 765
    for (int i = 0; i < nMax; ++i) {
      TSDBKEY maxKey = TSDBROW_KEY(max[i]);

766
      bool deleted = tsdbKeyDeleted(&maxKey, pSkyline, &iSkyline);
767
      if (!deleted) {
768
        // iMerge[nMerge] = i;
769 770
        merge[nMerge++] = max[i];
      }
771 772

      input[iMax[i]].next = deleted;
773 774 775 776
    }

    // merge if nMerge > 1
    if (nMerge > 0) {
777 778
      *dup = false;

779
      if (nMerge == 1) {
780 781
        code = tsRowFromTsdbRow(pTSchema, merge[nMerge - 1], ppRow);
        if (code) goto _err;
782 783 784 785 786 787 788 789 790 791 792 793
      } else {
        // merge 2 or 3 rows
        SRowMerger merger = {0};

        tRowMergerInit(&merger, merge[0], pTSchema);
        for (int i = 1; i < nMerge; ++i) {
          tRowMerge(&merger, merge[i]);
        }
        tRowMergerGetRow(&merger, ppRow);
        tRowMergerClear(&merger);
      }
    }
M
Minglei Jin 已提交
794

795
  } while (*ppRow == NULL);
796

797 798 799 800 801 802 803 804
  for (int i = 0; i < 3; ++i) {
    if (input[i].nextRowClearFn) {
      input[i].nextRowClearFn(input[i].iter);
    }
  }
  if (pSkyline) {
    taosArrayDestroy(pSkyline);
  }
805
  taosMemoryFreeClear(pTSchema);
806

807
  return code;
808
_err:
M
Minglei Jin 已提交
809 810 811 812 813
  for (int i = 0; i < 3; ++i) {
    if (input[i].nextRowClearFn) {
      input[i].nextRowClearFn(input[i].iter);
    }
  }
814 815 816
  if (pSkyline) {
    taosArrayDestroy(pSkyline);
  }
817
  taosMemoryFreeClear(pTSchema);
818 819 820 821
  tsdbError("vgId:%d merge last_row failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  return code;
}

822 823 824 825 826 827 828 829 830 831 832
typedef struct {
  TSKEY   ts;
  SColVal colVal;
} SLastCol;

// static int32_t mergeLast(tb_uid_t uid, STsdb *pTsdb, STSRow **ppRow) {
static int32_t mergeLast(tb_uid_t uid, STsdb *pTsdb, SArray **ppLastArray) {
  int32_t  code = 0;
  SArray  *pSkyline = NULL;
  STSRow  *pRow = NULL;
  STSRow **ppRow = &pRow;
M
Minglei Jin 已提交
833 834 835

  STSchema *pTSchema = metaGetTbTSchema(pTsdb->pVnode->pMeta, uid, -1);
  int16_t   nCol = pTSchema->numOfCols;
836 837
  //  SArray   *pColArray = taosArrayInit(nCol, sizeof(SColVal));
  SArray *pColArray = taosArrayInit(nCol, sizeof(SLastCol));
M
Minglei Jin 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850

  tb_uid_t suid = getTableSuidByUid(uid, pTsdb);

  STbData *pMem = NULL;
  if (pTsdb->mem) {
    tsdbGetTbDataFromMemTable(pTsdb->mem, suid, uid, &pMem);
  }

  STbData *pIMem = NULL;
  if (pTsdb->imem) {
    tsdbGetTbDataFromMemTable(pTsdb->imem, suid, uid, &pIMem);
  }

851
  *ppLastArray = NULL;
M
Minglei Jin 已提交
852

853
  pSkyline = taosArrayInit(32, sizeof(TSDBKEY));
M
Minglei Jin 已提交
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869

  SDelIdx delIdx;

  SDelFile *pDelFile = tsdbFSStateGetDelFile(pTsdb->fs->cState);
  if (pDelFile) {
    SDelFReader *pDelFReader;

    code = tsdbDelFReaderOpen(&pDelFReader, pDelFile, pTsdb, NULL);
    if (code) goto _err;

    code = getTableDelIdx(pDelFReader, suid, uid, &delIdx);
    if (code) goto _err;

    code = getTableDelSkyline(pMem, pIMem, pDelFReader, &delIdx, pSkyline);
    if (code) goto _err;

H
Hongze Cheng 已提交
870
    tsdbDelFReaderClose(&pDelFReader);
M
Minglei Jin 已提交
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
  } else {
    code = getTableDelSkyline(pMem, pIMem, NULL, NULL, pSkyline);
    if (code) goto _err;
  }

  int iSkyline = taosArrayGetSize(pSkyline) - 1;

  SBlockIdx idx = {.suid = suid, .uid = uid};

  SFSNextRowIter fsState = {0};
  fsState.state = SFSNEXTROW_FS;
  fsState.pTsdb = pTsdb;
  fsState.pBlockIdxExp = &idx;

  SMemNextRowIter memState = {0};
  SMemNextRowIter imemState = {0};
  TSDBROW         memRow, imemRow, fsRow;

889 890 891
  TsdbNextRowState input[3] = {{&memRow, true, false, &memState, getNextRowFromMem, NULL},
                               {&imemRow, true, false, &imemState, getNextRowFromMem, NULL},
                               {&fsRow, false, true, &fsState, getNextRowFromFS, clearNextRowFromFS}};
M
Minglei Jin 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958

  if (pMem) {
    memState.pMem = pMem;
    memState.state = SMEMNEXTROW_ENTER;
    input[0].stop = false;
    input[0].next = true;
  }
  if (pIMem) {
    imemState.pMem = pIMem;
    imemState.state = SMEMNEXTROW_ENTER;
    input[1].stop = false;
    input[1].next = true;
  }

  int16_t nilColCount = nCol - 1;  // count of null & none cols
  int     iCol = 0;                // index of first nil col index from left to right
  bool    setICol = false;

  do {
    for (int i = 0; i < 3; ++i) {
      if (input[i].next && !input[i].stop) {
        code = input[i].nextRowFn(input[i].iter, &input[i].pRow);
        if (code) goto _err;

        if (input[i].pRow == NULL) {
          input[i].stop = true;
          input[i].next = false;
        }
      }
    }

    if (input[0].stop && input[1].stop && input[2].stop) {
      break;
    }

    // select maxpoint(s) from mem, imem, fs
    TSDBROW *max[3] = {0};
    int      iMax[3] = {-1, -1, -1};
    int      nMax = 0;
    TSKEY    maxKey = TSKEY_MIN;

    for (int i = 0; i < 3; ++i) {
      if (!input[i].stop && input[i].pRow != NULL) {
        TSDBKEY key = TSDBROW_KEY(input[i].pRow);

        // merging & deduplicating on client side
        if (maxKey <= key.ts) {
          if (maxKey < key.ts) {
            nMax = 0;
            maxKey = key.ts;
          }

          iMax[nMax] = i;
          max[nMax++] = input[i].pRow;
        }
      }
    }

    // delete detection
    TSDBROW *merge[3] = {0};
    int      iMerge[3] = {-1, -1, -1};
    int      nMerge = 0;
    for (int i = 0; i < nMax; ++i) {
      TSDBKEY maxKey = TSDBROW_KEY(max[i]);

      bool deleted = tsdbKeyDeleted(&maxKey, pSkyline, &iSkyline);
      if (!deleted) {
959
        iMerge[nMerge] = iMax[i];
M
Minglei Jin 已提交
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
        merge[nMerge++] = max[i];
      }

      input[iMax[i]].next = deleted;
    }

    // merge if nMerge > 1
    if (nMerge > 0) {
      if (nMerge == 1) {
        code = tsRowFromTsdbRow(pTSchema, merge[nMerge - 1], ppRow);
        if (code) goto _err;
      } else {
        // merge 2 or 3 rows
        SRowMerger merger = {0};

        tRowMergerInit(&merger, merge[0], pTSchema);
        for (int i = 1; i < nMerge; ++i) {
          tRowMerge(&merger, merge[i]);
        }
        tRowMergerGetRow(&merger, ppRow);
        tRowMergerClear(&merger);
      }
982
    } else {
983 984 985
      /* *ppRow = NULL; */
      /* return code; */
      continue;
M
Minglei Jin 已提交
986 987 988 989 990 991 992 993
    }

    if (iCol == 0) {
      STColumn *pTColumn = &pTSchema->columns[0];
      SColVal  *pColVal = &(SColVal){0};

      *pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = maxKey});

994 995
      // if (taosArrayPush(pColArray, pColVal) == NULL) {
      if (taosArrayPush(pColArray, &(SLastCol){.ts = TSKEY_MAX, .colVal = *pColVal}) == NULL) {
M
Minglei Jin 已提交
996 997 998 999 1000 1001 1002
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _err;
      }

      ++iCol;

      setICol = false;
1003
      for (int16_t i = iCol; i < nCol; ++i) {
M
Minglei Jin 已提交
1004
        // tsdbRowGetColVal(*ppRow, pTSchema, i, pColVal);
1005
        tTSRowGetVal(*ppRow, pTSchema, i, pColVal);
1006 1007
        // if (taosArrayPush(pColArray, pColVal) == NULL) {
        if (taosArrayPush(pColArray, &(SLastCol){.ts = maxKey, .colVal = *pColVal}) == NULL) {
M
Minglei Jin 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
          code = TSDB_CODE_OUT_OF_MEMORY;
          goto _err;
        }

        if (pColVal->isNull || pColVal->isNone) {
          for (int j = 0; j < nMerge; ++j) {
            SColVal jColVal = {0};
            tsdbRowGetColVal(merge[j], pTSchema, i, &jColVal);
            if (jColVal.isNull || jColVal.isNone) {
              input[iMerge[j]].next = true;
            }
          }
          if (!setICol) {
            iCol = i;
            setICol = true;
          }
        } else {
          --nilColCount;
        }
      }
1028

M
Minglei Jin 已提交
1029 1030 1031
      if (*ppRow) {
        taosMemoryFreeClear(*ppRow);
      }
1032

M
Minglei Jin 已提交
1033 1034 1035 1036 1037 1038 1039
      continue;
    }

    setICol = false;
    for (int16_t i = iCol; i < nCol; ++i) {
      SColVal colVal = {0};
      tTSRowGetVal(*ppRow, pTSchema, i, &colVal);
1040
      TSKEY rowTs = (*ppRow)->ts;
M
Minglei Jin 已提交
1041

1042 1043 1044
      // SColVal *tColVal = (SColVal *)taosArrayGet(pColArray, i);
      SLastCol *tTsVal = (SLastCol *)taosArrayGet(pColArray, i);
      SColVal  *tColVal = &tTsVal->colVal;
M
Minglei Jin 已提交
1045 1046 1047

      if (!colVal.isNone && !colVal.isNull) {
        if (tColVal->isNull || tColVal->isNone) {
1048 1049
          // taosArraySet(pColArray, i, &colVal);
          taosArraySet(pColArray, i, &(SLastCol){.ts = rowTs, .colVal = colVal});
M
Minglei Jin 已提交
1050 1051 1052
          --nilColCount;
        }
      } else {
1053
        if ((tColVal->isNull || tColVal->isNone) && !setICol) {
M
Minglei Jin 已提交
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
          iCol = i;
          setICol = true;

          for (int j = 0; j < nMerge; ++j) {
            SColVal jColVal = {0};
            tsdbRowGetColVal(merge[j], pTSchema, i, &jColVal);
            if (jColVal.isNull || jColVal.isNone) {
              input[iMerge[j]].next = true;
            }
          }
        }
      }
    }
M
Minglei Jin 已提交
1067 1068 1069 1070

    if (*ppRow) {
      taosMemoryFreeClear(*ppRow);
    }
M
Minglei Jin 已提交
1071 1072 1073
  } while (nilColCount > 0);

  // if () new ts row from pColArray if non empty
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
  /* if (taosArrayGetSize(pColArray) == nCol) { */
  /*   code = tdSTSRowNew(pColArray, pTSchema, ppRow); */
  /*   if (code) goto _err; */
  /* } */
  /* taosArrayDestroy(pColArray); */
  if (taosArrayGetSize(pColArray) <= 0) {
    *ppLastArray = NULL;
    taosArrayDestroy(pColArray);
  } else {
    *ppLastArray = pColArray;
  }
  if (*ppRow) {
    taosMemoryFreeClear(*ppRow);
  }

  for (int i = 0; i < 3; ++i) {
    if (input[i].nextRowClearFn) {
      input[i].nextRowClearFn(input[i].iter);
    }
  }
  if (pSkyline) {
    taosArrayDestroy(pSkyline);
M
Minglei Jin 已提交
1096 1097 1098 1099 1100 1101
  }
  taosMemoryFreeClear(pTSchema);

  return code;
_err:
  taosArrayDestroy(pColArray);
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
  if (*ppRow) {
    taosMemoryFreeClear(*ppRow);
  }
  for (int i = 0; i < 3; ++i) {
    if (input[i].nextRowClearFn) {
      input[i].nextRowClearFn(input[i].iter);
    }
  }
  if (pSkyline) {
    taosArrayDestroy(pSkyline);
  }
M
Minglei Jin 已提交
1113 1114 1115 1116 1117
  taosMemoryFreeClear(pTSchema);
  tsdbError("vgId:%d merge last_row failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  return code;
}

1118 1119 1120 1121 1122
int32_t tsdbCacheGetLastrowH(SLRUCache *pCache, tb_uid_t uid, STsdb *pTsdb, LRUHandle **handle) {
  int32_t code = 0;
  char    key[32] = {0};
  int     keyLen = 0;

1123 1124
  //  getTableCacheKey(uid, "lr", key, &keyLen);
  getTableCacheKey(uid, 0, key, &keyLen);
1125 1126 1127 1128 1129
  LRUHandle *h = taosLRUCacheLookup(pCache, key, keyLen);
  if (h) {
    //*ppRow = (STSRow *)taosLRUCacheValue(pCache, h);
  } else {
    STSRow *pRow = NULL;
1130 1131
    bool    dup = false;
    code = mergeLastRow(uid, pTsdb, &dup, &pRow);
1132 1133
    // if table's empty or error, return code of -1
    if (code < 0 || pRow == NULL) {
1134
      if (!dup && pRow) {
1135 1136 1137
        taosMemoryFree(pRow);
      }

1138 1139
      *handle = NULL;
      return 0;
1140 1141
    }

1142
    tsdbCacheInsertLastrow(pCache, pTsdb, uid, pRow, dup);
1143 1144 1145 1146 1147 1148 1149 1150 1151
    h = taosLRUCacheLookup(pCache, key, keyLen);
    //*ppRow = (STSRow *)taosLRUCacheValue(pCache, h);
  }

  *handle = h;

  return code;
}

1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
int32_t tsdbCacheLastArray2Row(SArray *pLastArray, STSRow **ppRow, STSchema *pTSchema) {
  int32_t code = 0;
  int16_t nCol = taosArrayGetSize(pLastArray);
  SArray *pColArray = taosArrayInit(nCol, sizeof(SColVal));

  for (int16_t iCol = 0; iCol < nCol; ++iCol) {
    SLastCol *tTsVal = (SLastCol *)taosArrayGet(pLastArray, iCol);
    SColVal  *tColVal = &tTsVal->colVal;
    taosArrayPush(pColArray, tColVal);
  }

  code = tdSTSRowNew(pColArray, pTSchema, ppRow);
  if (code) goto _err;

  taosArrayDestroy(pColArray);

  return code;

_err:
  taosArrayDestroy(pColArray);

  return code;
}

1176 1177 1178 1179 1180
int32_t tsdbCacheGetLastH(SLRUCache *pCache, tb_uid_t uid, STsdb *pTsdb, LRUHandle **handle) {
  int32_t code = 0;
  char    key[32] = {0};
  int     keyLen = 0;

1181 1182
  // getTableCacheKey(uid, "l", key, &keyLen);
  getTableCacheKey(uid, 1, key, &keyLen);
1183 1184 1185 1186 1187
  LRUHandle *h = taosLRUCacheLookup(pCache, key, keyLen);
  if (h) {
    //*ppRow = (STSRow *)taosLRUCacheValue(pCache, h);

  } else {
1188 1189 1190 1191
    // STSRow *pRow = NULL;
    // code = mergeLast(uid, pTsdb, &pRow);
    SArray *pLastArray = NULL;
    code = mergeLast(uid, pTsdb, &pLastArray);
1192
    // if table's empty or error, return code of -1
1193 1194
    // if (code < 0 || pRow == NULL) {
    if (code < 0 || pLastArray == NULL) {
1195 1196
      *handle = NULL;
      return 0;
1197 1198
    }

1199
    _taos_lru_deleter_t deleter = deleteTableCacheLast;
1200
    LRUStatus           status =
1201
        taosLRUCacheInsert(pCache, key, keyLen, pLastArray, pLastArray->capacity, deleter, NULL, TAOS_LRU_PRIORITY_LOW);
1202 1203 1204 1205
    if (status != TAOS_LRU_STATUS_OK) {
      code = -1;
    }
    /* tsdbCacheInsertLast(pCache, uid, pRow); */
1206 1207 1208 1209 1210
    h = taosLRUCacheLookup(pCache, key, keyLen);
    //*ppRow = (STSRow *)taosLRUCacheValue(pCache, h);
  }

  *handle = h;
M
Minglei Jin 已提交
1211

1212 1213 1214
  return code;
}

1215
int32_t tsdbCacheDelete(SLRUCache *pCache, tb_uid_t uid, TSKEY eKey) {
1216
  int32_t code = 0;
H
Hongze Cheng 已提交
1217 1218
  char    key[32] = {0};
  int     keyLen = 0;
1219

1220 1221
  // getTableCacheKey(uid, "lr", key, &keyLen);
  getTableCacheKey(uid, 0, key, &keyLen);
1222 1223
  LRUHandle *h = taosLRUCacheLookup(pCache, key, keyLen);
  if (h) {
M
Minglei Jin 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
    STSRow *pRow = (STSRow *)taosLRUCacheValue(pCache, h);
    if (pRow->ts <= eKey) {
      taosLRUCacheRelease(pCache, h, true);
    } else {
      taosLRUCacheRelease(pCache, h, false);
    }

    // void taosLRUCacheErase(SLRUCache * cache, const void *key, size_t keyLen);
  }

1234 1235
  // getTableCacheKey(uid, "l", key, &keyLen);
  getTableCacheKey(uid, 1, key, &keyLen);
M
Minglei Jin 已提交
1236 1237 1238
  h = taosLRUCacheLookup(pCache, key, keyLen);
  if (h) {
    // clear last cache anyway, no matter where eKey ends.
1239
    taosLRUCacheRelease(pCache, h, true);
M
Minglei Jin 已提交
1240 1241

    // void taosLRUCacheErase(SLRUCache * cache, const void *key, size_t keyLen);
1242 1243 1244 1245
  }

  return code;
}
M
Minglei Jin 已提交
1246 1247 1248 1249 1250 1251 1252 1253

int32_t tsdbCacheRelease(SLRUCache *pCache, LRUHandle *h) {
  int32_t code = 0;

  taosLRUCacheRelease(pCache, h, false);

  return code;
}