tskiplist.c 20.4 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/>.
 */
H
TD-1437  
Hongze Cheng 已提交
16
#include "tskiplist.h"
H
hjxilinx 已提交
17
#include "os.h"
H
TD-1437  
Hongze Cheng 已提交
18
#include "tcompare.h"
S
slguan 已提交
19
#include "tulog.h"
H
hzcheng 已提交
20 21
#include "tutil.h"

H
TD-1194  
Hongze Cheng 已提交
22
static int                initForwardBackwardPtr(SSkipList *pSkipList);
H
Hongze Cheng 已提交
23
static SSkipListNode *    getPriorNode(SSkipList *pSkipList, const char *val, int32_t order, SSkipListNode **pCur);
H
TD-1194  
Hongze Cheng 已提交
24 25
static void               tSkipListRemoveNodeImpl(SSkipList *pSkipList, SSkipListNode *pNode);
static void               tSkipListCorrectLevel(SSkipList *pSkipList);
H
TD-1437  
Hongze Cheng 已提交
26
static SSkipListIterator *doCreateSkipListIterator(SSkipList *pSkipList, int32_t order);
H
Hongze Cheng 已提交
27 28 29
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 已提交
30
#define tSkipListFreeNode(n) tfree((n))
H
Hongze Cheng 已提交
31 32
static SSkipListNode *tSkipListPutImpl(SSkipList *pSkipList, void *pData, SSkipListNode **direction, bool isForward,
                                       bool hasDup);
H
TD-1437  
Hongze Cheng 已提交
33

34

H
TD-1437  
Hongze Cheng 已提交
35 36 37 38 39
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 已提交
40 41
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 已提交
42
  SSkipList *pSkipList = (SSkipList *)calloc(1, sizeof(SSkipList));
H
TD-1194  
Hongze Cheng 已提交
43
  if (pSkipList == NULL) return NULL;
H
TD-1437  
Hongze Cheng 已提交
44 45 46 47 48 49 50 51 52 53

  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 已提交
54
  pSkipList->seed = rand();
H
TD-1548  
Hongze Cheng 已提交
55
  if (comparFn == NULL) {
56
    pSkipList->comparFn = getKeyComparFunc(keyType, TSDB_ORDER_ASC);
H
TD-1548  
Hongze Cheng 已提交
57 58 59
  } else {
    pSkipList->comparFn = comparFn;
  }
H
TD-1437  
Hongze Cheng 已提交
60 61

  if (initForwardBackwardPtr(pSkipList) < 0) {
H
TD-1194  
Hongze Cheng 已提交
62
    tSkipListDestroy(pSkipList);
H
TD-1437  
Hongze Cheng 已提交
63
    return NULL;
S
slguan 已提交
64
  }
H
TD-1437  
Hongze Cheng 已提交
65

H
TD-1194  
Hongze Cheng 已提交
66
  if (SL_IS_THREAD_SAFE(pSkipList)) {
H
TD-1437  
Hongze Cheng 已提交
67
    pSkipList->lock = (pthread_rwlock_t *)calloc(1, sizeof(pthread_rwlock_t));
H
TD-1194  
Hongze Cheng 已提交
68 69 70 71
    if (pSkipList->lock == NULL) {
      tSkipListDestroy(pSkipList);
      return NULL;
    }
H
TD-1437  
Hongze Cheng 已提交
72 73

    if (pthread_rwlock_init(pSkipList->lock, NULL) != 0) {
H
TD-1194  
Hongze Cheng 已提交
74
      tSkipListDestroy(pSkipList);
H
TD-1437  
Hongze Cheng 已提交
75 76 77 78 79 80 81 82
      return NULL;
    }
  }

  srand((uint32_t)time(NULL));

#if SKIP_LIST_RECORD_PERFORMANCE
  pSkipList->state.nTotalMemSize += sizeof(SSkipList);
H
more  
hzcheng 已提交
83
#endif
84
  pSkipList->insertHandleFn = NULL;
H
TD-1437  
Hongze Cheng 已提交
85 86

  return pSkipList;
S
slguan 已提交
87
}
H
hzcheng 已提交
88

