tskiplist.h 4.2 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

16 17
#ifndef _TD_UTIL_SKILIST_H
#define _TD_UTIL_SKILIST_H
H
hzcheng 已提交
18

S
slguan 已提交
19
#include "os.h"
20
#include "taos.h"
H
more  
hzcheng 已提交
21
#include "tarray.h"
22
#include "tfunctional.h"
H
hzcheng 已提交
23

S
Shengliang Guan 已提交
24 25 26 27 28
#ifdef __cplusplus
extern "C" {
#endif

#define MAX_SKIP_LIST_LEVEL          15
H
more  
hzcheng 已提交
29 30
#define SKIP_LIST_RECORD_PERFORMANCE 0

H
TD-1437  
Hongze Cheng 已提交
31
// For key property setting
S
Shengliang Guan 已提交
32
#define SL_ALLOW_DUP_KEY   (uint8_t)0x0  // Allow duplicate key exists (for tag index usage)
H
Hongze Cheng 已提交
33
#define SL_DISCARD_DUP_KEY (uint8_t)0x1  // Discard duplicate key (for data update=0 case)
S
Shengliang Guan 已提交
34
#define SL_UPDATE_DUP_KEY  (uint8_t)0x2  // Update duplicate key by remove/insert (for data update!=0 case)
35

H
TD-1437  
Hongze Cheng 已提交
36 37 38
// For thread safety setting
#define SL_THREAD_SAFE (uint8_t)0x4

H
more  
hzcheng 已提交
39 40 41
typedef char *SSkipListKey;
typedef char *(*__sl_key_fn_t)(const void *);

S
Shengliang Guan 已提交
42 43
typedef void (*sl_patch_row_fn_t)(void *pDst, const void *pSrc);
typedef void *(*iter_next_fn_t)(void *iter);
44

H
more  
hzcheng 已提交
45
typedef struct SSkipListNode {
S
Shengliang Guan 已提交
46 47
  uint8_t               level;
  void                 *pData;
H
TD-1194  
Hongze Cheng 已提交
48
  struct SSkipListNode *forwards[];
H
more  
hzcheng 已提交
49
} SSkipListNode;
H
hzcheng 已提交
50

S
Shengliang Guan 已提交
51 52
#define SL_GET_NODE_DATA(n)                (n)->pData
#define SL_NODE_GET_FORWARD_POINTER(n, l)  (n)->forwards[(l)]
H
TD-1194  
Hongze Cheng 已提交
53
#define SL_NODE_GET_BACKWARD_POINTER(n, l) (n)->forwards[(n)->level + (l)]
H
hzcheng 已提交
54

S
Shengliang Guan 已提交
55
typedef enum { SSkipListPutSuccess = 0, SSkipListPutEarlyStop = 1, SSkipListPutSkipOne = 2 } SSkipListPutStatus;
56

H
more  
hzcheng 已提交
57
typedef struct SSkipList {
H
Hongze Cheng 已提交
58 59 60 61 62 63 64 65 66 67 68 69
  uint32_t           seed;
  __compar_fn_t      comparFn;
  __sl_key_fn_t      keyFn;
  TdThreadRwlock    *lock;
  uint16_t           len;
  uint8_t            maxLevel;
  uint8_t            flags;
  uint8_t            type;  // static info above
  uint8_t            level;
  uint32_t           size;
  SSkipListNode     *pHead;  // point to the first element
  SSkipListNode     *pTail;  // point to the last element
S
Shengliang Guan 已提交
70
  tGenericSavedFunc *insertHandleFn;
H
more  
hzcheng 已提交
71
} SSkipList;
H
hzcheng 已提交
72

S
slguan 已提交
73
typedef struct SSkipListIterator {
S
Shengliang Guan 已提交
74
  SSkipList     *pSkipList;
H
more  
hzcheng 已提交
75
  SSkipListNode *cur;
S
Shengliang Guan 已提交
76 77 78
  int32_t        step;   // the number of nodes that have been checked already
  int32_t        order;  // order of the iterator
  SSkipListNode *next;   // next points to the true qualified node in skiplist
S
slguan 已提交
79 80
} SSkipListIterator;

S
Shengliang Guan 已提交
81 82
#define SL_IS_THREAD_SAFE(s)  (((s)->flags) & SL_THREAD_SAFE)
#define SL_DUP_MODE(s)        (((s)->flags) & ((((uint8_t)1) << 2) - 1))
H
TD-1194  
Hongze Cheng 已提交
83
#define SL_GET_NODE_KEY(s, n) ((s)->keyFn((n)->pData))
S
Shengliang Guan 已提交
84 85 86
#define SL_GET_MIN_KEY(s)     SL_GET_NODE_KEY(s, SL_NODE_GET_FORWARD_POINTER((s)->pHead, 0))
#define SL_GET_MAX_KEY(s)     SL_GET_NODE_KEY((s), SL_NODE_GET_BACKWARD_POINTER((s)->pTail, 0))
#define SL_SIZE(s)            (s)->size
H
TD-1194  
Hongze Cheng 已提交
87

H
TD-1548  
Hongze Cheng 已提交
88 89 90
SSkipList *tSkipListCreate(uint8_t maxLevel, uint8_t keyType, uint16_t keyLen, __compar_fn_t comparFn, uint8_t flags,
                           __sl_key_fn_t fn);
void       tSkipListDestroy(SSkipList *pSkipList);
S
Shengliang Guan 已提交
91
SSkipListNode     *tSkipListPut(SSkipList *pSkipList, void *pData);
92
void               tSkipListPutBatchByIter(SSkipList *pSkipList, void *iter, iter_next_fn_t iterate);
S
Shengliang Guan 已提交
93
SArray            *tSkipListGet(SSkipList *pSkipList, SSkipListKey pKey);
H
TD-1548  
Hongze Cheng 已提交
94
void               tSkipListPrint(SSkipList *pSkipList, int16_t nlevel);
H
hjxilinx 已提交
95
SSkipListIterator *tSkipListCreateIter(SSkipList *pSkipList);
H
TD-1194  
Hongze Cheng 已提交
96 97
SSkipListIterator *tSkipListCreateIterFromVal(SSkipList *pSkipList, const char *val, int32_t type, int32_t order);
bool               tSkipListIterNext(SSkipListIterator *iter);
S
Shengliang Guan 已提交
98 99
SSkipListNode     *tSkipListIterGet(SSkipListIterator *iter);
void              *tSkipListDestroyIter(SSkipListIterator *iter);
H
TD-1194  
Hongze Cheng 已提交
100 101
uint32_t           tSkipListRemove(SSkipList *pSkipList, SSkipListKey key);
void               tSkipListRemoveNode(SSkipList *pSkipList, SSkipListNode *pNode);
H
hzcheng 已提交
102 103 104 105 106

#ifdef __cplusplus
}
#endif

S
Shengliang Guan 已提交
107
#endif /*_TD_UTIL_SKILIST_H*/