Documentation
¶
Overview ¶
Package skiplist is a sorted []byte key/value store.
Put, Get, and Pop are O(log n) on average. Keys sort with bytes.Compare.
Use SkipList for single-threaded code. Use SyncSkipList when goroutines share the same map.
s := NewSkipList(0, 0) // defaults: max level 32, p 0.5
s.Put([]byte("a"), []byte("1"))
v, _ := s.Get([]byte("a"))
s.Pop([]byte("a"))
for k, v := range s.All() { _ = k; _ = v }
safe := NewSyncSkipList(0, 0)
safe.Put([]byte("a"), []byte("1"))
v, _ = safe.Get([]byte("a"))
SyncSkipList is the mutex-protected skip list.
Same operations as SkipList, safe to call from many goroutines. Put takes ownership of the key and value slices — do not use them after Put returns.
There is no All() or ForEach() yet; use Get in a loop or add your own iteration under RLock if you need a full scan.
Example ¶
Example demonstrates basic usage of SkipList.
s := NewSkipList(16, 0.5)
s.Put([]byte("cat"), []byte("meow"))
s.Put([]byte("dog"), []byte("woof"))
s.Put([]byte("cat"), []byte("purr")) // update
if v, err := s.Get([]byte("cat")); err == nil {
fmt.Println("cat ->", string(v))
}
fmt.Println("All entries:")
for k, v := range s.All() {
fmt.Printf(" %s: %s\n", k, v)
}
s.Pop([]byte("dog"))
fmt.Println("After pop dog, len via iteration:")
count := 0
for range s.All() {
count++
}
fmt.Println("count:", count)
Output: cat -> purr All entries: cat: purr dog: woof After pop dog, len via iteration: count: 1
Index ¶
- Variables
- func DefaultValues() (int, float64)
- func MaxEntries(maxLevel int, p float64) int64
- func MaxLevelFor(n int64, p float64) int
- type Element
- type SkipList
- func (s *SkipList) All() iter.Seq2[[]byte, []byte]
- func (s *SkipList) Cap() int64
- func (s *SkipList) ForEach(do func(key, value []byte) bool)
- func (s *SkipList) Get(key []byte) ([]byte, error)
- func (s *SkipList) Len() int
- func (s *SkipList) NewElement(key, val []byte, l int) *Element
- func (s *SkipList) Pop(key []byte) error
- func (s *SkipList) Put(key, val []byte) error
- func (s *SkipList) Reset()
- type SyncSkipList
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func DefaultValues ¶ added in v0.0.2
DefaultValues returns the default max level (32) and probability (0.5).
func MaxEntries ¶ added in v0.1.0
MaxEntries returns how many entries maxLevel and p can hold: (1/p)^maxLevel. For p=0.5 and maxLevel=32 this is 2^32.
func MaxLevelFor ¶ added in v0.1.0
MaxLevelFor returns the minimum max level needed for n entries at probability p: ceil(log_{1/p}(n)).
Types ¶
type SkipList ¶
type SkipList struct {
Header *Element
MaxLevel int
P float64
// MaxLen is the max number of entries. 0 means unlimited.
// Theoretical max at current MaxLevel and P is MaxEntries(MaxLevel, P).
MaxLen int64
// contains filtered or unexported fields
}
SkipList stores sorted []byte key/value pairs.
func NewSkipList ¶
NewSkipList creates a list. Pass 0 for either arg to use defaults (32, 0.5).
func (*SkipList) All ¶
All iterates key/value pairs in sorted order. Don't modify the list while ranging.
func (*SkipList) Cap ¶ added in v0.1.0
Capacity returns the enforced entry limit when MaxLen is set. When MaxLen is 0 (unlimited), returns MaxEntries(MaxLevel, P).
func (*SkipList) ForEach ¶ added in v0.0.2
ForEach walks all pairs in sorted order. Return false from do to stop early.
func (*SkipList) Get ¶
Get returns the value for key. ErrKeyNotFound if missing, ErrNilKey if empty.
func (*SkipList) NewElement ¶
NewElement builds a node with level l. Use Put for normal inserts.
type SyncSkipList ¶ added in v0.1.0
type SyncSkipList struct {
Header *Element
MaxLevel int
P float64
MaxLen int64
// contains filtered or unexported fields
}
SyncSkipList is a sorted []byte map safe for concurrent use.
func NewSyncSkipList ¶ added in v0.1.0
func NewSyncSkipList(maxlevel int, p float64) *SyncSkipList
NewSyncSkipList creates a concurrent skip list. Pass 0 for either argument to use defaults (32, 0.5). MaxLen is set to the theoretical max for those settings.
func (*SyncSkipList) Cap ¶ added in v0.1.0
func (s *SyncSkipList) Cap() int64
Cap returns the entry limit (MaxLen).
func (*SyncSkipList) Get ¶ added in v0.1.0
func (s *SyncSkipList) Get(key []byte) ([]byte, error)
Get returns the value for key. Safe for concurrent readers. Zero allocs on hit. Returns ErrKeyNotFound or ErrNilKey.
func (*SyncSkipList) Len ¶ added in v0.1.0
func (s *SyncSkipList) Len() int64
Len returns the number of entries.
func (*SyncSkipList) Pop ¶ added in v0.1.0
func (s *SyncSkipList) Pop(key []byte) error
Pop removes key. Returns ErrKeyNotFound or ErrNilKey.
func (*SyncSkipList) Put ¶ added in v0.1.0
func (s *SyncSkipList) Put(key, val []byte) error
Put inserts or updates a key/value pair. Takes ownership of key and val — do not read or write those slices afterward. Returns ErrNilKey, ErrNilVal, or ErrSkiplistFull.

