tsdbCache.c 47.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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"

18 19 20 21 22
typedef struct {
  TSKEY   ts;
  SColVal colVal;
} SLastCol;

23
int32_t tsdbOpenCache(STsdb *pTsdb) {
H
Hongze Cheng 已提交
24
  int32_t    code = 0;
25
  SLRUCache *pCache = NULL;
26
  size_t     cfgCapacity = pTsdb->pVnode->config.cacheLastSize * 1024 * 1024;
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

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

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

54 55 56 57 58 59 60 61 62 63
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 已提交
64
static void deleteTableCacheLastrow(const void *key, size_t keyLen, void *value) { taosMemoryFree(value); }
65

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

68
int32_t tsdbCacheDeleteLastrow(SLRUCache *pCache, tb_uid_t uid, TSKEY eKey) {
69
  int32_t code = 0;
70 71 72

  char key[32] = {0};
  int  keyLen = 0;
73

74 75
  // getTableCacheKey(uid, "lr", key, &keyLen);
  getTableCacheKey(uid, 0, key, &keyLen);
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;
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
int32_t tsdbCacheDeleteLast(SLRUCache *pCache, tb_uid_t uid, TSKEY eKey) {
  int32_t code = 0;

  char key[32] = {0};
  int  keyLen = 0;

  // getTableCacheKey(uid, "l", key, &keyLen);
  getTableCacheKey(uid, 1, key, &keyLen);
  LRUHandle *h = taosLRUCacheLookup(pCache, key, keyLen);
  if (h) {
    SArray *pLast = (SArray *)taosLRUCacheValue(pCache, h);
    bool    invalidate = false;
    int16_t nCol = taosArrayGetSize(pLast);

    for (int16_t iCol = 0; iCol < nCol; ++iCol) {
      SLastCol *tTsVal = (SLastCol *)taosArrayGet(pLast, iCol);
      if (eKey >= tTsVal->ts) {
        invalidate = true;
        break;
      }
    }

    if (invalidate) {
      taosLRUCacheRelease(pCache, h, true);
    } else {
      taosLRUCacheRelease(pCache, h, false);
    }
    // void taosLRUCacheErase(SLRUCache * cache, const void *key, size_t keyLen);
  }

  return code;
}

int32_t tsdbCacheDelete(SLRUCache *pCache, tb_uid_t uid, TSKEY eKey) {
125 126 127 128
  int32_t code = 0;
  char    key[32] = {0};
  int     keyLen = 0;

129 130 131 132 133 134 135 136 137 138 139 140 141 142
  // getTableCacheKey(uid, "lr", key, &keyLen);
  getTableCacheKey(uid, 0, key, &keyLen);
  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);
  }

143 144
  // getTableCacheKey(uid, "l", key, &keyLen);
  getTableCacheKey(uid, 1, key, &keyLen);
145
  h = taosLRUCacheLookup(pCache, key, keyLen);
146
  if (h) {
147 148 149
    SArray *pLast = (SArray *)taosLRUCacheValue(pCache, h);
    bool    invalidate = false;
    int16_t nCol = taosArrayGetSize(pLast);
150

151 152 153 154 155 156 157 158 159 160 161 162 163
    for (int16_t iCol = 0; iCol < nCol; ++iCol) {
      SLastCol *tTsVal = (SLastCol *)taosArrayGet(pLast, iCol);
      if (eKey >= tTsVal->ts) {
        invalidate = true;
        break;
      }
    }

    if (invalidate) {
      taosLRUCacheRelease(pCache, h, true);
    } else {
      taosLRUCacheRelease(pCache, h, false);
    }
164 165 166 167 168 169
    // void taosLRUCacheErase(SLRUCache * cache, const void *key, size_t keyLen);
  }

  return code;
}

170
int32_t tsdbCacheInsertLastrow(SLRUCache *pCache, STsdb *pTsdb, tb_uid_t uid, STSRow *row, bool dup) {
171 172
  int32_t code = 0;
  STSRow *cacheRow = NULL;
H
Hongze Cheng 已提交
173 174
  char    key[32] = {0};
  int     keyLen = 0;
175

176 177
  // getTableCacheKey(uid, "lr", key, &keyLen);
  getTableCacheKey(uid, 0, key, &keyLen);
178 179
  LRUHandle *h = taosLRUCacheLookup(pCache, key, keyLen);
  if (h) {
H
Hongze Cheng 已提交
180
    cacheRow = (STSRow *)taosLRUCacheValue(pCache, h);
181
    if (row->ts >= cacheRow->ts) {
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
      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;
      }

200
      if (TD_ROW_LEN(row) <= TD_ROW_LEN(cacheRow)) {
H
Hongze Cheng 已提交
201
        tdRowCpy(cacheRow, row);
202 203 204
        if (!dup) {
          taosMemoryFree(row);
        }
205 206

        taosLRUCacheRelease(pCache, h, false);
207
      } else {
208 209
        taosLRUCacheRelease(pCache, h, true);
        // tsdbCacheDeleteLastrow(pCache, uid, TSKEY_MAX);
M
Minglei Jin 已提交
210 211 212 213 214
        if (dup) {
          cacheRow = tdRowDup(row);
        } else {
          cacheRow = row;
        }
215 216 217 218 219 220 221
        _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); */
222 223
      }
    }
