Skip to content

Commit

Permalink
添加 ZLEX 相关文档的原文
Browse files Browse the repository at this point in the history
  • Loading branch information
huangzworks committed Jul 22, 2014
1 parent 5e41428 commit b7f4706
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sorted_set/index.rst
Expand Up @@ -21,3 +21,6 @@ SortedSet(有序集合)
zunionstore
zinterstore
zscan
zlexcount
zrangebylex
zremrangebylex
35 changes: 35 additions & 0 deletions sorted_set/zlexcount.rst
@@ -0,0 +1,35 @@
.. _zlexcount:

ZLEXCOUNT
==============

**ZLEXCOUNT key min max**

When all the elements in a sorted set are inserted with the same score, in order to force lexicographical ordering, this command returns the number of elements in the sorted set at key with a value between min and max.

The min and max arguments have the same meaning as described for ZRANGEBYLEX.

Note: the command has a complexity of just O(log(N)) because it uses elements ranks (see ZRANK) to get an idea of the range. Because of this there is no need to do a work proportional to the size of the range.

**可用版本:**
>= 2.8.9

**时间复杂度:**
O(log(N)) with N being the number of elements in the sorted set.

**返回值:**
Integer reply: the number of elements in the specified score range.

::

redis> ZADD myzset 0 a 0 b 0 c 0 d 0 e
(integer) 5

redis> ZADD myzset 0 f 0 g
(integer) 2

redis> ZLEXCOUNT myzset - +
(integer) 7

redis> ZLEXCOUNT myzset [b [f
(integer) 5
70 changes: 70 additions & 0 deletions sorted_set/zrangebylex.rst
@@ -0,0 +1,70 @@
.. _zrangebylex:

ZRANGEBYLEX
==================

**ZRANGEBYLEX key min max [LIMIT offset count]**

When all the elements in a sorted set are inserted with the same score, in order to force lexicographical ordering, this command returns all the elements in the sorted set at key with a value between min and max.

If the elements in the sorted set have different scores, the returned elements are unspecified.

The elements are considered to be ordered from lower to higher strings as compared byte-by-byte using the memcmp() C function. Longer strings are considered greater than shorter strings if the common part is identical.

The optional LIMIT argument can be used to only get a range of the matching elements (similar to SELECT LIMIT offset, count in SQL). Keep in mind that if offset is large, the sorted set needs to be traversed for offset elements before getting to the elements to return, which can add up to O(N) time complexity.


How to specify intervals
---------------------------

Valid start and stop must start with ( or [, in order to specify if the range item is respectively exclusive or inclusive. The special values of + or - for start and stop have the special meaning or positively infinite and negatively infinite strings, so for instance the command ZRANGEBYLEX myzset - + is guaranteed to return all the elements in the sorted set, if all the elements have the same score.


Details on strings comparison
--------------------------------

Strings are compared as binary array of bytes. Because of how the ASCII character set is specified, this means that usually this also have the effect of comparing normal ASCII characters in an obvious dictionary way. However this is not true if non plain ASCII strings are used (for example utf8 strings).

However the user can apply a transformation to the encoded string so that the first part of the element inserted in the sorted set will compare as the user requires for the specific application. For example if I want to add strings that will be compared in a case-insensitive way, but I still want to retrieve the real case when querying, I can add strings in the following way:

::

ZADD autocomplete 0 foo:Foo 0 bar:BAR 0 zap:zap

Because of the first normalized part in every element (before the colon character), we are forcing a given comparison, however after the range is queries using ZRANGEBYLEX the application can display to the user the second part of the string, after the colon.

The binary nature of the comparison allows to use sorted sets as a general purpose index, for example the first part of the element can be a 64 bit big endian number: since big endian numbers have the most significant bytes in the initial positions, the binary comparison will match the numerical comparison of the numbers. This can be used in order to implement range queries on 64 bit values. As in the example below, after the first 8 bytes we can store the value of the element we are actually indexing.


**可用版本:**
>= 2.8.9


**时间复杂度:**
O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).


**返回值:**
Array reply: list of elements in the specified score range.


::

redis> ZADD myzset 0 a 0 b 0 c 0 d 0 e 0 f 0 g
(integer) 7

redis> ZRANGEBYLEX myzset - [c
1) "a"
2) "b"
3) "c"

redis> ZRANGEBYLEX myzset - (c
1) "a"
2) "b"

redis> ZRANGEBYLEX myzset [aaa (g
1) "b"
2) "c"
3) "d"
4) "e"
5) "f"
48 changes: 48 additions & 0 deletions sorted_set/zremrangebylex.rst
@@ -0,0 +1,48 @@
.. _zremrangebylex:

ZREMRANGEBYLEX
===============

**ZREMRANGEBYLEX key min max**

When all the elements in a sorted set are inserted with the same score, in order to force lexicographical ordering, this command removes all elements in the sorted set stored at key between the lexicographical range specified by min and max.

The meaining of min and max are the same of the ZRANGEBYLEX command. Similarly, this command actually returns the same elements that ZRANGEBYLEX would return if called with the same min and max arguments.

**可用版本:**
>= 2.8.9

**时间复杂度:**
O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements removed by the operation.

**返回值:**
Integer reply: the number of elements removed.

::

redis> ZADD myzset 0 aaaa 0 b 0 c 0 d 0 e
(integer) 5

redis> ZADD myzset 0 foo 0 zap 0 zip 0 ALPHA 0 alpha
(integer) 5

redis> ZRANGE myzset 0 -1
1) "ALPHA"
2) "aaaa"
3) "alpha"
4) "b"
5) "c"
6) "d"
7) "e"
8) "foo"
9) "zap"
10) "zip"

redis> ZREMRANGEBYLEX myzset [alpha [omega
(integer) 6

redis> ZRANGE myzset 0 -1
1) "ALPHA"
2) "aaaa"
3) "zap"
4) "zip"

0 comments on commit b7f4706

Please sign in to comment.