66--
77module Database.LSMTree.Internal.RunAcc (
88 RunAcc (.. )
9- , RunBloomFilterAlloc (.. )
109 , new
1110 , unsafeFinalise
1211 -- * Adding key\/op pairs
@@ -29,6 +28,9 @@ module Database.LSMTree.Internal.RunAcc (
2928 , addLargeKeyOp
3029 , addLargeSerialisedKeyOp
3130 , PageAcc. entryWouldFitInPage
31+ -- * Bloom filter allocation
32+ , RunBloomFilterAlloc (.. )
33+ -- ** Exposed for testing
3234 , numHashFunctions
3335 ) where
3436
@@ -76,17 +78,6 @@ data RunAcc s = RunAcc {
7678 , entryCount :: ! (PrimVar s Int )
7779 }
7880
79- -- | See 'Database.LSMTree.Internal.BloomFilterAlloc'
80- data RunBloomFilterAlloc =
81- -- | Bits per element in a filter
82- RunAllocFixed ! Word64
83- | RunAllocRequestFPR ! Double
84- deriving stock (Show , Eq )
85-
86- instance NFData RunBloomFilterAlloc where
87- rnf (RunAllocFixed a) = rnf a
88- rnf (RunAllocRequestFPR a) = rnf a
89-
9081-- | @'new' nentries@ starts an incremental run construction.
9182--
9283-- @nentries@ should be an upper bound on the expected number of entries in the
@@ -96,15 +87,8 @@ new ::
9687 -> RunBloomFilterAlloc
9788 -> IndexType
9889 -> ST s (RunAcc s )
99- new (NumEntries nentries) alloc indexType = do
100- mbloom <- case alloc of
101- RunAllocFixed ! bitsPerEntry ->
102- let ! nbits = fromIntegral bitsPerEntry * fromIntegral nentries
103- in MBloom. new
104- (fromIntegralChecked $ numHashFunctions nbits (fromIntegralChecked nentries))
105- (fromIntegralChecked nbits)
106- RunAllocRequestFPR ! fpr ->
107- Bloom.Easy. easyNew fpr nentries
90+ new nentries alloc indexType = do
91+ mbloom <- newMBloom nentries alloc
10892 mindex <- Index. newWithDefaults indexType
10993 mpageacc <- PageAcc. newPageAcc
11094 entryCount <- newPrimVar 0
@@ -333,6 +317,31 @@ selectPagesAndChunks mpagemchunkPre page chunks =
333317 Just (pagePre, Nothing ) -> ([pagePre, page], chunks)
334318 Just (pagePre, Just chunkPre) -> ([pagePre, page], chunkPre: chunks)
335319
320+ {- ------------------------------------------------------------------------------
321+ Bloom filter allocation
322+ -------------------------------------------------------------------------------}
323+
324+ -- | See 'Database.LSMTree.Internal.Config.BloomFilterAlloc'
325+ data RunBloomFilterAlloc =
326+ -- | Bits per element in a filter
327+ RunAllocFixed ! Word64
328+ | RunAllocRequestFPR ! Double
329+ deriving stock (Show , Eq )
330+
331+ instance NFData RunBloomFilterAlloc where
332+ rnf (RunAllocFixed a) = rnf a
333+ rnf (RunAllocRequestFPR a) = rnf a
334+
335+ newMBloom :: NumEntries -> RunBloomFilterAlloc -> ST s (MBloom s a )
336+ newMBloom (NumEntries nentries) = \ case
337+ RunAllocFixed ! bitsPerEntry ->
338+ let ! nbits = fromIntegral bitsPerEntry * fromIntegral nentries
339+ in MBloom. new
340+ (fromIntegralChecked $ numHashFunctions nbits (fromIntegralChecked nentries))
341+ (fromIntegralChecked nbits)
342+ RunAllocRequestFPR ! fpr ->
343+ Bloom.Easy. easyNew fpr nentries
344+
336345-- | Computes the optimal number of hash functions that minimises the false
337346-- positive rate for a bloom filter.
338347--
0 commit comments