224
  } /*else {
225 226 227 228 229
    if (dup) {
      cacheRow = tdRowDup(row);
    } else {
      cacheRow = row;
    }
230 231

    _taos_lru_deleter_t deleter = deleteTableCacheLastrow;
H
Hongze Cheng 已提交
232 233
    LRUStatus           status =
        taosLRUCacheInsert(pCache, key, keyLen, cacheRow, TD_ROW_LEN(cacheRow), deleter, NULL, TAOS_LRU_PRIORITY_LOW);
234 235 236
    if (status != TAOS_LRU_STATUS_OK) {
      code = -1;
    }
237
    }*/
238 239 240 241

  return code;
}

242
int32_t tsdbCacheInsertLast(SLRUCache *pCache, tb_uid_t uid, STSRow *row, STsdb *pTsdb) {
243 244 245 246 247
  int32_t code = 0;
  STSRow *cacheRow = NULL;
  char    key[32] = {0};
  int     keyLen = 0;

248
  // ((void)(row));
249

250 251
  // getTableCacheKey(uid, "l", key, &keyLen);
  getTableCacheKey(uid, 1, key, &keyLen);
252 253
  LRUHandle *h = taosLRUCacheLookup(pCache, key, keyLen);
  if (h) {
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    STSchema *pTSchema = metaGetTbTSchema(pTsdb->pVnode->pMeta, uid, -1);
    TSKEY     keyTs = row->ts;
    bool      invalidate = false;

    SArray *pLast = (SArray *)taosLRUCacheValue(pCache, h);
    int16_t nCol = taosArrayGetSize(pLast);
    int16_t iCol = 0;

    SLastCol *tTsVal = (SLastCol *)taosArrayGet(pLast, iCol);
    if (keyTs > tTsVal->ts) {
      STColumn *pTColumn = &pTSchema->columns[0];
      SColVal   tColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = keyTs});

      taosArraySet(pLast, iCol, &(SLastCol){.ts = keyTs, .colVal = tColVal});
    }

    for (++iCol; iCol < nCol; ++iCol) {
      SLastCol *tTsVal = (SLastCol *)taosArrayGet(pLast, iCol);
      if (keyTs >= tTsVal->ts) {
        SColVal *tColVal = &tTsVal->colVal;

        SColVal colVal = {0};
        tTSRowGetVal(row, pTSchema, iCol, &colVal);
        if (colVal.isNone || colVal.isNull) {
          if (keyTs == tTsVal->ts && !tColVal->isNone && !tColVal->isNull) {
            invalidate = true;

            break;
          }
        } else {
          taosArraySet(pLast, iCol, &(SLastCol){.ts = keyTs, .colVal = colVal});
        }
      }
    }

    taosMemoryFreeClear(pTSchema);

    taosLRUCacheRelease(pCache, h, invalidate);

293
    // clear last cache anyway, lazy load when get last lookup
294
    // taosLRUCacheRelease(pCache, h, true);
295 296 297 298 299
  }

  return code;
}

300 301 302 303 304 305
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 已提交
306
    metaReaderClear(&mr);  // table not esist
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    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;

326 327
  // SMapData delDataMap;
  // SDelData delData;
328 329

  if (pDelIdx) {
330
    // tMapDataReset(&delDataMap);
331

332
    // code = tsdbReadDelData(pDelReader, pDelIdx, &delDataMap, NULL);
333
    code = tsdbReadDelData(pDelReader, pDelIdx, aDelData, NULL);
334
    if (code) goto _err;
335
    /*
336 337 338 339 340 341
    for (int32_t iDelData = 0; iDelData < delDataMap.nItem; ++iDelData) {
      code = tMapDataGetItemByIdx(&delDataMap, iDelData, &delData, tGetDelData);
      if (code) goto _err;

      taosArrayPush(aDelData, &delData);
    }
342
    */
343 344 345 346 347 348 349
  }

_err:
  return code;
}

static int32_t getTableDelDataFromTbData(STbData *pTbData, SArray *aDelData) {
H
Hongze Cheng 已提交
350
  int32_t   code = 0;
351 352 353 354 355 356 357 358 359
  SDelData *pDelData = pTbData ? pTbData->pHead : NULL;

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

  return code;
}

