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

H
hjxilinx 已提交
17
#include "os.h"
18

H
Hongze Cheng 已提交
19
#include "compare.h"
20
#include "tskiplist.h"
H
hzcheng 已提交
21
#include "tutil.h"
22
#include "ulog.h"
H
hzcheng 已提交
23

H
TD-1194  
Hongze Cheng 已提交
24
static int                initForwardBackwardPtr(SSkipList *pSkipList);
H
Hongze Cheng 已提交
25
static SSkipListNode *    getPriorNode(SSkipList *pSkipList, const char *val, int32_t order, SSkipListNode **pCur);
H
TD-1194  
Hongze Cheng 已提交
26 27
static void               tSkipListRemoveNodeImpl(SSkipList *pSkipList, SSkipListNode *pNode);
static void               tSkipListCorrectLevel(SSkipList *pSkipList);
H
TD-1437  
Hongze Cheng 已提交
28
static SSkipListIterator *doCreateSkipListIterator(SSkipList *pSkipList, int32_t order);
H
Hongze Cheng 已提交
29 30 31
static void tSkipListDoInsert(SSkipList *pSkipList, SSkipListNode **direction, SSkipListNode *pNode, bool isForward);
static bool tSkipListGetPosToPut(SSkipList *pSkipList, SSkipListNode **backward, void *pData);
static SSkipListNode *tSkipListNewNode(uint8_t level);
S
TD-1848  
Shengliang Guan 已提交
32
#define tSkipListFreeNode(n) tfree((n))
H
Hongze Cheng 已提交
33 34
static SSkipListNode *tSkipListPutImpl(SSkipList *pSkipList, void *pData, SSkipListNode **direction, bool isForward,
                                       bool hasDup);
H
TD-1437  
Hongze Cheng 已提交
35

36

H
TD-1437  
Hongze Cheng 已提交
37 38 39 40 41
static FORCE_INLINE int     tSkipListWLock(SSkipList *pSkipList);
static FORCE_INLINE int     tSkipListRLock(SSkipList *pSkipList);
static FORCE_INLINE int     tSkipListUnlock(SSkipList *pSkipList);
static FORCE_INLINE int32_t getSkipListRandLevel(SSkipList *pSkipList);

H
TD-1548  
Hongze Cheng 已提交
42 43
SSkipList *tSkipListCreate(uint8_t maxLevel, uint8_t keyType, uint16_t keyLen, __compar_fn_t comparFn, uint8_t flags,
                           __sl_key_fn_t fn) {
H
TD-1437  
Hongze Cheng 已提交
44
  SSkipList *pSkipList = (SSkipList *)calloc(1, sizeof(SSkipList));
H
TD-1194  
Hongze Cheng 已提交
45
  if (pSkipList == NULL) return NULL;
H
TD-1437  
Hongze Cheng 已提交
46 47 48 49 50 51 52 53 54 55

  if (maxLevel > MAX_SKIP_LIST_LEVEL) {
    maxLevel = MAX_SKIP_LIST_LEVEL;
  }

  pSkipList->maxLevel = maxLevel;
  pSkipList->type = keyType;
  pSkipList->len = keyLen;
  pSkipList->flags = flags;
  pSkipList->keyFn = fn;
S
TD-4170  
Shengliang Guan 已提交
56
  pSkipList->seed = rand();
57 58 59

#if 0 
  // the function getkeycomparfunc is defined in common
H
TD-1548  
Hongze Cheng 已提交
60
  if (comparFn == NULL) {
61
    pSkipList->comparFn = getKeyComparFunc(keyType, TSDB_ORDER_ASC);
H
TD-1548  
Hongze Cheng 已提交
62 63 64
  } else {
    pSkipList->comparFn = comparFn;
  }
65 66 67
#else
  pSkipList->comparFn = comparFn;
#endif
H
TD-1437  
Hongze Cheng 已提交
68 69

  if (initForwardBackwardPtr(pSkipList) < 0) {
H
TD-1194  
Hongze Cheng 已提交
70
    tSkipListDestroy(pSkipList);
H
TD-1437  
Hongze Cheng 已提交
71
    return NULL;
S
slguan 已提交
72
  }
H
TD-1437  
Hongze Cheng 已提交
73

H
TD-1194  
Hongze Cheng 已提交
74
  if (SL_IS_THREAD_SAFE(pSkipList)) {
H
TD-1437  
Hongze Cheng 已提交
75
    pSkipList->lock = (pthread_rwlock_t *)calloc(1, sizeof(pthread_rwlock_t));
H
TD-1194  
Hongze Cheng 已提交
76 77 78 79
    if (pSkipList->lock == NULL) {
      tSkipListDestroy(pSkipList);
      return NULL;
    }
H
TD-1437  
Hongze Cheng 已提交
80 81

    if (pthread_rwlock_init(pSkipList->lock, NULL) != 0) {
H
TD-1194  
Hongze Cheng 已提交
82
      tSkipListDestroy(pSkipList);
H
TD-1437  
Hongze Cheng 已提交
83 84 85 86 87 88 89 90
      return NULL;
    }
  }

  srand((uint32_t)time(NULL));

#if SKIP_LIST_RECORD_PERFORMANCE
  pSkipList->state.nTotalMemSize += sizeof(SSkipList);
H
more  
hzcheng 已提交
91
#endif
92
  pSkipList->insertHandleFn = NULL;
H
TD-1437  
Hongze Cheng 已提交
93 94

  return pSkipList;
S
slguan 已提交
95
}
H
hzcheng 已提交
96

