tcompression.c 78.6 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

/* README.md   TAOS compression
 *
 * INTEGER Compression Algorithm:
S
Shengliang Guan 已提交
19
 *   To compress integers (including char, short, int32_t, int64_t), the difference
H
hzcheng 已提交
20 21
 *   between two integers is calculated at first. Then the difference is
 *   transformed to positive by zig-zag encoding method
22
 *   (https://gist.github.com/mfuerstenau/ba870a29e16536fdbaba). Then the value is
H
hzcheng 已提交
23 24 25
 *   encoded using simple 8B method. For more information about simple 8B,
 *   refer to https://en.wikipedia.org/wiki/8b/10b_encoding.
 *
26
 *   NOTE : For bigint, only 59 bits can be used, which means data from -(2**59) to (2**59)-1
H
hzcheng 已提交
27 28 29
 *   are allowed.
 *
 * BOOLEAN Compression Algorithm:
30
 *   We provide two methods for compress boolean types. Because boolean types in C
31
 *   code are char bytes with 0 and 1 values only, only one bit can used to discriminate
H
hzcheng 已提交
32
 *   the values.
33
 *   1. The first method is using only 1 bit to represent the boolean value with 1 for
H
hzcheng 已提交
34
 *   true and 0 for false. Then the compression rate is 1/8.
35
 *   2. The second method is using run length encoding (RLE) methods. This method works
H
hzcheng 已提交
36 37 38 39 40 41
 *   better when there are a lot of consecutive true values or false values.
 *
 * STRING Compression Algorithm:
 *   We us LZ4 method to compress the string type.
 *
 * FLOAT Compression Algorithm:
42 43 44 45 46
 *   We use the same method with Akumuli to compress float and double types. The compression
 *   algorithm assumes the float/double values change slightly. So we take the XOR between two
 *   adjacent values. Then compare the number of leading zeros and trailing zeros. If the number
 *   of leading zeros are larger than the trailing zeros, then record the last serveral bytes
 *   of the XORed value with informations. If not, record the first corresponding bytes.
H
hzcheng 已提交
47 48 49
 *
 */

S
Shengliang Guan 已提交
50
#define _DEFAULT_SOURCE
H
Haojun Liao 已提交
51
#include "tcompression.h"
S
Shengliang Guan 已提交
52
#include "lz4.h"
H
Hongze Cheng 已提交
53
#include "tRealloc.h"
S
log  
Shengliang Guan 已提交
54
#include "tlog.h"
H
hzcheng 已提交
55

S
Shengliang Guan 已提交
56 57 58 59 60 61
#ifdef TD_TSZ
#include "td_sz.h"
#endif

static const int32_t TEST_NUMBER = 1;
#define is_bigendian()     ((*(char *)&TEST_NUMBER) == 0)
H
Hongze Cheng 已提交
62
#define SIMPLE8B_MAX_INT64 ((uint64_t)1152921504606846974LL)
H
hzcheng 已提交
63

S
Shengliang Guan 已提交
64
#define safeInt64Add(a, b)  (((a >= 0) && (b <= INT64_MAX - a)) || ((a < 0) && (b >= INT64_MIN - a)))
H
Hongze Cheng 已提交
65 66
#define ZIGZAG_ENCODE(T, v) (((u##T)((v) >> (sizeof(T) * 8 - 1))) ^ (((u##T)(v)) << 1))  // zigzag encode
#define ZIGZAG_DECODE(T, v) (((v) >> 1) ^ -((T)((v)&1)))                                 // zigzag decode
H
hzcheng 已提交
67

T
tickduan 已提交
68
#ifdef TD_TSZ
S
Shengliang Guan 已提交
69
bool lossyFloat = false;
T
tickduan 已提交
70 71 72
bool lossyDouble = false;

// init call
S
Shengliang Guan 已提交
73 74 75
int32_t tsCompressInit() {
  // config
  if (lossyColumns[0] == 0) {
T
tickduan 已提交
76 77 78 79 80
    lossyFloat = false;
    lossyDouble = false;
    return 0;
  }

S
Shengliang Guan 已提交
81
  lossyFloat = strstr(lossyColumns, "float") != NULL;
T
tickduan 已提交
82 83
  lossyDouble = strstr(lossyColumns, "double") != NULL;

S
Shengliang Guan 已提交
84 85
  if (lossyFloat == false && lossyDouble == false) return 0;

T
tickduan 已提交
86
  tdszInit(fPrecision, dPrecision, maxRange, curRange, Compressor);
87 88
  if (lossyFloat) uTrace("lossy compression float  is opened. ");
  if (lossyDouble) uTrace("lossy compression double is opened. ");
T
tickduan 已提交
89 90 91
  return 1;
}
// exit call
S
Shengliang Guan 已提交
92
void tsCompressExit() { tdszExit(); }
T
tickduan 已提交
93

94
#endif
T
tickduan 已提交
95

H
hzcheng 已提交
96 97 98
/*
 * Compress Integer (Simple8B).
 */
S
Shengliang Guan 已提交
99
int32_t tsCompressINTImp(const char *const input, const int32_t nelements, char *const output, const char type) {
H
hzcheng 已提交
100 101
  // Selector value:              0    1   2   3   4   5   6   7   8  9  10  11
  // 12  13  14  15
S
Shengliang Guan 已提交
102 103 104
  char    bit_per_integer[] = {0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 15, 20, 30, 60};
  int32_t selector_to_elems[] = {240, 120, 60, 30, 20, 15, 12, 10, 8, 7, 6, 5, 4, 3, 2, 1};
  char    bit_to_selector[] = {0,  2,  3,  4,  5,  6,  7,  8,  9,  10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 13, 13,
H
Hongze Cheng 已提交
105 106
                               14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
                               15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15};
H
hzcheng 已提交
107 108

  // get the byte limit.
S
Shengliang Guan 已提交
109
  int32_t word_length = 0;
H
hzcheng 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123
  switch (type) {
    case TSDB_DATA_TYPE_BIGINT:
      word_length = LONG_BYTES;
      break;
    case TSDB_DATA_TYPE_INT:
      word_length = INT_BYTES;
      break;
    case TSDB_DATA_TYPE_SMALLINT:
      word_length = SHORT_BYTES;
      break;
    case TSDB_DATA_TYPE_TINYINT:
      word_length = CHAR_BYTES;
      break;
    default:
124
      uError("Invalid compress integer type:%d", type);
H
Hongze Cheng 已提交
125
      return -1;
H
hzcheng 已提交
126 127
  }

S
Shengliang Guan 已提交
128 129
  int32_t byte_limit = nelements * word_length + 1;
  int32_t opos = 1;
H
hzcheng 已提交
130 131
  int64_t prev_value = 0;

S
Shengliang Guan 已提交
132
  for (int32_t i = 0; i < nelements;) {
H
hzcheng 已提交
133 134
    char    selector = 0;
    char    bit = 0;
S
Shengliang Guan 已提交
135
    int32_t elems = 0;
H
hzcheng 已提交
136 137
    int64_t prev_value_tmp = prev_value;

S
Shengliang Guan 已提交
138
    for (int32_t j = i; j < nelements; j++) {
H
hzcheng 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
      // Read data from the input stream and convert it to INT64 type.
      int64_t curr_value = 0;
      switch (type) {
        case TSDB_DATA_TYPE_TINYINT:
          curr_value = (int64_t)(*((int8_t *)input + j));
          break;
        case TSDB_DATA_TYPE_SMALLINT:
          curr_value = (int64_t)(*((int16_t *)input + j));
          break;
        case TSDB_DATA_TYPE_INT:
          curr_value = (int64_t)(*((int32_t *)input + j));
          break;
        case TSDB_DATA_TYPE_BIGINT:
          curr_value = (int64_t)(*((int64_t *)input + j));
          break;
      }
      // Get difference.
Y
yihaoDeng 已提交
156
      if (!safeInt64Add(curr_value, -prev_value_tmp)) goto _copy_and_exit;
H
hzcheng 已提交
157 158 159

      int64_t diff = curr_value - prev_value_tmp;
      // Zigzag encode the value.
H
Hongze Cheng 已提交
160
      uint64_t zigzag_value = ZIGZAG_ENCODE(int64_t, diff);
H
hzcheng 已提交
161 162 163

      if (zigzag_value >= SIMPLE8B_MAX_INT64) goto _copy_and_exit;

S
TD-1037  
Shengliang Guan 已提交
164
      int64_t tmp_bit;
H
hzcheng 已提交
165 166 167 168
      if (zigzag_value == 0) {
        // Take care here, __builtin_clzl give wrong anser for value 0;
        tmp_bit = 0;
      } else {
169
        tmp_bit = (LONG_BYTES * BITS_PER_BYTE) - BUILDIN_CLZL(zigzag_value);
H
hzcheng 已提交
170 171
      }

S
Shengliang Guan 已提交
172 173
      if (elems + 1 <= selector_to_elems[(int32_t)selector] &&
          elems + 1 <= selector_to_elems[(int32_t)(bit_to_selector[(int32_t)tmp_bit])]) {
H
hzcheng 已提交
174
        // If can hold another one.
S
Shengliang Guan 已提交
175
        selector = selector > bit_to_selector[(int32_t)tmp_bit] ? selector : bit_to_selector[(int32_t)tmp_bit];
H
hzcheng 已提交
176
        elems++;
S
Shengliang Guan 已提交
177
        bit = bit_per_integer[(int32_t)selector];
H
hzcheng 已提交
178 179
      } else {
        // if cannot hold another one.
S
Shengliang Guan 已提交
180 181 182
        while (elems < selector_to_elems[(int32_t)selector]) selector++;
        elems = selector_to_elems[(int32_t)selector];
        bit = bit_per_integer[(int32_t)selector];
H
hzcheng 已提交
183 184 185 186 187 188 189
        break;
      }
      prev_value_tmp = curr_value;
    }

    uint64_t buffer = 0;
    buffer |= (uint64_t)selector;
S
Shengliang Guan 已提交
190
    for (int32_t k = 0; k < elems; k++) {
H
hzcheng 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
      int64_t curr_value = 0; /* get current values */
      switch (type) {
        case TSDB_DATA_TYPE_TINYINT:
          curr_value = (int64_t)(*((int8_t *)input + i));
          break;
        case TSDB_DATA_TYPE_SMALLINT:
          curr_value = (int64_t)(*((int16_t *)input + i));
          break;
        case TSDB_DATA_TYPE_INT:
          curr_value = (int64_t)(*((int32_t *)input + i));
          break;
        case TSDB_DATA_TYPE_BIGINT:
          curr_value = (int64_t)(*((int64_t *)input + i));
          break;
      }
      int64_t  diff = curr_value - prev_value;
H
Hongze Cheng 已提交
207
      uint64_t zigzag_value = ZIGZAG_ENCODE(int64_t, diff);
H
hzcheng 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
      buffer |= ((zigzag_value & INT64MASK(bit)) << (bit * k + 4));
      i++;
      prev_value = curr_value;
    }

    // Output the encoded value to the output.
    if (opos + sizeof(buffer) <= byte_limit) {
      memcpy(output + opos, &buffer, sizeof(buffer));
      opos += sizeof(buffer);
    } else {
    _copy_and_exit:
      output[0] = 1;
      memcpy(output + 1, input, byte_limit - 1);
      return byte_limit;
    }
  }

  // set the indicator.
  output[0] = 0;
  return opos;
}

