tsdbUtil.c 52.8 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
  tFree(pMapData->aOffset);
H
Hongze Cheng 已提交
27
  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
}

H
Hongze Cheng 已提交
104 105 106 107 108 109
int32_t tMapDataToArray(SMapData *pMapData, int32_t itemSize, int32_t (*tGetItemFn)(uint8_t *, void *),
                        SArray **ppArray) {
  int32_t code = 0;

  SArray *pArray = taosArrayInit(pMapData->nItem, itemSize);
  if (pArray == NULL) {
S
Shengliang Guan 已提交
110
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
111 112 113 114 115 116 117 118
    goto _exit;
  }

  for (int32_t i = 0; i < pMapData->nItem; i++) {
    tMapDataGetItemByIdx(pMapData, i, taosArrayReserve(pArray, 1), tGetItemFn);
  }

_exit:
H
Hongze Cheng 已提交
119
  *ppArray = pArray;
H
Hongze Cheng 已提交
120 121 122
  return code;
}

H
Hongze Cheng 已提交
123 124 125 126
int32_t tPutMapData(uint8_t *p, SMapData *pMapData) {
  int32_t n = 0;

  n += tPutI32v(p ? p + n : p, pMapData->nItem);
H
Hongze Cheng 已提交
127
  if (pMapData->nItem) {
H
Hongze Cheng 已提交
128
    int32_t lOffset = 0;
H
Hongze Cheng 已提交
129
    for (int32_t iItem = 0; iItem < pMapData->nItem; iItem++) {
H
Hongze Cheng 已提交
130 131
      n += tPutI32v(p ? p + n : p, pMapData->aOffset[iItem] - lOffset);
      lOffset = pMapData->aOffset[iItem];
H
Hongze Cheng 已提交
132
    }
H
Hongze Cheng 已提交
133 134 135 136 137 138

    n += tPutI32v(p ? p + n : p, pMapData->nData);
    if (p) {
      memcpy(p + n, pMapData->pData, pMapData->nData);
    }
    n += pMapData->nData;
H
Hongze Cheng 已提交
139 140 141 142 143 144 145
  }

  return n;
}

int32_t tGetMapData(uint8_t *p, SMapData *pMapData) {
  int32_t n = 0;
H
Hongze Cheng 已提交
146 147 148
  int32_t offset;

  tMapDataReset(pMapData);
H
Hongze Cheng 已提交
149 150

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

H
Hongze Cheng 已提交
154
    int32_t lOffset = 0;
H
Hongze Cheng 已提交
155 156
    for (int32_t iItem = 0; iItem < pMapData->nItem; iItem++) {
      n += tGetI32v(p + n, &pMapData->aOffset[iItem]);
H
Hongze Cheng 已提交
157 158
      pMapData->aOffset[iItem] += lOffset;
      lOffset = pMapData->aOffset[iItem];
H
Hongze Cheng 已提交
159
    }
H
Hongze Cheng 已提交
160 161

    n += tGetI32v(p + n, &pMapData->nData);
H
Hongze Cheng 已提交
162
    if (tRealloc(&pMapData->pData, pMapData->nData)) return -1;
H
Hongze Cheng 已提交
163 164
    memcpy(pMapData->pData, p + n, pMapData->nData);
    n += pMapData->nData;
H
Hongze Cheng 已提交
165 166 167 168 169
  }

  return n;
}

H
Hongze Cheng 已提交
170
// TABLEID =======================================================================
H
Hongze Cheng 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
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 已提交
190
// SBlockIdx ======================================================
H
Hongze Cheng 已提交
191 192 193 194 195 196 197 198
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 已提交
199 200 201 202

  return n;
}

H
Hongze Cheng 已提交
203 204 205
int32_t tGetBlockIdx(uint8_t *p, void *ph) {
  int32_t    n = 0;
  SBlockIdx *pBlockIdx = (SBlockIdx *)ph;
H
Hongze Cheng 已提交
206

H
Hongze Cheng 已提交
207 208 209 210
  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 已提交
211 212 213 214

  return n;
}

215
int32_t tCmprBlockIdx(void const *lhs, void const *rhs) {
M
Minglei Jin 已提交
216 217
  SBlockIdx *lBlockIdx = (SBlockIdx *)lhs;
  SBlockIdx *rBlockIdx = (SBlockIdx *)rhs;
218

M
Minglei Jin 已提交
219
  if (lBlockIdx->suid < rBlockIdx->suid) {
220
    return -1;
M
Minglei Jin 已提交
221
  } else if (lBlockIdx->suid > rBlockIdx->suid) {
222 223 224
    return 1;
  }

M
Minglei Jin 已提交
225
  if (lBlockIdx->uid < rBlockIdx->uid) {
226
    return -1;
M
Minglei Jin 已提交
227
  } else if (lBlockIdx->uid > rBlockIdx->uid) {
228 229 230 231 232 233
    return 1;
  }

  return 0;
}

234 235
int32_t tCmprBlockL(void const *lhs, void const *rhs) {
  SBlockIdx *lBlockIdx = (SBlockIdx *)lhs;
H
Hongze Cheng 已提交
236
  SSttBlk   *rBlockL = (SSttBlk *)rhs;
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

  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 已提交
253
// SDataBlk ======================================================
H
Hongze Cheng 已提交
254
void tDataBlkReset(SDataBlk *pDataBlk) {
H
Hongze Cheng 已提交
255
  *pDataBlk = (SDataBlk){.minKey = TSDBKEY_MAX, .maxKey = TSDBKEY_MIN, .minVer = VERSION_MAX, .maxVer = VERSION_MIN};
H
Hongze Cheng 已提交
256 257
}

H
Hongze Cheng 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
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 已提交
275
  }
H
Hongze Cheng 已提交
276 277 278
  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 已提交
279 280
  }

H
Hongze Cheng 已提交
281 282 283
  return n;
}

H
Hongze Cheng 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
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 已提交
301
  }
H
Hongze Cheng 已提交
302 303 304
  if (pDataBlk->nSubBlock == 1 && !pDataBlk->hasDup) {
    n += tGetI64v(p + n, &pDataBlk->smaInfo.offset);
    n += tGetI32v(p + n, &pDataBlk->smaInfo.size);
H
Hongze Cheng 已提交
305
  } else {
H
Hongze Cheng 已提交
306 307
    pDataBlk->smaInfo.offset = 0;
    pDataBlk->smaInfo.size = 0;
H
Hongze Cheng 已提交
308 309
  }

H
Hongze Cheng 已提交
310 311 312
  return n;
}

H
Hongze Cheng 已提交
313
int32_t tDataBlkCmprFn(const void *p1, const void *p2) {
H
Hongze Cheng 已提交
314 315
  SDataBlk *pBlock1 = (SDataBlk *)p1;
  SDataBlk *pBlock2 = (SDataBlk *)p2;
H
Hongze Cheng 已提交
316

H
Hongze Cheng 已提交
317
  if (tsdbKeyCmprFn(&pBlock1->maxKey, &pBlock2->minKey) < 0) {
H
Hongze Cheng 已提交
318
    return -1;
H
Hongze Cheng 已提交
319
  } else if (tsdbKeyCmprFn(&pBlock1->minKey, &pBlock2->maxKey) > 0) {
H
Hongze Cheng 已提交
320
    return 1;
H
Hongze Cheng 已提交
321 322 323 324 325
  }

  return 0;
}

H
Hongze Cheng 已提交
326
bool tDataBlkHasSma(SDataBlk *pDataBlk) {
H
Hongze Cheng 已提交
327 328
  if (pDataBlk->nSubBlock > 1) return false;
  if (pDataBlk->hasDup) return false;
H
Hongze Cheng 已提交
329

H
Hongze Cheng 已提交
330
  return pDataBlk->smaInfo.size > 0;
H
Hongze Cheng 已提交
331
}
H
Hongze Cheng 已提交
332

H
Hongze Cheng 已提交
333 334
// SSttBlk ======================================================
int32_t tPutSttBlk(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 += 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 已提交
349 350 351 352

  return n;
}

H
Hongze Cheng 已提交
353
int32_t tGetSttBlk(uint8_t *p, void *ph) {
H
Hongze Cheng 已提交
354
  int32_t  n = 0;
H
Hongze Cheng 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367
  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 已提交
368 369 370

  return n;
}
H
Hongze Cheng 已提交
371