H
TD-1194  
Hongze Cheng 已提交
97 98
void tSkipListDestroy(SSkipList *pSkipList) {
  if (pSkipList == NULL) return;
H
TD-1437  
Hongze Cheng 已提交
99 100 101

  tSkipListWLock(pSkipList);

H
TD-1194  
Hongze Cheng 已提交
102
  SSkipListNode *pNode = SL_NODE_GET_FORWARD_POINTER(pSkipList->pHead, 0);
H
TD-1437  
Hongze Cheng 已提交
103 104 105

  while (pNode != pSkipList->pTail) {
    SSkipListNode *pTemp = pNode;
H
TD-1194  
Hongze Cheng 已提交
106 107
    pNode = SL_NODE_GET_FORWARD_POINTER(pNode, 0);
    tSkipListFreeNode(pTemp);
S
slguan 已提交
108
  }
H
TD-1437  
Hongze Cheng 已提交
109

110 111
  tfree(pSkipList->insertHandleFn);

H
TD-1437  
Hongze Cheng 已提交
112 113 114
  tSkipListUnlock(pSkipList);
  if (pSkipList->lock != NULL) {
    pthread_rwlock_destroy(pSkipList->lock);
S
TD-1848  
Shengliang Guan 已提交
115
    tfree(pSkipList->lock);
H
TD-1437  
Hongze Cheng 已提交
116 117
  }

H
TD-1194  
Hongze Cheng 已提交
118 119
  tSkipListFreeNode(pSkipList->pHead);
  tSkipListFreeNode(pSkipList->pTail);
S
TD-1848  
Shengliang Guan 已提交
120
  tfree(pSkipList);
H
TD-1437  
Hongze Cheng 已提交
121 122
}

H
TD-1194  
Hongze Cheng 已提交
123
SSkipListNode *tSkipListPut(SSkipList *pSkipList, void *pData) {
H
TD-1437  
Hongze Cheng 已提交
124 125
  if (pSkipList == NULL || pData == NULL) return NULL;

H
Hongze Cheng 已提交
126
  SSkipListNode *backward[MAX_SKIP_LIST_LEVEL] = {0};
H
TD-1194  
Hongze Cheng 已提交
127 128
  SSkipListNode *pNode = NULL;

H
TD-1437  
Hongze Cheng 已提交
129 130
  tSkipListWLock(pSkipList);

H
Hongze Cheng 已提交
131
  bool hasDup = tSkipListGetPosToPut(pSkipList, backward, pData);
H
Hongze Cheng 已提交
132
  pNode = tSkipListPutImpl(pSkipList, pData, backward, false, hasDup);
H
TD-1437  
Hongze Cheng 已提交
133

H
Hongze Cheng 已提交
134 135 136 137 138
  tSkipListUnlock(pSkipList);

  return pNode;
}

139
void tSkipListPutBatchByIter(SSkipList *pSkipList, void *iter, iter_next_fn_t iterate) {
H
Hongze Cheng 已提交
140 141 142 143 144 145 146 147
  SSkipListNode *backward[MAX_SKIP_LIST_LEVEL] = {0};
  SSkipListNode *forward[MAX_SKIP_LIST_LEVEL] = {0};
  bool           hasDup = false;
  char *         pKey = NULL;
  char *         pDataKey = NULL;
  int            compare = 0;

  tSkipListWLock(pSkipList);
H
TD-1437  
Hongze Cheng 已提交
148

149 150 151
  void* pData = iterate(iter);
  if(pData == NULL) return;

H
Hongze Cheng 已提交
152
  // backward to put the first data
153 154 155
  hasDup = tSkipListGetPosToPut(pSkipList, backward, pData);

  tSkipListPutImpl(pSkipList, pData, backward, false, hasDup);
H
Hongze Cheng 已提交
156

H
Hongze Cheng 已提交
157
  for (int level = 0; level < pSkipList->maxLevel; level++) {
H
Hongze Cheng 已提交
158 159 160 161
    forward[level] = SL_NODE_GET_BACKWARD_POINTER(backward[level], level);
  }

  // forward to put the rest of data
162 163
  while ((pData = iterate(iter)) != NULL) {
    pDataKey = pSkipList->keyFn(pData);
H
Hongze Cheng 已提交
164
    hasDup = false;
H
Hongze Cheng 已提交
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

    // Compare max key
    pKey = SL_GET_MAX_KEY(pSkipList);
    compare = pSkipList->comparFn(pDataKey, pKey);
    if (compare > 0) {
      for (int i = 0; i < pSkipList->maxLevel; i++) {
        forward[i] = SL_NODE_GET_BACKWARD_POINTER(pSkipList->pTail, i);
      }
    } else {
      SSkipListNode *px = pSkipList->pHead;
      for (int i = pSkipList->maxLevel - 1; i >= 0; --i) {
        if (i < pSkipList->level) {
          // set new px
          if (forward[i] != pSkipList->pHead) {
            if (px == pSkipList->pHead ||
                pSkipList->comparFn(SL_GET_NODE_KEY(pSkipList, forward[i]), SL_GET_NODE_KEY(pSkipList, px)) > 0) {
              px = forward[i];
            }
          }

          SSkipListNode *p = SL_NODE_GET_FORWARD_POINTER(px, i);
          while (p != pSkipList->pTail) {
            pKey = SL_GET_NODE_KEY(pSkipList, p);

            compare = pSkipList->comparFn(pKey, pDataKey);
            if (compare >= 0) {
H
Hongze Cheng 已提交
191
              if (compare == 0 && !hasDup) hasDup = true;
H
Hongze Cheng 已提交
192 193 194 195 196 197 198 199 200 201
              break;
            } else {
              px = p;
              p = SL_NODE_GET_FORWARD_POINTER(px, i);
            }
          }
        }

        forward[i] = px;
      }
H
TD-1194  
Hongze Cheng 已提交
202
    }
H
Hongze Cheng 已提交
203

204
    tSkipListPutImpl(pSkipList, pData, forward, true, hasDup);
H
TD-1437  
Hongze Cheng 已提交
205 206
  }
  tSkipListUnlock(pSkipList);
H
hzcheng 已提交
207 208
}