H
Hongze Cheng 已提交
360 361
static int32_t getTableDelData(STbData *pMem, STbData *pIMem, SDelFReader *pDelReader, SDelIdx *pDelIdx,
                               SArray *aDelData) {
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
  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 已提交
383 384
static int32_t getTableDelSkyline(STbData *pMem, STbData *pIMem, SDelFReader *pDelReader, SDelIdx *pDelIdx,
                                  SArray *aSkyline) {
385
  int32_t code = 0;
M
Minglei Jin 已提交
386
  SArray *aDelData = NULL;
387

M
Minglei Jin 已提交
388
  aDelData = taosArrayInit(32, sizeof(SDelData));
389 390 391 392
  code = getTableDelData(pMem, pIMem, pDelReader, pDelIdx, aDelData);
  if (code) goto _err;

  size_t nDelData = taosArrayGetSize(aDelData);
393 394 395 396
  if (nDelData > 0) {
    code = tsdbBuildDeleteSkyline(aDelData, 0, (int32_t)(nDelData - 1), aSkyline);
    if (code) goto _err;
  }
397

M
Minglei Jin 已提交
398 399 400
  if (aDelData) {
    taosArrayDestroy(aDelData);
  }
401 402 403 404 405 406
_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 已提交
407
  SArray *pDelIdxArray = NULL;
408

409
  // SMapData delIdxMap;
M
Minglei Jin 已提交
410
  pDelIdxArray = taosArrayInit(32, sizeof(SDelIdx));
411
  SDelIdx idx = {.suid = suid, .uid = uid};
412

413 414 415
  // tMapDataReset(&delIdxMap);
  //  code = tsdbReadDelIdx(pDelFReader, &delIdxMap, NULL);
  code = tsdbReadDelIdx(pDelFReader, pDelIdxArray, NULL);
416 417
  if (code) goto _err;

418
  // code = tMapDataSearch(&delIdxMap, &idx, tGetDelIdx, tCmprDelIdx, pDelIdx);
419
  SDelIdx *pIdx = taosArraySearch(pDelIdxArray, &idx, tCmprDelIdx, TD_EQ);
420 421
  if (code) goto _err;

422 423
  *pDelIdx = *pIdx;

M
Minglei Jin 已提交
424 425 426
  if (pDelIdxArray) {
    taosArrayDestroy(pDelIdxArray);
  }
427 428 429
_err:
  return code;
}
430

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
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;
446 447 448
  SArray          *aBlockIdx;
  // SMapData         blockIdxMap;
  //  SBlockIdx  blockIdx;
449 450 451 452 453 454 455 456 457 458
  SBlockIdx  *pBlockIdx;
  SMapData    blockMap;
  int32_t     nBlock;
  int32_t     iBlock;
  SBlock      block;
  SBlockData  blockData;
  SBlockData *pBlockData;
  int32_t     nRow;
  int32_t     iRow;
  TSDBROW     row;
459 460 461 462 463 464 465 466
} SFSNextRowIter;

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

  switch (state->state) {
    case SFSNEXTROW_FS:
H
Hongze Cheng 已提交
467
      // state->aDFileSet = state->pTsdb->pFS->cState->aDFileSet;
468
      state->nFileSet = taosArrayGetSize(state->aDFileSet);
M
Minglei Jin 已提交
469
      state->iFileSet = state->nFileSet;
470 471

      state->pBlockData = NULL;
472 473 474

    case SFSNEXTROW_FILESET: {
      SDFileSet *pFileSet = NULL;
475
    _next_fileset:
476 477 478
      if (--state->iFileSet >= 0) {
        pFileSet = (SDFileSet *)taosArrayGet(state->aDFileSet, state->iFileSet);
      } else {
H
Hongze Cheng 已提交
479
        // tBlockDataClear(&state->blockData, 1);
480
        if (state->pBlockData) {
H
Hongze Cheng 已提交
481
          tBlockDataClear(state->pBlockData, 1);
482 483
          state->pBlockData = NULL;
        }
484

485 486 487 488 489 490
        *ppRow = NULL;
        return code;
      }

      code = tsdbDataFReaderOpen(&state->pDataFReader, state->pTsdb, pFileSet);
      if (code) goto _err;
491 492 493 494 495 496 497 498 499

      // 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);
500 501
      if (code) goto _err;

502 503
      /* if (state->pBlockIdx) { */
      /* } */
504 505
      /* code = tMapDataSearch(&state->blockIdxMap, state->pBlockIdxExp, tGetBlockIdx, tCmprBlockIdx,
       * &state->blockIdx);
506 507
       */
      state->pBlockIdx = taosArraySearch(state->aBlockIdx, state->pBlockIdxExp, tCmprBlockIdx, TD_EQ);
508
      if (code) goto _err;
509

510 511 512 513
      if (!state->pBlockIdx) {
        goto _next_fileset;
      }

514
      tMapDataReset(&state->blockMap);
515 516
      code = tsdbReadBlock(state->pDataFReader, state->pBlockIdx, &state->blockMap, NULL);
      /* code = tsdbReadBlock(state->pDataFReader, &state->blockIdx, &state->blockMap, NULL); */
517 518 519 520
      if (code) goto _err;

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

522 523 524 525 526
      if (!state->pBlockData) {
        state->pBlockData = &state->blockData;

        tBlockDataInit(&state->blockData);
      }
527 528 529 530 531 532
    }
    case SFSNEXTROW_BLOCKDATA:
      if (state->iBlock >= 0) {
        SBlock block = {0};

        tBlockReset(&block);
533 534
        // tBlockDataReset(&state->blockData);
        tBlockDataReset(state->pBlockData);
535 536

        tMapDataGetItemByIdx(&state->blockMap, state->iBlock, &block, tGetBlock);
537
        /* code = tsdbReadBlockData(state->pDataFReader, &state->blockIdx, &block, &state->blockData, NULL, NULL); */
538
        code = tsdbReadBlockData(state->pDataFReader, state->pBlockIdx, &block, state->pBlockData, NULL, NULL);
539 540 541 542 543 544 545 546 547
        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) {
548
        state->row = tsdbRowFromBlockData(state->pBlockData, state->iRow);
549 550 551 552 553 554
        *ppRow = &state->row;

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

557 558
            if (state->aBlockIdx) {
              taosArrayDestroy(state->aBlockIdx);
559
              state->aBlockIdx = NULL;
560
            }
561

562 563 564 565 566 567 568 569 570 571 572 573
            state->state = SFSNEXTROW_FILESET;
          }
        }
      }

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

