module HLRDB.Structures.List
(
HLRDB.Structures.List.lrange
, lprepend
, lappend
, HLRDB.Structures.List.lpop
, HLRDB.Structures.List.lrem
, HLRDB.Structures.List.llen
, HLRDB.Structures.List.rpop
, HLRDB.Structures.List.rpoplpush
, HLRDB.Structures.List.blpop
, HLRDB.Structures.List.brpop
, HLRDB.Structures.List.brpoplpush
) where
import Database.Redis as Redis
import HLRDB.Primitives.Redis
import HLRDB.Internal
import Data.Maybe (fromJust)
lrange :: MonadRedis m => RedisList a b -> a -> Integer -> Integer -> m [ b ]
lrange p@(RList (E _ _ d) _) k i =
(fmap . fmap) (d . pure)
. unwrap
. Redis.lrange (primKey p k) i
lappend :: (MonadRedis m , Traversable t) => RedisList a b -> a -> t b -> m ()
lappend = addItem True
lprepend :: (MonadRedis m , Traversable t) => RedisList a b -> a -> t b -> m ()
lprepend = addItem False
addItem :: (MonadRedis m , Traversable t) => Bool -> RedisList a b -> a -> t b -> m ()
addItem toTheEnd p@(RList (E _ e _) trimScheme) k bs' =
let bs = foldr (:) [] bs' in
case bs of
[] -> pure ()
_ -> do
let method = if toTheEnd then rpush else lpush
let key = primKey p k
itemCount <- unwrap $ method key (fmap (runIdentity . e) bs)
case trimScheme of
Just (TrimScheme maxItemCount prob) -> fmap (const ()) $ liftRedis $ probIO prob $
if itemCount > maxItemCount
then ignore $ if toTheEnd
then unwrap $ ltrim key (fromIntegral $ length bs) (-1)
else unwrap $ ltrim key 0 (maxItemCount - 1)
else pure ()
Nothing -> pure ()
lrem :: MonadRedis m => RedisList a b -> a -> b -> m ()
lrem p@(RList (E _ e _) _) k =
ignore
. unwrap
. Redis.lrem (primKey p k) 0
. runIdentity
. e
llen :: MonadRedis m => RedisList a b -> a -> m Integer
llen p =
unwrap
. Redis.llen
. primKey p
lpop :: MonadRedis m => RedisList a b -> a -> m (Maybe b)
lpop p@(RList (E _ _ d) _) =
(fmap . fmap) (d . pure)
. unwrap
. Redis.lpop
. primKey p
rpop :: MonadRedis m => RedisList a b -> a -> m (Maybe b)
rpop p@(RList (E _ _ d) _) =
(fmap . fmap) (d . pure)
. unwrap
. Redis.rpop
. primKey p
rpoplpush :: MonadRedis m => RedisList a b -> a -> a -> m (Maybe b)
rpoplpush p@(RList (E _ _ d) _) s =
(fmap . fmap) (d . pure)
. unwrap
. Redis.rpoplpush (primKey p s)
. primKey p
brpoplpush :: MonadRedis m => RedisList a b -> a -> a -> Integer -> m (Maybe b)
brpoplpush p@(RList (E _ _ d) _) s e =
(fmap . fmap) (d . pure)
. unwrap
. Redis.brpoplpush (primKey p s) (primKey p e)
blpop :: (MonadRedis m , Traversable t) => RedisList a b -> t a -> Integer -> m (Maybe (a , b))
blpop p@(RList (E e _ d) _) ts t = case foldr (\x a -> (e x , x) : a) [] ts of
[] -> pure Nothing
xs ->
let f (x , b) = (fromJust (lookup x xs) , (d . pure) b) in
(fmap . fmap . fmap) f unwrap $ Redis.blpop (primKey p . snd <$> xs) t
brpop :: (MonadRedis m , Traversable t) => RedisList a b -> t a -> Integer -> m (Maybe (a , b))
brpop p@(RList (E e _ d) _) ts t = case foldr (\x a -> (e x , x) : a) [] ts of
[] -> pure Nothing
xs ->
let f (x , b) = (fromJust (lookup x xs) , (d . pure) b) in
(fmap . fmap . fmap) f unwrap $ Redis.brpop (primKey p . snd <$> xs) t