tags:

views:

159

answers:

1

hi, i'm thinking use redis (http://code.google.com/p/redis/) to store this kind of ruby data structure.

suppose this:

node1server1 = { "volume1" => 10400, "volume2" => 11221, "volume3" => 13212, "volume4" => 17227 }
node1server2 = { "volume1" => 17450, "volume2" => 14241, "volume3" => 15512, "volume4" => 12427 }

node2server1 = { "volume1" => 17655, "volume2" => 16754, "volume3" => 19877, "volume4" => 10000 }
node2server2 = { "volume1" => 11456, "volume2" => 13776, "volume3" => 18766, "volume4" => 16555 }

what is the better redis data type to use? data need to be updated, and i need to sort values of node1server1+node1server2 and node2server1+node2server2

thank you

A: 

Hi Giovanni,

I can see it has been a while since you posted, but can you provide a little more information about how the data should be sorted?

Also, I assume bellow that there may be a variable number of volumes per server, and a variable number of servers per node

One way to do it is as follows:

node_id:1:server_id:1:volume_id:1 = 10400
node_id:1:server_id:1:volume_id:2 = 11221
node_id:1:server_id:1:volume_id:3 = 13212
node_id:1:server_id:1:volume_id:4 = 17227
node_id:1:server_id:2:volume_id:1 = 17450
node_id:1:server_id:2:volume_id:1 = 14241
...
node_id:1:volumes = SET("server_id:1:volume_id:1", "server_id:1:volume_id:2", "server_id:1:volume_id:3", "server_id:1:volume_id:4", "server_id:2:volume_id:1", "server_id:2:volume_id:2", "server_id:2:volume_id:3", "server_id:2:volume_id:4")

So to get a sorted list of all volumes numbers for node1:

SORT node_id:1:volumes BY node_id:1:*

...which would sort by the values assigned to each node and a sorted version of node_id:1:volumes.

Alternatively, you could use the ZSET which is new in Redis 1.1

Also note that I have used SETs here, rather than LISTs. I find that LISTs are more useful for structures described in terms of queues and stacks, and SETs are better for lists of unique items.

References:

Adam Charnock