tsdbCache.c 18.0 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 45 46 47

  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);
  }
}

static void getTableCacheKey(tb_uid_t uid, const char *cacheType, char *key, int *len) {
  int keyLen = 0;

H
Hongze Cheng 已提交
48
  snprintf(key, 30, "%" PRIi64 "%s", uid, cacheType);
49 50 51
  *len = strlen(key);
}

H
Hongze Cheng 已提交
52
static void deleteTableCacheLastrow(const void *key, size_t keyLen, void *value) { taosMemoryFree(value); }
53 54 55 56

int32_t tsdbCacheInsertLastrow(SLRUCache *pCache, tb_uid_t uid, STSRow *row) {
  int32_t code = 0;
  STSRow *cacheRow = NULL;
H
Hongze Cheng 已提交
57 58
  char    key[32] = {0};
  int     keyLen = 0;
59 60 61 62

  getTableCacheKey(uid, "lr", key, &keyLen);
  LRUHandle *h = taosLRUCacheLookup(pCache, key, keyLen);
  if (h) {
H
Hongze Cheng 已提交
63
    cacheRow = (STSRow *)taosLRUCacheValue(pCache, h);
64
    if (row->ts >= cacheRow->ts) {
65
      if (TD_ROW_LEN(row) <= TD_ROW_LEN(cacheRow)) {
H
Hongze Cheng 已提交
66
        tdRowCpy(cacheRow, row);
67 68 69
      } else {
        tsdbCacheDeleteLastrow(pCache, uid);
        tsdbCacheInsertLastrow(pCache, uid, row);
70 71 72 73 74 75
      }
    }
  } else {
    cacheRow = tdRowDup(row);

    _taos_lru_deleter_t deleter = deleteTableCacheLastrow;
H
Hongze Cheng 已提交
76 77
    LRUStatus           status =
        taosLRUCacheInsert(pCache, key, keyLen, cacheRow, TD_ROW_LEN(cacheRow), deleter, NULL, TAOS_LRU_PRIORITY_LOW);
78 79 80 81 82 83 84 85
    if (status != TAOS_LRU_STATUS_OK) {
      code = -1;
    }
  }

  return code;
}

