tsdbUtil.c 52.3 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

16
#include "tdataformat.h"
H
Hongze Cheng 已提交
17 18
#include "tsdb.h"

H
Hongze Cheng 已提交
19
// SMapData =======================================================================
H
Hongze Cheng 已提交
20 21 22 23
void tMapDataReset(SMapData *pMapData) {
  pMapData->nItem = 0;
  pMapData->nData = 0;
}
H
Hongze Cheng 已提交
24

H
Hongze Cheng 已提交
25
void tMapDataClear(SMapData *pMapData) {
H
Hongze Cheng 已提交
26 27
  tFree((uint8_t *)pMapData->aOffset);
  tFree(pMapData->pData);
28 29
  pMapData->pData = NULL;
  pMapData->aOffset = NULL;
H
Hongze Cheng 已提交
30 31
}

H
Hongze Cheng 已提交
32 33 34 35 36 37 38 39 40
int32_t tMapDataPutItem(SMapData *pMapData, void *pItem, int32_t (*tPutItemFn)(uint8_t *, void *)) {
  int32_t code = 0;
  int32_t offset = pMapData->nData;
  int32_t nItem = pMapData->nItem;

  pMapData->nItem++;
  pMapData->nData += tPutItemFn(NULL, pItem);

  // alloc
H
Hongze Cheng 已提交
41
  code = tRealloc((uint8_t **)&pMapData->aOffset, sizeof(int32_t) * pMapData->nItem);
H
Hongze Cheng 已提交
42
  if (code) goto _exit;
H
Hongze Cheng 已提交
43
  code = tRealloc(&pMapData->pData, pMapData->nData);
H
Hongze Cheng 已提交
44
  if (code) goto _exit;
H
Hongze Cheng 已提交
45 46

  // put
H
Hongze Cheng 已提交
47
  pMapData->aOffset[nItem] = offset;
H
Hongze Cheng 已提交
48 49
  tPutItemFn(pMapData->pData + offset, pItem);

H
Hongze Cheng 已提交
50
_exit:
H
Hongze Cheng 已提交
51 52 53
  return code;
}

H
Hongze Cheng 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
int32_t tMapDataCopy(SMapData *pFrom, SMapData *pTo) {
  int32_t code = 0;

  pTo->nItem = pFrom->nItem;
  pTo->nData = pFrom->nData;
  code = tRealloc((uint8_t **)&pTo->aOffset, sizeof(int32_t) * pFrom->nItem);
  if (code) goto _exit;
  code = tRealloc(&pTo->pData, pFrom->nData);
  if (code) goto _exit;
  memcpy(pTo->aOffset, pFrom->aOffset, sizeof(int32_t) * pFrom->nItem);
  memcpy(pTo->pData, pFrom->pData, pFrom->nData);

_exit:
  return code;
}

H
Hongze Cheng 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
int32_t tMapDataSearch(SMapData *pMapData, void *pSearchItem, int32_t (*tGetItemFn)(uint8_t *, void *),
                       int32_t (*tItemCmprFn)(const void *, const void *), void *pItem) {
  int32_t code = 0;
  int32_t lidx = 0;
  int32_t ridx = pMapData->nItem - 1;
  int32_t midx;
  int32_t c;

  while (lidx <= ridx) {
    midx = (lidx + ridx) / 2;

    tMapDataGetItemByIdx(pMapData, midx, pItem, tGetItemFn);

    c = tItemCmprFn(pSearchItem, pItem);
    if (c == 0) {
      goto _exit;
    } else if (c < 0) {
      ridx = midx - 1;
    } else {
      lidx = midx + 1;
    }
  }

  code = TSDB_CODE_NOT_FOUND;

_exit:
  return code;
}
H
Hongze Cheng 已提交
98

H
Hongze Cheng 已提交
99 100 101
void tMapDataGetItemByIdx(SMapData *pMapData, int32_t idx, void *pItem, int32_t (*tGetItemFn)(uint8_t *, void *)) {
  ASSERT(idx >= 0 && idx < pMapData->nItem);
  tGetItemFn(pMapData->pData + pMapData->aOffset[idx], pItem);
H
Hongze Cheng 已提交
102 103 104 105 106 107
}

int32_t tPutMapData(uint8_t *p, SMapData *pMapData) {
  int32_t n = 0;

  n += tPutI32v(p ? p + n : p, pMapData->nItem);
H
Hongze Cheng 已提交
108
  if (pMapData->nItem) {
H
Hongze Cheng 已提交
109
    int32_t lOffset = 0;
H
Hongze Cheng 已提交
110
    for (int32_t iItem = 0; iItem < pMapData->nItem; iItem++) {
H
Hongze Cheng 已提交
111 112
      n += tPutI32v(p ? p + n : p, pMapData->aOffset[iItem] - lOffset);
      lOffset = pMapData->aOffset[iItem];
H
Hongze Cheng 已提交
113
    }
H
Hongze Cheng 已提交
114 115 116 117 118 119

    n += tPutI32v(p ? p + n : p, pMapData->nData);
    if (p) {
      memcpy(p + n, pMapData->pData, pMapData->nData);
    }
    n += pMapData->nData;
H
Hongze Cheng 已提交
120 121 122 123 124 125 126
  }

  return n;
}

int32_t tGetMapData(uint8_t *p, SMapData *pMapData) {
  int32_t n = 0;
H
Hongze Cheng 已提交
127 128 129
  int32_t offset;

  tMapDataReset(pMapData);
H
Hongze Cheng 已提交
130 131

  n += tGetI32v(p + n, &pMapData->nItem);
H
Hongze Cheng 已提交
132
  if (pMapData->nItem) {
H
Hongze Cheng 已提交
133
    if (tRealloc((uint8_t **)&pMapData->aOffset, sizeof(int32_t) * pMapData->nItem)) return -1;
H
Hongze Cheng 已提交
134

H
Hongze Cheng 已提交
135
    int32_t lOffset = 0;
H
Hongze Cheng 已提交
136 137
    for (int32_t iItem = 0; iItem < pMapData->nItem; iItem++) {
      n += tGetI32v(p + n, &pMapData->aOffset[iItem]);
H
Hongze Cheng 已提交
138 139
      pMapData->aOffset[iItem] += lOffset;
      lOffset = pMapData->aOffset[iItem];
H
Hongze Cheng 已提交
140
    }
H
Hongze Cheng 已提交
141 142

    n += tGetI32v(p + n, &pMapData->nData);
H
Hongze Cheng 已提交
143
    if (tRealloc(&pMapData->pData, pMapData->nData)) return -1;
H
Hongze Cheng 已提交
144 145
    memcpy(pMapData->pData, p + n, pMapData->nData);
    n += pMapData->nData;
H
Hongze Cheng 已提交
146 147 148 149 150
  }

  return n;
}

H
Hongze Cheng 已提交
151
// TABLEID =======================================================================
H
Hongze Cheng 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
int32_t tTABLEIDCmprFn(const void *p1, const void *p2) {
  TABLEID *pId1 = (TABLEID *)p1;
  TABLEID *pId2 = (TABLEID *)p2;

  if (pId1->suid < pId2->suid) {
    return -1;
  } else if (pId1->suid > pId2->suid) {
    return 1;
  }

  if (pId1->uid < pId2->uid) {
    return -1;
  } else if (pId1->uid > pId2->uid) {
    return 1;
  }

  return 0;
}

H
Hongze Cheng 已提交
171
// SBlockIdx ======================================================
H
Hongze Cheng 已提交
172 173 174 175 176 177 178 179
int32_t tPutBlockIdx(uint8_t *p, void *ph) {
  int32_t    n = 0;
  SBlockIdx *pBlockIdx = (SBlockIdx *)ph;

  n += tPutI64(p ? p + n : p, pBlockIdx->suid);
  n += tPutI64(p ? p + n : p, pBlockIdx->uid);
  n += tPutI64v(p ? p + n : p, pBlockIdx->offset);
  n += tPutI64v(p ? p + n : p, pBlockIdx->size);
H
Hongze Cheng 已提交
180 181 182 183

  return n;
}

H
Hongze Cheng 已提交
184 185 186
int32_t tGetBlockIdx(uint8_t *p, void *ph) {
  int32_t    n = 0;
  SBlockIdx *pBlockIdx = (SBlockIdx *)ph;
H
Hongze Cheng 已提交
187

H
Hongze Cheng 已提交
188 189 190 191
  n += tGetI64(p + n, &pBlockIdx->suid);
  n += tGetI64(p + n, &pBlockIdx->uid);
  n += tGetI64v(p + n, &pBlockIdx->offset);
  n += tGetI64v(p + n, &pBlockIdx->size);
H
Hongze Cheng 已提交
192 193 194 195

  return n;
}

196
int32_t tCmprBlockIdx(void const *lhs, void const *rhs) {
M
Minglei Jin 已提交
197 198
  SBlockIdx *lBlockIdx = (SBlockIdx *)lhs;
  SBlockIdx *rBlockIdx = (SBlockIdx *)rhs;
199

M
Minglei Jin 已提交
200
  if (lBlockIdx->suid < rBlockIdx->suid) {
201
    return -1;
M
Minglei Jin 已提交
202
  } else if (lBlockIdx->suid > rBlockIdx->suid) {
203 204 205
    return 1;
  }

M
Minglei Jin 已提交
206
  if (lBlockIdx->uid < rBlockIdx->uid) {
207
    return -1;
M
Minglei Jin 已提交
208
  } else if (lBlockIdx->uid > rBlockIdx->uid) {
209 210 211 212 213 214
    return 1;
  }

  return 0;
}

215 216
int32_t tCmprBlockL(void const *lhs, void const *rhs) {
  SBlockIdx *lBlockIdx = (SBlockIdx *)lhs;
H
Hongze Cheng 已提交
217
  SSttBlk   *rBlockL = (SSttBlk *)rhs;
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

  if (lBlockIdx->suid < rBlockL->suid) {
    return -1;
  } else if (lBlockIdx->suid > rBlockL->suid) {
    return 1;
  }

  if (lBlockIdx->uid < rBlockL->minUid) {
    return -1;
  } else if (lBlockIdx->uid > rBlockL->maxUid) {
    return 1;
  }

  return 0;
}

H
Hongze Cheng 已提交
234
// SDataBlk ======================================================
H
Hongze Cheng 已提交
235
void tDataBlkReset(SDataBlk *pDataBlk) {
H
Hongze Cheng 已提交
236
  *pDataBlk = (SDataBlk){.minKey = TSDBKEY_MAX, .maxKey = TSDBKEY_MIN, .minVer = VERSION_MAX, .maxVer = VERSION_MIN};
H
Hongze Cheng 已提交
237 238
}