H
Hongze Cheng 已提交
372 373 374 375 376 377 378 379 380
// 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 已提交
381
  n += tPutI8(p ? p + n : p, pBlockCol->smaOn);
H
Hongze Cheng 已提交
382
  n += tPutI8(p ? p + n : p, pBlockCol->flag);
H
Hongze Cheng 已提交
383
  n += tPutI32v(p ? p + n : p, pBlockCol->szOrigin);
H
Hongze Cheng 已提交
384 385

  if (pBlockCol->flag != HAS_NULL) {
H
Hongze Cheng 已提交
386 387 388 389 390 391 392 393 394 395 396 397
    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 已提交
398
    n += tPutI32v(p ? p + n : p, pBlockCol->offset);
H
Hongze Cheng 已提交
399 400
  }

H
Hongze Cheng 已提交
401
_exit:
H
Hongze Cheng 已提交
402 403 404 405 406 407 408 409 410
  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 已提交
411
  n += tGetI8(p + n, &pBlockCol->smaOn);
H
Hongze Cheng 已提交
412
  n += tGetI8(p + n, &pBlockCol->flag);
H
Hongze Cheng 已提交
413
  n += tGetI32v(p + n, &pBlockCol->szOrigin);
H
Hongze Cheng 已提交
414 415 416

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

H
Hongze Cheng 已提交
417 418 419 420 421
  pBlockCol->szBitmap = 0;
  pBlockCol->szOffset = 0;
  pBlockCol->szValue = 0;
  pBlockCol->offset = 0;

H
Hongze Cheng 已提交
422
  if (pBlockCol->flag != HAS_NULL) {
H
Hongze Cheng 已提交
423 424 425 426 427 428 429 430 431 432 433 434
    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 已提交
435
    n += tGetI32v(p + n, &pBlockCol->offset);
H
Hongze Cheng 已提交
436 437 438 439 440
  }

  return n;
}

H
Hongze Cheng 已提交
441 442 443 444 445 446 447 448 449 450
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 已提交
451
// SDelIdx ======================================================
452
int32_t tCmprDelIdx(void const *lhs, void const *rhs) {
M
Minglei Jin 已提交
453 454
  SDelIdx *lDelIdx = (SDelIdx *)lhs;
  SDelIdx *rDelIdx = (SDelIdx *)rhs;
455

M
Minglei Jin 已提交
456
  if (lDelIdx->suid < rDelIdx->suid) {
457
    return -1;
M
Minglei Jin 已提交
458
  } else if (lDelIdx->suid > rDelIdx->suid) {
459 460 461
    return 1;
  }

M
Minglei Jin 已提交
462
  if (lDelIdx->uid < rDelIdx->uid) {
463
    return -1;
M
Minglei Jin 已提交
464
  } else if (lDelIdx->uid > rDelIdx->uid) {
465 466 467 468 469 470
    return 1;
  }

  return 0;
}

H
Hongze Cheng 已提交
471 472 473 474 475 476 477 478
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 已提交
479 480 481 482

  return n;
}

H
Hongze Cheng 已提交
483 484 485
int32_t tGetDelIdx(uint8_t *p, void *ph) {
  SDelIdx *pDelIdx = (SDelIdx *)ph;
  int32_t  n = 0;
H
Hongze Cheng 已提交
486

H
Hongze Cheng 已提交
487 488 489 490
  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 已提交
491 492 493 494

  return n;
}

H
Hongze Cheng 已提交
495
// SDelData ======================================================
H
Hongze Cheng 已提交
496 497 498 499 500 501 502
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 已提交
503 504 505 506

  return n;
}

H
Hongze Cheng 已提交
507 508 509
int32_t tGetDelData(uint8_t *p, void *ph) {
  SDelData *pDelData = (SDelData *)ph;
  int32_t   n = 0;
H
Hongze Cheng 已提交
510

H
Hongze Cheng 已提交
511 512 513
  n += tGetI64v(p + n, &pDelData->version);
  n += tGetI64(p + n, &pDelData->sKey);
  n += tGetI64(p + n, &pDelData->eKey);
H
Hongze Cheng 已提交
514 515

  return n;
H
Hongze Cheng 已提交
516 517
}

H
Hongze Cheng 已提交
518 519 520 521 522 523
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 已提交
524 525
}

H
Hongze Cheng 已提交
526
void tsdbFidKeyRange(int32_t fid, int32_t minutes, int8_t precision, TSKEY *minKey, TSKEY *maxKey) {
527 528
  *minKey = tsTickPerMin[precision] * fid * minutes;
  *maxKey = *minKey + tsTickPerMin[precision] * minutes - 1;
H
Hongze Cheng 已提交
529 530
}

H
Hongze Cheng 已提交
531 532 533 534
int32_t tsdbFidLevel(int32_t fid, STsdbKeepCfg *pKeepCfg, int64_t now) {
  int32_t aFid[3];
  TSKEY   key;

H
Hongze Cheng 已提交
535 536 537 538 539 540 541 542 543 544
  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 已提交
545
  key = now - pKeepCfg->keep0 * tsTickPerMin[pKeepCfg->precision];
H
Hongze Cheng 已提交
546
  aFid[0] = tsdbKeyFid(key, pKeepCfg->days, pKeepCfg->precision);
H
Hongze Cheng 已提交
547
  key = now - pKeepCfg->keep1 * tsTickPerMin[pKeepCfg->precision];
H
Hongze Cheng 已提交
548
  aFid[1] = tsdbKeyFid(key, pKeepCfg->days, pKeepCfg->precision);
H
Hongze Cheng 已提交
549
  key = now - pKeepCfg->keep2 * tsTickPerMin[pKeepCfg->precision];
H
Hongze Cheng 已提交
550
  aFid[2] = tsdbKeyFid(key, pKeepCfg->days, pKeepCfg->precision);
H
Hongze Cheng 已提交
551 552 553 554 555 556 557 558 559 560 561

  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 已提交
562

H
Hongze Cheng 已提交
563 564
// TSDBROW ======================================================
void tsdbRowGetColVal(TSDBROW *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal) {
H
Hongze Cheng 已提交
565 566 567
  STColumn *pTColumn = &pTSchema->columns[iCol];
  SValue    value;

H
Hongze Cheng 已提交
568 569
  ASSERT(iCol > 0);

H
Hongze Cheng 已提交
570
  if (pRow->type == TSDBROW_ROW_FMT) {
H
Hongze Cheng 已提交
571
    tRowGet(pRow->pTSRow, pTSchema, iCol, pColVal);
H
Hongze Cheng 已提交
572
  } else if (pRow->type == TSDBROW_COL_FMT) {
H
Hongze Cheng 已提交
573
    SColData *pColData;
H
Hongze Cheng 已提交
574

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

H
Hongze Cheng 已提交
577
    if (pColData) {
H
Hongze Cheng 已提交
578
      tColDataGetValue(pColData, pRow->iRow, pColVal);
H
Hongze Cheng 已提交
579
    } else {
H
Hongze Cheng 已提交
580
      *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
H
Hongze Cheng 已提交
581 582 583 584
    }
  } else {
    ASSERT(0);
  }
H
Hongze Cheng 已提交
585 586
}

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

H
Hongze Cheng 已提交
591
// STSDBRowIter ======================================================
H
Hongze Cheng 已提交
592 593 594
int32_t tsdbRowIterOpen(STSDBRowIter *pIter, TSDBROW *pRow, STSchema *pTSchema) {
  int32_t code = 0;

H
Hongze Cheng 已提交
595
  pIter->pRow = pRow;
H
Hongze Cheng 已提交
596
  if (pRow->type == TSDBROW_ROW_FMT) {
H
Hongze Cheng 已提交
597 598
    code = tRowIterOpen(pRow->pTSRow, pTSchema, &pIter->pIter);
    if (code) goto _exit;
H
Hongze Cheng 已提交
599
  } else if (pRow->type == TSDBROW_COL_FMT) {
H
Hongze Cheng 已提交
600
    pIter->iColData = 0;
H
Hongze Cheng 已提交
601 602 603
  } else {
    ASSERT(0);
  }
H
Hongze Cheng 已提交
604 605 606

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

H
Hongze Cheng 已提交
609
void tsdbRowClose(STSDBRowIter *pIter) {
H
Hongze Cheng 已提交
610
  if (pIter->pRow->type == TSDBROW_ROW_FMT) {
H
Hongze Cheng 已提交
611 612 613
    tRowIterClose(&pIter->pIter);
  }
}
H
Hongze Cheng 已提交
614

H
Hongze Cheng 已提交
615 616 617
SColVal *tsdbRowIterNext(STSDBRowIter *pIter) {
  if (pIter->pRow->type == TSDBROW_ROW_FMT) {
    return tRowIterNext(pIter->pIter);
H
Hongze Cheng 已提交
618
  } else if (pIter->pRow->type == TSDBROW_COL_FMT) {
H
Hongze Cheng 已提交
619 620 621 622 623 624
    if (pIter->iColData == 0) {
      pIter->cv = COL_VAL_VALUE(PRIMARYKEY_TIMESTAMP_COL_ID, TSDB_DATA_TYPE_TIMESTAMP,
                                (SValue){.val = pIter->pRow->pBlockData->aTSKEY[pIter->pRow->iRow]});
      ++pIter->iColData;
      return &pIter->cv;
    }
H
Hongze Cheng 已提交
625

H
Hongze Cheng 已提交
626 627 628 629 630 631
    if (pIter->iColData < pIter->pRow->pBlockData->nColData) {
      tColDataGetValue(&pIter->pRow->pBlockData->aColData[pIter->iColData], pIter->pRow->iRow, &pIter->cv);
      ++pIter->iColData;
      return &pIter->cv;
    } else {
      return NULL;
H
Hongze Cheng 已提交
632
    }
H
Hongze Cheng 已提交
633 634
  } else {
    ASSERT(0);
H
Hongze Cheng 已提交
635
    return NULL;  // suppress error report by compiler
H
Hongze Cheng 已提交
636 637 638
  }
}

H
Hongze Cheng 已提交
639
// SRowMerger ======================================================
640

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

M
Minglei Jin 已提交
648 649 650 651
  if (NULL == pResTSchema) {
    pResTSchema = pTSchema;
  }

652 653 654 655 656 657 658 659 660 661
  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 已提交
662
  pTColumn = &pTSchema->columns[jCol++];
663 664 665

  ASSERT(pTColumn->type == TSDB_DATA_TYPE_TIMESTAMP);

H
Hongze Cheng 已提交
666
  *pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.val = key.ts});
