Showing posts with label Datastructure. Show all posts
Showing posts with label Datastructure. Show all posts

Basic Redis Command Part -8

Redis is an open-source, BSD-licensed advanced key-value store. It is often referred to as a data structure server since the keys can contain strings, hashes, lists, sets, and sorted sets. Redis is written in C.

Redis is a NoSQL database that follows the principle of a key-value store.

Some commands of Redis.

ZLEXCOUNTThis command counts the number of members in a sorted set between a given lexicographical range.

ZRANGEBYLEXReturns a range of members in a sorted set, by lexicographical range.

ZREMRANGEBYLEX: Removes all members in a sorted set between the given lexicographical range.

ZREMRANGEBYRANK: This command removes all members in a sorted set within the given indexes.

ZREVRANGEBYSCORE: Returns a range of members in a sorted set, by score, with scores ordered from high to low.

ZREVRANK: Determines the index of a member in a sorted set, with scores ordered from high to low.

ZUNIONSTORE: Adds multiple sorted sets and stores the resulting sorted set in a new key.

PFADDThis command adds the specified element to the specified HyperLogLog.

PFCOUNTReturns the approximated cardinality of the set(s) observed by the HyperLogLog at key(s).

PFMERGEMerges N different HyperLogLogs into a single one.


Basic Redis Command part -1

Basic Redis Command Part -2

Basic Redis Command part -3

Basic Redis Command Part -4

Basic Redis Command Part -5

Basic Redis Command Part -6

Basic Redis Command Part -7


PFMERGE command in redis

PFMERGE destkey sourcekey [sourcekey ...]

Merges N different HyperLogLogs into a single one.

Redis PFMERGE command is used to merge multiple HyperLogLog values into a unique value that will approximate the cardinality of the union of the observed sets of the source HyperLogLog structures.

RETURN VALUE

Simple string reply OK.

Syntax:

PFMERGE destkey sourcekey [sourcekey ...]

Example

redis:6379> PFADD mykey a b c d e f g h
(integer) 1
redis:6379> PFADD mykey1 hello redis tutorial
(integer) 1
redis:6379> PFMERGE dstkey mykey mykey1
"OK"

Output

OK


PFCOUNT command in redis

PFCOUNT key [key ...]

Returns the approximated cardinality of the set(s) observed by the HyperLogLog at key(s).

Redis PFCOUNT command is used to get the approximated cardinality computed by the HyperLogLog data structure stored at the specified variable.

Note: If the key does not exist, then it returns 0.

RETURN VALUE

Integer reply, the approximated number of unique elements.

Syntax:

PFCOUNT key [key ...]

Example 1: When the key is present

redis:6379> PFADD mykey a b c d e f g h
(integer) 1
redis:6379> PFADD mykey Hello
(integer) 1
redis:6379> PFCOUNT mykey
(integer) 9

Output

9


Example 2: When the key is not present

redis:6379> PFCOUNT redisKey
(integer) 0

Output

0


PFADD command in redis

PFADD key element [element ...]

This command adds the specified element to the specified HyperLogLog.

Redis PFADD command adds all the element arguments to the HyperLogLog data structure stored at the key name specified as the first argument.

RETURN VALUE: Integer reply, 1 or 0.

Syntax:

PFADD key element [element ...]

Example

redis:6379> PFADD mykey a b c d e f g h
(integer) 1

Output

1

ZUNIONSTORE command in redis

ZUNIONSTORE destination numkeys key [key ...]

Adds multiple sorted sets and stores the resulting sorted set in a new key

Redis ZUNIONSTORE command calculates the union of numkeys sorted sets given by the specified keys, and stores the result in the destination.

It is mandatory to provide the number of input keys (numkeys) before passing the input keys and the other (optional) arguments.

RETURN VALUE

It returns the number of elements in the resulting sorted set at the destination.

Syntax:

ZUNIONSTORE destination numkeys key [key ...]

Example

