tsdbUtil.c 19.8 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
more  
Hongze Cheng 已提交
18
// SOffset =======================================================================
H
Hongze Cheng 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32
#define TSDB_OFFSET_I32 ((uint8_t)0)
#define TSDB_OFFSET_I16 ((uint8_t)1)
#define TSDB_OFFSET_I8  ((uint8_t)2)

static FORCE_INLINE int32_t tsdbOffsetSize(SOffset *pOfst) {
  switch (pOfst->flag) {
    case TSDB_OFFSET_I32:
      return sizeof(int32_t);
    case TSDB_OFFSET_I16:
      return sizeof(int16_t);
    case TSDB_OFFSET_I8:
      return sizeof(int8_t);
    default:
      ASSERT(0);
H
Hongze Cheng 已提交
33 34 35
  }
}

H
Hongze Cheng 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
static FORCE_INLINE int32_t tsdbGetOffset(SOffset *pOfst, int32_t idx) {
  int32_t offset = -1;

  if (idx >= 0 && idx < pOfst->nOffset) {
    switch (pOfst->flag) {
      case TSDB_OFFSET_I32:
        offset = ((int32_t *)pOfst->pOffset)[idx];
        break;
      case TSDB_OFFSET_I16:
        offset = ((int16_t *)pOfst->pOffset)[idx];
        break;
      case TSDB_OFFSET_I8:
        offset = ((int8_t *)pOfst->pOffset)[idx];
        break;
      default:
        ASSERT(0);
    }

    ASSERT(offset >= 0);
  }

  return offset;
}

static FORCE_INLINE int32_t tsdbAddOffset(SOffset *pOfst, int32_t offset) {
  int32_t code = 0;
  int32_t nOffset = pOfst->nOffset;

  ASSERT(pOfst->flag == TSDB_OFFSET_I32);
  ASSERT(offset >= 0);

  pOfst->nOffset++;

  // alloc
  code = tsdbRealloc(&pOfst->pOffset, sizeof(int32_t) * pOfst->nOffset);
  if (code) goto _exit;

  // put
  ((int32_t *)pOfst->pOffset)[nOffset] = offset;

_exit:
  return code;
}

static FORCE_INLINE int32_t tPutOffset(uint8_t *p, SOffset *pOfst) {
  int32_t n = 0;
  int32_t maxOffset;

  ASSERT(pOfst->flag == TSDB_OFFSET_I32);
  ASSERT(pOfst->nOffset > 0);

  maxOffset = tsdbGetOffset(pOfst, pOfst->nOffset - 1);

  n += tPutI32v(p ? p + n : p, pOfst->nOffset);
  if (maxOffset <= INT8_MAX) {
    n += tPutU8(p ? p + n : p, TSDB_OFFSET_I8);
    for (int32_t iOffset = 0; iOffset < pOfst->nOffset; iOffset++) {
      n += tPutI8(p ? p + n : p, (int8_t)tsdbGetOffset(pOfst, iOffset));
    }
  } else if (maxOffset <= INT16_MAX) {
    n += tPutU8(p ? p + n : p, TSDB_OFFSET_I16);
    for (int32_t iOffset = 0; iOffset < pOfst->nOffset; iOffset++) {
      n += tPutI16(p ? p + n : p, (int16_t)tsdbGetOffset(pOfst, iOffset));
    }
H
Hongze Cheng 已提交
100
  } else {
H
Hongze Cheng 已提交
101 102 103 104
    n += tPutU8(p ? p + n : p, TSDB_OFFSET_I32);
    for (int32_t iOffset = 0; iOffset < pOfst->nOffset; iOffset++) {
      n += tPutI32(p ? p + n : p, (int32_t)tsdbGetOffset(pOfst, iOffset));
    }
H
Hongze Cheng 已提交
105
  }
H
Hongze Cheng 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

  return n;
}

static FORCE_INLINE int32_t tGetOffset(uint8_t *p, SOffset *pOfst) {
  int32_t n = 0;

  n += tGetI32v(p + n, &pOfst->nOffset);
  n += tGetU8(p + n, &pOfst->flag);
  pOfst->pOffset = p + n;
  switch (pOfst->flag) {
    case TSDB_OFFSET_I32:
      n = n + pOfst->nOffset + sizeof(int32_t);
      break;
    case TSDB_OFFSET_I16:
      n = n + pOfst->nOffset + sizeof(int16_t);
      break;
    case TSDB_OFFSET_I8:
      n = n + pOfst->nOffset + sizeof(int8_t);
      break;
    default:
      ASSERT(0);
  }

  return n;
H
Hongze Cheng 已提交
131 132
}

