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) . unwrap . Redis.hmget (primKey p k) . 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
(b:bs') <- Control.Monad.State.get
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 =
liftRedis
. ignore
. Redis.hmset (primKey p k)
. foldr (\x a -> (e (fst x) , runIdentity (eb (snd x))) : a) []
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)