MCPcopy Index your code
hub / github.com/ByteStorage/FlyDB / ZRank

Method ZRank

structure/zset.go:669–685  ·  view source on GitHub ↗

* ZRank is a method belonging to the ZSetStructure type. This method retrieves the rank of an element within a sorted set identified by a key. The rank is an integer corresponding to the element's 0-based position in the sorted set when it is arranged in ascending order. Parameters: key (string): T

(key string, member string)

Source from the content-addressed store, hash-verified

667fmt.Printf("The rank of '%s' in the set '%s' is %d\n", "memberName", "myKey", rank)
668*/
669func (zs *ZSetStructure) ZRank(key string, member string) (int, error) {
670 if err := checkKey(key); err != nil {
671 return 0, err
672 }
673 keyBytes := stringToBytesWithKey(key)
674
675 zSet, err := zs.getZSetFromDB(keyBytes)
676 if err != nil {
677 return 0, fmt.Errorf("failed to get or create ZSet from DB with key '%v': %w", key, err)
678 }
679 if v, ok := zSet.dict[member]; ok {
680 return zSet.skipList.getRank(v.score, member), nil
681 }
682
683 // rank zero means no rank found
684 return 0, _const.ErrKeyNotFound
685}
686
687// ZRevRank calculates the reverse rank of a member in a ZSet (Sorted Set) associated with a given key.
688// ZSet exploits the Sorted Set data structure of Redis with O(log(N)) time complexity for Fetching the rank.

Callers

nothing calls this directly

Calls 4

getZSetFromDBMethod · 0.95
checkKeyFunction · 0.85
stringToBytesWithKeyFunction · 0.85
getRankMethod · 0.80

Tested by

no test coverage detected