H
Hongze Cheng 已提交
133
// SMapData =======================================================================
H
Hongze Cheng 已提交
134 135 136 137 138
void tMapDataReset(SMapData *pMapData) {
  pMapData->flag = TSDB_OFFSET_I32;
  pMapData->nItem = 0;
  pMapData->nData = 0;
}
H
Hongze Cheng 已提交
139

H
Hongze Cheng 已提交
140
void tMapDataClear(SMapData *pMapData) {
H
Hongze Cheng 已提交
141 142
  tsdbFree(pMapData->pOfst);
  tsdbFree(pMapData->pData);
H
Hongze Cheng 已提交
143
  tsdbFree(pMapData->pBuf);
H
Hongze Cheng 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
}

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
  code = tsdbRealloc(&pMapData->pOfst, sizeof(int32_t) * pMapData->nItem);
  if (code) goto _err;
  code = tsdbRealloc(&pMapData->pData, pMapData->nData);
  if (code) goto _err;

  // put
  ((int32_t *)pMapData->pOfst)[nItem] = offset;
  tPutItemFn(pMapData->pData + offset, pItem);

_err:
  return code;
}

int32_t tMapDataGetItemByIdx(SMapData *pMapData, int32_t idx, void *pItem, int32_t (*tGetItemFn)(uint8_t *, void *)) {
  int32_t code = 0;
  int32_t offset;

  if (idx < 0 || idx >= pMapData->nItem) {
    code = TSDB_CODE_NOT_FOUND;
    goto _exit;
  }

  switch (pMapData->flag) {
    case TSDB_OFFSET_I8:
      offset = ((int8_t *)pMapData->pOfst)[idx];
      break;
    case TSDB_OFFSET_I16:
      offset = ((int16_t *)pMapData->pOfst)[idx];
      break;
    case TSDB_OFFSET_I32:
      offset = ((int32_t *)pMapData->pOfst)[idx];
      break;

    default:
      ASSERT(0);
  }

  tGetItemFn(pMapData->pData + offset, pItem);

_exit:
  return code;
}

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

  ASSERT(pMapData->flag == TSDB_OFFSET_I32);
  ASSERT(pMapData->nItem > 0);

  maxOffset = ((int32_t *)pMapData->pOfst)[pMapData->nItem - 1];

  n += tPutI32v(p ? p + n : p, pMapData->nItem);
  if (maxOffset <= INT8_MAX) {
    n += tPutU8(p ? p + n : p, TSDB_OFFSET_I8);
    for (int32_t iItem = 0; iItem < pMapData->nItem; iItem++) {
      n += tPutI8(p ? p + n : p, (int8_t)(((int32_t *)pMapData->pData)[iItem]));
    }
  } else if (maxOffset <= INT16_MAX) {
    n += tPutU8(p ? p + n : p, TSDB_OFFSET_I16);
    for (int32_t iItem = 0; iItem < pMapData->nItem; iItem++) {
      n += tPutI8(p ? p + n : p, (int16_t)(((int32_t *)pMapData->pData)[iItem]));
    }
  } else {
    n += tPutU8(p ? p + n : p, TSDB_OFFSET_I32);
    for (int32_t iItem = 0; iItem < pMapData->nItem; iItem++) {
      n += tPutI8(p ? p + n : p, (int32_t)(((int32_t *)pMapData->pData)[iItem]));
    }
  }
  n += tPutBinary(p ? p + n : p, pMapData->pData, pMapData->nData);

  return n;
}

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

  n += tGetI32v(p + n, &pMapData->nItem);
  n += tGetU8(p + n, &pMapData->flag);
  pMapData->pOfst = p + n;
  switch (pMapData->flag) {
    case TSDB_OFFSET_I8:
      n = n + sizeof(int8_t) * pMapData->nItem;
      break;
    case TSDB_OFFSET_I16:
      n = n + sizeof(int16_t) * pMapData->nItem;
      break;
    case TSDB_OFFSET_I32:
      n = n + sizeof(int32_t) * pMapData->nItem;
      break;

    default:
      ASSERT(0);
  }
  n += tGetBinary(p ? p + n : p, &pMapData->pData, &pMapData->nData);

  return n;
}

