Skip to content

Commit

Permalink
Breaking change: change API of the sadd and del to accept NonEmpty lists
Browse files Browse the repository at this point in the history
Using sadd and del requires to pass at least one parameter and
it hurts users. So we change and API as it was discussed  in issue #44.

Fixes #44.
  • Loading branch information
qnikst committed Jun 10, 2023
1 parent ac46396 commit 9b77caf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
22 changes: 15 additions & 7 deletions src/Database/Redis/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ quit, -- |Close the connection (<http://redis.io/commands/quit>). Since Redis 1.
select, -- |Change the selected database for the current connection (<http://redis.io/commands/select>). Since Redis 1.0.0

-- ** Keys
del, -- |Delete a key (<http://redis.io/commands/del>). Since Redis 1.0.0
del,
dump, -- |Return a serialized version of the value stored at the specified key (<http://redis.io/commands/dump>). Since Redis 2.6.0
exists, -- |Determine if a key exists (<http://redis.io/commands/exists>). Since Redis 1.0.0
expire, -- |Set a key's time to live in seconds (<http://redis.io/commands/expire>). Since Redis 1.0.0
Expand Down Expand Up @@ -141,7 +141,7 @@ slowlogReset, -- |Manages the Redis slow queries log (<http://redis.io/commands/
time, -- |Return the current server time (<http://redis.io/commands/time>). Since Redis 2.6.0

-- ** Sets
sadd, -- |Add one or more members to a set (<http://redis.io/commands/sadd>). Since Redis 1.0.0
sadd,
scard, -- |Get the number of members in a set (<http://redis.io/commands/scard>). Since Redis 1.0.0
sdiff, -- |Subtract multiple sets (<http://redis.io/commands/sdiff>). Since Redis 1.0.0
sdiffstore, -- |Subtract multiple sets and store the resulting set in a key (<http://redis.io/commands/sdiffstore>). Since Redis 1.0.0
Expand Down Expand Up @@ -369,6 +369,8 @@ command

import Prelude hiding (min,max)
import Data.ByteString (ByteString)
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NE
import Database.Redis.ManualCommands
import Database.Redis.Types
import Database.Redis.Core(sendRequest, RedisCtx)
Expand Down Expand Up @@ -521,12 +523,15 @@ flushdb
=> m (f Status)
flushdb = sendRequest (["FLUSHDB"] )

-- | /O(1)/ for each element added.
-- Add one or more members to a set (<http://redis.io/commands/sadd>).
-- Since Redis 1.0.0
sadd
:: (RedisCtx m f)
=> ByteString -- ^ key
-> [ByteString] -- ^ member
=> ByteString -- ^ Key where set is stored.
-> NonEmpty ByteString -- ^ Member to add to the set.
-> m (f Integer)
sadd key member = sendRequest (["SADD"] ++ [encode key] ++ map encode member )
sadd key member = sendRequest (["SADD"] ++ [encode key] ++ NE.toList (fmap encode member))

lindex
:: (RedisCtx m f)
Expand Down Expand Up @@ -926,11 +931,14 @@ setrange
-> m (f Integer)
setrange key offset value = sendRequest (["SETRANGE"] ++ [encode key] ++ [encode offset] ++ [encode value] )

-- | Delete a key (<http://redis.io/commands/del>).
-- Returns a number of keys that were removed.
-- Since Redis 1.0.0
del
:: (RedisCtx m f)
=> [ByteString] -- ^ key
=> NonEmpty ByteString -- ^ List of keys to delete.
-> m (f Integer)
del key = sendRequest (["DEL"] ++ map encode key )
del key = sendRequest (["DEL"] ++ (NE.toList (fmap encode key)))

hincrbyfloat
:: (RedisCtx m f)
Expand Down
19 changes: 10 additions & 9 deletions test/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Control.Monad
import Control.Monad.Trans
import Data.Either (isRight)
import qualified Data.List as L
import qualified Data.List.NonEmpty as NE
import Data.Time
import Data.Time.Clock.POSIX
import qualified Test.Framework as Test (Test)
Expand Down Expand Up @@ -150,7 +151,7 @@ testKeys = testCase "keys" $ do
restore "{same}key'" 0 s >>=? Ok
rename "{same}key" "{same}key'" >>=? Ok
renamenx "{same}key'" "{same}key" >>=? True
del ["{same}key"] >>=? 1
del (NE.fromList ["{same}key"]) >>=? 1

testKeysNoncluster :: Test
testKeysNoncluster = testCase "keysNoncluster" $ do
Expand Down Expand Up @@ -198,12 +199,12 @@ testGetType = testCase "getType" $ do
forM_ ts $ \(setKey, typ) -> do
setKey
getType "key" >>=? typ
del ["key"] >>=? 1
del (NE.fromList ["key"]) >>=? 1
where
ts = [ (set "key" "value" >>=? Ok, String)
, (hset "key" "field" "value" >>=? 1, Hash)
, (lpush "key" ["value"] >>=? 1, List)
, (sadd "key" ["member"] >>=? 1, Set)
, (sadd "key" (NE.fromList ["member"]) >>=? 1, Set)
, (zadd "key" [(42,"member"),(12.3,"value")] >>=? 2, ZSet)
]

Expand Down Expand Up @@ -240,7 +241,7 @@ testStrings = testCase "strings" $ do
incr "key" >>=? 41
incrby "key" 1 >>=? 42
incrbyfloat "key" 1 >>=? 43
del ["key"] >>=? 1
del (NE.fromList ["key"]) >>=? 1
setbit "key" 42 "1" >>=? 0
getbit "key" 42 >>=? 1
bitcount "key" >>=? 1
Expand Down Expand Up @@ -318,22 +319,22 @@ testsSets = [testSets, testSetAlgebra]

testSets :: Test
testSets = testCase "sets" $ do
sadd "set" ["member"] >>=? 1
sadd "set" (NE.fromList ["member"]) >>=? 1
sismember "set" "member" >>=? True
scard "set" >>=? 1
smembers "set" >>=? ["member"]
srandmember "set" >>=? Just "member"
spop "set" >>=? Just "member"
srem "set" ["member"] >>=? 0
smove "{same}set" "{same}set'" "member" >>=? False
_ <- sadd "set" ["member1", "member2"]
_ <- sadd "set" (NE.fromList ["member1", "member2"])
(fmap L.sort <$> spopN "set" 2) >>=? ["member1", "member2"]
_ <- sadd "set" ["member1", "member2"]
_ <- sadd "set" (NE.fromList ["member1", "member2"])
(fmap L.sort <$> srandmemberN "set" 2) >>=? ["member1", "member2"]

testSetAlgebra :: Test
testSetAlgebra = testCase "set algebra" $ do
sadd "{same}s1" ["member"] >>=? 1
sadd "{same}s1" (NE.fromList ["member"]) >>=? 1
sdiff ["{same}s1", "{same}s2"] >>=? ["member"]
sunion ["{same}s1", "{same}s2"] >>=? ["member"]
sinter ["{same}s1", "{same}s2"] >>=? []
Expand Down Expand Up @@ -711,7 +712,7 @@ testScans = testCase "scans" $ do

testSScan :: Test
testSScan = testCase "sscan" $ do
sadd "set" ["1"] >>=? 1
sadd "set" (NE.fromList ["1"]) >>=? 1
sscan "set" cursor0 >>=? (cursor0, ["1"])

testHScan :: Test
Expand Down

0 comments on commit 9b77caf

Please sign in to comment.