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

#include "tsdb.h"

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

H
Hongze Cheng 已提交
24
void tMapDataClear(SMapData *pMapData) {
H
Hongze Cheng 已提交
25 26
  tsdbFree((uint8_t *)pMapData->aOffset);
  tsdbFree(pMapData->pData);
H
Hongze Cheng 已提交
27 28
}

H
Hongze Cheng 已提交
29 30 31 32 33 34 35 36 37
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 已提交
38
  code = tsdbRealloc((uint8_t **)&pMapData->aOffset, sizeof(int32_t) * pMapData->nItem);
H
Hongze Cheng 已提交
39 40 41 42 43
  if (code) goto _err;
  code = tsdbRealloc(&pMapData->pData, pMapData->nData);
  if (code) goto _err;

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

_err:
  return code;
}

H
Hongze Cheng 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
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 已提交
79

H
Hongze Cheng 已提交
80 81 82
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 已提交
83 84 85 86 87 88
}

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

  n += tPutI32v(p ? p + n : p, pMapData->nItem);
H
Hongze Cheng 已提交
89
  if (pMapData->nItem) {
H
Hongze Cheng 已提交
90 91
    for (int32_t iItem = 0; iItem < pMapData->nItem; iItem++) {
      n += tPutI32v(p ? p + n : p, pMapData->aOffset[iItem]);
H
Hongze Cheng 已提交
92
    }
H
Hongze Cheng 已提交
93 94 95 96 97 98

    n += tPutI32v(p ? p + n : p, pMapData->nData);
    if (p) {
      memcpy(p + n, pMapData->pData, pMapData->nData);
    }
    n += pMapData->nData;
H
Hongze Cheng 已提交
99 100 101 102 103 104 105
  }

  return n;
}

int32_t tGetMapData(uint8_t *p, SMapData *pMapData) {
  int32_t n = 0;
H
Hongze Cheng 已提交
106 107 108
  int32_t offset;

  tMapDataReset(pMapData);
H
Hongze Cheng 已提交
109 110

  n += tGetI32v(p + n, &pMapData->nItem);
H
Hongze Cheng 已提交
111
  if (pMapData->nItem) {
H
Hongze Cheng 已提交
112 113 114 115
    if (tsdbRealloc((uint8_t **)&pMapData->aOffset, sizeof(int32_t) * pMapData->nItem)) return -1;

    for (int32_t iItem = 0; iItem < pMapData->nItem; iItem++) {
      n += tGetI32v(p + n, &pMapData->aOffset[iItem]);
H
Hongze Cheng 已提交
116
    }
H
Hongze Cheng 已提交
117 118 119 120 121

    n += tGetI32v(p + n, &pMapData->nData);
    if (tsdbRealloc(&pMapData->pData, pMapData->nData)) return -1;
    memcpy(pMapData->pData, p + n, pMapData->nData);
    n += pMapData->nData;
H
Hongze Cheng 已提交
122 123 124 125 126 127
  }

  return n;
}

// Memory =======================================================================
H
Hongze Cheng 已提交
128 129 130 131 132 133 134 135 136 137 138
int32_t tsdbRealloc(uint8_t **ppBuf, int64_t size) {
  int32_t  code = 0;
  int64_t  bsize = 0;
  uint8_t *pBuf;

  if (*ppBuf) {
    bsize = *(int64_t *)((*ppBuf) - sizeof(int64_t));
  }

  if (bsize >= size) goto _exit;

H
Hongze Cheng 已提交
139
  if (bsize == 0) bsize = 64;
H
Hongze Cheng 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  while (bsize < size) {
    bsize *= 2;
  }

  pBuf = taosMemoryRealloc(*ppBuf ? (*ppBuf) - sizeof(int64_t) : *ppBuf, bsize + sizeof(int64_t));
  if (pBuf == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  *(int64_t *)pBuf = bsize;
  *ppBuf = pBuf + sizeof(int64_t);

_exit:
  return code;
}

void tsdbFree(uint8_t *pBuf) {
  if (pBuf) {
    taosMemoryFree(pBuf - sizeof(int64_t));
  }
}

H
Hongze Cheng 已提交
163
// TABLEID =======================================================================
H
Hongze Cheng 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
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 已提交
183
// TSDBKEY =======================================================================
H
Hongze Cheng 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
int32_t tsdbKeyCmprFn(const void *p1, const void *p2) {
  TSDBKEY *pKey1 = (TSDBKEY *)p1;
  TSDBKEY *pKey2 = (TSDBKEY *)p2;

  if (pKey1->ts < pKey2->ts) {
    return -1;
  } else if (pKey1->ts > pKey2->ts) {
    return 1;
  }

  if (pKey1->version < pKey2->version) {
    return -1;
  } else if (pKey1->version > pKey2->version) {
    return 1;
  }

  return 0;
H
Hongze Cheng 已提交
201 202
}

H
Hongze Cheng 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
// TSDBKEY ======================================================
static FORCE_INLINE int32_t tPutTSDBKEY(uint8_t *p, TSDBKEY *pKey) {
  int32_t n = 0;

  n += tPutI64v(p ? p + n : p, pKey->version);
  n += tPutI64(p ? p + n : p, pKey->ts);

  return n;
}

static FORCE_INLINE int32_t tGetTSDBKEY(uint8_t *p, TSDBKEY *pKey) {
  int32_t n = 0;

  n += tGetI64v(p + n, &pKey->version);
  n += tGetI64(p + n, &pKey->ts);

  return n;
}

// SBlockIdx ======================================================
H
Hongze Cheng 已提交
223 224 225 226 227 228 229 230 231
void tBlockIdxReset(SBlockIdx *pBlockIdx) {
  pBlockIdx->minKey = TSKEY_MAX;
  pBlockIdx->maxKey = TSKEY_MIN;
  pBlockIdx->minVersion = VERSION_MAX;
  pBlockIdx->maxVersion = VERSION_MIN;
  pBlockIdx->offset = -1;
  pBlockIdx->size = -1;
}

H
Hongze Cheng 已提交
232 233 234 235 236 237
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);
H
Hongze Cheng 已提交
238 239 240 241
  n += tPutI64(p ? p + n : p, pBlockIdx->minKey);
  n += tPutI64(p ? p + n : p, pBlockIdx->maxKey);
  n += tPutI64v(p ? p + n : p, pBlockIdx->minVersion);
  n += tPutI64v(p ? p + n : p, pBlockIdx->maxVersion);