// Memory =======================================================================
H
Hongze Cheng 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
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;

  if (bsize == 0) bsize = 128;
  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 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
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;
}

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 已提交
326 327
}

H
Hongze Cheng 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
// 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;
}

H
Hongze Cheng 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
// SDelIdxItem ======================================================
static FORCE_INLINE int32_t tPutDelIdxItem(uint8_t *p, SDelIdxItem *pDelIdxItem) {
  int32_t n = 0;

  n += tPutI64(p ? p + n : p, pDelIdxItem->suid);
  n += tPutI64(p ? p + n : p, pDelIdxItem->uid);
  n += tPutI64(p ? p + n : p, pDelIdxItem->minKey);
  n += tPutI64(p ? p + n : p, pDelIdxItem->maxKey);
  n += tPutI64v(p ? p + n : p, pDelIdxItem->minVersion);
  n += tPutI64v(p ? p + n : p, pDelIdxItem->maxVersion);
  n += tPutI64v(p ? p + n : p, pDelIdxItem->offset);
  n += tPutI64v(p ? p + n : p, pDelIdxItem->size);

  return n;
}

static FORCE_INLINE int32_t tGetDelIdxItem(uint8_t *p, SDelIdxItem *pDelIdxItem) {
  int32_t n = 0;

H
Hongze Cheng 已提交
366 367 368 369 370 371 372 373
  n += tGetI64(p + n, &pDelIdxItem->suid);
  n += tGetI64(p + n, &pDelIdxItem->uid);
  n += tGetI64(p + n, &pDelIdxItem->minKey);
  n += tGetI64(p + n, &pDelIdxItem->maxKey);
  n += tGetI64v(p + n, &pDelIdxItem->minVersion);
  n += tGetI64v(p + n, &pDelIdxItem->maxVersion);
  n += tGetI64v(p + n, &pDelIdxItem->offset);
  n += tGetI64v(p + n, &pDelIdxItem->size);
H
Hongze Cheng 已提交
374 375 376 377

  return n;
}

H
Hongze Cheng 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
// SBlockIdxItem ======================================================
static FORCE_INLINE int32_t tPutBlockIdxItem(uint8_t *p, SBlockIdxItem *pItem) {
  int32_t n = 0;

  n += tPutI64(p ? p + n : p, pItem->suid);
  n += tPutI64(p ? p + n : p, pItem->uid);
  n += tPutTSDBKEY(p ? p + n : p, &pItem->minKey);
  n += tPutTSDBKEY(p ? p + n : p, &pItem->maxKey);
  n += tPutI64v(p ? p + n : p, pItem->minVersion);
  n += tPutI64v(p ? p + n : p, pItem->maxVersion);
  n += tPutI64v(p ? p + n : p, pItem->offset);
  n += tPutI64v(p ? p + n : p, pItem->size);

  return n;
}

static FORCE_INLINE int32_t tGetBlockIdxItem(uint8_t *p, SBlockIdxItem *pItem) {
  int32_t n = 0;

  n += tGetI64(p + n, &pItem->suid);
  n += tGetI64(p + n, &pItem->uid);
  n += tGetTSDBKEY(p + n, &pItem->minKey);
  n += tGetTSDBKEY(p + n, &pItem->maxKey);
  n += tGetI64v(p + n, &pItem->minVersion);
  n += tGetI64v(p + n, &pItem->maxVersion);
  n += tGetI64v(p + n, &pItem->offset);
  n += tGetI64v(p + n, &pItem->size);

  return n;
}

// SBlockIdx ======================================================
int32_t tBlockIdxClear(SBlockIdx *pBlockIdx) {
  int32_t code = 0;
  tsdbFree(pBlockIdx->offset.pOffset);
  tsdbFree(pBlockIdx->pData);
  return code;
}

int32_t tBlockIdxPutItem(SBlockIdx *pBlockIdx, SBlockIdxItem *pItem) {
  int32_t code = 0;
  // TODO
  return code;
}

int32_t tBlockIdxGetItemByIdx(SBlockIdx *pBlockIdx, SBlockIdxItem *pItem, int32_t idx) {
  int32_t code = 0;
  int32_t offset;

  offset = tsdbGetOffset(&pBlockIdx->offset, idx);
  if (offset < 0) {
    code = TSDB_CODE_NOT_FOUND;
    goto _exit;
  }

  tGetBlockIdxItem(pBlockIdx->pData + offset, pItem);

_exit:
  return code;
}

