millisami
millisami

Reputation: 10151

How to calculate the set complement operation in redis using redis ruby?

For two sets in redis, there are operators to get a Union, Intersection, etc..

But I couldn't find any for the COMPLEMENT between 2 SETS?

Is there anyway that I missed to search/know on how to get a COMPLEMENT of SETS?

Example:

The redis example:

redis> SADD key1 "a"
(integer) 1
redis> SADD key1 "b"
(integer) 1
redis> SADD key1 "c"
(integer) 1
redis> SADD key2 "c"
(integer) 1
redis> SADD key2 "d"
(integer) 1
redis> SADD key2 "e"
(integer) 1
redis> SDIFF key1 key2
1) "a"
2) "b"
redis>

The diff just gives back "a" and "b".

But I want "a", "b", "d" and "e" which is I think its the complement.

How this can be achieved?

Upvotes: 1

Views: 455

Answers (1)

markijbema
markijbema

Reputation: 4055

This is probably what you want:

http://redis.io/commands/sdiff

Or, if you want to store the result:

http://redis.io/commands/sdiffstore

The ruby adapter follows the redis api quite strictly, so has the same commands.

update:

From the update i deduce you want the symmetric difference. You cannot do this in one operation, but you can do it in multiple:

redis> sdiffstore diff1 key1 key2 
redis> sdiffstore diff2 key2 key1
redis> sunion diff1 diff2

Upvotes: 3

Related Questions