H
TD-1194  
Hongze Cheng 已提交
89 90
void tSkipListDestroy(SSkipList *pSkipList) {
  if (pSkipList == NULL) return;
H
TD-1437  
Hongze Cheng 已提交
91 92 93

  tSkipListWLock(pSkipList);

H
TD-1194  
Hongze Cheng 已提交
94
  SSkipListNode *pNode = SL_NODE_GET_FORWARD_POINTER(pSkipList->pHead, 0);
H
TD-1437  
Hongze Cheng 已提交
95 96 97

  while (pNode != pSkipList->pTail) {
    SSkipListNode *pTemp = pNode;
H
TD-1194  
Hongze Cheng 已提交
98 99
    pNode = SL_NODE_GET_FORWARD_POINTER(pNode, 0);
    tSkipListFreeNode(pTemp);
S
slguan 已提交
100
  }
H
TD-1437  
Hongze Cheng 已提交
101

102 103
  tfree(pSkipList->insertHandleFn);

H
TD-1437  
Hongze Cheng 已提交
104 105 106
  tSkipListUnlock(pSkipList);
  if (pSkipList->lock != NULL) {
    pthread_rwlock_destroy(pSkipList->lock);
S
TD-1848  
Shengliang Guan 已提交
107
    tfree(pSkipList->lock);
H
TD-1437  
Hongze Cheng 已提交
108 109
  }

H
TD-1194  
Hongze Cheng 已提交
110 111
  tSkipListFreeNode(pSkipList->pHead);
  tSkipListFreeNode(pSkipList->pTail);
S
TD-1848  
Shengliang Guan 已提交
112
  tfree(pSkipList);
H
TD-1437  
Hongze Cheng 已提交
113 114
}

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

H
Hongze Cheng 已提交
118
  SSkipListNode *backward[MAX_SKIP_LIST_LEVEL] = {0};
H
TD-1194  
Hongze Cheng 已提交
119 120
  SSkipListNode *pNode = NULL;

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

H
Hongze Cheng 已提交
123
  bool hasDup = tSkipListGetPosToPut(pSkipList, backward, pData);
H
Hongze Cheng 已提交
124
  pNode = tSkipListPutImpl(pSkipList, pData, backward, false, hasDup);
H
TD-1437  
Hongze Cheng 已提交
125

H
Hongze Cheng 已提交
126 127 128 129 130
  tSkipListUnlock(pSkipList);

  return pNode;
}

131
void tSkipListPutBatchByIter(SSkipList *pSkipList, void *iter, iter_next_fn_t iterate) {
H
Hongze Cheng 已提交
132 133 134 135 136 137 138 139
  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 已提交
140

141 142 143
  void* pData = iterate(iter);
  if(pData == NULL) return;

H
Hongze Cheng 已提交
144
  // backward to put the first data
145 146 147
  hasDup = tSkipListGetPosToPut(pSkipList, backward, pData);

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

H
Hongze Cheng 已提交
149
  for (int level = 0; level < pSkipList->maxLevel; level++) {
H
Hongze Cheng 已提交
150 151 152 153
    forward[level] = SL_NODE_GET_BACKWARD_POINTER(backward[level], level);
  }

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

    // 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 已提交
183
              if (compare == 0 && !hasDup) hasDup = true;
H
Hongze Cheng 已提交
184 185 186 187 188 189 190 191 192 193
              break;
            } else {
              px = p;
              p = SL_NODE_GET_FORWARD_POINTER(px, i);
            }
          }
        }

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

196
    tSkipListPutImpl(pSkipList, pData, forward, true, hasDup);
H
TD-1437  
Hongze Cheng 已提交
197 198
  }
  tSkipListUnlock(pSkipList);
H
hzcheng 已提交
199 200
}

H
TD-1194  
Hongze Cheng 已提交
201 202
uint32_t tSkipListRemove(SSkipList *pSkipList, SSkipListKey key) {
  uint32_t count = 0;
H
TD-1437  
Hongze Cheng 已提交
203 204 205

  tSkipListWLock(pSkipList);

H
Hongze Cheng 已提交
206
  SSkipListNode *pNode = getPriorNode(pSkipList, key, TSDB_ORDER_ASC, NULL);
H
TD-1437  
Hongze Cheng 已提交
207
  while (1) {
H
TD-1194  
Hongze Cheng 已提交
208
    SSkipListNode *p = SL_NODE_GET_FORWARD_POINTER(pNode, 0);
H
TD-1437  
Hongze Cheng 已提交
209 210 211 212 213
    if (p == pSkipList->pTail) {
      break;
    }
    if (pSkipList->comparFn(key, SL_GET_NODE_KEY(pSkipList, p)) != 0) {
      break;
S
slguan 已提交
214
    }
H
TD-1194  
Hongze Cheng 已提交
215 216 217 218

    tSkipListRemoveNodeImpl(pSkipList, p);

    ++count;
S
slguan 已提交
219
  }
H
TD-1437  
Hongze Cheng 已提交
220

H
TD-1194  
Hongze Cheng 已提交
221 222
  tSkipListCorrectLevel(pSkipList);

H
TD-1437  
Hongze Cheng 已提交
223 224
  tSkipListUnlock(pSkipList);

H
TD-1194  
Hongze Cheng 已提交
225
  return count;
S
slguan 已提交
226 227
}

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