_err:
574 575
  if (state->pDataFReader) {
    tsdbDataFReaderClose(&state->pDataFReader);
576
    state->pDataFReader = NULL;
577 578 579
  }
  if (state->aBlockIdx) {
    taosArrayDestroy(state->aBlockIdx);
580 581 582
    state->aBlockIdx = NULL;
  }
  if (state->pBlockData) {
H
Hongze Cheng 已提交
583 584
    // tBlockDataClear(&state->blockData, 1);
    tBlockDataClear(state->pBlockData, 1);
585
    state->pBlockData = NULL;
586 587
  }

588
  *ppRow = NULL;
589

590 591 592
  return code;
}

593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
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) {
H
Hongze Cheng 已提交
610 611
    // tBlockDataClear(&state->blockData, 1);
    tBlockDataClear(state->pBlockData, 1);
612 613 614 615 616 617
    state->pBlockData = NULL;
  }

  return code;
}

618 619 620 621 622 623 624 625 626
typedef enum SMEMNEXTROWSTATES {
  SMEMNEXTROW_ENTER,
  SMEMNEXTROW_NEXT,
} SMEMNEXTROWSTATES;

typedef struct SMemNextRowIter {
  SMEMNEXTROWSTATES state;
  STbData          *pMem;  // [input]
  STbDataIter       iter;  // mem buffer skip list iterator
627 628
  // bool              iterOpened;
  // TSDBROW          *curRow;
629 630 631
} SMemNextRowIter;

static int32_t getNextRowFromMem(void *iter, TSDBROW **ppRow) {
632
  // static int32_t getNextRowFromMem(void *iter, SArray *pRowArray) {
633 634
  SMemNextRowIter *state = (SMemNextRowIter *)iter;
  int32_t          code = 0;
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
  /*
  if (!state->iterOpened) {
    if (state->pMem != NULL) {
      tsdbTbDataIterOpen(state->pMem, NULL, 1, &state->iter);

      state->iterOpened = true;

      TSDBROW *pMemRow = tsdbTbDataIterGet(&state->iter);
      if (pMemRow) {
        state->curRow = pMemRow;
      } else {
        return code;
      }
    } else {
      return code;
    }
  }
652

653 654 655 656 657 658 659 660 661 662 663 664 665 666
  taosArrayPush(pRowArray, state->curRow);
  while (tsdbTbDataIterNext(&state->iter)) {
    TSDBROW *row = tsdbTbDataIterGet(&state->iter);

    if (TSDBROW_TS(row) < TSDBROW_TS(state->curRow)) {
      state->curRow = row;
      break;
    } else {
      taosArrayPush(pRowArray, row);
    }
  }

  return code;
  */
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
  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;
}

705 706 707 708 709
static int32_t tsRowFromTsdbRow(STSchema *pTSchema, TSDBROW *pRow, STSRow **ppRow) {
  int32_t code = 0;

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

710
  if (pRow->type == 0) {
711
    *ppRow = tdRowDup(pRow->pTSRow);
712
  } else {
713 714 715 716 717 718
    SArray *pArray = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
    if (pArray == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }

719 720 721 722 723 724 725 726 727
    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;
    }

728 729 730 731 732 733 734 735 736 737
    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;
738 739
  }

740 741
_exit:
  return code;
742 743
}

744
static bool tsdbKeyDeleted(TSDBKEY *key, SArray *pSkyline, int64_t *iSkyline) {
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
  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;
}

770
typedef int32_t (*_next_row_fn_t)(void *iter, TSDBROW **ppRow);
771
// typedef int32_t (*_next_row_fn_t)(void *iter, SArray *pRowArray);
772
typedef int32_t (*_next_row_clear_fn_t)(void *iter);
773

774 775
// typedef struct TsdbNextRowState {
typedef struct {
776 777 778 779 780 781
  TSDBROW             *pRow;
  bool                 stop;
  bool                 next;
  void                *iter;
  _next_row_fn_t       nextRowFn;
  _next_row_clear_fn_t nextRowClearFn;
782
} TsdbNextRowState;
783

784 785 786 787 788 789 790 791 792 793 794 795
typedef struct {
  // STsdb  *pTsdb;
  SArray *pSkyline;
  int64_t iSkyline;

  SBlockIdx       idx;
  SMemNextRowIter memState;
  SMemNextRowIter imemState;
  SFSNextRowIter  fsState;
  TSDBROW         memRow, imemRow, fsRow;

  TsdbNextRowState input[3];
H
Hongze Cheng 已提交
796 797 798 799
  // SMemTable       *pMemTable;
  // SMemTable       *pIMemTable;
  STsdbReadSnap *pReadSnap;
  STsdb         *pTsdb;
800 801 802 803 804 805 806
} CacheNextRowIter;