H
TD-1194  
Hongze Cheng 已提交
209 210
uint32_t tSkipListRemove(SSkipList *pSkipList, SSkipListKey key) {
  uint32_t count = 0;
H
TD-1437  
Hongze Cheng 已提交
211 212 213

  tSkipListWLock(pSkipList);

H
Hongze Cheng 已提交
214
  SSkipListNode *pNode = getPriorNode(pSkipList, key, TSDB_ORDER_ASC, NULL);
H
TD-1437  
Hongze Cheng 已提交
215
  while (1) {
H
TD-1194  
Hongze Cheng 已提交
216
    SSkipListNode *p = SL_NODE_GET_FORWARD_POINTER(pNode, 0);
H
TD-1437  
Hongze Cheng 已提交
217 218 219 220 221
    if (p == pSkipList->pTail) {
      break;
    }
    if (pSkipList->comparFn(key, SL_GET_NODE_KEY(pSkipList, p)) != 0) {
      break;
S
slguan 已提交
222
    }
H
TD-1194  
Hongze Cheng 已提交
223 224 225 226

    tSkipListRemoveNodeImpl(pSkipList, p);

    ++count;
S
slguan 已提交
227
  }
H
TD-1437  
Hongze Cheng 已提交
228

H
TD-1194  
Hongze Cheng 已提交
229 230
  tSkipListCorrectLevel(pSkipList);

H
TD-1437  
Hongze Cheng 已提交
231 232
  tSkipListUnlock(pSkipList);

H
TD-1194  
Hongze Cheng 已提交
233
  return count;
S
slguan 已提交
234 235
}

H
TD-1194  
Hongze Cheng 已提交
236 237
SArray *tSkipListGet(SSkipList *pSkipList, SSkipListKey key) {
  SArray *sa = taosArrayInit(1, POINTER_BYTES);
S
slguan 已提交
238

H
TD-1194  
Hongze Cheng 已提交
239
  tSkipListRLock(pSkipList);
240

H
Hongze Cheng 已提交
241
  SSkipListNode *pNode = getPriorNode(pSkipList, key, TSDB_ORDER_ASC, NULL);
H
TD-1437  
Hongze Cheng 已提交
242
  while (1) {
H
TD-1194  
Hongze Cheng 已提交
243
    SSkipListNode *p = SL_NODE_GET_FORWARD_POINTER(pNode, 0);
H
TD-1437  
Hongze Cheng 已提交
244 245 246 247 248 249
    if (p == pSkipList->pTail) {
      break;
    }
    if (pSkipList->comparFn(key, SL_GET_NODE_KEY(pSkipList, p)) != 0) {
      break;
    }
H
TD-1548  
Hongze Cheng 已提交
250
    taosArrayPush(sa, &p);
H
TD-1194  
Hongze Cheng 已提交
251
    pNode = p;
H
TD-1437  
Hongze Cheng 已提交
252 253 254 255
  }

  tSkipListUnlock(pSkipList);

H
TD-1194  
Hongze Cheng 已提交
256
  return sa;
H
TD-1437  
Hongze Cheng 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
}

void tSkipListRemoveNode(SSkipList *pSkipList, SSkipListNode *pNode) {
  tSkipListWLock(pSkipList);
  tSkipListRemoveNodeImpl(pSkipList, pNode);
  tSkipListCorrectLevel(pSkipList);
  tSkipListUnlock(pSkipList);
}

SSkipListIterator *tSkipListCreateIter(SSkipList *pSkipList) {
  if (pSkipList == NULL) return NULL;

  return doCreateSkipListIterator(pSkipList, TSDB_ORDER_ASC);
}

