leveldb
Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
leveldb::SkipList< Key, Comparator > Class Template Reference

#include <skiplist.h>

Collaboration diagram for leveldb::SkipList< Key, Comparator >:
Collaboration graph
[legend]

Classes

class  Iterator
 
struct  Node
 

Public Member Functions

 SkipList (Comparator cmp, Arena *arena)
 
void Insert (const Key &key)
 
bool Contains (const Key &key) const
 

Private Types

enum  { kMaxHeight = 12 }
 

Private Member Functions

int GetMaxHeight () const
 
NodeNewNode (const Key &key, int height)
 
int RandomHeight ()
 
bool Equal (const Key &a, const Key &b) const
 
bool KeyIsAfterNode (const Key &key, Node *n) const
 
NodeFindGreaterOrEqual (const Key &key, Node **prev) const
 
NodeFindLessThan (const Key &key) const
 
NodeFindLast () const
 
 SkipList (const SkipList &)
 
void operator= (const SkipList &)
 

Private Attributes

Comparator const compare_
 
Arena *const arena_
 
Node *const head_
 
port::AtomicPointer max_height_
 
Random rnd_
 

Detailed Description

template<typename Key, class Comparator>
class leveldb::SkipList< Key, Comparator >

Definition at line 41 of file skiplist.h.

Member Enumeration Documentation

§ anonymous enum

template<typename Key, class Comparator>
anonymous enum
private
Enumerator
kMaxHeight 

Definition at line 98 of file skiplist.h.

Constructor & Destructor Documentation

§ SkipList() [1/2]

template<typename Key , class Comparator>
leveldb::SkipList< Key, Comparator >::SkipList ( Comparator  cmp,
Arena arena 
)
explicit

Definition at line 325 of file skiplist.h.

326  : compare_(cmp),
327  arena_(arena),
328  head_(NewNode(0 /* any key will do */, kMaxHeight)),
329  max_height_(reinterpret_cast<void*>(1)),
330  rnd_(0xdeadbeef) {
331  for (int i = 0; i < kMaxHeight; i++) {
332  head_->SetNext(i, NULL);
333  }
334 }
port::AtomicPointer max_height_
Definition: skiplist.h:108
Node * NewNode(const Key &key, int height)
Definition: skiplist.h:184
void SetNext(int n, Node *x)
Definition: skiplist.h:160
Arena *const arena_
Definition: skiplist.h:102
Node *const head_
Definition: skiplist.h:104
Comparator const compare_
Definition: skiplist.h:101
Here is the caller graph for this function:

§ SkipList() [2/2]

template<typename Key, class Comparator>
leveldb::SkipList< Key, Comparator >::SkipList ( const SkipList< Key, Comparator > &  )
private

Member Function Documentation

§ Contains()

template<typename Key, class Comparator >
bool leveldb::SkipList< Key, Comparator >::Contains ( const Key key) const

Definition at line 373 of file skiplist.h.

373  {
374  Node* x = FindGreaterOrEqual(key, NULL);
375  if (x != NULL && Equal(key, x->key)) {
376  return true;
377  } else {
378  return false;
379  }
380 }
bool Equal(const Key &a, const Key &b) const
Definition: skiplist.h:120
Node * FindGreaterOrEqual(const Key &key, Node **prev) const
Definition: skiplist.h:262
Here is the caller graph for this function:

§ Equal()

template<typename Key, class Comparator>
bool leveldb::SkipList< Key, Comparator >::Equal ( const Key a,
const Key b 
) const
inlineprivate

Definition at line 120 of file skiplist.h.

120 { return (compare_(a, b) == 0); }
Comparator const compare_
Definition: skiplist.h:101

§ FindGreaterOrEqual()

template<typename Key, class Comparator >
SkipList< Key, Comparator >::Node * leveldb::SkipList< Key, Comparator >::FindGreaterOrEqual ( const Key key,
Node **  prev 
) const
private