H
TD-1194  
Hongze Cheng 已提交
231
  tSkipListRLock(pSkipList);
232

H
Hongze Cheng 已提交
233
  SSkipListNode *pNode = getPriorNode(pSkipList, key, TSDB_ORDER_ASC, NULL);
H
TD-1437  
Hongze Cheng 已提交
234
  while (1) {
H
TD-1194  
Hongze Cheng 已提交
235
    SSkipListNode *p = SL_NODE_GET_FORWARD_POINTER(pNode, 0);
H
TD-1437  
Hongze Cheng 已提交
236 237 238 239 240 241
    if (p == pSkipList->pTail) {
      break;
    }
    if (pSkipList->comparFn(key, SL_GET_NODE_KEY(pSkipList, p)) != 0) {
      break;
    }
H
TD-1548  
Hongze Cheng 已提交
242
    taosArrayPush(sa, &p);
H
TD-1194  
Hongze Cheng 已提交
243
    pNode = p;
H
TD-1437  
Hongze Cheng 已提交
244 245 246 247
  }

  tSkipListUnlock(pSkipList);

H
TD-1194  
Hongze Cheng 已提交
248
  return sa;
H
TD-1437  
Hongze Cheng 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
}

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 已提交
265 266
  ASSERT(order == TSDB_ORDER_ASC || order == TSDB_ORDER_DESC);
  ASSERT(pSkipList != NULL);
H
TD-1437  
Hongze Cheng 已提交
267 268 269 270 271 272 273 274

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

  tSkipListRLock(pSkipList);

H
Hongze Cheng 已提交
275
  iter->cur = getPriorNode(pSkipList, val, order, &(iter->next));
H
TD-1437  
Hongze Cheng 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288

  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 已提交
289
  if (iter->order == TSDB_ORDER_ASC) {
290 291 292 293 294 295 296
    // 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 已提交
297
    iter->cur = SL_NODE_GET_FORWARD_POINTER(iter->cur, 0);
H
Hongze Cheng 已提交
298 299 300 301 302 303 304

    // 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 已提交
305
    iter->step++;
H
TD-1194  
Hongze Cheng 已提交
306
  } else {
H
Hongze Cheng 已提交
307
    if (iter->cur == pSkipList->pHead) {
308
      iter->cur = pSkipList->pHead;
H
Hongze Cheng 已提交
309 310 311
      tSkipListUnlock(pSkipList);
      return false;
    }
312

H
TD-1548  
Hongze Cheng 已提交
313
    iter->cur = SL_NODE_GET_BACKWARD_POINTER(iter->cur, 0);
H
Hongze Cheng 已提交
314 315 316 317 318 319 320

    // 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 已提交
321
    iter->step++;
322 323
  }

H
TD-1437  
Hongze Cheng 已提交
324
  tSkipListUnlock(pSkipList);
325

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

H
TD-1437  
Hongze Cheng 已提交
329 330 331 332 333
SSkipListNode *tSkipListIterGet(SSkipListIterator *iter) {
  if (iter == NULL || iter->cur == iter->pSkipList->pTail || iter->cur == iter->pSkipList->pHead) {
    return NULL;
  } else {
    return iter->cur;
334 335
  }
}
336

H
TD-1437  
Hongze Cheng 已提交
337 338
void *tSkipListDestroyIter(SSkipListIterator *iter) {
  if (iter == NULL) {
339
    return NULL;
H
hzcheng 已提交
340 341
  }

S
TD-1848  
Shengliang Guan 已提交
342
  tfree(iter);
H
TD-1437  
Hongze Cheng 已提交
343 344
  return NULL;
}
H
hzcheng 已提交
345

