MCPcopy Create free account
hub / github.com/ByteStorage/FlyDB / randomLevel

Function randomLevel

structure/zset.go:135–154  ·  view source on GitHub ↗

randomLevel is a function that generates a probabilistic level for a node in a SkipList data structure. The goal is to diversify the level distribution and contribute to achieving an ideal skiplist performance. Function has no parameters. The process starts with two initial variables: - 'level' whic

()

Source from the content-addressed store, hash-verified

133// If 'level' is greater, 'SKIPLIST_MAX_LEVEL' is returned, otherwise the calculated 'level' value is returned.
134// The function returns an integer which will be the level of new node in skiplist.
135func randomLevel() int {
136 // Initialize level to 1
137 level := 1
138 r := rand.New(rand.NewSource(time.Now().UnixNano()))
139 // Calculate the threshold for level. It's derived from the probability constant of the skip list.
140 thresh := int(math.Round(SKIPLIST_PROB * 0xFFF))
141
142 // While a randomly generated number is less than this threshold, increment the level.
143 for int(r.Int31()&0xFFF) < thresh {
144 level++
145 }
146
147 // Check if the level is more than the maximum allowed level for the skip list
148 // If it is, return the maximum level. Otherwise, return the generated level.
149 if level > SKIPLIST_MAX_LEVEL {
150 return SKIPLIST_MAX_LEVEL
151 } else {
152 return level
153 }
154}
155
156// NewZSetStructure Returns a new ZSetStructure
157func NewZSetStructure(options config.Options) (*ZSetStructure, error) {

Callers 2

TestRandomLevelFunction · 0.85
insertMethod · 0.85

Calls

no outgoing calls

Tested by 1

TestRandomLevelFunction · 0.68