Skip to content

Commit e61a430

Browse files
committed
Make snapshotRun/openRun singluar on Run rather than Levels
It is just a simple `traverse` to lift it to Levels. This makes it simpler to reuse with merging trees of runs, which can also just use `traverse`.
1 parent 23c41e3 commit e61a430

File tree

2 files changed

+65
-76
lines changed

2 files changed

+65
-76
lines changed

src/Database/LSMTree/Internal.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ createSnapshot snap label tableType t = do
12401240
snapLevels <- toSnapLevels (tableLevels content)
12411241

12421242
-- Hard link runs into the named snapshot directory
1243-
snapLevels' <- snapshotRuns reg snapUc snapDir snapLevels
1243+
snapLevels' <- traverse (snapshotRun hfs hbio snapUc reg snapDir) snapLevels
12441244

12451245
-- Release the table content
12461246
releaseTableContent reg content
@@ -1308,11 +1308,11 @@ openSnapshot sesh label tableType override snap resolve = do
13081308
(tableWriteBuffer, tableWriteBufferBlobs) <- openWriteBuffer reg resolve hfs hbio uc activeDir snapWriteBufferPaths
13091309

13101310
-- Hard link runs into the active directory,
1311-
snapLevels' <- openRuns reg hfs hbio (sessionUniqCounter seshEnv) snapDir activeDir snapLevels
1311+
snapLevels' <- traverse (openRun hfs hbio uc reg snapDir activeDir) snapLevels
13121312

13131313
-- Convert from the snapshot format, restoring merge progress in the process
1314-
tableLevels <- fromSnapLevels reg hfs hbio conf (sessionUniqCounter seshEnv) resolve activeDir snapLevels'
1315-
releaseRuns reg snapLevels'
1314+
tableLevels <- fromSnapLevels reg hfs hbio conf uc resolve activeDir snapLevels'
1315+
traverse_ (delayedCommit reg . releaseRef) snapLevels'
13161316

13171317
tableCache <- mkLevelsCache reg tableLevels
13181318
newWith reg sesh seshEnv conf' am $! TableContent {

src/Database/LSMTree/Internal/Snapshot.hs

Lines changed: 61 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ module Database.LSMTree.Internal.Snapshot (
1313
-- * Write buffer
1414
, snapshotWriteBuffer
1515
, openWriteBuffer
16-
-- * Runs
16+
-- * Run
1717
, SnapshotRun (..)
18-
, snapshotRuns
19-
, openRuns
20-
, releaseRuns
18+
, snapshotRun
19+
, openRun
2120
-- * Opening from levels snapshot format
2221
, fromSnapLevels
2322
-- * Hard links
@@ -33,9 +32,8 @@ import Control.Monad.Class.MonadST (MonadST)
3332
import Control.Monad.Class.MonadThrow (MonadMask, bracketOnError)
3433
import Control.Monad.Primitive (PrimMonad)
3534
import Control.RefCount
36-
import Data.Foldable (sequenceA_, traverse_)
35+
import Data.Foldable (sequenceA_)
3736
import Data.Text (Text)
38-
import Data.Traversable (for)
3937
import qualified Data.Vector as V
4038
import Database.LSMTree.Internal.Config
4139
import Database.LSMTree.Internal.CRC32C (checkCRC)
@@ -354,8 +352,8 @@ openWriteBuffer reg resolve hfs hbio uc activeDir snapWriteBufferPaths = do
354352
Runs
355353
-------------------------------------------------------------------------------}
356354