H
TD-1437  
Hongze Cheng 已提交
346 347 348
void tSkipListPrint(SSkipList *pSkipList, int16_t nlevel) {
  if (pSkipList == NULL || pSkipList->level < nlevel || nlevel <= 0) {
    return;
349
  }
H
hzcheng 已提交
350

H
TD-1194  
Hongze Cheng 已提交
351
  SSkipListNode *p = SL_NODE_GET_FORWARD_POINTER(pSkipList->pHead, nlevel - 1);
H
TD-1437  
Hongze Cheng 已提交
352 353 354 355 356 357 358

  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 已提交
359
      ASSERT(pSkipList->comparFn(prev, key) < 0);
H
TD-1437  
Hongze Cheng 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    }

    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 已提交
379
    }
H
TD-1437  
Hongze Cheng 已提交
380 381 382

    prev = SL_GET_NODE_KEY(pSkipList, p);

H
TD-1194  
Hongze Cheng 已提交
383
    p = SL_NODE_GET_FORWARD_POINTER(p, nlevel - 1);
H
hzcheng 已提交
384
  }
H
TD-1437  
Hongze Cheng 已提交
385
}
H
hzcheng 已提交
386

H
Hongze Cheng 已提交
387
static void tSkipListDoInsert(SSkipList *pSkipList, SSkipListNode **direction, SSkipListNode *pNode, bool isForward) {
H
TD-1437  
Hongze Cheng 已提交
388
  for (int32_t i = 0; i < pNode->level; ++i) {
H
Hongze Cheng 已提交
389 390 391 392 393 394 395 396 397
    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 已提交
398
    } else {
H
Hongze Cheng 已提交
399
      SL_NODE_GET_FORWARD_POINTER(pNode, i) = x;
H
TD-1437  
Hongze Cheng 已提交
400

H
Hongze Cheng 已提交
401 402
      SSkipListNode *prev = SL_NODE_GET_BACKWARD_POINTER(x, i);
      SL_NODE_GET_FORWARD_POINTER(prev, i) = pNode;
H
TD-1437  
Hongze Cheng 已提交
403

H
Hongze Cheng 已提交
404
      SL_NODE_GET_BACKWARD_POINTER(pNode, i) = prev;
H
Hongze Cheng 已提交
405
      SL_NODE_GET_BACKWARD_POINTER(x, i) = pNode;
H
TD-1194  
Hongze Cheng 已提交
406
    }
H
TD-1437  
Hongze Cheng 已提交
407 408
  }

H
TD-1194  
Hongze Cheng 已提交
409 410 411
  if (pSkipList->level < pNode->level) pSkipList->level = pNode->level;

  pSkipList->size += 1;
H
hzcheng 已提交
412 413
}

H
TD-1437  
Hongze Cheng 已提交
414 415 416 417 418 419 420
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 已提交
421
    iter->next = SL_NODE_GET_FORWARD_POINTER(iter->cur, 0);
H
TD-1437  
Hongze Cheng 已提交
422 423
  } else {
    iter->cur = pSkipList->pTail;
H
Hongze Cheng 已提交
424
    iter->next = SL_NODE_GET_BACKWARD_POINTER(iter->cur, 0);
H
hzcheng 已提交
425 426
  }

H
TD-1437  
Hongze Cheng 已提交
427 428 429 430
  return iter;
}

static FORCE_INLINE int tSkipListWLock(SSkipList *pSkipList) {
H
TD-1194  
Hongze Cheng 已提交
431
  if (pSkipList->lock) {
H
TD-1437  
Hongze Cheng 已提交
432
    return pthread_rwlock_wrlock(pSkipList->lock);
H
more  
hzcheng 已提交
433
  }
H
TD-1437  
Hongze Cheng 已提交
434 435
  return 0;
}
H
hzcheng 已提交
436

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

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