667 668 669 670 671 672
  if (taosArrayPush(pMerger->pArray, pColVal) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  // other
M
Minglei Jin 已提交
673
  for (iCol = 1; jCol < pTSchema->numOfCols && iCol < pResTSchema->numOfCols; ++iCol) {
674
    pTColumn = &pResTSchema->columns[iCol];
M
Minglei Jin 已提交
675 676 677 678 679
    if (pTSchema->columns[jCol].colId < pTColumn->colId) {
      ++jCol;
      --iCol;
      continue;
    } else if (pTSchema->columns[jCol].colId > pTColumn->colId) {
680 681 682 683
      taosArrayPush(pMerger->pArray, &COL_VAL_NONE(pTColumn->colId, pTColumn->type));
      continue;
    }

M
Minglei Jin 已提交
684
    tsdbRowGetColVal(pRow, pTSchema, jCol++, pColVal);
D
dapan1121 已提交
685
    if ((!COL_VAL_IS_NONE(pColVal)) && (!COL_VAL_IS_NULL(pColVal)) && IS_VAR_DATA_TYPE(pColVal->type)) {
686
      uint8_t *pVal = pColVal->value.pData;
X
Xiaoyu Wang 已提交
687

688 689 690
      pColVal->value.pData = NULL;
      code = tRealloc(&pColVal->value.pData, pColVal->value.nData);
      if (code) goto _exit;
D
dapan1121 已提交
691

D
dapan1121 已提交
692 693 694
      if (pColVal->value.nData) {
        memcpy(pColVal->value.pData, pVal, pColVal->value.nData);
      }
695 696
    }

697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
    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;
}

H
Hongze Cheng 已提交
712
int32_t tsdbRowMergerAdd(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema) {
713 714
  int32_t   code = 0;
  TSDBKEY   key = TSDBROW_KEY(pRow);
715
  SColVal * pColVal = &(SColVal){0};
716
  STColumn *pTColumn;
M
Minglei Jin 已提交
717
  int32_t   iCol, jCol = 1;
718

719 720 721
  if (NULL == pTSchema) {
    pTSchema = pMerger->pTSchema;
  }
722

723 724
  if (taosArrayGetSize(pMerger->pArray) == 0) {
    // ts
725
    jCol = 0;
726 727 728 729 730 731 732 733 734
    pTColumn = &pTSchema->columns[jCol++];

    ASSERT(pTColumn->type == TSDB_DATA_TYPE_TIMESTAMP);

    *pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.val = key.ts});
    if (taosArrayPush(pMerger->pArray, pColVal) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      return code;
      //      goto _exit;
735 736
    }

737 738 739 740 741 742 743 744 745 746 747
    // other
    for (iCol = 1; jCol < pTSchema->numOfCols && iCol < pMerger->pTSchema->numOfCols; ++iCol) {
      pTColumn = &pMerger->pTSchema->columns[iCol];
      if (pTSchema->columns[jCol].colId < pTColumn->colId) {
        ++jCol;
        --iCol;
        continue;
      } else if (pTSchema->columns[jCol].colId > pTColumn->colId) {
        taosArrayPush(pMerger->pArray, &COL_VAL_NONE(pTColumn->colId, pTColumn->type));
        continue;
      }
748

749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
      tsdbRowGetColVal(pRow, pTSchema, jCol++, pColVal);
      if ((!COL_VAL_IS_NONE(pColVal)) && (!COL_VAL_IS_NULL(pColVal)) && IS_VAR_DATA_TYPE(pColVal->type)) {
        uint8_t *pVal = pColVal->value.pData;

        pColVal->value.pData = NULL;
        code = tRealloc(&pColVal->value.pData, pColVal->value.nData);
        if (code) {
          return TSDB_CODE_OUT_OF_MEMORY;
        }

        if (pColVal->value.nData) {
          memcpy(pColVal->value.pData, pVal, pColVal->value.nData);
        }
      }

      if (taosArrayPush(pMerger->pArray, pColVal) == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        return code;
      }
    }

    for (; iCol < pMerger->pTSchema->numOfCols; ++iCol) {
      pTColumn = &pMerger->pTSchema->columns[iCol];
      taosArrayPush(pMerger->pArray, &COL_VAL_NONE(pTColumn->colId, pTColumn->type));
    }
K
kailixu 已提交
774

775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
    pMerger->version = key.version;
    return 0;
  } else {
    ASSERT(((SColVal *)pMerger->pArray->pData)->value.val == key.ts);

    for (iCol = 1; iCol < pMerger->pTSchema->numOfCols && jCol < pTSchema->numOfCols; ++iCol) {
      pTColumn = &pMerger->pTSchema->columns[iCol];
      if (pTSchema->columns[jCol].colId < pTColumn->colId) {
        ++jCol;
        --iCol;
        continue;
      } else if (pTSchema->columns[jCol].colId > pTColumn->colId) {
        continue;
      }

      tsdbRowGetColVal(pRow, pTSchema, jCol++, pColVal);

      if (key.version > pMerger->version) {
        if (!COL_VAL_IS_NONE(pColVal)) {
          if (IS_VAR_DATA_TYPE(pColVal->type)) {
            SColVal *pTColVal = taosArrayGet(pMerger->pArray, iCol);
            if (!COL_VAL_IS_NULL(pColVal)) {
              code = tRealloc(&pTColVal->value.pData, pColVal->value.nData);
              if (code) return code;

              pTColVal->value.nData = pColVal->value.nData;
              if (pTColVal->value.nData) {
                memcpy(pTColVal->value.pData, pColVal->value.pData, pTColVal->value.nData);
              }
              pTColVal->flag = 0;
            } else {
              tFree(pTColVal->value.pData);
              taosArraySet(pMerger->pArray, iCol, pColVal);
K
kailixu 已提交
808 809 810 811
            }
          } else {
            taosArraySet(pMerger->pArray, iCol, pColVal);
          }
812
        }
813 814 815 816 817 818 819 820 821 822 823 824 825 826
      } else if (key.version < pMerger->version) {
        SColVal *tColVal = (SColVal *)taosArrayGet(pMerger->pArray, iCol);
        if (COL_VAL_IS_NONE(tColVal) && !COL_VAL_IS_NONE(pColVal)) {
          if ((!COL_VAL_IS_NULL(pColVal)) && IS_VAR_DATA_TYPE(pColVal->type)) {
            code = tRealloc(&tColVal->value.pData, pColVal->value.nData);
            if (code) return code;

            tColVal->value.nData = pColVal->value.nData;
            if (pColVal->value.nData) {
              memcpy(tColVal->value.pData, pColVal->value.pData, pColVal->value.nData);
            }
            tColVal->flag = 0;
          } else {
            taosArraySet(pMerger->pArray, iCol, pColVal);
D
dapan1121 已提交
827
          }
828
        }
829 830
      } else {
        ASSERT(0 && "dup versions not allowed");
831 832 833
      }
    }

834 835 836
    pMerger->version = key.version;
    return code;
  }