H
Hongze Cheng 已提交
242 243
  n += tPutI64v(p ? p + n : p, pBlockIdx->offset);
  n += tPutI64v(p ? p + n : p, pBlockIdx->size);
H
Hongze Cheng 已提交
244 245 246 247

  return n;
}

H
Hongze Cheng 已提交
248 249 250
int32_t tGetBlockIdx(uint8_t *p, void *ph) {
  int32_t    n = 0;
  SBlockIdx *pBlockIdx = (SBlockIdx *)ph;
H
Hongze Cheng 已提交
251

H
Hongze Cheng 已提交
252 253
  n += tGetI64(p + n, &pBlockIdx->suid);
  n += tGetI64(p + n, &pBlockIdx->uid);
H
Hongze Cheng 已提交
254 255 256 257
  n += tGetI64(p + n, &pBlockIdx->minKey);
  n += tGetI64(p + n, &pBlockIdx->maxKey);
  n += tGetI64v(p + n, &pBlockIdx->minVersion);
  n += tGetI64v(p + n, &pBlockIdx->maxVersion);
H
Hongze Cheng 已提交
258 259
  n += tGetI64v(p + n, &pBlockIdx->offset);
  n += tGetI64v(p + n, &pBlockIdx->size);
H
Hongze Cheng 已提交
260 261 262 263

  return n;
}

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
int32_t tCmprBlockIdx(void const *lhs, void const *rhs) {
  SBlockIdx *lBlockIdx = *(SBlockIdx **)lhs;
  SBlockIdx *rBlockIdx = *(SBlockIdx **)rhs;

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

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

  return 0;
}

H
Hongze Cheng 已提交
283
// SBlock ======================================================
H
Hongze Cheng 已提交
284
void tBlockReset(SBlock *pBlock) {
H
Hongze Cheng 已提交
285 286
  *pBlock =
      (SBlock){.minKey = TSDBKEY_MAX, .maxKey = TSDBKEY_MIN, .minVersion = VERSION_MAX, .maxVersion = VERSION_MIN};
H
Hongze Cheng 已提交
287 288
}

H
Hongze Cheng 已提交
289 290
int32_t tPutBlock(uint8_t *p, void *ph) {
  int32_t n = 0;
H
Hongze Cheng 已提交
291
  SBlock *pBlock = (SBlock *)ph;
H
Hongze Cheng 已提交
292

H
Hongze Cheng 已提交
293 294 295 296
  n += tPutTSDBKEY(p ? p + n : p, &pBlock->minKey);
  n += tPutTSDBKEY(p ? p + n : p, &pBlock->maxKey);
  n += tPutI64v(p ? p + n : p, pBlock->minVersion);
  n += tPutI64v(p ? p + n : p, pBlock->maxVersion);
H
Hongze Cheng 已提交
297 298 299 300 301
  n += tPutI32v(p ? p + n : p, pBlock->nRow);
  n += tPutI8(p ? p + n : p, pBlock->last);
  n += tPutI8(p ? p + n : p, pBlock->hasDup);
  n += tPutI8(p ? p + n : p, pBlock->nSubBlock);
  for (int8_t iSubBlock = 0; iSubBlock < pBlock->nSubBlock; iSubBlock++) {
H
Hongze Cheng 已提交
302
    n += tPutI32v(p ? p + n : p, pBlock->aSubBlock[iSubBlock].nRow);
H
Hongze Cheng 已提交
303
    n += tPutI8(p ? p + n : p, pBlock->aSubBlock[iSubBlock].cmprAlg);
H
Hongze Cheng 已提交
304
    n += tPutI64v(p ? p + n : p, pBlock->aSubBlock[iSubBlock].offset);
H
Hongze Cheng 已提交
305 306 307 308 309 310
    n += tPutI32v(p ? p + n : p, pBlock->aSubBlock[iSubBlock].szBlockCol);
    n += tPutI32v(p ? p + n : p, pBlock->aSubBlock[iSubBlock].szVersion);
    n += tPutI32v(p ? p + n : p, pBlock->aSubBlock[iSubBlock].szTSKEY);
    n += tPutI32v(p ? p + n : p, pBlock->aSubBlock[iSubBlock].szBlock);
    n += tPutI64v(p ? p + n : p, pBlock->aSubBlock[iSubBlock].sOffset);
    n += tPutI32v(p ? p + n : p, pBlock->aSubBlock[iSubBlock].nSma);
H
Hongze Cheng 已提交
311 312
  }

H
Hongze Cheng 已提交
313 314 315 316 317
  return n;
}

