summaryrefslogtreecommitdiffstats
path: root/iredis/data/commands/xrevrange.md
blob: 35d743836ff2941ee8c64d8dd5ccaaf116c9334c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
This command is exactly like `XRANGE`, but with the notable difference of
returning the entries in reverse order, and also taking the start-end range in
reverse order: in `XREVRANGE` you need to state the _end_ ID and later the
_start_ ID, and the command will produce all the element between (or exactly
like) the two IDs, starting from the _end_ side.

So for instance, to get all the elements from the higher ID to the lower ID one
could use:

    XREVRANGE somestream + -

Similarly to get just the last element added into the stream it is enough to
send:

    XREVRANGE somestream + - COUNT 1

## Iterating with XREVRANGE

Like `XRANGE` this command can be used in order to iterate the whole stream
content, however note that in this case, the next command calls should use the
ID of the last entry, with the sequence number decremented by one. However if
the sequence number is already 0, the time part of the ID should be decremented
by 1, and the sequence part should be set to the maximum possible sequence
number, that is, 18446744073709551615, or could be omitted at all, and the
command will automatically assume it to be such a number (see `XRANGE` for more
info about incomplete IDs).

Example:

```
> XREVRANGE writers + - COUNT 2
1) 1) 1526985723355-0
   2) 1) "name"
      2) "Ngozi"
      3) "surname"
      4) "Adichie"
2) 1) 1526985712947-0
   2) 1) "name"
      2) "Agatha"
      3) "surname"
      4) "Christie"
```

The last ID returned is `1526985712947-0`, since the sequence number is already
zero, the next ID I'll use instead of the `+` special ID will be
`1526985712946-18446744073709551615`, or just `18446744073709551615`:

```
> XREVRANGE writers 1526985712946-18446744073709551615 - COUNT 2
1) 1) 1526985691746-0
   2) 1) "name"
      2) "Toni"
      3) "surname"
      4) "Morrison"
2) 1) 1526985685298-0
   2) 1) "name"
      2) "Jane"
      3) "surname"
      4) "Austen"
```

And so for until the iteration is complete and no result is returned. See the
`XRANGE` page about iterating for more information.

@return

@array-reply, specifically:

The command returns the entries with IDs matching the specified range, from the
higher ID to the lower ID matching. The returned entries are complete, that
means that the ID and all the fields they are composed are returned. Moreover
the entries are returned with their fields and values in the exact same order as
`XADD` added them.

@examples

```cli
XADD writers * name Virginia surname Woolf
XADD writers * name Jane surname Austen
XADD writers * name Toni surname Morrison
XADD writers * name Agatha surname Christie
XADD writers * name Ngozi surname Adichie
XLEN writers
XREVRANGE writers + - COUNT 1
```