RoyValentine
RoyValentine

Reputation: 143

Receiving reply from Redis ZRANGE

I currently have two sorted sets and I am trying to get all scores and members from one set and use it to remove members from the other set. The module that I am using is node_redis.

Right now I am trying to get the members and scores by calling client.zrange() and storing the reply in an array.

Am I correct in assuming the reply would be in array form? I realize the redis api says it returns a "Multi-bulk reply" but what does that mean exactly and how would I go about using it if it isn't an array?

I also have another question and that is can I use an array when using zadd()? An example would be like this.

client.zadd(historyKey, scores, members, function(err, reply){});

Where scores and members are arrays.

EDIT:

I am working with receiving and parsing SNMP Traps. Basically I receive a trap and check its alarm type. The useful information in these traps are the alarm type and the full trap name. I check to see if the alarm is a 0,1, or 2.

If it's a 1, then I store it in my Current sorted set at the unix time I received it. If it's a 0 or 2 I know that type of alarm is done and that I need to remove all traps like it from the Current set and put them into the History set along with the one I just received.

In order to remove the traps from the Current and put them into the History I had to create a separate set for each individual trap in order to keep track of where they would be located in the Current set.

That is if I receive the trap "RGB Gamut Error( ----Bb )" at time 1346276537 and store it in Current, I also store the exact score and member in a separate set with key "IPAddress:RGB Gamut Error".

That way when I receive alarm type 0 or 2 with the name "RGB Gamut Error" I can just append the IP Address to the front of it, go do zrange on that set, then add to History and remove from Current. And lastly delete the "IPAddress:RGB Gamut Error" set so I can start fresh.

Sidenote: My members actually have two numbers added to the end in order to make each member unique and not overwrite each other. This is really there only purpose.

Ex: IPAdress::RGB Gamut Error( Rr--Bb ):5:46

Upvotes: 1

Views: 2293

Answers (1)

Linus Thiel
Linus Thiel

Reputation: 39261

Am I correct in assuming the reply would be in array form?

Yes, node_redis will give you the reply from a zrange as an array.

I also have another question and that is can I use an array when using zadd()? An example would be like this.

No. Before redis 2.4, you could only send one parameter at a time (so zadd key score member). Since redis 2.4, zadd (and many other commands) are variadic, i.e. they accept any number of parameters -- but not as an array. You still have to call it like this:

client.zadd(key, score1, member1, score2, member2, ..., function(err, reply){});

You could do some .apply trickery, but you would have to zip the scores and members arrays into one array first.

Update:

If you already have scores and members arrays, you could merge them into one array like this:

var scores = [1, 2, 3],
    members = ['a', 'b', 'c'];

function merge (other) {
  return function (result, current, index) {
    result.push(current, other[index]);
    return result;
  }
}

var merged = scores.reduce(merge(members), []);

// Now merged = [1, 'a', 2, 'b', 3, 'c'];

var args = [key].concat(merged).concat(function(err, reply){});

client.zadd.apply(client, args);

Upvotes: 1

Related Questions