From 76d31044d44c7adb4af71dd273b52ec9a4768e17 Mon Sep 17 00:00:00 2001 From: antirez Date: Sat, 5 Dec 2009 19:35:15 +0100 Subject: [PATCH] more HTML doc changes --- TODO | 1 - doc/SetnxCommand.html | 14 +++++++++++--- doc/SortCommand.html | 33 +++++++++++++++++++-------------- redis.c | 2 +- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/TODO b/TODO index 42e126df..fea2c29c 100644 --- a/TODO +++ b/TODO @@ -5,7 +5,6 @@ VERSION 1.2 TODO (Zsets, Integer encoding, Append only journal) Most of the features already implemented for this release. The following is a list of the missing things in order to release the first beta tar.gz: -* Man pages for SRANDMEMBER, missing Z-commands, ... * Write docs for the "STORE" operaiton of SORT. Link to the article about SORT by written by defunkt. VERSION 1.4 TODO (Hash type) diff --git a/doc/SetnxCommand.html b/doc/SetnxCommand.html index ed4ee167..91f9206d 100644 --- a/doc/SetnxCommand.html +++ b/doc/SetnxCommand.html @@ -16,7 +16,7 @@
-SetnxCommand: Contents
  SETNX _key_ _value_
    Return value +SetnxCommand: Contents
  SETNX _key_ _value_
    Return value
    Design pattern: Implementing locking with SETNX
      Handling deadlocks

SetnxCommand

@@ -31,8 +31,16 @@

Return value

Integer reply, specifically:

 1 if the key was set
 0 if the key was not set
-
- +

Design pattern: Implementing locking with SETNX

SETNX can also be seen as a locking primitive. For instance to acquirethe lock of the key foo, the client could try the following:
+
+SETNX lock.foo <current UNIX time + lock timeout + 1>
+
If SETNX returns 1 the client acquired the lock, setting the lock.fookey to the UNIX time at witch the lock should no longer be considered valid.The client will later use DEL lock.foo in order to release the lock.
+
If SETNX returns 0 the key is already locked by some other client. We caneither return to the caller if it's a non blocking lock, or enter aloop retrying to hold the lock until we succeed or some kind of timeoutexpires.
+

Handling deadlocks

In the above locking algorithm there is a problem: what happens if a clientfails, crashes, or is otherwise not able to release the lock?It's possible to detect this condition because the lock key contains aUNIX timestamp. If such a timestamp is <= the current Unix time the lockis no longer valid.
+
When this happens we can't just call DEL against the key to remove the lockand then try to issue a SETNX, as there is a race condition here, whenmultiple clients detected an expired lock and are trying to release it.
+ +
Fortunately it's possible to avoid this issue using the following algorithm.Let's see how C4, our sane client, uses the good algorithm:
+
diff --git a/doc/SortCommand.html b/doc/SortCommand.html index 8022d53c..f89fd7c2 100644 --- a/doc/SortCommand.html +++ b/doc/SortCommand.html @@ -16,7 +16,7 @@
-SortCommand: Contents
    Return value +SortCommand: Contents
    Sorting by external keys
    Retrieving external keys
    Storing the result of a SORT operation
    Return value

SortCommand

@@ -26,33 +26,38 @@
- = SORT key [BY pattern] [LIMIT start count] [GET pattern] [ASC|DESC] [ALPHA] = -
Sort the elements contained in the List or Set value at key. By defaultsorting is numeric with elements being compared as double precisionfloating point numbers. This is the simplest form of SORT.
+ = SORT key [BY pattern] [LIMIT start count] [GET pattern] [ASC|DESC] [ALPHA] [STORE dstkey] = +
Sort the elements contained in the List, Set, orSorted Set value at key. By defaultsorting is numeric with elements being compared as double precisionfloating point numbers. This is the simplest form of SORT:
 SORT mylist
-
Assuming mylist contains a list of numbers, the return value will bethe list of numbers ordered from the smallest to the bigger number.In order to get the sorting in reverse order use DESC:
+
Assuming mylist contains a list of numbers, the return value will bethe list of numbers ordered from the smallest to the biggest number.In order to get the sorting in reverse order use DESC:
 SORT mylist DESC