H
Hongze Cheng 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
int32_t tPutDataBlk(uint8_t *p, void *ph) {
  int32_t   n = 0;
  SDataBlk *pDataBlk = (SDataBlk *)ph;

  n += tPutI64v(p ? p + n : p, pDataBlk->minKey.version);
  n += tPutI64v(p ? p + n : p, pDataBlk->minKey.ts);
  n += tPutI64v(p ? p + n : p, pDataBlk->maxKey.version);
  n += tPutI64v(p ? p + n : p, pDataBlk->maxKey.ts);
  n += tPutI64v(p ? p + n : p, pDataBlk->minVer);
  n += tPutI64v(p ? p + n : p, pDataBlk->maxVer);
  n += tPutI32v(p ? p + n : p, pDataBlk->nRow);
  n += tPutI8(p ? p + n : p, pDataBlk->hasDup);
  n += tPutI8(p ? p + n : p, pDataBlk->nSubBlock);
  for (int8_t iSubBlock = 0; iSubBlock < pDataBlk->nSubBlock; iSubBlock++) {
    n += tPutI64v(p ? p + n : p, pDataBlk->aSubBlock[iSubBlock].offset);
    n += tPutI32v(p ? p + n : p, pDataBlk->aSubBlock[iSubBlock].szBlock);
    n += tPutI32v(p ? p + n : p, pDataBlk->aSubBlock[iSubBlock].szKey);
H
Hongze Cheng 已提交
256
  }
H
Hongze Cheng 已提交
257 258 259
  if (pDataBlk->nSubBlock == 1 && !pDataBlk->hasDup) {
    n += tPutI64v(p ? p + n : p, pDataBlk->smaInfo.offset);
    n += tPutI32v(p ? p + n : p, pDataBlk->smaInfo.size);
H
Hongze Cheng 已提交
260 261
  }

H
Hongze Cheng 已提交
262 263 264
  return n;
}

H
Hongze Cheng 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
int32_t tGetDataBlk(uint8_t *p, void *ph) {
  int32_t   n = 0;
  SDataBlk *pDataBlk = (SDataBlk *)ph;

  n += tGetI64v(p + n, &pDataBlk->minKey.version);
  n += tGetI64v(p + n, &pDataBlk->minKey.ts);
  n += tGetI64v(p + n, &pDataBlk->maxKey.version);
  n += tGetI64v(p + n, &pDataBlk->maxKey.ts);
  n += tGetI64v(p + n, &pDataBlk->minVer);
  n += tGetI64v(p + n, &pDataBlk->maxVer);
  n += tGetI32v(p + n, &pDataBlk->nRow);
  n += tGetI8(p + n, &pDataBlk->hasDup);
  n += tGetI8(p + n, &pDataBlk->nSubBlock);
  for (int8_t iSubBlock = 0; iSubBlock < pDataBlk->nSubBlock; iSubBlock++) {
    n += tGetI64v(p + n, &pDataBlk->aSubBlock[iSubBlock].offset);
    n += tGetI32v(p + n, &pDataBlk->aSubBlock[iSubBlock].szBlock);
    n += tGetI32v(p + n, &pDataBlk->aSubBlock[iSubBlock].szKey);
H
Hongze Cheng 已提交
282
  }
H
Hongze Cheng 已提交
283 284 285
  if (pDataBlk->nSubBlock == 1 && !pDataBlk->hasDup) {
    n += tGetI64v(p + n, &pDataBlk->smaInfo.offset);
    n += tGetI32v(p + n, &pDataBlk->smaInfo.size);
H
Hongze Cheng 已提交
286
  } else {
H
Hongze Cheng 已提交
287 288
    pDataBlk->smaInfo.offset = 0;
    pDataBlk->smaInfo.size = 0;
H
Hongze Cheng 已提交
289 290
  }

H
Hongze Cheng 已提交
291 292 293
  return n;
}

H
Hongze Cheng 已提交
294
int32_t tDataBlkCmprFn(const void *p1, const void *p2) {
H
Hongze Cheng 已提交
295 296
  SDataBlk *pBlock1 = (SDataBlk *)p1;
  SDataBlk *pBlock2 = (SDataBlk *)p2;
H
Hongze Cheng 已提交
297

H
Hongze Cheng 已提交
298
  if (tsdbKeyCmprFn(&pBlock1->maxKey, &pBlock2->minKey) < 0) {
H
Hongze Cheng 已提交
299
    return -1;
H
Hongze Cheng 已提交
300
  } else if (tsdbKeyCmprFn(&pBlock1->minKey, &pBlock2->maxKey) > 0) {
H
Hongze Cheng 已提交
301
    return 1;
H
Hongze Cheng 已提交
302 303 304 305 306
  }

  return 0;
}

H
Hongze Cheng 已提交
307
bool tDataBlkHasSma(SDataBlk *pDataBlk) {
H
Hongze Cheng 已提交
308 309
  if (pDataBlk->nSubBlock > 1) return false;
  if (pDataBlk->hasDup) return false;
H
Hongze Cheng 已提交
310

H
Hongze Cheng 已提交
311
  return pDataBlk->smaInfo.size > 0;
H
Hongze Cheng 已提交
312
}
H
Hongze Cheng 已提交
313

H
Hongze Cheng 已提交
314 315
// SSttBlk ======================================================
int32_t tPutSttBlk(uint8_t *p, void *ph) {
H
Hongze Cheng 已提交
316
  int32_t  n = 0;
H
Hongze Cheng 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329
  SSttBlk *pSttBlk = (SSttBlk *)ph;

  n += tPutI64(p ? p + n : p, pSttBlk->suid);
  n += tPutI64(p ? p + n : p, pSttBlk->minUid);
  n += tPutI64(p ? p + n : p, pSttBlk->maxUid);
  n += tPutI64v(p ? p + n : p, pSttBlk->minKey);
  n += tPutI64v(p ? p + n : p, pSttBlk->maxKey);
  n += tPutI64v(p ? p + n : p, pSttBlk->minVer);
  n += tPutI64v(p ? p + n : p, pSttBlk->maxVer);
  n += tPutI32v(p ? p + n : p, pSttBlk->nRow);
  n += tPutI64v(p ? p + n : p, pSttBlk->bInfo.offset);
  n += tPutI32v(p ? p + n : p, pSttBlk->bInfo.szBlock);
  n += tPutI32v(p ? p + n : p, pSttBlk->bInfo.szKey);
H
Hongze Cheng 已提交
330 331 332 333

  return n;
}

H
Hongze Cheng 已提交
334
int32_t tGetSttBlk(uint8_t *p, void *ph) {
H
Hongze Cheng 已提交
335
  int32_t  n = 0;
H
Hongze Cheng 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348
  SSttBlk *pSttBlk = (SSttBlk *)ph;

  n += tGetI64(p + n, &pSttBlk->suid);
  n += tGetI64(p + n, &pSttBlk->minUid);
  n += tGetI64(p + n, &pSttBlk->maxUid);
  n += tGetI64v(p + n, &pSttBlk->minKey);
  n += tGetI64v(p + n, &pSttBlk->maxKey);
  n += tGetI64v(p + n, &pSttBlk->minVer);
  n += tGetI64v(p + n, &pSttBlk->maxVer);
  n += tGetI32v(p + n, &pSttBlk->nRow);
  n += tGetI64v(p + n, &pSttBlk->bInfo.offset);
  n += tGetI32v(p + n, &pSttBlk->bInfo.szBlock);
  n += tGetI32v(p + n, &pSttBlk->bInfo.szKey);
H
Hongze Cheng 已提交
349 350 351

  return n;
}
H
Hongze Cheng 已提交
352

H
Hongze Cheng 已提交
353 354 355 356 357 358 359 360 361
// SBlockCol ======================================================
int32_t tPutBlockCol(uint8_t *p, void *ph) {
  int32_t    n = 0;
  SBlockCol *pBlockCol = (SBlockCol *)ph;

  ASSERT(pBlockCol->flag && (pBlockCol->flag != HAS_NONE));

  n += tPutI16v(p ? p + n : p, pBlockCol->cid);
  n += tPutI8(p ? p + n : p, pBlockCol->type);
H
Hongze Cheng 已提交
362
  n += tPutI8(p ? p + n : p, pBlockCol->smaOn);
H
Hongze Cheng 已提交
363
  n += tPutI8(p ? p + n : p, pBlockCol->flag);
H
Hongze Cheng 已提交
364
  n += tPutI32v(p ? p + n : p, pBlockCol->szOrigin);
H
Hongze Cheng 已提交
365 366

  if (pBlockCol->flag != HAS_NULL) {
H
Hongze Cheng 已提交
367 368 369 370 371 372 373 374 375 376 377 378
    if (pBlockCol->flag != HAS_VALUE) {
      n += tPutI32v(p ? p + n : p, pBlockCol->szBitmap);
    }

    if (IS_VAR_DATA_TYPE(pBlockCol->type)) {
      n += tPutI32v(p ? p + n : p, pBlockCol->szOffset);
    }

    if (pBlockCol->flag != (HAS_NULL | HAS_NONE)) {
      n += tPutI32v(p ? p + n : p, pBlockCol->szValue);
    }

H
Hongze Cheng 已提交
379
    n += tPutI32v(p ? p + n : p, pBlockCol->offset);
H
Hongze Cheng 已提交
380 381
  }

H
Hongze Cheng 已提交
382
_exit:
H
Hongze Cheng 已提交
383 384 385 386 387 388 389 390 391
  return n;
}

int32_t tGetBlockCol(uint8_t *p, void *ph) {
  int32_t    n = 0;
  SBlockCol *pBlockCol = (SBlockCol *)ph;

  n += tGetI16v(p + n, &pBlockCol->cid);
  n += tGetI8(p + n, &pBlockCol->type);
H
Hongze Cheng 已提交
392
  n += tGetI8(p + n, &pBlockCol->smaOn);
H
Hongze Cheng 已提交
393
  n += tGetI8(p + n, &pBlockCol->flag);
H
Hongze Cheng 已提交
394
  n += tGetI32v(p + n, &pBlockCol->szOrigin);
H
Hongze Cheng 已提交
395 396 397

  ASSERT(pBlockCol->flag && (pBlockCol->flag != HAS_NONE));

H
Hongze Cheng 已提交
398 399 400 401 402
  pBlockCol->szBitmap = 0;
  pBlockCol->szOffset = 0;
  pBlockCol->szValue = 0;
  pBlockCol->offset = 0;

H
Hongze Cheng 已提交
403
  if (pBlockCol->flag != HAS_NULL) {
H
Hongze Cheng 已提交
404 405 406 407 408 409 410 411 412 413 414 415
    if (pBlockCol->flag != HAS_VALUE) {
      n += tGetI32v(p + n, &pBlockCol->szBitmap);
    }

    if (IS_VAR_DATA_TYPE(pBlockCol->type)) {
      n += tGetI32v(p + n, &pBlockCol->szOffset);
    }

    if (pBlockCol->flag != (HAS_NULL | HAS_NONE)) {
      n += tGetI32v(p + n, &pBlockCol->szValue);
    }

H
Hongze Cheng 已提交
416
    n += tGetI32v(p + n, &pBlockCol->offset);
H
Hongze Cheng 已提交
417 418 419 420 421
  }

  return n;
}

H
Hongze Cheng 已提交
422 423 424 425 426 427 428 429 430 431
int32_t tBlockColCmprFn(const void *p1, const void *p2) {
  if (((SBlockCol *)p1)->cid < ((SBlockCol *)p2)->cid) {
    return -1;
  } else if (((SBlockCol *)p1)->cid > ((SBlockCol *)p2)->cid) {
    return 1;
  }

  return 0;
}

H
Hongze Cheng 已提交
432
// SDelIdx ======================================================
433
int32_t tCmprDelIdx(void const *lhs, void const *rhs) {
M
Minglei Jin 已提交
434 435
  SDelIdx *lDelIdx = (SDelIdx *)lhs;
  SDelIdx *rDelIdx = (SDelIdx *)rhs;
436

M
Minglei Jin 已提交
437
  if (lDelIdx->suid < rDelIdx->suid) {
438
    return -1;
M
Minglei Jin 已提交
439
  } else if (lDelIdx->suid > rDelIdx->suid) {
440 441 442
    return 1;
  }

M
Minglei Jin 已提交
443
  if (lDelIdx->uid < rDelIdx->uid) {
444
    return -1;
M
Minglei Jin 已提交
445
  } else if (lDelIdx->uid > rDelIdx->uid) {
446 447 448 449 450 451
    return 1;
  }

  return 0;
}