redis:6379> ZADD mykey 0 A 2 C 3 D 1 E
(integer) 4
redis:6379> ZADD mykey1 0 A 2 C 3 D 11 E 12 F
(integer) 5
redis:6379> ZUNIONSTORE dstkey 2 mykey mykey2
(integer) 4

Output

4

ZREVRANK command in redis

ZREVRANK key member

Determines the index of a member in a sorted set, with scores ordered from high to low.

Redis ZREVRANK command returns the rank of the member in the sorted set stored at the key, with the scores ordered from high to low.

Note:

The rank (or index) is 0-based, which means that the member with the highest score has a rank of 0.

RETURN VALUE

If the member exists in the sorted set, Integer reply − the rank of the member.

If the member does not exist in the sorted set or the key does not exist, Bulk string reply − nil.

Syntax:

ZREVRANK key member

Example 1: When the member exists.

redis:6379> ZADD mykey 0 A 2 C 3 D 1 E
(integer) 4
redis:6379> ZREVRANK mykey A
(integer) 3

Output

3


Example 2: When the member does not exists.

redis:6379> ZADD mykey 0 A 2 C 3 D 1 E
(integer) 4
redis:6379> ZREVRANK mykey AA
(nil)

Output

nil


ZREVRANGEBYSCORE command in redis

ZREVRANGEBYSCORE key max min [WITHSCORES]

Returns a range of members in a sorted set, by score, with scores ordered from high to low.

Redis ZREVRANGEBYSCORE command returns all the elements in the sorted set at the key with a score between max and min (including elements with a score equal to max or min).

Note:

The elements having the same score are returned in a reverse lexicographical order

RETURN VALUE

Array reply, list of elements in the specified score range (optionally with their scores).

Syntax:

ZREVRANGEBYSCORE key max min [WITHSCORES]

Example 1: WITHOUT SCORE

redis:6379> ZADD mykey 0 A 2 C 3 D 1 E
(integer) 4
redis:6379> ZREVRANGEBYSCORE mykey 3 1
1) "D"
2) "C"
3) "E"


Example 2: WITHSCORES

redis:6379> ZADD mykey 0 A 2 C 3 D 1 E
(integer) 4
redis:6379> ZREVRANGEBYSCORE mykey 3 1 WITHSCORES
1) "D"
2) "3"
3) "C"
4) "2"
5) "E"
6) "1"


ZREMRANGEBYRANK command in redis

ZREMRANGEBYRANK key start stop

This command removes all members in a sorted set within the given indexes

Redis ZREMRANGEBYRANK command removes all elements in the sorted set stored at the key with the rank between the start and stop.

Both start and stop are 0-based indexes with 0 being the element with the lowest score. These indexes can be negative numbers, where they indicate offsets starting at the element with the highest score.

RETURN VALUE

Integer reply, the number of elements removed.

Syntax:

ZREMRANGEBYRANK key start stop

Example

redis:6379> ZADD mykey 0 A 2 C 3 D 1 E
(integer) 4
redis:6379> ZREMRANGEBYRANK mykey 0 3
(integer) 4

Output

4

ZREMRANGEBYLEX command in redis

ZREMRANGEBYLEX key min max

Removes all members in a sorted set between the given lexicographical range

Redis ZREMRANGEBYLEX command removes all the elements in the sorted set stored at the key between the lexicographical range specified by min and max.

RETURN VALUE

Integer reply, i.e the number of elements removed.

Syntax

ZREMRANGEBYLEX key min max

Example 

