tsdbUtil.c 9.1 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 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
static FORCE_INLINE int32_t tsdbOffsetSize(uint8_t flags) {
  if (flags & TSDB_OFFSET_U8) {
    return sizeof(uint8_t);
  } else if (flags & TSDB_OFFSET_U16) {
    return sizeof(uint16_t);
  } else if (flags & TSDB_OFFSET_U32) {
    return sizeof(uint32_t);
  } else {
    ASSERT(0);
  }
}

static FORCE_INLINE uint32_t tsdbGetOffset(uint8_t *pOffset, uint8_t flags, int32_t idx) {
  if (flags & TSDB_OFFSET_U8) {
    return ((uint8_t *)pOffset)[idx];
  } else if (flags & TSDB_OFFSET_U16) {
    return ((uint16_t *)pOffset)[idx];
  } else if (flags & TSDB_OFFSET_U32) {
    return ((uint32_t *)pOffset)[idx];
  } else {
    ASSERT(0);
  }
}

H
Hongze Cheng 已提交
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
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 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
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 已提交
113 114
}

H
Hongze Cheng 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
// 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;

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

  return n;
}

H
Hongze Cheng 已提交
146
// SDelIdx ======================================================
H
Hongze Cheng 已提交
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
int32_t tDelIdxGetItem(SDelIdx *pDelIdx, SDelIdxItem *pItem, TABLEID id) {
  int32_t  code = 0;
  int32_t  lidx = 0;
  int32_t  ridx = pDelIdx->nItem - 1;
  int32_t  midx;
  uint64_t offset;
  int32_t  c;

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

    tDelIdxGetItemByIdx(pDelIdx, pItem, midx);
    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 tDelIdxGetItemByIdx(SDelIdx *pDelIdx, SDelIdxItem *pItem, int32_t idx) {
  tGetDelIdxItem(pDelIdx->pData + tsdbGetOffset(pDelIdx->pOffset, pDelIdx->flags, idx), pItem);
H
Hongze Cheng 已提交
177
  return 0;
H
Hongze Cheng 已提交
178 179
}

H
Hongze Cheng 已提交
180
int32_t tDelIdxPutItem(SDelIdx *pDelIdx, SDelIdxItem *pItem) {
H
Hongze Cheng 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
  int32_t  code = 0;
  int32_t  size = tPutDelIdxItem(NULL, pItem);
  uint32_t offset = pDelIdx->nData;
  uint32_t nItem = pDelIdx->nItem;

  pDelIdx->nItem++;
  pDelIdx->nData += size;

  // alloc
  code = tsdbRealloc(&pDelIdx->pOffset, pDelIdx->nItem * sizeof(uint32_t));
  if (code) goto _exit;
  code = tsdbRealloc(&pDelIdx->pData, pDelIdx->nData);
  if (code) goto _exit;

  // put
  ((uint32_t *)pDelIdx->pOffset)[nItem] = offset;
  tPutDelIdxItem(pDelIdx->pData + offset, pItem);

_exit:
H
Hongze Cheng 已提交
200 201 202
  return code;
}

H
Hongze Cheng 已提交
203
int32_t tPutDelIdx(uint8_t *p, SDelIdx *pDelIdx) {
H
Hongze Cheng 已提交
204 205
  int32_t n = 0;

H
Hongze Cheng 已提交
206 207
  n += tPutU32(p ? p + n : p, pDelIdx->delimiter);
  n += tPutU8(p ? p + n : p, pDelIdx->flags);
H
Hongze Cheng 已提交
208 209
  n += tPutU32v(p ? p + n : p, pDelIdx->nItem);
  n += tPutBinary(p ? p + n : p, pDelIdx->pOffset, pDelIdx->nItem * tsdbOffsetSize(pDelIdx->flags));
H
Hongze Cheng 已提交
210
  n += tPutBinary(p ? p + n : p, pDelIdx->pData, pDelIdx->nData);
H
Hongze Cheng 已提交
211 212 213 214

  return n;
}

H
Hongze Cheng 已提交
215
int32_t tGetDelIdx(uint8_t *p, SDelIdx *pDelIdx) {
H
Hongze Cheng 已提交
216 217
  int32_t n = 0;

H
Hongze Cheng 已提交
218 219
  n += tGetU32(p, &pDelIdx->delimiter);
  n += tGetU8(p, &pDelIdx->flags);
H
Hongze Cheng 已提交
220 221
  n += tGetU32v(p, &pDelIdx->nItem);
  n += tGetBinary(p, &pDelIdx->pOffset, NULL);
H
Hongze Cheng 已提交
222 223 224 225 226
  n += tGetBinary(p, &pDelIdx->pData, &pDelIdx->nData);

  return n;
}

H
Hongze Cheng 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
// 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;

  n += tGetI64v(p, &pItem->version);
  n += tGetI64(p, &pItem->sKey);
  n += tGetI64(p, &pItem->eKey);

  return n;
}

