com.lordofthejars.nosqlunit.redis.embedded
public class ListDatatypeOperations extends ExpirationDatatypeOperations implements RedisDatatypeOperations
| Modifier and Type | Class and Description |
|---|---|
static class |
ListDatatypeOperations.ListPositionEnum |
ExpirationDatatypeOperations.TtlState| Modifier and Type | Field and Description |
|---|---|
protected BlockingMap<ByteBuffer,ByteBuffer> |
blockingMultimap |
protected static String |
LIST |
expirationsInMillis, NO_EXPIRATION| Constructor and Description |
|---|
ListDatatypeOperations() |
| Modifier and Type | Method and Description |
|---|---|
List<byte[]> |
blpop(int timeout,
byte[]... keys)
BLPOP (and BRPOP) is a blocking list pop primitive.
|
List<byte[]> |
brpop(int timeout,
byte[]... keys)
BLPOP (and BRPOP) is a blocking list pop primitive.
|
byte[] |
brpoplpush(byte[] source,
byte[] destination,
int timeout)
Pop a value from a list, push it to another list and return it; or block
until one is available
|
Long |
del(byte[]... keys) |
boolean |
exists(byte[] key) |
void |
flushAllKeys() |
long |
getNumberOfKeys() |
List<byte[]> |
keys() |
byte[] |
lindex(byte[] key,
long index)
Return the specified element of the list stored at the specified key.
|
Long |
linsert(byte[] key,
ListDatatypeOperations.ListPositionEnum where,
byte[] pivot,
byte[] value) |
Long |
llen(byte[] key)
Return the length of the list stored at the specified key.
|
byte[] |
lpop(byte[] key)
Atomically return and remove the first (LPOP) or last (RPOP) element of
the list.
|
Long |
lpush(byte[] key,
byte[]... values)
Add the string value to the head (LPUSH) or tail (RPUSH) of the list
stored at key.
|
Long |
lpushx(byte[] key,
byte[] value) |
List<byte[]> |
lrange(byte[] key,
long start,
long end)
Return the specified elements of the list stored at the specified key.
|
Long |
lrem(byte[] key,
long count,
byte[] value)
Remove the first count occurrences of the value element from the list.
|
String |
lset(byte[] key,
int index,
byte[] value)
Set a new value as the element at index position of the List at key.
|
String |
ltrim(byte[] key,
long start,
long end)
Trim an existing list so that it will contain only the specified range of
elements specified.
|
boolean |
renameKey(byte[] key,
byte[] newKey) |
byte[] |
rpop(byte[] key)
Atomically return and remove the first (LPOP) or last (RPOP) element of
the list.
|
Long |
rpush(byte[] key,
byte[]... values)
Add the string value to the head (LPUSH) or tail (RPUSH) of the list
stored at key.
|
Long |
rpushx(byte[] key,
byte[] value) |
List<byte[]> |
sort(byte[] key) |
String |
type() |
addExpirationAt, addExpirationTime, remainingTime, removeExpiration, renameTtlKey, timedoutStateclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitaddExpirationAt, addExpirationTime, remainingTime, removeExpiration, timedoutStateprotected static final String LIST
protected BlockingMap<ByteBuffer,ByteBuffer> blockingMultimap
public List<byte[]> blpop(int timeout, byte[]... keys)
The following is a description of the exact semantic. We describe BLPOP but the two commands are identical, the only difference is that BLPOP pops the element from the left (head) of the list, and BRPOP pops from the right (tail).
Non blocking behavior
When BLPOP is called, if at least one of the specified keys contain a non empty list, an element is popped from the head of the list and returned to the caller together with the name of the key (BLPOP returns a two elements array, the first element is the key, the second the popped value).
Keys are scanned from left to right, so for instance if you issue BLPOP list1 list2 list3 0 against a dataset where list1 does not exist but list2 and list3 contain non empty lists, BLPOP guarantees to return an element from the list stored at list2 (since it is the first non empty list starting from the left).
Blocking behavior
If none of the specified keys exist or contain non empty lists, BLPOP blocks until some other client performs a LPUSH or an RPUSH operation against one of the lists.
Once new data is present on one of the lists, the client finally returns with the name of the key unblocking it and the popped value.
When blocking, if a non-zero timeout is specified, the client will unblock returning a nil special value if the specified amount of seconds passed without a push operation against at least one of the specified keys.
The timeout argument is interpreted as an integer value. A timeout of zero means instead to block forever.
Multiple clients blocking for the same keys
Multiple clients can block for the same key. They are put into a queue, so the first to be served will be the one that started to wait earlier, in a first-blpopping first-served fashion.
blocking POP inside a MULTI/EXEC transaction
BLPOP and BRPOP can be used with pipelining (sending multiple commands and reading the replies in batch), but it does not make sense to use BLPOP or BRPOP inside a MULTI/EXEC block (a Redis transaction).
The behavior of BLPOP inside MULTI/EXEC when the list is empty is to return a multi-bulk nil reply, exactly what happens when the timeout is reached. If you like science fiction, think at it like if inside MULTI/EXEC the time will flow at infinite speed :)
timeout - keys - When a non-zero timeout is specified, and the BLPOP operation timed out, the return value is a nil multi bulk reply. Most client values will return false or nil accordingly to the programming language used.
#brpop(int, String...)public byte[] brpoplpush(byte[] source,
byte[] destination,
int timeout)
source - destination - timeout - public Long linsert(byte[] key, ListDatatypeOperations.ListPositionEnum where, byte[] pivot, byte[] value)
public byte[] lpop(byte[] key)
If the key does not exist or the list is already empty the special value 'nil' is returned.
key - rpop(byte[])public byte[] rpop(byte[] key)
If the key does not exist or the list is already empty the special value 'nil' is returned.
key - lpop(byte[])public Long llen(byte[] key)
key - public String lset(byte[] key, int index, byte[] value)
Out of range indexes will generate an error.
Similarly to other list commands accepting indexes, the index can be negative to access elements starting from the end of the list. So -1 is the last element, -2 is the penultimate, and so forth.
Time complexity:
key - index - value - #lindex(byte[], int)public byte[] lindex(byte[] key,
long index)
If the value stored at key is not of list type an error is returned. If the index is out of range a 'nil' reply is returned.
Note that even if the average time complexity is O(n) asking for the first or the last element of the list is O(1).
key - index - public List<byte[]> brpop(int timeout, byte[]... keys)
The following is a description of the exact semantic. We describe BLPOP but the two commands are identical, the only difference is that BLPOP pops the element from the left (head) of the list, and BRPOP pops from the right (tail).
Non blocking behavior
When BLPOP is called, if at least one of the specified keys contain a non empty list, an element is popped from the head of the list and returned to the caller together with the name of the key (BLPOP returns a two elements array, the first element is the key, the second the popped value).
Keys are scanned from left to right, so for instance if you issue BLPOP list1 list2 list3 0 against a dataset where list1 does not exist but list2 and list3 contain non empty lists, BLPOP guarantees to return an element from the list stored at list2 (since it is the first non empty list starting from the left).
Blocking behavior
If none of the specified keys exist or contain non empty lists, BLPOP blocks until some other client performs a LPUSH or an RPUSH operation against one of the lists.
Once new data is present on one of the lists, the client finally returns with the name of the key unblocking it and the popped value.
When blocking, if a non-zero timeout is specified, the client will unblock returning a nil special value if the specified amount of seconds passed without a push operation against at least one of the specified keys.
The timeout argument is interpreted as an integer value. A timeout of zero means instead to block forever.
Multiple clients blocking for the same keys
Multiple clients can block for the same key. They are put into a queue, so the first to be served will be the one that started to wait earlier, in a first-blpopping first-served fashion.
blocking POP inside a MULTI/EXEC transaction
BLPOP and BRPOP can be used with pipelining (sending multiple commands and reading the replies in batch), but it does not make sense to use BLPOP or BRPOP inside a MULTI/EXEC block (a Redis transaction).
The behavior of BLPOP inside MULTI/EXEC when the list is empty is to return a multi-bulk nil reply, exactly what happens when the timeout is reached. If you like science fiction, think at it like if inside MULTI/EXEC the time will flow at infinite speed :)
timeout - keys - When a non-zero timeout is specified, and the BLPOP operation timed out, the return value is a nil multi bulk reply. Most client values will return false or nil accordingly to the programming language used.
#blpop(int, String...)public Long lpush(byte[] key, byte[]... values)
key - elements - BinaryJedis#rpush(byte[], byte[]...)public Long lpushx(byte[] key, byte[] value)
public Long rpushx(byte[] key, byte[] value)
public List<byte[]> lrange(byte[] key, long start, long end)
For example LRANGE foobar 0 2 will return the first three elements of the list.
start and end can also be negative numbers indicating offsets from the end of the list. For example -1 is the last element of the list, -2 the penultimate element and so on.
Consistency with range functions in various programming languages
Note that if you have a list of numbers from 0 to 100, LRANGE 0 10 will return 11 elements, that is, rightmost item is included. This may or may not be consistent with behavior of range-related functions in your programming language of choice (think Ruby's Range.new, Array#slice or Python's range() function).
LRANGE behavior is consistent with one of Tcl.
Out-of-range indexes
Indexes out of range will not produce an error: if start is over the end of the list, or start > end, an empty list is returned. If end is over the end of the list Redis will threat it just like the last element of the list.
key - start - end - public Long rpush(byte[] key, byte[]... values)
Time complexity: O(1)
key - elements - BinaryJedis#rpush(byte[], byte[]...)public Long lrem(byte[] key, long count, byte[] value)
Time complexity: O(N) (with N being the length of the list)
key - count - value - public String ltrim(byte[] key, long start, long end)
For example LTRIM foobar 0 2 will modify the list stored at foobar key so that only the first three elements of the list will remain.
start and end can also be negative numbers indicating offsets from the end of the list. For example -1 is the last element of the list, -2 the penultimate element and so on.
Indexes out of range will not produce an error: if start is over the end of the list, or start > end, an empty list is left as value. If end over the end of the list Redis will threat it just like the last element of the list.
Hint: the obvious use of LTRIM is together with LPUSH/RPUSH. For example:
lpush("mylist", "someelement"); ltrim("mylist", 0, 99); *
The above two commands will push elements in the list taking care that the list will not grow without limits. This is very useful when using Redis to store logs for example. It is important to note that when used in this way LTRIM is an O(1) operation because in the average case just one element is removed from the tail of the list.
key - start - end - public long getNumberOfKeys()
getNumberOfKeys in interface RedisDatatypeOperationspublic void flushAllKeys()
flushAllKeys in interface RedisDatatypeOperationspublic Long del(byte[]... keys)
del in interface RedisDatatypeOperationspublic boolean exists(byte[] key)
exists in interface RedisDatatypeOperationspublic boolean renameKey(byte[] key,
byte[] newKey)
renameKey in interface RedisDatatypeOperationspublic List<byte[]> keys()
keys in interface RedisDatatypeOperationspublic String type()
type in interface RedisDatatypeOperationspublic List<byte[]> sort(byte[] key)
sort in interface RedisDatatypeOperationsCopyright © 2014. All Rights Reserved.