837
}
H
Hongze Cheng 已提交
838

H
Haojun Liao 已提交
839 840 841
int32_t tsdbRowMergerInit_rv(SRowMerger* pMerger, STSchema *pSchema) {
  pMerger->pTSchema = pSchema;
  pMerger->pArray = taosArrayInit(pSchema->numOfCols, sizeof(SColVal));
H
Hongze Cheng 已提交
842
  if (pMerger->pArray == NULL) {
H
Haojun Liao 已提交
843 844 845
    return TSDB_CODE_OUT_OF_MEMORY;
  } else {
    return TSDB_CODE_SUCCESS;
H
Hongze Cheng 已提交
846
  }
H
Haojun Liao 已提交
847
}
H
Hongze Cheng 已提交
848

H
Haojun Liao 已提交
849 850 851 852 853 854
void tsdbRowMergerClear_rv(SRowMerger* pMerger) {
  for (int32_t iCol = 1; iCol < pMerger->pTSchema->numOfCols; iCol++) {
    SColVal *pTColVal = taosArrayGet(pMerger->pArray, iCol);
    if (IS_VAR_DATA_TYPE(pTColVal->type)) {
      tFree(pTColVal->value.pData);
    }
H
Hongze Cheng 已提交
855 856
  }

H
Haojun Liao 已提交
857 858
  taosArrayClear(pMerger->pArray);
}
X
Xiaoyu Wang 已提交
859

H
Haojun Liao 已提交
860 861 862 863 864 865
void tsdbRowMergerCleanup_rv(SRowMerger* pMerger) {
  int32_t numOfCols = taosArrayGetSize(pMerger->pArray);
  for (int32_t iCol = 1; iCol < numOfCols; iCol++) {
    SColVal *pTColVal = taosArrayGet(pMerger->pArray, iCol);
    if (IS_VAR_DATA_TYPE(pTColVal->type)) {
      tFree(pTColVal->value.pData);
H
Hongze Cheng 已提交
866 867 868
    }
  }

H
Haojun Liao 已提交
869
  taosArrayDestroy(pMerger->pArray);
H
Hongze Cheng 已提交
870
}
H
Haojun Liao 已提交
871

X
Xiaoyu Wang 已提交
872
void tsdbRowMergerClear(SRowMerger *pMerger) {
873 874 875 876
  for (int32_t iCol = 1; iCol < pMerger->pTSchema->numOfCols; iCol++) {
    SColVal *pTColVal = taosArrayGet(pMerger->pArray, iCol);
    if (IS_VAR_DATA_TYPE(pTColVal->type)) {
      tFree(pTColVal->value.pData);
877 878 879
    }
  }

X
Xiaoyu Wang 已提交
880
  taosArrayDestroy(pMerger->pArray);
881
}
882
/*
H
Hongze Cheng 已提交
883
int32_t tsdbRowMerge(SRowMerger *pMerger, TSDBROW *pRow) {
H
Hongze Cheng 已提交
884
  int32_t  code = 0;
H
Hongze Cheng 已提交
885
  TSDBKEY  key = TSDBROW_KEY(pRow);
H
Hongze Cheng 已提交
886 887
  SColVal *pColVal = &(SColVal){0};

H
Hongze Cheng 已提交
888
  ASSERT(((SColVal *)pMerger->pArray->pData)->value.val == key.ts);
H
Hongze Cheng 已提交
889 890 891 892 893

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

    if (key.version > pMerger->version) {
H
Hongze Cheng 已提交
894
      if (!COL_VAL_IS_NONE(pColVal)) {
895 896
        if (IS_VAR_DATA_TYPE(pColVal->type)) {
          SColVal *pTColVal = taosArrayGet(pMerger->pArray, iCol);
897 898 899 900 901
          if (!COL_VAL_IS_NULL(pColVal)) {
            code = tRealloc(&pTColVal->value.pData, pColVal->value.nData);
            if (code) goto _exit;

            pTColVal->value.nData = pColVal->value.nData;
D
dapan1121 已提交
902
            if (pTColVal->value.nData) {
X
Xiaoyu Wang 已提交
903
              memcpy(pTColVal->value.pData, pColVal->value.pData, pTColVal->value.nData);
D
dapan1121 已提交
904
            }
D
dapan1121 已提交
905
            pTColVal->flag = 0;
906 907 908 909 910
          } else {
            tFree(pTColVal->value.pData);
            pTColVal->value.pData = NULL;
            taosArraySet(pMerger->pArray, iCol, pColVal);
          }
911 912 913
        } else {
          taosArraySet(pMerger->pArray, iCol, pColVal);
        }
H
Hongze Cheng 已提交
914
      }
H
Hongze Cheng 已提交
915
    } else if (key.version < pMerger->version) {
H
Hongze Cheng 已提交
916
      SColVal *tColVal = (SColVal *)taosArrayGet(pMerger->pArray, iCol);
H
Hongze Cheng 已提交
917
      if (COL_VAL_IS_NONE(tColVal) && !COL_VAL_IS_NONE(pColVal)) {
918 919 920 921 922 923
        if (IS_VAR_DATA_TYPE(pColVal->type)) {
          if (!COL_VAL_IS_NULL(pColVal)) {
            code = tRealloc(&tColVal->value.pData, pColVal->value.nData);
            if (code) goto _exit;

            tColVal->value.nData = pColVal->value.nData;
D
dapan1121 已提交
924
            if (tColVal->value.nData) {
X
Xiaoyu Wang 已提交
925
              memcpy(tColVal->value.pData, pColVal->value.pData, tColVal->value.nData);
D
dapan1121 已提交
926
            }
D
dapan1121 已提交
927
            tColVal->flag = 0;
928 929 930 931 932 933 934 935
          } else {
            tFree(tColVal->value.pData);
            tColVal->value.pData = NULL;
            taosArraySet(pMerger->pArray, iCol, pColVal);
          }
        } else {
          taosArraySet(pMerger->pArray, iCol, pColVal);
        }
H
Hongze Cheng 已提交
936
      }
H
Hongze Cheng 已提交
937 938 939 940 941 942 943 944 945 946
    } else {
      ASSERT(0);
    }
  }

  pMerger->version = key.version;

_exit:
  return code;
}
947
*/
H
Hongze Cheng 已提交
948
int32_t tsdbRowMergerGetRow(SRowMerger *pMerger, SRow **ppRow) {
H
Hongze Cheng 已提交
949
  return tRowBuild(pMerger->pArray, pMerger->pTSchema, ppRow);
H
Hongze Cheng 已提交
950 951
}

952
/*
H
Hongze Cheng 已提交
953
// delete skyline ======================================================
954
static int32_t tsdbMergeSkyline2(SArray *aSkyline1, SArray *aSkyline2, SArray *aSkyline) {
H
Hongze Cheng 已提交
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
  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;
}
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
*/

