@@ -8,7 +8,8 @@ module Database.LSMTree.Internal.IndexOrdinaryAcc
88(
99 IndexOrdinaryAcc ,
1010 new,
11- append,
11+ appendSingle,
12+ appendMulti,
1213 unsafeEnd
1314)
1415where
@@ -17,12 +18,11 @@ import Prelude hiding (take)
1718
1819import Control.Exception (assert )
1920import Control.Monad.ST.Strict (ST )
21+ import Data.Maybe (maybeToList )
2022import qualified Data.Vector.Primitive as Primitive (Vector , length )
21- import Data.Word (Word16 , Word8 )
23+ import Data.Word (Word16 , Word32 , Word8 )
2224import Database.LSMTree.Internal.Chunk (Baler , Chunk , createBaler ,
2325 feedBaler , unsafeEndBaler )
24- import Database.LSMTree.Internal.IndexCompactAcc
25- (Append (AppendMultiPage , AppendSinglePage ))
2626import Database.LSMTree.Internal.IndexOrdinary
2727 (IndexOrdinary (IndexOrdinary ))
2828import Database.LSMTree.Internal.Serialise
@@ -51,41 +51,58 @@ new initialKeyBufferSize minChunkSize = IndexOrdinaryAcc <$>
5151 Growing. new initialKeyBufferSize <*>
5252 createBaler minChunkSize
5353
54+ -- Yields the serialisation of an element of a key list.
55+ keyListElem :: SerialisedKey -> [Primitive. Vector Word8 ]
56+ keyListElem (SerialisedKey' keyBytes) = [keySizeBytes, keyBytes] where
57+
58+ keySize :: Int
59+ ! keySize = Primitive. length keyBytes
60+
61+ keySizeAsWord16 :: Word16
62+ ! keySizeAsWord16 = assert (keySize <= fromIntegral (maxBound :: Word16 )) $
63+ fromIntegral keySize
64+
65+ keySizeBytes :: Primitive. Vector Word8
66+ ! keySizeBytes = byteVectorFromPrim keySizeAsWord16
67+
5468{-|
55- Appends keys to the key list of an index and outputs newly available chunks
56- of the serialised key list.
69+ Adds information about a single page that fully comprises one or more
70+ key–value pairs to an index and outputs newly available chunks of the
71+ serialised key list.
5772
58- __Warning:__ Appending keys whose length cannot be represented by a 16-bit
59- word may result in a corrupted serialised key list.
73+ __Warning:__ Using keys whose length cannot be represented by a 16-bit word
74+ may result in a corrupted serialised key list.
6075-}
61- append :: Append -> IndexOrdinaryAcc s -> ST s (Maybe Chunk )
62- append instruction (IndexOrdinaryAcc lastKeys baler)
63- = case instruction of
64- AppendSinglePage _ key -> do
65- Growing. append lastKeys 1 key
66- feedBaler (keyListElem key) baler
67- AppendMultiPage key overflowPageCount -> do
68- let
69-
70- pageCount :: Int
71- ! pageCount = succ (fromIntegral overflowPageCount)
72-
73- Growing. append lastKeys pageCount key
74- feedBaler (concat (replicate pageCount (keyListElem key))) baler
75- where
76+ appendSingle :: (SerialisedKey , SerialisedKey )
77+ -> IndexOrdinaryAcc s
78+ -> ST s (Maybe Chunk )
79+ appendSingle (_, key) (IndexOrdinaryAcc lastKeys baler)
80+ = do
81+ Growing. append lastKeys 1 key
82+ feedBaler (keyListElem key) baler
7683
77- keyListElem :: SerialisedKey -> [Primitive. Vector Word8 ]
78- keyListElem (SerialisedKey' keyBytes) = [keySizeBytes, keyBytes] where
84+ {-|
85+ Adds information about multiple pages that together comprise a single
86+ key–value pair to an index and outputs newly available chunks of the
87+ serialised key list.
7988
80- keySize :: Int
81- ! keySize = Primitive. length keyBytes
89+ __Warning:__ Using keys whose length cannot be represented by a 16-bit word
90+ may result in a corrupted serialised key list.
91+ -}
92+ appendMulti :: (SerialisedKey , Word32 )
93+ -> IndexOrdinaryAcc s
94+ -> ST s [Chunk ]
95+ appendMulti (key, overflowPageCount) (IndexOrdinaryAcc lastKeys baler)
96+ = do
97+ Growing. append lastKeys pageCount key
98+ maybeToList <$> feedBaler keyListElems baler
99+ where
82100
83- keySizeAsWord16 :: Word16
84- ! keySizeAsWord16 = assert (keySize <= fromIntegral (maxBound :: Word16 )) $
85- fromIntegral keySize
101+ pageCount :: Int
102+ ! pageCount = succ (fromIntegral overflowPageCount)
86103
87- keySizeBytes :: Primitive. Vector Word8
88- ! keySizeBytes = byteVectorFromPrim keySizeAsWord16
104+ keyListElems :: [ Primitive. Vector Word8 ]
105+ keyListElems = concat ( replicate pageCount (keyListElem key))
89106
90107{-|
91108 Returns the constructed index, along with a final chunk in case the
0 commit comments