module HLRDB.Structures.Basic where
import Database.Redis as Redis
import HLRDB.Primitives.Aggregate
import HLRDB.Primitives.Redis
import HLRDB.Internal
import Data.ByteString.Char8 (pack)
import qualified Data.HashMap.Strict as HM
get :: MonadRedis m => RedisStructure (BASIC w) a b -> a -> m b
get (RKeyValue (E k _ d)) a = liftRedis $ Redis.get (k a) >>= \case
Left e -> fail (show e)
Right r -> pure (d r)
get (RKeyValueInteger k _ d) a = liftRedis $ Redis.get (k a) >>= \case
Left e -> fail (show e)
Right r -> pure $ d . fromIntegral $ decodeMInteger r
liftq :: RedisStructure (BASIC w) a b -> a ⟿ b
liftq (RKeyValue (E k _ d)) = T $ \f -> fmap d . f . k
liftq (RKeyValueInteger k _ d) = T $ \f -> fmap (d . fromIntegral . decodeMInteger) . f . k
mget :: MonadRedis m => a ⟿ b -> a -> m b
mget = runT (liftRedis . mget')
where
mget' [] = pure []
mget' xs = Redis.mget xs >>= \case
Left e -> fail (show e)
Right vs -> pure vs
set :: MonadRedis m => RedisStructure (BASIC w) a b -> a -> b -> m ()
set (RKeyValue (E k e _)) a b = liftRedis $ case e b of
Just bs -> ignore $ Redis.set (k a) bs
Nothing -> ignore $ del [ k a ]
set (RKeyValueInteger k e _) a i = liftRedis $ ignore $ Redis.set (k a) (pack $ show (e i))
set' :: MonadRedis m => RedisBasic a (Maybe b) -> a -> b -> m ()
set' (RKeyValue (E k e _)) a b = liftRedis $ case e (Just b) of
Just bs -> ignore $ Redis.set (k a) bs
Nothing -> ignore $ del [ k a ]
liftqs :: RedisStructure (BASIC w) a b -> (a , b) -> MSET
liftqs (RKeyValue (E k e _)) (a , b) = MSET $ (<>) [ (k a , e b) ]
liftqs (RKeyValueInteger k e _) (a , b) = MSET $ (<>) [ (k a , Just $ pack (show (e b))) ]
mset :: MonadRedis m => MSET -> m ()
mset = go . flip runMSET []
where
go xs = case (splitWith f . HM.toList . HM.fromList) xs of
(as , bs) -> mdel' as >> mset' bs >> pure ()
where
f (x , Nothing) = Left x
f (x , Just y) = Right (x , y)
mdel' [] = pure 0
mdel' xs = unwrap $ liftRedis $ Redis.del xs
mset' [] = pure undefined
mset' xs = unwrap $ liftRedis $ Redis.mset xs
setex :: MonadRedis m => RedisStructure (BASIC w) a b -> a -> Integer -> b -> m ()
setex (RKeyValue (E k e _)) a t b = liftRedis $ case e b of
Just bs -> ignore $ Redis.setex (k a) t bs
Nothing -> ignore $ del [ k a ]
setex (RKeyValueInteger k e _) a t i = liftRedis $ ignore $ Redis.setex (k a) t (pack $ show (e i))
incr :: MonadRedis m => RedisIntegral a b -> a -> m b
incr (RKeyValueInteger p _ d) =
fmap d
. unwrap
. Redis.incr
. p
incrby :: MonadRedis m => RedisIntegral a b -> a -> b -> m b
incrby (RKeyValueInteger p e d) k =
fmap d
. unwrap
. Redis.incrby (p k)
. e
decr :: MonadRedis m => RedisIntegral a b -> a -> m b
decr (RKeyValueInteger p _ d) =
fmap d
. unwrap
. Redis.decr
. p
decrby :: MonadRedis m => RedisIntegral a b -> a -> b -> m b
decrby (RKeyValueInteger p e d) k =
fmap d
. unwrap
. Redis.decrby (p k)
. e