Get a list range in reverse

2,883 views
Skip to first unread message

Adam Michaels

unread,
Jun 3, 2009, 3:01:39 AM6/3/09
to Redis DB
Does anyone know how to get a range from a list going from right to
left?

Justin

unread,
Jun 3, 2009, 4:58:30 AM6/3/09
to Redis DB
Does it have to be strictly within the Redis protocol? If not, an easy
way would be to put the range into an array as usual, and then reverse
the array in your client code.

Salvatore Sanfilippo

unread,
Jun 3, 2009, 5:22:44 AM6/3/09
to redi...@googlegroups.com
On Wed, Jun 3, 2009 at 9:01 AM, Adam Michaels <ilo...@gmail.com> wrote:
>
> Does anyone know how to get a range from a list going from right to
> left?

Hello!

Currently it's not possible, because:

LRANGE mylist 0 4
LRANGE mylist 4 0

will return the same range. Actually it is possible to change the
semantic of the latter in order to return the elements in reverse
order. It makes sense to me but I wonder why the Array API of most
dynamic languages disallow this...
Btw most of the time of you want to reverse the list probably you
could like to insert the elements in a different order
that is using LPUSH instead of RPUSH (or the reverse).

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

Aman Gupta

unread,
Jun 3, 2009, 5:27:57 AM6/3/09
to redi...@googlegroups.com
On Wed, Jun 3, 2009 at 2:22 AM, Salvatore Sanfilippo <ant...@gmail.com> wrote:
>
> On Wed, Jun 3, 2009 at 9:01 AM, Adam Michaels <ilo...@gmail.com> wrote:
>>
>> Does anyone know how to get a range from a list going from right to
>> left?
>
> Hello!
>
> Currently it's not possible, because:
>
> LRANGE mylist 0 4
> LRANGE mylist 4 0
>
> will return the same range.

The second command returns an empty list for me, and the code confirms
this behavior:

if (start > end || start >= llen) {
/* Out of range start or start > end result in empty list */
addReply(c,shared.emptymultibulk);

> Actually it is possible to change the
> semantic of the latter in order to return the elements in reverse
> order. It makes sense to me but I wonder why the Array API of most
> dynamic languages disallow this...

Seems like it would be easy to support this behavior, and I can't
think of a good reason not to.

Aman

Salvatore Sanfilippo

unread,
Jun 3, 2009, 5:36:11 AM6/3/09
to redi...@googlegroups.com
On Wed, Jun 3, 2009 at 11:27 AM, Aman Gupta <themast...@gmail.com> wrote:

> The second command returns an empty list for me, and the code confirms
> this behavior:

Indeed, sorry, I remember I tried to mimic Ruby's behavior.

> Seems like it would be easy to support this behavior, and I can't
> think of a good reason not to.

I agree!
Adding to the post-1.0 todo-list

Cheers,
Salvatore

Michel Martens

unread,
Jun 3, 2009, 11:17:32 AM6/3/09
to redi...@googlegroups.com
On Wed, Jun 3, 2009 at 6:36 AM, Salvatore Sanfilippo <ant...@gmail.com> wrote:
>
> On Wed, Jun 3, 2009 at 11:27 AM, Aman Gupta <themast...@gmail.com> wrote:
>
>> The second command returns an empty list for me, and the code confirms
>> this behavior:
>
> Indeed, sorry, I remember I tried to mimic Ruby's behavior.
>
>> Seems like it would be easy to support this behavior, and I can't
>> think of a good reason not to.
>
> I agree!
> Adding to the post-1.0 todo-list
>

I was trying to come up with a use case for this. Reversing an array
is optimized in most (all?) languages, and besides that, allowing for
this kind of behavior may hide some bugs on the app side. A use case I
can imagine would be to keep a list of users and be able to grab the
last N registered users in one operation. Again, this is something I
can accomplish on the app side (I would add a method last(key, n) in
my app and encapsulate the lrange + reverse logic).

In any case, I don't strongly oppose this addition, especially if you
guys find a use case for it.

--
Michel

Salvatore Sanfilippo

unread,
Jun 3, 2009, 11:26:55 AM6/3/09
to redi...@googlegroups.com
On Wed, Jun 3, 2009 at 5:17 PM, Michel Martens <sov...@gmail.com> wrote:

> In any case, I don't strongly oppose this addition, especially if you
> guys find a use case for it.

Hello Michel,

It's a bit hard to find a legitimate use case for me, because it is
pretty simple to add the elements in the order you want to take them
back. And is not common that you need the two different sorting when
using LRANGE. It makes sense to have the latest comments, but the
first N-comments? And anyway since LRANGE is often used for
pagination, and usually there are few items per page, to just reverse
the taken range in the application side is trivial.

That said it's simple to implement. What about that: to implement this
only if we see a couple of requests of this feature that makes sense
(with explained use case) on the list.

Michel Martens

unread,
Jun 3, 2009, 11:36:27 AM6/3/09
to redi...@googlegroups.com
On Wed, Jun 3, 2009 at 12:26 PM, Salvatore Sanfilippo <ant...@gmail.com> wrote:
>
> On Wed, Jun 3, 2009 at 5:17 PM, Michel Martens <sov...@gmail.com> wrote:
>
>> In any case, I don't strongly oppose this addition, especially if you
>> guys find a use case for it.
>
> Hello Michel,
>
> It's a bit hard to find a legitimate use case for me, because it is
> pretty simple to add the elements in the order you want to take them
> back. And is not common that you need the two different sorting when
> using LRANGE. It makes sense to have the latest comments, but the
> first N-comments? And anyway since LRANGE is often used for
> pagination, and usually there are few items per page, to just reverse
> the taken range in the application side is trivial.
>
> That said it's simple to implement. What about that: to implement this
> only if we see a couple of requests of this feature that makes sense
> (with explained use case) on the list.

Yes, that would be perfect :-)

--
Michel

Adam Michaels

unread,
Jun 3, 2009, 5:07:26 PM6/3/09
to Redis DB
I ran into this when messing around with the Ruby client. Maybe it was
the empty array that was returned that I found confusing. One use case
I can think of would be changing the sorting of a table of info from
asc to desc. In that case, passing the range values in reverse would
return the values in desc order.

This brings up another point in the KV vs. RDB debate. In a RDB you
put the data in and can query it 1000 different ways. In a KV you have
to be very deliberate in how you store your data because it can only
be returned in the same way it was stored.

So I +1 adding this to further enhance the flexibility of Redis.
Although I do think we should wait for more use cases since there is a
bunch more important stuff to do.

On Jun 3, 2:36 am, Salvatore Sanfilippo <anti...@gmail.com> wrote:
> On Wed, Jun 3, 2009 at 11:27 AM, Aman Gupta <themastermi...@gmail.com> wrote:
> > The second command returns an empty list for me, and the code confirms
> > this behavior:
>
> Indeed, sorry, I remember I tried to mimic Ruby's behavior.
>
> > Seems like it would be easy to support this behavior, and I can't
> > think of a good reason not to.
>
> I agree!
> Adding to the post-1.0 todo-list
>
> Cheers,
> Salvatore
>
>
>
>
>
>
>
> >  Aman
>
> >> Btw most of the time of you want to reverse the list probably you
> >> could like to insert the elements in a different order
> >> that is using LPUSH instead of RPUSH (or the reverse).
>
> >> 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
>
> --
> Salvatore 'antirez' Sanfilippohttp://invece.org
Reply all
Reply to author
Forward
0 new messages