0

The Redis ZSET Sorted Set (member, score) sorts the set by the score.

The Redis SET are an unordered collection of unique Strings.

What I need is a method that returns the members in a Sorted Set matching a pattern as in ZRANGEBYLEX but with members with different scores.

Is is possible at all with redis?

Juancki
  • 1,793
  • 1
  • 14
  • 21
  • 1
    Just to be clear, you're asking for a way to return a range of sorted set solely by lexicographic ordering? – dizzyf Sep 29 '20 at 16:31
  • 1
    What's you need is a [std::map](https://en.cppreference.com/w/cpp/container/map) in C++. [This](https://stackoverflow.com/questions/40070174/why-there-is-no-ordered-hashmap-in-redis/40070832#40070832) is a solution to build a `std::map` with Redis. – for_stack Sep 30 '20 at 01:04
  • @for_stack thanks for commenting. Does the solution you included requires storing the scores separately?, which makes it difficult to delete them as compared to them being in structure, am I correct? – Juancki Sep 30 '20 at 07:27
  • 1
    @Juancki YES, you need to store scores separately. Since there's no built-in structure to support `std::map`, you have to use two built-in data structures to simulate it. When you want to delete an item, just remove it from these 2 data structures. – for_stack Sep 30 '20 at 07:43
  • great, thanks for pointing in the right direction! If you post an answer I'll mark you as correct. – Juancki Sep 30 '20 at 07:45

1 Answers1

0

Well, it seems I did not know about the SCAN suite. In this case ZSCAN solves this issue, however with cost O(N) where N is the number of items in sorted set because it iterates over the whole set.

Example of elements in:

127.0.0.1:6379> zrange l 0 -1 WITHSCORES
 1) "foodgood:irene"
 2) "1"
 3) "foodgood:pep"
 4) "1"
 5) "sleep:irene"
 6) "1"
 7) "sleep:juan"
 8) "1"
 9) "sleep:pep"
10) "1"
11) "sleep:toni"
12) "1"
13) "foodgood:juan"
14) "2"

Now ZSCAN for those with prefix foodgood:

127.0.0.1:6379> ZSCAN l 0 match foodgood:*
1) "0"
2) 1) "foodgood:irene"
   2) "1"
   3) "foodgood:pep"
   4) "1"
   5) "foodgood:juan"
   6) "2"

The first returned value "0" if zero indicates collection was completely explored.

What I would have liked is that to be O(log(N)+M) where M is the number of elements retrieved similar to Binary Search Tree.

Juancki
  • 1,793
  • 1
  • 14
  • 21