static int32_t nextRowIterOpen(CacheNextRowIter *pIter, tb_uid_t uid, STsdb *pTsdb) {
  int code = 0;

  tb_uid_t suid = getTableSuidByUid(uid, pTsdb);

H
Hongze Cheng 已提交
807
  tsdbTakeReadSnap(pTsdb, &pIter->pReadSnap);
H
Hongze Cheng 已提交
808

809
  STbData *pMem = NULL;
H
Hongze Cheng 已提交
810 811
  if (pIter->pReadSnap->pMem) {
    tsdbGetTbDataFromMemTable(pIter->pReadSnap->pMem, suid, uid, &pMem);
812 813 814
  }

  STbData *pIMem = NULL;
H
Hongze Cheng 已提交
815 816
  if (pIter->pReadSnap->pIMem) {
    tsdbGetTbDataFromMemTable(pIter->pReadSnap->pIMem, suid, uid, &pIMem);
817 818
  }

H
Hongze Cheng 已提交
819 820
  pIter->pTsdb = pTsdb;

821 822 823 824
  pIter->pSkyline = taosArrayInit(32, sizeof(TSDBKEY));

  SDelIdx delIdx;

H
Hongze Cheng 已提交
825
  SDelFile *pDelFile = pIter->pReadSnap->fs.pDelFile;
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
  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, pIter->pSkyline);
    if (code) goto _err;

    tsdbDelFReaderClose(&pDelFReader);
  } else {
    code = getTableDelSkyline(pMem, pIMem, NULL, NULL, pIter->pSkyline);
    if (code) goto _err;
  }

  pIter->iSkyline = taosArrayGetSize(pIter->pSkyline) - 1;

  pIter->idx = (SBlockIdx){.suid = suid, .uid = uid};

  pIter->fsState.state = SFSNEXTROW_FS;
  pIter->fsState.pTsdb = pTsdb;
H
Hongze Cheng 已提交
850
  pIter->fsState.aDFileSet = pIter->pReadSnap->fs.aDFileSet;
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
  pIter->fsState.pBlockIdxExp = &pIter->idx;

  pIter->input[0] = (TsdbNextRowState){&pIter->memRow, true, false, &pIter->memState, getNextRowFromMem, NULL};
  pIter->input[1] = (TsdbNextRowState){&pIter->imemRow, true, false, &pIter->imemState, getNextRowFromMem, NULL};
  pIter->input[2] =
      (TsdbNextRowState){&pIter->fsRow, false, true, &pIter->fsState, getNextRowFromFS, clearNextRowFromFS};

  if (pMem) {
    pIter->memState.pMem = pMem;
    pIter->memState.state = SMEMNEXTROW_ENTER;
    pIter->input[0].stop = false;
    pIter->input[0].next = true;
  }

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

  return code;
_err:
  return code;
}

static int32_t nextRowIterClose(CacheNextRowIter *pIter) {
  int code = 0;

  for (int i = 0; i < 3; ++i) {
    if (pIter->input[i].nextRowClearFn) {
      pIter->input[i].nextRowClearFn(pIter->input[i].iter);
    }
  }

  if (pIter->pSkyline) {
    taosArrayDestroy(pIter->pSkyline);
  }

H
Hongze Cheng 已提交
890
  tsdbUntakeReadSnap(pIter->pTsdb, pIter->pReadSnap);
H
Hongze Cheng 已提交
891

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 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
  return code;
_err:
  return code;
}

// iterate next row non deleted backward ts, version (from high to low)
static int32_t nextRowIterGet(CacheNextRowIter *pIter, TSDBROW **ppRow) {
  int code = 0;

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

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

  if (pIter->input[0].stop && pIter->input[1].stop && pIter->input[2].stop) {
    *ppRow = NULL;
    return code;
  }

  // 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 (!pIter->input[i].stop && pIter->input[i].pRow != NULL) {
      TSDBKEY key = TSDBROW_KEY(pIter->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++] = pIter->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, pIter->pSkyline, &pIter->iSkyline);
    if (!deleted) {
      iMerge[nMerge] = iMax[i];
      merge[nMerge++] = max[i];
    }

    pIter->input[iMax[i]].next = deleted;
  }

  if (nMerge > 0) {
    pIter->input[iMerge[0]].next = true;

    *ppRow = merge[0];
  } else {
    *ppRow = NULL;
  }

  return code;