SSkipListIterator *tSkipListCreateIterFromVal(SSkipList *pSkipList, const char *val, int32_t type, int32_t order) {
H
TD-1194  
Hongze Cheng 已提交
273 274
  ASSERT(order == TSDB_ORDER_ASC || order == TSDB_ORDER_DESC);
  ASSERT(pSkipList != NULL);
H
TD-1437  
Hongze Cheng 已提交
275 276 277 278 279 280 281 282

  SSkipListIterator *iter = doCreateSkipListIterator(pSkipList, order);
  if (val == NULL) {
    return iter;
  }

  tSkipListRLock(pSkipList);

H
Hongze Cheng 已提交
283
  iter->cur = getPriorNode(pSkipList, val, order, &(iter->next));
H
TD-1437  
Hongze Cheng 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296

  tSkipListUnlock(pSkipList);

  return iter;
}

bool tSkipListIterNext(SSkipListIterator *iter) {
  if (iter->pSkipList == NULL) return false;

  SSkipList *pSkipList = iter->pSkipList;

  tSkipListRLock(pSkipList);

H
TD-1194  
Hongze Cheng 已提交
297
  if (iter->order == TSDB_ORDER_ASC) {
298 299 300 301 302 303 304
    // no data in the skip list
    if (iter->cur == pSkipList->pTail || iter->next == NULL) {
      iter->cur = pSkipList->pTail;
      tSkipListUnlock(pSkipList);
      return false;
    }

H
TD-1548  
Hongze Cheng 已提交
305
    iter->cur = SL_NODE_GET_FORWARD_POINTER(iter->cur, 0);
H
Hongze Cheng 已提交
306 307 308 309 310 311 312

    // a new node is inserted into between iter->cur and iter->next, ignore it
    if (iter->cur != iter->next && (iter->next != NULL)) {
      iter->cur = iter->next;
    }

    iter->next = SL_NODE_GET_FORWARD_POINTER(iter->cur, 0);
H
TD-1548  
Hongze Cheng 已提交
313
    iter->step++;
H
TD-1194  
Hongze Cheng 已提交
314
  } else {
H
Hongze Cheng 已提交
315
    if (iter->cur == pSkipList->pHead) {
316
      iter->cur = pSkipList->pHead;
H
Hongze Cheng 已提交
317 318 319
      tSkipListUnlock(pSkipList);
      return false;
    }
320

H
TD-1548  
Hongze Cheng 已提交
321
    iter->cur = SL_NODE_GET_BACKWARD_POINTER(iter->cur, 0);
H
Hongze Cheng 已提交
322 323 324 325 326 327 328

    // a new node is inserted into between iter->cur and iter->next, ignore it
    if (iter->cur != iter->next && (iter->next != NULL)) {
      iter->cur = iter->next;
    }

    iter->next = SL_NODE_GET_BACKWARD_POINTER(iter->cur, 0);
H
TD-1548  
Hongze Cheng 已提交
329
    iter->step++;
330 331
  }

H
TD-1437  
Hongze Cheng 已提交
332
  tSkipListUnlock(pSkipList);
333

H
TD-1437  
Hongze Cheng 已提交
334 335
  return (iter->order == TSDB_ORDER_ASC) ? (iter->cur != pSkipList->pTail) : (iter->cur != pSkipList->pHead);
}
336

H
TD-1437  
Hongze Cheng 已提交
337 338 339 340 341
SSkipListNode *tSkipListIterGet(SSkipListIterator *iter) {
  if (iter == NULL || iter->cur == iter->pSkipList->pTail || iter->cur == iter->pSkipList->pHead) {
    return NULL;
  } else {
    return iter->cur;
342 343
  }
}
344

H
TD-1437  
Hongze Cheng 已提交
345 346
void *tSkipListDestroyIter(SSkipListIterator *iter) {
  if (iter == NULL) {
347
    return NULL;
H
hzcheng 已提交
348 349
  }

S
TD-1848  
Shengliang Guan 已提交
350
  tfree(iter);
H
TD-1437  
Hongze Cheng 已提交
351 352
  return NULL;
}
H
hzcheng 已提交
353

H
TD-1437  
Hongze Cheng 已提交
354 355 356
void tSkipListPrint(SSkipList *pSkipList, int16_t nlevel) {
  if (pSkipList == NULL || pSkipList->level < nlevel || nlevel <= 0) {
    return;
357
  }
H
hzcheng 已提交
358

H
TD-1194  
Hongze Cheng 已提交
359
  SSkipListNode *p = SL_NODE_GET_FORWARD_POINTER(pSkipList->pHead, nlevel - 1);
H
TD-1437  
Hongze Cheng 已提交
360 361 362 363 364 365 366

  int32_t id = 1;
  char *  prev = NULL;

  while (p != pSkipList->pTail) {
    char *key = SL_GET_NODE_KEY(pSkipList, p);
    if (prev != NULL) {
H
TD-1194  
Hongze Cheng 已提交
367
      ASSERT(pSkipList->comparFn(prev, key) < 0);
H
TD-1437  
Hongze Cheng 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
    }

    switch (pSkipList->type) {
      case TSDB_DATA_TYPE_INT:
        fprintf(stdout, "%d: %d\n", id++, *(int32_t *)key);
        break;
      case TSDB_DATA_TYPE_SMALLINT:
      case TSDB_DATA_TYPE_TINYINT:
      case TSDB_DATA_TYPE_BIGINT:
        fprintf(stdout, "%d: %" PRId64 " \n", id++, *(int64_t *)key);
        break;
      case TSDB_DATA_TYPE_BINARY:
        fprintf(stdout, "%d: %s \n", id++, key);
        break;
      case TSDB_DATA_TYPE_DOUBLE:
        fprintf(stdout, "%d: %lf \n", id++, *(double *)key);
        break;
      default:
        fprintf(stdout, "\n");
H
hzcheng 已提交
387
    }
H
TD-1437  
Hongze Cheng 已提交
388 389 390

    prev = SL_GET_NODE_KEY(pSkipList, p);

H
TD-1194  
Hongze Cheng 已提交
391
    p = SL_NODE_GET_FORWARD_POINTER(p, nlevel - 1);
H
hzcheng 已提交
392
  }
H
TD-1437  
Hongze Cheng 已提交
393
}
H
hzcheng 已提交
394

