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"
19
#include "tdataformat.h"
S
slguan 已提交
20
#include "tulog.h"
H
hzcheng 已提交
21 22
#include "tutil.h"

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

35

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

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

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

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

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

  srand((uint32_t)time(NULL));

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

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

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

  tSkipListWLock(pSkipList);

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

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

103 104
  tfree(pSkipList->insertHandleFn);

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

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

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

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

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

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

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

  return pNode;
}

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

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

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

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

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

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

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

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

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

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

  tSkipListWLock(pSkipList);

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

    tSkipListRemoveNodeImpl(pSkipList, p);

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

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

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

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

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

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

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

  tSkipListUnlock(pSkipList);

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

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

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

  tSkipListRLock(pSkipList);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    prev = SL_GET_NODE_KEY(pSkipList, p);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      return (compare == 0);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return pNode;
}