// delete skyline ======================================================
static int32_t tsdbMergeSkyline(SArray *pSkyline1, SArray *pSkyline2, SArray *pSkyline) {
  int32_t  code = 0;
  int32_t  i1 = 0;
  int32_t  n1 = taosArrayGetSize(pSkyline1);
  int32_t  i2 = 0;
  int32_t  n2 = taosArrayGetSize(pSkyline2);
  TSDBKEY *pKey1;
  TSDBKEY *pKey2;
  int64_t  version1 = 0;
  int64_t  version2 = 0;

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

  taosArrayClear(pSkyline);
  TSDBKEY **pItem = TARRAY_GET_ELEM(pSkyline, 0);

  while (i1 < n1 && i2 < n2) {
    pKey1 = (TSDBKEY *)taosArrayGetP(pSkyline1, i1);
    pKey2 = (TSDBKEY *)taosArrayGetP(pSkyline2, i2);

    if (pKey1->ts < pKey2->ts) {
      version1 = pKey1->version;
      *pItem = pKey1;
      i1++;
    } else if (pKey1->ts > pKey2->ts) {
      version2 = pKey2->version;
      *pItem = pKey2;
      i2++;
    } else {
      version1 = pKey1->version;
      version2 = pKey2->version;
      *pItem = pKey1;
      i1++;
      i2++;
    }

    (*pItem)->version = TMAX(version1, version2);
    pItem++;
  }

  while (i1 < n1) {
    pKey1 = (TSDBKEY *)taosArrayGetP(pSkyline1, i1);
    *pItem = pKey1;
    pItem++;
    i1++;
  }

  while (i2 < n2) {
    pKey2 = (TSDBKEY *)taosArrayGetP(pSkyline2, i2);
    *pItem = pKey2;
    pItem++;
    i2++;
  }

1077
  pSkyline->size = TARRAY_ELEM_IDX(pSkyline, pItem);
1078 1079 1080 1081 1082 1083 1084 1085 1086
  return code;
}

int32_t tsdbBuildDeleteSkylineImpl(SArray *aSkyline, int32_t sidx, int32_t eidx, SArray *pSkyline) {
  int32_t   code = 0;
  SDelData *pDelData;
  int32_t   midx;

  taosArrayClear(pSkyline);
H
Hongze Cheng 已提交
1087
  if (sidx == eidx) {
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
    TSDBKEY *pItem1 = taosArrayGet(aSkyline, sidx * 2);
    TSDBKEY *pItem2 = taosArrayGet(aSkyline, sidx * 2 + 1);
    taosArrayPush(pSkyline, &pItem1);
    taosArrayPush(pSkyline, &pItem2);
  } else {
    SArray *pSkyline1 = NULL;
    SArray *pSkyline2 = NULL;
    midx = (sidx + eidx) / 2;

    pSkyline1 = taosArrayInit((midx - sidx + 1) * 2, POINTER_BYTES);
    pSkyline2 = taosArrayInit((eidx - midx) * 2, POINTER_BYTES);
    if (pSkyline1 == NULL || pSkyline1 == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _clear;
    }

    code = tsdbBuildDeleteSkylineImpl(aSkyline, sidx, midx, pSkyline1);
    if (code) goto _clear;

    code = tsdbBuildDeleteSkylineImpl(aSkyline, midx + 1, eidx, pSkyline2);
    if (code) goto _clear;

    code = tsdbMergeSkyline(pSkyline1, pSkyline2, pSkyline);

  _clear:
    taosArrayDestroy(pSkyline1);
    taosArrayDestroy(pSkyline2);
  }

  return code;
}

H
Hongze Cheng 已提交
1120
int32_t tsdbBuildDeleteSkyline(SArray *aDelData, int32_t sidx, int32_t eidx, SArray *aSkyline) {
1121
  SDelData *pDelData;
H
Hongze Cheng 已提交
1122 1123 1124 1125 1126
  int32_t   code = 0;
  int32_t   dataNum = eidx - sidx + 1;
  SArray   *aTmpSkyline = taosArrayInit(dataNum * 2, sizeof(TSDBKEY));
  SArray   *pSkyline = taosArrayInit(dataNum * 2, POINTER_BYTES);

H
Hongze Cheng 已提交
1127
  taosArrayClear(aSkyline);
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
  for (int32_t i = sidx; i <= eidx; ++i) {
    pDelData = (SDelData *)taosArrayGet(aDelData, i);
    taosArrayPush(aTmpSkyline, &(TSDBKEY){.ts = pDelData->sKey, .version = pDelData->version});
    taosArrayPush(aTmpSkyline, &(TSDBKEY){.ts = pDelData->eKey, .version = 0});
  }

  code = tsdbBuildDeleteSkylineImpl(aTmpSkyline, sidx, eidx, pSkyline);
  if (code) goto _clear;

  int32_t skylineNum = taosArrayGetSize(pSkyline);
  for (int32_t i = 0; i < skylineNum; ++i) {
H
Hongze Cheng 已提交
1139 1140
    TSDBKEY *p = taosArrayGetP(pSkyline, i);
    taosArrayPush(aSkyline, p);
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
  }

_clear:
  taosArrayDestroy(aTmpSkyline);
  taosArrayDestroy(pSkyline);

  return code;
}

/*
int32_t tsdbBuildDeleteSkyline2(SArray *aDelData, int32_t sidx, int32_t eidx, SArray *aSkyline) {
H
Hongze Cheng 已提交
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 1177 1178 1179 1180 1181 1182 1183 1184 1185
  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 已提交
1186 1187
  return code;
}
1188
*/
H
Hongze Cheng 已提交
1189

H
Hongze Cheng 已提交
1190
// SBlockData ======================================================
H
Hongze Cheng 已提交
1191
int32_t tBlockDataCreate(SBlockData *pBlockData) {
H
Hongze Cheng 已提交
1192
  int32_t code = 0;
H
Hongze Cheng 已提交
1193

H
Hongze Cheng 已提交
1194 1195
  pBlockData->suid = 0;
  pBlockData->uid = 0;
H
Hongze Cheng 已提交
1196
  pBlockData->nRow = 0;
H
Hongze Cheng 已提交
1197
  pBlockData->aUid = NULL;
H
Hongze Cheng 已提交
1198 1199
  pBlockData->aVersion = NULL;
  pBlockData->aTSKEY = NULL;
H
Hongze Cheng 已提交
1200
  pBlockData->nColData = 0;
H
Hongze Cheng 已提交
1201
  pBlockData->aColData = NULL;
H
Hongze Cheng 已提交
1202 1203 1204 1205 1206

_exit:
  return code;
}

H
Hongze Cheng 已提交
1207
void tBlockDataDestroy(SBlockData *pBlockData) {
H
Hongze Cheng 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
  tFree(pBlockData->aUid);
  tFree(pBlockData->aVersion);
  tFree(pBlockData->aTSKEY);

  for (int32_t i = 0; i < pBlockData->nColData; i++) {
    tColDataDestroy(&pBlockData->aColData[i]);
  }

  if (pBlockData->aColData) {
    taosMemoryFree(pBlockData->aColData);
    pBlockData->aColData = NULL;
  }
H
Hongze Cheng 已提交
1220 1221
}

