Skip to content

Commit 3b801cc

Browse files
committed
Convert Run 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 feb1cf4 commit 3b801cc

File tree

5 files changed

+27
-21
lines changed

5 files changed

+27
-21
lines changed

src/Database/LSMTree/Internal/Merge.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ finaliser var b rs = do
190190
-- this function with async exceptions masked. Otherwise, these resources can
191191
-- leak.
192192
complete ::
193-
(MonadFix m, MonadSTM m, MonadST m, MonadThrow m)
193+
(MonadFix m, MonadSTM m, MonadST m, MonadMask m)
194194
=> Merge m h
195195
-> m (Run m h)
196196
complete Merge{..} = do
@@ -218,7 +218,7 @@ complete Merge{..} = do
218218
--
219219
-- Note: run with async exceptions masked. See 'complete'.
220220
stepsToCompletion ::
221-
(MonadCatch m, MonadFix m, MonadSTM m, MonadST m)
221+
(MonadMask m, MonadFix m, MonadSTM m, MonadST m)
222222
=> Merge m h
223223
-> Int
224224
-> m (Run m h)
@@ -237,7 +237,7 @@ stepsToCompletion m stepBatchSize = go
237237
--
238238
-- Note: run with async exceptions masked. See 'complete'.
239239
stepsToCompletionCounted ::
240-
(MonadCatch m, MonadFix m, MonadSTM m, MonadST m)
240+
(MonadMask m, MonadFix m, MonadSTM m, MonadST m)
241241
=> Merge m h
242242
-> Int
243243
-> m (Int, Run m h)

src/Database/LSMTree/Internal/MergeSchedule.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ levelIsFull sr rs = V.length rs + 1 >= (sizeRatioInt sr)
869869