S
Shengliang Guan 已提交
230
int32_t tsDecompressINTImp(const char *const input, const int32_t nelements, char *const output, const char type) {
231
#if 1
S
Shengliang Guan 已提交
232
  int32_t word_length = 0;
H
hzcheng 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246
  switch (type) {
    case TSDB_DATA_TYPE_BIGINT:
      word_length = LONG_BYTES;
      break;
    case TSDB_DATA_TYPE_INT:
      word_length = INT_BYTES;
      break;
    case TSDB_DATA_TYPE_SMALLINT:
      word_length = SHORT_BYTES;
      break;
    case TSDB_DATA_TYPE_TINYINT:
      word_length = CHAR_BYTES;
      break;
    default:
247
      uError("Invalid decompress integer type:%d", type);
H
Hongze Cheng 已提交
248
      return -1;
H
hzcheng 已提交
249 250 251 252 253 254 255 256 257 258
  }

  // If not compressed.
  if (input[0] == 1) {
    memcpy(output, input + 1, nelements * word_length);
    return nelements * word_length;
  }

  // Selector value:              0    1   2   3   4   5   6   7   8  9  10  11
  // 12  13  14  15
S
Shengliang Guan 已提交
259 260
  char    bit_per_integer[] = {0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 15, 20, 30, 60};
  int32_t selector_to_elems[] = {240, 120, 60, 30, 20, 15, 12, 10, 8, 7, 6, 5, 4, 3, 2, 1};
H
hzcheng 已提交
261 262

  const char *ip = input + 1;
S
Shengliang Guan 已提交
263 264
  int32_t     count = 0;
  int32_t     _pos = 0;
H
hzcheng 已提交
265 266 267
  int64_t     prev_value = 0;

  while (1) {
268
    if (_pos == nelements) break;
H
hzcheng 已提交
269 270 271 272

    uint64_t w = 0;
    memcpy(&w, ip, LONG_BYTES);

S
Shengliang Guan 已提交
273 274 275
    char    selector = (char)(w & INT64MASK(4));       // selector = 4
    char    bit = bit_per_integer[(int32_t)selector];  // bit = 3
    int32_t elems = selector_to_elems[(int32_t)selector];
H
hzcheng 已提交
276

H
Haojun Liao 已提交
277
    // Optimize the performance, by remove the constantly switch operation.
278
    int32_t  v = 4;
279 280
    uint64_t zigzag_value = 0;
    uint64_t mask = INT64MASK(bit);
H
Haojun Liao 已提交
281 282 283

    switch (type) {
      case TSDB_DATA_TYPE_BIGINT: {
284 285
        int64_t* p = (int64_t*) output;

286
        if (selector == 0 || selector == 1) {
287 288
          int32_t gRemainder = nelements - _pos;
          int32_t num = gRemainder < elems? gRemainder:elems;
289

290 291 292
          int32_t batch = num >> 2;
          int32_t remainder = num & 0x03;
          for (int32_t i = 0; i < batch; ++i) {
293 294 295 296 297 298
            p[_pos++] = prev_value;
            p[_pos++] = prev_value;
            p[_pos++] = prev_value;
            p[_pos++] = prev_value;
          }

299
          for (int32_t i = 0; i < remainder; ++i) {
300
            p[_pos++] = prev_value;
H
Haojun Liao 已提交
301
          }
302

303
          count += num;
304
        } else {
305 306
          int32_t gRemainder = (nelements - _pos);
          int32_t num = (gRemainder > elems)? elems:gRemainder;
307 308 309

          int32_t batch = num >> 2;
          int32_t remain = num & 0x03;
310 311
#if 1
          __m256i base =  _mm256_set1_epi64x(w);
312
          __m256i maskVal = _mm256_set1_epi64x(mask);
313 314 315 316 317 318

          __m256i shiftBits = _mm256_set_epi64x(bit * 3 + 4, bit * 2 + 4, bit + 4, 4);
          __m256i inc = _mm256_set1_epi64x(bit << 2);

          for(int32_t i = 0; i < batch; ++i) {
            __m256i after = _mm256_srlv_epi64(base, shiftBits);
319
            __m256i zigzagVal= _mm256_and_si256(after, maskVal);
320

321 322
            // ZIGZAG_DECODE(T, v) (((v) >> 1) ^ -((T)((v)&1)))
            __m256i signmask = _mm256_and_si256(_mm256_set1_epi64x(1), zigzagVal);
323
            signmask = _mm256_sub_epi64(_mm256_setzero_si256(), signmask);
324 325
            // get the four zigzag values here
            __m256i delta = _mm256_xor_si256(_mm256_srli_epi64(zigzagVal, 1), signmask);
326

327
            // calculate the cumulative sum (prefix sum) for each number
328 329 330 331 332
            // decode[0] = prev_value + final[0]
            // decode[1] = decode[0] + final[1]   -----> prev_value + final[0] + final[1]
            // decode[2] = decode[1] + final[1]   -----> prev_value + final[0] + final[1] + final[2]
            // decode[3] = decode[2] + final[1]   -----> prev_value + final[0] + final[1] + final[2] + final[3]

333 334 335 336
            //  1, 2, 3, 4
            //+ 0, 1, 2, 3
            //  1, 3, 5, 7
            // shift and add for the first round
337
            __m128i prev = _mm_set1_epi64x(prev_value);
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
            delta = _mm256_add_epi64(delta, _mm256_slli_si256(delta, 8));
            _mm256_storeu_si256((__m256i *)&p[_pos], delta);

            //  1, 3, 5, 7
            //+ 0, 0, 1, 3
            //  1, 3, 6, 10
            // shift and add operation for the second round
            __m128i firstPart = _mm_loadu_si128((__m128i *)&p[_pos]);
            __m128i secPart = _mm_add_epi64(_mm_loadu_si128((__m128i *)&p[_pos + 2]), firstPart);
            firstPart = _mm_add_epi64(firstPart, prev);
            secPart = _mm_add_epi64(secPart, prev);

            // save it in the memory
            _mm_storeu_si128((__m128i *)&p[_pos], firstPart);
            _mm_storeu_si128((__m128i *)&p[_pos + 2], secPart);
353 354 355 356

            shiftBits = _mm256_add_epi64(shiftBits, inc);
            prev_value = p[_pos + 3];
            _pos += 4;
357
          }
H
Haojun Liao 已提交
358

359
          // handle the remain value
360
          for (int32_t i = 0; i < remain; i++) {
361
            zigzag_value = ((w >> (v + (batch * bit))) & mask);
362
            prev_value += ZIGZAG_DECODE(int64_t, zigzag_value);
H
Haojun Liao 已提交
363

364 365 366
            p[_pos++] = prev_value;
            v += bit;
          }
H
Haojun Liao 已提交
367
#else
368
          for (int32_t i = 0; i < elems && count < nelements; i++, count++) {
369 370
            zigzag_value = ((w >> v) & mask);
            prev_value += ZIGZAG_DECODE(int64_t, zigzag_value);
H
Haojun Liao 已提交
371

372
            p[_pos++] = prev_value;
373 374
            v += bit;
          }
H
Haojun Liao 已提交
375
#endif
H
Haojun Liao 已提交
376 377 378
        }
      } break;
      case TSDB_DATA_TYPE_INT: {
379 380
        int32_t* p = (int32_t*) output;

381
        if (selector == 0 || selector == 1) {
382 383
          for (int32_t i = 0; i < elems && count < nelements; i++, count++) {
            p[_pos++] = (int32_t)prev_value;
H
Haojun Liao 已提交
384
          }
385
        } else {
386
          for (int32_t i = 0; i < elems && count < nelements; i++, count++) {
387 388
            zigzag_value = ((w >> v) & mask);
            prev_value += ZIGZAG_DECODE(int64_t, zigzag_value);
H
Haojun Liao 已提交
389

390
            p[_pos++] = (int32_t)prev_value;
391 392
            v += bit;
          }
H
Haojun Liao 已提交
393 394 395
        }
      } break;
      case TSDB_DATA_TYPE_SMALLINT: {
396 397 398 399 400 401 402 403
        int16_t* p = (int16_t*) output;

        if (selector == 0 || selector == 1) {
          for (int32_t i = 0; i < elems && count < nelements; i++, count++) {
            p[_pos++] = (int16_t)prev_value;
          }
        } else {
          for (int32_t i = 0; i < elems && count < nelements; i++, count++) {
404 405
            zigzag_value = ((w >> v) & mask);
            prev_value += ZIGZAG_DECODE(int64_t, zigzag_value);
H
Haojun Liao 已提交
406

407 408 409
            p[_pos++] = (int16_t)prev_value;
            v += bit;
          }
H
Haojun Liao 已提交
410 411 412 413
        }
      } break;

      case TSDB_DATA_TYPE_TINYINT: {
414 415 416 417 418 419 420 421
        int8_t *p = (int8_t *)output;

        if (selector == 0 || selector == 1) {
          for (int32_t i = 0; i < elems && count < nelements; i++, count++) {
            p[_pos++] = (int8_t)prev_value;
          }
        } else {
          for (int32_t i = 0; i < elems && count < nelements; i++, count++) {
422 423
            zigzag_value = ((w >> v) & mask);
            prev_value += ZIGZAG_DECODE(int64_t, zigzag_value);
H
Haojun Liao 已提交
424

425 426 427
            p[_pos++] = (int8_t)prev_value;
            v += bit;
          }
H
Haojun Liao 已提交
428 429 430 431
        }
      } break;
    }

H
hzcheng 已提交
432 433 434 435
    ip += LONG_BYTES;
  }

  return nelements * word_length;
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 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
#else

  int32_t word_length = 0;
  switch (type) {
    case TSDB_DATA_TYPE_BIGINT:
      word_length = LONG_BYTES;
      break;
    case TSDB_DATA_TYPE_INT:
      word_length = INT_BYTES;
      break;
    case TSDB_DATA_TYPE_SMALLINT:
      word_length = SHORT_BYTES;
      break;
    case TSDB_DATA_TYPE_TINYINT:
      word_length = CHAR_BYTES;
      break;
    default:
    uError("Invalid decompress integer type:%d", type);
      return -1;
  }

  // If not compressed.
  if (input[0] == 1) {
    memcpy(output, input + 1, nelements * word_length);
    return nelements * word_length;
  }

  // Selector value:              0    1   2   3   4   5   6   7   8  9  10  11
  // 12  13  14  15
  char    bit_per_integer[] = {0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 15, 20, 30, 60};
  int32_t selector_to_elems[] = {240, 120, 60, 30, 20, 15, 12, 10, 8, 7, 6, 5, 4, 3, 2, 1};

  const char *ip = input + 1;
  int32_t     count = 0;
  int32_t     _pos = 0;
  int64_t     prev_value = 0;

  while (1) {
    if (count == nelements) break;

    uint64_t w = 0;
    memcpy(&w, ip, LONG_BYTES);

    char    selector = (char)(w & INT64MASK(4));       // selector = 4
    char    bit = bit_per_integer[(int32_t)selector];  // bit = 3
    int32_t elems = selector_to_elems[(int32_t)selector];

    for (int32_t i = 0; i < elems; i++) {
      uint64_t zigzag_value;

      if (selector == 0 || selector == 1) {
        zigzag_value = 0;
      } else {
        zigzag_value = ((w >> (4 + bit * i)) & INT64MASK(bit));
      }
      int64_t diff = ZIGZAG_DECODE(int64_t, zigzag_value);
      int64_t curr_value = diff + prev_value;
      prev_value = curr_value;

      switch (type) {
        case TSDB_DATA_TYPE_BIGINT:
          *((int64_t *)output + _pos) = (int64_t)curr_value;
          _pos++;
          break;
        case TSDB_DATA_TYPE_INT:
          *((int32_t *)output + _pos) = (int32_t)curr_value;
          _pos++;
          break;
        case TSDB_DATA_TYPE_SMALLINT:
          *((int16_t *)output + _pos) = (int16_t)curr_value;
          _pos++;
          break;
        case TSDB_DATA_TYPE_TINYINT:
          *((int8_t *)output + _pos) = (int8_t)curr_value;
          _pos++;
          break;
        default:
          perror("Wrong integer types.\n");
          return -1;
      }
      count++;
      if (count == nelements) break;
    }
    ip += LONG_BYTES;
  }

  return nelements * word_length;
#endif
H
hzcheng 已提交
524 525 526 527 528
}

/* ----------------------------------------------Bool Compression
 * ---------------------------------------------- */
// TODO: You can also implement it using RLE method.
S
Shengliang Guan 已提交
529 530 531
int32_t tsCompressBoolImp(const char *const input, const int32_t nelements, char *const output) {
  int32_t pos = -1;
  int32_t ele_per_byte = BITS_PER_BYTE / 2;
H
hzcheng 已提交
532

S
Shengliang Guan 已提交
533
  for (int32_t i = 0; i < nelements; i++) {
H
hzcheng 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
    if (i % ele_per_byte == 0) {
      pos++;
      output[pos] = 0;
    }

    uint8_t t = 0;
    if (input[i] == 1) {
      t = (((uint8_t)1) << (2 * (i % ele_per_byte)));
      output[pos] |= t;
    } else if (input[i] == 0) {
      t = ((uint8_t)1 << (2 * (i % ele_per_byte))) - 1;
      /* t = (~((( uint8_t)1) << (7-i%BITS_PER_BYTE))); */
      output[pos] &= t;
    } else if (input[i] == TSDB_DATA_BOOL_NULL) {
      t = ((uint8_t)2 << (2 * (i % ele_per_byte)));
      /* t = (~((( uint8_t)1) << (7-i%BITS_PER_BYTE))); */
      output[pos] |= t;
    } else {
552
      uError("Invalid compress bool value:%d", output[pos]);
H
Hongze Cheng 已提交
553
      return -1;
H
hzcheng 已提交
554 555 556 557 558 559
    }
  }

  return pos + 1;
}

S
Shengliang Guan 已提交
560 561 562
int32_t tsDecompressBoolImp(const char *const input, const int32_t nelements, char *const output) {
  int32_t ipos = -1, opos = 0;
  int32_t ele_per_byte = BITS_PER_BYTE / 2;
H
hzcheng 已提交
563

S
Shengliang Guan 已提交
564
  for (int32_t i = 0; i < nelements; i++) {
H
hzcheng 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
    if (i % ele_per_byte == 0) {
      ipos++;
    }

    uint8_t ele = (input[ipos] >> (2 * (i % ele_per_byte))) & INT8MASK(2);
    if (ele == 1) {
      output[opos++] = 1;
    } else if (ele == 2) {
      output[opos++] = TSDB_DATA_BOOL_NULL;
    } else {
      output[opos++] = 0;
    }
  }

  return nelements;
}

582
#if 0
H
hzcheng 已提交
583
/* Run Length Encoding(RLE) Method */
S
Shengliang Guan 已提交
584 585
int32_t tsCompressBoolRLEImp(const char *const input, const int32_t nelements, char *const output) {
  int32_t _pos = 0;
H
hzcheng 已提交
586

S
Shengliang Guan 已提交
587
  for (int32_t i = 0; i < nelements;) {
H
hzcheng 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
    unsigned char counter = 1;
    char          num = input[i];

    for (++i; i < nelements; i++) {
      if (input[i] == num) {
        counter++;
        if (counter == INT8MASK(7)) {
          i++;
          break;
        }
      } else {
        break;
      }
    }

    // Encode the data.
    if (num == 1) {
      output[_pos++] = INT8MASK(1) | (counter << 1);
    } else if (num == 0) {
      output[_pos++] = (counter << 1) | INT8MASK(0);
    } else {
609
      uError("Invalid compress bool value:%d", output[_pos]);
H
Hongze Cheng 已提交
610
      return -1;
H
hzcheng 已提交
611 612 613 614 615 616
    }
  }

  return _pos;
}

S
Shengliang Guan 已提交
617 618
int32_t tsDecompressBoolRLEImp(const char *const input, const int32_t nelements, char *const output) {
  int32_t ipos = 0, opos = 0;
H
hzcheng 已提交
619 620 621 622 623 624 625 626 627 628 629 630
  while (1) {
    char     encode = input[ipos++];
    unsigned counter = (encode >> 1) & INT8MASK(7);
    char     value = encode & INT8MASK(1);

    memset(output + opos, value, counter);
    opos += counter;
    if (opos >= nelements) {
      return nelements;
    }
  }
}
631
#endif
H
hzcheng 已提交
632 633 634 635 636 637

/* ----------------------------------------------String Compression
 * ---------------------------------------------- */
// Note: the size of the output must be larger than input_size + 1 and
// LZ4_compressBound(size) + 1;
// >= max(input_size, LZ4_compressBound(input_size)) + 1;
S
Shengliang Guan 已提交
638
int32_t tsCompressStringImp(const char *const input, int32_t inputSize, char *const output, int32_t outputSize) {
H
hzcheng 已提交
639
  // Try to compress using LZ4 algorithm.
S
Shengliang Guan 已提交
640
  const int32_t compressed_data_size = LZ4_compress_default(input, output + 1, inputSize, outputSize - 1);
H
hzcheng 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653

  // If cannot compress or after compression, data becomes larger.
  if (compressed_data_size <= 0 || compressed_data_size > inputSize) {
    /* First byte is for indicator */
    output[0] = 0;
    memcpy(output + 1, input, inputSize);
    return inputSize + 1;
  }

  output[0] = 1;
  return compressed_data_size + 1;
}

S
Shengliang Guan 已提交
654
int32_t tsDecompressStringImp(const char *const input, int32_t compressedSize, char *const output, int32_t outputSize) {
H
hzcheng 已提交
655
  // compressedSize is the size of data after compression.
S
Shengliang Guan 已提交
656

H
hzcheng 已提交
657 658
  if (input[0] == 1) {
    /* It is compressed by LZ4 algorithm */
S
Shengliang Guan 已提交
659
    const int32_t decompressed_size = LZ4_decompress_safe(input + 1, output, compressedSize - 1, outputSize);
H
hzcheng 已提交
660
    if (decompressed_size < 0) {
661
      uError("Failed to decompress string with LZ4 algorithm, decompressed size:%d", decompressed_size);
H
Hongze Cheng 已提交
662
      return -1;
H
hzcheng 已提交
663 664 665 666 667 668 669 670
    }

    return decompressed_size;
  } else if (input[0] == 0) {
    /* It is not compressed by LZ4 algorithm */
    memcpy(output, input + 1, compressedSize - 1);
    return compressedSize - 1;
  } else {
671
    uError("Invalid decompress string indicator:%d", input[0]);
H
Hongze Cheng 已提交
672
    return -1;
H
hzcheng 已提交
673 674 675 676 677 678
  }
}

/* --------------------------------------------Timestamp Compression
 * ---------------------------------------------- */
// TODO: Take care here, we assumes little endian encoding.
S
Shengliang Guan 已提交
679 680
int32_t tsCompressTimestampImp(const char *const input, const int32_t nelements, char *const output) {
  int32_t _pos = 1;
681
  ASSERTS(nelements >= 0, "nelements is negative");
H
hzcheng 已提交
682 683 684 685 686

  if (nelements == 0) return 0;

  int64_t *istream = (int64_t *)input;

S
Shengliang Guan 已提交
687 688 689 690
  int64_t prev_value = istream[0];
  if (prev_value >= 0x8000000000000000) {
    uWarn("compression timestamp is over signed long long range. ts = 0x%" PRIx64 " \n", prev_value);
    goto _exit_over;
T
tickduan 已提交
691
  }
H
hzcheng 已提交
692 693 694 695
  int64_t  prev_delta = -prev_value;
  uint8_t  flags = 0, flag1 = 0, flag2 = 0;
  uint64_t dd1 = 0, dd2 = 0;

S
Shengliang Guan 已提交
696
  for (int32_t i = 0; i < nelements; i++) {
H
hzcheng 已提交
697 698 699 700 701 702
    int64_t curr_value = istream[i];
    if (!safeInt64Add(curr_value, -prev_value)) goto _exit_over;
    int64_t curr_delta = curr_value - prev_value;
    if (!safeInt64Add(curr_delta, -prev_delta)) goto _exit_over;
    int64_t delta_of_delta = curr_delta - prev_delta;
    // zigzag encode the value.
H
Hongze Cheng 已提交
703
    uint64_t zigzag_value = ZIGZAG_ENCODE(int64_t, delta_of_delta);
H
hzcheng 已提交
704 705 706 707 708 709
    if (i % 2 == 0) {
      flags = 0;
      dd1 = zigzag_value;
      if (dd1 == 0) {
        flag1 = 0;
      } else {
H
Hongze Cheng 已提交
710
        flag1 = (uint8_t)(LONG_BYTES - BUILDIN_CLZL(dd1) / BITS_PER_BYTE);
H
hzcheng 已提交
711 712 713 714 715 716
      }
    } else {
      dd2 = zigzag_value;
      if (dd2 == 0) {
        flag2 = 0;
      } else {
H
Hongze Cheng 已提交
717
        flag2 = (uint8_t)(LONG_BYTES - BUILDIN_CLZL(dd2) / BITS_PER_BYTE);
H
hzcheng 已提交
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
      }
      flags = flag1 | (flag2 << 4);
      // Encode the flag.
      if ((_pos + CHAR_BYTES - 1) >= nelements * LONG_BYTES) goto _exit_over;
      memcpy(output + _pos, &flags, CHAR_BYTES);
      _pos += CHAR_BYTES;
      /* Here, we assume it is little endian encoding method. */
      // Encode dd1
      if (is_bigendian()) {
        if ((_pos + flag1 - 1) >= nelements * LONG_BYTES) goto _exit_over;
        memcpy(output + _pos, (char *)(&dd1) + LONG_BYTES - flag1, flag1);
      } else {
        if ((_pos + flag1 - 1) >= nelements * LONG_BYTES) goto _exit_over;
        memcpy(output + _pos, (char *)(&dd1), flag1);
      }
      _pos += flag1;
      // Encode dd2;
      if (is_bigendian()) {
        if ((_pos + flag2 - 1) >= nelements * LONG_BYTES) goto _exit_over;
        memcpy(output + _pos, (char *)(&dd2) + LONG_BYTES - flag2, flag2);
      } else {
        if ((_pos + flag2 - 1) >= nelements * LONG_BYTES) goto _exit_over;
        memcpy(output + _pos, (char *)(&dd2), flag2);
      }
      _pos += flag2;
    }
    prev_value = curr_value;
    prev_delta = curr_delta;
  }

  if (nelements % 2 == 1) {
    flag2 = 0;
    flags = flag1 | (flag2 << 4);
    // Encode the flag.
    if ((_pos + CHAR_BYTES - 1) >= nelements * LONG_BYTES) goto _exit_over;
    memcpy(output + _pos, &flags, CHAR_BYTES);
    _pos += CHAR_BYTES;
    // Encode dd1;
    if (is_bigendian()) {
      if ((_pos + flag1 - 1) >= nelements * LONG_BYTES) goto _exit_over;
      memcpy(output + _pos, (char *)(&dd1) + LONG_BYTES - flag1, flag1);
    } else {
      if ((_pos + flag1 - 1) >= nelements * LONG_BYTES) goto _exit_over;
      memcpy(output + _pos, (char *)(&dd1), flag1);
    }
    _pos += flag1;
  }

  output[0] = 1;  // Means the string is compressed
  return _pos;

_exit_over:
  output[0] = 0;  // Means the string is not compressed
  memcpy(output + 1, input, nelements * LONG_BYTES);
  return nelements * LONG_BYTES + 1;
}