H
Hongze Cheng 已提交
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
static int32_t tBlockDataAdjustColData(SBlockData *pBlockData, int32_t nColData) {
  int32_t code = 0;

  if (pBlockData->nColData > nColData) {
    for (int32_t i = nColData; i < pBlockData->nColData; i++) {
      tColDataDestroy(&pBlockData->aColData[i]);
    }
  } else if (pBlockData->nColData < nColData) {
    SColData *aColData = taosMemoryRealloc(pBlockData->aColData, sizeof(SBlockData) * nColData);
    if (aColData == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }

    pBlockData->aColData = aColData;
    memset(&pBlockData->aColData[pBlockData->nColData], 0, sizeof(SBlockData) * (nColData - pBlockData->nColData));
  }
  pBlockData->nColData = nColData;

_exit:
  return code;
}
H
Hongze Cheng 已提交
1244
int32_t tBlockDataInit(SBlockData *pBlockData, TABLEID *pId, STSchema *pTSchema, int16_t *aCid, int32_t nCid) {
H
Hongze Cheng 已提交
1245 1246
  int32_t code = 0;

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

H
Hongze Cheng 已提交
1249 1250
  pBlockData->suid = pId->suid;
  pBlockData->uid = pId->uid;
H
Hongze Cheng 已提交
1251
  pBlockData->nRow = 0;
H
Hongze Cheng 已提交
1252

H
Hongze Cheng 已提交
1253
  if (aCid) {
H
Hongze Cheng 已提交
1254 1255 1256
    code = tBlockDataAdjustColData(pBlockData, nCid);
    if (code) goto _exit;

H
Hongze Cheng 已提交
1257
    int32_t   iColumn = 1;
H
Hongze Cheng 已提交
1258
    STColumn *pTColumn = &pTSchema->columns[iColumn];
H
Hongze Cheng 已提交
1259
    for (int32_t iCid = 0; iCid < nCid; iCid++) {
H
Hongze Cheng 已提交
1260 1261 1262 1263 1264
      if (ASSERTS(pTColumn != NULL, "invalid input param")) {
        code = TSDB_CODE_INVALID_PARA;
        goto _exit;
      }

H
Hongze Cheng 已提交
1265
      while (pTColumn->colId < aCid[iCid]) {
H
Hongze Cheng 已提交
1266
        iColumn++;
H
Hongze Cheng 已提交
1267 1268
        ASSERT(iColumn < pTSchema->numOfCols);
        pTColumn = &pTSchema->columns[iColumn];
H
Hongze Cheng 已提交
1269
      }
H
Hongze Cheng 已提交
1270

H
Hongze Cheng 已提交
1271 1272 1273 1274 1275
      if (ASSERTS(pTColumn->colId == aCid[iCid], "invalid input param")) {
        code = TSDB_CODE_INVALID_PARA;
        goto _exit;
      }

H
Hongze Cheng 已提交
1276 1277
      tColDataInit(&pBlockData->aColData[iCid], pTColumn->colId, pTColumn->type,
                   (pTColumn->flags & COL_SMA_ON) ? 1 : 0);
H
Hongze Cheng 已提交
1278

H
Hongze Cheng 已提交
1279 1280
      iColumn++;
      pTColumn = (iColumn < pTSchema->numOfCols) ? &pTSchema->columns[iColumn] : NULL;
H
Hongze Cheng 已提交
1281 1282
    }
  } else {
H
Hongze Cheng 已提交
1283 1284
    code = tBlockDataAdjustColData(pBlockData, pTSchema->numOfCols - 1);
    if (code) goto _exit;
H
Hongze Cheng 已提交
1285

H
Hongze Cheng 已提交
1286 1287 1288 1289
    for (int32_t iColData = 0; iColData < pBlockData->nColData; iColData++) {
      STColumn *pTColumn = &pTSchema->columns[iColData + 1];
      tColDataInit(&pBlockData->aColData[iColData], pTColumn->colId, pTColumn->type,
                   (pTColumn->flags & COL_SMA_ON) ? 1 : 0);
H
Hongze Cheng 已提交
1290
    }
H
Hongze Cheng 已提交
1291 1292 1293 1294
  }

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

H
Hongze Cheng 已提交
1297 1298 1299
void tBlockDataReset(SBlockData *pBlockData) {
  pBlockData->suid = 0;
  pBlockData->uid = 0;
H
Hongze Cheng 已提交
1300
  pBlockData->nRow = 0;
H
Hongze Cheng 已提交
1301 1302 1303
  for (int32_t i = 0; i < pBlockData->nColData; i++) {
    tColDataDestroy(&pBlockData->aColData[i]);
  }
H
Hongze Cheng 已提交
1304
  pBlockData->nColData = 0;
H
Hongze Cheng 已提交
1305
  taosMemoryFreeClear(pBlockData->aColData);
H
Hongze Cheng 已提交
1306 1307 1308
}

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

H
Hongze Cheng 已提交
1311
  pBlockData->nRow = 0;
H
Hongze Cheng 已提交
1312
  for (int32_t iColData = 0; iColData < pBlockData->nColData; iColData++) {
H
Hongze Cheng 已提交
1313
    tColDataClear(tBlockDataGetColDataByIdx(pBlockData, iColData));
H
Hongze Cheng 已提交
1314 1315
  }
}
H
Hongze Cheng 已提交
1316

H
Hongze Cheng 已提交
1317 1318 1319 1320 1321 1322
/* flag > 0: forward update
 * flag == 0: insert
 * flag < 0: backward update
 */
static int32_t tBlockDataUpsertBlockRow(SBlockData *pBlockData, SBlockData *pBlockDataFrom, int32_t iRow,
                                        int32_t flag) {
H
Hongze Cheng 已提交
1323 1324 1325 1326
  int32_t code = 0;

  SColVal   cv = {0};
  int32_t   iColDataFrom = 0;
H
Hongze Cheng 已提交
1327
  SColData *pColDataFrom = (iColDataFrom < pBlockDataFrom->nColData) ? &pBlockDataFrom->aColData[iColDataFrom] : NULL;
H
Hongze Cheng 已提交
1328 1329

  for (int32_t iColDataTo = 0; iColDataTo < pBlockData->nColData; iColDataTo++) {
H
Hongze Cheng 已提交
1330
    SColData *pColDataTo = &pBlockData->aColData[iColDataTo];
H
Hongze Cheng 已提交
1331 1332

    while (pColDataFrom && pColDataFrom->cid < pColDataTo->cid) {
H
Hongze Cheng 已提交
1333
      pColDataFrom = (++iColDataFrom < pBlockDataFrom->nColData) ? &pBlockDataFrom->aColData[iColDataFrom] : NULL;
H
Hongze Cheng 已提交
1334 1335 1336
    }

    if (pColDataFrom == NULL || pColDataFrom->cid > pColDataTo->cid) {
H
Hongze Cheng 已提交
1337
      cv = COL_VAL_NONE(pColDataTo->cid, pColDataTo->type);
H
Hongze Cheng 已提交
1338
      if (flag == 0 && (code = tColDataAppendValue(pColDataTo, &cv))) goto _exit;
H
Hongze Cheng 已提交
1339 1340 1341
    } else {
      tColDataGetValue(pColDataFrom, iRow, &cv);

H
Hongze Cheng 已提交
1342 1343 1344 1345 1346
      if (flag) {
        code = tColDataUpdateValue(pColDataTo, &cv, flag > 0);
      } else {
        code = tColDataAppendValue(pColDataTo, &cv);
      }
H
Hongze Cheng 已提交
1347 1348
      if (code) goto _exit;

H
Hongze Cheng 已提交
1349
      pColDataFrom = (++iColDataFrom < pBlockDataFrom->nColData) ? &pBlockDataFrom->aColData[iColDataFrom] : NULL;
H
Hongze Cheng 已提交
1350 1351 1352 1353 1354 1355 1356
    }
  }

_exit:
  return code;
}

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

H
Hongze Cheng 已提交
1360 1361 1362 1363 1364 1365
  ASSERT(pBlockData->suid || pBlockData->uid);

  // uid
  if (pBlockData->uid == 0) {
    ASSERT(uid);
    code = tRealloc((uint8_t **)&pBlockData->aUid, sizeof(int64_t) * (pBlockData->nRow + 1));
H
Hongze Cheng 已提交
1366
    if (code) goto _exit;
H
Hongze Cheng 已提交
1367 1368 1369
    pBlockData->aUid[pBlockData->nRow] = uid;
  }
  // version
H
Hongze Cheng 已提交
1370
  code = tRealloc((uint8_t **)&pBlockData->aVersion, sizeof(int64_t) * (pBlockData->nRow + 1));
H
Hongze Cheng 已提交
1371
  if (code) goto _exit;
H
Hongze Cheng 已提交
1372 1373
  pBlockData->aVersion[pBlockData->nRow] = TSDBROW_VERSION(pRow);
  // timestamp
H
Hongze Cheng 已提交
1374
  code = tRealloc((uint8_t **)&pBlockData->aTSKEY, sizeof(TSKEY) * (pBlockData->nRow + 1));
H
Hongze Cheng 已提交
1375
  if (code) goto _exit;
H
Hongze Cheng 已提交
1376
  pBlockData->aTSKEY[pBlockData->nRow] = TSDBROW_TS(pRow);
H
Hongze Cheng 已提交
1377