redis:6379> ZADD mykey 0 A 2 C 3 D 1 E
(integer) 4
redis:6379> ZREMRANGEBYLEX mykey - [D
(integer) 1
redis:6379> ZREMRANGEBYLEX mykey - (a
(integer) 3


ZRANGEBYLEX command in redis

ZRANGEBYLEX key min max [LIMIT offset count]

Returns a range of members in a sorted set, by lexicographical range.

Redis ZRANGEBYLEX command returns the specified range of elements in the sorted set stored at the key.

The elements are considered to be ordered from the lowest to the highest score. Lexicographical order is used for elements with an equal score.

Both start and stop are zero-based indexes, where 0 is the first element, 1 is the next element, and so on. They can also be negative numbers indicating offsets from the end of the sorted set, with -1 being the last element of the sorted set, -2 the penultimate element, and so on.

Return Value

Array reply, list of elements in the specified score range.

Syntax:

ZRANGEBYLEX key min max

Example

redis:6379> ZADD mykey 0 A 2 C 3 D 1 E
(integer) 4
redis:6379> ZRANGEBYLEX mykey - (E
1) "A"
redis:6379> ZRANGEBYLEX mykey - [E
1) "A"
2) "E"
3) "C"
4) "D"



ZLEXCOUNT command in redis

ZLEXCOUNT key min max

This command counts the number of members in a sorted set between a given lexicographical range.

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 the key with a value between min and max.

RETURN VALUE:

Integer reply, the number of elements in the specified score range.

Syntax:

ZLEXCOUNT key min max

Example 1: When the min and max are valid string

redis:6379> ZADD mykey 2 A 1 C 3 B 4 E 5 D
(integer) 5 
redis:6379> ZLEXCOUNT mykey - +
(integer) 5

Output

5


Example 2: When the min and max are not a valid string

redis:6379> ZADD mykey 2 A 1 C 3 B 4 E 5 D
(integer) 5
redis:6379> ZLEXCOUNT mykey 1 7
(error) min or max not valid string range item
redis:6379> ZLEXCOUNT mykey A Z
(error) min or max not valid string range item

Output

(error) min or max not valid string range item


Basic Redis Command Part -7

Redis is an open-source, BSD-licensed advanced key-value store. It is often referred to as a data structure server since the keys can contain strings, hashes, lists, sets, and sorted sets. Redis is written in C.

Redis is a NoSQL database that follows the principle of a key-value store.

Some commands of Redis.

ZINCRBYIf the member already exists in the sorted set add the increment to its score and update the position of the element in the sorted set accordingly.

ZRANGEReturn the specified elements of the sorted set at the specified key.

ZRANGEBYSCOREReturn all the elements in the sorted set at a key with a score between min and max (including elements with a score equal to min or max).

ZRANKReturns the rank of member in the sorted set stored at key, with the scores ordered from low to high.

ZREMZREM command in redis removes the specified member from the sorted set value stored at the key.

ZREMRANGEBYSCORERemove all the elements in the sorted set at a key with a score between min and max (including elements with a score equal to min or max).

ZREVRANGEReturn the specified elements of the sorted set at the specified key.

ZSCOREReturn the score of the specified element of the sorted set at the key.

ZCOUNTThis command counts the members in a sorted set with scores within the given values.

ZINTERSTOREIntersects multiple sorted sets and stores the resulting sorted set in a new key


Basic Redis Command part -1

Basic Redis Command Part -2

Basic Redis Command part -3

Basic Redis Command Part -4

Basic Redis Command Part -5

Basic Redis Command Part -6

Basic Redis Command Part -8


ZINTERSTORE command in redis

ZINTERSTORE destination numkeys key [key ...]

Intersects multiple sorted sets and stores the resulting sorted set in a new key

ZINTERSTORE command in redis computes the intersection of numkeys sorted sets given by the specified keys, and stores the result in the destination. 

RETURN VALUE:

Integer reply, the number of elements in the resulting sorted set at the destination

Syntax:

ZINTERSTORE destination numkeys key [key ...]

Example 1: When the intersection is present

redis:6379> ZADD mykey 1 A 2 B 3 C 4 D 5 E
(integer) 5
redis:6379> ZADD mykey2 1 A 2 B 4 D
(integer) 3
redis:6379> ZINTERSTORE dstKey 2 mykey mykey2
(integer) 3