S
Shengliang Guan 已提交
775
int32_t tsDecompressTimestampImp(const char *const input, const int32_t nelements, char *const output) {
776
  ASSERTS(nelements >= 0, "nelements is negative");
H
hzcheng 已提交
777 778 779 780 781 782 783 784
  if (nelements == 0) return 0;

  if (input[0] == 0) {
    memcpy(output, input + 1, nelements * LONG_BYTES);
    return nelements * LONG_BYTES;
  } else if (input[0] == 1) {  // Decompress
    int64_t *ostream = (int64_t *)output;

S
Shengliang Guan 已提交
785
    int32_t ipos = 1, opos = 0;
H
hzcheng 已提交
786 787 788 789 790 791 792 793 794 795 796 797 798 799
    int8_t  nbytes = 0;
    int64_t prev_value = 0;
    int64_t prev_delta = 0;
    int64_t delta_of_delta = 0;

    while (1) {
      uint8_t flags = input[ipos++];
      // Decode dd1
      uint64_t dd1 = 0;
      nbytes = flags & INT8MASK(4);
      if (nbytes == 0) {
        delta_of_delta = 0;
      } else {
        if (is_bigendian()) {
H
Hui Li 已提交
800
          memcpy(((char *)(&dd1)) + LONG_BYTES - nbytes, input + ipos, nbytes);
H
hzcheng 已提交
801 802 803
        } else {
          memcpy(&dd1, input + ipos, nbytes);
        }
H
Hongze Cheng 已提交
804
        delta_of_delta = ZIGZAG_DECODE(int64_t, dd1);
H
hzcheng 已提交
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
      }
      ipos += nbytes;
      if (opos == 0) {
        prev_value = delta_of_delta;
        prev_delta = 0;
        ostream[opos++] = delta_of_delta;
      } else {
        prev_delta = delta_of_delta + prev_delta;
        prev_value = prev_value + prev_delta;
        ostream[opos++] = prev_value;
      }
      if (opos == nelements) return nelements * LONG_BYTES;

      // Decode dd2
      uint64_t dd2 = 0;
      nbytes = (flags >> 4) & INT8MASK(4);
      if (nbytes == 0) {
        delta_of_delta = 0;
      } else {
        if (is_bigendian()) {
H
Hui Li 已提交
825
          memcpy(((char *)(&dd2)) + LONG_BYTES - nbytes, input + ipos, nbytes);
H
hzcheng 已提交
826 827 828 829
        } else {
          memcpy(&dd2, input + ipos, nbytes);
        }
        // zigzag_decoding
H
Hongze Cheng 已提交
830
        delta_of_delta = ZIGZAG_DECODE(int64_t, dd2);
H
hzcheng 已提交
831 832 833 834 835 836 837 838 839
      }
      ipos += nbytes;
      prev_delta = delta_of_delta + prev_delta;
      prev_value = prev_value + prev_delta;
      ostream[opos++] = prev_value;
      if (opos == nelements) return nelements * LONG_BYTES;
    }

  } else {
840
    ASSERT(0);
S
TD-1057  
Shengliang Guan 已提交
841
    return -1;
H
hzcheng 已提交
842 843 844 845
  }
}
/* --------------------------------------------Double Compression
 * ---------------------------------------------- */
S
Shengliang Guan 已提交
846
void encodeDoubleValue(uint64_t diff, uint8_t flag, char *const output, int32_t *const pos) {
H
hzcheng 已提交
847
  uint8_t nbytes = (flag & INT8MASK(3)) + 1;
S
Shengliang Guan 已提交
848
  int32_t nshift = (LONG_BYTES * BITS_PER_BYTE - nbytes * BITS_PER_BYTE) * (flag >> 3);
H
hzcheng 已提交
849 850 851 852 853 854 855 856 857
  diff >>= nshift;

  while (nbytes) {
    output[(*pos)++] = (int8_t)(diff & INT64MASK(8));
    diff >>= BITS_PER_BYTE;
    nbytes--;
  }
}

S
Shengliang Guan 已提交
858 859 860
int32_t tsCompressDoubleImp(const char *const input, const int32_t nelements, char *const output) {
  int32_t byte_limit = nelements * DOUBLE_BYTES + 1;
  int32_t opos = 1;
H
hzcheng 已提交
861 862 863 864 865 866 867 868

  uint64_t prev_value = 0;
  uint64_t prev_diff = 0;
  uint8_t  prev_flag = 0;

  double *istream = (double *)input;

  // Main loop
S
Shengliang Guan 已提交
869
  for (int32_t i = 0; i < nelements; i++) {
H
hzcheng 已提交
870 871 872 873 874 875 876 877 878 879 880
    union {
      double   real;
      uint64_t bits;
    } curr;

    curr.real = istream[i];

    // Here we assume the next value is the same as previous one.
    uint64_t predicted = prev_value;
    uint64_t diff = curr.bits ^ predicted;

S
Shengliang Guan 已提交
881 882
    int32_t leading_zeros = LONG_BYTES * BITS_PER_BYTE;
    int32_t trailing_zeros = leading_zeros;
H
hzcheng 已提交
883 884

    if (diff) {
885 886
      trailing_zeros = BUILDIN_CTZL(diff);
      leading_zeros = BUILDIN_CLZL(diff);
H
hzcheng 已提交
887 888 889 890 891 892
    }

    uint8_t nbytes = 0;
    uint8_t flag;

    if (trailing_zeros > leading_zeros) {
H
Hongze Cheng 已提交
893
      nbytes = (uint8_t)(LONG_BYTES - trailing_zeros / BITS_PER_BYTE);
H
hzcheng 已提交
894 895 896 897

      if (nbytes > 0) nbytes--;
      flag = ((uint8_t)1 << 3) | nbytes;
    } else {
H
Hongze Cheng 已提交
898
      nbytes = (uint8_t)(LONG_BYTES - leading_zeros / BITS_PER_BYTE);
H
hzcheng 已提交
899 900 901 902 903 904 905 906
      if (nbytes > 0) nbytes--;
      flag = nbytes;
    }

    if (i % 2 == 0) {
      prev_diff = diff;
      prev_flag = flag;
    } else {
S
Shengliang Guan 已提交
907 908
      int32_t nbyte1 = (prev_flag & INT8MASK(3)) + 1;
      int32_t nbyte2 = (flag & INT8MASK(3)) + 1;
H
hzcheng 已提交
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
      if (opos + 1 + nbyte1 + nbyte2 <= byte_limit) {
        uint8_t flags = prev_flag | (flag << 4);
        output[opos++] = flags;
        encodeDoubleValue(prev_diff, prev_flag, output, &opos);
        encodeDoubleValue(diff, flag, output, &opos);
      } else {
        output[0] = 1;
        memcpy(output + 1, input, byte_limit - 1);
        return byte_limit;
      }
    }
    prev_value = curr.bits;
  }

  if (nelements % 2) {
S
Shengliang Guan 已提交
924 925
    int32_t nbyte1 = (prev_flag & INT8MASK(3)) + 1;
    int32_t nbyte2 = 1;
H
hzcheng 已提交
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
    if (opos + 1 + nbyte1 + nbyte2 <= byte_limit) {
      uint8_t flags = prev_flag;
      output[opos++] = flags;
      encodeDoubleValue(prev_diff, prev_flag, output, &opos);
      encodeDoubleValue(0ul, 0, output, &opos);
    } else {
      output[0] = 1;
      memcpy(output + 1, input, byte_limit - 1);
      return byte_limit;
    }
  }

  output[0] = 0;
  return opos;
}

S
Shengliang Guan 已提交
942
uint64_t decodeDoubleValue(const char *const input, int32_t *const ipos, uint8_t flag) {
H
hzcheng 已提交
943
  uint64_t diff = 0ul;
S
Shengliang Guan 已提交
944 945
  int32_t  nbytes = (flag & INT8MASK(3)) + 1;
  for (int32_t i = 0; i < nbytes; i++) {
H
hzcheng 已提交
946 947
    diff = diff | ((INT64MASK(8) & input[(*ipos)++]) << BITS_PER_BYTE * i);
  }
S
Shengliang Guan 已提交
948
  int32_t shift_width = (LONG_BYTES * BITS_PER_BYTE - nbytes * BITS_PER_BYTE) * (flag >> 3);
H
hzcheng 已提交
949 950 951 952 953
  diff <<= shift_width;

  return diff;
}

S
Shengliang Guan 已提交
954
int32_t tsDecompressDoubleImp(const char *const input, const int32_t nelements, char *const output) {
H
hzcheng 已提交
955 956 957 958 959 960 961 962 963
  // output stream
  double *ostream = (double *)output;

  if (input[0] == 1) {
    memcpy(output, input + 1, nelements * DOUBLE_BYTES);
    return nelements * DOUBLE_BYTES;
  }

  uint8_t  flags = 0;
S
Shengliang Guan 已提交
964 965
  int32_t  ipos = 1;
  int32_t  opos = 0;
H
hzcheng 已提交
966 967
  uint64_t prev_value = 0;

S
Shengliang Guan 已提交
968
  for (int32_t i = 0; i < nelements; i++) {
H
Haojun Liao 已提交
969
    if ((i & 0x01) == 0) {
H
hzcheng 已提交
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
      flags = input[ipos++];
    }

    uint8_t flag = flags & INT8MASK(4);
    flags >>= 4;

    uint64_t diff = decodeDoubleValue(input, &ipos, flag);
    union {
      uint64_t bits;
      double   real;
    } curr;

    uint64_t predicted = prev_value;
    curr.bits = predicted ^ diff;
    prev_value = curr.bits;

    ostream[opos++] = curr.real;
  }

  return nelements * DOUBLE_BYTES;
}

/* --------------------------------------------Float Compression
 * ---------------------------------------------- */
S
Shengliang Guan 已提交
994
void encodeFloatValue(uint32_t diff, uint8_t flag, char *const output, int32_t *const pos) {
H
hzcheng 已提交
995
  uint8_t nbytes = (flag & INT8MASK(3)) + 1;
S
Shengliang Guan 已提交
996
  int32_t nshift = (FLOAT_BYTES * BITS_PER_BYTE - nbytes * BITS_PER_BYTE) * (flag >> 3);
H
hzcheng 已提交
997 998 999 1000 1001 1002 1003 1004 1005
  diff >>= nshift;

  while (nbytes) {
    output[(*pos)++] = (int8_t)(diff & INT32MASK(8));
    diff >>= BITS_PER_BYTE;
    nbytes--;
  }
}

S
Shengliang Guan 已提交
1006 1007 1008 1009
int32_t tsCompressFloatImp(const char *const input, const int32_t nelements, char *const output) {
  float  *istream = (float *)input;
  int32_t byte_limit = nelements * FLOAT_BYTES + 1;
  int32_t opos = 1;
H
hzcheng 已提交
1010 1011 1012 1013 1014 1015

  uint32_t prev_value = 0;
  uint32_t prev_diff = 0;
  uint8_t  prev_flag = 0;

  // Main loop
S
Shengliang Guan 已提交
1016
  for (int32_t i = 0; i < nelements; i++) {
H
hzcheng 已提交
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
    union {
      float    real;
      uint32_t bits;
    } curr;

    curr.real = istream[i];

    // Here we assume the next value is the same as previous one.
    uint32_t predicted = prev_value;
    uint32_t diff = curr.bits ^ predicted;

H
Hongze Cheng 已提交
1028 1029
    int32_t clz = FLOAT_BYTES * BITS_PER_BYTE;
    int32_t ctz = clz;
H
hzcheng 已提交
1030 1031

    if (diff) {
H
Hongze Cheng 已提交
1032 1033
      ctz = BUILDIN_CTZ(diff);
      clz = BUILDIN_CLZ(diff);
H
hzcheng 已提交
1034 1035 1036 1037 1038
    }

    uint8_t nbytes = 0;
    uint8_t flag;

H
Hongze Cheng 已提交
1039 1040
    if (ctz > clz) {
      nbytes = (uint8_t)(FLOAT_BYTES - ctz / BITS_PER_BYTE);
H
hzcheng 已提交
1041 1042 1043 1044

      if (nbytes > 0) nbytes--;
      flag = ((uint8_t)1 << 3) | nbytes;
    } else {
H
Hongze Cheng 已提交
1045
      nbytes = (uint8_t)(FLOAT_BYTES - clz / BITS_PER_BYTE);
H
hzcheng 已提交
1046 1047 1048 1049 1050 1051 1052 1053
      if (nbytes > 0) nbytes--;
      flag = nbytes;
    }

    if (i % 2 == 0) {
      prev_diff = diff;
      prev_flag = flag;
    } else {
S
Shengliang Guan 已提交
1054 1055
      int32_t nbyte1 = (prev_flag & INT8MASK(3)) + 1;
      int32_t nbyte2 = (flag & INT8MASK(3)) + 1;
H
hzcheng 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
      if (opos + 1 + nbyte1 + nbyte2 <= byte_limit) {
        uint8_t flags = prev_flag | (flag << 4);
        output[opos++] = flags;
        encodeFloatValue(prev_diff, prev_flag, output, &opos);
        encodeFloatValue(diff, flag, output, &opos);
      } else {
        output[0] = 1;
        memcpy(output + 1, input, byte_limit - 1);
        return byte_limit;
      }
    }
    prev_value = curr.bits;
  }

  if (nelements % 2) {
S
Shengliang Guan 已提交
1071 1072
    int32_t nbyte1 = (prev_flag & INT8MASK(3)) + 1;
    int32_t nbyte2 = 1;
H
hzcheng 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
    if (opos + 1 + nbyte1 + nbyte2 <= byte_limit) {
      uint8_t flags = prev_flag;
      output[opos++] = flags;
      encodeFloatValue(prev_diff, prev_flag, output, &opos);
      encodeFloatValue(0, 0, output, &opos);
    } else {
      output[0] = 1;
      memcpy(output + 1, input, byte_limit - 1);
      return byte_limit;
    }
  }

  output[0] = 0;
  return opos;
}

S
Shengliang Guan 已提交
1089
uint32_t decodeFloatValue(const char *const input, int32_t *const ipos, uint8_t flag) {
H
hzcheng 已提交
1090
  uint32_t diff = 0ul;
S
Shengliang Guan 已提交
1091 1092
  int32_t  nbytes = (flag & INT8MASK(3)) + 1;
  for (int32_t i = 0; i < nbytes; i++) {
H
hzcheng 已提交
1093 1094
    diff = diff | ((INT32MASK(8) & input[(*ipos)++]) << BITS_PER_BYTE * i);
  }
S
Shengliang Guan 已提交
1095
  int32_t shift_width = (FLOAT_BYTES * BITS_PER_BYTE - nbytes * BITS_PER_BYTE) * (flag >> 3);
H
hzcheng 已提交
1096 1097 1098 1099 1100
  diff <<= shift_width;

  return diff;
}

S
Shengliang Guan 已提交
1101
int32_t tsDecompressFloatImp(const char *const input, const int32_t nelements, char *const output) {
H
hzcheng 已提交
1102 1103 1104 1105 1106 1107 1108 1109
  float *ostream = (float *)output;

  if (input[0] == 1) {
    memcpy(output, input + 1, nelements * FLOAT_BYTES);
    return nelements * FLOAT_BYTES;
  }

  uint8_t  flags = 0;
S
Shengliang Guan 已提交
1110 1111
  int32_t  ipos = 1;
  int32_t  opos = 0;
H
hzcheng 已提交
1112 1113
  uint32_t prev_value = 0;

S
Shengliang Guan 已提交
1114
  for (int32_t i = 0; i < nelements; i++) {
H
hzcheng 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
    if (i % 2 == 0) {
      flags = input[ipos++];
    }

    uint8_t flag = flags & INT8MASK(4);
    flags >>= 4;

    uint32_t diff = decodeFloatValue(input, &ipos, flag);
    union {
      uint32_t bits;
      float    real;
    } curr;

    uint32_t predicted = prev_value;
    curr.bits = predicted ^ diff;
    prev_value = curr.bits;

    ostream[opos++] = curr.real;
  }

  return nelements * FLOAT_BYTES;
}
T
tickduan 已提交
1137

