Skip to content

Commit 68caad0

Browse files
committed
Address review comments
* Rename readBlobFile to readBlob * Change newBlobFile to openBlobFile, also now responsible for opening the file, rather than adopting an existing file handle. * Update comments * Add specialise pragmas * Drop unused language pragmas
1 parent 85d2611 commit 68caad0

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

src/Database/LSMTree/Internal/BlobFile.hs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
{-# LANGUAGE DeriveAnyClass #-}
2-
{-# LANGUAGE DeriveGeneric #-}
3-
{-# LANGUAGE DerivingStrategies #-}
4-
{- HLINT ignore "Use unless" -}
5-
61
module Database.LSMTree.Internal.BlobFile (
72
BlobFile (..)
83
, BlobSpan (..)
94
, removeReference
105
, RemoveFileOnClose (..)
11-
, newBlobFile
12-
, readBlobFile
6+
, openBlobFile
7+
, readBlob
138
) where
149

1510
import Control.DeepSeq (NFData (..))
@@ -27,7 +22,7 @@ import qualified System.FS.API as FS
2722
import System.FS.API (HasFS)
2823
import qualified System.FS.BlockIO.API as FS
2924

30-
-- | An open handle to a file containing blobs.
25+
-- | A handle to a file containing blobs.
3126
--
3227
-- This is a reference counted object. Upon finalisation, the file is closed
3328
-- and deleted.
@@ -51,6 +46,7 @@ data BlobSpan = BlobSpan {
5146
instance NFData BlobSpan where
5247
rnf (BlobSpan a b) = rnf a `seq` rnf b
5348

49+
{-# INLINE removeReference #-}
5450
removeReference ::
5551
(MonadMask m, PrimMonad m)
5652
=> BlobFile m h
@@ -63,37 +59,41 @@ removeReference BlobFile{blobFileRefCounter} =
6359
data RemoveFileOnClose = RemoveFileOnClose | DoNotRemoveFileOnClose
6460
deriving stock Eq
6561

66-
-- | Adopt an existing open file handle to make a 'BlobFile'. The file must at
67-
-- least be open for reading (but may or may not be open for writing).
62+
-- | Open the given file to make a 'BlobFile'. The finaliser will close and
63+
-- delete the file.
6864
--
69-
-- The finaliser will close and delete the file.
65+
-- TODO: Temporarily we have a 'RemoveFileOnClose' flag, which can be removed
66+
-- once 'Run' no longer needs it, when snapshots are implemented.
7067
--
71-
newBlobFile ::
68+
{-# SPECIALISE openBlobFile :: HasFS IO h -> FS.FsPath -> FS.OpenMode -> RemoveFileOnClose -> IO (BlobFile IO h) #-}
69+
openBlobFile ::
7270
PrimMonad m
7371
=> HasFS m h
72+
-> FS.FsPath
73+
-> FS.OpenMode
7474
-> RemoveFileOnClose
75-
-> FS.Handle h
7675
-> m (BlobFile m h)
77-
newBlobFile fs r blobFileHandle = do
76+
openBlobFile fs path mode remove = do
77+
blobFileHandle <- FS.hOpen fs path mode
7878
let finaliser = do
7979
FS.hClose fs blobFileHandle
80-
unless (r == DoNotRemoveFileOnClose) $
80+
unless (remove == DoNotRemoveFileOnClose) $
8181
FS.removeFile fs (FS.handlePath blobFileHandle)
8282
blobFileRefCounter <- RC.mkRefCounter1 (Just finaliser)
8383
return BlobFile {
8484
blobFileHandle,
8585
blobFileRefCounter
8686
}
8787

88-
{-# SPECIALISE readBlobFile :: HasFS IO h -> BlobFile IO h -> BlobSpan -> IO SerialisedBlob #-}
89-
readBlobFile ::
88+
{-# SPECIALISE readBlob :: HasFS IO h -> BlobFile IO h -> BlobSpan -> IO SerialisedBlob #-}
89+
readBlob ::
9090
(MonadThrow m, PrimMonad m)
9191
=> HasFS m h
9292
-> BlobFile m h
9393
-> BlobSpan
9494
-> m SerialisedBlob
95-
readBlobFile fs BlobFile {blobFileHandle}
96-
BlobSpan {blobSpanOffset, blobSpanSize} = do
95+
readBlob fs BlobFile {blobFileHandle}
96+
BlobSpan {blobSpanOffset, blobSpanSize} = do
9797
let off = FS.AbsOffset blobSpanOffset
9898
len :: Int
9999
len = fromIntegral blobSpanSize

src/Database/LSMTree/Internal/BlobRef.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ readRawBlobRef ::
141141
-> RawBlobRef m h
142142
-> m SerialisedBlob
143143
readRawBlobRef fs RawBlobRef {rawBlobRefFile, rawBlobRefSpan} =
144-
BlobFile.readBlobFile fs rawBlobRefFile rawBlobRefSpan
144+
BlobFile.readBlob fs rawBlobRefFile rawBlobRefSpan
145145

146146
{-# SPECIALISE readWeakBlobRef :: HasFS IO h -> WeakBlobRef IO h -> IO SerialisedBlob #-}
147147
readWeakBlobRef ::
@@ -152,7 +152,7 @@ readWeakBlobRef ::
152152
readWeakBlobRef fs wref =
153153
bracket (deRefWeakBlobRef wref) removeReference $
154154
\StrongBlobRef {strongBlobRefFile, strongBlobRefSpan} ->
155-
BlobFile.readBlobFile fs strongBlobRefFile strongBlobRefSpan
155+
BlobFile.readBlob fs strongBlobRefFile strongBlobRefSpan
156156

157157
{-# SPECIALISE readWeakBlobRefs :: HasBlockIO IO h -> V.Vector (WeakBlobRef IO h) -> IO (V.Vector SerialisedBlob) #-}
158158
readWeakBlobRefs ::

src/Database/LSMTree/Internal/Run.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ mkWeakBlobRef Run{runBlobFile} blobspan =
169169
--
170170
-- TODO: Once snapshots are implemented, files should get removed, but for now
171171
-- we want to be able to re-open closed runs from disk.
172-
-- TODO: see newBlobFile DoNotRemoveFileOnClose. This can be dropped at the
172+
-- TODO: see openBlobFile DoNotRemoveFileOnClose. This can be dropped at the
173173
-- same once when snapshots are implemented.
174174
close :: (MonadSTM m, MonadMask m, PrimMonad m) => Run m h -> m ()
175175
close Run {..} = do
@@ -227,8 +227,8 @@ fromMutable runRunDataCaching refCount builder = do
227227
(runHasFS, runHasBlockIO, runRunFsPaths, runFilter, runIndex, runNumEntries) <-
228228
Builder.unsafeFinalise (runRunDataCaching == NoCacheRunData) builder
229229
runKOpsFile <- FS.hOpen runHasFS (runKOpsPath runRunFsPaths) FS.ReadMode
230-
runBlobFile <- newBlobFile runHasFS DoNotRemoveFileOnClose
231-
=<< FS.hOpen runHasFS (runBlobPath runRunFsPaths) FS.ReadMode
230+
runBlobFile <- openBlobFile runHasFS (runBlobPath runRunFsPaths) FS.ReadMode
231+
DoNotRemoveFileOnClose
232232
setRunDataCaching runHasBlockIO runKOpsFile runRunDataCaching
233233
rec runRefCounter <- RC.unsafeMkRefCounterN refCount (Just $ close r)
234234
let !r = Run { .. }
@@ -313,8 +313,8 @@ openFromDisk fs hbio runRunDataCaching runRunFsPaths = do
313313
=<< readCRC (forRunIndex expectedChecksums) (forRunIndex paths)
314314

315315
runKOpsFile <- FS.hOpen fs (runKOpsPath runRunFsPaths) FS.ReadMode
316-
runBlobFile <- newBlobFile fs DoNotRemoveFileOnClose
317-
=<< FS.hOpen fs (runBlobPath runRunFsPaths) FS.ReadMode
316+
runBlobFile <- openBlobFile fs (runBlobPath runRunFsPaths) FS.ReadMode
317+
DoNotRemoveFileOnClose
318318
setRunDataCaching hbio runKOpsFile runRunDataCaching
319319
rec runRefCounter <- RC.unsafeMkRefCounterN (RefCount 1) (Just $ close r)
320320
let !r = Run

src/Database/LSMTree/Internal/WriteBufferBlobs.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ new :: PrimMonad m
121121
new fs blobFileName = do
122122
-- Must use read/write mode because we write blobs when adding, but
123123
-- we can also be asked to retrieve blobs at any time.
124-
blobFileHandle <- FS.hOpen fs blobFileName (FS.ReadWriteMode FS.MustBeNew)
124+
blobFile <- openBlobFile fs blobFileName (FS.ReadWriteMode FS.MustBeNew)
125+
RemoveFileOnClose
125126
blobFilePointer <- newFilePointer
126-
blobFile <- newBlobFile fs RemoveFileOnClose blobFileHandle
127127
return WriteBufferBlobs {
128128
blobFile,
129129
blobFilePointer

0 commit comments

Comments
 (0)