Skip to content

Commit 1488d9e

Browse files
committed
Temporary hack until we have proper snapshots
Currently, pre-snapshots, the Run does _not_ delete its blob files when the run is closed, but the WriteBufferBlobs _does_ delete its blob file. Insert a temporary hack to accomodate this. This commit can be reverted as part of implementing snapshots properly.
1 parent 8293486 commit 1488d9e

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/Database/LSMTree/Internal/BlobFile.hs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ module Database.LSMTree.Internal.BlobFile (
77
BlobFile (..)
88
, BlobSpan (..)
99
, removeReference
10+
, RemoveFileOnClose (..)
1011
, newBlobFile
1112
, readBlobFile
1213
) where
1314

1415
import Control.DeepSeq (NFData (..))
16+
import Control.Monad (unless)
1517
import Control.Monad.Class.MonadThrow (MonadThrow, MonadMask)
1618
import Control.Monad.Primitive (PrimMonad)
1719
import Control.RefCount (RefCounter)
@@ -56,6 +58,11 @@ removeReference ::
5658
removeReference BlobFile{blobFileRefCounter} =
5759
RC.removeReference blobFileRefCounter
5860

61+
-- | TODO: this hack can be removed once snapshots are done properly and so
62+
-- runs can delete their files on close.
63+
data RemoveFileOnClose = RemoveFileOnClose | DoNotRemoveFileOnClose
64+
deriving stock Eq
65+
5966
-- | Adopt an existing open file handle to make a 'BlobFile'. The file must at
6067
-- least be open for reading (but may or may not be open for writing).
6168
--
@@ -64,12 +71,14 @@ removeReference BlobFile{blobFileRefCounter} =
6471
newBlobFile ::
6572
PrimMonad m
6673
=> HasFS m h
74+
-> RemoveFileOnClose
6775
-> FS.Handle h
6876
-> m (BlobFile m h)
69-
newBlobFile fs blobFileHandle = do
77+
newBlobFile fs r blobFileHandle = do
7078
let finaliser = do
7179
FS.hClose fs blobFileHandle
72-
FS.removeFile fs (FS.handlePath blobFileHandle)
80+
unless (r == DoNotRemoveFileOnClose) $
81+
FS.removeFile fs (FS.handlePath blobFileHandle)
7382
blobFileRefCounter <- RC.mkRefCounter1 (Just finaliser)
7483
return BlobFile {
7584
blobFileHandle,

src/Database/LSMTree/Internal/Run.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ mkWeakBlobRef Run{runBlobFile} blobspan =
168168
--
169169
-- TODO: Once snapshots are implemented, files should get removed, but for now
170170
-- we want to be able to re-open closed runs from disk.
171+
-- TODO: see newBlobFile DoNotRemoveFileOnClose. This can be dropped at the
172+
-- same once when snapshots are implemented.
171173
close :: (MonadSTM m, MonadMask m, PrimMonad m) => Run m h -> m ()
172174
close Run {..} = do
173175
-- TODO: removing files should drop them from the page cache, but until we
@@ -224,7 +226,7 @@ fromMutable runRunDataCaching refCount builder = do
224226
(runHasFS, runHasBlockIO, runRunFsPaths, runFilter, runIndex, runNumEntries) <-
225227
Builder.unsafeFinalise (runRunDataCaching == NoCacheRunData) builder
226228
runKOpsFile <- FS.hOpen runHasFS (runKOpsPath runRunFsPaths) FS.ReadMode
227-
runBlobFile <- newBlobFile runHasFS
229+
runBlobFile <- newBlobFile runHasFS DoNotRemoveFileOnClose
228230
=<< FS.hOpen runHasFS (runBlobPath runRunFsPaths) FS.ReadMode
229231
setRunDataCaching runHasBlockIO runKOpsFile runRunDataCaching
230232
rec runRefCounter <- RC.unsafeMkRefCounterN refCount (Just $ close r)
@@ -310,7 +312,7 @@ openFromDisk fs hbio runRunDataCaching runRunFsPaths = do
310312
=<< readCRC (forRunIndex expectedChecksums) (forRunIndex paths)
311313

312314
runKOpsFile <- FS.hOpen fs (runKOpsPath runRunFsPaths) FS.ReadMode
313-
runBlobFile <- newBlobFile fs
315+
runBlobFile <- newBlobFile fs DoNotRemoveFileOnClose
314316
=<< FS.hOpen fs (runBlobPath runRunFsPaths) FS.ReadMode
315317
setRunDataCaching hbio runKOpsFile runRunDataCaching
316318
rec runRefCounter <- RC.unsafeMkRefCounterN (RefCount 1) (Just $ close r)

src/Database/LSMTree/Internal/WriteBufferBlobs.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ new fs blobFileName = do
122122
-- we can also be asked to retrieve blobs at any time.
123123
blobFileHandle <- FS.hOpen fs blobFileName (FS.ReadWriteMode FS.MustBeNew)
124124
blobFilePointer <- newFilePointer
125-
blobFile <- newBlobFile fs blobFileHandle
125+
blobFile <- newBlobFile fs RemoveFileOnClose blobFileHandle
126126
return WriteBufferBlobs {
127127
blobFile,
128128
blobFilePointer

0 commit comments

Comments
 (0)