int32_t tGetBlock(uint8_t *p, void *ph) {
  int32_t n = 0;
H
Hongze Cheng 已提交
318
  SBlock *pBlock = (SBlock *)ph;
H
Hongze Cheng 已提交
319

H
Hongze Cheng 已提交
320 321 322 323
  n += tGetTSDBKEY(p + n, &pBlock->minKey);
  n += tGetTSDBKEY(p + n, &pBlock->maxKey);
  n += tGetI64v(p + n, &pBlock->minVersion);
  n += tGetI64v(p + n, &pBlock->maxVersion);
H
Hongze Cheng 已提交
324 325 326 327 328
  n += tGetI32v(p + n, &pBlock->nRow);
  n += tGetI8(p + n, &pBlock->last);
  n += tGetI8(p + n, &pBlock->hasDup);
  n += tGetI8(p + n, &pBlock->nSubBlock);
  for (int8_t iSubBlock = 0; iSubBlock < pBlock->nSubBlock; iSubBlock++) {
H
Hongze Cheng 已提交
329
    n += tGetI32v(p + n, &pBlock->aSubBlock[iSubBlock].nRow);
H
Hongze Cheng 已提交
330
    n += tGetI8(p + n, &pBlock->aSubBlock[iSubBlock].cmprAlg);
H
Hongze Cheng 已提交
331
    n += tGetI64v(p + n, &pBlock->aSubBlock[iSubBlock].offset);
H
Hongze Cheng 已提交
332 333 334 335 336 337
    n += tGetI32v(p + n, &pBlock->aSubBlock[iSubBlock].szBlockCol);
    n += tGetI32v(p + n, &pBlock->aSubBlock[iSubBlock].szVersion);
    n += tGetI32v(p + n, &pBlock->aSubBlock[iSubBlock].szTSKEY);
    n += tGetI32v(p + n, &pBlock->aSubBlock[iSubBlock].szBlock);
    n += tGetI64v(p + n, &pBlock->aSubBlock[iSubBlock].sOffset);
    n += tGetI32v(p + n, &pBlock->aSubBlock[iSubBlock].nSma);
H
Hongze Cheng 已提交
338 339
  }

H
Hongze Cheng 已提交
340 341 342
  return n;
}

H
Hongze Cheng 已提交
343 344 345 346
int32_t tBlockCmprFn(const void *p1, const void *p2) {
  SBlock *pBlock1 = (SBlock *)p1;
  SBlock *pBlock2 = (SBlock *)p2;

H
Hongze Cheng 已提交
347
  if (tsdbKeyCmprFn(&pBlock1->maxKey, &pBlock2->minKey) < 0) {
H
Hongze Cheng 已提交
348
    return -1;
H
Hongze Cheng 已提交
349
  } else if (tsdbKeyCmprFn(&pBlock1->minKey, &pBlock2->maxKey) > 0) {
H
Hongze Cheng 已提交
350
    return 1;
H
Hongze Cheng 已提交
351 352 353 354 355
  }

  return 0;
}

H
Hongze Cheng 已提交
356 357 358 359 360 361 362 363
bool tBlockHasSma(SBlock *pBlock) {
  if (pBlock->nSubBlock > 1) return false;
  if (pBlock->last) return false;
  if (pBlock->hasDup) return false;

  return pBlock->aSubBlock[0].nSma > 0;
}

H
Hongze Cheng 已提交
364 365 366 367 368 369 370 371 372 373 374 375
// 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);
  n += tPutI8(p ? p + n : p, pBlockCol->flag);

  if (pBlockCol->flag != HAS_NULL) {
H
Hongze Cheng 已提交
376 377 378 379 380
    n += tPutI32v(p ? p + n : p, pBlockCol->offset);
    n += tPutI32v(p ? p + n : p, pBlockCol->szBitmap);
    n += tPutI32v(p ? p + n : p, pBlockCol->szOffset);
    n += tPutI32v(p ? p + n : p, pBlockCol->szValue);
    n += tPutI32v(p ? p + n : p, pBlockCol->szOrigin);
H
Hongze Cheng 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  }

  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);
  n += tGetI8(p + n, &pBlockCol->flag);

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

  if (pBlockCol->flag != HAS_NULL) {
H
Hongze Cheng 已提交
397 398 399 400 401
    n += tGetI32v(p + n, &pBlockCol->offset);
    n += tGetI32v(p + n, &pBlockCol->szBitmap);
    n += tGetI32v(p + n, &pBlockCol->szOffset);
    n += tGetI32v(p + n, &pBlockCol->szValue);
    n += tGetI32v(p + n, &pBlockCol->szOrigin);
H
Hongze Cheng 已提交
402 403 404 405 406
  }

  return n;
}

H
Hongze Cheng 已提交
407 408 409 410 411 412 413 414 415 416
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 已提交
417
// SDelIdx ======================================================
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
int32_t tCmprDelIdx(void const *lhs, void const *rhs) {
  SDelIdx *lDelIdx = *(SDelIdx **)lhs;
  SDelIdx *rDelIdx = *(SDelIdx **)rhs;

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

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

  return 0;
}

H
Hongze Cheng 已提交
437 438 439 440 441 442 443 444
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 已提交
445 446 447 448

  return n;
}