H
Hongze Cheng 已提交
395
static void tSkipListDoInsert(SSkipList *pSkipList, SSkipListNode **direction, SSkipListNode *pNode, bool isForward) {
H
TD-1437  
Hongze Cheng 已提交
396
  for (int32_t i = 0; i < pNode->level; ++i) {
H
Hongze Cheng 已提交
397 398 399 400 401 402 403 404 405
    SSkipListNode *x = direction[i];
    if (isForward) {
      SL_NODE_GET_BACKWARD_POINTER(pNode, i) = x;

      SSkipListNode *next = SL_NODE_GET_FORWARD_POINTER(x, i);
      SL_NODE_GET_BACKWARD_POINTER(next, i) = pNode;

      SL_NODE_GET_FORWARD_POINTER(pNode, i) = next;
      SL_NODE_GET_FORWARD_POINTER(x, i) = pNode;
H
TD-1194  
Hongze Cheng 已提交
406
    } else {
H
Hongze Cheng 已提交
407
      SL_NODE_GET_FORWARD_POINTER(pNode, i) = x;
H
TD-1437  
Hongze Cheng 已提交
408

H
Hongze Cheng 已提交
409 410
      SSkipListNode *prev = SL_NODE_GET_BACKWARD_POINTER(x, i);
      SL_NODE_GET_FORWARD_POINTER(prev, i) = pNode;
H
TD-1437  
Hongze Cheng 已提交
411

H
Hongze Cheng 已提交
412
      SL_NODE_GET_BACKWARD_POINTER(pNode, i) = prev;
H
Hongze Cheng 已提交
413
      SL_NODE_GET_BACKWARD_POINTER(x, i) = pNode;
H
TD-1194  
Hongze Cheng 已提交
414
    }
H
TD-1437  
Hongze Cheng 已提交
415 416
  }

H
TD-1194  
Hongze Cheng 已提交
417 418 419
  if (pSkipList->level < pNode->level) pSkipList->level = pNode->level;

  pSkipList->size += 1;
H
hzcheng 已提交
420 421
}

H
TD-1437  
Hongze Cheng 已提交
422 423 424 425 426 427 428
static SSkipListIterator *doCreateSkipListIterator(SSkipList *pSkipList, int32_t order) {
  SSkipListIterator *iter = calloc(1, sizeof(SSkipListIterator));

  iter->pSkipList = pSkipList;
  iter->order = order;
  if (order == TSDB_ORDER_ASC) {
    iter->cur = pSkipList->pHead;
H
Hongze Cheng 已提交
429
    iter->next = SL_NODE_GET_FORWARD_POINTER(iter->cur, 0);
H
TD-1437  
Hongze Cheng 已提交
430 431
  } else {
    iter->cur = pSkipList->pTail;
H
Hongze Cheng 已提交
432
    iter->next = SL_NODE_GET_BACKWARD_POINTER(iter->cur, 0);
H
hzcheng 已提交
433 434
  }

H
TD-1437  
Hongze Cheng 已提交
435 436 437 438
  return iter;
}

static FORCE_INLINE int tSkipListWLock(SSkipList *pSkipList) {
H
TD-1194  
Hongze Cheng 已提交
439
  if (pSkipList->lock) {
H
TD-1437  
Hongze Cheng 已提交
440
    return pthread_rwlock_wrlock(pSkipList->lock);
H
more  
hzcheng 已提交
441
  }
H
TD-1437  
Hongze Cheng 已提交
442 443
  return 0;
}
H
hzcheng 已提交
444

H
TD-1437  
Hongze Cheng 已提交
445
static FORCE_INLINE int tSkipListRLock(SSkipList *pSkipList) {
H
TD-1194  
Hongze Cheng 已提交
446
  if (pSkipList->lock) {
H
TD-1437  
Hongze Cheng 已提交
447 448 449 450
    return pthread_rwlock_rdlock(pSkipList->lock);
  }
  return 0;
}
H
hzcheng 已提交
451

H
TD-1437  
Hongze Cheng 已提交
452
static FORCE_INLINE int tSkipListUnlock(SSkipList *pSkipList) {
H
TD-1194  
Hongze Cheng 已提交
453
  if (pSkipList->lock) {
H
TD-1437  
Hongze Cheng 已提交
454
    return pthread_rwlock_unlock(pSkipList->lock);
H
more  
hzcheng 已提交
455
  }
H
TD-1437  
Hongze Cheng 已提交
456 457
  return 0;
}
H
hzcheng 已提交
458

