tskiplist.h 5.9 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 TDENGINE_TSKIPLIST_H
#define TDENGINE_TSKIPLIST_H
H
hzcheng 已提交
18 19 20 21 22

#ifdef __cplusplus
extern "C" {
#endif

S
slguan 已提交
23
#include "os.h"
H
hzcheng 已提交
24
#include "taosdef.h"
H
more  
hzcheng 已提交
25
#include "tarray.h"
H
hzcheng 已提交
26

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

H
TD-1437  
Hongze Cheng 已提交
30 31 32
// For key property setting
#define SL_ALLOW_DUP_KEY (uint8_t)0x0    // Allow duplicate key exists
#define SL_DISCARD_DUP_KEY (uint8_t)0x1  // Discard duplicate key
H
TD-1194  
Hongze Cheng 已提交
33
#define SL_UPDATE_DUP_KEY (uint8_t)0x2   // Update duplicate key by remove/insert
H
TD-1437  
Hongze Cheng 已提交
34 35 36
// For thread safety setting
#define SL_THREAD_SAFE (uint8_t)0x4

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

typedef struct SSkipListNode {
H
TD-1194  
Hongze Cheng 已提交
41 42 43 44
  uint8_t        level;
  uint8_t        flags;
  void *         pData;
  struct SSkipListNode *forwards[];
H
more  
hzcheng 已提交
45
} SSkipListNode;
H
hzcheng 已提交
46

H
TD-1194  
Hongze Cheng 已提交
47
#define SL_NODE_DELETED_FLAG (uint8_t)0x1
H
hzcheng 已提交
48

H
TD-1194  
Hongze Cheng 已提交
49 50 51 52 53
#define SL_GET_NODE_DATA(n) (n)->pData
#define SL_IS_NODE_DELETED(n) ((n)->flags & SL_NODE_DELETED_FLAG)
#define SL_SET_NODE_DELETED(n) (n)->flags |= SL_NODE_DELETED_FLAG
#define SL_NODE_GET_FORWARD_POINTER(n, l) (n)->forwards[(l)]
#define SL_NODE_GET_BACKWARD_POINTER(n, l) (n)->forwards[(n)->level + (l)]
H
hzcheng 已提交
54 55

/*
H
more  
hzcheng 已提交
56
 * @version 0.3
H
hzcheng 已提交
57
 * @date   2017/11/12
H
more  
hzcheng 已提交
58 59
 * the simple version of skip list.
 *
S
slguan 已提交
60
 * for multi-thread safe purpose, we employ pthread_rwlock_t to guarantee to generate
H
more  
hzcheng 已提交
61 62 63
 * deterministic result. Later, we will remove the lock in SkipList to further enhance the performance.
 * In this case, one should use the concurrent skip list (by using michael-scott algorithm) instead of
 * this simple version in a multi-thread environment, to achieve higher performance of read/write operations.
H
hzcheng 已提交
64 65
 *
 * Note: Duplicated primary key situation.
S
slguan 已提交
66
 * In case of duplicated primary key, two ways can be employed to handle this situation:
H
more  
hzcheng 已提交
67 68 69
 * 1. add as normal insertion without special process.
 * 2. add an overflow pointer at each list node, all nodes with the same key will be added in the overflow pointer.
 *    In this case, the total steps of each search will be reduced significantly.
S
slguan 已提交
70 71 72 73
 *    Currently, we implement the skip list in a line with the first means, maybe refactor it soon.
 *
 *    Memory consumption: the memory alignment causes many memory wasted. So, employ a memory
 *    pool will significantly reduce the total memory consumption, as well as the calloc/malloc operation costs.
H
hzcheng 已提交
74 75 76 77 78 79 80 81 82
 *
 */

// state struct, record following information:
// number of links in each level.
// avg search steps, for latest 1000 queries
// avg search rsp time, for latest 1000 queries
// total memory size
typedef struct tSkipListState {
H
more  
hzcheng 已提交
83
  // in bytes, sizeof(SSkipList)+sizeof(SSkipListNode)*SSkipList->nSize
H
hzcheng 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  uint64_t nTotalMemSize;
  uint64_t nLevelNodeCnt[MAX_SKIP_LIST_LEVEL];
  uint64_t queryCount;  // total query count

  /*
   * only record latest 1000 queries
   * when the value==1000, = 0,
   * nTotalStepsForQueries = 0,
   * nTotalElapsedTimeForQueries = 0
   */
  uint64_t nRecQueries;
  uint16_t nTotalStepsForQueries;
  uint64_t nTotalElapsedTimeForQueries;

  uint16_t nInsertObjs;
  uint16_t nTotalStepsForInsert;
  uint64_t nTotalElapsedTimeForInsert;
} tSkipListState;

H
more  
hzcheng 已提交
103
typedef struct SSkipList {
H
hjxilinx 已提交
104 105
  __compar_fn_t     comparFn;
  __sl_key_fn_t     keyFn;
H
TD-1437  
Hongze Cheng 已提交
106 107
  pthread_rwlock_t *lock;
  uint16_t          len;
H
hjxilinx 已提交
108
  uint8_t           maxLevel;
H
TD-1437  
Hongze Cheng 已提交
109
  uint8_t           flags;
H
TD-1194  
Hongze Cheng 已提交
110
  uint8_t           type;  // static info above
H
hjxilinx 已提交
111
  uint8_t           level;
H
TD-1194  
Hongze Cheng 已提交
112 113
  uint32_t          size;   // semantic meaning of size
  uint32_t          tsize;  // # of all skiplist nodes in this SL
H
TD-1437  
Hongze Cheng 已提交
114 115
  SSkipListNode *   pHead;  // point to the first element
  SSkipListNode *   pTail;  // point to the last element
H
more  
hzcheng 已提交
116 117 118 119
#if SKIP_LIST_RECORD_PERFORMANCE
  tSkipListState state;  // skiplist state
#endif
} SSkipList;
H
hzcheng 已提交
120

S
slguan 已提交
121
typedef struct SSkipListIterator {
H
more  
hzcheng 已提交
122 123
  SSkipList *    pSkipList;
  SSkipListNode *cur;
H
TD-1194  
Hongze Cheng 已提交
124 125
  int32_t        step;   // the number of nodes that have been checked already
  int32_t        order;  // order of the iterator
S
slguan 已提交
126 127
} SSkipListIterator;

H
TD-1194  
Hongze Cheng 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140
#define SL_IS_THREAD_SAFE(s) (((s)->flags) & SL_THREAD_SAFE)
#define SL_DUP_MODE(s) (((s)->flags) & ((((uint8_t)1) << 2) - 1))
#define SL_GET_NODE_KEY(s, n) ((s)->keyFn((n)->pData))
#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
#define SL_TSIZE(s) (s)->tsize

SSkipList *    tSkipListCreate(uint8_t nMaxLevel, uint8_t keyType, uint16_t keyLen, uint8_t flags, __sl_key_fn_t fn);
void           tSkipListDestroy(SSkipList *pSkipList);
SSkipListNode *tSkipListPut(SSkipList *pSkipList, void *pData);
SArray *       tSkipListGet(SSkipList *pSkipList, SSkipListKey pKey);
void           tSkipListPrint(SSkipList *pSkipList, int16_t nlevel);
H
hjxilinx 已提交
141
SSkipListIterator *tSkipListCreateIter(SSkipList *pSkipList);
H
TD-1194  
Hongze Cheng 已提交
142 143 144 145 146 147
SSkipListIterator *tSkipListCreateIterFromVal(SSkipList *pSkipList, const char *val, int32_t type, int32_t order);
bool               tSkipListIterNext(SSkipListIterator *iter);
SSkipListNode *    tSkipListIterGet(SSkipListIterator *iter);
void *             tSkipListDestroyIter(SSkipListIterator *iter);
uint32_t           tSkipListRemove(SSkipList *pSkipList, SSkipListKey key);
void               tSkipListRemoveNode(SSkipList *pSkipList, SSkipListNode *pNode);
H
TD-1194  
Hongze Cheng 已提交
148 149
SSkipListKey       tSkipListGetMinKey(SSkipList *pSkipList);
SSkipListKey       tSkipListGetMaxKey(SSkipList *pSkipList);
H
hzcheng 已提交
150 151 152 153 154

#ifdef __cplusplus
}
#endif

155
#endif  // TDENGINE_TSKIPLIST_H