S
Shengliang Guan 已提交
1138
#ifdef TD_TSZ
T
tickduan 已提交
1139 1140 1141
//
//   ----------  float double lossy  -----------
//
S
Shengliang Guan 已提交
1142 1143 1144
int32_t tsCompressFloatLossyImp(const char *input, const int32_t nelements, char *const output) {
  // compress with sz
  int32_t       compressedSize = tdszCompress(SZ_FLOAT, input, nelements, output + 1);
T
tickduan 已提交
1145
  unsigned char algo = ALGO_SZ_LOSSY << 1;
S
Shengliang Guan 已提交
1146
  if (compressedSize == 0 || compressedSize >= nelements * sizeof(float)) {
T
tickduan 已提交
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
    // compressed error or large than original
    output[0] = MODE_NOCOMPRESS | algo;
    memcpy(output + 1, input, nelements * sizeof(float));
    compressedSize = 1 + nelements * sizeof(float);
  } else {
    // compressed successfully
    output[0] = MODE_COMPRESS | algo;
    compressedSize += 1;
  }

  return compressedSize;
T
tickduan 已提交
1158 1159
}

S
Shengliang Guan 已提交
1160 1161 1162 1163
int32_t tsDecompressFloatLossyImp(const char *input, int32_t compressedSize, const int32_t nelements,
                                  char *const output) {
  int32_t decompressedSize = 0;
  if (HEAD_MODE(input[0]) == MODE_NOCOMPRESS) {
T
tickduan 已提交
1164 1165 1166 1167 1168
    // orginal so memcpy directly
    decompressedSize = nelements * sizeof(float);
    memcpy(output, input + 1, decompressedSize);

    return decompressedSize;
S
Shengliang Guan 已提交
1169
  }
T
tickduan 已提交
1170 1171 1172

  // decompressed with sz
  return tdszDecompress(SZ_FLOAT, input + 1, compressedSize - 1, nelements, output);
T
tickduan 已提交
1173 1174
}

S
Shengliang Guan 已提交
1175 1176 1177
int32_t tsCompressDoubleLossyImp(const char *input, const int32_t nelements, char *const output) {
  // compress with sz
  int32_t       compressedSize = tdszCompress(SZ_DOUBLE, input, nelements, output + 1);
T
tickduan 已提交
1178
  unsigned char algo = ALGO_SZ_LOSSY << 1;
S
Shengliang Guan 已提交
1179
  if (compressedSize == 0 || compressedSize >= nelements * sizeof(double)) {
T
tickduan 已提交
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
    // compressed error or large than original
    output[0] = MODE_NOCOMPRESS | algo;
    memcpy(output + 1, input, nelements * sizeof(double));
    compressedSize = 1 + nelements * sizeof(double);
  } else {
    // compressed successfully
    output[0] = MODE_COMPRESS | algo;
    compressedSize += 1;
  }

S
Shengliang Guan 已提交
1190
  return compressedSize;
T
tickduan 已提交
1191 1192
}

S
Shengliang Guan 已提交
1193 1194 1195 1196
int32_t tsDecompressDoubleLossyImp(const char *input, int32_t compressedSize, const int32_t nelements,
                                   char *const output) {
  int32_t decompressedSize = 0;
  if (HEAD_MODE(input[0]) == MODE_NOCOMPRESS) {
T
tickduan 已提交
1197 1198 1199 1200 1201
    // orginal so memcpy directly
    decompressedSize = nelements * sizeof(double);
    memcpy(output, input + 1, decompressedSize);

    return decompressedSize;
S
Shengliang Guan 已提交
1202
  }
T
tickduan 已提交
1203 1204 1205

  // decompressed with sz
  return tdszDecompress(SZ_DOUBLE, input + 1, compressedSize - 1, nelements, output);
1206
}
Y
yihaoDeng 已提交
1207
#endif
H
Hongze Cheng 已提交
1208 1209 1210 1211

/*************************************************************************
 *                  STREAM COMPRESSION
 *************************************************************************/
H
Hongze Cheng 已提交
1212
#define I64_SAFE_ADD(a, b) (((a) >= 0 && (b) <= INT64_MAX - (a)) || ((a) < 0 && (b) >= INT64_MIN - (a)))
H
Hongze Cheng 已提交
1213

H
Hongze Cheng 已提交
1214
static int32_t tCompBoolStart(SCompressor *pCmprsor, int8_t type, int8_t cmprAlg);
H
Hongze Cheng 已提交
1215
static int32_t tCompBool(SCompressor *pCmprsor, const void *pData, int32_t nData);
H
Hongze Cheng 已提交
1216 1217 1218
static int32_t tCompBoolEnd(SCompressor *pCmprsor, const uint8_t **ppData, int32_t *nData);

static int32_t tCompIntStart(SCompressor *pCmprsor, int8_t type, int8_t cmprAlg);
H
Hongze Cheng 已提交
1219
static int32_t tCompInt(SCompressor *pCmprsor, const void *pData, int32_t nData);
H
Hongze Cheng 已提交
1220 1221 1222
static int32_t tCompIntEnd(SCompressor *pCmprsor, const uint8_t **ppData, int32_t *nData);

static int32_t tCompFloatStart(SCompressor *pCmprsor, int8_t type, int8_t cmprAlg);
H
Hongze Cheng 已提交
1223
static int32_t tCompFloat(SCompressor *pCmprsor, const void *pData, int32_t nData);
H
Hongze Cheng 已提交
1224 1225 1226
static int32_t tCompFloatEnd(SCompressor *pCmprsor, const uint8_t **ppData, int32_t *nData);

static int32_t tCompDoubleStart(SCompressor *pCmprsor, int8_t type, int8_t cmprAlg);
H
Hongze Cheng 已提交
1227
static int32_t tCompDouble(SCompressor *pCmprsor, const void *pData, int32_t nData);
H
Hongze Cheng 已提交
1228 1229 1230
static int32_t tCompDoubleEnd(SCompressor *pCmprsor, const uint8_t **ppData, int32_t *nData);

static int32_t tCompTimestampStart(SCompressor *pCmprsor, int8_t type, int8_t cmprAlg);
H
Hongze Cheng 已提交
1231
static int32_t tCompTimestamp(SCompressor *pCmprsor, const void *pData, int32_t nData);
H
Hongze Cheng 已提交
1232 1233 1234
static int32_t tCompTimestampEnd(SCompressor *pCmprsor, const uint8_t **ppData, int32_t *nData);

static int32_t tCompBinaryStart(SCompressor *pCmprsor, int8_t type, int8_t cmprAlg);
H
Hongze Cheng 已提交
1235
static int32_t tCompBinary(SCompressor *pCmprsor, const void *pData, int32_t nData);
H
Hongze Cheng 已提交
1236
static int32_t tCompBinaryEnd(SCompressor *pCmprsor, const uint8_t **ppData, int32_t *nData);
H
Hongze Cheng 已提交
1237

H
Hongze Cheng 已提交
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
static FORCE_INLINE int64_t tGetI64OfI8(const void *pData) { return *(int8_t *)pData; }
static FORCE_INLINE int64_t tGetI64OfI16(const void *pData) { return *(int16_t *)pData; }
static FORCE_INLINE int64_t tGetI64OfI32(const void *pData) { return *(int32_t *)pData; }
static FORCE_INLINE int64_t tGetI64OfI64(const void *pData) { return *(int64_t *)pData; }

static FORCE_INLINE void tPutI64OfI8(int64_t v, void *pData) { *(int8_t *)pData = v; }
static FORCE_INLINE void tPutI64OfI16(int64_t v, void *pData) { *(int16_t *)pData = v; }
static FORCE_INLINE void tPutI64OfI32(int64_t v, void *pData) { *(int32_t *)pData = v; }
static FORCE_INLINE void tPutI64OfI64(int64_t v, void *pData) { *(int64_t *)pData = v; }

H
Hongze Cheng 已提交
1248 1249 1250 1251
static struct {
  int8_t  type;
  int32_t bytes;
  int8_t  isVarLen;
H
Hongze Cheng 已提交
1252
  int32_t (*startFn)(SCompressor *, int8_t type, int8_t cmprAlg);
H
Hongze Cheng 已提交
1253
  int32_t (*cmprFn)(SCompressor *, const void *, int32_t nData);
H
Hongze Cheng 已提交
1254
  int32_t (*endFn)(SCompressor *, const uint8_t **, int32_t *);
H
Hongze Cheng 已提交
1255
  int64_t (*getI64)(const void *pData);
H
Hongze Cheng 已提交
1256
  void (*putI64)(int64_t v, void *pData);
H
Hongze Cheng 已提交
1257
} DATA_TYPE_INFO[] = {
H
Hongze Cheng 已提交
1258 1259 1260 1261 1262 1263
    {.type = TSDB_DATA_TYPE_NULL,
     .bytes = 0,
     .isVarLen = 0,
     .startFn = NULL,
     .cmprFn = NULL,
     .endFn = NULL,
H
Hongze Cheng 已提交
1264 1265
     .getI64 = NULL,
     .putI64 = NULL},
H
Hongze Cheng 已提交
1266 1267 1268 1269 1270
    {.type = TSDB_DATA_TYPE_BOOL,
     .bytes = 1,
     .isVarLen = 0,
     .startFn = tCompBoolStart,
     .cmprFn = tCompBool,
H
Hongze Cheng 已提交
1271
     .endFn = tCompBoolEnd,
H
Hongze Cheng 已提交
1272 1273
     .getI64 = NULL,
     .putI64 = NULL},
H
Hongze Cheng 已提交
1274 1275 1276 1277 1278
    {.type = TSDB_DATA_TYPE_TINYINT,
     .bytes = 1,
     .isVarLen = 0,
     .startFn = tCompIntStart,
     .cmprFn = tCompInt,
H
Hongze Cheng 已提交
1279
     .endFn = tCompIntEnd,
H
Hongze Cheng 已提交
1280 1281
     .getI64 = tGetI64OfI8,
     .putI64 = tPutI64OfI8},
H
Hongze Cheng 已提交
1282 1283 1284 1285 1286
    {.type = TSDB_DATA_TYPE_SMALLINT,
     .bytes = 2,
     .isVarLen = 0,
     .startFn = tCompIntStart,
     .cmprFn = tCompInt,
H
Hongze Cheng 已提交
1287
     .endFn = tCompIntEnd,
H
Hongze Cheng 已提交
1288 1289
     .getI64 = tGetI64OfI16,
     .putI64 = tPutI64OfI16},
H
Hongze Cheng 已提交
1290 1291 1292 1293 1294
    {.type = TSDB_DATA_TYPE_INT,
     .bytes = 4,
     .isVarLen = 0,
     .startFn = tCompIntStart,
     .cmprFn = tCompInt,
H
Hongze Cheng 已提交
1295
     .endFn = tCompIntEnd,
H
Hongze Cheng 已提交
1296 1297
     .getI64 = tGetI64OfI32,
     .putI64 = tPutI64OfI32},
H
Hongze Cheng 已提交
1298 1299 1300 1301 1302
    {.type = TSDB_DATA_TYPE_BIGINT,
     .bytes = 8,
     .isVarLen = 0,
     .startFn = tCompIntStart,
     .cmprFn = tCompInt,
H
Hongze Cheng 已提交
1303
     .endFn = tCompIntEnd,
H
Hongze Cheng 已提交
1304 1305
     .getI64 = tGetI64OfI64,
     .putI64 = tPutI64OfI64},
H
Hongze Cheng 已提交
1306 1307 1308 1309 1310
    {.type = TSDB_DATA_TYPE_FLOAT,
     .bytes = 4,
     .isVarLen = 0,
     .startFn = tCompFloatStart,
     .cmprFn = tCompFloat,
H
Hongze Cheng 已提交
1311
     .endFn = tCompFloatEnd,
H
Hongze Cheng 已提交
1312 1313
     .getI64 = NULL,
     .putI64 = NULL},
H
Hongze Cheng 已提交
1314 1315 1316 1317 1318
    {.type = TSDB_DATA_TYPE_DOUBLE,
     .bytes = 8,
     .isVarLen = 0,
     .startFn = tCompDoubleStart,
     .cmprFn = tCompDouble,
H
Hongze Cheng 已提交
1319
     .endFn = tCompDoubleEnd,
H
Hongze Cheng 已提交
1320 1321
     .getI64 = NULL,
     .putI64 = NULL},
H
Hongze Cheng 已提交
1322 1323 1324 1325 1326
    {.type = TSDB_DATA_TYPE_VARCHAR,
     .bytes = 1,
     .isVarLen = 1,
     .startFn = tCompBinaryStart,
     .cmprFn = tCompBinary,
H
Hongze Cheng 已提交
1327
     .endFn = tCompBinaryEnd,
H
Hongze Cheng 已提交
1328 1329
     .getI64 = NULL,
     .putI64 = NULL},
H
Hongze Cheng 已提交
1330 1331 1332
    {.type = TSDB_DATA_TYPE_TIMESTAMP,
     .bytes = 8,
     .isVarLen = 0,
H
Hongze Cheng 已提交
1333 1334
     .startFn = tCompTimestampStart,
     .cmprFn = tCompTimestamp,
H
Hongze Cheng 已提交
1335
     .endFn = tCompTimestampEnd,
H
Hongze Cheng 已提交
1336 1337
     .getI64 = NULL,
     .putI64 = NULL},
H
Hongze Cheng 已提交
1338 1339 1340 1341 1342
    {.type = TSDB_DATA_TYPE_NCHAR,
     .bytes = 1,
     .isVarLen = 1,
     .startFn = tCompBinaryStart,
     .cmprFn = tCompBinary,
H
Hongze Cheng 已提交
1343
     .endFn = tCompBinaryEnd,
H
Hongze Cheng 已提交
1344 1345
     .getI64 = NULL,
     .putI64 = NULL},
H
Hongze Cheng 已提交
1346 1347 1348 1349 1350
    {.type = TSDB_DATA_TYPE_UTINYINT,
     .bytes = 1,
     .isVarLen = 0,
     .startFn = tCompIntStart,
     .cmprFn = tCompInt,
H
Hongze Cheng 已提交
1351
     .endFn = tCompIntEnd,
H
Hongze Cheng 已提交
1352 1353
     .getI64 = tGetI64OfI8,
     .putI64 = tPutI64OfI8},
H
Hongze Cheng 已提交
1354 1355 1356 1357 1358
    {.type = TSDB_DATA_TYPE_USMALLINT,
     .bytes = 2,
     .isVarLen = 0,
     .startFn = tCompIntStart,
     .cmprFn = tCompInt,
H
Hongze Cheng 已提交
1359
     .endFn = tCompIntEnd,
H
Hongze Cheng 已提交
1360 1361
     .getI64 = tGetI64OfI16,
     .putI64 = tPutI64OfI16},
H
Hongze Cheng 已提交
1362 1363 1364 1365 1366
    {.type = TSDB_DATA_TYPE_UINT,
     .bytes = 4,
     .isVarLen = 0,
     .startFn = tCompIntStart,
     .cmprFn = tCompInt,
H
Hongze Cheng 已提交
1367
     .endFn = tCompIntEnd,
H
Hongze Cheng 已提交
1368 1369
     .getI64 = tGetI64OfI32,
     .putI64 = tPutI64OfI32},
H
Hongze Cheng 已提交
1370 1371 1372 1373 1374
    {.type = TSDB_DATA_TYPE_UBIGINT,
     .bytes = 8,
     .isVarLen = 0,
     .startFn = tCompIntStart,
     .cmprFn = tCompInt,
H
Hongze Cheng 已提交
1375
     .endFn = tCompIntEnd,
H
Hongze Cheng 已提交
1376 1377
     .getI64 = tGetI64OfI64,
     .putI64 = tPutI64OfI64},
H
Hongze Cheng 已提交
1378 1379 1380 1381 1382
    {.type = TSDB_DATA_TYPE_JSON,
     .bytes = 1,
     .isVarLen = 1,
     .startFn = tCompBinaryStart,
     .cmprFn = tCompBinary,
H
Hongze Cheng 已提交
1383
     .endFn = tCompBinaryEnd,
H
Hongze Cheng 已提交
1384 1385
     .getI64 = NULL,
     .putI64 = NULL},
H
Hongze Cheng 已提交
1386 1387 1388 1389 1390
    {.type = TSDB_DATA_TYPE_VARBINARY,
     .bytes = 1,
     .isVarLen = 1,
     .startFn = tCompBinaryStart,
     .cmprFn = tCompBinary,
H
Hongze Cheng 已提交
1391
     .endFn = tCompBinaryEnd,
H
Hongze Cheng 已提交
1392 1393
     .getI64 = NULL,
     .putI64 = NULL},
H
Hongze Cheng 已提交
1394 1395 1396 1397 1398
    {.type = TSDB_DATA_TYPE_DECIMAL,
     .bytes = 1,
     .isVarLen = 1,
     .startFn = tCompBinaryStart,
     .cmprFn = tCompBinary,
H
Hongze Cheng 已提交
1399
     .endFn = tCompBinaryEnd,
H
Hongze Cheng 已提交
1400 1401
     .getI64 = NULL,
     .putI64 = NULL},
H
Hongze Cheng 已提交
1402 1403 1404 1405 1406
    {.type = TSDB_DATA_TYPE_BLOB,
     .bytes = 1,
     .isVarLen = 1,
     .startFn = tCompBinaryStart,
     .cmprFn = tCompBinary,
H
Hongze Cheng 已提交
1407
     .endFn = tCompBinaryEnd,
H
Hongze Cheng 已提交
1408 1409
     .getI64 = NULL,
     .putI64 = NULL},
H
Hongze Cheng 已提交
1410 1411 1412 1413 1414
    {.type = TSDB_DATA_TYPE_MEDIUMBLOB,
     .bytes = 1,
     .isVarLen = 1,
     .startFn = tCompBinaryStart,
     .cmprFn = tCompBinary,
H
Hongze Cheng 已提交
1415
     .endFn = tCompBinaryEnd,
H
Hongze Cheng 已提交
1416 1417
     .getI64 = NULL,
     .putI64 = NULL},
H
Hongze Cheng 已提交
1418 1419 1420
};

