Skip to content

Commit

Permalink
Update to hedis 0.5. Generalize types.
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Dorofeev <aka.spin@gmail.com>
  • Loading branch information
akaspin committed May 12, 2012
1 parent 43599f0 commit 99885f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
6 changes: 3 additions & 3 deletions hedis-tags.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: hedis-tags
version: 0.1.1
version: 0.2.0
cabal-version: >= 1.8
build-type: Simple
stability: Experimental
Expand All @@ -20,7 +20,7 @@ library
hs-source-dirs: src
build-depends: base >= 4 && < 5,
bytestring >= 0.9 && < 0.10,
hedis >= 0.4 && < 0.5
hedis >= 0.5 && < 0.6
ghc-options: -Wall
exposed-modules: Database.Redis.Tags

Expand All @@ -40,7 +40,7 @@ test-suite test

-- from lib
bytestring >= 0.9 && < 0.10,
hedis >= 0.4 && < 0.5,
hedis >= 0.5 && < 0.6,

lifted-base,
transformers
Expand Down
33 changes: 18 additions & 15 deletions src/Database/Redis/Tags.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedStrings, FlexibleContexts #-}

-- | Hedis tags helper.

Expand All @@ -15,7 +15,7 @@ import qualified Data.ByteString as B
import qualified Database.Redis as R
import Data.Either (rights)

import Control.Monad (void, filterM)
import Control.Monad (filterM)

-- | Mark keys with tags. Keys may be absent. All tags named in next manner:
--
Expand All @@ -25,16 +25,17 @@ import Control.Monad (void, filterM)
-- each other.
--
-- /Time complexity/ @O(K+T)@ where @K@ and @T@ is number of keys and tags.
markTags ::
markTags :: R.RedisCtx m f =>
[B.ByteString] -- ^ Keys.
-> B.ByteString -- ^ Prefix for tags.
-> [B.ByteString] -- ^ Tags. To make list of nested tags use 'nestTags'.
-> R.Redis ()
-> m ()
markTags [] _ _ = return ()
markTags _ _ [] = return ()
markTags keys pref tags =
let pt = map (tagName pref) tags in
void $ mapM (`R.sadd` keys) pt
markTags keys pref tags = do
let pt = map (tagName pref) tags
_ <- mapM (`R.sadd` keys) pt
return ()

-- | Purge tagged keys and tags.
--
Expand All @@ -45,17 +46,18 @@ markTags keys pref tags =
--
-- /Time complexity/ @~O(T+2K)@ where @T@ is number tags and @K@ is number
-- of tagged keys.
purgeTags ::
purgeTags :: R.RedisCtx m (Either a) =>
B.ByteString -- ^ Prefix for tags.
-> [B.ByteString] -- ^ Tags. To make list of nested tags use 'nestTags'.
-> R.Redis ()
-> m ()
purgeTags _ [] = return ()
purgeTags pref tags = do
let pt = map (tagName pref) tags
a <- R.sunion pt
let keys = head $ rights [a]
void $ R.del pt
void $ R.del keys
_ <- R.del pt
_ <- R.del keys
return ()

-- | Helper for create list of nested tags.
--
Expand All @@ -71,13 +73,14 @@ nestTags = scanl1 (\a b -> B.append a $ B.append ":" b)
-- * Remove empty tags.
--
-- This operation take huge time complexity. Use it only for maintenance.
reconsileTags ::
reconsileTags :: R.RedisCtx m (Either a) =>
B.ByteString -- ^ Tags prefix.
-> R.Redis ()
-> m ()
reconsileTags pref = do
allTags <- R.keys $ tagName pref "*"
needRem <- filterM reconsileTag $ head $ rights [allTags]
void $ R.del needRem
_ <- R.del needRem
return ()
where
reconsileTag t = do
keys <- R.smembers t
Expand All @@ -86,7 +89,7 @@ reconsileTags pref = do
if needRem == keys'
then return True
else do
void $ R.srem t needRem
_ <- R.srem t needRem
return False
checkKey k = do
ex <- R.exists k
Expand Down

0 comments on commit 99885f7

Please sign in to comment.