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

Method InsertNode

structure/zset.go:945–972  ·  view source on GitHub ↗

InsertNode is a method on the FZSet structure. It inserts a new node or updates an existing node in the skip list and the dictionary. It takes three parameters: score (an integer), key (a string), and value (of any interface type). If key already exists in the dictionary and the score equals the ex

(score int, member string, value interface{})

Source from the content-addressed store, hash-verified

943// to the dictionary, increments the size of the dictionary by 1, and also adds
944// the node to the skip list.
945func (fzs *FZSet) InsertNode(score int, member string, value interface{}) error {
946 // Instantiate dictionary if it's not already
947 if fzs.dict == nil {
948 fzs.dict = make(map[string]*ZSetValue)
949 }
950 if fzs.skipList == nil {
951 fzs.skipList = newSkipList()
952 }
953
954 // Check if key exists in dictionary
955 if v, ok := fzs.dict[member]; ok {
956 if v.score != score {
957 // Update value and score as the score remains the same
958 fzs.skipList.delete(score, member)
959 fzs.dict[member] = fzs.skipList.insert(score, member, value)
960 } else {
961 // Ranking isn't altered, only update value
962 v.value = value
963 }
964 } else { // Key doesn't exist, create new key
965 fzs.dict[member] = fzs.skipList.insert(score, member, value)
966 fzs.size++ // Increase size count by 1
967 // Node is also added to the skip list
968 }
969
970 // Returns nil as no specific error condition is checked in this function
971 return nil
972}
973func (zs *ZSetStructure) adjustMinMax(zSet *FZSet, min int, max int) (adjustedMin int, adjustedMax int, err error) {
974 if min > max {
975 return min, max, ErrInvalidArgs

Callers 8

TestZSetNodes_InsertNodeFunction · 0.95
TestFZSetMinMaxFunction · 0.95
UnmarshalBinaryMethod · 0.95
TestSortedSetFunction · 0.80
ZIncrByMethod · 0.80
updateZSetMethod · 0.80

Calls 3

newSkipListFunction · 0.85
deleteMethod · 0.80
insertMethod · 0.80

Tested by 5

TestZSetNodes_InsertNodeFunction · 0.76
TestFZSetMinMaxFunction · 0.76
TestSortedSetFunction · 0.64