-
ASC is also supported but it's the default so you don't really need it.If you want to sort lexicographically use ALPHA. Note that Redis isutf-8 aware assuming you set the right value for the LC_COLLATEenvironment variable.
-
Sort is able to limit the number of results using the LIMIT option:
+
The ASC option is also supported but it's the default so you don'treally need it.If you want to sort lexicographically use ALPHA. Note that Redis isutf-8 aware assuming you set the right value for the LC_COLLATEenvironment variable.
+
Sort is able to limit the number of returned elements using the LIMIT option:
 SORT mylist LIMIT 0 10
-
In the above example SORT will return only 10 elements, starting fromthe first one (star is zero-based). Almost all the sort options canbe mixed together. For example:
+
In the above example SORT will return only 10 elements, starting fromthe first one (start is zero-based). Almost all the sort options canbe mixed together. For example the command:
 SORT mylist LIMIT 0 10 ALPHA DESC
 
Will sort mylist lexicographically, in descending order, returning onlythe first 10 elements.
-
Sometimes you want to sort elements using external keys as weights tocompare instead to compare the actual List or Set elements. For examplethe list mylist may contain the elements 1, 2, 3, 4, that are justthe unique IDs of objects stored at object_1, object_2, object_3and object_4, while the keys weight_1, weight_2, weight_3 and weight_4can contain weights we want to use to sort the list of objectsidentifiers. We can use the following command:
-
+
Sometimes you want to sort elements using external keys as weights tocompare instead to compare the actual List Sets or Sorted Set elements.For example the list mylist may contain the elements 1, 2, 3, 4, thatare just unique IDs of objects stored at object_1, object_2, object_3and object_4, while the keys weight_1, weight_2, weight_3 and weight_4can contain weights we want to use to sort our list of objectsidentifiers. We can use the following command:
+

Sorting by external keys

 SORT mylist BY weight_*
-
the BY option takes a pattern (weight_* in our example) that is usedin order to generate the key names of the weights used for sorting.Weight key names are obtained substituting the first occurrence of *with the actual value of the elements on the list (1,2,3,4 in our example).
-
Still our previous example will return just the sorted IDs. Often it isneeded to get the actual objects sorted (object_1, ..., object_4 in theexample). We can do it with the following command:
-
+
the BY option takes a pattern (weight_* in our example) that is usedin order to generate the key names of the weights used for sorting.Weight key names are obtained substituting the first occurrence of *with the actual value of the elements on the list (1,2,3,4 in our example).
+
Our previous example will return just the sorted IDs. Often it isneeded to get the actual objects sorted (object_1, ..., object_4 in theexample). We can do it with the following command:
+

Retrieving external keys

 SORT mylist BY weight_* GET object_*
-
Note that GET can be used multiple times in order to get more keys forevery element of the original List or Set sorted.
+
Note that GET can be used multiple times in order to get more keys forevery element of the original List, Set or Sorted Set sorted.
Since Redis >= 1.1 it's possible to also GET the list elements itselfusing the special # pattern:
 SORT mylist BY weight_* GET object_* GET #
-

Return value

Multi bulk reply, specifically a list of sorted elements. +

Storing the result of a SORT operation

By default SORT returns the sorted elements as its return value.Using the STORE option instead to return the elements SORT willstore this elements as a Redis List in the specified key.An example:
+
+SORT mylist BY weight_* STORE resultkey
+
An interesting pattern using SORT ... STORE consists in associatingan EXPIRE timeout to the resulting key so that inapplications where the result of a sort operation can be cached forsome time other clients will use the cached list instead to call SORTfor every request. When the key will timeout an updated version ofthe cache can be created using SORT ... STORE again.
+
Note that implementing this pattern it is important to avoid that multipleclients will try to rebuild the cached version of the cacheat the same time, so some form of locking should be implemented(for instance using SETNX).
+

Return value

Multi bulk reply, specifically a list of sorted elements.
diff --git a/redis.c b/redis.c index 16027af2..b7dc921a 100644 --- a/redis.c +++ b/redis.c @@ -27,7 +27,7 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#define REDIS_VERSION "1.100" +#define REDIS_VERSION "1.1.90" #include "fmacros.h" #include "config.h" -- 2.45.2