instance Hashable Key for bloomfilter

This commit is contained in:
Joey Hess 2015-06-16 18:37:41 -04:00
parent 8b74aec3ea
commit a0a8127956
4 changed files with 13 additions and 8 deletions

View file

@ -40,14 +40,14 @@ bloomBitsHashes = do
- Once the action completes, the mutable filter is frozen
- for later use.
-}
genBloomFilter :: Hashable t => (v -> t) -> ((v -> Annex ()) -> Annex b) -> Annex (Bloom t)
genBloomFilter convert populate = do
genBloomFilter :: Hashable v => ((v -> Annex ()) -> Annex b) -> Annex (Bloom v)
genBloomFilter populate = do
(numbits, numhashes) <- bloomBitsHashes
bloom <- lift $ newMB (cheapHashes numhashes) numbits
_ <- populate $ \v -> lift $ insertMB bloom (convert v)
_ <- populate $ \v -> lift $ insertMB bloom v
lift $ unsafeFreezeMB bloom
where
lift = liftIO . stToIO
bloomFilter :: Hashable t => (v -> t) -> [v] -> Bloom t -> [v]
bloomFilter convert l bloom = filter (\k -> convert k `notElemB` bloom) l
bloomFilter :: Hashable v => [v] -> Bloom v -> [v]
bloomFilter l bloom = filter (\v -> v `notElemB` bloom) l

View file

@ -167,7 +167,7 @@ excludeReferenced :: RefSpec -> [Key] -> Annex [Key]
excludeReferenced refspec ks = runfilter firstlevel ks >>= runfilter secondlevel
where
runfilter _ [] = return [] -- optimisation
runfilter a l = bloomFilter show l <$> genBloomFilter show a
runfilter a l = bloomFilter l <$> genBloomFilter a
firstlevel = withKeysReferencedM
secondlevel = withKeysReferencedInGit refspec

View file

@ -26,6 +26,7 @@ import System.Posix.Types
import Common
import Utility.QuickCheck
import Utility.Bloom
{- A Key has a unique name, which is derived from a particular backend,
- and may contain other optional metadata. -}
@ -130,6 +131,10 @@ instance Arbitrary Key where
<*> ((abs <$>) <$> arbitrary) -- chunksize cannot be negative
<*> ((succ . abs <$>) <$> arbitrary) -- chunknum cannot be 0 or negative
instance Hashable Key where
hashIO32 = hashIO32 . show
hashIO64 = hashIO64 . show
prop_idempotent_key_encode :: Key -> Bool
prop_idempotent_key_encode k = Just k == (file2key . key2file) k

View file

@ -10,7 +10,7 @@
module Utility.Bloom (
Bloom,
safeSuggestSizing,
Hashable,
Hashable(..),
cheapHashes,
notElemB,
@ -26,7 +26,7 @@ import qualified Data.BloomFilter as Bloom
import qualified Data.BloomFilter as Bloom
#endif
import Data.BloomFilter.Easy (safeSuggestSizing, Bloom)
import Data.BloomFilter.Hash (Hashable, cheapHashes)
import Data.BloomFilter.Hash (Hashable(..), cheapHashes)
import Control.Monad.ST (ST)
#if MIN_VERSION_bloomfilter(2,0,0)