diff --git a/src/util/inc/tskiplist.h b/src/util/inc/tskiplist.h index 176135cf92c8ae5ffdd8d93bd8acb48f05331f77..4634c148816d2c9d47792ae0d0961e2434667819 100644 --- a/src/util/inc/tskiplist.h +++ b/src/util/inc/tskiplist.h @@ -50,6 +50,8 @@ typedef struct SSkipListNode { #define SL_GET_NODE_DATA(n) ((char *)(n) + SL_NODE_HEADER_SIZE((n)->level)) #define SL_GET_NODE_KEY(s, n) ((s)->keyFn(SL_GET_NODE_DATA(n))) +#define SL_GET_SL_MIN_KEY(s) (SL_GET_NODE_KEY((s), SL_GET_FORWARD_POINTER((s)->pHead, 0))) + #define SL_GET_NODE_LEVEL(n) *(uint8_t *)((n)) /* diff --git a/src/util/src/tskiplist.c b/src/util/src/tskiplist.c index ee090130993e2d87838f8453a5ead4e9837c6b92..109b4b45b6fa56975ed31622a912332ede276531 100644 --- a/src/util/src/tskiplist.c +++ b/src/util/src/tskiplist.c @@ -71,7 +71,8 @@ memset(pNode, 0, SL_NODE_HEADER_SIZE(_l));\ } while(0) static void tSkipListDoInsert(SSkipList *pSkipList, SSkipListNode **forward, SSkipListNode *pNode); -static SSkipListNode* tSkipListDoAppend(SSkipList *pSkipList, SSkipListNode *pNode); +static SSkipListNode* tSkipListPushBack(SSkipList *pSkipList, SSkipListNode *pNode); +static SSkipListNode* tSkipListPushFront(SSkipList* pSkipList, SSkipListNode *pNode); static SSkipListIterator* doCreateSkipListIterator(SSkipList *pSkipList, int32_t order); static bool initForwardBackwardPtr(SSkipList* pSkipList) { @@ -207,10 +208,17 @@ SSkipListNode *tSkipListPut(SSkipList *pSkipList, SSkipListNode *pNode) { pthread_rwlock_wrlock(pSkipList->lock); } - // the new key is greater than the last key of skiplist append it at last position + // if the new key is greater than the maximum key of skip list, push back this node at the end of skip list char *newDatakey = SL_GET_NODE_KEY(pSkipList, pNode); if (pSkipList->size == 0 || pSkipList->comparFn(pSkipList->lastKey, newDatakey) < 0) { - return tSkipListDoAppend(pSkipList, pNode); + return tSkipListPushBack(pSkipList, pNode); + } + + // if the new key is less than the minimum key of skip list, push front this node at the front of skip list + assert(pSkipList->size > 0); + char* minKey = SL_GET_SL_MIN_KEY(pSkipList); + if (pSkipList->comparFn(newDatakey, minKey) < 0) { + return tSkipListPushFront(pSkipList, pNode); } // find the appropriated position to insert data @@ -269,7 +277,17 @@ void tSkipListDoInsert(SSkipList *pSkipList, SSkipListNode **forward, SSkipListN } } -SSkipListNode* tSkipListDoAppend(SSkipList *pSkipList, SSkipListNode *pNode) { +SSkipListNode* tSkipListPushFront(SSkipList* pSkipList, SSkipListNode *pNode) { + SSkipListNode* forward[MAX_SKIP_LIST_LEVEL] = {0}; + for(int32_t i = 0; i < pSkipList->level; ++i) { + forward[i] = pSkipList->pHead; + } + + tSkipListDoInsert(pSkipList, forward, pNode); + return pNode; +} + +SSkipListNode* tSkipListPushBack(SSkipList *pSkipList, SSkipListNode *pNode) { // do clear pointer area DO_MEMSET_PTR_AREA(pNode);