struct SCompressor {
H
Hongze Cheng 已提交
1421 1422
  int8_t   type;
  int8_t   cmprAlg;
H
Hongze Cheng 已提交
1423
  int8_t   autoAlloc;
H
Hongze Cheng 已提交
1424
  int32_t  nVal;
H
Hongze Cheng 已提交
1425 1426 1427
  uint8_t *pBuf;
  int32_t  nBuf;
  uint8_t *aBuf[1];
H
Hongze Cheng 已提交
1428 1429 1430 1431 1432 1433 1434 1435 1436
  union {
    // Timestamp ----
    struct {
      int64_t  ts_prev_val;
      int64_t  ts_prev_delta;
      uint8_t *ts_flag_p;
    };
    // Integer ----
    struct {
H
Hongze Cheng 已提交
1437 1438 1439 1440
      int64_t  i_prev;
      int32_t  i_selector;
      int32_t  i_start;
      int32_t  i_end;
H
Hongze Cheng 已提交
1441
      int32_t  i_nEle;
H
Hongze Cheng 已提交
1442 1443
      uint64_t i_aZigzag[241];
      int8_t   i_aBitN[241];
H
Hongze Cheng 已提交
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
    };
    // Float ----
    struct {
      uint32_t f_prev;
      uint8_t *f_flag_p;
    };
    // Double ----
    struct {
      uint64_t d_prev;
      uint8_t *d_flag_p;
    };
  };
H
Hongze Cheng 已提交
1456
};
H
Hongze Cheng 已提交
1457

H
Hongze Cheng 已提交
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
static int32_t tTwoStageComp(SCompressor *pCmprsor, int32_t *szComp) {
  int32_t code = 0;

  if (pCmprsor->autoAlloc && (code = tRealloc(&pCmprsor->aBuf[0], pCmprsor->nBuf + 1))) {
    return code;
  }

  *szComp = LZ4_compress_default(pCmprsor->pBuf, pCmprsor->aBuf[0] + 1, pCmprsor->nBuf, pCmprsor->nBuf);
  if (*szComp && *szComp < pCmprsor->nBuf) {
    pCmprsor->aBuf[0][0] = 1;
    *szComp += 1;
  } else {
    pCmprsor->aBuf[0][0] = 0;
    memcpy(pCmprsor->aBuf[0] + 1, pCmprsor->pBuf, pCmprsor->nBuf);
    *szComp = pCmprsor->nBuf + 1;
  }

  return code;
}

H
Hongze Cheng 已提交
1478
// Timestamp =====================================================
H
Hongze Cheng 已提交
1479
static int32_t tCompTimestampStart(SCompressor *pCmprsor, int8_t type, int8_t cmprAlg) {
H
Hongze Cheng 已提交
1480
  int32_t code = 0;
H
Hongze Cheng 已提交
1481 1482 1483 1484 1485 1486 1487 1488

  pCmprsor->nBuf = 1;

  code = tRealloc(&pCmprsor->pBuf, pCmprsor->nBuf);
  if (code) return code;

  pCmprsor->pBuf[0] = 1;

H
Hongze Cheng 已提交
1489 1490 1491
  return code;
}

H
Hongze Cheng 已提交
1492
static int32_t tCompTSSwitchToCopy(SCompressor *pCmprsor) {
H
Hongze Cheng 已提交
1493 1494
  int32_t code = 0;

H
Hongze Cheng 已提交
1495
  if (pCmprsor->nVal == 0) goto _exit;
H
Hongze Cheng 已提交
1496

H
Hongze Cheng 已提交
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
  if (pCmprsor->autoAlloc && (code = tRealloc(&pCmprsor->aBuf[0], sizeof(int64_t) * pCmprsor->nVal + 1))) {
    return code;
  }

  int32_t n = 1;
  int32_t nBuf = 1;
  int64_t value;
  int64_t delta;
  for (int32_t iVal = 0; iVal < pCmprsor->nVal;) {
    uint8_t aN[2] = {(pCmprsor->pBuf[n] & 0xf), (pCmprsor->pBuf[n] >> 4)};
H
Hongze Cheng 已提交
1507

H
Hongze Cheng 已提交
1508
    n++;
H
Hongze Cheng 已提交
1509

H
Hongze Cheng 已提交
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
    for (int32_t i = 0; i < 2; i++) {
      uint64_t vZigzag = 0;
      for (uint8_t j = 0; j < aN[i]; j++) {
        vZigzag |= (((uint64_t)pCmprsor->pBuf[n]) << (8 * j));
        n++;
      }

      int64_t delta_of_delta = ZIGZAG_DECODE(int64_t, vZigzag);
      if (iVal) {
        delta = delta_of_delta + delta;
        value = delta + value;
      } else {
        delta = 0;
        value = delta_of_delta;
H
Hongze Cheng 已提交
1524 1525
      }

H
Hongze Cheng 已提交
1526 1527
      memcpy(pCmprsor->aBuf[0] + nBuf, &value, sizeof(value));
      nBuf += sizeof(int64_t);
H
Hongze Cheng 已提交
1528

H
Hongze Cheng 已提交
1529 1530
      iVal++;
      if (iVal >= pCmprsor->nVal) break;
H
Hongze Cheng 已提交
1531 1532
    }
  }
H
Hongze Cheng 已提交
1533

H
Hongze Cheng 已提交
1534
  ASSERT(n == pCmprsor->nBuf && nBuf == sizeof(int64_t) * pCmprsor->nVal + 1);
H
Hongze Cheng 已提交
1535 1536 1537 1538 1539 1540 1541 1542

  uint8_t *pBuf = pCmprsor->pBuf;
  pCmprsor->pBuf = pCmprsor->aBuf[0];
  pCmprsor->aBuf[0] = pBuf;
  pCmprsor->nBuf = nBuf;

_exit:
  pCmprsor->pBuf[0] = 0;
H
Hongze Cheng 已提交
1543 1544
  return code;
}
H
Hongze Cheng 已提交
1545
static int32_t tCompTimestamp(SCompressor *pCmprsor, const void *pData, int32_t nData) {
H
Hongze Cheng 已提交
1546 1547
  int32_t code = 0;

H
Hongze Cheng 已提交
1548 1549
  int64_t ts = *(int64_t *)pData;
  ASSERT(nData == 8);
H
Hongze Cheng 已提交
1550

H
Hongze Cheng 已提交
1551
  if (pCmprsor->pBuf[0] == 1) {
H
Hongze Cheng 已提交
1552
    if (pCmprsor->nVal == 0) {
H
Hongze Cheng 已提交
1553 1554 1555
      pCmprsor->ts_prev_val = ts;
      pCmprsor->ts_prev_delta = -ts;
    }
H
Hongze Cheng 已提交
1556

H
Hongze Cheng 已提交
1557
    if (!I64_SAFE_ADD(ts, -pCmprsor->ts_prev_val)) {
H
Hongze Cheng 已提交
1558
      code = tCompTSSwitchToCopy(pCmprsor);
H
Hongze Cheng 已提交
1559 1560 1561 1562
      if (code) return code;
      goto _copy_cmpr;
    }
    int64_t delta = ts - pCmprsor->ts_prev_val;
H
Hongze Cheng 已提交
1563

H
Hongze Cheng 已提交
1564
    if (!I64_SAFE_ADD(delta, -pCmprsor->ts_prev_delta)) {
H
Hongze Cheng 已提交
1565
      code = tCompTSSwitchToCopy(pCmprsor);
H
Hongze Cheng 已提交
1566 1567 1568 1569 1570
      if (code) return code;
      goto _copy_cmpr;
    }
    int64_t  delta_of_delta = delta - pCmprsor->ts_prev_delta;
    uint64_t vZigzag = ZIGZAG_ENCODE(int64_t, delta_of_delta);
H
Hongze Cheng 已提交
1571

H
Hongze Cheng 已提交
1572 1573
    pCmprsor->ts_prev_val = ts;
    pCmprsor->ts_prev_delta = delta;
H
Hongze Cheng 已提交
1574

H
Hongze Cheng 已提交
1575
    if ((pCmprsor->nVal & 0x1) == 0) {
H
Hongze Cheng 已提交
1576 1577
      if (pCmprsor->autoAlloc && (code = tRealloc(&pCmprsor->pBuf, pCmprsor->nBuf + 17))) {
        return code;
H
Hongze Cheng 已提交
1578
      }
H
Hongze Cheng 已提交
1579

H
Hongze Cheng 已提交
1580 1581
      pCmprsor->ts_flag_p = pCmprsor->pBuf + pCmprsor->nBuf;
      pCmprsor->nBuf++;
H
Hongze Cheng 已提交
1582 1583
      pCmprsor->ts_flag_p[0] = 0;
      while (vZigzag) {
H
Hongze Cheng 已提交
1584 1585
        pCmprsor->pBuf[pCmprsor->nBuf] = (vZigzag & 0xff);
        pCmprsor->nBuf++;
H
Hongze Cheng 已提交
1586 1587 1588 1589 1590
        pCmprsor->ts_flag_p[0]++;
        vZigzag >>= 8;
      }
    } else {
      while (vZigzag) {
H
Hongze Cheng 已提交
1591 1592
        pCmprsor->pBuf[pCmprsor->nBuf] = (vZigzag & 0xff);
        pCmprsor->nBuf++;
H
Hongze Cheng 已提交
1593 1594 1595
        pCmprsor->ts_flag_p[0] += 0x10;
        vZigzag >>= 8;
      }
H
Hongze Cheng 已提交
1596 1597
    }
  } else {
H
Hongze Cheng 已提交
1598
  _copy_cmpr:
H
Hongze Cheng 已提交
1599 1600
    if (pCmprsor->autoAlloc && (code = tRealloc(&pCmprsor->pBuf, pCmprsor->nBuf + sizeof(ts)))) {
      return code;
H
Hongze Cheng 已提交
1601 1602
    }

H
Hongze Cheng 已提交
1603 1604
    memcpy(pCmprsor->pBuf + pCmprsor->nBuf, &ts, sizeof(ts));
    pCmprsor->nBuf += sizeof(ts);
H
Hongze Cheng 已提交
1605
  }
H
Hongze Cheng 已提交
1606
  pCmprsor->nVal++;
H
Hongze Cheng 已提交
1607 1608 1609 1610

  return code;
}

H
Hongze Cheng 已提交
1611 1612
static int32_t tCompTimestampEnd(SCompressor *pCmprsor, const uint8_t **ppData, int32_t *nData) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1613 1614

  if (pCmprsor->nBuf >= sizeof(int64_t) * pCmprsor->nVal + 1 && pCmprsor->pBuf[0] == 1) {
H
Hongze Cheng 已提交
1615
    code = tCompTSSwitchToCopy(pCmprsor);
H
Hongze Cheng 已提交
1616 1617 1618 1619
    if (code) return code;
  }

  if (pCmprsor->cmprAlg == TWO_STAGE_COMP) {
H
Hongze Cheng 已提交
1620 1621
    code = tTwoStageComp(pCmprsor, nData);
    if (code) return code;
H
Hongze Cheng 已提交
1622 1623 1624 1625 1626 1627 1628 1629
    *ppData = pCmprsor->aBuf[0];
  } else if (pCmprsor->cmprAlg == ONE_STAGE_COMP) {
    *ppData = pCmprsor->pBuf;
    *nData = pCmprsor->nBuf;
  } else {
    ASSERT(0);
  }

H
Hongze Cheng 已提交
1630 1631 1632
  return code;
}

H
Hongze Cheng 已提交
1633 1634
// Integer =====================================================
#define SIMPLE8B_MAX ((uint64_t)1152921504606846974LL)
H
Hongze Cheng 已提交
1635
static const uint8_t BIT_PER_INTEGER[] = {0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 15, 20, 30, 60};
H
Hongze Cheng 已提交
1636
static const int32_t SELECTOR_TO_ELEMS[] = {240, 120, 60, 30, 20, 15, 12, 10, 8, 7, 6, 5, 4, 3, 2, 1};
H
Hongze Cheng 已提交
1637
static const uint8_t BIT_TO_SELECTOR[] = {0,  2,  3,  4,  5,  6,  7,  8,  9,  10, 10, 11, 11, 12, 12, 12,
H
Hongze Cheng 已提交
1638 1639 1640
                                          13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15,
                                          15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
                                          15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15};
H
Hongze Cheng 已提交
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
static const int32_t NEXT_IDX[] = {
    1,   2,   3,   4,   5,   6,   7,   8,   9,   10,  11,  12,  13,  14,  15,  16,  17,  18,  19,  20,  21,  22,
    23,  24,  25,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  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,  100, 101, 102, 103, 104, 105, 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, 131, 132,
    133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 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, 0};
H
Hongze Cheng 已提交
1653

H
Hongze Cheng 已提交
1654
static int32_t tCompIntStart(SCompressor *pCmprsor, int8_t type, int8_t cmprAlg) {
H
Hongze Cheng 已提交
1655
  int32_t code = 0;
H
Hongze Cheng 已提交
1656

H
Hongze Cheng 已提交
1657 1658 1659 1660
  pCmprsor->i_prev = 0;
  pCmprsor->i_selector = 0;
  pCmprsor->i_start = 0;
  pCmprsor->i_end = 0;
H
Hongze Cheng 已提交
1661
  pCmprsor->i_nEle = 0;
H
Hongze Cheng 已提交
1662 1663
  pCmprsor->nBuf = 1;

H
Hongze Cheng 已提交
1664
  code = tRealloc(&pCmprsor->pBuf, pCmprsor->nBuf);
H
Hongze Cheng 已提交
1665 1666 1667 1668 1669 1670 1671 1672 1673
  if (code) return code;

  pCmprsor->pBuf[0] = 0;

  return code;
}

static int32_t tCompIntSwitchToCopy(SCompressor *pCmprsor) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1674 1675 1676

  if (pCmprsor->nVal == 0) goto _exit;

H
Hongze Cheng 已提交
1677 1678 1679 1680 1681 1682 1683 1684
  int32_t size = DATA_TYPE_INFO[pCmprsor->type].bytes * pCmprsor->nVal + 1;
  if (pCmprsor->autoAlloc && (code = tRealloc(&pCmprsor->aBuf[0], size))) {
    return code;
  }

  int32_t n = 1;
  int32_t nBuf = 1;
  int64_t vPrev = 0;
