|
| 1 | +{- HLINT ignore "Avoid restricted alias" -} |
| 2 | + |
| 3 | +{-| |
| 4 | + Incremental construction functionality for the general-purpose fence pointer |
| 5 | + index. |
| 6 | +-} |
| 7 | +module Database.LSMTree.Internal.IndexOrdinaryAcc |
| 8 | +( |
| 9 | + IndexOrdinaryAcc, |
| 10 | + new, |
| 11 | + append, |
| 12 | + unsafeEnd |
| 13 | +) |
| 14 | +where |
| 15 | + |
| 16 | +import Prelude hiding (take) |
| 17 | + |
| 18 | +import Control.Exception (assert) |
| 19 | +import Control.Monad.ST.Strict (ST, runST) |
| 20 | +import Data.Primitive.ByteArray (newByteArray, unsafeFreezeByteArray, |
| 21 | + writeByteArray) |
| 22 | +import Data.STRef.Strict (STRef, newSTRef, readSTRef, writeSTRef) |
| 23 | +import Data.Vector (force, take, unsafeFreeze) |
| 24 | +import Data.Vector.Mutable (MVector) |
| 25 | +import qualified Data.Vector.Mutable as Mutable (unsafeNew, write) |
| 26 | +import qualified Data.Vector.Primitive as Primitive (Vector, length) |
| 27 | +import Data.Word (Word16, Word8) |
| 28 | +import Database.LSMTree.Internal.Chunk (Baler, Chunk, createBaler, |
| 29 | + feedBaler, unsafeEndBaler) |
| 30 | +import Database.LSMTree.Internal.IndexCompactAcc |
| 31 | + (Append (AppendMultiPage, AppendSinglePage)) |
| 32 | +import Database.LSMTree.Internal.IndexOrdinary |
| 33 | + (IndexOrdinary (IndexOrdinary)) |
| 34 | +import Database.LSMTree.Internal.Serialise |
| 35 | + (SerialisedKey (SerialisedKey')) |
| 36 | +import Database.LSMTree.Internal.Vector (mkPrimVector) |
| 37 | + |
| 38 | +{-| |
| 39 | + A general-purpose fence pointer index under incremental construction. |
| 40 | +
|
| 41 | + A value @IndexOrdinaryAcc buffer keyCountRef baler@ denotes a partially |
| 42 | + constructed index with the following properties: |
| 43 | +
|
| 44 | + * The keys that the index assigns to pages are stored as a prefix of the |
| 45 | + mutable vector @buffer@. |
| 46 | + * The reference @keyCountRef@ points to the number of those keys. |
| 47 | + * The @baler@ object is used by the index for incremental output of the |
| 48 | + serialised key list. |
| 49 | +-} |
| 50 | +data IndexOrdinaryAcc s = IndexOrdinaryAcc |
| 51 | + !(MVector s SerialisedKey) |
| 52 | + !(STRef s Int) |
| 53 | + !(Baler s) |
| 54 | + |
| 55 | +-- | Creates a new, initially empty, index. |
| 56 | +new :: Int -- ^ Maximum number of keys |
| 57 | + -> Int -- ^ Minimum chunk size in bytes |
| 58 | + -> ST s (IndexOrdinaryAcc s) -- ^ Construction of the index |
| 59 | +new maxKeyCount minChunkSize = assert (maxKeyCount >= 0) $ |
| 60 | + IndexOrdinaryAcc <$> |
| 61 | + Mutable.unsafeNew maxKeyCount <*> |
| 62 | + newSTRef 0 <*> |
| 63 | + createBaler minChunkSize |
| 64 | + |
| 65 | +{-| |
| 66 | + Appends keys to the key list of an index and outputs newly available chunks |
| 67 | + of the serialised key list. |
| 68 | +
|
| 69 | + __Warning:__ Appending keys whose length cannot be represented by a 16-bit |
| 70 | + word may result in a corrupted serialised key list. |
| 71 | +-} |
| 72 | +append :: Append -> IndexOrdinaryAcc s -> ST s (Maybe Chunk) |
| 73 | +append instruction (IndexOrdinaryAcc buffer keyCountRef baler) |
| 74 | + = case instruction of |
| 75 | + AppendSinglePage _ key -> do |
| 76 | + keyCount <- readSTRef keyCountRef |
| 77 | + Mutable.write buffer keyCount key |
| 78 | + writeSTRef keyCountRef $! succ keyCount |
| 79 | + feedBaler (keyListElem key) baler |
| 80 | + AppendMultiPage key overflowPageCount -> do |
| 81 | + keyCount <- readSTRef keyCountRef |
| 82 | + let |
| 83 | + |
| 84 | + pageCount :: Int |
| 85 | + !pageCount = succ (fromIntegral overflowPageCount) |
| 86 | + |
| 87 | + keyCount' :: Int |
| 88 | + !keyCount' = keyCount + pageCount |
| 89 | + |
| 90 | + mapM_ (flip (Mutable.write buffer) key) |
| 91 | + [keyCount .. pred keyCount'] |
| 92 | + writeSTRef keyCountRef $! keyCount' |
| 93 | + feedBaler (concat (replicate pageCount (keyListElem key))) baler |
| 94 | + where |
| 95 | + |
| 96 | + keyListElem :: SerialisedKey -> [Primitive.Vector Word8] |
| 97 | + keyListElem (SerialisedKey' keyBytes) = [keySizeBytes, keyBytes] where |
| 98 | + |
| 99 | + keySize :: Int |
| 100 | + !keySize = Primitive.length keyBytes |
| 101 | + |
| 102 | + keySizeAsWord16 :: Word16 |
| 103 | + !keySizeAsWord16 = assert (keySize <= fromIntegral (maxBound :: Word16)) $ |
| 104 | + fromIntegral keySize |
| 105 | + |
| 106 | + keySizeBytes :: Primitive.Vector Word8 |
| 107 | + !keySizeBytes = mkPrimVector 0 2 $ |
| 108 | + runST $ do |
| 109 | + rep <- newByteArray 2 |
| 110 | + writeByteArray rep 0 keySizeAsWord16 |
| 111 | + unsafeFreezeByteArray rep |
| 112 | + |
| 113 | +{-| |
| 114 | + Returns the constructed index, along with a final chunk in case the |
| 115 | + serialised key list has not been fully output yet, thereby invalidating the |
| 116 | + index under construction. Executing @unsafeEnd index@ is only safe when |
| 117 | + @index@ is not used afterwards. |
| 118 | +-} |
| 119 | +unsafeEnd :: IndexOrdinaryAcc s -> ST s (Maybe Chunk, IndexOrdinary) |
| 120 | +unsafeEnd (IndexOrdinaryAcc buffer keyCountRef baler) = do |
| 121 | + keyCount <- readSTRef keyCountRef |
| 122 | + keys <- force <$> take keyCount <$> unsafeFreeze buffer |
| 123 | + remnant <- unsafeEndBaler baler |
| 124 | + return (remnant, IndexOrdinary keys) |
0 commit comments