Output

3


Example 2: When the intersection is not present

redis:6379> ZADD mykey 1 A 2 B 3 C 4 D 5 E
(integer) 5
redis:6379> ZADD mykey1 10 AA 20 BB 30 CC 40 DD 50 EE
(integer) 5
redis:6379> ZINTERSTORE dstKey 2 mykey mykey1
(integer) 0

Output

0

ZCOUNT command in redis

ZCOUNT key min max

This command counts the members in a sorted set with scores within the given values.

ZCOUNT command in redis returns the number of elements in the sorted set at the key with a score between min and max.

RETURN VALUE: 

Integer reply, the number of elements in the specified score range.

Syntax:

ZCOUNT KEY_NAME min max

Example 1: When  the key is present and min and max are in the range

redis:6379> ZADD mykey 1 A 2 B 3 C 4 D 5 E
(integer) 5
redis:6379> ZCOUNT mykey 1 3
(integer) 3

Output

3


Example 2: When  the key is not present or min and max are out of range

redis:6379> ZADD mykey 1 A 2 B 3 C 4 D 5 E
(integer) 5
redis:6379> ZCOUNT redisKey 1 3
(integer) 0
redis:6379> ZCOUNT mykey 10 20
(integer) 0

Output

0


ZSCORE command in redis

ZSCORE key member

Return the score of the specified element of the sorted set at the key.

Note: If the specified element does not exist in the sorted set, or the key does not exist at all, a special 'nil' value is returned.

TIME COMPLEXITY: O(1)

RETURN VALUE: Bulk reply, the score of member (a double precision floating point number), represented as a string.

Syntax:

ZSCORE key member

Example 1: When the key and member are present.

redis:6379> ZADD mykey 1 A 2 B 3 C 4 D 5 E
(integer) 5
redis:6379> ZSCORE mykey A
"1"

Output

1


Example 2: When the key or member is not present.

redis:6379> ZADD mykey 1 A 2 B 3 C 4 D 5 E
(integer) 5
redis:6379> ZSCORE mykey AA
(nil)
redis:6379> ZSCORE redisKey A
(nil)

Output

nil


ZREVRANGE command in redis

ZREVRANGE key start end [WITHSCORES]

Return the specified elements of the sorted set at the specified key.

The elements are considered sorted from the lowerest to the highest score when using ZRANGE, and in the reverse order when using ZREVRANGE.

Start and end are zero-based indexes. 0 is the first element of the sorted set (the one with the lowerest score when using ZRANGE), 1 is the next element by score, and so on.


TIME COMPLEXITY: O(log(N))+O(M) (with N being the number of elements in the sorted set and M the number of elements requested)

Note:

1. start and end can also be negative numbers indicating offsets from the end of the sorted set. For example, -1 is the last element of the sorted set, -2 is the penultimate element, and so on.

2. Indexes out of range will not produce an error: if start is over the end of the sorted set, or start > end, an empty list is returned. If the end is over at the end of the sorted set Redis will threaten it just like the last element of the sorted set.

RETURN VALUE: Multi bulk reply, specifically a list of elements in the specified range.

Syntax:

ZREVRANGE key start end

Example 1: When the key is present and the start and end are in the range.

redis:6379> ZADD mykey 1 A 2 B 3 C 4 D 5 E
(integer) 5
redis:6379> ZREVRANGE mykey 1 4
1) "D"
2) "C"
3) "B"
4) "A"

Output

1) "D"

2) "C"

3) "B"

4) "A"


Example 2: When the key is not present or the start and end are in out of range.

redis:6379> ZADD mykey 1 A 2 B 3 C 4 D 5 E
(integer) 5
redis:6379> ZREVRANGE redisKey 1 4
(empty array)
redis:6379> ZREVRANGE mykey 10 20
(empty array)

Output

empty array


ZREMRANGEBYSCORE command in redis

ZREMRANGEBYSCORE key min max