H
Hongze Cheng 已提交
1685
  while (n < pCmprsor->nBuf) {
H
Hongze Cheng 已提交
1686 1687 1688 1689
    uint64_t b;
    memcpy(&b, pCmprsor->pBuf + n, sizeof(b));
    n += sizeof(b);

H
Hongze Cheng 已提交
1690 1691 1692 1693
    int32_t  i_selector = (b & 0xf);
    int32_t  nEle = SELECTOR_TO_ELEMS[i_selector];
    uint8_t  bits = BIT_PER_INTEGER[i_selector];
    uint64_t mask = (((uint64_t)1) << bits) - 1;
H
Hongze Cheng 已提交
1694
    for (int32_t iEle = 0; iEle < nEle; iEle++) {
H
Hongze Cheng 已提交
1695
      uint64_t vZigzag = (b >> (bits * iEle + 4)) & mask;
H
Hongze Cheng 已提交
1696
      vPrev = ZIGZAG_DECODE(int64_t, vZigzag) + vPrev;
H
Hongze Cheng 已提交
1697

H
Hongze Cheng 已提交
1698
      DATA_TYPE_INFO[pCmprsor->type].putI64(vPrev, pCmprsor->aBuf[0] + nBuf);
H
Hongze Cheng 已提交
1699 1700
      nBuf += DATA_TYPE_INFO[pCmprsor->type].bytes;
    }
H
Hongze Cheng 已提交
1701 1702
  }

H
Hongze Cheng 已提交
1703
  while (pCmprsor->i_nEle) {
H
Hongze Cheng 已提交
1704 1705 1706 1707
    vPrev = ZIGZAG_DECODE(int64_t, pCmprsor->i_aZigzag[pCmprsor->i_start]) + vPrev;

    memcpy(pCmprsor->aBuf[0] + nBuf, &vPrev, DATA_TYPE_INFO[pCmprsor->type].bytes);
    nBuf += DATA_TYPE_INFO[pCmprsor->type].bytes;
H
Hongze Cheng 已提交
1708 1709 1710

    pCmprsor->i_start = NEXT_IDX[pCmprsor->i_start];
    pCmprsor->i_nEle--;
H
Hongze Cheng 已提交
1711 1712 1713 1714 1715 1716 1717 1718
  }

  ASSERT(n == pCmprsor->nBuf && nBuf == size);

  uint8_t *pBuf = pCmprsor->pBuf;
  pCmprsor->pBuf = pCmprsor->aBuf[0];
  pCmprsor->aBuf[0] = pBuf;
  pCmprsor->nBuf = size;
H
Hongze Cheng 已提交
1719 1720 1721

_exit:
  pCmprsor->pBuf[0] = 1;
H
Hongze Cheng 已提交
1722 1723 1724
  return code;
}

H
Hongze Cheng 已提交
1725
static int32_t tCompInt(SCompressor *pCmprsor, const void *pData, int32_t nData) {
H
Hongze Cheng 已提交
1726 1727
  int32_t code = 0;

H
Hongze Cheng 已提交
1728
  ASSERT(nData == DATA_TYPE_INFO[pCmprsor->type].bytes);
H
Hongze Cheng 已提交
1729

H
Hongze Cheng 已提交
1730
  if (pCmprsor->pBuf[0] == 0) {
H
Hongze Cheng 已提交
1731
    int64_t val = DATA_TYPE_INFO[pCmprsor->type].getI64(pData);
H
Hongze Cheng 已提交
1732

H
Hongze Cheng 已提交
1733
    if (!I64_SAFE_ADD(val, -pCmprsor->i_prev)) {
H
Hongze Cheng 已提交
1734 1735
      code = tCompIntSwitchToCopy(pCmprsor);
      if (code) return code;
H
Hongze Cheng 已提交
1736 1737
      goto _copy_cmpr;
    }
H
Hongze Cheng 已提交
1738

H
Hongze Cheng 已提交
1739 1740 1741
    int64_t  diff = val - pCmprsor->i_prev;
    uint64_t vZigzag = ZIGZAG_ENCODE(int64_t, diff);
    if (vZigzag >= SIMPLE8B_MAX) {
H
Hongze Cheng 已提交
1742 1743
      code = tCompIntSwitchToCopy(pCmprsor);
      if (code) return code;
H
Hongze Cheng 已提交
1744
      goto _copy_cmpr;
H
Hongze Cheng 已提交
1745 1746
    }

H
Hongze Cheng 已提交
1747 1748
    int8_t nBit = (vZigzag) ? (64 - BUILDIN_CLZL(vZigzag)) : 0;
    pCmprsor->i_prev = val;
H
Hongze Cheng 已提交
1749

H
Hongze Cheng 已提交
1750
    for (;;) {
H
Hongze Cheng 已提交
1751 1752
      if (pCmprsor->i_nEle + 1 <= SELECTOR_TO_ELEMS[pCmprsor->i_selector] &&
          pCmprsor->i_nEle + 1 <= SELECTOR_TO_ELEMS[BIT_TO_SELECTOR[nBit]]) {
H
Hongze Cheng 已提交
1753 1754 1755 1756 1757
        if (pCmprsor->i_selector < BIT_TO_SELECTOR[nBit]) {
          pCmprsor->i_selector = BIT_TO_SELECTOR[nBit];
        }
        pCmprsor->i_aZigzag[pCmprsor->i_end] = vZigzag;
        pCmprsor->i_aBitN[pCmprsor->i_end] = nBit;
H
Hongze Cheng 已提交
1758 1759
        pCmprsor->i_end = NEXT_IDX[pCmprsor->i_end];
        pCmprsor->i_nEle++;
H
Hongze Cheng 已提交
1760 1761
        break;
      } else {
H
Hongze Cheng 已提交
1762
        if (pCmprsor->i_nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
H
Hongze Cheng 已提交
1763 1764 1765 1766 1767
          int32_t lidx = pCmprsor->i_selector + 1;
          int32_t ridx = 15;
          while (lidx <= ridx) {
            pCmprsor->i_selector = (lidx + ridx) >> 1;

H
Hongze Cheng 已提交
1768
            if (pCmprsor->i_nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
H
Hongze Cheng 已提交
1769
              lidx = pCmprsor->i_selector + 1;
H
Hongze Cheng 已提交
1770
            } else if (pCmprsor->i_nEle > SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
H
Hongze Cheng 已提交
1771 1772 1773 1774 1775 1776
              ridx = pCmprsor->i_selector - 1;
            } else {
              break;
            }
          }

H
Hongze Cheng 已提交
1777
          if (pCmprsor->i_nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) pCmprsor->i_selector++;
H
Hongze Cheng 已提交
1778
        }
H
Hongze Cheng 已提交
1779
        int32_t nEle = SELECTOR_TO_ELEMS[pCmprsor->i_selector];
H
Hongze Cheng 已提交
1780

H
Hongze Cheng 已提交
1781 1782
        if (pCmprsor->autoAlloc && (code = tRealloc(&pCmprsor->pBuf, pCmprsor->nBuf + sizeof(uint64_t)))) {
          return code;
H
Hongze Cheng 已提交
1783
        }
H
Hongze Cheng 已提交
1784

H
Hongze Cheng 已提交
1785 1786
        uint64_t *bp = (uint64_t *)(pCmprsor->pBuf + pCmprsor->nBuf);
        pCmprsor->nBuf += sizeof(uint64_t);
H
Hongze Cheng 已提交
1787 1788 1789
        bp[0] = pCmprsor->i_selector;
        uint8_t bits = BIT_PER_INTEGER[pCmprsor->i_selector];
        for (int32_t iVal = 0; iVal < nEle; iVal++) {
H
Hongze Cheng 已提交
1790 1791 1792
          bp[0] |= (pCmprsor->i_aZigzag[pCmprsor->i_start] << (bits * iVal + 4));
          pCmprsor->i_start = NEXT_IDX[pCmprsor->i_start];
          pCmprsor->i_nEle--;
H
Hongze Cheng 已提交
1793
        }
H
Hongze Cheng 已提交
1794 1795 1796

        // reset and continue
        pCmprsor->i_selector = 0;
H
Hongze Cheng 已提交
1797
        for (int32_t iVal = pCmprsor->i_start; iVal < pCmprsor->i_end; iVal = NEXT_IDX[iVal]) {
H
Hongze Cheng 已提交
1798 1799 1800 1801 1802
          if (pCmprsor->i_selector < BIT_TO_SELECTOR[pCmprsor->i_aBitN[iVal]]) {
            pCmprsor->i_selector = BIT_TO_SELECTOR[pCmprsor->i_aBitN[iVal]];
          }
        }
      }
H
Hongze Cheng 已提交
1803 1804 1805
    }
  } else {
  _copy_cmpr:
H
Hongze Cheng 已提交
1806
    code = tRealloc(&pCmprsor->pBuf, pCmprsor->nBuf + nData);
H
Hongze Cheng 已提交
1807 1808
    if (code) return code;

H
Hongze Cheng 已提交
1809 1810
    memcpy(pCmprsor->pBuf + pCmprsor->nBuf, pData, nData);
    pCmprsor->nBuf += nData;
H
Hongze Cheng 已提交
1811 1812
  }
  pCmprsor->nVal++;
H
Hongze Cheng 已提交
1813 1814 1815 1816

  return code;
}

H
Hongze Cheng 已提交
1817
static int32_t tCompIntEnd(SCompressor *pCmprsor, const uint8_t **ppData, int32_t *nData) {
H
Hongze Cheng 已提交
1818
  int32_t code = 0;
H
Hongze Cheng 已提交
1819

H
Hongze Cheng 已提交
1820 1821
  for (; pCmprsor->i_nEle;) {
    if (pCmprsor->i_nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
H
Hongze Cheng 已提交
1822 1823 1824 1825 1826
      int32_t lidx = pCmprsor->i_selector + 1;
      int32_t ridx = 15;
      while (lidx <= ridx) {
        pCmprsor->i_selector = (lidx + ridx) >> 1;

H
Hongze Cheng 已提交
1827
        if (pCmprsor->i_nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
H
Hongze Cheng 已提交
1828
          lidx = pCmprsor->i_selector + 1;
H
Hongze Cheng 已提交
1829
        } else if (pCmprsor->i_nEle > SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
H
Hongze Cheng 已提交
1830 1831 1832 1833 1834 1835
          ridx = pCmprsor->i_selector - 1;
        } else {
          break;
        }
      }

H
Hongze Cheng 已提交
1836
      if (pCmprsor->i_nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) pCmprsor->i_selector++;
H
Hongze Cheng 已提交
1837
    }
H
Hongze Cheng 已提交
1838
    int32_t nEle = SELECTOR_TO_ELEMS[pCmprsor->i_selector];
H
Hongze Cheng 已提交
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848

    if (pCmprsor->autoAlloc && (code = tRealloc(&pCmprsor->pBuf, pCmprsor->nBuf + sizeof(uint64_t)))) {
      return code;
    }

    uint64_t *bp = (uint64_t *)(pCmprsor->pBuf + pCmprsor->nBuf);
    pCmprsor->nBuf += sizeof(uint64_t);
    bp[0] = pCmprsor->i_selector;
    uint8_t bits = BIT_PER_INTEGER[pCmprsor->i_selector];
    for (int32_t iVal = 0; iVal < nEle; iVal++) {
H
Hongze Cheng 已提交
1849 1850 1851
      bp[0] |= (pCmprsor->i_aZigzag[pCmprsor->i_start] << (bits * iVal + 4));
      pCmprsor->i_start = NEXT_IDX[pCmprsor->i_start];
      pCmprsor->i_nEle--;
H
Hongze Cheng 已提交
1852 1853 1854 1855 1856
    }

    pCmprsor->i_selector = 0;
  }

H
Hongze Cheng 已提交
1857 1858 1859 1860 1861 1862
  if (pCmprsor->nBuf >= DATA_TYPE_INFO[pCmprsor->type].bytes * pCmprsor->nVal + 1 && pCmprsor->pBuf[0] == 0) {
    code = tCompIntSwitchToCopy(pCmprsor);
    if (code) return code;
  }

  if (pCmprsor->cmprAlg == TWO_STAGE_COMP) {
H
Hongze Cheng 已提交
1863 1864
    code = tTwoStageComp(pCmprsor, nData);
    if (code) return code;
H
Hongze Cheng 已提交
1865 1866 1867 1868 1869 1870 1871 1872
    *ppData = pCmprsor->aBuf[0];
  } else if (pCmprsor->cmprAlg == ONE_STAGE_COMP) {
    *ppData = pCmprsor->pBuf;
    *nData = pCmprsor->nBuf;
  } else {
    ASSERT(0);
  }

H
Hongze Cheng 已提交
1873 1874 1875
  return code;
}

H
Hongze Cheng 已提交
1876 1877 1878
// Float =====================================================
static int32_t tCompFloatStart(SCompressor *pCmprsor, int8_t type, int8_t cmprAlg) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1879

H
Hongze Cheng 已提交
1880 1881
  pCmprsor->f_prev = 0;
  pCmprsor->f_flag_p = NULL;
H
Hongze Cheng 已提交
1882 1883 1884 1885 1886 1887 1888 1889

  pCmprsor->nBuf = 1;

  code = tRealloc(&pCmprsor->pBuf, pCmprsor->nBuf);
  if (code) return code;

  pCmprsor->pBuf[0] = 0;

H
Hongze Cheng 已提交
1890 1891 1892
  return code;
}

H
Hongze Cheng 已提交
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
static int32_t tCompFloatSwitchToCopy(SCompressor *pCmprsor) {
  int32_t code = 0;

  if (pCmprsor->nVal == 0) goto _exit;

  if (pCmprsor->autoAlloc && (code = tRealloc(&pCmprsor->aBuf[0], sizeof(float) * pCmprsor->nVal + 1))) {
    return code;
  }

  int32_t n = 1;
  int32_t nBuf = 1;
  union {
    float    f;
    uint32_t u;
  } val = {.u = 0};

  for (int32_t iVal = 0; iVal < pCmprsor->nVal;) {
    uint8_t flags[2] = {(pCmprsor->pBuf[n] & 0xf), (pCmprsor->pBuf[n] >> 4)};

    n++;

    for (int8_t i = 0; i < 2; i++) {
      uint8_t flag = flags[i];

      uint32_t diff = 0;
      int8_t   nBytes = (flag & 0x7) + 1;
      for (int j = 0; j < nBytes; j++) {
        diff |= (((uint32_t)pCmprsor->pBuf[n]) << (8 * j));
        n++;
      }

      if (flag & 0x8) {
        diff <<= (32 - nBytes * 8);
      }

      val.u ^= diff;

      memcpy(pCmprsor->aBuf[0] + nBuf, &val.f, sizeof(val));
      nBuf += sizeof(val);

      iVal++;
      if (iVal >= pCmprsor->nVal) break;
    }
  }
  uint8_t *pBuf = pCmprsor->pBuf;
  pCmprsor->pBuf = pCmprsor->aBuf[0];
  pCmprsor->aBuf[0] = pBuf;
  pCmprsor->nBuf = nBuf;

_exit:
  pCmprsor->pBuf[0] = 1;
  return code;
}
H
Hongze Cheng 已提交
1946
static int32_t tCompFloat(SCompressor *pCmprsor, const void *pData, int32_t nData) {
H
Hongze Cheng 已提交
1947 1948
  int32_t code = 0;

H
Hongze Cheng 已提交
1949 1950
  ASSERT(nData == sizeof(float));

H
Hongze Cheng 已提交
1951 1952 1953
  union {
    float    f;
    uint32_t u;
H
Hongze Cheng 已提交
1954
  } val = {.f = *(float *)pData};
H
Hongze Cheng 已提交
1955 1956 1957 1958 1959 1960 1961 1962 1963

  uint32_t diff = val.u ^ pCmprsor->f_prev;
  pCmprsor->f_prev = val.u;

  int32_t clz, ctz;
  if (diff) {
    clz = BUILDIN_CLZ(diff);
    ctz = BUILDIN_CTZ(diff);
  } else {
H
Hongze Cheng 已提交
1964 1965
    clz = 32;
    ctz = 32;
H
Hongze Cheng 已提交
1966 1967
  }

H
Hongze Cheng 已提交
1968 1969 1970 1971 1972 1973 1974 1975 1976
  uint8_t nBytes;
  if (clz < ctz) {
    nBytes = sizeof(uint32_t) - ctz / BITS_PER_BYTE;
    if (nBytes) diff >>= (32 - nBytes * BITS_PER_BYTE);
  } else {
    nBytes = sizeof(uint32_t) - clz / BITS_PER_BYTE;
  }
  if (nBytes == 0) nBytes++;

H
Hongze Cheng 已提交
1977
  if ((pCmprsor->nVal & 0x1) == 0) {
H
Hongze Cheng 已提交
1978 1979
    if (pCmprsor->autoAlloc && (code = tRealloc(&pCmprsor->pBuf, pCmprsor->nBuf + 9))) {
      return code;
H
Hongze Cheng 已提交
1980
    }
H
Hongze Cheng 已提交
1981

H
Hongze Cheng 已提交
1982 1983
    pCmprsor->f_flag_p = &pCmprsor->pBuf[pCmprsor->nBuf];
    pCmprsor->nBuf++;
H
Hongze Cheng 已提交
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996

    if (clz < ctz) {
      pCmprsor->f_flag_p[0] = (0x08 | (nBytes - 1));
    } else {
      pCmprsor->f_flag_p[0] = nBytes - 1;
    }
  } else {
    if (clz < ctz) {
      pCmprsor->f_flag_p[0] |= ((0x08 | (nBytes - 1)) << 4);
    } else {
      pCmprsor->f_flag_p[0] |= ((nBytes - 1) << 4);
    }
  }
H
Hongze Cheng 已提交
1997
  for (; nBytes; nBytes--) {
H
Hongze Cheng 已提交
1998 1999
    pCmprsor->pBuf[pCmprsor->nBuf] = (diff & 0xff);
    pCmprsor->nBuf++;
H
Hongze Cheng 已提交
2000 2001
    diff >>= BITS_PER_BYTE;
  }
H
Hongze Cheng 已提交
2002
  pCmprsor->nVal++;
H
Hongze Cheng 已提交
2003

H
Hongze Cheng 已提交
2004 2005 2006
  return code;
}

