@@ -41,8 +41,9 @@ import Data.Primitive.ByteArray as P
4141import Data.Primitive.PrimVar as P
4242import qualified Data.Vector.Primitive as VP
4343import Data.Word (Word64 )
44- import Database.LSMTree.Internal.BlobRef (BlobRef (.. ), BlobSpan (.. ))
45- import Database.LSMTree.Internal.RawBytes as RB
44+ import Database.LSMTree.Internal.BlobFile hiding (removeReference )
45+ import qualified Database.LSMTree.Internal.BlobFile as BlobFile
46+ import Database.LSMTree.Internal.BlobRef (BlobRef (.. ))
4647import Database.LSMTree.Internal.Serialise
4748import qualified System.FS.API as FS
4849import System.FS.API (HasFS )
@@ -102,17 +103,14 @@ import qualified System.Posix.Types as FS (ByteCount)
102103--
103104data WriteBufferBlobs m h =
104105 WriteBufferBlobs {
105- blobFileHandle :: {-# UNPACK #-} ! (FS. Handle h )
106+ blobFile :: ! (BlobFile m h )
106107
107108 -- | The manually tracked file pointer.
108- , blobFilePointer :: ! (FilePointer m )
109-
110- -- | The reference counter for the blob file.
111- , blobFileRefCounter :: {-# UNPACK #-} ! (RC. RefCounter m )
109+ , blobFilePointer :: ! (FilePointer m )
112110 }
113111
114112instance NFData h => NFData (WriteBufferBlobs m h ) where
115- rnf (WriteBufferBlobs a b c ) = rnf a `seq` rnf b `seq` rnf c
113+ rnf (WriteBufferBlobs a b) = rnf a `seq` rnf b
116114
117115{-# SPECIALISE new :: HasFS IO h -> FS.FsPath -> IO (WriteBufferBlobs IO h) #-}
118116new :: PrimMonad m
@@ -124,42 +122,32 @@ new fs blobFileName = do
124122 -- we can also be asked to retrieve blobs at any time.
125123 blobFileHandle <- FS. hOpen fs blobFileName (FS. ReadWriteMode FS. MustBeNew )
126124 blobFilePointer <- newFilePointer
127- blobFileRefCounter <- RC. mkRefCounter1 ( Just (finaliser fs blobFileHandle))
125+ blobFile <- newBlobFile fs blobFileHandle
128126 return WriteBufferBlobs {
129- blobFileHandle,
130- blobFilePointer,
131- blobFileRefCounter
127+ blobFile,
128+ blobFilePointer
132129 }
133130
134- {-# SPECIALISE finaliser :: HasFS IO h -> FS.Handle h -> IO () #-}
135- finaliser :: PrimMonad m
136- => HasFS m h
137- -> FS. Handle h
138- -> m ()
139- finaliser fs h = do
140- FS. hClose fs h
141- FS. removeFile fs (FS. handlePath h)
142-
143131{-# SPECIALISE addReference :: WriteBufferBlobs IO h -> IO () #-}
144132addReference :: PrimMonad m => WriteBufferBlobs m h -> m ()
145- addReference WriteBufferBlobs {blobFileRefCounter } =
146- RC. addReference blobFileRefCounter
133+ addReference WriteBufferBlobs {blobFile } =
134+ RC. addReference ( blobFileRefCounter blobFile)
147135
148136{-# SPECIALISE removeReference :: WriteBufferBlobs IO h -> IO () #-}
149137removeReference :: (PrimMonad m , MonadMask m ) => WriteBufferBlobs m h -> m ()
150- removeReference WriteBufferBlobs {blobFileRefCounter } =
151- RC . removeReference blobFileRefCounter
138+ removeReference WriteBufferBlobs {blobFile } =
139+ BlobFile . removeReference blobFile
152140
153141{-# SPECIALISE addBlob :: HasFS IO h -> WriteBufferBlobs IO h -> SerialisedBlob -> IO BlobSpan #-}
154142addBlob :: (PrimMonad m , MonadThrow m )
155143 => HasFS m h
156144 -> WriteBufferBlobs m h
157145 -> SerialisedBlob
158146 -> m BlobSpan
159- addBlob fs WriteBufferBlobs {blobFileHandle , blobFilePointer} blob = do
147+ addBlob fs WriteBufferBlobs {blobFile , blobFilePointer} blob = do
160148 let blobsize = sizeofBlob blob
161149 bloboffset <- updateFilePointer blobFilePointer blobsize
162- writeBlobAtOffset fs blobFileHandle blob bloboffset
150+ writeBlobAtOffset fs ( blobFileHandle blobFile) blob bloboffset
163151 return BlobSpan {
164152 blobSpanOffset = bloboffset,
165153 blobSpanSize = fromIntegral blobsize
@@ -187,27 +175,17 @@ readBlob :: (PrimMonad m, MonadThrow m)
187175 -> WriteBufferBlobs m h
188176 -> BlobSpan
189177 -> m SerialisedBlob
190- readBlob fs WriteBufferBlobs {blobFileHandle}
191- BlobSpan {blobSpanOffset, blobSpanSize} = do
192- let off = FS. AbsOffset blobSpanOffset
193- len :: Int
194- len = fromIntegral blobSpanSize
195- mba <- P. newPinnedByteArray len
196- _ <- FS. hGetBufExactlyAt fs blobFileHandle mba 0
197- (fromIntegral len :: FS. ByteCount ) off
198- ba <- P. unsafeFreezeByteArray mba
199- let ! rb = RB. fromByteArray 0 len ba
200- return (SerialisedBlob rb)
201-
178+ readBlob fs WriteBufferBlobs {blobFile} blobspan =
179+ readBlobFile fs blobFile blobspan
202180
203181-- | Helper function to make a 'BlobRef' that points into a 'WriteBufferBlobs'.
204182mkBlobRef :: WriteBufferBlobs m h
205183 -> BlobSpan
206184 -> BlobRef m (FS. Handle h )
207- mkBlobRef WriteBufferBlobs {blobFileHandle, blobFileRefCounter } blobRefSpan =
185+ mkBlobRef WriteBufferBlobs {blobFile } blobRefSpan =
208186 BlobRef {
209- blobRefFile = blobFileHandle,
210- blobRefCount = blobFileRefCounter,
187+ blobRefFile = blobFileHandle blobFile ,
188+ blobRefCount = blobFileRefCounter blobFile ,
211189 blobRefSpan
212190 }
213191
0 commit comments