357-
-- | Information needed to open a 'Run' from disk using 'snapshotRuns' and
358-
-- 'openRuns'.
355+
-- | Information needed to open a 'Run' from disk using 'snapshotRun' and
356+
-- 'openRun'.
359357
--
360358
-- TODO: one could imagine needing only the 'RunNumber' to identify the files
361359
-- on disk, and the other parameters being stored with the run itself, rather
@@ -370,87 +368,78 @@ data SnapshotRun = SnapshotRun {
370368
instance NFData SnapshotRun where
371369
rnf (SnapshotRun a b c) = rnf a `seq` rnf b `seq` rnf c
372370

373-
{-# SPECIALISE snapshotRuns ::
374-
ActionRegistry IO
371+
{-# SPECIALISE snapshotRun ::
372+
HasFS IO h
373+
-> HasBlockIO IO h
375374
-> UniqCounter IO
375+
-> ActionRegistry IO
376376
-> NamedSnapshotDir
377-
-> SnapLevels (Ref (Run IO h))
378-
-> IO (SnapLevels SnapshotRun) #-}
379-
-- | @'snapshotRuns' _ _ snapUc targetDir levels@ creates hard links for all run
380-
-- files associated with the runs in @levels@, and puts the new directory
381-
-- entries in the @targetDir@ directory. The entries are renamed using @snapUc@.
382-
snapshotRuns ::
377+
-> Ref (Run IO h)
378+
-> IO SnapshotRun #-}
379+
-- | @'snapshotRun' _ _ snapUc targetDir run@ creates hard links for all files
380+
-- associated with the run, and puts the new directory entries in the
381+
-- @targetDir@ directory. The entries are renamed using @snapUc@.
382+
snapshotRun ::
383383
(MonadMask m, PrimMonad m)
384-
=> ActionRegistry m
384+
=> HasFS m h
385+
-> HasBlockIO m h
385386
-> UniqCounter m
387+
-> ActionRegistry m
386388
-> NamedSnapshotDir
387-
-> SnapLevels (Ref (Run m h))
388-
-> m (SnapLevels SnapshotRun)
389-
snapshotRuns reg snapUc (NamedSnapshotDir targetDir) levels = do
390-
for levels $ \run@(DeRef Run.Run {
391-
Run.runHasFS = hfs,
392-
Run.runHasBlockIO = hbio
393-
}) -> do
394-
rn <- uniqueToRunNumber <$> incrUniqCounter snapUc
395-
let sourcePaths = Run.runFsPaths run
396-
let targetPaths = sourcePaths { runDir = targetDir , runNumber = rn}
397-
hardLinkRunFiles reg hfs hbio sourcePaths targetPaths
398-
pure SnapshotRun {
399-
snapRunNumber = runNumber targetPaths,
400-
snapRunCaching = Run.runDataCaching run,
401-
snapRunIndex = Run.runIndexType run
402-
}
403-
404-
{-# SPECIALISE openRuns ::
405-
ActionRegistry IO
406-
-> HasFS IO h
389+
-> Ref (Run m h)
390+
-> m SnapshotRun
391+
snapshotRun hfs hbio snapUc reg (NamedSnapshotDir targetDir) run = do
392+
rn <- uniqueToRunNumber <$> incrUniqCounter snapUc
393+
let sourcePaths = Run.runFsPaths run
394+
let targetPaths = sourcePaths { runDir = targetDir , runNumber = rn}
395+
hardLinkRunFiles reg hfs hbio sourcePaths targetPaths
396+
pure SnapshotRun {
397+
snapRunNumber = runNumber targetPaths,
398+
snapRunCaching = Run.runDataCaching run,
399+
snapRunIndex = Run.runIndexType run
400+
}
401+
402+
{-# SPECIALISE openRun ::
403+
HasFS IO h
407404
-> HasBlockIO IO h
408405
-> UniqCounter IO
406+
-> ActionRegistry IO
409407
-> NamedSnapshotDir
410408
-> ActiveDir
411-
-> SnapLevels SnapshotRun
412-
-> IO (SnapLevels (Ref (Run IO h))) #-}
413-
-- | @'openRuns' _ _ _ _ uniqCounter sourceDir targetDir levels@ takes all run
414-
-- files that are referenced by @levels@, and hard links them from @sourceDir@
409+
-> SnapshotRun
410+
-> IO (Ref (Run IO h)) #-}
411+
-- | @'openRun' _ _ _ _ uniqCounter sourceDir targetDir snaprun@ takes all run
412+
-- files that are referenced by @snaprun@, and hard links them from @sourceDir@
415413
-- into @targetDir@ with new, unique names (using @uniqCounter@). Each set of
416414
-- (hard linked) files that represents a run is opened and verified, returning
417-
-- 'Run's as a result.
415+
-- 'Run' as a result.
418416
--
419-
-- The result must ultimately be released using 'releaseRuns'.
420-
openRuns ::
417+
-- The result must ultimately be released using 'releaseRef'.
418+
openRun ::
421419
(MonadMask m, MonadSTM m, MonadST m)
422-
=> ActionRegistry m
423-
-> HasFS m h
420+
=> HasFS m h
424421
-> HasBlockIO m h
425422
-> UniqCounter m
423+
-> ActionRegistry m
426424
-> NamedSnapshotDir
427425
-> ActiveDir
428-
-> SnapLevels SnapshotRun
429-
-> m (SnapLevels (Ref (Run m h)))
430-
openRuns reg hfs hbio uc (NamedSnapshotDir sourceDir) (ActiveDir targetDir)
431-
levels =
432-
for levels $
433-
\SnapshotRun {
434-
snapRunNumber = runNum,
435-
snapRunCaching = caching,
436-
snapRunIndex = indexType
437-
} -> do
438-
let sourcePaths = RunFsPaths sourceDir runNum
439-
runNum' <- uniqueToRunNumber <$> incrUniqCounter uc
440-
let targetPaths = RunFsPaths targetDir runNum'
441-
hardLinkRunFiles reg hfs hbio sourcePaths targetPaths
442-
443-
withRollback reg
444-
(Run.openFromDisk hfs hbio caching indexType targetPaths)
445-
releaseRef
446-
447-
{-# SPECIALISE releaseRuns ::
448-
ActionRegistry IO -> SnapLevels (Ref (Run IO h)) -> IO ()
449-
#-}
450-
releaseRuns ::
451-
(MonadMask m, MonadST m)
452-
=> ActionRegistry m -> SnapLevels (Ref (Run m h)) -> m ()
453-
releaseRuns reg = traverse_ $ \r -> delayedCommit reg (releaseRef r)
426+
-> SnapshotRun
427+
-> m (Ref (Run m h))
428+
openRun hfs hbio uc reg
429+
(NamedSnapshotDir sourceDir) (ActiveDir targetDir)
430+
SnapshotRun {
431+
snapRunNumber = runNum,
432+
snapRunCaching = caching,
433+
snapRunIndex = indexType
434+
} = do
435+
let sourcePaths = RunFsPaths sourceDir runNum
436+
runNum' <- uniqueToRunNumber <$> incrUniqCounter uc
437+
let targetPaths = RunFsPaths targetDir runNum'
438+
hardLinkRunFiles reg hfs hbio sourcePaths targetPaths
439+
440+
withRollback reg
441+
(Run.openFromDisk hfs hbio caching indexType targetPaths)
442+
releaseRef
454443

455444
{-------------------------------------------------------------------------------
456445
Opening from levels snapshot format

0 commit comments

Comments
 (0)