skiplist.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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.
  4. #ifndef STORAGE_LEVELDB_DB_SKIPLIST_H_
  5. #define STORAGE_LEVELDB_DB_SKIPLIST_H_
  6. // Thread safety
  7. // -------------
  8. //
  9. // Writes require external synchronization, most likely a mutex.
  10. // Reads require a guarantee that the SkipList will not be destroyed
  11. // while the read is in progress. Apart from that, reads progress
  12. // without any internal locking or synchronization.
  13. //
  14. // Invariants:
  15. //
  16. // (1) Allocated nodes are never deleted until the SkipList is
  17. // destroyed. This is trivially guaranteed by the code since we
  18. // never delete any skip list nodes.
  19. //
  20. // (2) The contents of a Node except for the next/prev pointers are
  21. // immutable after the Node has been linked into the SkipList.
  22. // Only Insert() modifies the list, and it is careful to initialize
  23. // a node and use release-stores to publish the nodes in one or
  24. // more lists.
  25. //
  26. // ... prev vs. next pointer ordering ...
  27. #include <atomic>
  28. #include <cassert>
  29. #include <cstdlib>
  30. #include "util/arena.h"
  31. #include "util/random.h"
  32. namespace leveldb {
  33. class Arena;
  34. template <typename Key, class Comparator>
  35. class SkipList {
  36. private:
  37. struct Node;
  38. public:
  39. // Create a new SkipList object that will use "cmp" for comparing keys,
  40. // and will allocate memory using "*arena". Objects allocated in the arena
  41. // must remain allocated for the lifetime of the skiplist object.
  42. explicit SkipList(Comparator cmp, Arena* arena);
  43. SkipList(const SkipList&) = delete;
  44. SkipList& operator=(const SkipList&) = delete;
  45. // Insert key into the list.
  46. // REQUIRES: nothing that compares equal to key is currently in the list.
  47. void Insert(const Key& key);
  48. // Returns true iff an entry that compares equal to key is in the list.
  49. bool Contains(const Key& key) const;
  50. // Iteration over the contents of a skip list
  51. class Iterator {
  52. public:
  53. // Initialize an iterator over the specified list.
  54. // The returned iterator is not valid.
  55. explicit Iterator(const SkipList* list);
  56. // Returns true iff the iterator is positioned at a valid node.
  57. bool Valid() const;
  58. // Returns the key at the current position.
  59. // REQUIRES: Valid()
  60. const Key& key() const;
  61. // Advances to the next position.
  62. // REQUIRES: Valid()
  63. void Next();
  64. // Advances to the previous position.
  65. // REQUIRES: Valid()
  66. void Prev();
  67. // Advance to the first entry with a key >= target
  68. void Seek(const Key& target);
  69. // Position at the first entry in list.
  70. // Final state of iterator is Valid() iff list is not empty.
  71. void SeekToFirst();
  72. // Position at the last entry in list.
  73. // Final state of iterator is Valid() iff list is not empty.
  74. void SeekToLast();
  75. private:
  76. const SkipList* list_;
  77. Node* node_;
  78. // Intentionally copyable
  79. };
  80. private:
  81. enum { kMaxHeight = 12 };
  82. inline int GetMaxHeight() const {
  83. return max_height_.load(std::memory_order_relaxed);
  84. }
  85. Node* NewNode(const Key& key, int height);
  86. int RandomHeight();
  87. bool Equal(const Key& a, const Key& b) const { return (compare_(a, b) == 0); }
  88. // Return true if key is greater than the data stored in "n"
  89. bool KeyIsAfterNode(const Key& key, Node* n) const;
  90. // Return the earliest node that comes at or after key.
  91. // Return nullptr if there is no such node.
  92. //
  93. // If prev is non-null, fills prev[level] with pointer to previous
  94. // node at "level" for every level in [0..max_height_-1].
  95. Node* FindGreaterOrEqual(const Key& key, Node** prev) const;
  96. // Return the latest node with a key < key.
  97. // Return head_ if there is no such node.
  98. Node* FindLessThan(const Key& key) const;
  99. // Return the last node in the list.
  100. // Return head_ if list is empty.
  101. Node* FindLast() const;
  102. // Immutable after construction
  103. Comparator const compare_;
  104. Arena* const arena_; // Arena used for allocations of nodes
  105. Node* const head_;
  106. // Modified only by Insert(). Read racily by readers, but stale
  107. // values are ok.
  108. std::atomic<int> max_height_; // Height of the entire list
  109. // Read/written only by Insert().
  110. Random rnd_;
  111. };
  112. // Implementation details follow
  113. template <typename Key, class Comparator>
  114. struct SkipList<Key, Comparator>::Node {
  115. explicit Node(const Key& k) : key(k) {}
  116. Key const key;
  117. // Accessors/mutators for links. Wrapped in methods so we can
  118. // add the appropriate barriers as necessary.
  119. Node* Next(int n) {
  120. assert(n >= 0);
  121. // Use an 'acquire load' so that we observe a fully initialized
  122. // version of the returned Node.
  123. return next_[n].load(std::memory_order_acquire);
  124. }
  125. void SetNext(int n, Node* x) {
  126. assert(n >= 0);
  127. // Use a 'release store' so that anybody who reads through this
  128. // pointer observes a fully initialized version of the inserted node.
  129. next_[n].store(x, std::memory_order_release);
  130. }
  131. // No-barrier variants that can be safely used in a few locations.
  132. Node* NoBarrier_Next(int n) {
  133. assert(n >= 0);
  134. return next_[n].load(std::memory_order_relaxed);
  135. }
  136. void NoBarrier_SetNext(int n, Node* x) {
  137. assert(n >= 0);
  138. next_[n].store(x, std::memory_order_relaxed);
  139. }
  140. private:
  141. // Array of length equal to the node height. next_[0] is lowest level link.
  142. std::atomic<Node*> next_[1];
  143. };
  144. template <typename Key, class Comparator>
  145. typename SkipList<Key, Comparator>::Node* SkipList<Key, Comparator>::NewNode(
  146. const Key& key, int height) {
  147. char* const node_memory = arena_->AllocateAligned(
  148. sizeof(Node) + sizeof(std::atomic<Node*>) * (height - 1));
  149. return new (node_memory) Node(key);
  150. }
  151. template <typename Key, class Comparator>
  152. inline SkipList<Key, Comparator>::Iterator::Iterator(const SkipList* list) {
  153. list_ = list;
  154. node_ = nullptr;
  155. }
  156. template <typename Key, class Comparator>
  157. inline bool SkipList<Key, Comparator>::Iterator::Valid() const {
  158. return node_ != nullptr;
  159. }
  160. template <typename Key, class Comparator>
  161. inline const Key& SkipList<Key, Comparator>::Iterator::key() const {
  162. assert(Valid());
  163. return node_->key;
  164. }
  165. template <typename Key, class Comparator>
  166. inline void SkipList<Key, Comparator>::Iterator::Next() {
  167. assert(Valid());
  168. node_ = node_->Next(0);
  169. }
  170. template <typename Key, class Comparator>
  171. inline void SkipList<Key, Comparator>::Iterator::Prev() {
  172. // Instead of using explicit "prev" links, we just search for the
  173. // last node that falls before key.
  174. assert(Valid());
  175. node_ = list_->FindLessThan(node_->key);
  176. if (node_ == list_->head_) {
  177. node_ = nullptr;
  178. }
  179. }
  180. template <typename Key, class Comparator>
  181. inline void SkipList<Key, Comparator>::Iterator::Seek(const Key& target) {
  182. node_ = list_->FindGreaterOrEqual(target, nullptr);
  183. }
  184. template <typename Key, class Comparator>
  185. inline void SkipList<Key, Comparator>::Iterator::SeekToFirst() {
  186. node_ = list_->head_->Next(0);
  187. }
  188. template <typename Key, class Comparator>
  189. inline void SkipList<Key, Comparator>::Iterator::SeekToLast() {
  190. node_ = list_->FindLast();
  191. if (node_ == list_->head_) {
  192. node_ = nullptr;
  193. }
  194. }
  195. template <typename Key, class Comparator>
  196. int SkipList<Key, Comparator>::RandomHeight() {
  197. // Increase height with probability 1 in kBranching
  198. static const unsigned int kBranching = 4;
  199. int height = 1;
  200. while (height < kMaxHeight && ((rnd_.Next() % kBranching) == 0)) {
  201. height++;
  202. }
  203. assert(height > 0);
  204. assert(height <= kMaxHeight);
  205. return height;
  206. }
  207. template <typename Key, class Comparator>
  208. bool SkipList<Key, Comparator>::KeyIsAfterNode(const Key& key, Node* n) const {
  209. // null n is considered infinite
  210. return (n != nullptr) && (compare_(n->key, key) < 0);
  211. }
  212. template <typename Key, class Comparator>
  213. typename SkipList<Key, Comparator>::Node*
  214. SkipList<Key, Comparator>::FindGreaterOrEqual(const Key& key,
  215. Node** prev) const {
  216. Node* x = head_;
  217. int level = GetMaxHeight() - 1;
  218. while (true) {
  219. Node* next = x->Next(level);
  220. if (KeyIsAfterNode(key, next)) {
  221. // Keep searching in this list
  222. x = next;
  223. } else {
  224. if (prev != nullptr) prev[level] = x;
  225. if (level == 0) {
  226. return next;
  227. } else {
  228. // Switch to next list
  229. level--;
  230. }
  231. }
  232. }
  233. }
  234. template <typename Key, class Comparator>
  235. typename SkipList<Key, Comparator>::Node*
  236. SkipList<Key, Comparator>::FindLessThan(const Key& key) const {
  237. Node* x = head_;
  238. int level = GetMaxHeight() - 1;
  239. while (true) {
  240. assert(x == head_ || compare_(x->key, key) < 0);
  241. Node* next = x->Next(level);
  242. if (next == nullptr || compare_(next->key, key) >= 0) {
  243. if (level == 0) {
  244. return x;
  245. } else {
  246. // Switch to next list
  247. level--;
  248. }
  249. } else {
  250. x = next;
  251. }
  252. }
  253. }
  254. template <typename Key, class Comparator>
  255. typename SkipList<Key, Comparator>::Node* SkipList<Key, Comparator>::FindLast()
  256. const {
  257. Node* x = head_;
  258. int level = GetMaxHeight() - 1;
  259. while (true) {
  260. Node* next = x->Next(level);
  261. if (next == nullptr) {
  262. if (level == 0) {
  263. return x;
  264. } else {
  265. // Switch to next list
  266. level--;
  267. }
  268. } else {
  269. x = next;
  270. }
  271. }
  272. }
  273. template <typename Key, class Comparator>
  274. SkipList<Key, Comparator>::SkipList(Comparator cmp, Arena* arena)
  275. : compare_(cmp),
  276. arena_(arena),
  277. head_(NewNode(0 /* any key will do */, kMaxHeight)),
  278. max_height_(1),
  279. rnd_(0xdeadbeef) {
  280. for (int i = 0; i < kMaxHeight; i++) {
  281. head_->SetNext(i, nullptr);
  282. }
  283. }
  284. template <typename Key, class Comparator>
  285. void SkipList<Key, Comparator>::Insert(const Key& key) {
  286. // TODO(opt): We can use a barrier-free variant of FindGreaterOrEqual()
  287. // here since Insert() is externally synchronized.
  288. Node* prev[kMaxHeight];
  289. Node* x = FindGreaterOrEqual(key, prev);
  290. // Our data structure does not allow duplicate insertion
  291. assert(x == nullptr || !Equal(key, x->key));
  292. int height = RandomHeight();
  293. if (height > GetMaxHeight()) {
  294. for (int i = GetMaxHeight(); i < height; i++) {
  295. prev[i] = head_;
  296. }
  297. // It is ok to mutate max_height_ without any synchronization
  298. // with concurrent readers. A concurrent reader that observes
  299. // the new value of max_height_ will see either the old value of
  300. // new level pointers from head_ (nullptr), or a new value set in
  301. // the loop below. In the former case the reader will
  302. // immediately drop to the next level since nullptr sorts after all
  303. // keys. In the latter case the reader will use the new node.
  304. max_height_.store(height, std::memory_order_relaxed);
  305. }
  306. x = NewNode(key, height);
  307. for (int i = 0; i < height; i++) {
  308. // NoBarrier_SetNext() suffices since we will add a barrier when
  309. // we publish a pointer to "x" in prev[i].
  310. x->NoBarrier_SetNext(i, prev[i]->NoBarrier_Next(i));
  311. prev[i]->SetNext(i, x);
  312. }
  313. }
  314. template <typename Key, class Comparator>
  315. bool SkipList<Key, Comparator>::Contains(const Key& key) const {
  316. Node* x = FindGreaterOrEqual(key, nullptr);
  317. if (x != nullptr && Equal(key, x->key)) {
  318. return true;
  319. } else {
  320. return false;
  321. }
  322. }
  323. } // namespace leveldb
  324. #endif // STORAGE_LEVELDB_DB_SKIPLIST_H_