Skip to content

Commit

Permalink
Implemented ZRANGEBYLEX command
Browse files Browse the repository at this point in the history
  • Loading branch information
qrilka committed Mar 27, 2016
1 parent c02bb60 commit a854598
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
4 changes: 3 additions & 1 deletion codegen/GenCmds.hs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ blacklist = [ manual "AUTH" ["auth"]
, manual "SSCAN" ["sscan", "sscanOpts"]
, manual "HSCAN" ["hscan", "hscanOpts"]
, manual "ZSCAN" ["zscan", "zscanOpts"]
, manualWithType "ZRANGEBYLEX"
["zrangebylex, zrangebylexLimit"]
["RangeLex(..)"]
, unimplemented "COMMAND"
, unimplemented "COMMAND GETKEYS"
, unimplemented "ROLE"
, unimplemented "CLIENT KILL"
, unimplemented "ZRANGEBYLEX"
, unimplemented "ZREVRANGEBYLEX"
, unimplemented "ZRANGEBYSCORE"
, unimplemented "ZREVRANGEBYSCORE"
Expand Down
5 changes: 2 additions & 3 deletions src/Database/Redis/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ zinterstoreWeights, -- |Intersect multiple sorted sets and store the resulting s
zlexcount, -- |Count the number of members in a sorted set between a given lexicographical range (<http://redis.io/commands/zlexcount>). Since Redis 2.8.9
zrange, -- |Return a range of members in a sorted set, by index (<http://redis.io/commands/zrange>). The Redis command @ZRANGE@ is split up into 'zrange', 'zrangeWithscores'. Since Redis 1.2.0
zrangeWithscores, -- |Return a range of members in a sorted set, by index (<http://redis.io/commands/zrange>). The Redis command @ZRANGE@ is split up into 'zrange', 'zrangeWithscores'. Since Redis 1.2.0
RangeLex(..),
zrangebylex, zrangebylexLimit, -- |Return a range of members in a sorted set, by lexicographical range (<http://redis.io/commands/zrangebylex>). Since Redis 2.8.9
zrangebyscore, -- |Return a range of members in a sorted set, by score (<http://redis.io/commands/zrangebyscore>). The Redis command @ZRANGEBYSCORE@ is split up into 'zrangebyscore', 'zrangebyscoreWithscores', 'zrangebyscoreLimit', 'zrangebyscoreWithscoresLimit'. Since Redis 1.0.5
zrangebyscoreWithscores, -- |Return a range of members in a sorted set, by score (<http://redis.io/commands/zrangebyscore>). The Redis command @ZRANGEBYSCORE@ is split up into 'zrangebyscore', 'zrangebyscoreWithscores', 'zrangebyscoreLimit', 'zrangebyscoreWithscoresLimit'. Since Redis 1.0.5
zrangebyscoreLimit, -- |Return a range of members in a sorted set, by score (<http://redis.io/commands/zrangebyscore>). The Redis command @ZRANGEBYSCORE@ is split up into 'zrangebyscore', 'zrangebyscoreWithscores', 'zrangebyscoreLimit', 'zrangebyscoreWithscoresLimit'. Since Redis 1.0.5
Expand Down Expand Up @@ -239,9 +241,6 @@ strlen, -- |Get the length of the value stored in a key (<http://redis.io/comman
-- * CLIENT KILL (<http://redis.io/commands/client-kill>)
--
--
-- * ZRANGEBYLEX (<http://redis.io/commands/zrangebylex>)
--
--
-- * ZREVRANGEBYLEX (<http://redis.io/commands/zrevrangebylex>)
--
--
Expand Down
32 changes: 30 additions & 2 deletions src/Database/Redis/ManualCommands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module Database.Redis.ManualCommands where

import Prelude hiding (min,max)
import Data.ByteString (ByteString, empty)
import Prelude hiding (min, max)
import Data.ByteString (ByteString, empty, append)
import Data.Maybe (maybeToList)
import Database.Redis.Core
import Database.Redis.Protocol
Expand Down Expand Up @@ -752,3 +752,31 @@ zscanOpts
-> ScanOpts
-> m (f (Cursor, [(ByteString, Double)])) -- ^ next cursor and values
zscanOpts key cursor opts = sendRequest $ addScanOpts ["ZSCAN", key, encode cursor] opts

data RangeLex a = Incl a | Excl a | Minr | Maxr

instance RedisArg a => RedisArg (RangeLex a) where
encode (Incl bs) = "[" `append` encode bs
encode (Excl bs) = "(" `append` encode bs
encode Minr = "-"
encode Maxr = "+"

zrangebylex::(RedisCtx m f) =>
ByteString -- ^ key
-> RangeLex ByteString -- ^ min
-> RangeLex ByteString -- ^ max
-> m (f [ByteString])
zrangebylex key min max =
sendRequest ["ZRANGEBYLEX", encode key, encode min, encode max]

zrangebylexLimit
::(RedisCtx m f)
=> ByteString -- ^ key
-> RangeLex ByteString -- ^ min
-> RangeLex ByteString -- ^ max
-> Integer -- ^ offset
-> Integer -- ^ count
-> m (f [ByteString])
zrangebylexLimit key min max offset count =
sendRequest ["ZRANGEBYLEX", encode key, encode min, encode max,
"LIMIT", encode offset, encode count]
11 changes: 10 additions & 1 deletion test/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ tests :: Connection -> [Test.Test]
tests conn = map ($conn) $ concat
[ testsMisc, testsKeys, testsStrings, [testHashes], testsLists, testsSets, [testHyperLogLog]
, testsZSets, [testPubSub], [testTransaction], [testScripting]
, testsConnection, testsServer, [testScans]
, testsConnection, testsServer, [testScans], [testZrangelex]
-- should always be run last as connection gets closed after it
, [testQuit]
]
Expand Down Expand Up @@ -534,3 +534,12 @@ testScans = testCase "scans" $ do
hscan "hash" cursor0 >>=? (cursor0, [("k", "v")])
zadd "zset" [(42, "2")] >>=? 1
zscan "zset" cursor0 >>=? (cursor0, [("2", 42)])

testZrangelex ::Test
testZrangelex = testCase "zrangebylex" $ do
let testSet = [(10, "aaa"), (10, "abb"), (10, "ccc"), (10, "ddd")]
zadd "zrangebylex" testSet >>=? 4
zrangebylex "zrangebylex" (Incl "aaa") (Incl "bbb") >>=? ["aaa","abb"]
zrangebylex "zrangebylex" (Excl "aaa") (Excl "ddd") >>=? ["abb","ccc"]
zrangebylex "zrangebylex" Minr Maxr >>=? ["aaa","abb","ccc","ddd"]
zrangebylexLimit "zrangebylex" Minr Maxr 2 1 >>=? ["ccc"]

0 comments on commit a854598

Please sign in to comment.