H
Hongze Cheng 已提交
2007
static int32_t tCompFloatEnd(SCompressor *pCmprsor, const uint8_t **ppData, int32_t *nData) {
H
Hongze Cheng 已提交
2008
  int32_t code = 0;
H
Hongze Cheng 已提交
2009 2010 2011 2012 2013 2014 2015

  if (pCmprsor->nBuf >= sizeof(float) * pCmprsor->nVal + 1) {
    code = tCompFloatSwitchToCopy(pCmprsor);
    if (code) return code;
  }

  if (pCmprsor->cmprAlg == TWO_STAGE_COMP) {
H
Hongze Cheng 已提交
2016 2017
    code = tTwoStageComp(pCmprsor, nData);
    if (code) return code;
H
Hongze Cheng 已提交
2018 2019 2020 2021 2022 2023 2024 2025
    *ppData = pCmprsor->aBuf[0];
  } else if (pCmprsor->cmprAlg == ONE_STAGE_COMP) {
    *ppData = pCmprsor->pBuf;
    *nData = pCmprsor->nBuf;
  } else {
    ASSERT(0);
  }

H
Hongze Cheng 已提交
2026 2027 2028
  return code;
}

H
Hongze Cheng 已提交
2029 2030 2031
// Double =====================================================
static int32_t tCompDoubleStart(SCompressor *pCmprsor, int8_t type, int8_t cmprAlg) {
  int32_t code = 0;
H
Hongze Cheng 已提交
2032

H
Hongze Cheng 已提交
2033 2034
  pCmprsor->d_prev = 0;
  pCmprsor->d_flag_p = NULL;
H
Hongze Cheng 已提交
2035 2036 2037 2038 2039 2040 2041 2042

  pCmprsor->nBuf = 1;

  code = tRealloc(&pCmprsor->pBuf, pCmprsor->nBuf);
  if (code) return code;

  pCmprsor->pBuf[0] = 0;

H
Hongze Cheng 已提交
2043 2044 2045
  return code;
}

H
Hongze Cheng 已提交
2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
static int32_t tCompDoubleSwitchToCopy(SCompressor *pCmprsor) {
  int32_t code = 0;

  if (pCmprsor->nVal == 0) goto _exit;

  if (pCmprsor->autoAlloc && (code = tRealloc(&pCmprsor->aBuf[0], sizeof(double) * pCmprsor->nVal + 1))) {
    return code;
  }

  int32_t n = 1;
  int32_t nBuf = 1;
  union {
    double   f;
    uint64_t u;
  } val = {.u = 0};

  for (int32_t iVal = 0; iVal < pCmprsor->nVal;) {
    uint8_t flags[2] = {(pCmprsor->pBuf[n] & 0xf), (pCmprsor->pBuf[n] >> 4)};

    n++;

    for (int8_t i = 0; i < 2; i++) {
      uint8_t flag = flags[i];

      uint64_t diff = 0;
      int8_t   nBytes = (flag & 0x7) + 1;
      for (int j = 0; j < nBytes; j++) {
        diff |= (((uint64_t)pCmprsor->pBuf[n]) << (8 * j));
        n++;
      }

      if (flag & 0x8) {
        diff <<= (64 - nBytes * 8);
      }

      val.u ^= diff;

      memcpy(pCmprsor->aBuf[0] + nBuf, &val.f, sizeof(val));
      nBuf += sizeof(val);

      iVal++;
      if (iVal >= pCmprsor->nVal) break;
    }
  }
  uint8_t *pBuf = pCmprsor->pBuf;
  pCmprsor->pBuf = pCmprsor->aBuf[0];
  pCmprsor->aBuf[0] = pBuf;
  pCmprsor->nBuf = nBuf;

_exit:
  pCmprsor->pBuf[0] = 1;
  return code;
}
H
Hongze Cheng 已提交
2099
static int32_t tCompDouble(SCompressor *pCmprsor, const void *pData, int32_t nData) {
H
Hongze Cheng 已提交
2100 2101
  int32_t code = 0;

H
Hongze Cheng 已提交
2102 2103
  ASSERT(nData == sizeof(double));

H
Hongze Cheng 已提交
2104 2105 2106
  union {
    double   d;
    uint64_t u;
H
Hongze Cheng 已提交
2107
  } val = {.d = *(double *)pData};
H
Hongze Cheng 已提交
2108 2109 2110 2111 2112 2113 2114 2115 2116

  uint64_t diff = val.u ^ pCmprsor->d_prev;
  pCmprsor->d_prev = val.u;

  int32_t clz, ctz;
  if (diff) {
    clz = BUILDIN_CLZL(diff);
    ctz = BUILDIN_CTZL(diff);
  } else {
H
Hongze Cheng 已提交
2117 2118
    clz = 64;
    ctz = 64;
H
Hongze Cheng 已提交
2119 2120
  }

H
Hongze Cheng 已提交
2121 2122 2123 2124 2125 2126 2127 2128 2129
  uint8_t nBytes;
  if (clz < ctz) {
    nBytes = sizeof(uint64_t) - ctz / BITS_PER_BYTE;
    if (nBytes) diff >>= (64 - nBytes * BITS_PER_BYTE);
  } else {
    nBytes = sizeof(uint64_t) - clz / BITS_PER_BYTE;
  }
  if (nBytes == 0) nBytes++;

H
Hongze Cheng 已提交
2130
  if ((pCmprsor->nVal & 0x1) == 0) {
H
Hongze Cheng 已提交
2131 2132
    if (pCmprsor->autoAlloc && (code = tRealloc(&pCmprsor->pBuf, pCmprsor->nBuf + 17))) {
      return code;
H
Hongze Cheng 已提交
2133
    }
H
Hongze Cheng 已提交
2134

H
Hongze Cheng 已提交
2135 2136
    pCmprsor->d_flag_p = &pCmprsor->pBuf[pCmprsor->nBuf];
    pCmprsor->nBuf++;
H
Hongze Cheng 已提交
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149

    if (clz < ctz) {
      pCmprsor->d_flag_p[0] = (0x08 | (nBytes - 1));
    } else {
      pCmprsor->d_flag_p[0] = nBytes - 1;
    }
  } else {
    if (clz < ctz) {
      pCmprsor->d_flag_p[0] |= ((0x08 | (nBytes - 1)) << 4);
    } else {
      pCmprsor->d_flag_p[0] |= ((nBytes - 1) << 4);
    }
  }
H
Hongze Cheng 已提交
2150
  for (; nBytes; nBytes--) {
H
Hongze Cheng 已提交
2151 2152
    pCmprsor->pBuf[pCmprsor->nBuf] = (diff & 0xff);
    pCmprsor->nBuf++;
H
Hongze Cheng 已提交
2153 2154
    diff >>= BITS_PER_BYTE;
  }
H
Hongze Cheng 已提交
2155
  pCmprsor->nVal++;
H
Hongze Cheng 已提交
2156

H
Hongze Cheng 已提交
2157 2158 2159
  return code;
}

H
Hongze Cheng 已提交
2160
static int32_t tCompDoubleEnd(SCompressor *pCmprsor, const uint8_t **ppData, int32_t *nData) {
H
Hongze Cheng 已提交
2161
  int32_t code = 0;
H
Hongze Cheng 已提交
2162 2163 2164 2165 2166 2167 2168

  if (pCmprsor->nBuf >= sizeof(double) * pCmprsor->nVal + 1) {
    code = tCompDoubleSwitchToCopy(pCmprsor);
    if (code) return code;
  }

  if (pCmprsor->cmprAlg == TWO_STAGE_COMP) {
H
Hongze Cheng 已提交
2169 2170
    code = tTwoStageComp(pCmprsor, nData);
    if (code) return code;
H
Hongze Cheng 已提交
2171 2172 2173 2174 2175 2176 2177 2178
    *ppData = pCmprsor->aBuf[0];
  } else if (pCmprsor->cmprAlg == ONE_STAGE_COMP) {
    *ppData = pCmprsor->pBuf;
    *nData = pCmprsor->nBuf;
  } else {
    ASSERT(0);
  }

H
Hongze Cheng 已提交
2179 2180 2181
  return code;
}

H
Hongze Cheng 已提交
2182 2183
// Binary =====================================================
static int32_t tCompBinaryStart(SCompressor *pCmprsor, int8_t type, int8_t cmprAlg) {
H
Hongze Cheng 已提交
2184 2185
  pCmprsor->nBuf = 1;
  return 0;
H
Hongze Cheng 已提交
2186 2187
}

H
Hongze Cheng 已提交
2188
static int32_t tCompBinary(SCompressor *pCmprsor, const void *pData, int32_t nData) {
H
Hongze Cheng 已提交
2189 2190 2191
  int32_t code = 0;

  if (nData) {
H
Hongze Cheng 已提交
2192 2193
    if (pCmprsor->autoAlloc && (code = tRealloc(&pCmprsor->pBuf, pCmprsor->nBuf + nData))) {
      return code;
H
Hongze Cheng 已提交
2194
    }
H
Hongze Cheng 已提交
2195

H
Hongze Cheng 已提交
2196 2197
    memcpy(pCmprsor->pBuf + pCmprsor->nBuf, pData, nData);
    pCmprsor->nBuf += nData;
H
Hongze Cheng 已提交
2198
  }
H
Hongze Cheng 已提交
2199
  pCmprsor->nVal++;
H
Hongze Cheng 已提交
2200 2201 2202 2203

  return code;
}

H
Hongze Cheng 已提交
2204 2205
static int32_t tCompBinaryEnd(SCompressor *pCmprsor, const uint8_t **ppData, int32_t *nData) {
  int32_t code = 0;
H
Hongze Cheng 已提交
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224

  if (pCmprsor->nBuf == 1) return code;

  if (pCmprsor->autoAlloc && (code = tRealloc(&pCmprsor->aBuf[0], pCmprsor->nBuf))) {
    return code;
  }

  int32_t szComp =
      LZ4_compress_default(pCmprsor->pBuf + 1, pCmprsor->aBuf[0] + 1, pCmprsor->nBuf - 1, pCmprsor->nBuf - 1);
  if (szComp && szComp < pCmprsor->nBuf - 1) {
    pCmprsor->aBuf[0][0] = 1;
    *ppData = pCmprsor->aBuf[0];
    *nData = szComp + 1;
  } else {
    pCmprsor->pBuf[0] = 0;
    *ppData = pCmprsor->pBuf;
    *nData = pCmprsor->nBuf;
  }

H
Hongze Cheng 已提交
2225 2226 2227
  return code;
}

H
Hongze Cheng 已提交
2228 2229 2230
// Bool =====================================================
static const uint8_t BOOL_CMPR_TABLE[] = {0b01, 0b0100, 0b010000, 0b01000000};

H
Hongze Cheng 已提交
2231
static int32_t tCompBoolStart(SCompressor *pCmprsor, int8_t type, int8_t cmprAlg) {
H
Hongze Cheng 已提交
2232 2233
  pCmprsor->nBuf = 0;
  return 0;
H
Hongze Cheng 已提交
2234 2235
}

H
Hongze Cheng 已提交
2236
static int32_t tCompBool(SCompressor *pCmprsor, const void *pData, int32_t nData) {
H
Hongze Cheng 已提交
2237 2238
  int32_t code = 0;

H
Hongze Cheng 已提交
2239 2240
  bool vBool = *(int8_t *)pData;

H
Hongze Cheng 已提交
2241
  int32_t mod4 = (pCmprsor->nVal & 3);
H
Hongze Cheng 已提交
2242
  if (mod4 == 0) {
H
Hongze Cheng 已提交
2243
    pCmprsor->nBuf++;
H
Hongze Cheng 已提交
2244

H
Hongze Cheng 已提交
2245 2246
    if (pCmprsor->autoAlloc && (code = tRealloc(&pCmprsor->pBuf, pCmprsor->nBuf))) {
      return code;
H
Hongze Cheng 已提交
2247 2248
    }

H
Hongze Cheng 已提交
2249
    pCmprsor->pBuf[pCmprsor->nBuf - 1] = 0;
H
Hongze Cheng 已提交
2250
  }
H
Hongze Cheng 已提交
2251
  if (vBool) {
H
Hongze Cheng 已提交
2252
    pCmprsor->pBuf[pCmprsor->nBuf - 1] |= BOOL_CMPR_TABLE[mod4];
H
Hongze Cheng 已提交
2253
  }
H
Hongze Cheng 已提交
2254
  pCmprsor->nVal++;
H
Hongze Cheng 已提交
2255 2256 2257 2258

  return code;
}

H
Hongze Cheng 已提交
2259 2260
static int32_t tCompBoolEnd(SCompressor *pCmprsor, const uint8_t **ppData, int32_t *nData) {
  int32_t code = 0;
H
Hongze Cheng 已提交
2261 2262

  if (pCmprsor->cmprAlg == TWO_STAGE_COMP) {
H
Hongze Cheng 已提交
2263 2264
    code = tTwoStageComp(pCmprsor, nData);
    if (code) return code;
H
Hongze Cheng 已提交
2265 2266 2267 2268 2269 2270 2271 2272
    *ppData = pCmprsor->aBuf[0];
  } else if (pCmprsor->cmprAlg == ONE_STAGE_COMP) {
    *ppData = pCmprsor->pBuf;
    *nData = pCmprsor->nBuf;
  } else {
    ASSERT(0);
  }

H
Hongze Cheng 已提交
2273 2274 2275
  return code;
}

H
Hongze Cheng 已提交
2276 2277 2278 2279 2280 2281 2282
// SCompressor =====================================================
int32_t tCompressorCreate(SCompressor **ppCmprsor) {
  int32_t code = 0;

  *ppCmprsor = (SCompressor *)taosMemoryCalloc(1, sizeof(SCompressor));
  if ((*ppCmprsor) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
2283
    return code;
H
Hongze Cheng 已提交
2284 2285 2286 2287 2288 2289 2290 2291
  }

  return code;
}

int32_t tCompressorDestroy(SCompressor *pCmprsor) {
  int32_t code = 0;

H
Hongze Cheng 已提交
2292 2293
  tFree(pCmprsor->pBuf);

H
Hongze Cheng 已提交
2294 2295 2296
  int32_t nBuf = sizeof(pCmprsor->aBuf) / sizeof(pCmprsor->aBuf[0]);
  for (int32_t iBuf = 0; iBuf < nBuf; iBuf++) {
    tFree(pCmprsor->aBuf[iBuf]);
H
Hongze Cheng 已提交
2297 2298
  }

H
Hongze Cheng 已提交
2299 2300
  taosMemoryFree(pCmprsor);

H
Hongze Cheng 已提交
2301 2302 2303
  return code;
}

H
Hongze Cheng 已提交
2304
int32_t tCompressStart(SCompressor *pCmprsor, int8_t type, int8_t cmprAlg) {
H
Hongze Cheng 已提交
2305 2306 2307 2308
  int32_t code = 0;

  pCmprsor->type = type;
  pCmprsor->cmprAlg = cmprAlg;
H
Hongze Cheng 已提交
2309
  pCmprsor->autoAlloc = 1;
H
Hongze Cheng 已提交
2310
  pCmprsor->nVal = 0;
H
Hongze Cheng 已提交
2311

H
Hongze Cheng 已提交
2312 2313
  if (DATA_TYPE_INFO[type].startFn) {
    DATA_TYPE_INFO[type].startFn(pCmprsor, type, cmprAlg);
H
Hongze Cheng 已提交
2314 2315 2316 2317 2318
  }

  return code;
}