H
Hongze Cheng 已提交
1378
  if (pRow->type == TSDBROW_ROW_FMT) {
H
Hongze Cheng 已提交
1379
    code = tRowUpsertColData(pRow->pTSRow, pTSchema, pBlockData->aColData, pBlockData->nColData, 0 /* append */);
H
Hongze Cheng 已提交
1380
    if (code) goto _exit;
H
Hongze Cheng 已提交
1381
  } else if (pRow->type == TSDBROW_COL_FMT) {
H
Hongze Cheng 已提交
1382
    code = tBlockDataUpsertBlockRow(pBlockData, pRow->pBlockData, pRow->iRow, 0 /* append */);
H
Hongze Cheng 已提交
1383
    if (code) goto _exit;
H
Hongze Cheng 已提交
1384 1385
  } else {
    ASSERT(0);
H
Hongze Cheng 已提交
1386
  }
H
Hongze Cheng 已提交
1387
  pBlockData->nRow++;
H
Hongze Cheng 已提交
1388

H
Hongze Cheng 已提交
1389
_exit:
H
Hongze Cheng 已提交
1390
  return code;
H
Hongze Cheng 已提交
1391
}
H
Hongze Cheng 已提交
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
static int32_t tBlockDataUpdateRow(SBlockData *pBlockData, TSDBROW *pRow, STSchema *pTSchema) {
  int32_t code = 0;

  // version
  int64_t lversion = pBlockData->aVersion[pBlockData->nRow - 1];
  int64_t rversion = TSDBROW_VERSION(pRow);
  ASSERT(lversion != rversion);
  if (rversion > lversion) {
    pBlockData->aVersion[pBlockData->nRow - 1] = rversion;
  }
H
Hongze Cheng 已提交
1402

H
Hongze Cheng 已提交
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
  // update other rows
  if (pRow->type == TSDBROW_ROW_FMT) {
    code = tRowUpsertColData(pRow->pTSRow, pTSchema, pBlockData->aColData, pBlockData->nColData,
                             (rversion > lversion) ? 1 : -1 /* update */);
    if (code) goto _exit;
  } else if (pRow->type == TSDBROW_COL_FMT) {
    code = tBlockDataUpsertBlockRow(pBlockData, pRow->pBlockData, pRow->iRow, (rversion > lversion) ? 1 : -1);
    if (code) goto _exit;
  } else {
    ASSERT(0);
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
1419 1420 1421
int32_t tBlockDataTryUpsertRow(SBlockData *pBlockData, TSDBROW *pRow, int64_t uid) {
  if (pBlockData->nRow == 0) {
    return 1;
H
Hongze Cheng 已提交
1422
  } else if (pBlockData->aTSKEY[pBlockData->nRow - 1] == TSDBROW_TS(pRow)) {
H
Hongze Cheng 已提交
1423 1424 1425 1426 1427 1428
    return pBlockData->nRow;
  } else {
    return pBlockData->nRow + 1;
  }
}

H
Hongze Cheng 已提交
1429
int32_t tBlockDataUpsertRow(SBlockData *pBlockData, TSDBROW *pRow, STSchema *pTSchema, int64_t uid) {
H
Hongze Cheng 已提交
1430
  if (pBlockData->nRow > 0 && pBlockData->aTSKEY[pBlockData->nRow - 1] == TSDBROW_TS(pRow)) {
H
Hongze Cheng 已提交
1431 1432 1433 1434
    return tBlockDataUpdateRow(pBlockData, pRow, pTSchema);
  } else {
    return tBlockDataAppendRow(pBlockData, pRow, pTSchema, uid);
  }
1435
}
H
Hongze Cheng 已提交
1436

H
Hongze Cheng 已提交
1437 1438
void tBlockDataGetColData(SBlockData *pBlockData, int16_t cid, SColData **ppColData) {
  ASSERT(cid != PRIMARYKEY_TIMESTAMP_COL_ID);
H
Hongze Cheng 已提交
1439
  int32_t lidx = 0;
H
Hongze Cheng 已提交
1440
  int32_t ridx = pBlockData->nColData - 1;
H
Hongze Cheng 已提交
1441

H
Hongze Cheng 已提交
1442
  while (lidx <= ridx) {
H
Hongze Cheng 已提交
1443
    int32_t   midx = (lidx + ridx) >> 1;
M
Minglei Jin 已提交
1444
    SColData *pColData = tBlockDataGetColDataByIdx(pBlockData, midx);
H
refact  
Hongze Cheng 已提交
1445
    int32_t   c = (pColData->cid == cid) ? 0 : ((pColData->cid > cid) ? 1 : -1);
H
Hongze Cheng 已提交
1446 1447 1448 1449 1450 1451 1452 1453 1454

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

  *ppColData = NULL;
H
Hongze Cheng 已提交
1458 1459
}

H
Hongze Cheng 已提交
1460 1461
int32_t tCmprBlockData(SBlockData *pBlockData, int8_t cmprAlg, uint8_t **ppOut, int32_t *szOut, uint8_t *aBuf[],
                       int32_t aBufN[]) {
H
Hongze Cheng 已提交
1462
  int32_t code = 0;
H
Hongze Cheng 已提交
1463

H
Hongze Cheng 已提交
1464 1465 1466 1467 1468 1469 1470 1471 1472
  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 已提交
1473
  aBufN[0] = 0;
H
Hongze Cheng 已提交
1474
  for (int32_t iColData = 0; iColData < pBlockData->nColData; iColData++) {
H
Hongze Cheng 已提交
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
    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 已提交
1488
      code = tsdbCmprColData(pColData, cmprAlg, &blockCol, &aBuf[0], aBufN[0], &aBuf[2]);
H
Hongze Cheng 已提交
1489 1490
      if (code) goto _exit;

H
Hongze Cheng 已提交
1491
      blockCol.offset = aBufN[0];
H
Hongze Cheng 已提交
1492
      aBufN[0] = aBufN[0] + blockCol.szBitmap + blockCol.szOffset + blockCol.szValue;
H
Hongze Cheng 已提交
1493 1494
    }

H
Hongze Cheng 已提交
1495
    code = tRealloc(&aBuf[1], hdr.szBlkCol + tPutBlockCol(NULL, &blockCol));
H
Hongze Cheng 已提交
1496
    if (code) goto _exit;
H
Hongze Cheng 已提交
1497
    hdr.szBlkCol += tPutBlockCol(aBuf[1] + hdr.szBlkCol, &blockCol);
H
Hongze Cheng 已提交
1498
  }
H
Hongze Cheng 已提交
1499

H
Hongze Cheng 已提交
1500 1501
  // SBlockCol
  aBufN[1] = hdr.szBlkCol;
H
Hongze Cheng 已提交
1502

H
Hongze Cheng 已提交
1503
  // uid + version + tskey
H
Hongze Cheng 已提交
1504
  aBufN[2] = 0;
H
Hongze Cheng 已提交
1505 1506
  if (pBlockData->uid == 0) {
    code = tsdbCmprData((uint8_t *)pBlockData->aUid, sizeof(int64_t) * pBlockData->nRow, TSDB_DATA_TYPE_BIGINT, cmprAlg,
H
Hongze Cheng 已提交
1507
                        &aBuf[2], aBufN[2], &hdr.szUid, &aBuf[3]);
H
Hongze Cheng 已提交
1508
    if (code) goto _exit;
H
Hongze Cheng 已提交
1509
  }
H
Hongze Cheng 已提交
1510
  aBufN[2] += hdr.szUid;
H
Hongze Cheng 已提交
1511

H
Hongze Cheng 已提交
1512
  code = tsdbCmprData((uint8_t *)pBlockData->aVersion, sizeof(int64_t) * pBlockData->nRow, TSDB_DATA_TYPE_BIGINT,
H
Hongze Cheng 已提交
1513
                      cmprAlg, &aBuf[2], aBufN[2], &hdr.szVer, &aBuf[3]);
H
Hongze Cheng 已提交
1514
  if (code) goto _exit;
H
Hongze Cheng 已提交
1515
  aBufN[2] += hdr.szVer;
H
Hongze Cheng 已提交
1516

H
Hongze Cheng 已提交
1517
  code = tsdbCmprData((uint8_t *)pBlockData->aTSKEY, sizeof(TSKEY) * pBlockData->nRow, TSDB_DATA_TYPE_TIMESTAMP,
H
Hongze Cheng 已提交
1518
                      cmprAlg, &aBuf[2], aBufN[2], &hdr.szKey, &aBuf[3]);
H
Hongze Cheng 已提交
1519
  if (code) goto _exit;
H
Hongze Cheng 已提交
1520
  aBufN[2] += hdr.szKey;
H
Hongze Cheng 已提交
1521

H
Hongze Cheng 已提交
1522
  // hdr
H
Hongze Cheng 已提交
1523 1524
  aBufN[3] = tPutDiskDataHdr(NULL, &hdr);
  code = tRealloc(&aBuf[3], aBufN[3]);
H
Hongze Cheng 已提交
1525
  if (code) goto _exit;
H
Hongze Cheng 已提交
1526
  tPutDiskDataHdr(aBuf[3], &hdr);
H
Hongze Cheng 已提交
1527

H
Hongze Cheng 已提交
1528 1529
  // aggragate
  if (ppOut) {
H
Hongze Cheng 已提交
1530
    *szOut = aBufN[0] + aBufN[1] + aBufN[2] + aBufN[3];
H
Hongze Cheng 已提交
1531 1532
    code = tRealloc(ppOut, *szOut);
    if (code) goto _exit;
H
Hongze Cheng 已提交
1533

H
Hongze Cheng 已提交
1534 1535 1536 1537
    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 已提交
1538
    }
H
Hongze Cheng 已提交
1539 1540
    if (aBufN[0]) {
      memcpy(*ppOut + aBufN[3] + aBufN[2] + aBufN[1], aBuf[0], aBufN[0]);
H
Hongze Cheng 已提交
1541
    }
H
Hongze Cheng 已提交
1542 1543
  }

H
Hongze Cheng 已提交
1544 1545 1546 1547
_exit:
  return code;
}

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

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

  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 已提交
1585
  n += hdr.szKey;
H
Hongze Cheng 已提交
1586 1587 1588 1589

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

H
Hongze Cheng 已提交
1590
  int32_t nColData = 0;
H
Hongze Cheng 已提交
1591 1592 1593 1594
  int32_t nt = 0;
  while (nt < hdr.szBlkCol) {
    SBlockCol blockCol = {0};
    nt += tGetBlockCol(pIn + n + nt, &blockCol);
H
Hongze Cheng 已提交
1595 1596 1597
    ++nColData;
  }
  ASSERT(nt == hdr.szBlkCol);
H
Hongze Cheng 已提交
1598

H
Hongze Cheng 已提交
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
  code = tBlockDataAdjustColData(pBlockData, nColData);
  if (code) goto _exit;

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

    SColData *pColData = &pBlockData->aColData[iColData++];
H
Hongze Cheng 已提交
1609 1610 1611 1612 1613 1614 1615 1616

    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 已提交
1617 1618
      code = tsdbDecmprColData(pIn + n + hdr.szBlkCol + blockCol.offset, &blockCol, hdr.cmprAlg, hdr.nRow, pColData,
                               &aBuf[0]);
H
Hongze Cheng 已提交
1619 1620 1621 1622 1623
      if (code) goto _exit;
    }
  }