H
Hongze Cheng 已提交
248
// SDelData ======================================================
H
Hongze Cheng 已提交
249
int32_t tDelDataGetItem(SDelData *pDelData, SDelDataItem *pItem, int64_t version) {
H
Hongze Cheng 已提交
250
  int32_t code = 0;
H
Hongze Cheng 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
  int32_t lidx = 0;
  int32_t ridx = pDelData->nItem - 1;
  int32_t midx;

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

    tDelDataGetItemByIdx(pDelData, pItem, midx);
    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 已提交
271 272 273
  return code;
}

H
Hongze Cheng 已提交
274 275 276
int32_t tDelDataGetItemByIdx(SDelData *pDelData, SDelDataItem *pItem, int32_t idx) {
  tGetDelDataItem(pDelData->pData + tsdbGetOffset(pDelData->pOffset, pDelData->flags, idx), pItem);
  return 0;
H
Hongze Cheng 已提交
277 278 279
}

int32_t tDelDataPutItem(SDelData *pDelData, SDelDataItem *pItem) {
H
Hongze Cheng 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
  int32_t  code = 0;
  uint32_t nItem = pDelData->nItem;
  uint32_t offset = pDelData->nData;

  pDelData->nItem++;
  pDelData->nData += tPutDelDataItem(NULL, pItem);

  // alloc
  code = tsdbRealloc(&pDelData->pOffset, pDelData->nItem * sizeof(uint32_t));
  if (code) goto _exit;
  code = tsdbRealloc(&pDelData->pData, pDelData->nData);
  if (code) goto _exit;

  // put
  ((uint32_t *)pDelData->pOffset)[nItem] = offset;
  tPutDelDataItem(pDelData->pData + offset, pItem);

_exit:
H
Hongze Cheng 已提交
298 299 300
  return code;
}

H
Hongze Cheng 已提交
301 302 303 304
int32_t tPutDelData(uint8_t *p, SDelData *pDelData) {
  int32_t n = 0;

  n += tPutU32(p ? p + n : p, pDelData->delimiter);
H
Hongze Cheng 已提交
305 306
  n += tPutI64(p ? p + n : p, pDelData->suid);
  n += tPutI64(p ? p + n : p, pDelData->uid);
H
Hongze Cheng 已提交
307
  n += tPutU8(p ? p + n : p, pDelData->flags);
H
Hongze Cheng 已提交
308 309
  n += tPutU32v(p ? p + n : p, pDelData->nItem);
  n += tPutBinary(p ? p + n : p, pDelData->pOffset, pDelData->nItem * tsdbOffsetSize(pDelData->flags));
H
Hongze Cheng 已提交
310 311 312 313 314 315 316 317 318
  n += tPutBinary(p ? p + n : p, pDelData->pData, pDelData->nData);

  return n;
}

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

  n += tGetU32(p, &pDelData->delimiter);
H
Hongze Cheng 已提交
319 320
  n += tGetI64(p, &pDelData->suid);
  n += tGetI64(p, &pDelData->uid);
H
Hongze Cheng 已提交
321
  n += tGetU8(p, &pDelData->flags);
H
Hongze Cheng 已提交
322 323
  n += tGetU32v(p, &pDelData->nItem);
  n += tGetBinary(p, &pDelData->pOffset, NULL);
H
Hongze Cheng 已提交
324
  n += tGetBinary(p, &pDelData->pData, &pDelData->nData);
H
Hongze Cheng 已提交
325 326

  return n;
H
Hongze Cheng 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
}

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;

  n += tGetI64(p, &pDelFile->minKey);
  n += tGetI64(p, &pDelFile->maxKey);
  n += tGetI64v(p, &pDelFile->minVersion);
  n += tGetI64v(p, &pDelFile->maxVersion);
  n += tGetI64v(p, &pDelFile->size);
  n += tGetI64v(p, &pDelFile->offset);

  return n;
}