H
Hongze Cheng 已提交
2319
int32_t tCompressEnd(SCompressor *pCmprsor, const uint8_t **ppOut, int32_t *nOut, int32_t *nOrigin) {
H
Hongze Cheng 已提交
2320 2321
  int32_t code = 0;

H
Hongze Cheng 已提交
2322 2323 2324 2325 2326 2327 2328 2329 2330
  *ppOut = NULL;
  *nOut = 0;
  if (nOrigin) {
    if (DATA_TYPE_INFO[pCmprsor->type].isVarLen) {
      *nOrigin = pCmprsor->nBuf - 1;
    } else {
      *nOrigin = pCmprsor->nVal * DATA_TYPE_INFO[pCmprsor->type].bytes;
    }
  }
H
Hongze Cheng 已提交
2331

H
Hongze Cheng 已提交
2332 2333
  if (pCmprsor->nVal == 0) return code;

H
Hongze Cheng 已提交
2334
  if (DATA_TYPE_INFO[pCmprsor->type].endFn) {
H
Hongze Cheng 已提交
2335
    return DATA_TYPE_INFO[pCmprsor->type].endFn(pCmprsor, ppOut, nOut);
H
Hongze Cheng 已提交
2336 2337 2338 2339 2340
  }

  return code;
}

H
Hongze Cheng 已提交
2341 2342
int32_t tCompress(SCompressor *pCmprsor, const void *pData, int64_t nData) {
  return DATA_TYPE_INFO[pCmprsor->type].cmprFn(pCmprsor, pData, nData);
H
Hongze Cheng 已提交
2343 2344 2345 2346 2347 2348
}

/*************************************************************************
 *                  REGULAR COMPRESSION
 *************************************************************************/
// Timestamp =====================================================
H
Hongze Cheng 已提交
2349 2350 2351 2352 2353 2354 2355
int32_t tsCompressTimestamp(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg, void *pBuf,
                            int32_t nBuf) {
  if (cmprAlg == ONE_STAGE_COMP) {
    return tsCompressTimestampImp(pIn, nEle, pOut);
  } else if (cmprAlg == TWO_STAGE_COMP) {
    int32_t len = tsCompressTimestampImp(pIn, nEle, pBuf);
    return tsCompressStringImp(pBuf, len, pOut, nOut);
H
Hongze Cheng 已提交
2356
  } else {
2357
    ASSERTS(0, "compress algo not one or two stage");
H
Hongze Cheng 已提交
2358 2359 2360 2361
    return -1;
  }
}

H
Hongze Cheng 已提交
2362 2363 2364 2365 2366 2367 2368
int32_t tsDecompressTimestamp(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg,
                              void *pBuf, int32_t nBuf) {
  if (cmprAlg == ONE_STAGE_COMP) {
    return tsDecompressTimestampImp(pIn, nEle, pOut);
  } else if (cmprAlg == TWO_STAGE_COMP) {
    if (tsDecompressStringImp(pIn, nIn, pBuf, nBuf) < 0) return -1;
    return tsDecompressTimestampImp(pBuf, nEle, pOut);
H
Hongze Cheng 已提交
2369
  } else {
2370
    ASSERTS(0, "compress algo invalid");
H
Hongze Cheng 已提交
2371 2372 2373 2374 2375
    return -1;
  }
}

// Float =====================================================
H
Hongze Cheng 已提交
2376 2377
int32_t tsCompressFloat(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg, void *pBuf,
                        int32_t nBuf) {
H
Hongze Cheng 已提交
2378 2379 2380
#ifdef TD_TSZ
  // lossy mode
  if (lossyFloat) {
H
Hongze Cheng 已提交
2381
    return tsCompressFloatLossyImp(pIn, nEle, pOut);
H
Hongze Cheng 已提交
2382 2383 2384
    // lossless mode
  } else {
#endif
H
Hongze Cheng 已提交
2385 2386 2387 2388 2389
    if (cmprAlg == ONE_STAGE_COMP) {
      return tsCompressFloatImp(pIn, nEle, pOut);
    } else if (cmprAlg == TWO_STAGE_COMP) {
      int32_t len = tsCompressFloatImp(pIn, nEle, pBuf);
      return tsCompressStringImp(pBuf, len, pOut, nOut);
H
Hongze Cheng 已提交
2390
    } else {
2391
      ASSERTS(0, "compress algo invalid");
H
Hongze Cheng 已提交
2392 2393 2394 2395 2396 2397 2398
      return -1;
    }
#ifdef TD_TSZ
  }
#endif
}

H
Hongze Cheng 已提交
2399 2400
int32_t tsDecompressFloat(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg, void *pBuf,
                          int32_t nBuf) {
H
Hongze Cheng 已提交
2401
#ifdef TD_TSZ
H
Hongze Cheng 已提交
2402
  if (HEAD_ALGO(pIn[0]) == ALGO_SZ_LOSSY) {
H
Hongze Cheng 已提交
2403
    // decompress lossy
H
Hongze Cheng 已提交
2404
    return tsDecompressFloatLossyImp(pIn, nIn, nEle, pOut);
H
Hongze Cheng 已提交
2405 2406 2407
  } else {
#endif
    // decompress lossless
H
Hongze Cheng 已提交
2408 2409 2410 2411 2412
    if (cmprAlg == ONE_STAGE_COMP) {
      return tsDecompressFloatImp(pIn, nEle, pOut);
    } else if (cmprAlg == TWO_STAGE_COMP) {
      if (tsDecompressStringImp(pIn, nIn, pBuf, nBuf) < 0) return -1;
      return tsDecompressFloatImp(pBuf, nEle, pOut);
H
Hongze Cheng 已提交
2413
    } else {
2414
      ASSERTS(0, "compress algo invalid");
H
Hongze Cheng 已提交
2415 2416 2417 2418 2419 2420 2421 2422
      return -1;
    }
#ifdef TD_TSZ
  }
#endif
}

// Double =====================================================
H
Hongze Cheng 已提交
2423 2424
int32_t tsCompressDouble(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg, void *pBuf,
                         int32_t nBuf) {
H
Hongze Cheng 已提交
2425 2426 2427
#ifdef TD_TSZ
  if (lossyDouble) {
    // lossy mode
H
Hongze Cheng 已提交
2428
    return tsCompressDoubleLossyImp(pIn, nEle, pOut);
H
Hongze Cheng 已提交
2429 2430 2431
  } else {
#endif
    // lossless mode
H
Hongze Cheng 已提交
2432 2433 2434 2435 2436
    if (cmprAlg == ONE_STAGE_COMP) {
      return tsCompressDoubleImp(pIn, nEle, pOut);
    } else if (cmprAlg == TWO_STAGE_COMP) {
      int32_t len = tsCompressDoubleImp(pIn, nEle, pBuf);
      return tsCompressStringImp(pBuf, len, pOut, nOut);
H
Hongze Cheng 已提交
2437
    } else {
2438
      ASSERTS(0, "compress algo invalid");
H
Hongze Cheng 已提交
2439 2440 2441 2442 2443 2444 2445
      return -1;
    }
#ifdef TD_TSZ
  }
#endif
}

H
Hongze Cheng 已提交
2446 2447
int32_t tsDecompressDouble(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg, void *pBuf,
                           int32_t nBuf) {
H
Hongze Cheng 已提交
2448 2449 2450
#ifdef TD_TSZ
  if (HEAD_ALGO(input[0]) == ALGO_SZ_LOSSY) {
    // decompress lossy
H
Hongze Cheng 已提交
2451
    return tsDecompressDoubleLossyImp(pIn, nIn, nEle, pOut);
H
Hongze Cheng 已提交
2452 2453 2454
  } else {
#endif
    // decompress lossless
H
Hongze Cheng 已提交
2455 2456 2457 2458 2459
    if (cmprAlg == ONE_STAGE_COMP) {
      return tsDecompressDoubleImp(pIn, nEle, pOut);
    } else if (cmprAlg == TWO_STAGE_COMP) {
      if (tsDecompressStringImp(pIn, nIn, pBuf, nBuf) < 0) return -1;
      return tsDecompressDoubleImp(pBuf, nEle, pOut);
H
Hongze Cheng 已提交
2460
    } else {
2461
      ASSERTS(0, "compress algo invalid");
H
Hongze Cheng 已提交
2462 2463 2464 2465 2466 2467 2468 2469
      return -1;
    }
#ifdef TD_TSZ
  }
#endif
}

// Binary =====================================================
H
Hongze Cheng 已提交
2470 2471 2472
int32_t tsCompressString(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg, void *pBuf,
                         int32_t nBuf) {
  return tsCompressStringImp(pIn, nIn, pOut, nOut);
H
Hongze Cheng 已提交
2473 2474
}

H
Hongze Cheng 已提交
2475 2476 2477
int32_t tsDecompressString(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg, void *pBuf,
                           int32_t nBuf) {
  return tsDecompressStringImp(pIn, nIn, pOut, nOut);
H
Hongze Cheng 已提交
2478 2479 2480
}

// Bool =====================================================
H
Hongze Cheng 已提交
2481 2482 2483 2484 2485 2486
int32_t tsCompressBool(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg, void *pBuf,
                       int32_t nBuf) {
  if (cmprAlg == ONE_STAGE_COMP) {
    return tsCompressBoolImp(pIn, nEle, pOut);
  } else if (cmprAlg == TWO_STAGE_COMP) {
    int32_t len = tsCompressBoolImp(pIn, nEle, pBuf);
2487 2488 2489
    if (len < 0) {
      return -1;
    }
H
Hongze Cheng 已提交
2490
    return tsCompressStringImp(pBuf, len, pOut, nOut);
H
Hongze Cheng 已提交
2491
  } else {
2492
    ASSERTS(0, "compress algo invalid");
H
Hongze Cheng 已提交
2493 2494 2495 2496
    return -1;
  }
}

H
Hongze Cheng 已提交
2497 2498 2499 2500 2501 2502 2503
int32_t tsDecompressBool(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg, void *pBuf,
                         int32_t nBuf) {
  if (cmprAlg == ONE_STAGE_COMP) {
    return tsDecompressBoolImp(pIn, nEle, pOut);
  } else if (cmprAlg == TWO_STAGE_COMP) {
    if (tsDecompressStringImp(pIn, nIn, pBuf, nBuf) < 0) return -1;
    return tsDecompressBoolImp(pBuf, nEle, pOut);
H
Hongze Cheng 已提交
2504
  } else {
2505
    ASSERTS(0, "compress algo invalid");
H
Hongze Cheng 已提交
2506 2507 2508 2509 2510
    return -1;
  }
}

// Tinyint =====================================================
H
Hongze Cheng 已提交
2511 2512 2513 2514 2515 2516 2517
int32_t tsCompressTinyint(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg, void *pBuf,
                          int32_t nBuf) {
  if (cmprAlg == ONE_STAGE_COMP) {
    return tsCompressINTImp(pIn, nEle, pOut, TSDB_DATA_TYPE_TINYINT);
  } else if (cmprAlg == TWO_STAGE_COMP) {
    int32_t len = tsCompressINTImp(pIn, nEle, pBuf, TSDB_DATA_TYPE_TINYINT);
    return tsCompressStringImp(pBuf, len, pOut, nOut);
H
Hongze Cheng 已提交
2518
  } else {
2519
    ASSERTS(0, "compress algo invalid");
H
Hongze Cheng 已提交
2520 2521 2522 2523
    return -1;
  }
}

H
Hongze Cheng 已提交
2524 2525 2526 2527 2528 2529 2530
int32_t tsDecompressTinyint(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg, void *pBuf,
                            int32_t nBuf) {
  if (cmprAlg == ONE_STAGE_COMP) {
    return tsDecompressINTImp(pIn, nEle, pOut, TSDB_DATA_TYPE_TINYINT);
  } else if (cmprAlg == TWO_STAGE_COMP) {
    if (tsDecompressStringImp(pIn, nIn, pBuf, nBuf) < 0) return -1;
    return tsDecompressINTImp(pBuf, nEle, pOut, TSDB_DATA_TYPE_TINYINT);
H
Hongze Cheng 已提交
2531
  } else {
2532
    ASSERTS(0, "compress algo invalid");
H
Hongze Cheng 已提交
2533 2534 2535 2536 2537
    return -1;
  }
}

// Smallint =====================================================
H
Hongze Cheng 已提交
2538 2539 2540 2541 2542 2543 2544
int32_t tsCompressSmallint(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg, void *pBuf,
                           int32_t nBuf) {
  if (cmprAlg == ONE_STAGE_COMP) {
    return tsCompressINTImp(pIn, nEle, pOut, TSDB_DATA_TYPE_SMALLINT);
  } else if (cmprAlg == TWO_STAGE_COMP) {
    int32_t len = tsCompressINTImp(pIn, nEle, pBuf, TSDB_DATA_TYPE_SMALLINT);
    return tsCompressStringImp(pBuf, len, pOut, nOut);
H
Hongze Cheng 已提交
2545
  } else {
2546
    ASSERTS(0, "compress algo invalid");
H
Hongze Cheng 已提交
2547 2548 2549 2550
    return -1;
  }
}

H
Hongze Cheng 已提交
2551 2552 2553 2554 2555 2556 2557
int32_t tsDecompressSmallint(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg,
                             void *pBuf, int32_t nBuf) {
  if (cmprAlg == ONE_STAGE_COMP) {
    return tsDecompressINTImp(pIn, nEle, pOut, TSDB_DATA_TYPE_SMALLINT);
  } else if (cmprAlg == TWO_STAGE_COMP) {
    if (tsDecompressStringImp(pIn, nIn, pBuf, nBuf) < 0) return -1;
    return tsDecompressINTImp(pBuf, nEle, pOut, TSDB_DATA_TYPE_SMALLINT);
H
Hongze Cheng 已提交
2558
  } else {
2559
    ASSERTS(0, "compress algo invalid");
H
Hongze Cheng 已提交
2560 2561 2562 2563 2564
    return -1;
  }
}

// Int =====================================================
H
Hongze Cheng 已提交
2565 2566 2567 2568 2569 2570 2571
int32_t tsCompressInt(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg, void *pBuf,
                      int32_t nBuf) {
  if (cmprAlg == ONE_STAGE_COMP) {
    return tsCompressINTImp(pIn, nEle, pOut, TSDB_DATA_TYPE_INT);
  } else if (cmprAlg == TWO_STAGE_COMP) {
    int32_t len = tsCompressINTImp(pIn, nEle, pBuf, TSDB_DATA_TYPE_INT);
    return tsCompressStringImp(pBuf, len, pOut, nOut);
H
Hongze Cheng 已提交
2572
  } else {
2573
    ASSERTS(0, "compress algo invalid");
H
Hongze Cheng 已提交
2574 2575 2576 2577
    return -1;
  }
}

H
Hongze Cheng 已提交
2578 2579 2580 2581 2582 2583 2584
int32_t tsDecompressInt(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg, void *pBuf,
                        int32_t nBuf) {
  if (cmprAlg == ONE_STAGE_COMP) {
    return tsDecompressINTImp(pIn, nEle, pOut, TSDB_DATA_TYPE_INT);
  } else if (cmprAlg == TWO_STAGE_COMP) {
    if (tsDecompressStringImp(pIn, nIn, pBuf, nBuf) < 0) return -1;
    return tsDecompressINTImp(pBuf, nEle, pOut, TSDB_DATA_TYPE_INT);
H
Hongze Cheng 已提交
2585
  } else {
2586
    ASSERTS(0, "compress algo invalid");
H
Hongze Cheng 已提交
2587 2588 2589 2590 2591
    return -1;
  }
}

// Bigint =====================================================
H
Hongze Cheng 已提交
2592 2593 2594 2595 2596 2597 2598
int32_t tsCompressBigint(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg, void *pBuf,
                         int32_t nBuf) {
  if (cmprAlg == ONE_STAGE_COMP) {
    return tsCompressINTImp(pIn, nEle, pOut, TSDB_DATA_TYPE_BIGINT);
  } else if (cmprAlg == TWO_STAGE_COMP) {
    int32_t len = tsCompressINTImp(pIn, nEle, pBuf, TSDB_DATA_TYPE_BIGINT);
    return tsCompressStringImp(pBuf, len, pOut, nOut);
H
Hongze Cheng 已提交
2599
  } else {
2600
    ASSERTS(0, "compress algo invalid");
H
Hongze Cheng 已提交
2601 2602 2603 2604
    return -1;
  }
}

H
Hongze Cheng 已提交
2605 2606 2607 2608 2609 2610 2611
int32_t tsDecompressBigint(void *pIn, int32_t nIn, int32_t nEle, void *pOut, int32_t nOut, uint8_t cmprAlg, void *pBuf,
                           int32_t nBuf) {
  if (cmprAlg == ONE_STAGE_COMP) {
    return tsDecompressINTImp(pIn, nEle, pOut, TSDB_DATA_TYPE_BIGINT);
  } else if (cmprAlg == TWO_STAGE_COMP) {
    if (tsDecompressStringImp(pIn, nIn, pBuf, nBuf) < 0) return -1;
    return tsDecompressINTImp(pBuf, nEle, pOut, TSDB_DATA_TYPE_BIGINT);
H
Hongze Cheng 已提交
2612
  } else {
2613
    ASSERTS(0, "compress algo invalid");
H
Hongze Cheng 已提交
2614 2615 2616
    return -1;
  }
}