_err:
  return code;
}

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

  STSchema *pTSchema = metaGetTbTSchema(pTsdb->pVnode->pMeta, uid, -1);
  int16_t   nCol = pTSchema->numOfCols;
  int16_t   iCol = 0;
  int16_t   noneCol = 0;
  bool      setNoneCol = false;
  SArray   *pColArray = taosArrayInit(nCol, sizeof(SColVal));
  SColVal  *pColVal = &(SColVal){0};

  // tb_uid_t suid = getTableSuidByUid(uid, pTsdb);

  TSKEY lastRowTs = TSKEY_MAX;

  CacheNextRowIter iter = {0};
  nextRowIterOpen(&iter, uid, pTsdb);

  do {
    TSDBROW *pRow = NULL;
    nextRowIterGet(&iter, &pRow);

    if (!pRow) {
      break;
    }

    if (lastRowTs == TSKEY_MAX) {
      lastRowTs = TSDBROW_TS(pRow);
      STColumn *pTColumn = &pTSchema->columns[0];

      *pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = lastRowTs});
      if (taosArrayPush(pColArray, pColVal) == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _err;
      }

      for (iCol = 1; iCol < nCol; ++iCol) {
        tsdbRowGetColVal(pRow, pTSchema, iCol, pColVal);

        if (taosArrayPush(pColArray, pColVal) == NULL) {
          code = TSDB_CODE_OUT_OF_MEMORY;
          goto _err;
        }

        if (pColVal->isNone && !setNoneCol) {
          noneCol = iCol;
          setNoneCol = true;
        }
      }
      if (!setNoneCol) {
        // goto build the result ts row
        break;
      } else {
        continue;
      }
    }

    if ((TSDBROW_TS(pRow) < lastRowTs)) {
      // goto build the result ts row
      break;
    }

    // merge into pColArray
    setNoneCol = false;
    for (iCol = noneCol; iCol < nCol; ++iCol) {
      // high version's column value
      SColVal *tColVal = (SColVal *)taosArrayGet(pColArray, iCol);

      tsdbRowGetColVal(pRow, pTSchema, iCol, pColVal);
      if (tColVal->isNone && !pColVal->isNone) {
        taosArraySet(pColArray, iCol, pColVal);
      } else if (tColVal->isNone && pColVal->isNone && !setNoneCol) {
        noneCol = iCol;
        setNoneCol = true;
      }
    }
  } while (setNoneCol);

  // build the result ts row here
  *dup = false;
  if (taosArrayGetSize(pColArray) == nCol) {
    code = tdSTSRowNew(pColArray, pTSchema, ppRow);
    if (code) goto _err;
  } else {
    *ppRow = NULL;
  }

  nextRowIterClose(&iter);
  taosArrayDestroy(pColArray);
  taosMemoryFreeClear(pTSchema);
  return code;

_err:
  nextRowIterClose(&iter);
  taosArrayDestroy(pColArray);
  taosMemoryFreeClear(pTSchema);
  return code;
}

static int32_t mergeLast2(tb_uid_t uid, STsdb *pTsdb, SArray **ppLastArray) {
  int32_t code = 0;

  STSchema *pTSchema = metaGetTbTSchema(pTsdb->pVnode->pMeta, uid, -1);
  int16_t   nCol = pTSchema->numOfCols;
  int16_t   iCol = 0;
  int16_t   noneCol = 0;
  bool      setNoneCol = false;
  SArray   *pColArray = taosArrayInit(nCol, sizeof(SLastCol));
  SColVal  *pColVal = &(SColVal){0};

  // tb_uid_t suid = getTableSuidByUid(uid, pTsdb);

  TSKEY lastRowTs = TSKEY_MAX;

  CacheNextRowIter iter = {0};
  nextRowIterOpen(&iter, uid, pTsdb);

  do {
    TSDBROW *pRow = NULL;
    nextRowIterGet(&iter, &pRow);

    if (!pRow) {
      break;
    }

    TSKEY rowTs = TSDBROW_TS(pRow);

    if (lastRowTs == TSKEY_MAX) {
      lastRowTs = rowTs;
      STColumn *pTColumn = &pTSchema->columns[0];

      *pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = lastRowTs});
      if (taosArrayPush(pColArray, &(SLastCol){.ts = lastRowTs, .colVal = *pColVal}) == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _err;
      }

      for (iCol = 1; iCol < nCol; ++iCol) {
        tsdbRowGetColVal(pRow, pTSchema, iCol, pColVal);

        if (taosArrayPush(pColArray, &(SLastCol){.ts = lastRowTs, .colVal = *pColVal}) == NULL) {
          code = TSDB_CODE_OUT_OF_MEMORY;
          goto _err;
        }

        if ((pColVal->isNone || pColVal->isNull) && !setNoneCol) {
          noneCol = iCol;
          setNoneCol = true;
        }
      }
      if (!setNoneCol) {
        // goto build the result ts row
        break;
      } else {
        continue;
      }
    }
    /*
    if ((TSDBROW_TS(pRow) < lastRowTs)) {
      // goto build the result ts row
      break;
    }
    */
    // merge into pColArray
    setNoneCol = false;
    for (iCol = noneCol; iCol < nCol; ++iCol) {
      // high version's column value
      SColVal *tColVal = (SColVal *)taosArrayGet(pColArray, iCol);

      tsdbRowGetColVal(pRow, pTSchema, iCol, pColVal);
      if ((tColVal->isNone || tColVal->isNull) && (!pColVal->isNone && !pColVal->isNull)) {
        taosArraySet(pColArray, iCol, &(SLastCol){.ts = rowTs, .colVal = *pColVal});
        //} else if (tColVal->isNone && pColVal->isNone && !setNoneCol) {
      } else if ((tColVal->isNone || tColVal->isNull) && (pColVal->isNone || pColVal->isNull) && !setNoneCol) {
        noneCol = iCol;
        setNoneCol = true;
      }
    }
  } while (setNoneCol);

  // build the result ts row here
  //*dup = false;
  if (taosArrayGetSize(pColArray) <= 0) {
    *ppLastArray = NULL;
    taosArrayDestroy(pColArray);
  } else {
    *ppLastArray = pColArray;
  }
  /*  if (taosArrayGetSize(pColArray) == nCol) {
    code = tdSTSRowNew(pColArray, pTSchema, ppRow);
    if (code) goto _err;
  } else {
    *ppRow = NULL;
    }*/

  nextRowIterClose(&iter);
  // taosArrayDestroy(pColArray);
  taosMemoryFreeClear(pTSchema);
  return code;