int32_t tBlockIdxGetItem(SBlockIdx *pBlockIdx, SBlockIdxItem *pItem, TABLEID id) {
  int32_t code = 0;
  int32_t lidx = 0;
  int32_t ridx = pBlockIdx->offset.nOffset - 1;
  int32_t midx;
  int32_t c;

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

    code = tBlockIdxGetItemByIdx(pBlockIdx, pItem, midx);
    if (code) goto _exit;

    c = tTABLEIDCmprFn(&id, 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;
}

int32_t tPutBlockIdx(uint8_t *p, SBlockIdx *pBlockIdx) {
  int32_t n = 0;

  n += tPutU32(p ? p + n : p, pBlockIdx->delimiter);
  n += tPutOffset(p ? p + n : p, &pBlockIdx->offset);
  n += tPutBinary(p ? p + n : p, pBlockIdx->pData, pBlockIdx->nData);

  return n;
}

int32_t tGetBlockIdx(uint8_t *p, SBlockIdx *pBlockIdx) {
  int32_t n = 0;

  n += tGetU32(p + n, &pBlockIdx->delimiter);
  n += tGetOffset(p + n, &pBlockIdx->offset);
  n += tGetBinary(p + n, &pBlockIdx->pData, &pBlockIdx->nData);

  return n;
}

H
Hongze Cheng 已提交
488 489 490 491 492 493 494 495 496
// SBlock ======================================================
int32_t tBlockCmprFn(const void *p1, const void *p2) {
  int32_t c;
  SBlock *pBlock1 = (SBlock *)p1;
  SBlock *pBlock2 = (SBlock *)p2;

  if (tsdbKeyCmprFn(&pBlock1->maxKey, &pBlock2->minKey) < 0) {
    return -1;
  } else if (tsdbKeyCmprFn(&pBlock1->minKey, &pBlock2->maxKey) > 0) {
H
Hongze Cheng 已提交
497
    return 1;
H
Hongze Cheng 已提交
498 499 500 501 502
  }

  return 0;
}

H
Hongze Cheng 已提交
503
// SDelIdx ======================================================
H
more  
Hongze Cheng 已提交
504 505 506 507 508 509 510
int32_t tDelIdxClear(SDelIdx *pDelIdx) {
  int32_t code = 0;
  tdbFree(pDelIdx->offset.pOffset);
  tdbFree(pDelIdx->pData);
  return code;
}

H
Hongze Cheng 已提交
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
int32_t tDelIdxPutItem(SDelIdx *pDelIdx, SDelIdxItem *pItem) {
  int32_t  code = 0;
  uint32_t offset = pDelIdx->nData;

  // offset
  code = tsdbAddOffset(&pDelIdx->offset, offset);
  if (code) goto _exit;

  // alloc
  pDelIdx->nData += tPutDelIdxItem(NULL, pItem);
  code = tsdbRealloc(&pDelIdx->pData, pDelIdx->nData);
  if (code) goto _exit;

  // put
  tPutDelIdxItem(pDelIdx->pData + offset, pItem);

_exit:
  return code;
}

int32_t tDelIdxGetItemByIdx(SDelIdx *pDelIdx, SDelIdxItem *pItem, int32_t idx) {
  int32_t code = 0;
  int32_t offset;

  offset = tsdbGetOffset(&pDelIdx->offset, idx);
  if (offset < 0) {
    code = TSDB_CODE_NOT_FOUND;
    goto _exit;
  }

  tGetDelIdxItem(pDelIdx->pData + offset, pItem);

_exit:
  return code;
}

H
Hongze Cheng 已提交
547
int32_t tDelIdxGetItem(SDelIdx *pDelIdx, SDelIdxItem *pItem, TABLEID id) {
H
Hongze Cheng 已提交
548 549 550 551 552
  int32_t code = 0;
  int32_t lidx = 0;
  int32_t ridx = pDelIdx->offset.nOffset - 1;
  int32_t midx;
  int32_t c;
H
Hongze Cheng 已提交
553 554 555 556

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

H
Hongze Cheng 已提交
557 558 559
    code = tDelIdxGetItemByIdx(pDelIdx, pItem, midx);
    if (code) goto _exit;

H
Hongze Cheng 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
    c = tTABLEIDCmprFn(&id, 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 已提交
576
int32_t tPutDelIdx(uint8_t *p, SDelIdx *pDelIdx) {
H
Hongze Cheng 已提交
577 578
  int32_t n = 0;

H
Hongze Cheng 已提交
579
  n += tPutU32(p ? p + n : p, pDelIdx->delimiter);
H
Hongze Cheng 已提交
580
  n += tPutOffset(p ? p + n : p, &pDelIdx->offset);
H
Hongze Cheng 已提交
581
  n += tPutBinary(p ? p + n : p, pDelIdx->pData, pDelIdx->nData);
H
Hongze Cheng 已提交
582 583 584 585

  return n;
}

H
Hongze Cheng 已提交
586
int32_t tGetDelIdx(uint8_t *p, SDelIdx *pDelIdx) {
H
Hongze Cheng 已提交
587 588
  int32_t n = 0;

H
Hongze Cheng 已提交
589 590 591
  n += tGetU32(p + n, &pDelIdx->delimiter);
  n += tGetOffset(p + n, &pDelIdx->offset);
  n += tGetBinary(p + n, &pDelIdx->pData, &pDelIdx->nData);
H
Hongze Cheng 已提交
592 593 594 595

  return n;
}

H
Hongze Cheng 已提交
596 597 598 599 600 601 602 603 604 605 606 607 608 609
// SDelDataItem ======================================================
static FORCE_INLINE int32_t tPutDelDataItem(uint8_t *p, SDelDataItem *pItem) {
  int32_t n = 0;

  n += tPutI64v(p ? p + n : p, pItem->version);
  n += tPutI64(p ? p + n : p, pItem->sKey);
  n += tPutI64(p ? p + n : p, pItem->eKey);

  return n;
}

static FORCE_INLINE int32_t tGetDelDataItem(uint8_t *p, SDelDataItem *pItem) {
  int32_t n = 0;

H
Hongze Cheng 已提交
610 611 612
  n += tGetI64v(p + n, &pItem->version);
  n += tGetI64(p + n, &pItem->sKey);
  n += tGetI64(p + n, &pItem->eKey);
H
Hongze Cheng 已提交
613 614 615 616

  return n;
}

H
Hongze Cheng 已提交
617
// SDelData ======================================================
H
Hongze Cheng 已提交
618 619 620 621 622 623 624
int32_t tDelDataClear(SDelData *pDelData) {
  int32_t code = 0;
  tsdbFree(pDelData->offset.pOffset);
  tsdbFree(pDelData->pData);
  return code;
}

H
Hongze Cheng 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
int32_t tDelDataPutItem(SDelData *pDelData, SDelDataItem *pItem) {
  int32_t  code = 0;
  uint32_t offset = pDelData->nData;

  // offset
  code = tsdbAddOffset(&pDelData->offset, offset);
  if (code) goto _exit;

  // alloc
  pDelData->nData += tPutDelDataItem(NULL, pItem);
  code = tsdbRealloc(&pDelData->pData, pDelData->nData);
  if (code) goto _exit;

  // put
  tPutDelDataItem(pDelData->pData + offset, pItem);

_exit:
  return code;
}

int32_t tDelDataGetItemByIdx(SDelData *pDelData, SDelDataItem *pItem, int32_t idx) {
  int32_t code = 0;
  int32_t offset;

  offset = tsdbGetOffset(&pDelData->offset, idx);
  if (offset < 0) {
    code = TSDB_CODE_NOT_FOUND;
    goto _exit;
  }
  tGetDelDataItem(pDelData->pData + offset, pItem);

_exit:
  return code;
}

H
Hongze Cheng 已提交
660
int32_t tDelDataGetItem(SDelData *pDelData, SDelDataItem *pItem, int64_t version) {
H
Hongze Cheng 已提交
661
  int32_t code = 0;
H
Hongze Cheng 已提交
662
  int32_t lidx = 0;
H
Hongze Cheng 已提交
663
  int32_t ridx = pDelData->offset.nOffset - 1;
H
Hongze Cheng 已提交
664 665 666 667 668
  int32_t midx;

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

H
Hongze Cheng 已提交
669 670 671
    code = tDelDataGetItemByIdx(pDelData, pItem, midx);
    if (code) goto _exit;

H
Hongze Cheng 已提交
672 673 674 675 676 677 678 679 680 681 682 683
    if (version == pItem->version) {
      goto _exit;
    } else if (version < pItem->version) {
      ridx = midx - 1;
    } else {
      ridx = midx + 1;
    }
  }

  code = TSDB_CODE_NOT_FOUND;

_exit:
H
Hongze Cheng 已提交
684 685 686
  return code;
}

H
Hongze Cheng 已提交
687 688 689 690
int32_t tPutDelData(uint8_t *p, SDelData *pDelData) {
  int32_t n = 0;

  n += tPutU32(p ? p + n : p, pDelData->delimiter);
H
Hongze Cheng 已提交
691 692
  n += tPutI64(p ? p + n : p, pDelData->suid);
  n += tPutI64(p ? p + n : p, pDelData->uid);
H
Hongze Cheng 已提交
693
  n += tPutOffset(p ? p + n : p, &pDelData->offset);
H
Hongze Cheng 已提交
694 695 696 697 698 699 700 701
  n += tPutBinary(p ? p + n : p, pDelData->pData, pDelData->nData);

  return n;
}

int32_t tGetDelData(uint8_t *p, SDelData *pDelData) {
  int32_t n = 0;

H
Hongze Cheng 已提交
702 703 704 705 706
  n += tGetU32(p + n, &pDelData->delimiter);
  n += tGetI64(p + n, &pDelData->suid);
  n += tGetI64(p + n, &pDelData->uid);
  n += tGetOffset(p + n, &pDelData->offset);
  n += tGetBinary(p + n, &pDelData->pData, &pDelData->nData);
H
Hongze Cheng 已提交
707 708

  return n;
H
Hongze Cheng 已提交
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
}

int32_t tPutDelFileHdr(uint8_t *p, SDelFile *pDelFile) {
  int32_t n = 0;

  n += tPutI64(p ? p + n : p, pDelFile->minKey);
  n += tPutI64(p ? p + n : p, pDelFile->maxKey);
  n += tPutI64v(p ? p + n : p, pDelFile->minVersion);
  n += tPutI64v(p ? p + n : p, pDelFile->maxVersion);
  n += tPutI64v(p ? p + n : p, pDelFile->size);
  n += tPutI64v(p ? p + n : p, pDelFile->offset);

  return n;
}

int32_t tGetDelFileHdr(uint8_t *p, SDelFile *pDelFile) {
  int32_t n = 0;

H
Hongze Cheng 已提交
727 728 729 730 731 732
  n += tGetI64(p + n, &pDelFile->minKey);
  n += tGetI64(p + n, &pDelFile->maxKey);
  n += tGetI64v(p + n, &pDelFile->minVersion);
  n += tGetI64v(p + n, &pDelFile->maxVersion);
  n += tGetI64v(p + n, &pDelFile->size);
  n += tGetI64v(p + n, &pDelFile->offset);
H
Hongze Cheng 已提交
733 734 735

  return n;
}
H
Hongze Cheng 已提交
736 737 738 739 740 741 742

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 已提交
743 744
}

H
Hongze Cheng 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
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 已提交
762 763 764 765 766 767
// TSDBROW ======================================================
void tsdbRowGetColVal(TSDBROW *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal) {
  // TODO
}

// SColDataBlock ======================================================
H
Hongze Cheng 已提交
768 769 770 771
void tsdbColDataBlockReset(SColDataBlock *pColDataBlock) {
  // TODO
}

H
Hongze Cheng 已提交
772 773 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
int32_t tsdbColDataBlockAppend(SColDataBlock *pColDataBlock, TSDBROW *pRow, STSchema *pTSchema) {
  int32_t   code = 0;
  int32_t   nRow = pColDataBlock->nRow;
  STColumn *pTColumn;
  SColData *pColData;
  SColVal   colVal;

  pColDataBlock->nRow++;

  // version
  pColDataBlock->aVersion[nRow] = pRow->version;  // TODO

  // ts
  pColDataBlock->aTSKey[nRow] = pRow->pTSRow->ts;  // TODO

  // other rows
  for (int32_t iCol = 1; iCol < pTSchema->numOfCols; iCol++) {
    pTColumn = &pTSchema->columns[iCol];

    tsdbRowGetColVal(pRow, pTSchema, iCol, &colVal);

    if (colVal.isNone) {
      // TODO
    } else if (colVal.isNull) {
      // TODO
    } else {
      pColData->nData += tPutValue(pColData->pData + pColData->nData, &colVal.value, pTColumn->type);
    }
  }

  return code;
}