H
Hongze Cheng 已提交
451
static bool tSkipListGetPosToPut(SSkipList *pSkipList, SSkipListNode **backward, void *pData) {
H
TD-1194  
Hongze Cheng 已提交
452
  int     compare = 0;
H
TD-1437  
Hongze Cheng 已提交
453
  bool    hasDupKey = false;
H
TD-1194  
Hongze Cheng 已提交
454
  char *  pDataKey = pSkipList->keyFn(pData);
H
hzcheng 已提交
455

H
TD-1437  
Hongze Cheng 已提交
456
  if (pSkipList->size == 0) {
H
Hongze Cheng 已提交
457
    for (int i = 0; i < pSkipList->maxLevel; i++) {
H
Hongze Cheng 已提交
458
      backward[i] = pSkipList->pTail;
H
TD-1437  
Hongze Cheng 已提交
459 460 461 462
    }
  } else {
    char *pKey = NULL;

H
Hongze Cheng 已提交
463 464
    // Compare max key
    pKey = SL_GET_MAX_KEY(pSkipList);
H
TD-1194  
Hongze Cheng 已提交
465
    compare = pSkipList->comparFn(pDataKey, pKey);
H
Hongze Cheng 已提交
466
    if (compare >= 0) {
H
Hongze Cheng 已提交
467
      for (int i = 0; i < pSkipList->maxLevel; i++) {
H
Hongze Cheng 已提交
468
        backward[i] = pSkipList->pTail;
H
TD-1437  
Hongze Cheng 已提交
469
      }
H
TD-1194  
Hongze Cheng 已提交
470

H
TD-1437  
Hongze Cheng 已提交
471 472 473
      return (compare == 0);
    }

H
Hongze Cheng 已提交
474 475
    // Compare min key
    pKey = SL_GET_MIN_KEY(pSkipList);
H
TD-1194  
Hongze Cheng 已提交
476
    compare = pSkipList->comparFn(pDataKey, pKey);
H
Hongze Cheng 已提交
477
    if (compare < 0) {
H
Hongze Cheng 已提交
478
      for (int i = 0; i < pSkipList->maxLevel; i++) {
H
Hongze Cheng 已提交
479
        backward[i] = SL_NODE_GET_FORWARD_POINTER(pSkipList->pHead, i);
H
TD-1437  
Hongze Cheng 已提交
480 481 482 483 484
      }

      return (compare == 0);
    }

H
Hongze Cheng 已提交
485
    SSkipListNode *px = pSkipList->pTail;
H
Hongze Cheng 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499
    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 已提交
500 501 502
        }
      }

H
Hongze Cheng 已提交
503
      backward[i] = px;
H
TD-1437  
Hongze Cheng 已提交
504
    }
H
hzcheng 已提交
505 506
  }

H
TD-1437  
Hongze Cheng 已提交
507
  return hasDupKey;
H
hzcheng 已提交
508 509
}

H
TD-1437  
Hongze Cheng 已提交
510 511
static void tSkipListRemoveNodeImpl(SSkipList *pSkipList, SSkipListNode *pNode) {
  int32_t level = pNode->level;
H
TD-1194  
Hongze Cheng 已提交
512
  uint8_t dupMode = SL_DUP_MODE(pSkipList);
H
TD-1548  
Hongze Cheng 已提交
513
  ASSERT(dupMode != SL_DISCARD_DUP_KEY && dupMode != SL_UPDATE_DUP_KEY);
H
TD-1437  
Hongze Cheng 已提交
514

H
TD-1548  
Hongze Cheng 已提交
515 516 517
  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 已提交
518

H
TD-1548  
Hongze Cheng 已提交
519 520
    SL_NODE_GET_FORWARD_POINTER(prev, j) = next;
    SL_NODE_GET_BACKWARD_POINTER(next, j) = prev;
H
hzcheng 已提交
521
  }
H
TD-1548  
Hongze Cheng 已提交
522 523 524

  tSkipListFreeNode(pNode);
  pSkipList->size--;
H
hzcheng 已提交
525 526
}

H
TD-1437  
Hongze Cheng 已提交
527 528
// Function must be called after calling tSkipListRemoveNodeImpl() function
static void tSkipListCorrectLevel(SSkipList *pSkipList) {
H
TD-1194  
Hongze Cheng 已提交
529
  while (pSkipList->level > 0 && SL_NODE_GET_FORWARD_POINTER(pSkipList->pHead, pSkipList->level - 1) == pSkipList->pTail) {
H
TD-1437  
Hongze Cheng 已提交
530
    pSkipList->level -= 1;
H
hzcheng 已提交
531
  }
H
TD-1437  
Hongze Cheng 已提交
532
}
H
hzcheng 已提交
533

H
TD-1437  
Hongze Cheng 已提交
534 535 536 537 538
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 已提交
539
  }
H
TD-1437  
Hongze Cheng 已提交
540 541 542 543 544 545 546
#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]--;
547
  }