_err:
  nextRowIterClose(&iter);
  // taosArrayDestroy(pColArray);
  taosMemoryFreeClear(pTSchema);
  return code;
}

H
Hongze Cheng 已提交
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
// static int32_t mergeLastRow(tb_uid_t uid, STsdb *pTsdb, bool *dup, STSRow **ppRow) {
//   int32_t code = 0;
//   SArray *pSkyline = NULL;

//   STSchema *pTSchema = metaGetTbTSchema(pTsdb->pVnode->pMeta, uid, -1);
//   int16_t   nCol = pTSchema->numOfCols;
//   SArray   *pColArray = taosArrayInit(nCol, sizeof(SColVal));

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

//   *ppRow = NULL;

//   pSkyline = taosArrayInit(32, sizeof(TSDBKEY));

//   SDelIdx delIdx;

//   SDelFile *pDelFile = tsdbFSStateGetDelFile(pTsdb->pFS->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;

//     tsdbDelFReaderClose(&pDelFReader);
//   } else {
//     code = getTableDelSkyline(pMem, pIMem, NULL, NULL, pSkyline);
//     if (code) goto _err;
//   }

//   int64_t 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;

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

//   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) {
//         if (input[i].pRow == NULL) {
//           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) {
//         iMerge[nMerge] = i;
//         merge[nMerge++] = max[i];
//       }

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

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

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

//   } while (1);

//   for (int i = 0; i < 3; ++i) {
//     if (input[i].nextRowClearFn) {
//       input[i].nextRowClearFn(input[i].iter);
//     }
//   }
//   if (pSkyline) {
//     taosArrayDestroy(pSkyline);
//   }
//   taosMemoryFreeClear(pTSchema);

//   return code;
// _err:
//   for (int i = 0; i < 3; ++i) {
//     if (input[i].nextRowClearFn) {
//       input[i].nextRowClearFn(input[i].iter);
//     }
//   }
//   if (pSkyline) {
//     taosArrayDestroy(pSkyline);
//   }
//   taosMemoryFreeClear(pTSchema);
//   tsdbError("vgId:%d merge last_row failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
//   return code;
// }
1360

1361
// static int32_t mergeLast(tb_uid_t uid, STsdb *pTsdb, STSRow **ppRow) {
H
Hongze Cheng 已提交
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
// 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;

//   STSchema *pTSchema = metaGetTbTSchema(pTsdb->pVnode->pMeta, uid, -1);
//   int16_t   nCol = pTSchema->numOfCols;
//   //  SArray   *pColArray = taosArrayInit(nCol, sizeof(SColVal));
//   SArray *pColArray = taosArrayInit(nCol, sizeof(SLastCol));

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

//   *ppLastArray = NULL;

//   pSkyline = taosArrayInit(32, sizeof(TSDBKEY));

//   SDelIdx delIdx;

//   SDelFile *pDelFile = tsdbFSStateGetDelFile(pTsdb->pFS->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;

//     tsdbDelFReaderClose(&pDelFReader);
//   } else {
//     code = getTableDelSkyline(pMem, pIMem, NULL, NULL, pSkyline);
//     if (code) goto _err;
//   }

//   int64_t 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;

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

//   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) {
//         iMerge[nMerge] = iMax[i];
//         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);
//       }
//     } else {
//       /* *ppRow = NULL; */
//       /* return code; */
//       continue;
//     }

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

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

//       // if (taosArrayPush(pColArray, pColVal) == NULL) {
//       if (taosArrayPush(pColArray, &(SLastCol){.ts = maxKey, .colVal = *pColVal}) == NULL) {
//         code = TSDB_CODE_OUT_OF_MEMORY;
//         goto _err;
//       }

//       ++iCol;

//       setICol = false;
//       for (int16_t i = iCol; i < nCol; ++i) {
//         // tsdbRowGetColVal(*ppRow, pTSchema, i, pColVal);
//         tTSRowGetVal(*ppRow, pTSchema, i, pColVal);
//         // if (taosArrayPush(pColArray, pColVal) == NULL) {
//         if (taosArrayPush(pColArray, &(SLastCol){.ts = maxKey, .colVal = *pColVal}) == NULL) {
//           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;
//         }
//       }

//       if (*ppRow) {
//         taosMemoryFreeClear(*ppRow);
//       }

//       continue;
//     }

//     setICol = false;
//     for (int16_t i = iCol; i < nCol; ++i) {
//       SColVal colVal = {0};
//       tTSRowGetVal(*ppRow, pTSchema, i, &colVal);
//       TSKEY rowTs = (*ppRow)->ts;

//       // SColVal *tColVal = (SColVal *)taosArrayGet(pColArray, i);
//       SLastCol *tTsVal = (SLastCol *)taosArrayGet(pColArray, i);
//       SColVal  *tColVal = &tTsVal->colVal;