H
Hongze Cheng 已提交
452 453 454 455 456 457 458 459
int32_t tPutDelIdx(uint8_t *p, void *ph) {
  SDelIdx *pDelIdx = (SDelIdx *)ph;
  int32_t  n = 0;

  n += tPutI64(p ? p + n : p, pDelIdx->suid);
  n += tPutI64(p ? p + n : p, pDelIdx->uid);
  n += tPutI64v(p ? p + n : p, pDelIdx->offset);
  n += tPutI64v(p ? p + n : p, pDelIdx->size);
H
Hongze Cheng 已提交
460 461 462 463

  return n;
}

H
Hongze Cheng 已提交
464 465 466
int32_t tGetDelIdx(uint8_t *p, void *ph) {
  SDelIdx *pDelIdx = (SDelIdx *)ph;
  int32_t  n = 0;
H
Hongze Cheng 已提交
467

H
Hongze Cheng 已提交
468 469 470 471
  n += tGetI64(p + n, &pDelIdx->suid);
  n += tGetI64(p + n, &pDelIdx->uid);
  n += tGetI64v(p + n, &pDelIdx->offset);
  n += tGetI64v(p + n, &pDelIdx->size);
H
Hongze Cheng 已提交
472 473 474 475

  return n;
}

H
Hongze Cheng 已提交
476
// SDelData ======================================================
H
Hongze Cheng 已提交
477 478 479 480 481 482 483
int32_t tPutDelData(uint8_t *p, void *ph) {
  SDelData *pDelData = (SDelData *)ph;
  int32_t   n = 0;

  n += tPutI64v(p ? p + n : p, pDelData->version);
  n += tPutI64(p ? p + n : p, pDelData->sKey);
  n += tPutI64(p ? p + n : p, pDelData->eKey);
H
Hongze Cheng 已提交
484 485 486 487

  return n;
}

H
Hongze Cheng 已提交
488 489 490
int32_t tGetDelData(uint8_t *p, void *ph) {
  SDelData *pDelData = (SDelData *)ph;
  int32_t   n = 0;
H
Hongze Cheng 已提交
491

H
Hongze Cheng 已提交
492 493 494
  n += tGetI64v(p + n, &pDelData->version);
  n += tGetI64(p + n, &pDelData->sKey);
  n += tGetI64(p + n, &pDelData->eKey);
H
Hongze Cheng 已提交
495 496

  return n;
H
Hongze Cheng 已提交
497 498
}

H
Hongze Cheng 已提交
499 500 501 502 503 504
int32_t tsdbKeyFid(TSKEY key, int32_t minutes, int8_t precision) {
  if (key < 0) {
    return (int)((key + 1) / tsTickPerMin[precision] / minutes - 1);
  } else {
    return (int)((key / tsTickPerMin[precision] / minutes));
  }
H
Hongze Cheng 已提交
505 506
}

H
Hongze Cheng 已提交
507 508 509 510 511
void tsdbFidKeyRange(int32_t fid, int32_t minutes, int8_t precision, TSKEY *minKey, TSKEY *maxKey) {
  *minKey = fid * minutes * tsTickPerMin[precision];
  *maxKey = *minKey + minutes * tsTickPerMin[precision] - 1;
}

H
Hongze Cheng 已提交
512 513 514 515
int32_t tsdbFidLevel(int32_t fid, STsdbKeepCfg *pKeepCfg, int64_t now) {
  int32_t aFid[3];
  TSKEY   key;

H
Hongze Cheng 已提交
516 517 518 519 520 521 522 523 524 525
  if (pKeepCfg->precision == TSDB_TIME_PRECISION_MILLI) {
    now = now * 1000;
  } else if (pKeepCfg->precision == TSDB_TIME_PRECISION_MICRO) {
    now = now * 1000000l;
  } else if (pKeepCfg->precision == TSDB_TIME_PRECISION_NANO) {
    now = now * 1000000000l;
  } else {
    ASSERT(0);
  }

H
Hongze Cheng 已提交
526
  key = now - pKeepCfg->keep0 * tsTickPerMin[pKeepCfg->precision];
H
Hongze Cheng 已提交
527
  aFid[0] = tsdbKeyFid(key, pKeepCfg->days, pKeepCfg->precision);
H
Hongze Cheng 已提交
528
  key = now - pKeepCfg->keep1 * tsTickPerMin[pKeepCfg->precision];
H
Hongze Cheng 已提交
529
  aFid[1] = tsdbKeyFid(key, pKeepCfg->days, pKeepCfg->precision);
H
Hongze Cheng 已提交
530
  key = now - pKeepCfg->keep2 * tsTickPerMin[pKeepCfg->precision];
H
Hongze Cheng 已提交
531
  aFid[2] = tsdbKeyFid(key, pKeepCfg->days, pKeepCfg->precision);
H
Hongze Cheng 已提交
532 533 534 535 536 537 538 539 540 541 542

  if (fid >= aFid[0]) {
    return 0;
  } else if (fid >= aFid[1]) {
    return 1;
  } else if (fid >= aFid[2]) {
    return 2;
  } else {
    return -1;
  }
}
H
Hongze Cheng 已提交
543

H
Hongze Cheng 已提交
544 545
// TSDBROW ======================================================
void tsdbRowGetColVal(TSDBROW *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal) {
H
Hongze Cheng 已提交
546 547 548
  STColumn *pTColumn = &pTSchema->columns[iCol];
  SValue    value;

H
Hongze Cheng 已提交
549 550
  ASSERT(iCol > 0);

H
Hongze Cheng 已提交
551
  if (pRow->type == 0) {
H
Hongze Cheng 已提交
552
    tTSRowGetVal(pRow->pTSRow, pTSchema, iCol, pColVal);
H
Hongze Cheng 已提交
553
  } else if (pRow->type == 1) {
H
Hongze Cheng 已提交
554
    SColData *pColData;
H
Hongze Cheng 已提交
555

H
Hongze Cheng 已提交
556
    tBlockDataGetColData(pRow->pBlockData, pTColumn->colId, &pColData);
H
Hongze Cheng 已提交
557

H
Hongze Cheng 已提交
558
    if (pColData) {
H
Hongze Cheng 已提交
559
      tColDataGetValue(pColData, pRow->iRow, pColVal);
H
Hongze Cheng 已提交
560
    } else {
H
Hongze Cheng 已提交
561
      *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
H
Hongze Cheng 已提交
562 563 564 565
    }
  } else {
    ASSERT(0);
  }
H
Hongze Cheng 已提交
566 567
}

H
Hongze Cheng 已提交
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
int32_t tPutTSDBRow(uint8_t *p, TSDBROW *pRow) {
  int32_t n = 0;

  n += tPutI64(p, pRow->version);
  if (p) memcpy(p + n, pRow->pTSRow, pRow->pTSRow->len);
  n += pRow->pTSRow->len;

  return n;
}

int32_t tGetTSDBRow(uint8_t *p, TSDBROW *pRow) {
  int32_t n = 0;

  n += tGetI64(p, &pRow->version);
  pRow->pTSRow = (STSRow *)(p + n);
  n += pRow->pTSRow->len;

  return n;
}

H
Hongze Cheng 已提交
588 589 590 591
int32_t tsdbRowCmprFn(const void *p1, const void *p2) {
  return tsdbKeyCmprFn(&TSDBROW_KEY((TSDBROW *)p1), &TSDBROW_KEY((TSDBROW *)p2));
}

H
Hongze Cheng 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
// SRowIter ======================================================
void tRowIterInit(SRowIter *pIter, TSDBROW *pRow, STSchema *pTSchema) {
  pIter->pRow = pRow;
  if (pRow->type == 0) {
    ASSERT(pTSchema);
    pIter->pTSchema = pTSchema;
    pIter->i = 1;
  } else if (pRow->type == 1) {
    pIter->pTSchema = NULL;
    pIter->i = 0;
  } else {
    ASSERT(0);
  }
}

SColVal *tRowIterNext(SRowIter *pIter) {
  if (pIter->pRow->type == 0) {
    if (pIter->i < pIter->pTSchema->numOfCols) {
      tsdbRowGetColVal(pIter->pRow, pIter->pTSchema, pIter->i, &pIter->colVal);
      pIter->i++;

      return &pIter->colVal;
    }
  } else {
H
Hongze Cheng 已提交
616 617
    if (pIter->i < taosArrayGetSize(pIter->pRow->pBlockData->aIdx)) {
      SColData *pColData = tBlockDataGetColDataByIdx(pIter->pRow->pBlockData, pIter->i);
H
Hongze Cheng 已提交
618 619 620 621 622 623 624 625 626 627 628

      tColDataGetValue(pColData, pIter->pRow->iRow, &pIter->colVal);
      pIter->i++;

      return &pIter->colVal;
    }
  }

  return NULL;
}

H
Hongze Cheng 已提交
629
// SRowMerger ======================================================
630 631 632 633 634 635

int32_t tRowMergerInit2(SRowMerger *pMerger, STSchema *pResTSchema, TSDBROW *pRow, STSchema *pTSchema) {
  int32_t   code = 0;
  TSDBKEY   key = TSDBROW_KEY(pRow);
  SColVal  *pColVal = &(SColVal){0};
  STColumn *pTColumn;
M
Minglei Jin 已提交
636
  int32_t   iCol, jCol = 0;
637 638 639 640 641 642 643 644 645 646 647

  pMerger->pTSchema = pResTSchema;
  pMerger->version = key.version;

  pMerger->pArray = taosArrayInit(pResTSchema->numOfCols, sizeof(SColVal));
  if (pMerger->pArray == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  // ts
M
Minglei Jin 已提交
648
  pTColumn = &pTSchema->columns[jCol++];
649 650 651

  ASSERT(pTColumn->type == TSDB_DATA_TYPE_TIMESTAMP);

H
Hongze Cheng 已提交
652
  *pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.val = key.ts});