H
Hongze Cheng 已提交
459
static bool tSkipListGetPosToPut(SSkipList *pSkipList, SSkipListNode **backward, void *pData) {
H
TD-1194  
Hongze Cheng 已提交
460
  int     compare = 0;
H
TD-1437  
Hongze Cheng 已提交
461
  bool    hasDupKey = false;
H
TD-1194  
Hongze Cheng 已提交
462
  char *  pDataKey = pSkipList->keyFn(pData);
H
hzcheng 已提交
463

H
TD-1437  
Hongze Cheng 已提交
464
  if (pSkipList->size == 0) {
H
Hongze Cheng 已提交
465
    for (int i = 0; i < pSkipList->maxLevel; i++) {
H
Hongze Cheng 已提交
466
      backward[i] = pSkipList->pTail;
H
TD-1437  
Hongze Cheng 已提交
467 468 469 470
    }
  } else {
    char *pKey = NULL;

H
Hongze Cheng 已提交
471 472
    // Compare max key
    pKey = SL_GET_MAX_KEY(pSkipList);
H
TD-1194  
Hongze Cheng 已提交
473
    compare = pSkipList->comparFn(pDataKey, pKey);
H
Hongze Cheng 已提交
474
    if (compare >= 0) {
H
Hongze Cheng 已提交
475
      for (int i = 0; i < pSkipList->maxLevel; i++) {
H
Hongze Cheng 已提交
476
        backward[i] = pSkipList->pTail;
H
TD-1437  
Hongze Cheng 已提交
477
      }
H
TD-1194  
Hongze Cheng 已提交
478

H
TD-1437  
Hongze Cheng 已提交
479 480 481
      return (compare == 0);
    }

H
Hongze Cheng 已提交
482 483
    // Compare min key
    pKey = SL_GET_MIN_KEY(pSkipList);
H
TD-1194  
Hongze Cheng 已提交
484
    compare = pSkipList->comparFn(pDataKey, pKey);
H
Hongze Cheng 已提交
485
    if (compare < 0) {
H
Hongze Cheng 已提交
486
      for (int i = 0; i < pSkipList->maxLevel; i++) {
H
Hongze Cheng 已提交
487
        backward[i] = SL_NODE_GET_FORWARD_POINTER(pSkipList->pHead, i);
H
TD-1437  
Hongze Cheng 已提交
488 489 490 491 492
      }

      return (compare == 0);
    }

H
Hongze Cheng 已提交
493
    SSkipListNode *px = pSkipList->pTail;
H
Hongze Cheng 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506 507
    for (int i = pSkipList->maxLevel - 1; i >= 0; --i) {
      if (i < pSkipList->level) {
        SSkipListNode *p = SL_NODE_GET_BACKWARD_POINTER(px, i);
        while (p != pSkipList->pHead) {
          pKey = SL_GET_NODE_KEY(pSkipList, p);

          compare = pSkipList->comparFn(pKey, pDataKey);
          if (compare <= 0) {
            if (compare == 0 && !hasDupKey) hasDupKey = true;
            break;
          } else {
            px = p;
            p = SL_NODE_GET_BACKWARD_POINTER(px, i);
          }
H
TD-1437  
Hongze Cheng 已提交
508 509 510
        }
      }

H
Hongze Cheng 已提交
511
      backward[i] = px;
H
TD-1437  
Hongze Cheng 已提交
512
    }
H
hzcheng 已提交
513 514
  }

H
TD-1437  
Hongze Cheng 已提交
515
  return hasDupKey;
H
hzcheng 已提交
516 517
}

H
TD-1437  
Hongze Cheng 已提交
518 519
static void tSkipListRemoveNodeImpl(SSkipList *pSkipList, SSkipListNode *pNode) {
  int32_t level = pNode->level;
H
TD-1194  
Hongze Cheng 已提交
520
  uint8_t dupMode = SL_DUP_MODE(pSkipList);
H
TD-1548  
Hongze Cheng 已提交
521
  ASSERT(dupMode != SL_DISCARD_DUP_KEY && dupMode != SL_UPDATE_DUP_KEY);
H
TD-1437  
Hongze Cheng 已提交
522

H
TD-1548  
Hongze Cheng 已提交
523 524 525
  for (int32_t j = level - 1; j >= 0; --j) {
    SSkipListNode *prev = SL_NODE_GET_BACKWARD_POINTER(pNode, j);
    SSkipListNode *next = SL_NODE_GET_FORWARD_POINTER(pNode, j);
H
TD-1437  
Hongze Cheng 已提交
526

H
TD-1548  
Hongze Cheng 已提交
527 528
    SL_NODE_GET_FORWARD_POINTER(prev, j) = next;
    SL_NODE_GET_BACKWARD_POINTER(next, j) = prev;
H
hzcheng 已提交
529
  }
H
TD-1548  
Hongze Cheng 已提交
530 531 532

  tSkipListFreeNode(pNode);
  pSkipList->size--;
H
hzcheng 已提交
533 534
}