870870
{-# SPECIALISE mergeRuns :: ResolveSerialisedValue -> HasFS IO h -> HasBlockIO IO h -> RunDataCaching -> RunBloomFilterAlloc -> RunFsPaths -> Merge.Level -> V.Vector (Run IO h) -> IO (Run IO h) #-}
871871
mergeRuns ::
872-
(MonadCatch m, MonadFix m, MonadST m, MonadSTM m)
872+
(MonadMask m, MonadFix m, MonadST m, MonadSTM m)
873873
=> ResolveSerialisedValue
874874
-> HasFS m h
875875
-> HasBlockIO m h

src/Database/LSMTree/Internal/Run.hs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ import Data.BloomFilter (Bloom)
6666
import qualified Data.ByteString.Short as SBS
6767
import Data.Foldable (for_)
6868
import Data.Word (Word64)
69-
import Database.LSMTree.Internal.BlobRef (BlobRef (..), BlobSpan (..))
69+
import Database.LSMTree.Internal.BlobFile hiding (removeReference)
70+
import qualified Database.LSMTree.Internal.BlobFile as BlobFile
71+
import Database.LSMTree.Internal.BlobRef hiding (removeReference)
7072
import Database.LSMTree.Internal.BloomFilter (bloomFilterFromSBS)
7173
import qualified Database.LSMTree.Internal.CRC32C as CRC
7274
import Database.LSMTree.Internal.Entry (NumEntries (..))
@@ -111,7 +113,7 @@ data Run m h = Run {
111113
-- | The file handle for the BLOBs file. This file is opened
112114
-- read-only and is accessed in a normal style using buffered
113115
-- I\/O, reading arbitrary file offset and length spans.
114-
, runBlobFile :: !(FS.Handle h)
116+
, runBlobFile :: !(BlobFile m h)
115117
, runRunDataCaching :: !RunDataCaching
116118
, runHasFS :: !(HasFS m h)
117119
, runHasBlockIO :: !(HasBlockIO m h)
@@ -143,10 +145,10 @@ removeReferenceN r = RC.removeReferenceN (runRefCounter r)
143145

144146
-- | Helper function to make a 'BlobRef' that points into a 'Run'.
145147
mkBlobRefForRun :: Run m h -> BlobSpan -> BlobRef m (FS.Handle h)
146-
mkBlobRefForRun Run{runBlobFile, runRefCounter} blobRefSpan =
148+
mkBlobRefForRun Run{runBlobFile} blobRefSpan =
147149
BlobRef {
148-
blobRefFile = runBlobFile,
149-
blobRefCount = runRefCounter,
150+
blobRefFile = blobFileHandle runBlobFile,
151+
blobRefCount = blobFileRefCounter runBlobFile,
150152
blobRefSpan
151153
}
152154

@@ -158,18 +160,18 @@ mkBlobRefForRun Run{runBlobFile, runRefCounter} blobRefSpan =
158160
--
159161
-- TODO: Once snapshots are implemented, files should get removed, but for now
160162
-- we want to be able to re-open closed runs from disk.
161-
close :: (MonadSTM m, MonadThrow m) => Run m h -> m ()
163+
close :: (MonadSTM m, MonadMask m, PrimMonad m) => Run m h -> m ()
162164
close Run {..} = do
163165
-- TODO: removing files should drop them from the page cache, but until we
164166
-- have proper snapshotting we are keeping the files around. Because of
165167
-- this, we instruct the OS to drop all run-related files from the page
166168
-- cache
167169
FS.hDropCacheAll runHasBlockIO runKOpsFile
168-
FS.hDropCacheAll runHasBlockIO runBlobFile
170+
FS.hDropCacheAll runHasBlockIO (blobFileHandle runBlobFile)
169171

170172
FS.hClose runHasFS runKOpsFile
171173
`finally`
172-
FS.hClose runHasFS runBlobFile
174+
BlobFile.removeReference runBlobFile
173175

174176
-- | Should this run cache key\/ops data in memory?
175177
data RunDataCaching = CacheRunData | NoCacheRunData
@@ -205,7 +207,7 @@ setRunDataCaching hbio runKOpsFile NoCacheRunData = do
205207
-> RunBuilder IO h
206208
-> IO (Run IO h) #-}
207209
fromMutable ::
208-
(MonadFix m, MonadST m, MonadSTM m, MonadThrow m)
210+
(MonadFix m, MonadST m, MonadSTM m, MonadMask m)
209211
=> RunDataCaching
210212
-> RefCount
211213
-> RunBuilder m h
@@ -214,7 +216,8 @@ fromMutable runRunDataCaching refCount builder = do
214216
(runHasFS, runHasBlockIO, runRunFsPaths, runFilter, runIndex, runNumEntries) <-
215217
Builder.unsafeFinalise (runRunDataCaching == NoCacheRunData) builder
216218
runKOpsFile <- FS.hOpen runHasFS (runKOpsPath runRunFsPaths) FS.ReadMode
217-
runBlobFile <- FS.hOpen runHasFS (runBlobPath runRunFsPaths) FS.ReadMode
219+
runBlobFile <- newBlobFile runHasFS
220+
=<< FS.hOpen runHasFS (runBlobPath runRunFsPaths) FS.ReadMode
218221
setRunDataCaching runHasBlockIO runKOpsFile runRunDataCaching
219222
rec runRefCounter <- RC.unsafeMkRefCounterN refCount (Just $ close r)
220223
let !r = Run { .. }
@@ -236,7 +239,7 @@ fromMutable runRunDataCaching refCount builder = do
236239
-- immediately when they are added to the write buffer, avoiding the need to do
237240
-- it here.
238241
fromWriteBuffer ::
239-
(MonadFix m, MonadST m, MonadSTM m, MonadThrow m)
242+
(MonadFix m, MonadST m, MonadSTM m, MonadMask m)
240243
=> HasFS m h
241244
-> HasBlockIO m h
242245
-> RunDataCaching
@@ -274,7 +277,7 @@ data FileFormatError = FileFormatError FS.FsPath String
274277
-- checksum ('ChecksumError') or can't be parsed ('FileFormatError').
275278
openFromDisk ::
276279
forall m h.
277-
(MonadFix m, MonadSTM m, MonadThrow m, PrimMonad m)
280+
(MonadFix m, MonadSTM m, MonadMask m, PrimMonad m)
278281
=> HasFS m h
279282
-> HasBlockIO m h
280283
-> RunDataCaching
@@ -299,7 +302,8 @@ openFromDisk fs hbio runRunDataCaching runRunFsPaths = do
299302
=<< readCRC (forRunIndex expectedChecksums) (forRunIndex paths)
300303

301304
runKOpsFile <- FS.hOpen fs (runKOpsPath runRunFsPaths) FS.ReadMode
302-
runBlobFile <- FS.hOpen fs (runBlobPath runRunFsPaths) FS.ReadMode
305+
runBlobFile <- newBlobFile fs
306+
=<< FS.hOpen fs (runBlobPath runRunFsPaths) FS.ReadMode
303307
setRunDataCaching hbio runKOpsFile runRunDataCaching
304308
rec runRefCounter <- RC.unsafeMkRefCounterN (RefCount 1) (Just $ close r)
305309
let !r = Run

test/Test/Database/LSMTree/Internal/Merge.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import qualified Data.Vector as V
1212
import Database.LSMTree.Extras
1313
import Database.LSMTree.Extras.Generators (KeyForIndexCompact)
1414
import Database.LSMTree.Extras.RunData
15+
import qualified Database.LSMTree.Internal.BlobFile as BlobFile
1516
import qualified Database.LSMTree.Internal.Entry as Entry
1617
import qualified Database.LSMTree.Internal.Merge as Merge
1718
import Database.LSMTree.Internal.PageAcc (entryWouldFitInPage)
@@ -73,9 +74,9 @@ prop_MergeDistributes fs hbio level stepSize (SmallList rds) =
7374
withRun fs hbio (simplePath 1) (RunData $ mergeWriteBuffers level $ fmap unRunData rds') $ \rhs -> do
7475

7576
lhsKOpsFile <- FS.hGetAll fs (Run.runKOpsFile lhs)
76-
lhsBlobFile <- FS.hGetAll fs (Run.runBlobFile lhs)
77+
lhsBlobFile <- FS.hGetAll fs (BlobFile.blobFileHandle (Run.runBlobFile lhs))
7778
rhsKOpsFile <- FS.hGetAll fs (Run.runKOpsFile rhs)
78-
rhsBlobFile <- FS.hGetAll fs (Run.runBlobFile rhs)
79+
rhsBlobFile <- FS.hGetAll fs (BlobFile.blobFileHandle (Run.runBlobFile rhs))
7980

8081
lhsKOps <- readKOps Nothing lhs
8182
rhsKOps <- readKOps Nothing rhs

test/Test/Database/LSMTree/Internal/Run.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Test.Tasty.QuickCheck
2525
import Control.RefCount (RefCount (..), readRefCount)
2626
import Database.LSMTree.Extras.Generators (KeyForIndexCompact (..))
2727
import Database.LSMTree.Extras.RunData
28+
import Database.LSMTree.Internal.BlobFile (BlobFile (..))
2829
import Database.LSMTree.Internal.BlobRef (BlobSpan (..))
2930
import qualified Database.LSMTree.Internal.CRC32C as CRC
3031
import Database.LSMTree.Internal.Entry
@@ -195,8 +196,8 @@ prop_WriteAndOpen fs hbio wb =
195196
(FS.handlePath (runKOpsFile written))
196197
(FS.handlePath (runKOpsFile loaded))
197198
assertEqual "blob file"
198-
(FS.handlePath (runBlobFile written))
199-
(FS.handlePath (runBlobFile loaded))
199+
(FS.handlePath (blobFileHandle (runBlobFile written)))
200+
(FS.handlePath (blobFileHandle (runBlobFile loaded)))
200201

201202
-- make sure runs get closed again
202203
removeReference loaded

0 commit comments

Comments
 (0)