ZCount traverses through the elements of the ZSetStructure based on the given key. The count of elements between the range of min and max scores is determined. The method takes a string as the key and two integers as min and max ranges. The range values are inclusive: [min, max]. If min is greater
(key string, min int, max int)
| 784 | // 1. int: The total count of elements based on the score range. |
| 785 | // 2. error: Errors that occurred during execution, if any. |
| 786 | func (zs *ZSetStructure) ZCount(key string, min int, max int) (count int, err error) { |
| 787 | if err = checkKey(key); err != nil { |
| 788 | return 0, err |
| 789 | } |
| 790 | keyBytes := stringToBytesWithKey(key) |
| 791 | zSet, err := zs.getZSetFromDB(keyBytes) |
| 792 | if err != nil { |
| 793 | return 0, err |
| 794 | } |
| 795 | if min > max { |
| 796 | return 0, ErrInvalidArgs |
| 797 | } |
| 798 | min, max, err = zs.adjustMinMax(zSet, min, max) |
| 799 | if err != nil { |
| 800 | return 0, err |
| 801 | } |
| 802 | x := zSet.skipList.head |
| 803 | // Node traversal loop. We keep moving to the next node at current level |
| 804 | // as long as the score of the next node's value is less than 'min'. |
| 805 | for i := zSet.skipList.level - 1; i >= 0; i-- { |
| 806 | for x.level[i].next != nil && x.level[i].next.value.score < min { |
| 807 | x = x.level[i].next |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | x = x.level[0].next |
| 812 | // Score range check loop. We traverse nodes and increment 'count' |
| 813 | // as long as node value's score is in the range ['min', 'max'] |
| 814 | for x != nil { |
| 815 | if x.value.score > max { |
| 816 | break |
| 817 | } |
| 818 | count++ |
| 819 | x = x.level[0].next |
| 820 | } |
| 821 | return count, nil |
| 822 | } |
| 823 | |
| 824 | // ZRevRange retrieves a range of elements from a sorted set (ZSet) in descending order. |
| 825 | // Inputs: |
nothing calls this directly
no test coverage detected