-
Notifications
You must be signed in to change notification settings - Fork 11
/
db_skiplist.c
220 lines (158 loc) · 4.62 KB
/
db_skiplist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// skip list implementation
#include "base64.h"
#include "db.h"
#include "db_arena.h"
#include "db_map.h"
#include "db_skiplist.h"
// initialize initial skip list node
uint64_t skipInit(DbMap *map, int numEntries) {
if (numEntries > 127)
numEntries = 127;
return allocBlk(map, numEntries * sizeof(SkipEntry) + sizeof(SkipNode), true);
}
// find key value in skiplist, return entry address
SkipEntry *skipFind(DbMap *map, DbAddr *skip, uint64_t key) {
DbAddr *next = skip;
SkipNode *skipNode;
SkipEntry *entry;
while (next->addr) {
skipNode = getObj(map, *next);
if (*skipNode->array[next->nslot-1].key >= key) {
entry = skipNode->array + skipSearch(skipNode->array, next->nslot-1, key);
if (*entry->key == key)
return entry;
return NULL;
}
next = skipNode->next;
}
return NULL;
}
// remove key from skip list
// returning value from slot
uint64_t skipDel(DbMap *map, DbAddr *skip, uint64_t key) {
SkipNode *skipNode = NULL, *prevNode;
DbAddr *next = skip;
SkipEntry *entry;
uint64_t val;
while (next->addr) {
prevNode = skipNode;
skipNode = getObj(map, *next);
if (*skipNode->array[next->nslot-1].key >= key) {
entry = skipNode->array + skipSearch(skipNode->array, next->nslot-1, key);
if (*entry->key == key)
val = *entry->val;
else
return 0;
// remove the entry slot
if (--next->nslot) {
while (entry - skipNode->array < next->nslot) {
entry[0] = entry[1];
entry++;
}
return val;
}
// skip list node is empty, remove it
if (prevNode)
prevNode->next->bits = skipNode->next->bits;
else
skip->bits = skipNode->next->bits;
freeBlk(map, *next);
return val;
}
next = skipNode->next;
}
return 0;
}
// Push new maximal key onto head of skip list
// return the value slot address
SkipEntry *skipPush(DbMap *map, DbAddr *skip, uint64_t key) {
SkipNode *skipNode;
SkipEntry *entry;
uint64_t next;
if (!skip->addr || skip->nslot == skipSize(skip)) {
next = skip->bits;
skip->bits = allocBlk(map, SKIP_node * sizeof(SkipEntry) + sizeof(SkipNode), true) | ADDR_MUTEX_SET;
skipNode = getObj(map, *skip);
skipNode->next->bits = next;
}
else
skipNode = getObj(map, *skip);
entry = skipNode->array + skip->nslot++;
*entry->key = key;
return entry;
}
// add item to skiplist
DbStatus addItemToSkiplist(DbMap *map, DbAddr *skip, uint64_t key, uint64_t item) {
SkipEntry *entry;
lockLatch(skip->latch);
entry = skipAdd(map, skip, key);
*entry->val = item;
unlockLatch(skip->latch);
return DB_OK;
}
// Add arbitrary key to skip list
// call with skip hdr locked
// return entry address
SkipEntry *skipAdd(DbMap *map, DbAddr *skip, uint64_t key) {
SkipNode *skipNode = NULL, *nextNode;
DbAddr *next = skip;
uint64_t prevBits;
SkipEntry *entry;
int min, max;
while (next->addr) {
skipNode = getObj(map, *next);
// find skiplist node that covers key
if (*skipNode->array[next->nslot-1].key < key)
if (skipNode->next->bits) {
next = skipNode->next;
continue;
}
// we belong in this skipNode
// find the first entry .ge. to the new key
min = skipSearch(skipNode->array, next->nslot, key);
entry = skipNode->array + min;
// does key already exist?
if (min < next->nslot && *entry->key == key)
return entry;
// split node if already full
if (next->nslot == skipSize(next)) {
prevBits = skipNode->next->bits;
skipNode->next->bits = allocBlk(map, SKIP_node * sizeof(SkipEntry) + sizeof(SkipNode), true);
nextNode = getObj(map, *skipNode->next);
nextNode->next->bits = prevBits;
memcpy(nextNode->array, skipNode->array + skipSize(skipNode->next) / 2, sizeof(skipNode->next) * (skipSize(skipNode->next) - skipSize(skipNode->next) / 2));
skipNode->next->nslot = skipSize(skipNode->next) - skipSize(skipNode->next) / 2;
next->nslot = skipSize(next) / 2;
continue;
}
// insert new entry slot at min
max = next->nslot++;
while (--max > min)
skipNode->array[max + 1] = skipNode->array[max];
// fill in key and return value slot
*skipNode->array[min].key = key;
return skipNode->array + min;
}
// initialize empty list
skip->bits = allocBlk(map, SKIP_node * sizeof(SkipEntry) + sizeof(SkipNode), true) | ADDR_MUTEX_SET;
skipNode = getObj(map, *skip);
*skipNode->array->key = key;
skip->nslot = 1;
return skipNode->array;
}
// search Skip node for idx of key value slot
// e.g. key value is <= entry[idx]
int skipSearch(SkipEntry *array, int high, uint64_t key) {
int low = 0;
// invariants:
// key <= entry[high]
// key > entry[low-1]
while (high > low) {
int diff = (high - 1 - low) / 2;
if (key <= *array[low + diff].key)
high = low + diff;
else
low += diff + 1;
}
return high;
}