Remove all the elements in the sorted set at a key with a score between min and max (including elements with a score equal to min or max).

TIME COMPLEXITY: O(log(N))+O(M) with N being the number of elements in the sorted set and M being the number of elements removed by the operation.

RETURN VALUE: Integer reply, specifically the number of elements removed.

Syntax:

ZREMRANGEBYSCORE key min max

Example 1: When the key is present and min and max in the range.

redis:6379> ZADD mykey 1 A 2 B 3 C 4 D 5 E
(integer) 5
redis:6379> ZREMRANGEBYSCORE mykey 1 3
(integer) 2

Output

2

Example 2: When the key is not present or min and max is out of range.

redis:6379> ZADD mykey 1 A 2 B 3 C 4 D 5 E
(integer) 5
redis:6379> ZREMRANGEBYSCORE myredisKey 1 4
(integer) 0
redis:6379> ZREMRANGEBYSCORE mykey 10 20
(integer) 0

Output

0

ZREM command in redis

ZREM key member

ZREM command in redis removes the specified member from the sorted set value stored at the key.

Note:

1. If a member was not a member of the set no operation is performed.

2. If the key does not hold a set value an error is returned.

TIME COMPLEXITY: O(log(N)) with N being the number of elements in the sorted set

RETURN VALUE: Integer reply, specifically:

1 if the new element was removed

0 if the new element was not a member of the set.

Syntax:

ZREM key member

Example 1: When the member is present

redis:6379> ZADD mykey 1 A 2 B 3 C 4 D 5 E
(integer) 5
redis:6379> ZREM mykey A
(integer) 1

Output

1


Example 2: When the member is not present

redis:6379> ZADD mykey 1 A 2 B 3 C 4 D 5 E
(integer) 5
redis:6379> ZREM myKey AA
(integer) 0

Output

0


ZRANK command in redis

ZRANK key member

Returns the rank of member in the sorted set stored at key, with the scores ordered from low to high. The rank (or index) is 0-based, which means that the member with the lowest score has a rank of 0.

TIME COMPLEXITY: O(log(N))

RETURN VALUE:

If a member exists in the sorted set, Integer reply: the rank of the member.

If the member does not exist in the sorted set or key does not exist, Bulk reply: nil.

Syntax:

ZRANK key member

Example 1: When the member is present

redis:6379> ZADD mykey 1 A 2 B 3 C 4 D 5 E
(integer) 5
redis:6379> ZRANK mykey A
(integer) 0
redis:6379> ZRANK mykey B
(integer) 1


Example 2: When the member or key is not present

redis:6379> ZADD mykey 1 A 2 B 3 C 4 D 5 E
(integer) 5
redis:6379> ZRANK mykey AA
(nil)

Output

nil


ZRANGEBYSCORE command in redis

ZRANGEBYSCORE key min max [LIMIT offset count] (Redis >= 1.1)

ZRANGEBYSCORE key min max [LIMIT offset count] [WITHSCORES] (Redis >= 1.3.4)

Return all the elements in the sorted set at a key with a score between min and max (including elements with a score equal to min or max).

TIME COMPLEXITY: O(log(N))+O(M) with N being the number of elements in the sorted set and M the number of elements returned by the command, so if M is constant (for instance you always ask for the first ten elements with LIMIT) you can consider it O(log(N))

RETURN VALUE: Multi bulk reply, specifically a list of elements in the specified score range.

Syntax:

ZRANGEBYSCORE key min max

Example 1: When the key is present.

redis:6379> ZADD mykey 1 A 2 B 3 C 4 D 5 E
(integer) 5
redis:6379> ZRANGEBYSCORE mykey 1 4
1) "A"
2) "B"
3) "C"
4) "D"

Output

1) "A"

2) "B"

3) "C"

4) "D"


Example 2: When the key is not present.

redis:6379> ZRANGEBYSCORE redisKey 1 5
(empty array)

Output

empty array