Definition at line 262 of file skiplist.h.

263  {
264  Node* x = head_;
265  int level = GetMaxHeight() - 1;
266  while (true) {
267  Node* next = x->Next(level);
268  if (KeyIsAfterNode(key, next)) {
269  // Keep searching in this list
270  x = next;
271  } else {
272  if (prev != NULL) prev[level] = x;
273  if (level == 0) {
274  return next;
275  } else {
276  // Switch to next list
277  level--;
278  }
279  }
280  }
281 }
int GetMaxHeight() const
Definition: skiplist.h:110
Node *const head_
Definition: skiplist.h:104
bool KeyIsAfterNode(const Key &key, Node *n) const
Definition: skiplist.h:256
Here is the caller graph for this function:

§ FindLast()

template<typename Key , class Comparator >
SkipList< Key, Comparator >::Node * leveldb::SkipList< Key, Comparator >::FindLast ( ) const
private

Definition at line 305 of file skiplist.h.

306  {
307  Node* x = head_;
308  int level = GetMaxHeight() - 1;
309  while (true) {
310  Node* next = x->Next(level);
311  if (next == NULL) {
312  if (level == 0) {
313  return x;
314  } else {
315  // Switch to next list
316  level--;
317  }
318  } else {
319  x = next;
320  }
321  }
322 }
int GetMaxHeight() const
Definition: skiplist.h:110
Node *const head_
Definition: skiplist.h:104
Here is the caller graph for this function:

§ FindLessThan()

template<typename Key, class Comparator >
SkipList< Key, Comparator >::Node * leveldb::SkipList< Key, Comparator >::FindLessThan ( const Key key) const
private

Definition at line 285 of file skiplist.h.

285  {
286  Node* x = head_;
287  int level = GetMaxHeight() - 1;
288  while (true) {
289  assert(x == head_ || compare_(x->key, key) < 0);
290  Node* next = x->Next(level);
291  if (next == NULL || compare_(next->key, key) >= 0) {
292  if (level == 0) {
293  return x;
294  } else {
295  // Switch to next list
296  level--;
297  }
298  } else {
299  x = next;
300  }
301  }
302 }
int GetMaxHeight() const
Definition: skiplist.h:110
Node *const head_
Definition: skiplist.h:104
Comparator const compare_
Definition: skiplist.h:101
Here is the caller graph for this function:

§ GetMaxHeight()

template<typename Key, class Comparator>
int leveldb::SkipList< Key, Comparator >::GetMaxHeight ( ) const
inlineprivate

Definition at line 110 of file skiplist.h.

110  {
111  return static_cast<int>(
112  reinterpret_cast<intptr_t>(max_height_.NoBarrier_Load()));
113  }
port::AtomicPointer max_height_
Definition: skiplist.h:108
Here is the caller graph for this function:

§ Insert()

template<typename Key, class Comparator >
void leveldb::SkipList< Key, Comparator >::Insert ( const Key key)

Definition at line 337 of file skiplist.h.

