Skip to content

Commit feb1cf4

Browse files
committed
Convert WriteBufferBlobs to use BlobFile
Where before it contained a file handle and a ref counter, it now just contains a BlobFile (which itself is the pair of a handle and counter).
1 parent 3e105b5 commit feb1cf4

File tree

2 files changed

+25
-42
lines changed

2 files changed

+25
-42
lines changed

src-extras/Database/LSMTree/Extras/NoThunks.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import qualified Data.Vector.Primitive as VP
3434
import qualified Data.Vector.Unboxed.Mutable as VUM
3535
import Data.Word
3636
import Database.LSMTree.Internal as Internal
37+
import Database.LSMTree.Internal.BlobFile
3738
import Database.LSMTree.Internal.BlobRef
3839
import Database.LSMTree.Internal.Config
3940
import Database.LSMTree.Internal.CRC32C
@@ -423,6 +424,10 @@ deriving anyclass instance (NoThunks h, Typeable (PrimState m))
423424
deriving stock instance Generic BlobSpan
424425
deriving anyclass instance NoThunks BlobSpan
425426

427+
deriving stock instance Generic (BlobFile m h)
428+
deriving anyclass instance (Typeable h, Typeable (PrimState m))
429+
=> NoThunks (BlobFile m h)
430+
426431
{-------------------------------------------------------------------------------
427432
Arena
428433
-------------------------------------------------------------------------------}

src/Database/LSMTree/Internal/WriteBufferBlobs.hs

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ import Data.Primitive.ByteArray as P
4141
import Data.Primitive.PrimVar as P
4242
import qualified Data.Vector.Primitive as VP
4343
import 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 (..))
4647
import Database.LSMTree.Internal.Serialise
4748
import qualified System.FS.API as FS
4849
import System.FS.API (HasFS)
@@ -102,17 +103,14 @@ import qualified System.Posix.Types as FS (ByteCount)
102103
--
103104
data 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

114112
instance 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) #-}
118116
new :: 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 () #-}
144132
addReference :: 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 () #-}
149137
removeReference :: (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 #-}
154142
addBlob :: (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'.
204182
mkBlobRef :: 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

Comments
 (0)