0

I have some values in redis with this key structure

key:1:abc -> votes -> 0
          -> name  -> John 

key:1:xyz -> votes -> 0
          -> name  -> Mary 

key:1:def -> votes -> 1
          -> name  -> Larry

key:2:ijk -> votes -> 0
          -> name  -> apple

Thats how my keyspace looks like. I am using hmset to store the stuff in redis. The "key:1" is a placeholder for identifying different users in a particular space and the part after "key:1" is a unique differentiator for each record in "key:1". I want to write some code for filtering out the data from redis for getting all the records who have number of votes set to 0. So the output of the jedis code should be something like

key:1:abc -> votes -> 0
          -> name  -> John 

key:1:xyz -> votes -> 0
          -> name  -> Mary 

And Larry gets filtered out. I was looking into hmscan for solving this problem but am not sure what the command would look like. Any clue on what I can do to get that output? Also what do you think will be the time complexity to get this time?

anonymous123
  • 1,271
  • 6
  • 19
  • 43
  • Why not using a zset in redis for this ranking with key as value and votes as score? You will update this zset each time a user has a vote change. – zenbeni Apr 02 '15 at 14:46

1 Answers1

1

There's a couple of solutions for this, but the first that comes to mind is using an auxiliar structure.

Every time you add a item to the HASH, you also add the name of the person to a SET:

> SADD zerocount "Larry"
(integer) 1
> SADD zerocount "Mary"
(integer) 1

Whenever you want the list of names with zero count you can do an SSCAN (gives you pagination):

> sscan zerocount 0
1) "0"
2) 1) "Mary"
2) "Larry"

At the point where you would increment the value of Larry for example, you would remove that value from the set:

> srem zerocount "Larry"
1

SADD is O(N) for each member added, in this case O(1). SSCAN is O(1) for each call, and SREM is O(1) for our scenario, in general O(N) where N is the number of members to be removed.

bitoiu
  • 6,893
  • 5
  • 38
  • 60