337  {
338  // TODO(opt): We can use a barrier-free variant of FindGreaterOrEqual()
339  // here since Insert() is externally synchronized.
340  Node* prev[kMaxHeight];
341  Node* x = FindGreaterOrEqual(key, prev);
342 
343  // Our data structure does not allow duplicate insertion
344  assert(x == NULL || !Equal(key, x->key));
345 
346  int height = RandomHeight();
347  if (height > GetMaxHeight()) {
348  for (int i = GetMaxHeight(); i < height; i++) {
349  prev[i] = head_;
350  }
351  //fprintf(stderr, "Change height from %d to %d\n", max_height_, height);
352 
353  // It is ok to mutate max_height_ without any synchronization
354  // with concurrent readers. A concurrent reader that observes
355  // the new value of max_height_ will see either the old value of
356  // new level pointers from head_ (NULL), or a new value set in
357  // the loop below. In the former case the reader will
358  // immediately drop to the next level since NULL sorts after all
359  // keys. In the latter case the reader will use the new node.
360  max_height_.NoBarrier_Store(reinterpret_cast<void*>(height));
361  }
362 
363  x = NewNode(key, height);
364  for (int i = 0; i < height; i++) {
365  // NoBarrier_SetNext() suffices since we will add a barrier when
366  // we publish a pointer to "x" in prev[i].
367  x->NoBarrier_SetNext(i, prev[i]->NoBarrier_Next(i));
368  prev[i]->SetNext(i, x);
369  }
370 }
port::AtomicPointer max_height_
Definition: skiplist.h:108
Node * NewNode(const Key &key, int height)
Definition: skiplist.h:184
int GetMaxHeight() const
Definition: skiplist.h:110
Node *const head_
Definition: skiplist.h:104
bool Equal(const Key &a, const Key &b) const
Definition: skiplist.h:120
Node * FindGreaterOrEqual(const Key &key, Node **prev) const
Definition: skiplist.h:262
int RandomHeight()
Definition: skiplist.h:243
Here is the caller graph for this function:

§ KeyIsAfterNode()

template<typename Key, class Comparator >
bool leveldb::SkipList< Key, Comparator >::KeyIsAfterNode ( const Key key,
Node n 
) const
private

Definition at line 256 of file skiplist.h.

256  {
257  // NULL n is considered infinite
258  return (n != NULL) && (compare_(n->key, key) < 0);
259 }
Comparator const compare_
Definition: skiplist.h:101
Here is the caller graph for this function:

§ NewNode()

template<typename Key, class Comparator >
SkipList< Key, Comparator >::Node * leveldb::SkipList< Key, Comparator >::NewNode ( const Key key,
int  height 
)
private

Definition at line 184 of file skiplist.h.

184  {
185  char* mem = arena_->AllocateAligned(
186  sizeof(Node) + sizeof(port::AtomicPointer) * (height - 1));
187  return new (mem) Node(key);
188 }
Arena *const arena_
Definition: skiplist.h:102
char * AllocateAligned(size_t bytes)
Definition: arena.cc:41

§ operator=()

template<typename Key, class Comparator>
void leveldb::SkipList< Key, Comparator >::operator= ( const SkipList< Key, Comparator > &  )
private
Here is the caller graph for this function:

§ RandomHeight()

template<typename Key , class Comparator >
int leveldb::SkipList< Key, Comparator >::RandomHeight ( )
private

Definition at line 243 of file skiplist.h.

243  {
244  // Increase height with probability 1 in kBranching
245  static const unsigned int kBranching = 4;
246  int height = 1;
247  while (height < kMaxHeight && ((rnd_.Next() % kBranching) == 0)) {
248  height++;
249  }
250  assert(height > 0);
251  assert(height <= kMaxHeight);
252  return height;
253 }
uint32_t Next()
Definition: random.h:25

Member Data Documentation

§ arena_

template<typename Key, class Comparator>
Arena* const leveldb::SkipList< Key, Comparator >::arena_
private

Definition at line 102 of file skiplist.h.

§ compare_

template<typename Key, class Comparator>
Comparator const leveldb::SkipList< Key, Comparator >::compare_
private

Definition at line 101 of file skiplist.h.

§ head_

template<typename Key, class Comparator>
Node* const leveldb::SkipList< Key, Comparator >::head_
private

Definition at line 104 of file skiplist.h.

§ max_height_

template<typename Key, class Comparator>
port::AtomicPointer leveldb::SkipList< Key, Comparator >::max_height_
private

Definition at line 108 of file skiplist.h.

§ rnd_

template<typename Key, class Comparator>
Random leveldb::SkipList< Key, Comparator >::rnd_
private

Definition at line 116 of file skiplist.h.


The documentation for this class was generated from the following file: