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

S
Shengliang Guan 已提交
18
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
19

dengyihao's avatar
dengyihao 已提交
20 21 22 23
#ifdef __cplusplus
extern "C" {
#endif

dengyihao's avatar
dengyihao 已提交
24 25 26 27
#define SERIALIZE_MEM_TO_BUF(buf, key, mem)                     \
  do {                                                          \
    memcpy((void *)buf, (void *)(&key->mem), sizeof(key->mem)); \
    buf += sizeof(key->mem);                                    \
dengyihao's avatar
dengyihao 已提交
28 29 30
  } while (0)

#define SERIALIZE_STR_MEM_TO_BUF(buf, key, mem, len) \
31
  do {                                               \
dengyihao's avatar
dengyihao 已提交
32
    memcpy((void *)buf, (void *)key->mem, len);      \
33
    buf += len;                                      \
dengyihao's avatar
dengyihao 已提交
34 35
  } while (0)

dengyihao's avatar
dengyihao 已提交
36 37 38 39 40 41
#define SERIALIZE_VAR_TO_BUF(buf, var, type)    \
  do {                                          \
    type c = var;                               \
    assert(sizeof(type) == sizeof(c));          \
    memcpy((void *)buf, (void *)&c, sizeof(c)); \
    buf += sizeof(c);                           \
dengyihao's avatar
dengyihao 已提交
42 43 44
  } while (0)

#define SERIALIZE_STR_VAR_TO_BUF(buf, var, len) \
45
  do {                                          \
dengyihao's avatar
dengyihao 已提交
46
    memcpy((void *)buf, (void *)var, len);      \
47
    buf += len;                                 \
dengyihao's avatar
dengyihao 已提交
48 49
  } while (0)

dengyihao's avatar
dengyihao 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62
#define INDEX_MERGE_ADD_DEL(src, dst, tgt)            \
  {                                                   \
    bool f = false;                                   \
    for (int i = 0; i < taosArrayGetSize(src); i++) { \
      if (*(uint64_t *)taosArrayGet(src, i) == tgt) { \
        f = true;                                     \
      }                                               \
    }                                                 \
    if (f == false) {                                 \
      taosArrayPush(dst, &tgt);                       \
    }                                                 \
  }

dengyihao's avatar
dengyihao 已提交
63 64 65 66 67 68 69
/* multi sorted result intersection
 * input: [1, 2, 4, 5]
 *        [2, 3, 4, 5]
 *        [1, 4, 5]
 * output:[4, 5]
 */
void iIntersection(SArray *interResults, SArray *finalResult);
70

dengyihao's avatar
dengyihao 已提交
71
/* multi sorted result union
72 73 74 75 76
 * input: [1, 2, 4, 5]
 *        [2, 3, 4, 5]
 *        [1, 4, 5]
 * output:[1, 2, 3, 4, 5]
 */
dengyihao's avatar
dengyihao 已提交
77
void iUnion(SArray *interResults, SArray *finalResult);
78

dengyihao's avatar
dengyihao 已提交
79
/* see example
80 81
 * total:   [1, 2, 4, 5, 7, 8]
 * except:  [4, 5]
dengyihao's avatar
dengyihao 已提交
82
 * return:  [1, 2, 7, 8] saved in total
83 84 85
 */

void iExcept(SArray *total, SArray *except);
dengyihao's avatar
dengyihao 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

int uidCompare(const void *a, const void *b);

// data with ver
typedef struct {
  uint32_t ver;
  uint64_t data;
} SIdxVerdata;

typedef struct {
  SArray *total;
  SArray *added;
  SArray *deled;
} SIdxTempResult;

SIdxTempResult *sIdxTempResultCreate();

void sIdxTempResultClear(SIdxTempResult *tr);

void sIdxTempResultDestroy(SIdxTempResult *tr);

void sIdxTempResultMergeTo(SArray *result, SIdxTempResult *tr);
dengyihao's avatar
dengyihao 已提交
108 109 110 111 112
#ifdef __cplusplus
}
#endif

#endif