* 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)
| 667 | fmt.Printf("The rank of '%s' in the set '%s' is %d\n", "memberName", "myKey", rank) |
| 668 | */ |
| 669 | func (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. |
nothing calls this directly
no test coverage detected