@@ -17,11 +17,6 @@ import Prelude hiding (take)
1717
1818import Control.Exception (assert )
1919import Control.Monad.ST.Strict (ST )
20- import Data.Primitive.PrimVar (PrimVar , newPrimVar , readPrimVar ,
21- writePrimVar )
22- import Data.Vector (force , take , unsafeFreeze )
23- import Data.Vector.Mutable (MVector )
24- import qualified Data.Vector.Mutable as Mutable (unsafeNew , write )
2520import qualified Data.Vector.Primitive as Primitive (Vector , length )
2621import Data.Word (Word16 , Word8 )
2722import Database.LSMTree.Internal.Chunk (Baler , Chunk , createBaler ,
@@ -33,33 +28,28 @@ import Database.LSMTree.Internal.IndexOrdinary
3328import Database.LSMTree.Internal.Serialise
3429 (SerialisedKey (SerialisedKey' ))
3530import Database.LSMTree.Internal.Vector (byteVectorFromPrim )
31+ import Database.LSMTree.Internal.Vector.Growing (GrowingVector )
32+ import qualified Database.LSMTree.Internal.Vector.Growing as Growing (append ,
33+ freeze , new )
3634
3735{-|
3836 A general-purpose fence pointer index under incremental construction.
3937
40- A value @IndexOrdinaryAcc buffer keyCountRef baler@ denotes a partially
41- constructed index with the following properties:
42-
43- * The keys that the index assigns to pages are stored as a prefix of the
44- mutable vector @buffer@.
45- * The reference @keyCountRef@ points to the number of those keys.
46- * The @baler@ object is used by the index for incremental output of the
47- serialised key list.
38+ A value @IndexOrdinaryAcc lastKeys baler@ denotes a partially constructed
39+ index that assigns keys to pages according to @lastKeys@ and uses @baler@
40+ for incremental output of the serialised key list.
4841-}
4942data IndexOrdinaryAcc s = IndexOrdinaryAcc
50- ! (MVector s SerialisedKey )
51- ! (PrimVar s Int )
43+ ! (GrowingVector s SerialisedKey )
5244 ! (Baler s )
5345
5446-- | Creates a new, initially empty, index.
55- new :: Int -- ^ Maximum number of keys
47+ new :: Int -- ^ Initial size of the key buffer
5648 -> Int -- ^ Minimum chunk size in bytes
5749 -> ST s (IndexOrdinaryAcc s ) -- ^ Construction of the index
58- new maxKeyCount minChunkSize = assert (maxKeyCount >= 0 ) $
59- IndexOrdinaryAcc <$>
60- Mutable. unsafeNew maxKeyCount <*>
61- newPrimVar 0 <*>
62- createBaler minChunkSize
50+ new initialKeyBufferSize minChunkSize = IndexOrdinaryAcc <$>
51+ Growing. new initialKeyBufferSize <*>
52+ createBaler minChunkSize
6353
6454{-|
6555 Appends keys to the key list of an index and outputs newly available chunks
@@ -69,26 +59,18 @@ new maxKeyCount minChunkSize = assert (maxKeyCount >= 0) $
6959 word may result in a corrupted serialised key list.
7060-}
7161append :: Append -> IndexOrdinaryAcc s -> ST s (Maybe Chunk )
72- append instruction (IndexOrdinaryAcc buffer keyCountRef baler)
62+ append instruction (IndexOrdinaryAcc lastKeys baler)
7363 = case instruction of
7464 AppendSinglePage _ key -> do
75- keyCount <- readPrimVar keyCountRef
76- Mutable. write buffer keyCount key
77- writePrimVar keyCountRef (succ keyCount)
65+ Growing. append lastKeys 1 key
7866 feedBaler (keyListElem key) baler
7967 AppendMultiPage key overflowPageCount -> do
80- keyCount <- readPrimVar keyCountRef
8168 let
8269
8370 pageCount :: Int
8471 ! pageCount = succ (fromIntegral overflowPageCount)
8572
86- keyCount' :: Int
87- ! keyCount' = keyCount + pageCount
88-
89- mapM_ (flip (Mutable. write buffer) key)
90- [keyCount .. pred keyCount']
91- writePrimVar keyCountRef keyCount'
73+ Growing. append lastKeys pageCount key
9274 feedBaler (concat (replicate pageCount (keyListElem key))) baler
9375 where
9476
@@ -112,8 +94,7 @@ append instruction (IndexOrdinaryAcc buffer keyCountRef baler)
11294 @index@ is not used afterwards.
11395-}
11496unsafeEnd :: IndexOrdinaryAcc s -> ST s (Maybe Chunk , IndexOrdinary )
115- unsafeEnd (IndexOrdinaryAcc buffer keyCountRef baler) = do
116- keyCount <- readPrimVar keyCountRef
117- keys <- force <$> take keyCount <$> unsafeFreeze buffer
97+ unsafeEnd (IndexOrdinaryAcc lastKeys baler) = do
98+ keys <- Growing. freeze lastKeys
11899 remnant <- unsafeEndBaler baler
119100 return (remnant, IndexOrdinary keys)
0 commit comments