H
TD-1437  
Hongze Cheng 已提交
535 536
// Function must be called after calling tSkipListRemoveNodeImpl() function
static void tSkipListCorrectLevel(SSkipList *pSkipList) {
H
TD-1194  
Hongze Cheng 已提交
537
  while (pSkipList->level > 0 && SL_NODE_GET_FORWARD_POINTER(pSkipList->pHead, pSkipList->level - 1) == pSkipList->pTail) {
H
TD-1437  
Hongze Cheng 已提交
538
    pSkipList->level -= 1;
H
hzcheng 已提交
539
  }
H
TD-1437  
Hongze Cheng 已提交
540
}
H
hzcheng 已提交
541

H
TD-1437  
Hongze Cheng 已提交
542 543 544 545 546
UNUSED_FUNC static FORCE_INLINE void recordNodeEachLevel(SSkipList *pSkipList,
                                                         int32_t    level) {  // record link count in each level
#if SKIP_LIST_RECORD_PERFORMANCE
  for (int32_t i = 0; i < level; ++i) {
    pSkipList->state.nLevelNodeCnt[i]++;
H
more  
hzcheng 已提交
547
  }
H
TD-1437  
Hongze Cheng 已提交
548 549 550 551 552 553 554
#endif
}

UNUSED_FUNC static FORCE_INLINE void removeNodeEachLevel(SSkipList *pSkipList, int32_t level) {
#if SKIP_LIST_RECORD_PERFORMANCE
  for (int32_t i = 0; i < level; ++i) {
    pSkipList->state.nLevelNodeCnt[i]--;
555
  }
H
TD-1437  
Hongze Cheng 已提交
556 557 558 559 560 561 562
#endif
}

