1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
8 // Writes require external synchronization, most likely a mutex.
9 // Reads require a guarantee that the SkipList will not be destroyed
10 // while the read is in progress. Apart from that, reads progress
11 // without any internal locking or synchronization.
15 // (1) Allocated nodes are never deleted until the SkipList is
16 // destroyed. This is trivially guaranteed by the code since we
17 // never delete any skip list nodes.
19 // (2) The contents of a Node except for the next/prev pointers are
20 // immutable after the Node has been linked into the SkipList.
21 // Only Insert() modifies the list, and it is careful to initialize
22 // a node and use release-stores to publish the nodes in one or
25 // ... prev vs. next pointer ordering ...
29 #include "port/port.h"
30 #include "util/arena.h"
31 #include "util/random.h"
37 template<typename Key, class Comparator>
43 // Create a new SkipList object that will use "cmp" for comparing keys,
44 // and will allocate memory using "*arena". Objects allocated in the arena
45 // must remain allocated for the lifetime of the skiplist object.
46 explicit SkipList(Comparator cmp, Arena* arena);
48 // Insert key into the list.
49 // REQUIRES: nothing that compares equal to key is currently in the list.
50 void Insert(const Key& key);
52 // Returns true iff an entry that compares equal to key is in the list.
53 bool Contains(const Key& key) const;
55 // Iteration over the contents of a skip list
58 // Initialize an iterator over the specified list.
59 // The returned iterator is not valid.
60 explicit Iterator(const SkipList* list);
62 // Returns true iff the iterator is positioned at a valid node.
65 // Returns the key at the current position.
67 const Key& key() const;
69 // Advances to the next position.
73 // Advances to the previous position.
77 // Advance to the first entry with a key >= target
78 void Seek(const Key& target);
80 // Position at the first entry in list.
81 // Final state of iterator is Valid() iff list is not empty.
84 // Position at the last entry in list.
85 // Final state of iterator is Valid() iff list is not empty.
89 const SkipList* list_;
91 // Intentionally copyable
95 enum { kMaxHeight = 12 };
97 // Immutable after construction
98 Comparator const compare_;
99 Arena* const arena_; // Arena used for allocations of nodes
103 // Modified only by Insert(). Read racily by readers, but stale
105 port::AtomicPointer max_height_; // Height of the entire list
107 inline int GetMaxHeight() const {
108 return static_cast<int>(
109 reinterpret_cast<intptr_t>(max_height_.NoBarrier_Load()));
112 // Read/written only by Insert().
115 Node* NewNode(const Key& key, int height);
117 bool Equal(const Key& a, const Key& b) const { return (compare_(a, b) == 0); }
119 // Return true if key is greater than the data stored in "n"
120 bool KeyIsAfterNode(const Key& key, Node* n) const;
122 // Return the earliest node that comes at or after key.
123 // Return NULL if there is no such node.
125 // If prev is non-NULL, fills prev[level] with pointer to previous
126 // node at "level" for every level in [0..max_height_-1].
127 Node* FindGreaterOrEqual(const Key& key, Node** prev) const;
129 // Return the latest node with a key < key.
130 // Return head_ if there is no such node.
131 Node* FindLessThan(const Key& key) const;
133 // Return the last node in the list.
134 // Return head_ if list is empty.
135 Node* FindLast() const;
137 // No copying allowed
138 SkipList(const SkipList&);
139 void operator=(const SkipList&);
142 // Implementation details follow
143 template<typename Key, class Comparator>
144 struct SkipList<Key,Comparator>::Node {
145 explicit Node(const Key& k) : key(k) { }
149 // Accessors/mutators for links. Wrapped in methods so we can
150 // add the appropriate barriers as necessary.
153 // Use an 'acquire load' so that we observe a fully initialized
154 // version of the returned Node.
155 return reinterpret_cast<Node*>(next_[n].Acquire_Load());
157 void SetNext(int n, Node* x) {
159 // Use a 'release store' so that anybody who reads through this
160 // pointer observes a fully initialized version of the inserted node.
161 next_[n].Release_Store(x);
164 // No-barrier variants that can be safely used in a few locations.
165 Node* NoBarrier_Next(int n) {
167 return reinterpret_cast<Node*>(next_[n].NoBarrier_Load());
169 void NoBarrier_SetNext(int n, Node* x) {
171 next_[n].NoBarrier_Store(x);
175 // Array of length equal to the node height. next_[0] is lowest level link.
176 port::AtomicPointer next_[1];
179 template<typename Key, class Comparator>
180 typename SkipList<Key,Comparator>::Node*
181 SkipList<Key,Comparator>::NewNode(const Key& key, int height) {
182 char* mem = arena_->AllocateAligned(
183 sizeof(Node) + sizeof(port::AtomicPointer) * (height - 1));
184 return new (mem) Node(key);
187 template<typename Key, class Comparator>
188 inline SkipList<Key,Comparator>::Iterator::Iterator(const SkipList* list) {
193 template<typename Key, class Comparator>
194 inline bool SkipList<Key,Comparator>::Iterator::Valid() const {
195 return node_ != NULL;
198 template<typename Key, class Comparator>
199 inline const Key& SkipList<Key,Comparator>::Iterator::key() const {
204 template<typename Key, class Comparator>
205 inline void SkipList<Key,Comparator>::Iterator::Next() {
207 node_ = node_->Next(0);
210 template<typename Key, class Comparator>
211 inline void SkipList<Key,Comparator>::Iterator::Prev() {
212 // Instead of using explicit "prev" links, we just search for the
213 // last node that falls before key.
215 node_ = list_->FindLessThan(node_->key);
216 if (node_ == list_->head_) {
221 template<typename Key, class Comparator>
222 inline void SkipList<Key,Comparator>::Iterator::Seek(const Key& target) {
223 node_ = list_->FindGreaterOrEqual(target, NULL);
226 template<typename Key, class Comparator>
227 inline void SkipList<Key,Comparator>::Iterator::SeekToFirst() {
228 node_ = list_->head_->Next(0);
231 template<typename Key, class Comparator>
232 inline void SkipList<Key,Comparator>::Iterator::SeekToLast() {
233 node_ = list_->FindLast();
234 if (node_ == list_->head_) {
239 template<typename Key, class Comparator>
240 int SkipList<Key,Comparator>::RandomHeight() {
241 // Increase height with probability 1 in kBranching
242 static const unsigned int kBranching = 4;
244 while (height < kMaxHeight && ((rnd_.Next() % kBranching) == 0)) {
248 assert(height <= kMaxHeight);
252 template<typename Key, class Comparator>
253 bool SkipList<Key,Comparator>::KeyIsAfterNode(const Key& key, Node* n) const {
254 // NULL n is considered infinite
255 return (n != NULL) && (compare_(n->key, key) < 0);
258 template<typename Key, class Comparator>
259 typename SkipList<Key,Comparator>::Node* SkipList<Key,Comparator>::FindGreaterOrEqual(const Key& key, Node** prev)
262 int level = GetMaxHeight() - 1;
264 Node* next = x->Next(level);
265 if (KeyIsAfterNode(key, next)) {
266 // Keep searching in this list
269 if (prev != NULL) prev[level] = x;
273 // Switch to next list
280 template<typename Key, class Comparator>
281 typename SkipList<Key,Comparator>::Node*
282 SkipList<Key,Comparator>::FindLessThan(const Key& key) const {
284 int level = GetMaxHeight() - 1;
286 assert(x == head_ || compare_(x->key, key) < 0);
287 Node* next = x->Next(level);
288 if (next == NULL || compare_(next->key, key) >= 0) {
292 // Switch to next list
301 template<typename Key, class Comparator>
302 typename SkipList<Key,Comparator>::Node* SkipList<Key,Comparator>::FindLast()
305 int level = GetMaxHeight() - 1;
307 Node* next = x->Next(level);
312 // Switch to next list
321 template<typename Key, class Comparator>
322 SkipList<Key,Comparator>::SkipList(Comparator cmp, Arena* arena)
325 head_(NewNode(0 /* any key will do */, kMaxHeight)),
326 max_height_(reinterpret_cast<void*>(1)),
328 for (int i = 0; i < kMaxHeight; i++) {
329 head_->SetNext(i, NULL);
333 template<typename Key, class Comparator>
334 void SkipList<Key,Comparator>::Insert(const Key& key) {
335 // TODO(opt): We can use a barrier-free variant of FindGreaterOrEqual()
336 // here since Insert() is externally synchronized.
337 Node* prev[kMaxHeight];
338 Node* x = FindGreaterOrEqual(key, prev);
340 // Our data structure does not allow duplicate insertion
341 assert(x == NULL || !Equal(key, x->key));
343 int height = RandomHeight();
344 if (height > GetMaxHeight()) {
345 for (int i = GetMaxHeight(); i < height; i++) {
348 //fprintf(stderr, "Change height from %d to %d\n", max_height_, height);
350 // It is ok to mutate max_height_ without any synchronization
351 // with concurrent readers. A concurrent reader that observes
352 // the new value of max_height_ will see either the old value of
353 // new level pointers from head_ (NULL), or a new value set in
354 // the loop below. In the former case the reader will
355 // immediately drop to the next level since NULL sorts after all
356 // keys. In the latter case the reader will use the new node.
357 max_height_.NoBarrier_Store(reinterpret_cast<void*>(height));
360 x = NewNode(key, height);
361 for (int i = 0; i < height; i++) {
362 // NoBarrier_SetNext() suffices since we will add a barrier when
363 // we publish a pointer to "x" in prev[i].
364 x->NoBarrier_SetNext(i, prev[i]->NoBarrier_Next(i));
365 prev[i]->SetNext(i, x);
369 template<typename Key, class Comparator>
370 bool SkipList<Key,Comparator>::Contains(const Key& key) const {
371 Node* x = FindGreaterOrEqual(key, NULL);
372 if (x != NULL && Equal(key, x->key)) {
379 } // namespace leveldb