|
| 1 | +{-# LANGUAGE DeriveAnyClass #-} |
| 2 | +{-# LANGUAGE DeriveGeneric #-} |
| 3 | +{-# LANGUAGE DerivingStrategies #-} |
| 4 | +{- HLINT ignore "Use unless" -} |
| 5 | + |
| 6 | +module Database.LSMTree.Internal.BlobFile ( |
| 7 | + BlobFile (..) |
| 8 | + , BlobSpan (..) |
| 9 | + , removeReference |
| 10 | + , newBlobFile |
| 11 | + , readBlobFile |
| 12 | + ) where |
| 13 | + |
| 14 | +import Control.DeepSeq (NFData (..)) |
| 15 | +import Control.Monad.Class.MonadThrow (MonadThrow, MonadMask) |
| 16 | +import Control.Monad.Primitive (PrimMonad) |
| 17 | +import Control.RefCount (RefCounter) |
| 18 | +import qualified Control.RefCount as RC |
| 19 | +import qualified Data.Primitive.ByteArray as P (newPinnedByteArray, |
| 20 | + unsafeFreezeByteArray) |
| 21 | +import Data.Word (Word32, Word64) |
| 22 | +import qualified Database.LSMTree.Internal.RawBytes as RB |
| 23 | +import Database.LSMTree.Internal.Serialise (SerialisedBlob (..)) |
| 24 | +import qualified System.FS.API as FS |
| 25 | +import System.FS.API (HasFS) |
| 26 | +import qualified System.FS.BlockIO.API as FS |
| 27 | + |
| 28 | +-- | An open handle to a file containing blobs. |
| 29 | +-- |
| 30 | +-- This is a reference counted object. Upon finalisation, the file is closed |
| 31 | +-- and deleted. |
| 32 | +-- |
| 33 | +data BlobFile m h = BlobFile { |
| 34 | + blobFileHandle :: {-# UNPACK #-} !(FS.Handle h), |
| 35 | + blobFileRefCounter :: {-# UNPACK #-} !(RefCounter m) |
| 36 | + } |
| 37 | + deriving stock (Show) |
| 38 | + |
| 39 | +instance NFData h => NFData (BlobFile m h) where |
| 40 | + rnf (BlobFile a b) = rnf a `seq` rnf b |
| 41 | + |
| 42 | +-- | The location of a blob inside a blob file. |
| 43 | +data BlobSpan = BlobSpan { |
| 44 | + blobSpanOffset :: {-# UNPACK #-} !Word64 |
| 45 | + , blobSpanSize :: {-# UNPACK #-} !Word32 |
| 46 | + } |
| 47 | + deriving stock (Show, Eq) |
| 48 | + |
| 49 | +instance NFData BlobSpan where |
| 50 | + rnf (BlobSpan a b) = rnf a `seq` rnf b |
| 51 | + |
| 52 | +removeReference :: |
| 53 | + (MonadMask m, PrimMonad m) |
| 54 | + => BlobFile m h |
| 55 | + -> m () |
| 56 | +removeReference BlobFile{blobFileRefCounter} = |
| 57 | + RC.removeReference blobFileRefCounter |
| 58 | + |
| 59 | +-- | Adopt an existing open file handle to make a 'BlobFile'. The file must at |
| 60 | +-- least be open for reading (but may or may not be open for writing). |
| 61 | +-- |
| 62 | +-- The finaliser will close and delete the file. |
| 63 | +-- |
| 64 | +newBlobFile :: |
| 65 | + PrimMonad m |
| 66 | + => HasFS m h |
| 67 | + -> FS.Handle h |
| 68 | + -> m (BlobFile m h) |
| 69 | +newBlobFile fs blobFileHandle = do |
| 70 | + let finaliser = do |
| 71 | + FS.hClose fs blobFileHandle |
| 72 | + FS.removeFile fs (FS.handlePath blobFileHandle) |
| 73 | + blobFileRefCounter <- RC.mkRefCounter1 (Just finaliser) |
| 74 | + return BlobFile { |
| 75 | + blobFileHandle, |
| 76 | + blobFileRefCounter |
| 77 | + } |
| 78 | + |
| 79 | +{-# SPECIALISE readBlobFile :: HasFS IO h -> BlobFile IO h -> BlobSpan -> IO SerialisedBlob #-} |
| 80 | +readBlobFile :: |
| 81 | + (MonadThrow m, PrimMonad m) |
| 82 | + => HasFS m h |
| 83 | + -> BlobFile m h |
| 84 | + -> BlobSpan |
| 85 | + -> m SerialisedBlob |
| 86 | +readBlobFile fs BlobFile {blobFileHandle} |
| 87 | + BlobSpan {blobSpanOffset, blobSpanSize} = do |
| 88 | + let off = FS.AbsOffset blobSpanOffset |
| 89 | + len :: Int |
| 90 | + len = fromIntegral blobSpanSize |
| 91 | + mba <- P.newPinnedByteArray len |
| 92 | + _ <- FS.hGetBufExactlyAt fs blobFileHandle mba 0 |
| 93 | + (fromIntegral len :: FS.ByteCount) off |
| 94 | + ba <- P.unsafeFreezeByteArray mba |
| 95 | + let !rb = RB.fromByteArray 0 len ba |
| 96 | + return (SerialisedBlob rb) |
0 commit comments