From a0a8127956d891261a51fe9371d50b4f0653dc08 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 16 Jun 2015 18:37:41 -0400 Subject: [PATCH] instance Hashable Key for bloomfilter --- Annex/BloomFilter.hs | 10 +++++----- Command/Unused.hs | 2 +- Types/Key.hs | 5 +++++ Utility/Bloom.hs | 4 ++-- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Annex/BloomFilter.hs b/Annex/BloomFilter.hs index 3dcd8140bd..3ac81fa58c 100644 --- a/Annex/BloomFilter.hs +++ b/Annex/BloomFilter.hs @@ -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 diff --git a/Command/Unused.hs b/Command/Unused.hs index 82a6052900..7bf252243a 100644 --- a/Command/Unused.hs +++ b/Command/Unused.hs @@ -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 diff --git a/Types/Key.hs b/Types/Key.hs index 553fd8f3de..7f9f514c72 100644 --- a/Types/Key.hs +++ b/Types/Key.hs @@ -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 diff --git a/Utility/Bloom.hs b/Utility/Bloom.hs index 95ade6d323..9076abddb5 100644 --- a/Utility/Bloom.hs +++ b/Utility/Bloom.hs @@ -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)