static FORCE_INLINE int32_t getSkipListNodeRandomHeight(SSkipList *pSkipList) {
  const uint32_t factor = 4;

  int32_t n = 1;
S
Shengliang Guan 已提交
563 564 565
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  while ((rand() % factor) == 0 && n <= pSkipList->maxLevel) {
#else
S
TD-4170  
Shengliang Guan 已提交
566
  while ((rand_r(&(pSkipList->seed)) % factor) == 0 && n <= pSkipList->maxLevel) {
S
Shengliang Guan 已提交
567
#endif
H
TD-1437  
Hongze Cheng 已提交
568
    n++;
569
  }
H
hzcheng 已提交
570

H
TD-1437  
Hongze Cheng 已提交
571 572 573 574 575 576 577 578 579 580 581 582
  return n;
}

static FORCE_INLINE int32_t getSkipListRandLevel(SSkipList *pSkipList) {
  int32_t level = 0;
  if (pSkipList->size == 0) {
    level = 1;
  } else {
    level = getSkipListNodeRandomHeight(pSkipList);
    if (level > pSkipList->level) {
      if (pSkipList->level < pSkipList->maxLevel) {
        level = pSkipList->level + 1;
H
more  
hzcheng 已提交
583
      } else {
H
TD-1437  
Hongze Cheng 已提交
584
        level = pSkipList->level;
H
more  
hzcheng 已提交
585
      }
H
hzcheng 已提交
586 587 588
    }
  }

H
TD-1194  
Hongze Cheng 已提交
589
  ASSERT(level <= pSkipList->maxLevel);
H
TD-1437  
Hongze Cheng 已提交
590 591
  return level;
}
592

H
TD-1437  
Hongze Cheng 已提交
593 594
// when order is TSDB_ORDER_ASC, return the last node with key less than val
// when order is TSDB_ORDER_DESC, return the first node with key large than val
H
Hongze Cheng 已提交
595
static SSkipListNode *getPriorNode(SSkipList *pSkipList, const char *val, int32_t order, SSkipListNode **pCur) {
H
TD-1437  
Hongze Cheng 已提交
596 597
  __compar_fn_t  comparFn = pSkipList->comparFn;
  SSkipListNode *pNode = NULL;
H
Hongze Cheng 已提交
598 599 600
  if (pCur != NULL) {
    *pCur = NULL;
  }
H
TD-1437  
Hongze Cheng 已提交
601 602 603 604

  if (order == TSDB_ORDER_ASC) {
    pNode = pSkipList->pHead;
    for (int32_t i = pSkipList->level - 1; i >= 0; --i) {
H
TD-1194  
Hongze Cheng 已提交
605
      SSkipListNode *p = SL_NODE_GET_FORWARD_POINTER(pNode, i);
H
TD-1437  
Hongze Cheng 已提交
606 607 608 609
      while (p != pSkipList->pTail) {
        char *key = SL_GET_NODE_KEY(pSkipList, p);
        if (comparFn(key, val) < 0) {
          pNode = p;
H
TD-1194  
Hongze Cheng 已提交
610
          p = SL_NODE_GET_FORWARD_POINTER(p, i);
H
TD-1437  
Hongze Cheng 已提交
611
        } else {
H
Hongze Cheng 已提交
612 613 614
          if (pCur != NULL) {
            *pCur = p;
          }
H
TD-1437  
Hongze Cheng 已提交
615 616 617 618 619 620 621
          break;
        }
      }
    }
  } else {
    pNode = pSkipList->pTail;
    for (int32_t i = pSkipList->level - 1; i >= 0; --i) {
H
TD-1194  
Hongze Cheng 已提交
622
      SSkipListNode *p = SL_NODE_GET_BACKWARD_POINTER(pNode, i);
H
TD-1437  
Hongze Cheng 已提交
623 624 625 626
      while (p != pSkipList->pHead) {
        char *key = SL_GET_NODE_KEY(pSkipList, p);
        if (comparFn(key, val) > 0) {
          pNode = p;
H
TD-1194  
Hongze Cheng 已提交
627
          p = SL_NODE_GET_BACKWARD_POINTER(p, i);
H
TD-1437  
Hongze Cheng 已提交
628
        } else {
H
Hongze Cheng 已提交
629 630 631
          if (pCur != NULL) {
            *pCur = p;
          }
H
TD-1437  
Hongze Cheng 已提交
632 633 634 635
          break;
        }
      }
    }
H
hzcheng 已提交
636
  }
H
TD-1437  
Hongze Cheng 已提交
637

H
hzcheng 已提交
638 639 640
  return pNode;
}

H
TD-1437  
Hongze Cheng 已提交
641 642
static int initForwardBackwardPtr(SSkipList *pSkipList) {
  uint32_t maxLevel = pSkipList->maxLevel;
643

H
TD-1437  
Hongze Cheng 已提交
644
  // head info
H
TD-1194  
Hongze Cheng 已提交
645 646
  pSkipList->pHead = tSkipListNewNode(maxLevel);
  if (pSkipList->pHead == NULL) return -1;
647

H
TD-1437  
Hongze Cheng 已提交
648
  // tail info
H
TD-1194  
Hongze Cheng 已提交
649 650 651 652 653
  pSkipList->pTail = tSkipListNewNode(maxLevel);
  if (pSkipList->pTail == NULL) {
    tSkipListFreeNode(pSkipList->pHead);
    return -1;
  }
H
TD-1437  
Hongze Cheng 已提交
654 655

  for (uint32_t i = 0; i < maxLevel; ++i) {
H
TD-1194  
Hongze Cheng 已提交
656 657
    SL_NODE_GET_FORWARD_POINTER(pSkipList->pHead, i) = pSkipList->pTail;
    SL_NODE_GET_BACKWARD_POINTER(pSkipList->pTail, i) = pSkipList->pHead;
658 659
  }

H
TD-1437  
Hongze Cheng 已提交
660
  return 0;
H
more  
hzcheng 已提交
661 662
}

H
TD-1194  
Hongze Cheng 已提交
663 664
static SSkipListNode *tSkipListNewNode(uint8_t level) {
  int32_t tsize = sizeof(SSkipListNode) + sizeof(SSkipListNode *) * level * 2;
665

H
TD-1194  
Hongze Cheng 已提交
666 667
  SSkipListNode *pNode = (SSkipListNode *)calloc(1, tsize);
  if (pNode == NULL) return NULL;
H
TD-1437  
Hongze Cheng 已提交
668

H
TD-1194  
Hongze Cheng 已提交
669 670
  pNode->level = level;
  return pNode;
H
hjxilinx 已提交
671 672
}

H
Hongze Cheng 已提交
673 674 675 676 677
static SSkipListNode *tSkipListPutImpl(SSkipList *pSkipList, void *pData, SSkipListNode **direction, bool isForward,
                                       bool hasDup) {
  uint8_t        dupMode = SL_DUP_MODE(pSkipList);
  SSkipListNode *pNode = NULL;

678
  if (hasDup && (dupMode != SL_ALLOW_DUP_KEY)) {
H
Hongze Cheng 已提交
679 680 681 682 683 684
    if (dupMode == SL_UPDATE_DUP_KEY) {
      if (isForward) {
        pNode = SL_NODE_GET_FORWARD_POINTER(direction[0], 0);
      } else {
        pNode = SL_NODE_GET_BACKWARD_POINTER(direction[0], 0);
      }
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
      if (pSkipList->insertHandleFn) {
        pSkipList->insertHandleFn->args[0] = pData;
        pSkipList->insertHandleFn->args[1] = pNode->pData;
        pData = genericInvoke(pSkipList->insertHandleFn);
      }
      if(pData) {
        atomic_store_ptr(&(pNode->pData), pData);
      }
    } else {
      //for compatiblity, duplicate key inserted when update=0 should be also calculated as affected rows!
      if(pSkipList->insertHandleFn) {
        pSkipList->insertHandleFn->args[0] = NULL;
        pSkipList->insertHandleFn->args[1] = NULL;
        genericInvoke(pSkipList->insertHandleFn);
      }
H
Hongze Cheng 已提交
700 701 702 703
    }
  } else {
    pNode = tSkipListNewNode(getSkipListRandLevel(pSkipList));
    if (pNode != NULL) {
704 705 706 707 708 709 710 711
      // insertHandleFn will be assigned only for timeseries data,
      // in which case, pData is pointed to an memory to be freed later;
      // while for metadata, the mem alloc will not be called.
      if (pSkipList->insertHandleFn) {
        pSkipList->insertHandleFn->args[0] = pData;
        pSkipList->insertHandleFn->args[1] = NULL;
        pData = genericInvoke(pSkipList->insertHandleFn);
      }
H
Hongze Cheng 已提交
712 713 714 715 716 717 718 719
      pNode->pData = pData;

      tSkipListDoInsert(pSkipList, direction, pNode, isForward);
    }
  }

  return pNode;
}