653 654 655 656 657 658
  if (taosArrayPush(pMerger->pArray, pColVal) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  // other
M
Minglei Jin 已提交
659
  for (iCol = 1; jCol < pTSchema->numOfCols && iCol < pResTSchema->numOfCols; ++iCol) {
660
    pTColumn = &pResTSchema->columns[iCol];
M
Minglei Jin 已提交
661 662 663 664 665
    if (pTSchema->columns[jCol].colId < pTColumn->colId) {
      ++jCol;
      --iCol;
      continue;
    } else if (pTSchema->columns[jCol].colId > pTColumn->colId) {
666 667 668 669
      taosArrayPush(pMerger->pArray, &COL_VAL_NONE(pTColumn->colId, pTColumn->type));
      continue;
    }

M
Minglei Jin 已提交
670
    tsdbRowGetColVal(pRow, pTSchema, jCol++, pColVal);
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
    if (taosArrayPush(pMerger->pArray, pColVal) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
  }

  for (; iCol < pResTSchema->numOfCols; ++iCol) {
    pTColumn = &pResTSchema->columns[iCol];
    taosArrayPush(pMerger->pArray, &COL_VAL_NONE(pTColumn->colId, pTColumn->type));
  }

_exit:
  return code;
}

int32_t tRowMergerAdd(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema) {
  int32_t   code = 0;
  TSDBKEY   key = TSDBROW_KEY(pRow);
  SColVal  *pColVal = &(SColVal){0};
  STColumn *pTColumn;
M
Minglei Jin 已提交
691
  int32_t   iCol, jCol = 1;
692

H
Hongze Cheng 已提交
693
  ASSERT(((SColVal *)pMerger->pArray->pData)->value.val == key.ts);
694

M
Minglei Jin 已提交
695
  for (iCol = 1; iCol < pMerger->pTSchema->numOfCols && jCol < pTSchema->numOfCols; ++iCol) {
696
    pTColumn = &pMerger->pTSchema->columns[iCol];
M
Minglei Jin 已提交
697 698 699 700 701
    if (pTSchema->columns[jCol].colId < pTColumn->colId) {
      ++jCol;
      --iCol;
      continue;
    } else if (pTSchema->columns[jCol].colId > pTColumn->colId) {
702 703 704
      continue;
    }

M
Minglei Jin 已提交
705
    tsdbRowGetColVal(pRow, pTSchema, jCol++, pColVal);
706 707

    if (key.version > pMerger->version) {
H
Hongze Cheng 已提交
708
      if (!COL_VAL_IS_NONE(pColVal)) {
709 710 711 712
        taosArraySet(pMerger->pArray, iCol, pColVal);
      }
    } else if (key.version < pMerger->version) {
      SColVal *tColVal = (SColVal *)taosArrayGet(pMerger->pArray, iCol);
H
Hongze Cheng 已提交
713
      if (COL_VAL_IS_NONE(tColVal) && !COL_VAL_IS_NONE(pColVal)) {
714 715 716 717 718 719 720 721 722 723 724
        taosArraySet(pMerger->pArray, iCol, pColVal);
      }
    } else {
      ASSERT(0);
    }
  }

  pMerger->version = key.version;
  return code;
}

H
Hongze Cheng 已提交
725 726
int32_t tRowMergerInit(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema) {
  int32_t   code = 0;
H
Hongze Cheng 已提交
727
  TSDBKEY   key = TSDBROW_KEY(pRow);
H
Hongze Cheng 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
  SColVal  *pColVal = &(SColVal){0};
  STColumn *pTColumn;

  pMerger->pTSchema = pTSchema;
  pMerger->version = key.version;

  pMerger->pArray = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
  if (pMerger->pArray == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  // ts
  pTColumn = &pTSchema->columns[0];

  ASSERT(pTColumn->type == TSDB_DATA_TYPE_TIMESTAMP);

H
Hongze Cheng 已提交
745
  *pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.val = key.ts});
H
Hongze Cheng 已提交
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
  if (taosArrayPush(pMerger->pArray, pColVal) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  // other
  for (int16_t iCol = 1; iCol < pTSchema->numOfCols; iCol++) {
    tsdbRowGetColVal(pRow, pTSchema, iCol, pColVal);
    if (taosArrayPush(pMerger->pArray, pColVal) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
  }

_exit:
  return code;
}

void tRowMergerClear(SRowMerger *pMerger) { taosArrayDestroy(pMerger->pArray); }

int32_t tRowMerge(SRowMerger *pMerger, TSDBROW *pRow) {
  int32_t  code = 0;
H
Hongze Cheng 已提交
768
  TSDBKEY  key = TSDBROW_KEY(pRow);
H
Hongze Cheng 已提交
769 770
  SColVal *pColVal = &(SColVal){0};

H
Hongze Cheng 已提交
771
  ASSERT(((SColVal *)pMerger->pArray->pData)->value.val == key.ts);
H
Hongze Cheng 已提交
772 773 774 775 776

  for (int32_t iCol = 1; iCol < pMerger->pTSchema->numOfCols; iCol++) {
    tsdbRowGetColVal(pRow, pMerger->pTSchema, iCol, pColVal);

    if (key.version > pMerger->version) {
H
Hongze Cheng 已提交
777
      if (!COL_VAL_IS_NONE(pColVal)) {
H
Hongze Cheng 已提交
778 779
        taosArraySet(pMerger->pArray, iCol, pColVal);
      }
H
Hongze Cheng 已提交
780
    } else if (key.version < pMerger->version) {
H
Hongze Cheng 已提交
781
      SColVal *tColVal = (SColVal *)taosArrayGet(pMerger->pArray, iCol);
H
Hongze Cheng 已提交
782
      if (COL_VAL_IS_NONE(tColVal) && !COL_VAL_IS_NONE(pColVal)) {
H
Hongze Cheng 已提交
783 784
        taosArraySet(pMerger->pArray, iCol, pColVal);
      }
H
Hongze Cheng 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797
    } else {
      ASSERT(0);
    }
  }

  pMerger->version = key.version;

_exit:
  return code;
}

int32_t tRowMergerGetRow(SRowMerger *pMerger, STSRow **ppRow) {
  int32_t code = 0;
798 799 800

  code = tdSTSRowNew(pMerger->pArray, pMerger->pTSchema, ppRow);

H
Hongze Cheng 已提交
801 802 803
  return code;
}

H
Hongze Cheng 已提交
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 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 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
// delete skyline ======================================================
static int32_t tsdbMergeSkyline(SArray *aSkyline1, SArray *aSkyline2, SArray *aSkyline) {
  int32_t  code = 0;
  int32_t  i1 = 0;
  int32_t  n1 = taosArrayGetSize(aSkyline1);
  int32_t  i2 = 0;
  int32_t  n2 = taosArrayGetSize(aSkyline2);
  TSDBKEY *pSkyline1;
  TSDBKEY *pSkyline2;
  TSDBKEY  item;
  int64_t  version1 = 0;
  int64_t  version2 = 0;

  ASSERT(n1 > 0 && n2 > 0);

  taosArrayClear(aSkyline);

  while (i1 < n1 && i2 < n2) {
    pSkyline1 = (TSDBKEY *)taosArrayGet(aSkyline1, i1);
    pSkyline2 = (TSDBKEY *)taosArrayGet(aSkyline2, i2);

    if (pSkyline1->ts < pSkyline2->ts) {
      version1 = pSkyline1->version;
      i1++;
    } else if (pSkyline1->ts > pSkyline2->ts) {
      version2 = pSkyline2->version;
      i2++;
    } else {
      version1 = pSkyline1->version;
      version2 = pSkyline2->version;
      i1++;
      i2++;
    }

    item.ts = TMIN(pSkyline1->ts, pSkyline2->ts);
    item.version = TMAX(version1, version2);
    if (taosArrayPush(aSkyline, &item) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
  }

  while (i1 < n1) {
    pSkyline1 = (TSDBKEY *)taosArrayGet(aSkyline1, i1);
    item.ts = pSkyline1->ts;
    item.version = pSkyline1->version;
    if (taosArrayPush(aSkyline, &item) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
    i1++;
  }

  while (i2 < n2) {
    pSkyline2 = (TSDBKEY *)taosArrayGet(aSkyline2, i2);
    item.ts = pSkyline2->ts;
    item.version = pSkyline2->version;
    if (taosArrayPush(aSkyline, &item) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
    i2++;
  }

_exit:
  return code;
}
int32_t tsdbBuildDeleteSkyline(SArray *aDelData, int32_t sidx, int32_t eidx, SArray *aSkyline) {
  int32_t   code = 0;
  SDelData *pDelData;
  int32_t   midx;

  taosArrayClear(aSkyline);
  if (sidx == eidx) {
    pDelData = (SDelData *)taosArrayGet(aDelData, sidx);
    taosArrayPush(aSkyline, &(TSDBKEY){.ts = pDelData->sKey, .version = pDelData->version});
    taosArrayPush(aSkyline, &(TSDBKEY){.ts = pDelData->eKey, .version = 0});
  } else {
    SArray *aSkyline1 = NULL;
    SArray *aSkyline2 = NULL;

    aSkyline1 = taosArrayInit(0, sizeof(TSDBKEY));
    aSkyline2 = taosArrayInit(0, sizeof(TSDBKEY));
    if (aSkyline1 == NULL || aSkyline2 == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _clear;
    }

    midx = (sidx + eidx) / 2;

    code = tsdbBuildDeleteSkyline(aDelData, sidx, midx, aSkyline1);
    if (code) goto _clear;

    code = tsdbBuildDeleteSkyline(aDelData, midx + 1, eidx, aSkyline2);
    if (code) goto _clear;

    code = tsdbMergeSkyline(aSkyline1, aSkyline2, aSkyline);

  _clear:
    taosArrayDestroy(aSkyline1);
    taosArrayDestroy(aSkyline2);
  }

H
Hongze Cheng 已提交
907 908 909
  return code;
}

H
Hongze Cheng 已提交
910
// SBlockData ======================================================
H
Hongze Cheng 已提交
911
int32_t tBlockDataCreate(SBlockData *pBlockData) {
H
Hongze Cheng 已提交
912
  int32_t code = 0;
H
Hongze Cheng 已提交
913

H
Hongze Cheng 已提交
914 915
  pBlockData->suid = 0;
  pBlockData->uid = 0;
H
Hongze Cheng 已提交
916
  pBlockData->nRow = 0;
H
Hongze Cheng 已提交
917
  pBlockData->aUid = NULL;
H
Hongze Cheng 已提交
918 919
  pBlockData->aVersion = NULL;
  pBlockData->aTSKEY = NULL;
H
Hongze Cheng 已提交
920 921
  pBlockData->aIdx = taosArrayInit(0, sizeof(int32_t));
  if (pBlockData->aIdx == NULL) {
H
Hongze Cheng 已提交
922 923 924 925 926
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
  pBlockData->aColData = taosArrayInit(0, sizeof(SColData));
  if (pBlockData->aColData == NULL) {
H
Hongze Cheng 已提交
927
    taosArrayDestroy(pBlockData->aIdx);
H
Hongze Cheng 已提交
928 929 930
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
H
Hongze Cheng 已提交
931 932 933 934 935

_exit:
  return code;
}

H
Hongze Cheng 已提交
936
void tBlockDataDestroy(SBlockData *pBlockData, int8_t deepClear) {
H
Hongze Cheng 已提交
937
  tFree((uint8_t *)pBlockData->aUid);
H
Hongze Cheng 已提交
938 939
  tFree((uint8_t *)pBlockData->aVersion);
  tFree((uint8_t *)pBlockData->aTSKEY);
H
Hongze Cheng 已提交
940
  taosArrayDestroy(pBlockData->aIdx);
H
Hongze Cheng 已提交
941
  taosArrayDestroyEx(pBlockData->aColData, deepClear ? tColDataDestroy : NULL);
H
Hongze Cheng 已提交
942
  pBlockData->aUid = NULL;
H
Haojun Liao 已提交
943
  pBlockData->aVersion = NULL;
H
Hongze Cheng 已提交
944 945 946
  pBlockData->aTSKEY = NULL;
  pBlockData->aIdx = NULL;
  pBlockData->aColData = NULL;
H
Hongze Cheng 已提交
947 948
}

H
Hongze Cheng 已提交
949
int32_t tBlockDataInit(SBlockData *pBlockData, TABLEID *pId, STSchema *pTSchema, int16_t *aCid, int32_t nCid) {
H
Hongze Cheng 已提交
950 951
  int32_t code = 0;

H
Hongze Cheng 已提交
952
  ASSERT(pId->suid || pId->uid);
H
Hongze Cheng 已提交
953

H
Hongze Cheng 已提交
954 955
  pBlockData->suid = pId->suid;
  pBlockData->uid = pId->uid;
H
Hongze Cheng 已提交
956
  pBlockData->nRow = 0;
H
Hongze Cheng 已提交
957

H
Hongze Cheng 已提交
958
  taosArrayClear(pBlockData->aIdx);
H
Hongze Cheng 已提交
959 960
  if (aCid) {
    int32_t   iColumn = 1;
H
Hongze Cheng 已提交
961
    STColumn *pTColumn = &pTSchema->columns[iColumn];
H
Hongze Cheng 已提交
962 963 964 965 966
    for (int32_t iCid = 0; iCid < nCid; iCid++) {
      while (pTColumn && pTColumn->colId < aCid[iCid]) {
        iColumn++;
        pTColumn = (iColumn < pTSchema->numOfCols) ? &pTSchema->columns[iColumn] : NULL;
      }
H
Hongze Cheng 已提交
967

H
Hongze Cheng 已提交
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
      if (pTColumn == NULL) {
        break;
      } else if (pTColumn->colId == aCid[iCid]) {
        SColData *pColData;
        code = tBlockDataAddColData(pBlockData, taosArrayGetSize(pBlockData->aIdx), &pColData);
        if (code) goto _exit;
        tColDataInit(pColData, pTColumn->colId, pTColumn->type, (pTColumn->flags & COL_SMA_ON) ? 1 : 0);

        iColumn++;
        pTColumn = (iColumn < pTSchema->numOfCols) ? &pTSchema->columns[iColumn] : NULL;
      }
    }
  } else {
    for (int32_t iColumn = 1; iColumn < pTSchema->numOfCols; iColumn++) {
      STColumn *pTColumn = &pTSchema->columns[iColumn];
H
Hongze Cheng 已提交
983

H
Hongze Cheng 已提交
984 985 986 987 988 989
      SColData *pColData;
      code = tBlockDataAddColData(pBlockData, iColumn - 1, &pColData);
      if (code) goto _exit;

      tColDataInit(pColData, pTColumn->colId, pTColumn->type, (pTColumn->flags & COL_SMA_ON) ? 1 : 0);
    }
H
Hongze Cheng 已提交
990 991 992 993
  }

_exit:
  return code;
H
Hongze Cheng 已提交
994 995
}

H
Hongze Cheng 已提交
996
int32_t tBlockDataInitEx(SBlockData *pBlockData, SBlockData *pBlockDataFrom) {
H
Hongze Cheng 已提交
997 998
  int32_t code = 0;

H
Hongze Cheng 已提交
999
  ASSERT(pBlockDataFrom->suid || pBlockDataFrom->uid);
H
Hongze Cheng 已提交
1000

H
Hongze Cheng 已提交
1001 1002
  pBlockData->suid = pBlockDataFrom->suid;
  pBlockData->uid = pBlockDataFrom->uid;
H
Hongze Cheng 已提交
1003
  pBlockData->nRow = 0;
H
Hongze Cheng 已提交
1004

H
Hongze Cheng 已提交
1005
  taosArrayClear(pBlockData->aIdx);
H
Hongze Cheng 已提交
1006 1007
  for (int32_t iColData = 0; iColData < taosArrayGetSize(pBlockDataFrom->aIdx); iColData++) {
    SColData *pColDataFrom = tBlockDataGetColDataByIdx(pBlockDataFrom, iColData);
H
Hongze Cheng 已提交
1008

H
Hongze Cheng 已提交
1009 1010 1011
    SColData *pColData;
    code = tBlockDataAddColData(pBlockData, iColData, &pColData);
    if (code) goto _exit;
H
Hongze Cheng 已提交
1012

H
Hongze Cheng 已提交
1013
    tColDataInit(pColData, pColDataFrom->cid, pColDataFrom->type, pColDataFrom->smaOn);
H
Hongze Cheng 已提交
1014 1015 1016 1017 1018 1019
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
1020 1021 1022 1023 1024 1025 1026 1027
void tBlockDataReset(SBlockData *pBlockData) {
  pBlockData->suid = 0;
  pBlockData->uid = 0;
  pBlockData->nRow = 0;
  taosArrayClear(pBlockData->aIdx);
}

void tBlockDataClear(SBlockData *pBlockData) {
H
Hongze Cheng 已提交
1028 1029
  ASSERT(pBlockData->suid || pBlockData->uid);

H
Hongze Cheng 已提交
1030
  pBlockData->nRow = 0;
H
Hongze Cheng 已提交
1031 1032
  for (int32_t iColData = 0; iColData < taosArrayGetSize(pBlockData->aIdx); iColData++) {
    SColData *pColData = tBlockDataGetColDataByIdx(pBlockData, iColData);
H
Hongze Cheng 已提交
1033
    tColDataClear(pColData);
H
Hongze Cheng 已提交
1034 1035 1036
  }
}

H
Hongze Cheng 已提交
1037 1038
int32_t tBlockDataAddColData(SBlockData *pBlockData, int32_t iColData, SColData **ppColData) {
  int32_t   code = 0;
H
Hongze Cheng 已提交
1039
  SColData *pColData = NULL;
H
Hongze Cheng 已提交
1040
  int32_t   idx = taosArrayGetSize(pBlockData->aIdx);
H
Hongze Cheng 已提交
1041 1042

  if (idx >= taosArrayGetSize(pBlockData->aColData)) {
H
Hongze Cheng 已提交
1043 1044 1045 1046
    if (taosArrayPush(pBlockData->aColData, &((SColData){0})) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
1047 1048 1049
  }
  pColData = (SColData *)taosArrayGet(pBlockData->aColData, idx);

H
Hongze Cheng 已提交
1050
  if (taosArrayInsert(pBlockData->aIdx, iColData, &idx) == NULL) {
H
Hongze Cheng 已提交
1051 1052
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
H
Hongze Cheng 已提交
1053 1054
  }

H
Hongze Cheng 已提交
1055 1056 1057 1058 1059 1060
  *ppColData = pColData;
  return code;

_err:
  *ppColData = NULL;
  return code;
H
Hongze Cheng 已提交
1061
}
H
Hongze Cheng 已提交
1062

H
Hongze Cheng 已提交
1063
int32_t tBlockDataAppendRow(SBlockData *pBlockData, TSDBROW *pRow, STSchema *pTSchema, int64_t uid) {
H
Hongze Cheng 已提交
1064
  int32_t code = 0;
H
Hongze Cheng 已提交
1065

H
Hongze Cheng 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
  ASSERT(pBlockData->suid || pBlockData->uid);

  // uid
  if (pBlockData->uid == 0) {
    ASSERT(uid);
    code = tRealloc((uint8_t **)&pBlockData->aUid, sizeof(int64_t) * (pBlockData->nRow + 1));
    if (code) goto _err;
    pBlockData->aUid[pBlockData->nRow] = uid;
  }
  // version
H
Hongze Cheng 已提交
1076
  code = tRealloc((uint8_t **)&pBlockData->aVersion, sizeof(int64_t) * (pBlockData->nRow + 1));
H
Hongze Cheng 已提交
1077
  if (code) goto _err;
H
Hongze Cheng 已提交
1078 1079
  pBlockData->aVersion[pBlockData->nRow] = TSDBROW_VERSION(pRow);
  // timestamp
H
Hongze Cheng 已提交
1080
  code = tRealloc((uint8_t **)&pBlockData->aTSKEY, sizeof(TSKEY) * (pBlockData->nRow + 1));
H
Hongze Cheng 已提交
1081
  if (code) goto _err;
H
Hongze Cheng 已提交
1082
  pBlockData->aTSKEY[pBlockData->nRow] = TSDBROW_TS(pRow);
H
Hongze Cheng 已提交
1083

H
Hongze Cheng 已提交
1084
  // OTHER
H
Hongze Cheng 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
  SRowIter rIter = {0};
  SColVal *pColVal;

  tRowIterInit(&rIter, pRow, pTSchema);
  pColVal = tRowIterNext(&rIter);
  for (int32_t iColData = 0; iColData < taosArrayGetSize(pBlockData->aIdx); iColData++) {
    SColData *pColData = tBlockDataGetColDataByIdx(pBlockData, iColData);

    while (pColVal && pColVal->cid < pColData->cid) {
      pColVal = tRowIterNext(&rIter);
    }

    if (pColVal == NULL || pColVal->cid > pColData->cid) {
H
Hongze Cheng 已提交
1098
      code = tColDataAppendValue(pColData, &COL_VAL_NONE(pColData->cid, pColData->type));
H
Hongze Cheng 已提交
1099
      if (code) goto _err;
H
Hongze Cheng 已提交
1100 1101 1102 1103
    } else {
      code = tColDataAppendValue(pColData, pColVal);
      if (code) goto _err;
      pColVal = tRowIterNext(&rIter);
H
Hongze Cheng 已提交
1104
    }
H
Hongze Cheng 已提交
1105
  }
H
Hongze Cheng 已提交
1106
  pBlockData->nRow++;
H
Hongze Cheng 已提交
1107

H
Hongze Cheng 已提交
1108
  return code;
H
Hongze Cheng 已提交
1109

H
Hongze Cheng 已提交
1110
_err:
H
Hongze Cheng 已提交
1111
  return code;
1112
}
H
Hongze Cheng 已提交
1113

H
Hongze Cheng 已提交
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
int32_t tBlockDataCorrectSchema(SBlockData *pBlockData, SBlockData *pBlockDataFrom) {
  int32_t code = 0;

  int32_t iColData = 0;
  for (int32_t iColDataFrom = 0; iColDataFrom < taosArrayGetSize(pBlockDataFrom->aIdx); iColDataFrom++) {
    SColData *pColDataFrom = tBlockDataGetColDataByIdx(pBlockDataFrom, iColDataFrom);

    while (true) {
      SColData *pColData;
      if (iColData < taosArrayGetSize(pBlockData->aIdx)) {
        pColData = tBlockDataGetColDataByIdx(pBlockData, iColData);
      } else {
        pColData = NULL;
      }

      if (pColData == NULL || pColData->cid > pColDataFrom->cid) {
        code = tBlockDataAddColData(pBlockData, iColData, &pColData);
        if (code) goto _exit;

H
Hongze Cheng 已提交
1133
        tColDataInit(pColData, pColDataFrom->cid, pColDataFrom->type, pColDataFrom->smaOn);
H
Hongze Cheng 已提交
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
        for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
          code = tColDataAppendValue(pColData, &COL_VAL_NONE(pColData->cid, pColData->type));
          if (code) goto _exit;
        }

        iColData++;
        break;
      } else if (pColData->cid == pColDataFrom->cid) {
        iColData++;
        break;
      } else {
        iColData++;
      }
    }
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
1154 1155 1156
int32_t tBlockDataMerge(SBlockData *pBlockData1, SBlockData *pBlockData2, SBlockData *pBlockData) {
  int32_t code = 0;

H
Hongze Cheng 已提交
1157 1158 1159 1160
  ASSERT(pBlockData->suid == pBlockData1->suid);
  ASSERT(pBlockData->uid == pBlockData1->uid);
  ASSERT(pBlockData1->nRow > 0);
  ASSERT(pBlockData2->nRow > 0);
H
Hongze Cheng 已提交
1161

H
Hongze Cheng 已提交
1162
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
1163

H
Hongze Cheng 已提交
1164 1165 1166 1167
  TSDBROW  row1 = tsdbRowFromBlockData(pBlockData1, 0);
  TSDBROW  row2 = tsdbRowFromBlockData(pBlockData2, 0);
  TSDBROW *pRow1 = &row1;
  TSDBROW *pRow2 = &row2;
H
Hongze Cheng 已提交
1168

H
Hongze Cheng 已提交
1169 1170
  while (pRow1 && pRow2) {
    int32_t c = tsdbRowCmprFn(pRow1, pRow2);
H
Hongze Cheng 已提交
1171 1172

    if (c < 0) {
H
Hongze Cheng 已提交
1173 1174
      code = tBlockDataAppendRow(pBlockData, pRow1, NULL,
                                 pBlockData1->uid ? pBlockData1->uid : pBlockData1->aUid[pRow1->iRow]);
H
Hongze Cheng 已提交
1175
      if (code) goto _exit;
H
Hongze Cheng 已提交
1176 1177 1178 1179 1180 1181 1182

      pRow1->iRow++;
      if (pRow1->iRow < pBlockData1->nRow) {
        *pRow1 = tsdbRowFromBlockData(pBlockData1, pRow1->iRow);
      } else {
        pRow1 = NULL;
      }
H
Hongze Cheng 已提交
1183
    } else if (c > 0) {
H
Hongze Cheng 已提交
1184 1185
      code = tBlockDataAppendRow(pBlockData, pRow2, NULL,
                                 pBlockData2->uid ? pBlockData2->uid : pBlockData2->aUid[pRow2->iRow]);
H
Hongze Cheng 已提交
1186
      if (code) goto _exit;
H
Hongze Cheng 已提交
1187 1188 1189 1190 1191 1192 1193

      pRow2->iRow++;
      if (pRow2->iRow < pBlockData2->nRow) {
        *pRow2 = tsdbRowFromBlockData(pBlockData2, pRow2->iRow);
      } else {
        pRow2 = NULL;
      }
H
Hongze Cheng 已提交
1194 1195 1196 1197 1198
    } else {
      ASSERT(0);
    }
  }

H
Hongze Cheng 已提交
1199 1200 1201
  while (pRow1) {
    code = tBlockDataAppendRow(pBlockData, pRow1, NULL,
                               pBlockData1->uid ? pBlockData1->uid : pBlockData1->aUid[pRow1->iRow]);
H
Hongze Cheng 已提交
1202
    if (code) goto _exit;
H
Hongze Cheng 已提交
1203 1204 1205 1206 1207 1208 1209

    pRow1->iRow++;
    if (pRow1->iRow < pBlockData1->nRow) {
      *pRow1 = tsdbRowFromBlockData(pBlockData1, pRow1->iRow);
    } else {
      pRow1 = NULL;
    }
H
Hongze Cheng 已提交
1210 1211
  }

H
Hongze Cheng 已提交
1212 1213 1214
  while (pRow2) {
    code = tBlockDataAppendRow(pBlockData, pRow2, NULL,
                               pBlockData2->uid ? pBlockData2->uid : pBlockData2->aUid[pRow2->iRow]);
H
Hongze Cheng 已提交
1215
    if (code) goto _exit;
H
Hongze Cheng 已提交
1216 1217 1218 1219 1220 1221 1222

    pRow2->iRow++;
    if (pRow2->iRow < pBlockData2->nRow) {
      *pRow2 = tsdbRowFromBlockData(pBlockData2, pRow2->iRow);
    } else {
      pRow2 = NULL;
    }
H
Hongze Cheng 已提交
1223 1224 1225 1226 1227 1228
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
1229 1230 1231 1232 1233 1234 1235 1236
int32_t tBlockDataCopy(SBlockData *pSrc, SBlockData *pDest) {
  int32_t code = 0;

  tBlockDataClear(pDest);

  ASSERT(pDest->suid == pSrc->suid);
  ASSERT(pDest->uid == pSrc->uid);
  ASSERT(taosArrayGetSize(pSrc->aIdx) == taosArrayGetSize(pDest->aIdx));
H
Hongze Cheng 已提交
1237

H
Hongze Cheng 已提交
1238
  pDest->nRow = pSrc->nRow;
H
Hongze Cheng 已提交
1239

H
Hongze Cheng 已提交
1240 1241 1242 1243 1244
  if (pSrc->uid == 0) {
    code = tRealloc((uint8_t **)&pDest->aUid, sizeof(int64_t) * pDest->nRow);
    if (code) goto _exit;
    memcpy(pDest->aUid, pSrc->aUid, sizeof(int64_t) * pDest->nRow);
  }
H
Hongze Cheng 已提交
1245

H
Hongze Cheng 已提交
1246
  code = tRealloc((uint8_t **)&pDest->aVersion, sizeof(int64_t) * pDest->nRow);
H
Hongze Cheng 已提交
1247
  if (code) goto _exit;
H
Hongze Cheng 已提交
1248 1249 1250
  memcpy(pDest->aVersion, pSrc->aVersion, sizeof(int64_t) * pDest->nRow);

  code = tRealloc((uint8_t **)&pDest->aTSKEY, sizeof(TSKEY) * pDest->nRow);
H
Hongze Cheng 已提交
1251
  if (code) goto _exit;
H
Hongze Cheng 已提交
1252
  memcpy(pDest->aTSKEY, pSrc->aTSKEY, sizeof(TSKEY) * pDest->nRow);
H
Hongze Cheng 已提交
1253

H
Hongze Cheng 已提交
1254 1255 1256 1257 1258 1259
  for (int32_t iColData = 0; iColData < taosArrayGetSize(pSrc->aIdx); iColData++) {
    SColData *pColSrc = tBlockDataGetColDataByIdx(pSrc, iColData);
    SColData *pColDest = tBlockDataGetColDataByIdx(pDest, iColData);

    ASSERT(pColSrc->cid == pColDest->cid);
    ASSERT(pColSrc->type == pColDest->type);
H
Hongze Cheng 已提交
1260

H
Hongze Cheng 已提交
1261
    code = tColDataCopy(pColSrc, pColDest);
H
Hongze Cheng 已提交
1262 1263 1264 1265 1266
    if (code) goto _exit;
  }

_exit:
  return code;
1267
}
H
Hongze Cheng 已提交
1268

H
Hongze Cheng 已提交
1269 1270 1271 1272 1273
SColData *tBlockDataGetColDataByIdx(SBlockData *pBlockData, int32_t idx) {
  ASSERT(idx >= 0 && idx < taosArrayGetSize(pBlockData->aIdx));
  return (SColData *)taosArrayGet(pBlockData->aColData, *(int32_t *)taosArrayGet(pBlockData->aIdx, idx));
}

H
Hongze Cheng 已提交
1274 1275
void tBlockDataGetColData(SBlockData *pBlockData, int16_t cid, SColData **ppColData) {
  ASSERT(cid != PRIMARYKEY_TIMESTAMP_COL_ID);
H
Hongze Cheng 已提交
1276 1277
  int32_t lidx = 0;
  int32_t ridx = taosArrayGetSize(pBlockData->aIdx) - 1;
H
Hongze Cheng 已提交
1278

H
Hongze Cheng 已提交
1279
  while (lidx <= ridx) {
M
Minglei Jin 已提交
1280 1281
    int32_t   midx = (lidx + ridx) / 2;
    SColData *pColData = tBlockDataGetColDataByIdx(pBlockData, midx);
H
refact  
Hongze Cheng 已提交
1282
    int32_t   c = (pColData->cid == cid) ? 0 : ((pColData->cid > cid) ? 1 : -1);
H
Hongze Cheng 已提交
1283 1284 1285 1286 1287 1288 1289 1290 1291

    if (c == 0) {
      *ppColData = pColData;
      return;
    } else if (c < 0) {
      lidx = midx + 1;
    } else {
      ridx = midx - 1;
    }
H
Hongze Cheng 已提交
1292
  }
H
Hongze Cheng 已提交
1293 1294

  *ppColData = NULL;
H
Hongze Cheng 已提交
1295 1296
}

H
Hongze Cheng 已提交
1297 1298
int32_t tCmprBlockData(SBlockData *pBlockData, int8_t cmprAlg, uint8_t **ppOut, int32_t *szOut, uint8_t *aBuf[],
                       int32_t aBufN[]) {
H
Hongze Cheng 已提交
1299
  int32_t code = 0;
H
Hongze Cheng 已提交
1300

H
Hongze Cheng 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309
  SDiskDataHdr hdr = {.delimiter = TSDB_FILE_DLMT,
                      .fmtVer = 0,
                      .suid = pBlockData->suid,
                      .uid = pBlockData->uid,
                      .nRow = pBlockData->nRow,
                      .cmprAlg = cmprAlg};

  // encode =================
  // columns AND SBlockCol
H
Hongze Cheng 已提交
1310
  aBufN[0] = 0;
H
Hongze Cheng 已提交
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
  for (int32_t iColData = 0; iColData < taosArrayGetSize(pBlockData->aIdx); iColData++) {
    SColData *pColData = tBlockDataGetColDataByIdx(pBlockData, iColData);

    ASSERT(pColData->flag);

    if (pColData->flag == HAS_NONE) continue;

    SBlockCol blockCol = {.cid = pColData->cid,
                          .type = pColData->type,
                          .smaOn = pColData->smaOn,
                          .flag = pColData->flag,
                          .szOrigin = pColData->nData};

    if (pColData->flag != HAS_NULL) {
H
Hongze Cheng 已提交
1325
      code = tsdbCmprColData(pColData, cmprAlg, &blockCol, &aBuf[0], aBufN[0], &aBuf[2]);
H
Hongze Cheng 已提交
1326 1327
      if (code) goto _exit;

H
Hongze Cheng 已提交
1328
      blockCol.offset = aBufN[0];
H
Hongze Cheng 已提交
1329
      aBufN[0] = aBufN[0] + blockCol.szBitmap + blockCol.szOffset + blockCol.szValue;
H
Hongze Cheng 已提交
1330 1331
    }

H
Hongze Cheng 已提交
1332
    code = tRealloc(&aBuf[1], hdr.szBlkCol + tPutBlockCol(NULL, &blockCol));
H
Hongze Cheng 已提交
1333
    if (code) goto _exit;
H
Hongze Cheng 已提交
1334
    hdr.szBlkCol += tPutBlockCol(aBuf[1] + hdr.szBlkCol, &blockCol);
H
Hongze Cheng 已提交
1335
  }
H
Hongze Cheng 已提交
1336

H
Hongze Cheng 已提交
1337 1338
  // SBlockCol
  aBufN[1] = hdr.szBlkCol;
H
Hongze Cheng 已提交
1339

H
Hongze Cheng 已提交
1340
  // uid + version + tskey
H
Hongze Cheng 已提交
1341
  aBufN[2] = 0;
H
Hongze Cheng 已提交
1342 1343
  if (pBlockData->uid == 0) {
    code = tsdbCmprData((uint8_t *)pBlockData->aUid, sizeof(int64_t) * pBlockData->nRow, TSDB_DATA_TYPE_BIGINT, cmprAlg,
H
Hongze Cheng 已提交
1344
                        &aBuf[2], aBufN[2], &hdr.szUid, &aBuf[3]);
H
Hongze Cheng 已提交
1345
    if (code) goto _exit;
H
Hongze Cheng 已提交
1346
  }
H
Hongze Cheng 已提交
1347
  aBufN[2] += hdr.szUid;
H
Hongze Cheng 已提交
1348

H
Hongze Cheng 已提交
1349
  code = tsdbCmprData((uint8_t *)pBlockData->aVersion, sizeof(int64_t) * pBlockData->nRow, TSDB_DATA_TYPE_BIGINT,
H
Hongze Cheng 已提交
1350
                      cmprAlg, &aBuf[2], aBufN[2], &hdr.szVer, &aBuf[3]);
H
Hongze Cheng 已提交
1351
  if (code) goto _exit;
H
Hongze Cheng 已提交
1352
  aBufN[2] += hdr.szVer;
H
Hongze Cheng 已提交
1353

H
Hongze Cheng 已提交
1354
  code = tsdbCmprData((uint8_t *)pBlockData->aTSKEY, sizeof(TSKEY) * pBlockData->nRow, TSDB_DATA_TYPE_TIMESTAMP,
H
Hongze Cheng 已提交
1355
                      cmprAlg, &aBuf[2], aBufN[2], &hdr.szKey, &aBuf[3]);
H
Hongze Cheng 已提交
1356
  if (code) goto _exit;
H
Hongze Cheng 已提交
1357
  aBufN[2] += hdr.szKey;
H
Hongze Cheng 已提交
1358

H
Hongze Cheng 已提交
1359
  // hdr
H
Hongze Cheng 已提交
1360 1361
  aBufN[3] = tPutDiskDataHdr(NULL, &hdr);
  code = tRealloc(&aBuf[3], aBufN[3]);
H
Hongze Cheng 已提交
1362
  if (code) goto _exit;
H
Hongze Cheng 已提交
1363
  tPutDiskDataHdr(aBuf[3], &hdr);
H
Hongze Cheng 已提交
1364

H
Hongze Cheng 已提交
1365 1366
  // aggragate
  if (ppOut) {
H
Hongze Cheng 已提交
1367
    *szOut = aBufN[0] + aBufN[1] + aBufN[2] + aBufN[3];
H
Hongze Cheng 已提交
1368 1369
    code = tRealloc(ppOut, *szOut);
    if (code) goto _exit;
H
Hongze Cheng 已提交
1370

H
Hongze Cheng 已提交
1371 1372 1373 1374
    memcpy(*ppOut, aBuf[3], aBufN[3]);
    memcpy(*ppOut + aBufN[3], aBuf[2], aBufN[2]);
    if (aBufN[1]) {
      memcpy(*ppOut + aBufN[3] + aBufN[2], aBuf[1], aBufN[1]);
H
Hongze Cheng 已提交
1375
    }
H
Hongze Cheng 已提交
1376 1377
    if (aBufN[0]) {
      memcpy(*ppOut + aBufN[3] + aBufN[2] + aBufN[1], aBuf[0], aBufN[0]);
H
Hongze Cheng 已提交
1378
    }
H
Hongze Cheng 已提交
1379 1380
  }

H
Hongze Cheng 已提交
1381 1382 1383 1384
_exit:
  return code;
}

H
Hongze Cheng 已提交
1385
int32_t tDecmprBlockData(uint8_t *pIn, int32_t szIn, SBlockData *pBlockData, uint8_t *aBuf[]) {
H
Hongze Cheng 已提交
1386
  int32_t code = 0;
H
Hongze Cheng 已提交
1387

H
Hongze Cheng 已提交
1388
  tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
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

  int32_t      n = 0;
  SDiskDataHdr hdr = {0};

  // SDiskDataHdr
  n += tGetDiskDataHdr(pIn + n, &hdr);
  ASSERT(hdr.delimiter == TSDB_FILE_DLMT);

  pBlockData->suid = hdr.suid;
  pBlockData->uid = hdr.uid;
  pBlockData->nRow = hdr.nRow;

  // uid
  if (hdr.uid == 0) {
    ASSERT(hdr.szUid);
    code = tsdbDecmprData(pIn + n, hdr.szUid, TSDB_DATA_TYPE_BIGINT, hdr.cmprAlg, (uint8_t **)&pBlockData->aUid,
                          sizeof(int64_t) * hdr.nRow, &aBuf[0]);
    if (code) goto _exit;
  } else {
    ASSERT(!hdr.szUid);
  }
  n += hdr.szUid;

  // version
  code = tsdbDecmprData(pIn + n, hdr.szVer, TSDB_DATA_TYPE_BIGINT, hdr.cmprAlg, (uint8_t **)&pBlockData->aVersion,
                        sizeof(int64_t) * hdr.nRow, &aBuf[0]);
  if (code) goto _exit;
  n += hdr.szVer;

  // TSKEY
  code = tsdbDecmprData(pIn + n, hdr.szKey, TSDB_DATA_TYPE_TIMESTAMP, hdr.cmprAlg, (uint8_t **)&pBlockData->aTSKEY,
                        sizeof(TSKEY) * hdr.nRow, &aBuf[0]);
  if (code) goto _exit;
H
Hongze Cheng 已提交
1422
  n += hdr.szKey;
H
Hongze Cheng 已提交
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443

  // loop to decode each column data
  if (hdr.szBlkCol == 0) goto _exit;

  int32_t nt = 0;
  while (nt < hdr.szBlkCol) {
    SBlockCol blockCol = {0};
    nt += tGetBlockCol(pIn + n + nt, &blockCol);
    ASSERT(nt <= hdr.szBlkCol);

    SColData *pColData;
    code = tBlockDataAddColData(pBlockData, taosArrayGetSize(pBlockData->aIdx), &pColData);
    if (code) goto _exit;

    tColDataInit(pColData, blockCol.cid, blockCol.type, blockCol.smaOn);
    if (blockCol.flag == HAS_NULL) {
      for (int32_t iRow = 0; iRow < hdr.nRow; iRow++) {
        code = tColDataAppendValue(pColData, &COL_VAL_NULL(blockCol.cid, blockCol.type));
        if (code) goto _exit;
      }
    } else {
H
Hongze Cheng 已提交
1444 1445
      code = tsdbDecmprColData(pIn + n + hdr.szBlkCol + blockCol.offset, &blockCol, hdr.cmprAlg, hdr.nRow, pColData,
                               &aBuf[0]);
H
Hongze Cheng 已提交
1446 1447 1448 1449 1450
      if (code) goto _exit;
    }
  }

_exit:
H
Hongze Cheng 已提交
1451
  return code;
H
Hongze Cheng 已提交
1452 1453
}

H
Hongze Cheng 已提交
1454
// SDiskDataHdr ==============================
H
Hongze Cheng 已提交
1455 1456
int32_t tPutDiskDataHdr(uint8_t *p, const SDiskDataHdr *pHdr) {
  int32_t n = 0;
H
Hongze Cheng 已提交
1457 1458

  n += tPutU32(p ? p + n : p, pHdr->delimiter);
H
Hongze Cheng 已提交
1459
  n += tPutU32v(p ? p + n : p, pHdr->fmtVer);
H
Hongze Cheng 已提交
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
  n += tPutI64(p ? p + n : p, pHdr->suid);
  n += tPutI64(p ? p + n : p, pHdr->uid);
  n += tPutI32v(p ? p + n : p, pHdr->szUid);
  n += tPutI32v(p ? p + n : p, pHdr->szVer);
  n += tPutI32v(p ? p + n : p, pHdr->szKey);
  n += tPutI32v(p ? p + n : p, pHdr->szBlkCol);
  n += tPutI32v(p ? p + n : p, pHdr->nRow);
  n += tPutI8(p ? p + n : p, pHdr->cmprAlg);

  return n;
}

int32_t tGetDiskDataHdr(uint8_t *p, void *ph) {
  int32_t       n = 0;
  SDiskDataHdr *pHdr = (SDiskDataHdr *)ph;

  n += tGetU32(p + n, &pHdr->delimiter);
H
Hongze Cheng 已提交
1477
  n += tGetU32v(p + n, &pHdr->fmtVer);
H
Hongze Cheng 已提交
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
  n += tGetI64(p + n, &pHdr->suid);
  n += tGetI64(p + n, &pHdr->uid);
  n += tGetI32v(p + n, &pHdr->szUid);
  n += tGetI32v(p + n, &pHdr->szVer);
  n += tGetI32v(p + n, &pHdr->szKey);
  n += tGetI32v(p + n, &pHdr->szBlkCol);
  n += tGetI32v(p + n, &pHdr->nRow);
  n += tGetI8(p + n, &pHdr->cmprAlg);

  return n;
}

H
Hongze Cheng 已提交
1490
// ALGORITHM ==============================
H
Hongze Cheng 已提交
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
int32_t tPutColumnDataAgg(uint8_t *p, SColumnDataAgg *pColAgg) {
  int32_t n = 0;

  n += tPutI16v(p ? p + n : p, pColAgg->colId);
  n += tPutI16v(p ? p + n : p, pColAgg->numOfNull);
  n += tPutI64(p ? p + n : p, pColAgg->sum);
  n += tPutI64(p ? p + n : p, pColAgg->max);
  n += tPutI64(p ? p + n : p, pColAgg->min);

  return n;
}

int32_t tGetColumnDataAgg(uint8_t *p, SColumnDataAgg *pColAgg) {
  int32_t n = 0;

  n += tGetI16v(p + n, &pColAgg->colId);
  n += tGetI16v(p + n, &pColAgg->numOfNull);
  n += tGetI64(p + n, &pColAgg->sum);
  n += tGetI64(p + n, &pColAgg->max);
  n += tGetI64(p + n, &pColAgg->min);

  return n;
}

H
Hongze Cheng 已提交
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
#define SMA_UPDATE(SUM_V, MIN_V, MAX_V, VAL, MINSET, MAXSET) \
  do {                                                       \
    (SUM_V) += (VAL);                                        \
    if (!(MINSET)) {                                         \
      (MIN_V) = (VAL);                                       \
      (MINSET) = 1;                                          \
    } else if ((MIN_V) > (VAL)) {                            \
      (MIN_V) = (VAL);                                       \
    }                                                        \
    if (!(MAXSET)) {                                         \
      (MAX_V) = (VAL);                                       \
      (MAXSET) = 1;                                          \
    } else if ((MAX_V) < (VAL)) {                            \
      (MAX_V) = (VAL);                                       \
    }                                                        \
  } while (0)

static FORCE_INLINE void tSmaUpdateBool(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, uint8_t *maxSet) {
  int8_t val = *(int8_t *)&pColVal->value.val ? 1 : 0;
  SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet);
}
static FORCE_INLINE void tSmaUpdateTinyint(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet,
                                           uint8_t *maxSet) {
  int8_t val = *(int8_t *)&pColVal->value.val;
  SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet);
}
static FORCE_INLINE void tSmaUpdateSmallint(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet,
                                            uint8_t *maxSet) {
  int16_t val = *(int16_t *)&pColVal->value.val;
  SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet);
}
static FORCE_INLINE void tSmaUpdateInt(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, uint8_t *maxSet) {
  int32_t val = *(int32_t *)&pColVal->value.val;
  SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet);
}
static FORCE_INLINE void tSmaUpdateBigint(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, uint8_t *maxSet) {
  int64_t val = *(int64_t *)&pColVal->value.val;
  SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet);
}
static FORCE_INLINE void tSmaUpdateFloat(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, uint8_t *maxSet) {
  float val = *(float *)&pColVal->value.val;
  SMA_UPDATE(*(double *)&pColAgg->sum, *(double *)&pColAgg->min, *(double *)&pColAgg->max, val, *minSet, *maxSet);
}
static FORCE_INLINE void tSmaUpdateDouble(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, uint8_t *maxSet) {
  double val = *(double *)&pColVal->value.val;
  SMA_UPDATE(*(double *)&pColAgg->sum, *(double *)&pColAgg->min, *(double *)&pColAgg->max, val, *minSet, *maxSet);
}
static FORCE_INLINE void tSmaUpdateUTinyint(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet,
                                            uint8_t *maxSet) {
  uint8_t val = *(uint8_t *)&pColVal->value.val;
  SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet);
}
static FORCE_INLINE void tSmaUpdateUSmallint(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet,
                                             uint8_t *maxSet) {
  uint16_t val = *(uint16_t *)&pColVal->value.val;
  SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet);
}
static FORCE_INLINE void tSmaUpdateUInt(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, uint8_t *maxSet) {
  uint32_t val = *(uint32_t *)&pColVal->value.val;
  SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet);
}
static FORCE_INLINE void tSmaUpdateUBigint(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet,
                                           uint8_t *maxSet) {
  uint64_t val = *(uint64_t *)&pColVal->value.val;
  SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet);
}
void (*tSmaUpdateImpl[])(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, uint8_t *maxSet) = {
    NULL,
    tSmaUpdateBool,       // TSDB_DATA_TYPE_BOOL
    tSmaUpdateTinyint,    // TSDB_DATA_TYPE_TINYINT
    tSmaUpdateSmallint,   // TSDB_DATA_TYPE_SMALLINT
    tSmaUpdateInt,        // TSDB_DATA_TYPE_INT
    tSmaUpdateBigint,     // TSDB_DATA_TYPE_BIGINT
    tSmaUpdateFloat,      // TSDB_DATA_TYPE_FLOAT
    tSmaUpdateDouble,     // TSDB_DATA_TYPE_DOUBLE
    NULL,                 // TSDB_DATA_TYPE_VARCHAR
    tSmaUpdateBigint,     // TSDB_DATA_TYPE_TIMESTAMP
    NULL,                 // TSDB_DATA_TYPE_NCHAR
    tSmaUpdateUTinyint,   // TSDB_DATA_TYPE_UTINYINT
    tSmaUpdateUSmallint,  // TSDB_DATA_TYPE_USMALLINT
    tSmaUpdateUInt,       // TSDB_DATA_TYPE_UINT
    tSmaUpdateUBigint,    // TSDB_DATA_TYPE_UBIGINT
    NULL,                 // TSDB_DATA_TYPE_JSON
    NULL,                 // TSDB_DATA_TYPE_VARBINARY
    NULL,                 // TSDB_DATA_TYPE_DECIMAL
    NULL,                 // TSDB_DATA_TYPE_BLOB
    NULL,                 // TSDB_DATA_TYPE_MEDIUMBLOB
};
H
Hongze Cheng 已提交
1603 1604
void tsdbCalcColDataSMA(SColData *pColData, SColumnDataAgg *pColAgg) {
  *pColAgg = (SColumnDataAgg){.colId = pColData->cid};
H
Hongze Cheng 已提交
1605 1606 1607 1608
  uint8_t minSet = 0;
  uint8_t maxSet = 0;

  SColVal cv;
H
Hongze Cheng 已提交
1609
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
H
Hongze Cheng 已提交
1610
    tColDataGetValue(pColData, iVal, &cv);
H
Hongze Cheng 已提交
1611

H
Hongze Cheng 已提交
1612
    if (COL_VAL_IS_VALUE(&cv)) {
H
Hongze Cheng 已提交
1613
      tSmaUpdateImpl[pColData->type](pColAgg, &cv, &minSet, &maxSet);
H
Hongze Cheng 已提交
1614 1615
    } else {
      pColAgg->numOfNull++;
H
Hongze Cheng 已提交
1616 1617 1618
    }
  }
}
H
Hongze Cheng 已提交
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 1651 1652 1653 1654 1655

int32_t tsdbCmprData(uint8_t *pIn, int32_t szIn, int8_t type, int8_t cmprAlg, uint8_t **ppOut, int32_t nOut,
                     int32_t *szOut, uint8_t **ppBuf) {
  int32_t code = 0;

  ASSERT(szIn > 0 && ppOut);

  if (cmprAlg == NO_COMPRESSION) {
    code = tRealloc(ppOut, nOut + szIn);
    if (code) goto _exit;

    memcpy(*ppOut + nOut, pIn, szIn);
    *szOut = szIn;
  } else {
    int32_t size = szIn + COMP_OVERFLOW_BYTES;

    code = tRealloc(ppOut, nOut + size);
    if (code) goto _exit;

    if (cmprAlg == TWO_STAGE_COMP) {
      ASSERT(ppBuf);
      code = tRealloc(ppBuf, size);
      if (code) goto _exit;
    }

    *szOut =
        tDataTypes[type].compFunc(pIn, szIn, szIn / tDataTypes[type].bytes, *ppOut + nOut, size, cmprAlg, *ppBuf, size);
    if (*szOut <= 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _exit;
    }
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
int32_t tsdbDecmprData(uint8_t *pIn, int32_t szIn, int8_t type, int8_t cmprAlg, uint8_t **ppOut, int32_t szOut,
                       uint8_t **ppBuf) {
  int32_t code = 0;

  code = tRealloc(ppOut, szOut);
  if (code) goto _exit;

  if (cmprAlg == NO_COMPRESSION) {
    ASSERT(szIn == szOut);
    memcpy(*ppOut, pIn, szOut);
  } else {
    if (cmprAlg == TWO_STAGE_COMP) {
      code = tRealloc(ppBuf, szOut + COMP_OVERFLOW_BYTES);
      if (code) goto _exit;
    }

    int32_t size = tDataTypes[type].decompFunc(pIn, szIn, szOut / tDataTypes[type].bytes, *ppOut, szOut, cmprAlg,
                                               *ppBuf, szOut + COMP_OVERFLOW_BYTES);
    if (size <= 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _exit;
    }

    ASSERT(size == szOut);
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
1686
int32_t tsdbCmprColData(SColData *pColData, int8_t cmprAlg, SBlockCol *pBlockCol, uint8_t **ppOut, int32_t nOut,
H
Hongze Cheng 已提交
1687
                        uint8_t **ppBuf) {
H
Hongze Cheng 已提交
1688 1689
  int32_t code = 0;

H
Hongze Cheng 已提交
1690 1691
  ASSERT(pColData->flag && (pColData->flag != HAS_NONE) && (pColData->flag != HAS_NULL));

H
Hongze Cheng 已提交
1692 1693 1694 1695 1696
  pBlockCol->szBitmap = 0;
  pBlockCol->szOffset = 0;
  pBlockCol->szValue = 0;

  int32_t size = 0;
H
Hongze Cheng 已提交
1697 1698
  // bitmap
  if (pColData->flag != HAS_VALUE) {
H
Hongze Cheng 已提交
1699 1700 1701 1702
    int32_t szBitMap;
    if (pColData->flag == (HAS_VALUE | HAS_NULL | HAS_NONE)) {
      szBitMap = BIT2_SIZE(pColData->nVal);
    } else {
H
Hongze Cheng 已提交
1703 1704 1705
      szBitMap = BIT1_SIZE(pColData->nVal);
    }

H
Hongze Cheng 已提交
1706 1707
    code = tsdbCmprData(pColData->pBitMap, szBitMap, TSDB_DATA_TYPE_TINYINT, cmprAlg, ppOut, nOut + size,
                        &pBlockCol->szBitmap, ppBuf);
H
Hongze Cheng 已提交
1708 1709
    if (code) goto _exit;
  }
H
Hongze Cheng 已提交
1710
  size += pBlockCol->szBitmap;
H
Hongze Cheng 已提交
1711 1712 1713 1714

  // offset
  if (IS_VAR_DATA_TYPE(pColData->type)) {
    code = tsdbCmprData((uint8_t *)pColData->aOffset, sizeof(int32_t) * pColData->nVal, TSDB_DATA_TYPE_INT, cmprAlg,
H
Hongze Cheng 已提交
1715
                        ppOut, nOut + size, &pBlockCol->szOffset, ppBuf);
H
Hongze Cheng 已提交
1716 1717
    if (code) goto _exit;
  }
H
Hongze Cheng 已提交
1718
  size += pBlockCol->szOffset;
H
Hongze Cheng 已提交
1719 1720

  // value
H
Hongze Cheng 已提交
1721
  if ((pColData->flag != (HAS_NULL | HAS_NONE)) && pColData->nData) {
H
Hongze Cheng 已提交
1722
    code = tsdbCmprData((uint8_t *)pColData->pData, pColData->nData, pColData->type, cmprAlg, ppOut, nOut + size,
H
Hongze Cheng 已提交
1723 1724 1725
                        &pBlockCol->szValue, ppBuf);
    if (code) goto _exit;
  }
H
Hongze Cheng 已提交
1726
  size += pBlockCol->szValue;
H
Hongze Cheng 已提交
1727 1728 1729 1730

_exit:
  return code;
}
H
Hongze Cheng 已提交
1731 1732 1733 1734 1735

int32_t tsdbDecmprColData(uint8_t *pIn, SBlockCol *pBlockCol, int8_t cmprAlg, int32_t nVal, SColData *pColData,
                          uint8_t **ppBuf) {
  int32_t code = 0;

H
Hongze Cheng 已提交
1736 1737
  ASSERT(pColData->cid == pBlockCol->cid);
  ASSERT(pColData->type == pBlockCol->type);
H
Hongze Cheng 已提交
1738 1739 1740 1741 1742 1743 1744 1745
  pColData->smaOn = pBlockCol->smaOn;
  pColData->flag = pBlockCol->flag;
  pColData->nVal = nVal;
  pColData->nData = pBlockCol->szOrigin;

  uint8_t *p = pIn;
  // bitmap
  if (pBlockCol->szBitmap) {
H
Hongze Cheng 已提交
1746 1747 1748
    int32_t szBitMap;
    if (pColData->flag == (HAS_VALUE | HAS_NULL | HAS_NONE)) {
      szBitMap = BIT2_SIZE(pColData->nVal);
H
Hongze Cheng 已提交
1749
    } else {
H
Hongze Cheng 已提交
1750
      szBitMap = BIT1_SIZE(pColData->nVal);
H
Hongze Cheng 已提交
1751
    }
H
Hongze Cheng 已提交
1752 1753 1754

    code = tsdbDecmprData(p, pBlockCol->szBitmap, TSDB_DATA_TYPE_TINYINT, cmprAlg, &pColData->pBitMap, szBitMap, ppBuf);
    if (code) goto _exit;
H
Hongze Cheng 已提交
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
  }
  p += pBlockCol->szBitmap;

  // offset
  if (pBlockCol->szOffset) {
    code = tsdbDecmprData(p, pBlockCol->szOffset, TSDB_DATA_TYPE_INT, cmprAlg, (uint8_t **)&pColData->aOffset,
                          sizeof(int32_t) * pColData->nVal, ppBuf);
    if (code) goto _exit;
  }
  p += pBlockCol->szOffset;

  // value
  if (pBlockCol->szValue) {
    code = tsdbDecmprData(p, pBlockCol->szValue, pColData->type, cmprAlg, &pColData->pData, pColData->nData, ppBuf);
    if (code) goto _exit;
  }
  p += pBlockCol->szValue;

H
Hongze Cheng 已提交
1773 1774 1775
_exit:
  return code;
}