86 87 88 89 90 91
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 已提交
92
    metaReaderClear(&mr);  // table not esist
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    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;
}
/*
109 110
static int32_t getMemLastRow(SMemTable *mem, tb_uid_t suid, tb_uid_t uid, STSRow
**ppRow) { int32_t code = 0;
111 112 113 114 115 116 117 118 119 120

  if (mem) {
    STbData *pMem = NULL;
    STbDataIter* iter;              // mem buffer skip list iterator

    tsdbGetTbDataFromMemTable(mem, suid, uid, &pMem);
    if (pMem != NULL) {
      tsdbTbDataIterCreate(pMem, NULL, 1, &iter);

      if (iter != NULL) {
H
Hongze Cheng 已提交
121
        TSDBROW *row = tsdbTbDataIterGet(iter);
122

H
Hongze Cheng 已提交
123
        tsdbTbDataIterDestroy(iter);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
      }
    }
  } else {
    *ppRow = NULL;
  }

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

  SMapData delDataMap;
  SDelData delData;

  if (pDelIdx) {
    tMapDataReset(&delDataMap);

    code = tsdbReadDelData(pDelReader, pDelIdx, &delDataMap, NULL);
    if (code) goto _err;

    for (int32_t iDelData = 0; iDelData < delDataMap.nItem; ++iDelData) {
      code = tMapDataGetItemByIdx(&delDataMap, iDelData, &delData, tGetDelData);
      if (code) goto _err;

      taosArrayPush(aDelData, &delData);
    }
  }

_err:
  return code;
}

static int32_t getTableDelDataFromTbData(STbData *pTbData, SArray *aDelData) {
H
Hongze Cheng 已提交
158
  int32_t   code = 0;
159 160 161 162 163 164 165 166 167
  SDelData *pDelData = pTbData ? pTbData->pHead : NULL;

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

  return code;
}

H
Hongze Cheng 已提交
168 169
static int32_t getTableDelData(STbData *pMem, STbData *pIMem, SDelFReader *pDelReader, SDelIdx *pDelIdx,
                               SArray *aDelData) {
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  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 已提交
191 192
static int32_t getTableDelSkyline(STbData *pMem, STbData *pIMem, SDelFReader *pDelReader, SDelIdx *pDelIdx,
                                  SArray *aSkyline) {
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  int32_t code = 0;

  SArray *aDelData = taosArrayInit(32, sizeof(SDelData));
  code = getTableDelData(pMem, pIMem, pDelReader, pDelIdx, aDelData);
  if (code) goto _err;

  size_t nDelData = taosArrayGetSize(aDelData);
  code = tsdbBuildDeleteSkyline(aDelData, 0, (int32_t)(nDelData - 1), aSkyline);
  if (code) goto _err;

  taosArrayDestroy(aDelData);

_err:
  return code;
}

static int32_t getTableDelIdx(SDelFReader *pDelFReader, tb_uid_t suid, tb_uid_t uid, SDelIdx *pDelIdx) {
  int32_t code = 0;

  SMapData delIdxMap;
  SDelIdx  idx = {.suid = suid, .uid = uid};

  tMapDataReset(&delIdxMap);
  code = tsdbReadDelIdx(pDelFReader, &delIdxMap, NULL);
  if (code) goto _err;

  code = tMapDataSearch(&delIdxMap, &idx, tGetDelIdx, tCmprDelIdx, pDelIdx);
  if (code) goto _err;

_err:
  return code;
}
225
#if 0
H
Hongze Cheng 已提交
226
static int32_t mergeLastRowFileSet(STbDataIter *iter, STbDataIter *iiter, SDFileSet *pFileSet, SArray *pSkyline,
227
                                   STsdb *pTsdb, STSRow **ppLastRow) {
228 229 230 231
  int32_t code = 0;

  TSDBROW *pMemRow = NULL;
  TSDBROW *pIMemRow = NULL;
232 233
  TSDBKEY  memKey = TSDBKEY_MIN;
  TSDBKEY  imemKey = TSDBKEY_MIN;
234 235 236

  if (iter != NULL) {
    pMemRow = tsdbTbDataIterGet(iter);
237 238 239
    if (pMemRow) {
      memKey = tsdbRowKey(pMemRow);
    }
240 241 242 243
  }

  if (iter != NULL) {
    pIMemRow = tsdbTbDataIterGet(iiter);
244 245 246
    if (pIMemRow) {
      imemKey = tsdbRowKey(pIMemRow);
    }
247 248 249 250 251 252 253 254 255 256 257
  }

  SDataFReader *pDataFReader;
  code = tsdbDataFReaderOpen(&pDataFReader, pTsdb, pFileSet);
  if (code) goto _err;

  SMapData blockIdxMap;
  tMapDataReset(&blockIdxMap);
  code = tsdbReadBlockIdx(pDataFReader, &blockIdxMap, NULL);
  if (code) goto _err;

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
  SBlockIdx blockIdx = {0};
  tBlockIdxReset(&blockIdx);
  code = tMapDataSearch(&blockIdxMap, pBlockIdx, tGetBlockIdx, tCmprBlockIdx, &blockIdx);
  if (code) goto _err;

  SMapData blockMap = {0};
  tMapDataReset(&blockMap);
  code = tsdbReadBlock(pDataFReader, &blockIdx, &blockMap, NULL);
  if (code) goto _err;

  int32_t nBlock = blockMap.nItem;
  for (int32_t iBlock = nBlock - 1; iBlock >= 0; --iBlock) {
    SBlock     block = {0};
    SBlockData blockData = {0};

    tBlockReset(&block);
    tBlockDataReset(&blockData);

    tMapDataGetItemByIdx(&blockMap, iBlock, &block, tGetBlock);

278
    code = tsdbReadBlockData(pDataFReader, &blockIdx, &block, &blockData, NULL, NULL);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    if (code) goto _err;

    int32_t nRow = blockData.nRow;
    for (int32_t iRow = nRow - 1; iRow >= 0; --iRow) {
      TSDBROW row = tsdbRowFromBlockData(&blockData, iRow);

      TSDBKEY key = tsdbRowKey(&row);
      if (pMemRow != NULL && pIMemRow != NULL) {
        int32_t c = tsdbKeyCmprFn(memKey, imemKey);
        if (c < 0) {
        } else if (c > 0) {
        } else {
        }
      } else if (pMemRow != NULL) {
        pMemRow = tsdbTbDataIterGet(iter);

      } else if (pIMemRow != NULL) {
      } else {
        if (!tsdbKeyDeleted(key, pSkyline)) {
          *ppLastRow = buildTsrowFromTsdbrow(&row);
          goto _done;
        } else {
          continue;
        }
      }
      // select current row if outside delete area
      STSchema *pTSchema = metaGetTbTSchema(pTsdb->pVnode->pMeta, uid, -1);
    }
  }
308

309
_done:
H
Hongze Cheng 已提交
310
  tsdbDataFReaderClose(&pDataFReader);
311

312 313
  return code;

314 315 316
_err:
  return code;
}
317
#endif
318

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
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;
  SMapData         blockIdxMap;
  SBlockIdx        blockIdx;
  SMapData         blockMap;
  int32_t          nBlock;
  int32_t          iBlock;
  SBlock           block;
  SBlockData       blockData;
  int32_t          nRow;
  int32_t          iRow;
  TSDBROW          row;
} 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);
      state->iFileSet = state->nFileSet;

    case SFSNEXTROW_FILESET: {
      SDFileSet *pFileSet = NULL;
      if (--state->iFileSet >= 0) {
        pFileSet = (SDFileSet *)taosArrayGet(state->aDFileSet, state->iFileSet);
      } else {
        *ppRow = NULL;
        return code;
      }

      code = tsdbDataFReaderOpen(&state->pDataFReader, state->pTsdb, pFileSet);
      if (code) goto _err;

      tMapDataReset(&state->blockIdxMap);
      code = tsdbReadBlockIdx(state->pDataFReader, &state->blockIdxMap, NULL);
      if (code) goto _err;

      tBlockIdxReset(&state->blockIdx);
      code = tMapDataSearch(&state->blockIdxMap, state->pBlockIdxExp, tGetBlockIdx, tCmprBlockIdx, &state->blockIdx);
      if (code) goto _err;

      tMapDataReset(&state->blockMap);
      code = tsdbReadBlock(state->pDataFReader, &state->blockIdx, &state->blockMap, NULL);
      if (code) goto _err;

      state->nBlock = state->blockMap.nItem;
      state->iBlock = state->nBlock - 1;
    }
    case SFSNEXTROW_BLOCKDATA:
      if (state->iBlock >= 0) {
        SBlock block = {0};

        tBlockReset(&block);
        tBlockDataReset(&state->blockData);

        tMapDataGetItemByIdx(&state->blockMap, state->iBlock, &block, tGetBlock);
391
        code = tsdbReadBlockData(state->pDataFReader, &state->blockIdx, &block, &state->blockData, NULL, NULL);
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
        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) {
        state->row = tsdbRowFromBlockData(&state->blockData, state->iRow);
        *ppRow = &state->row;

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

            state->state = SFSNEXTROW_FILESET;
          }
        }
      }

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

_err:
  *ppRow = NULL;
  return code;
}

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;
}

static STSRow *tsRowFromTsdbRow(TSDBROW *pRow) {
  // TODO: new tsrow from tsdbrow
  STSRow *ret = NULL;
  if (pRow->type == 0) {
482
    return tdRowDup(pRow->pTSRow);
483 484 485 486 487 488
  } else {
  }

  return ret;
}

489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
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;
}

515 516 517 518 519 520 521 522 523
typedef int32_t (*_next_row_fn_t)(void *iter, TSDBROW **ppRow);

typedef struct TsdbNextRowState {
  TSDBROW       *pRow;
  bool           stop;
  bool           next;
  void          *iter;
  _next_row_fn_t nextRowFn;
} TsdbNextRowState;
524 525 526 527 528 529

static int32_t mergeLastRow(tb_uid_t uid, STsdb *pTsdb, STSRow **ppRow) {
  int32_t code = 0;

  tb_uid_t suid = getTableSuidByUid(uid, pTsdb);

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

535
  STbData *pIMem = NULL;
536 537 538 539 540 541 542
  if (pTsdb->imem) {
    tsdbGetTbDataFromMemTable(pTsdb->imem, suid, uid, &pIMem);
  }

  *ppRow = NULL;

  SDelFReader *pDelFReader;
543 544
  SDelFile    *pDelFile = tsdbFSStateGetDelFile(pTsdb->fs->cState);
  code = tsdbDelFReaderOpen(&pDelFReader, pDelFile, pTsdb, NULL);
545 546 547 548 549 550 551 552 553 554
  if (code) goto _err;

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

  SArray *pSkyline = taosArrayInit(32, sizeof(TSDBKEY));
  code = getTableDelSkyline(pMem, pIMem, pDelFReader, &delIdx, pSkyline);
  if (code) goto _err;

555
  int iSkyline = taosArrayGetSize(pSkyline) - 1;
556

557 558 559 560 561 562 563 564
  tsdbDelFReaderClose(pDelFReader);

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

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

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
  SMemNextRowIter memState = {0};
  SMemNextRowIter imemState = {0};
  TSDBROW         memRow, imemRow, fsRow;

  TsdbNextRowState input[3] = {{&memRow, true, false, &memState, getNextRowFromMem},
                               {&imemRow, true, false, &imemState, getNextRowFromMem},
                               {&fsRow, false, true, &fsState, getNextRowFromFS}};

  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 已提交
585
  }
586

587 588 589 590 591 592 593 594 595 596 597 598 599 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 628 629 630
  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;
    for (int i = 0; i < 3; ++i) {
      if (input[i].pRow != NULL) {
        TSDBKEY key = TSDBROW_KEY(input[i].pRow);
        TSDBKEY maxKey = TSDBROW_KEY(max[nMax]);

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

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

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

631 632
      // bool deleted = false;
      bool deleted = tsdbKeyDeleted(&maxKey, pSkyline, &iSkyline);
633 634 635
      if (!deleted) {
        merge[nMerge++] = max[i];
      }
636 637

      input[iMax[i]].next = deleted;
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
    }

    // merge if nMerge > 1
    if (nMerge > 0) {
      if (nMerge == 1) {
        *ppRow = tsRowFromTsdbRow(merge[nMerge]);
      } else {
        // merge 2 or 3 rows
        SRowMerger merger = {0};

        STSchema *pTSchema = metaGetTbTSchema(pTsdb->pVnode->pMeta, uid, -1);

        tRowMergerInit(&merger, merge[0], pTSchema);
        for (int i = 1; i < nMerge; ++i) {
          tRowMerge(&merger, merge[i]);
        }
        tRowMergerGetRow(&merger, ppRow);
        tRowMergerClear(&merger);
      }
    }
  } while (*ppRow == NULL);
659 660 661 662 663 664 665 666 667

  return code;

_err:
  tsdbError("vgId:%d merge last_row failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  return code;
}

int32_t tsdbCacheGetLastrow(SLRUCache *pCache, tb_uid_t uid, STsdb *pTsdb, STSRow **ppRow) {
668
  int32_t code = 0;
H
Hongze Cheng 已提交
669 670
  char    key[32] = {0};
  int     keyLen = 0;
671 672 673 674

  getTableCacheKey(uid, "lr", key, &keyLen);
  LRUHandle *h = taosLRUCacheLookup(pCache, key, keyLen);
  if (h) {
H
Hongze Cheng 已提交
675
    *ppRow = (STSRow *)taosLRUCacheValue(pCache, h);
676
  } else {
677 678 679 680 681 682
    STSRow *pRow = NULL;
    code = mergeLastRow(uid, pTsdb, &pRow);
    // if table's empty or error, return code of -1
    if (code < 0 || pRow == NULL) {
      return -1;
    }
683 684 685 686

    tsdbCacheInsertLastrow(pCache, uid, pRow);
    LRUHandle *h = taosLRUCacheLookup(pCache, key, keyLen);
    *ppRow = (STSRow *)taosLRUCacheValue(pCache, h);
687 688 689 690 691 692 693
  }

  return code;
}

int32_t tsdbCacheDeleteLastrow(SLRUCache *pCache, tb_uid_t uid) {
  int32_t code = 0;
H
Hongze Cheng 已提交
694 695
  char    key[32] = {0};
  int     keyLen = 0;
696 697 698 699 700

  getTableCacheKey(uid, "lr", key, &keyLen);
  LRUHandle *h = taosLRUCacheLookup(pCache, key, keyLen);
  if (h) {
    taosLRUCacheRelease(pCache, h, true);
701 702
    // void taosLRUCacheErase(SLRUCache * cache, const void *key, size_t
    // keyLen);
703 704 705 706
  }

  return code;
}