H
TD-1437  
Hongze Cheng 已提交
548 549 550 551 552 553 554
#endif
}

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

  int32_t n = 1;
S
Shengliang Guan 已提交
555 556 557
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  while ((rand() % factor) == 0 && n <= pSkipList->maxLevel) {
#else
S
TD-4170  
Shengliang Guan 已提交
558
  while ((rand_r(&(pSkipList->seed)) % factor) == 0 && n <= pSkipList->maxLevel) {
S
Shengliang Guan 已提交
559
#endif
H
TD-1437  
Hongze Cheng 已提交
560
    n++;
561
  }
H
hzcheng 已提交
562

H
TD-1437  
Hongze Cheng 已提交
563 564 565 566 567 568 569 570 571 572 573 574
  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 已提交
575
      } else {
H
TD-1437  
Hongze Cheng 已提交
576
        level = pSkipList->level;
H
more  
hzcheng 已提交
577
      }
H
hzcheng 已提交
578 579 580
    }
  }

H
TD-1194  
Hongze Cheng 已提交
581
  ASSERT(level <= pSkipList->maxLevel);
H
TD-1437  
Hongze Cheng 已提交
582 583
  return level;
}
584

H
TD-1437  
Hongze Cheng 已提交
585 586
// 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 已提交
587
static SSkipListNode *getPriorNode(SSkipList *pSkipList, const char *val, int32_t order, SSkipListNode **pCur) {
H
TD-1437  
Hongze Cheng 已提交
588 589
  __compar_fn_t  comparFn = pSkipList->comparFn;
  SSkipListNode *pNode = NULL;
H
Hongze Cheng 已提交
590 591 592
  if (pCur != NULL) {
    *pCur = NULL;
  }
H
TD-1437  
Hongze Cheng 已提交
593 594 595 596

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

H
hzcheng 已提交
630 631 632
  return pNode;
}

H
TD-1437  
Hongze Cheng 已提交
633 634
static int initForwardBackwardPtr(SSkipList *pSkipList) {
  uint32_t maxLevel = pSkipList->maxLevel;
635

H
TD-1437  
Hongze Cheng 已提交
636
  // head info
H
TD-1194  
Hongze Cheng 已提交
637 638
  pSkipList->pHead = tSkipListNewNode(maxLevel);
  if (pSkipList->pHead == NULL) return -1;
639

H
TD-1437  
Hongze Cheng 已提交
640
  // tail info
H
TD-1194  
Hongze Cheng 已提交
641 642 643 644 645
  pSkipList->pTail = tSkipListNewNode(maxLevel);
  if (pSkipList->pTail == NULL) {
    tSkipListFreeNode(pSkipList->pHead);
    return -1;
  }
H
TD-1437  
Hongze Cheng 已提交
646 647

  for (uint32_t i = 0; i < maxLevel; ++i) {
H
TD-1194  
Hongze Cheng 已提交
648 649
    SL_NODE_GET_FORWARD_POINTER(pSkipList->pHead, i) = pSkipList->pTail;
    SL_NODE_GET_BACKWARD_POINTER(pSkipList->pTail, i) = pSkipList->pHead;
650 651
  }

H
TD-1437  
Hongze Cheng 已提交
652
  return 0;
H
more  
hzcheng 已提交
653 654
}

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

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

H
TD-1194  
Hongze Cheng 已提交
661 662
  pNode->level = level;
  return pNode;
H
hjxilinx 已提交
663 664
}

H
Hongze Cheng 已提交
665 666 667 668 669
static SSkipListNode *tSkipListPutImpl(SSkipList *pSkipList, void *pData, SSkipListNode **direction, bool isForward,
                                       bool hasDup) {
  uint8_t        dupMode = SL_DUP_MODE(pSkipList);
  SSkipListNode *pNode = NULL;

670
  if (hasDup && (dupMode != SL_ALLOW_DUP_KEY)) {
H
Hongze Cheng 已提交
671 672 673 674 675 676
    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);
      }
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
      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 已提交
692 693 694 695
    }
  } else {
    pNode = tSkipListNewNode(getSkipListRandLevel(pSkipList));
    if (pNode != NULL) {
696 697 698 699 700 701 702 703
      // 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 已提交
704 705 706 707 708 709 710 711
      pNode->pData = pData;

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

  return pNode;
}