H
Hongze Cheng 已提交
449 450 451
int32_t tGetDelIdx(uint8_t *p, void *ph) {
  SDelIdx *pDelIdx = (SDelIdx *)ph;
  int32_t  n = 0;
H
Hongze Cheng 已提交
452

H
Hongze Cheng 已提交
453 454 455 456
  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 已提交
457 458 459 460

  return n;
}

H
Hongze Cheng 已提交
461
// SDelData ======================================================
H
Hongze Cheng 已提交
462 463 464 465 466 467 468
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 已提交
469 470 471 472

  return n;
}

H
Hongze Cheng 已提交
473 474 475
int32_t tGetDelData(uint8_t *p, void *ph) {
  SDelData *pDelData = (SDelData *)ph;
  int32_t   n = 0;
H
Hongze Cheng 已提交
476

H
Hongze Cheng 已提交
477 478 479
  n += tGetI64v(p + n, &pDelData->version);
  n += tGetI64(p + n, &pDelData->sKey);
  n += tGetI64(p + n, &pDelData->eKey);
H
Hongze Cheng 已提交
480 481

  return n;
H
Hongze Cheng 已提交
482 483
}

H
Hongze Cheng 已提交
484 485 486 487 488 489
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 已提交
490 491
}

H
Hongze Cheng 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
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;
}

// int tsdFidLevel(int fid, TSKEY now, minute) {
//   if (fid >= pRtn->maxFid) {
//     return 0;
//   } else if (fid >= pRtn->midFid) {
//     return 1;
//   } else if (fid >= pRtn->minFid) {
//     return 2;
//   } else {
//     return -1;
//   }
// }

H
Hongze Cheng 已提交
509 510
// TSDBROW ======================================================
void tsdbRowGetColVal(TSDBROW *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal) {
H
Hongze Cheng 已提交
511 512 513
  STColumn *pTColumn = &pTSchema->columns[iCol];
  SValue    value;

H
Hongze Cheng 已提交
514 515
  ASSERT(iCol > 0);

H
Hongze Cheng 已提交
516
  if (pRow->type == 0) {
H
Hongze Cheng 已提交
517
    tTSRowGetVal(pRow->pTSRow, pTSchema, iCol, pColVal);
H
Hongze Cheng 已提交
518
  } else if (pRow->type == 1) {
H
Hongze Cheng 已提交
519
    SColData *pColData;
H
Hongze Cheng 已提交
520

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

H
Hongze Cheng 已提交
523
    if (pColData) {
H
Hongze Cheng 已提交
524
      tColDataGetValue(pColData, pRow->iRow, pColVal);
H
Hongze Cheng 已提交
525
    } else {
H
Hongze Cheng 已提交
526
      *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
H
Hongze Cheng 已提交
527 528 529 530
    }
  } else {
    ASSERT(0);
  }
H
Hongze Cheng 已提交
531 532
}

H
Hongze Cheng 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
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 已提交
553 554 555 556
int32_t tsdbRowCmprFn(const void *p1, const void *p2) {
  return tsdbKeyCmprFn(&TSDBROW_KEY((TSDBROW *)p1), &TSDBROW_KEY((TSDBROW *)p2));
}

H
Hongze Cheng 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
// 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 已提交
581 582
    if (pIter->i < taosArrayGetSize(pIter->pRow->pBlockData->aColDataP)) {
      SColData *pColData = (SColData *)taosArrayGetP(pIter->pRow->pBlockData->aColDataP, pIter->i);
H
Hongze Cheng 已提交
583 584 585 586 587 588 589 590 591 592 593

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

      return &pIter->colVal;
    }
  }

  return NULL;
}

H
Hongze Cheng 已提交
594 595 596
// SRowMerger ======================================================
int32_t tRowMergerInit(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema) {
  int32_t   code = 0;
H
Hongze Cheng 已提交
597
  TSDBKEY   key = TSDBROW_KEY(pRow);
H
Hongze Cheng 已提交
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
  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);

  *pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = key.ts});
  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 已提交
638
  TSDBKEY  key = TSDBROW_KEY(pRow);
H
Hongze Cheng 已提交
639 640 641 642 643 644 645 646
  SColVal *pColVal = &(SColVal){0};

  ASSERT(((SColVal *)pMerger->pArray->pData)->value.ts == key.ts);

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

    if (key.version > pMerger->version) {
H
Hongze Cheng 已提交
647 648 649
      if (!pColVal->isNone) {
        taosArraySet(pMerger->pArray, iCol, pColVal);
      }
H
Hongze Cheng 已提交
650
    } else if (key.version < pMerger->version) {
H
Hongze Cheng 已提交
651 652 653 654
      SColVal *tColVal = (SColVal *)taosArrayGet(pMerger->pArray, iCol);
      if (tColVal->isNone && !pColVal->isNone) {
        taosArraySet(pMerger->pArray, iCol, pColVal);
      }
H
Hongze Cheng 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667
    } else {
      ASSERT(0);
    }
  }

  pMerger->version = key.version;

_exit:
  return code;
}

int32_t tRowMergerGetRow(SRowMerger *pMerger, STSRow **ppRow) {
  int32_t code = 0;
668 669 670

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

H
Hongze Cheng 已提交
671 672 673
  return code;
}

H
Hongze Cheng 已提交
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
// 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 已提交
777 778 779
  return code;
}