_exit:
H
Hongze Cheng 已提交
1624
  return code;
H
Hongze Cheng 已提交
1625 1626
}

H
Hongze Cheng 已提交
1627
// SDiskDataHdr ==============================
H
Hongze Cheng 已提交
1628 1629
int32_t tPutDiskDataHdr(uint8_t *p, const SDiskDataHdr *pHdr) {
  int32_t n = 0;
H
Hongze Cheng 已提交
1630 1631

  n += tPutU32(p ? p + n : p, pHdr->delimiter);
H
Hongze Cheng 已提交
1632
  n += tPutU32v(p ? p + n : p, pHdr->fmtVer);
H
Hongze Cheng 已提交
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
  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 已提交
1650
  n += tGetU32v(p + n, &pHdr->fmtVer);
H
Hongze Cheng 已提交
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
  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 已提交
1663
// ALGORITHM ==============================
H
Hongze Cheng 已提交
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
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 已提交
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
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 已提交
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
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 已提交
1754
int32_t tsdbCmprColData(SColData *pColData, int8_t cmprAlg, SBlockCol *pBlockCol, uint8_t **ppOut, int32_t nOut,
H
Hongze Cheng 已提交
1755
                        uint8_t **ppBuf) {
H
Hongze Cheng 已提交
1756 1757
  int32_t code = 0;

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

H
Hongze Cheng 已提交
1760 1761 1762 1763 1764
  pBlockCol->szBitmap = 0;
  pBlockCol->szOffset = 0;
  pBlockCol->szValue = 0;

  int32_t size = 0;
H
Hongze Cheng 已提交
1765 1766
  // bitmap
  if (pColData->flag != HAS_VALUE) {
H
Hongze Cheng 已提交
1767 1768 1769 1770
    int32_t szBitMap;
    if (pColData->flag == (HAS_VALUE | HAS_NULL | HAS_NONE)) {
      szBitMap = BIT2_SIZE(pColData->nVal);
    } else {
H
Hongze Cheng 已提交
1771 1772 1773
      szBitMap = BIT1_SIZE(pColData->nVal);
    }

H
Hongze Cheng 已提交
1774 1775
    code = tsdbCmprData(pColData->pBitMap, szBitMap, TSDB_DATA_TYPE_TINYINT, cmprAlg, ppOut, nOut + size,
                        &pBlockCol->szBitmap, ppBuf);
H
Hongze Cheng 已提交
1776 1777
    if (code) goto _exit;
  }
H
Hongze Cheng 已提交
1778
  size += pBlockCol->szBitmap;
H
Hongze Cheng 已提交
1779 1780

  // offset
1781
  if (IS_VAR_DATA_TYPE(pColData->type) && pColData->flag != (HAS_NULL | HAS_NONE)) {
H
Hongze Cheng 已提交
1782
    code = tsdbCmprData((uint8_t *)pColData->aOffset, sizeof(int32_t) * pColData->nVal, TSDB_DATA_TYPE_INT, cmprAlg,
H
Hongze Cheng 已提交
1783
                        ppOut, nOut + size, &pBlockCol->szOffset, ppBuf);
H
Hongze Cheng 已提交
1784 1785
    if (code) goto _exit;
  }
H
Hongze Cheng 已提交
1786
  size += pBlockCol->szOffset;
H
Hongze Cheng 已提交
1787 1788

  // value
H
Hongze Cheng 已提交
1789
  if ((pColData->flag != (HAS_NULL | HAS_NONE)) && pColData->nData) {
H
Hongze Cheng 已提交
1790
    code = tsdbCmprData((uint8_t *)pColData->pData, pColData->nData, pColData->type, cmprAlg, ppOut, nOut + size,
H
Hongze Cheng 已提交
1791 1792 1793
                        &pBlockCol->szValue, ppBuf);
    if (code) goto _exit;
  }
H
Hongze Cheng 已提交
1794
  size += pBlockCol->szValue;
H
Hongze Cheng 已提交
1795 1796 1797 1798

_exit:
  return code;
}
H
Hongze Cheng 已提交
1799 1800 1801 1802 1803

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 已提交
1804 1805
  ASSERT(pColData->cid == pBlockCol->cid);
  ASSERT(pColData->type == pBlockCol->type);
H
Hongze Cheng 已提交
1806 1807 1808 1809 1810 1811 1812 1813
  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 已提交
1814 1815 1816
    int32_t szBitMap;
    if (pColData->flag == (HAS_VALUE | HAS_NULL | HAS_NONE)) {
      szBitMap = BIT2_SIZE(pColData->nVal);
H
Hongze Cheng 已提交
1817
    } else {
H
Hongze Cheng 已提交
1818
      szBitMap = BIT1_SIZE(pColData->nVal);
H
Hongze Cheng 已提交
1819
    }
H
Hongze Cheng 已提交
1820 1821 1822

    code = tsdbDecmprData(p, pBlockCol->szBitmap, TSDB_DATA_TYPE_TINYINT, cmprAlg, &pColData->pBitMap, szBitMap, ppBuf);
    if (code) goto _exit;
H
Hongze Cheng 已提交
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
  }
  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 已提交
1841 1842 1843
_exit:
  return code;
}