//       if (!colVal.isNone && !colVal.isNull) {
//         if (tColVal->isNull || tColVal->isNone) {
//           // taosArraySet(pColArray, i, &colVal);
//           taosArraySet(pColArray, i, &(SLastCol){.ts = rowTs, .colVal = colVal});
//           --nilColCount;
//         }
//       } else {
//         if ((tColVal->isNull || tColVal->isNone) && !setICol) {
//           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;
//             }
//           }
//         }
//       }
//     }

//     if (*ppRow) {
//       taosMemoryFreeClear(*ppRow);
//     }
//   } while (nilColCount > 0);

//   // if () new ts row from pColArray if non empty
//   /* 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);
//   }
//   taosMemoryFreeClear(pTSchema);

//   return code;
// _err:
//   taosArrayDestroy(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);
//   }
//   taosMemoryFreeClear(pTSchema);
//   tsdbError("vgId:%d merge last_row failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
//   return code;
// }
M
Minglei Jin 已提交
1651

1652 1653 1654 1655 1656
int32_t tsdbCacheGetLastrowH(SLRUCache *pCache, tb_uid_t uid, STsdb *pTsdb, LRUHandle **handle) {
  int32_t code = 0;
  char    key[32] = {0};
  int     keyLen = 0;

1657 1658
  //  getTableCacheKey(uid, "lr", key, &keyLen);
  getTableCacheKey(uid, 0, key, &keyLen);
1659 1660 1661 1662 1663
  LRUHandle *h = taosLRUCacheLookup(pCache, key, keyLen);
  if (h) {
    //*ppRow = (STSRow *)taosLRUCacheValue(pCache, h);
  } else {
    STSRow *pRow = NULL;
1664
    bool    dup = false;  // which is always false for now
1665
    code = mergeLastRow2(uid, pTsdb, &dup, &pRow);
1666 1667
    // if table's empty or error, return code of -1
    if (code < 0 || pRow == NULL) {
1668
      if (!dup && pRow) {
1669 1670 1671
        taosMemoryFree(pRow);
      }

1672 1673
      *handle = NULL;
      return 0;
1674 1675
    }

1676 1677 1678 1679 1680 1681 1682 1683
    _taos_lru_deleter_t deleter = deleteTableCacheLastrow;
    LRUStatus           status =
        taosLRUCacheInsert(pCache, key, keyLen, pRow, TD_ROW_LEN(pRow), deleter, NULL, TAOS_LRU_PRIORITY_LOW);
    if (status != TAOS_LRU_STATUS_OK) {
      code = -1;
    }

    //    tsdbCacheInsertLastrow(pCache, pTsdb, uid, pRow, dup);
1684 1685 1686 1687 1688 1689 1690 1691 1692
    h = taosLRUCacheLookup(pCache, key, keyLen);
    //*ppRow = (STSRow *)taosLRUCacheValue(pCache, h);
  }

  *handle = h;

  return code;
}

1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
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;
}

1717 1718 1719 1720 1721
int32_t tsdbCacheGetLastH(SLRUCache *pCache, tb_uid_t uid, STsdb *pTsdb, LRUHandle **handle) {
  int32_t code = 0;
  char    key[32] = {0};
  int     keyLen = 0;

1722 1723
  // getTableCacheKey(uid, "l", key, &keyLen);
  getTableCacheKey(uid, 1, key, &keyLen);
1724 1725 1726 1727 1728
  LRUHandle *h = taosLRUCacheLookup(pCache, key, keyLen);
  if (h) {
    //*ppRow = (STSRow *)taosLRUCacheValue(pCache, h);

  } else {
1729 1730 1731
    // STSRow *pRow = NULL;
    // code = mergeLast(uid, pTsdb, &pRow);
    SArray *pLastArray = NULL;
1732 1733
    // code = mergeLast(uid, pTsdb, &pLastArray);
    code = mergeLast2(uid, pTsdb, &pLastArray);
1734
    // if table's empty or error, return code of -1
1735 1736
    // if (code < 0 || pRow == NULL) {
    if (code < 0 || pLastArray == NULL) {
1737 1738
      *handle = NULL;
      return 0;
1739 1740
    }

1741
    _taos_lru_deleter_t deleter = deleteTableCacheLast;
1742
    LRUStatus           status =
1743
        taosLRUCacheInsert(pCache, key, keyLen, pLastArray, pLastArray->capacity, deleter, NULL, TAOS_LRU_PRIORITY_LOW);
1744 1745 1746
    if (status != TAOS_LRU_STATUS_OK) {
      code = -1;
    }
1747

1748 1749 1750 1751 1752
    h = taosLRUCacheLookup(pCache, key, keyLen);
    //*ppRow = (STSRow *)taosLRUCacheValue(pCache, h);
  }

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

1754 1755 1756
  return code;
}

M
Minglei Jin 已提交
1757 1758 1759 1760 1761 1762 1763
int32_t tsdbCacheRelease(SLRUCache *pCache, LRUHandle *h) {
  int32_t code = 0;

  taosLRUCacheRelease(pCache, h, false);

  return code;
}
1764 1765 1766 1767 1768 1769

void tsdbCacheSetCapacity(SVnode *pVnode, size_t capacity) {
  taosLRUCacheSetCapacity(pVnode->pTsdb->lruCache, capacity);
}

size_t tsdbCacheGetCapacity(SVnode *pVnode) { return taosLRUCacheGetCapacity(pVnode->pTsdb->lruCache); }