H
Hongze Cheng 已提交
780
// SColData ========================================
H
Hongze Cheng 已提交
781
void tColDataReset(SColData *pColData, int16_t cid, int8_t type, int8_t smaOn) {
H
Hongze Cheng 已提交
782 783
  pColData->cid = cid;
  pColData->type = type;
H
Hongze Cheng 已提交
784
  pColData->smaOn = smaOn;
H
Hongze Cheng 已提交
785 786
  pColData->nVal = 0;
  pColData->flag = 0;
H
Hongze Cheng 已提交
787
  pColData->nData = 0;
H
Hongze Cheng 已提交
788 789
}

H
Hongze Cheng 已提交
790 791 792 793
void tColDataClear(void *ph) {
  SColData *pColData = (SColData *)ph;

  tsdbFree(pColData->pBitMap);
H
Hongze Cheng 已提交
794
  tsdbFree((uint8_t *)pColData->aOffset);
H
Hongze Cheng 已提交
795
  tsdbFree(pColData->pData);
H
Hongze Cheng 已提交
796 797 798 799
}

int32_t tColDataAppendValue(SColData *pColData, SColVal *pColVal) {
  int32_t code = 0;
H
Hongze Cheng 已提交
800 801 802
  int64_t size;
  SValue  value = {0};
  SValue *pValue = &value;
H
Hongze Cheng 已提交
803

H
Hongze Cheng 已提交
804 805 806 807 808 809 810 811 812
  ASSERT(pColVal->cid == pColData->cid);
  ASSERT(pColVal->type == pColData->type);

  // realloc bitmap
  size = BIT2_SIZE(pColData->nVal + 1);
  code = tsdbRealloc(&pColData->pBitMap, size);
  if (code) goto _exit;

  // put value
H
Hongze Cheng 已提交
813
  if (pColVal->isNone) {
H
Hongze Cheng 已提交
814 815
    pColData->flag |= HAS_NONE;
    SET_BIT2(pColData->pBitMap, pColData->nVal, 0);
H
Hongze Cheng 已提交
816
  } else if (pColVal->isNull) {
H
Hongze Cheng 已提交
817 818
    pColData->flag |= HAS_NULL;
    SET_BIT2(pColData->pBitMap, pColData->nVal, 1);
H
Hongze Cheng 已提交
819
  } else {
H
Hongze Cheng 已提交
820 821 822 823 824
    pColData->flag |= HAS_VALUE;
    SET_BIT2(pColData->pBitMap, pColData->nVal, 2);
    pValue = &pColVal->value;
  }

H
Hongze Cheng 已提交
825 826 827
  if (IS_VAR_DATA_TYPE(pColData->type)) {
    // offset
    code = tsdbRealloc((uint8_t **)&pColData->aOffset, sizeof(int32_t) * (pColData->nVal + 1));
H
Hongze Cheng 已提交
828
    if (code) goto _exit;
H
Hongze Cheng 已提交
829
    pColData->aOffset[pColData->nVal] = pColData->nData;
H
Hongze Cheng 已提交
830

H
Hongze Cheng 已提交
831 832 833 834 835 836 837 838 839 840 841
    // value
    if ((!pColVal->isNone) && (!pColVal->isNull)) {
      code = tsdbRealloc(&pColData->pData, pColData->nData + pColVal->value.nData);
      if (code) goto _exit;
      memcpy(pColData->pData + pColData->nData, pColVal->value.pData, pColVal->value.nData);
      pColData->nData += pColVal->value.nData;
    }
  } else {
    code = tsdbRealloc(&pColData->pData, pColData->nData + tPutValue(NULL, pValue, pColVal->type));
    if (code) goto _exit;
    pColData->nData += tPutValue(pColData->pData + pColData->nData, pValue, pColVal->type);
H
Hongze Cheng 已提交
842 843
  }

H
Hongze Cheng 已提交
844 845 846
  pColData->nVal++;

_exit:
H
Hongze Cheng 已提交
847 848 849
  return code;
}

H
Hongze Cheng 已提交
850 851
int32_t tColDataCopy(SColData *pColDataSrc, SColData *pColDataDest) {
  int32_t code = 0;
H
Hongze Cheng 已提交
852
  int32_t size;
H
Hongze Cheng 已提交
853

H
Hongze Cheng 已提交
854 855 856
  pColDataDest->cid = pColDataSrc->cid;
  pColDataDest->type = pColDataSrc->type;
  pColDataDest->smaOn = pColDataSrc->smaOn;
H
Hongze Cheng 已提交
857 858 859
  pColDataDest->nVal = pColDataSrc->nVal;
  pColDataDest->flag = pColDataSrc->flag;

H
Hongze Cheng 已提交
860 861
  size = BIT2_SIZE(pColDataSrc->nVal);
  code = tsdbRealloc(&pColDataDest->pBitMap, size);
H
Hongze Cheng 已提交
862
  if (code) goto _exit;
H
Hongze Cheng 已提交
863
  memcpy(pColDataDest->pBitMap, pColDataSrc->pBitMap, size);
H
Hongze Cheng 已提交
864

H
Hongze Cheng 已提交
865 866
  if (IS_VAR_DATA_TYPE(pColDataDest->type)) {
    size = sizeof(int32_t) * pColDataSrc->nVal;
H
Hongze Cheng 已提交
867

H
Hongze Cheng 已提交
868
    code = tsdbRealloc((uint8_t **)&pColDataDest->aOffset, size);
H
Hongze Cheng 已提交
869 870
    if (code) goto _exit;

H
Hongze Cheng 已提交
871
    memcpy(pColDataDest->aOffset, pColDataSrc->aOffset, size);
H
Hongze Cheng 已提交
872 873
  }

H
Hongze Cheng 已提交
874 875 876 877 878
  code = tsdbRealloc(&pColDataDest->pData, pColDataSrc->nData);
  if (code) goto _exit;
  pColDataDest->nData = pColDataSrc->nData;
  memcpy(pColDataDest->pData, pColDataSrc->pData, pColDataDest->nData);

H
Hongze Cheng 已提交
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 907 908
_exit:
  return code;
}

