module HLRDB.Structures.HSet where
import Database.Redis as Redis
import HLRDB.Primitives.Redis
import HLRDB.Internal
import Control.Monad.State
hgetall :: MonadRedis m => RedisHSet a s b -> a -> m [ (s,b) ]
hgetall p@(RHSet (E _ _ d) (HSET _ ds)) =
(fmap . fmap) (\(s,b) -> (ds s , d (pure b)))
. unwrap
. Redis.hgetall
. primKey p
hget :: MonadRedis m => RedisHSet a s b -> a -> s -> m (Maybe b)
hget p@(RHSet (E _ _ d) (HSET e _)) k =
(fmap . fmap) (d . pure)
. unwrap
. Redis.hget (primKey p k)
. e
hmget :: (MonadRedis m , Traversable t) => RedisHSet a s b -> a -> t s -> m (t (s , Maybe b))
hmget p@(RHSet (E _ _ d) (HSET e _)) k t = do
let f = (fmap . fmap . fmap) (d . pure) . fixEmpty (unwrap . Redis.hmget (primKey p k)) id . fmap e
let xs = foldr (:) [] t
reifyTraversal t <$> liftRedis (f xs)
where
reifyTraversal :: Traversable t => t a -> [ b ] -> t (a,b)
reifyTraversal tr bs = evalState (traverse g tr) bs
where
g a = do
xs <- Control.Monad.State.get
case xs of
[] -> error "Impossible in hmget: unexpected data size in HLRDB.Structures.HSet.hmget"
(b:bs') -> do
put bs'
return (a,b)
hset :: MonadRedis m => RedisHSet a s b -> a -> s -> b -> m (ActionPerformed Creation)
hset p@(RHSet (E _ eb _) (HSET e _)) k s =
unwrapCreatedBool
. Redis.hset (primKey p k) (e s)
. runIdentity
. eb
hmset :: (MonadRedis m , Traversable t) => RedisHSet a s b -> a -> t (s , b) -> m ()
hmset p@(RHSet (E _ eb _) (HSET e _)) k =
fixEmpty (ignore . unwrap . Redis.hmset (primKey p k)) (\(s,b) -> (e s , runIdentity $ eb b))
hdel :: (MonadRedis m , Traversable t) => RedisHSet a s b -> a -> t s -> m (ActionPerformed Deletion)
hdel p@(RHSet _ (HSET e _)) k =
fmap Deleted
. fixEmpty' (unwrap . Redis.hdel (primKey p k)) e
hsetnx :: MonadRedis m => RedisHSet a s b -> a -> s -> b -> m (ActionPerformed Creation)
hsetnx p@(RHSet (E _ eb _) (HSET e _)) k s =
unwrapCreatedBool
. Redis.hsetnx (primKey p k) (e s)
. runIdentity
. eb
hscan :: RedisHSet a s b -> a -> Cursor -> Redis (Maybe Cursor , [ (s , b) ])
hscan p@(RHSet (E _ _ d) (HSET _ d')) k =
let f (a,b) = (d' a , d (pure b)) in
unwrapCursor (fmap f)
. Redis.hscan (primKey p k)