SADDMULTI or similar - command to add a bunch of items to a set all at once

42 views
Skip to first unread message

Simon Willison

unread,
Dec 12, 2009, 6:27:32 PM12/12/09
to Redis DB
I've been doing a bunch of works with sets recently (for
http://mps-expenses2.guardian.co.uk which primarily uses Redis for
random item selection with SRANDMEMBER). Currently, sets can be
composed either by multiple calls to SADD or using the extremely
useful SUNIONSTORE/SDIFFSTORE etc commands. The one case that isn't
covered is quickly building up a new set from scratch - that currently
takes multiple SADD commands.

Would it be appropriate for Redis to add a SADDMULTI command which can
take an arbitrary number of values and add them to a set all in one
atomic unit? It would have made a few tasks for the mps-expenses2
project more convenient. As it was, I had to build up temporary sets
and then RENAME them to ensure atomicity.

Cheers,

Simon

Salvatore Sanfilippo

unread,
Dec 12, 2009, 6:33:30 PM12/12/09
to redi...@googlegroups.com
On Sun, Dec 13, 2009 at 12:27 AM, Simon Willison
<si...@simonwillison.net> wrote:

> Would it be appropriate for Redis to add a SADDMULTI command which can
> take an arbitrary number of values and add them to a set all in one
> atomic unit? It would have made a few tasks for the mps-expenses2
> project more convenient. As it was, I had to build up temporary sets
> and then RENAME them to ensure atomicity.

Hello Simon!

yes, it's planned, will probably be called MSADD, and will work in the
following way:

MSADD <num keys> <num values> <key1> <key2> ... <keyN> <val1> <val2> ... <valN>

So it will be a bit more general, for example allowing to add the same
two elements to multiple sets at once:

MSADD 3 2 set1 set2 set3 foo bar

Not sure if I can add this for 1.4, probably yes as it should not be
too complex to implement, but at the same time I need a refactoring of
the Sets code as I want to implement specialized sets representations
for sets composed of small (< signed 64bit) integers in a
memory-efficient way.

But at some time this command will surely get inside.

Cheers,
Salvatore

--
Salvatore 'antirez' Sanfilippo
http://invece.org

"Once you have something that grows faster than education grows,
you’re always going to get a pop culture.", Alan Kay

Simon Willison

unread,
Dec 12, 2009, 6:51:23 PM12/12/09
to Redis DB
On Dec 12, 11:33 pm, Salvatore Sanfilippo <anti...@gmail.com> wrote:
> yes, it's planned, will probably be called MSADD, and will work in the
> following way:
>
> MSADD <num keys> <num values> <key1> <key2> ... <keyN> <val1> <val2> ... <valN>
>
> So it will be a bit more general, for example allowing to add the same
> two elements to multiple sets at once:
>
> MSADD 3 2 set1 set2 set3 foo bar

That's excellent - much more useful than what I was thinking about (I
can see how it would have helped with my most recent project). I
imagine the client libraries will hide the details of the API -
redis.msadd(['set1', 'set2', 'set3'], ['foo', 'bar']) for example.

Thanks,

Simon

Salvatore Sanfilippo

unread,
Dec 12, 2009, 6:54:33 PM12/12/09
to redi...@googlegroups.com
On Sun, Dec 13, 2009 at 12:51 AM, Simon Willison
<si...@simonwillison.net> wrote:

> imagine the client libraries will hide the details of the API -
> redis.msadd(['set1', 'set2', 'set3'], ['foo', 'bar']) for example.

Yes, that's exactly my idea for the API. Also if the argument is not a
list but a string or symbol it should be considered like a
single-element list, so that this forms will work:

r.msadd('myset',['1','2','3'])
r.msadd(['set1','set2','set3'],"foo")
Reply all
Reply to author
Forward
0 new messages