int32_t tColDataGetValue(SColData *pColData, int32_t iVal, SColVal *pColVal) {
  int32_t code = 0;

  ASSERT(iVal < pColData->nVal);
  ASSERT(pColData->flag);

  if (pColData->flag == HAS_NONE) {
    *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
    goto _exit;
  } else if (pColData->flag == HAS_NULL) {
    *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
    goto _exit;
  } else if (pColData->flag != HAS_VALUE) {
    uint8_t v = GET_BIT2(pColData->pBitMap, iVal);
    if (v == 0) {
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
      goto _exit;
    } else if (v == 1) {
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
      goto _exit;
    }
  }

  // get value
  SValue value;
  if (IS_VAR_DATA_TYPE(pColData->type)) {
H
Hongze Cheng 已提交
909 910 911 912
    if (iVal + 1 < pColData->nVal) {
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
    } else {
      value.nData = pColData->nData - pColData->aOffset[iVal];
H
Hongze Cheng 已提交
913
    }
H
Hongze Cheng 已提交
914 915

    value.pData = pColData->pData + pColData->aOffset[iVal];
H
Hongze Cheng 已提交
916 917 918 919 920 921 922
  } else {
    tGetValue(pColData->pData + tDataTypes[pColData->type].bytes * iVal, &value, pColData->type);
  }
  *pColVal = COL_VAL_VALUE(pColData->cid, pColData->type, value);

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

H
Hongze Cheng 已提交
925
static FORCE_INLINE int32_t tColDataPCmprFn(const void *p1, const void *p2) {
H
Hongze Cheng 已提交
926 927 928 929
  SColData *pColData1 = *(SColData **)p1;
  SColData *pColData2 = *(SColData **)p2;

  if (pColData1->cid < pColData2->cid) {
H
Hongze Cheng 已提交
930
    return -1;
H
Hongze Cheng 已提交
931
  } else if (pColData1->cid > pColData2->cid) {
H
Hongze Cheng 已提交
932 933 934 935 936 937
    return 1;
  }

  return 0;
}

H
Hongze Cheng 已提交
938
// SBlockData ======================================================
H
Hongze Cheng 已提交
939 940
int32_t tBlockDataInit(SBlockData *pBlockData) {
  int32_t code = 0;
H
Hongze Cheng 已提交
941

H
Hongze Cheng 已提交
942 943 944 945 946
  pBlockData->nRow = 0;
  pBlockData->aVersion = NULL;
  pBlockData->aTSKEY = NULL;
  pBlockData->aColDataP = taosArrayInit(0, sizeof(SColData *));
  if (pBlockData->aColDataP == NULL) {
H
Hongze Cheng 已提交
947 948 949 950 951
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
  pBlockData->aColData = taosArrayInit(0, sizeof(SColData));
  if (pBlockData->aColData == NULL) {
H
Hongze Cheng 已提交
952
    taosArrayDestroy(pBlockData->aColDataP);
H
Hongze Cheng 已提交
953 954 955
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
H
Hongze Cheng 已提交
956 957 958 959 960

_exit:
  return code;
}

H
Hongze Cheng 已提交
961 962
void tBlockDataReset(SBlockData *pBlockData) {
  pBlockData->nRow = 0;
H
Hongze Cheng 已提交
963
  taosArrayClear(pBlockData->aColDataP);
H
Hongze Cheng 已提交
964
}
H
Hongze Cheng 已提交
965

H
Hongze Cheng 已提交
966 967 968
void tBlockDataClear(SBlockData *pBlockData) {
  tsdbFree((uint8_t *)pBlockData->aVersion);
  tsdbFree((uint8_t *)pBlockData->aTSKEY);
H
Hongze Cheng 已提交
969
  taosArrayDestroy(pBlockData->aColDataP);
H
Hongze Cheng 已提交
970 971 972
  taosArrayDestroyEx(pBlockData->aColData, tColDataClear);
}

H
Hongze Cheng 已提交
973 974
int32_t tBlockDataAddColData(SBlockData *pBlockData, int32_t iColData, SColData **ppColData) {
  int32_t   code = 0;
H
Hongze Cheng 已提交
975 976 977 978
  SColData *pColData = NULL;
  int32_t   idx = taosArrayGetSize(pBlockData->aColDataP);

  if (idx >= taosArrayGetSize(pBlockData->aColData)) {
H
Hongze Cheng 已提交
979 980 981 982
    if (taosArrayPush(pBlockData->aColData, &((SColData){0})) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
983 984 985
  }
  pColData = (SColData *)taosArrayGet(pBlockData->aColData, idx);

H
Hongze Cheng 已提交
986 987 988
  if (taosArrayInsert(pBlockData->aColDataP, iColData, &pColData) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
H
Hongze Cheng 已提交
989 990
  }

H
Hongze Cheng 已提交
991 992 993 994 995 996
  *ppColData = pColData;
  return code;

_err:
  *ppColData = NULL;
  return code;
H
Hongze Cheng 已提交
997
}
H
Hongze Cheng 已提交
998 999 1000

int32_t tBlockDataAppendRow(SBlockData *pBlockData, TSDBROW *pRow, STSchema *pTSchema) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1001

H
Hongze Cheng 已提交
1002
  // TSDBKEY
H
Hongze Cheng 已提交
1003 1004 1005 1006
  code = tsdbRealloc((uint8_t **)&pBlockData->aVersion, sizeof(int64_t) * (pBlockData->nRow + 1));
  if (code) goto _err;
  code = tsdbRealloc((uint8_t **)&pBlockData->aTSKEY, sizeof(TSKEY) * (pBlockData->nRow + 1));
  if (code) goto _err;
H
Hongze Cheng 已提交
1007 1008
  pBlockData->aVersion[pBlockData->nRow] = TSDBROW_VERSION(pRow);
  pBlockData->aTSKEY[pBlockData->nRow] = TSDBROW_TS(pRow);
H
Hongze Cheng 已提交
1009

H
Hongze Cheng 已提交
1010 1011
  // OTHER
  int32_t   iColData = 0;
H
Hongze Cheng 已提交
1012
  SRowIter *pIter = &((SRowIter){0});
H
Hongze Cheng 已提交
1013 1014
  SColData *pColData;
  SColVal  *pColVal;
H
Hongze Cheng 已提交
1015

H
Hongze Cheng 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
  tRowIterInit(pIter, pRow, pTSchema);
  pColVal = tRowIterNext(pIter);
  pColData = (iColData < taosArrayGetSize(pBlockData->aColDataP))
                 ? (SColData *)taosArrayGetP(pBlockData->aColDataP, iColData)
                 : NULL;

  while (pColVal && pColData) {
    if (pColVal->cid == pColData->cid) {
      code = tColDataAppendValue(pColData, pColVal);
      if (code) goto _err;

      pColVal = tRowIterNext(pIter);
    } else if (pColVal->cid > pColData->cid) {
H
Hongze Cheng 已提交
1029
      code = tColDataAppendValue(pColData, &(COL_VAL_NONE(pColData->cid, pColData->type)));
H
Hongze Cheng 已提交
1030
      if (code) goto _err;
H
Hongze Cheng 已提交
1031
    } else {
H
Hongze Cheng 已提交
1032 1033 1034 1035
      code = tBlockDataAddColData(pBlockData, iColData, &pColData);
      if (code) goto _err;

      // append a NONE
H
Hongze Cheng 已提交
1036
      tColDataReset(pColData, pColVal->cid, pColVal->type, 0);
H
Hongze Cheng 已提交
1037 1038 1039
      for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
        code = tColDataAppendValue(pColData, &COL_VAL_NONE(pColVal->cid, pColVal->type));
        if (code) goto _err;
H
Hongze Cheng 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
      }

      code = tColDataAppendValue(pColData, pColVal);
      if (code) goto _err;

      pColVal = tRowIterNext(pIter);
    }

    pColData = ((++iColData) < taosArrayGetSize(pBlockData->aColDataP))
                   ? (SColData *)taosArrayGetP(pBlockData->aColDataP, iColData)
                   : NULL;
  }

  while (pColData) {
H
Hongze Cheng 已提交
1054
    code = tColDataAppendValue(pColData, &COL_VAL_NONE(pColData->cid, pColData->type));
H
Hongze Cheng 已提交
1055 1056 1057 1058 1059 1060 1061 1062
    if (code) goto _err;

    pColData = ((++iColData) < taosArrayGetSize(pBlockData->aColDataP))
                   ? (SColData *)taosArrayGetP(pBlockData->aColDataP, iColData)
                   : NULL;
  }

  while (pColVal) {
H
Hongze Cheng 已提交
1063 1064 1065
    code = tBlockDataAddColData(pBlockData, iColData, &pColData);
    if (code) goto _err;

H
Hongze Cheng 已提交
1066
    tColDataReset(pColData, pColVal->cid, pColVal->type, 0);
H
Hongze Cheng 已提交
1067 1068 1069
    for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
      code = tColDataAppendValue(pColData, &COL_VAL_NONE(pColVal->cid, pColVal->type));
      if (code) goto _err;
H
Hongze Cheng 已提交
1070
    }
H
Hongze Cheng 已提交
1071 1072 1073 1074 1075 1076

    code = tColDataAppendValue(pColData, pColVal);
    if (code) goto _err;

    iColData++;
    pColVal = tRowIterNext(pIter);
H
Hongze Cheng 已提交
1077
  }
H
Hongze Cheng 已提交
1078

H
Hongze Cheng 已提交
1079
  pBlockData->nRow++;
H
Hongze Cheng 已提交
1080
  return code;
H
Hongze Cheng 已提交
1081

H
Hongze Cheng 已提交
1082
_err:
H
Hongze Cheng 已提交
1083
  return code;
1084
}
H
Hongze Cheng 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

int32_t tBlockDataMerge(SBlockData *pBlockData1, SBlockData *pBlockData2, SBlockData *pBlockData) {
  int32_t code = 0;

  tBlockDataReset(pBlockData);

  // loop to merge
  int32_t iRow1 = 0;
  int32_t nRow1 = pBlockData1->nRow;
  int32_t iRow2 = 0;
  int32_t nRow2 = pBlockData2->nRow;
  TSDBROW row1;
  TSDBROW row2;
  int32_t c;

  while (iRow1 < nRow1 && iRow2 < nRow2) {
    row1 = tsdbRowFromBlockData(pBlockData1, iRow1);
    row2 = tsdbRowFromBlockData(pBlockData2, iRow2);

    c = tsdbKeyCmprFn(&TSDBROW_KEY(&row1), &TSDBROW_KEY(&row2));
    if (c < 0) {
      code = tBlockDataAppendRow(pBlockData, &row1, NULL);
      if (code) goto _exit;
      iRow1++;
    } else if (c > 0) {
      code = tBlockDataAppendRow(pBlockData, &row2, NULL);
      if (code) goto _exit;
      iRow2++;
    } else {
      ASSERT(0);
    }
  }

  while (iRow1 < nRow1) {
    row1 = tsdbRowFromBlockData(pBlockData1, iRow1);
    code = tBlockDataAppendRow(pBlockData, &row1, NULL);
    if (code) goto _exit;
    iRow1++;
  }

  while (iRow2 < nRow2) {
    row2 = tsdbRowFromBlockData(pBlockData2, iRow2);
    code = tBlockDataAppendRow(pBlockData, &row2, NULL);
    if (code) goto _exit;
    iRow2++;
  }

_exit:
  return code;
}

int32_t tBlockDataCopy(SBlockData *pBlockDataSrc, SBlockData *pBlockDataDest) {
  int32_t   code = 0;
  SColData *pColDataSrc;
  SColData *pColDataDest;

  ASSERT(pBlockDataSrc->nRow > 0);

  tBlockDataReset(pBlockDataDest);

  pBlockDataDest->nRow = pBlockDataSrc->nRow;
  // TSDBKEY
  code = tsdbRealloc((uint8_t **)&pBlockDataDest->aVersion, sizeof(int64_t) * pBlockDataSrc->nRow);
  if (code) goto _exit;
  code = tsdbRealloc((uint8_t **)&pBlockDataDest->aTSKEY, sizeof(TSKEY) * pBlockDataSrc->nRow);
  if (code) goto _exit;
  memcpy(pBlockDataDest->aVersion, pBlockDataSrc->aVersion, sizeof(int64_t) * pBlockDataSrc->nRow);
  memcpy(pBlockDataDest->aTSKEY, pBlockDataSrc->aTSKEY, sizeof(TSKEY) * pBlockDataSrc->nRow);

  // other
  for (size_t iColData = 0; iColData < taosArrayGetSize(pBlockDataSrc->aColDataP); iColData++) {
    pColDataSrc = (SColData *)taosArrayGetP(pBlockDataSrc->aColDataP, iColData);
    code = tBlockDataAddColData(pBlockDataDest, iColData, &pColDataDest);
    if (code) goto _exit;

    code = tColDataCopy(pColDataSrc, pColDataDest);
    if (code) goto _exit;
  }

_exit:
  return code;
1166
}
H
Hongze Cheng 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178

void tBlockDataGetColData(SBlockData *pBlockData, int16_t cid, SColData **ppColData) {
  ASSERT(cid != PRIMARYKEY_TIMESTAMP_COL_ID);

  SColData *pColData = &(SColData){.cid = cid};

  void *p = taosArraySearch(pBlockData->aColDataP, &pColData, tColDataPCmprFn, TD_EQ);
  if (p == NULL) {
    *ppColData = NULL;
  } else {
    *ppColData = *(SColData **)p;
  }
H
Hongze Cheng 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
}

// ALGORITHM ==============================
void tsdbCalcColDataSMA(SColData *pColData, SColumnDataAgg *pColAgg) {
  SColVal  colVal;
  SColVal *pColVal = &colVal;

  *pColAgg = (SColumnDataAgg){.colId = pColData->cid};
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
    tColDataGetValue(pColData, iVal, pColVal);

    if (pColVal->isNone || pColVal->isNull) {
      pColAgg->numOfNull++;
    } else {
      switch (pColData->type) {
        case TSDB_DATA_TYPE_NULL:
          break;
        case TSDB_DATA_TYPE_BOOL:
          break;
        case TSDB_DATA_TYPE_TINYINT:
          break;
        case TSDB_DATA_TYPE_SMALLINT:
          break;
        case TSDB_DATA_TYPE_INT:
          break;
        case TSDB_DATA_TYPE_BIGINT:
          break;
        case TSDB_DATA_TYPE_FLOAT:
          break;
        case TSDB_DATA_TYPE_DOUBLE:
          break;
        case TSDB_DATA_TYPE_VARCHAR:
          break;
        case TSDB_DATA_TYPE_TIMESTAMP:
          break;
        case TSDB_DATA_TYPE_NCHAR:
          break;
        case TSDB_DATA_TYPE_UTINYINT:
          break;
        case TSDB_DATA_TYPE_USMALLINT:
          break;
        case TSDB_DATA_TYPE_UINT:
          break;
        case TSDB_DATA_TYPE_UBIGINT:
          break;
        case TSDB_DATA_TYPE_JSON:
          break;
        case TSDB_DATA_TYPE_VARBINARY:
          break;
        case TSDB_DATA_TYPE_DECIMAL:
          break;
        case TSDB_DATA_TYPE_BLOB:
          break;
        case TSDB_DATA_TYPE_MEDIUMBLOB:
          break;
        default:
          ASSERT(0);
      }
    }
  }
}