@@ -64,7 +64,7 @@ module Database.LSMTree.Internal (
6464 , openSnapshot
6565 , deleteSnapshot
6666 , listSnapshots
67- -- * Mutiple writable tables
67+ -- * Multiple writable tables
6868 , duplicate
6969 -- * Table union
7070 , unions
@@ -323,7 +323,7 @@ data SessionState m h =
323323 | SessionClosed
324324
325325data SessionEnv m h = SessionEnv {
326- -- | The path to the directory in which this sesion is live. This is a path
326+ -- | The path to the directory in which this session is live. This is a path
327327 -- relative to root of the 'HasFS' instance.
328328 --
329329 -- INVARIANT: the session root is never changed during the lifetime of a
@@ -1234,18 +1234,30 @@ createSnapshot snap label tableType t = do
12341234 let wb = tableWriteBuffer content
12351235 let wbb = tableWriteBufferBlobs content
12361236 snapWriteBufferNumber <- Paths. writeBufferNumber <$>
1237- snapshotWriteBuffer reg hfs hbio activeUc snapUc activeDir snapDir wb wbb
1237+ snapshotWriteBuffer hfs hbio activeUc snapUc reg activeDir snapDir wb wbb
12381238
12391239 -- Convert to snapshot format
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
1244+
1245+ -- If a merging tree exists, do the same hard-linking for the runs within
1246+ mTreeOpt <- case tableUnionLevel content of
1247+ NoUnion -> pure Nothing
1248+ Union mTreeRef -> do
1249+ mTree <- toSnapMergingTree mTreeRef
1250+ Just <$> traverse (snapshotRun hfs hbio snapUc reg snapDir) mTree
12441251
1245- -- Release the table content
12461252 releaseTableContent reg content
12471253
1248- let snapMetaData = SnapshotMetaData label tableType (tableConfig t) snapWriteBufferNumber snapLevels'
1254+ let snapMetaData = SnapshotMetaData
1255+ label
1256+ tableType
1257+ (tableConfig t)
1258+ snapWriteBufferNumber
1259+ snapLevels'
1260+ mTreeOpt
12491261 SnapshotMetaDataFile contentPath = Paths. snapshotMetaDataFile snapDir
12501262 SnapshotMetaDataChecksumFile checksumPath = Paths. snapshotMetaDataChecksumFile snapDir
12511263 writeFileSnapshotMetaData hfs contentPath checksumPath snapMetaData
@@ -1290,7 +1302,7 @@ openSnapshot sesh label tableType override snap resolve = do
12901302 Left e -> throwIO (ErrSnapshotDeserialiseFailure e snap)
12911303 Right x -> pure x
12921304
1293- let SnapshotMetaData label' tableType' conf snapWriteBuffer snapLevels = snapMetaData
1305+ let SnapshotMetaData label' tableType' conf snapWriteBuffer snapLevels mTreeOpt = snapMetaData
12941306
12951307 unless (tableType == tableType') $
12961308 throwIO (ErrSnapshotWrongTableType snap tableType tableType')
@@ -1308,19 +1320,26 @@ openSnapshot sesh label tableType override snap resolve = do
13081320 (tableWriteBuffer, tableWriteBufferBlobs) <- openWriteBuffer reg resolve hfs hbio uc activeDir snapWriteBufferPaths
13091321
13101322 -- Hard link runs into the active directory,
1311- snapLevels' <- openRuns reg hfs hbio conf (sessionUniqCounter seshEnv) snapDir activeDir snapLevels
1323+ snapLevels' <- traverse (openRun hfs hbio uc reg snapDir activeDir) snapLevels
1324+ unionLevel <- case mTreeOpt of
1325+ Nothing -> pure NoUnion
1326+ Just mTree -> do
1327+ snapTree <- traverse (openRun hfs hbio uc reg snapDir activeDir) mTree
1328+ mt <- fromSnapMergingTree hfs hbio uc resolve activeDir reg snapTree
1329+ traverse_ (delayedCommit reg . releaseRef) snapTree
1330+ pure (Union mt)
13121331
13131332 -- 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'
1333+ tableLevels <- fromSnapLevels hfs hbio uc conf resolve reg activeDir snapLevels'
1334+ traverse_ (delayedCommit reg . releaseRef) snapLevels'
13161335
13171336 tableCache <- mkLevelsCache reg tableLevels
13181337 newWith reg sesh seshEnv conf' am $! TableContent {
13191338 tableWriteBuffer
13201339 , tableWriteBufferBlobs
13211340 , tableLevels
13221341 , tableCache
1323- , tableUnionLevel = NoUnion -- TODO: at some point also load union level from snapshot
1342+ , tableUnionLevel = unionLevel
13241343 }
13251344
13261345{-# SPECIALISE deleteSnapshot ::
@@ -1370,7 +1389,7 @@ listSnapshots sesh = do
13701389 else pure $ Nothing
13711390
13721391{- ------------------------------------------------------------------------------
1373- Mutiple writable tables
1392+ Multiple writable tables
13741393-------------------------------------------------------------------------------}
13751394
13761395{-# SPECIALISE duplicate :: Table IO h -> IO (Table IO h) #-}
@@ -1534,28 +1553,19 @@ writeBufferToNewRun SessionEnv {
15341553 sessionHasBlockIO = hbio,
15351554 sessionUniqCounter = uc
15361555 }
1537- conf@ TableConfig {
1538- confDiskCachePolicy,
1539- confFencePointerIndex
1540- }
1556+ conf
15411557 TableContent {
15421558 tableWriteBuffer,
15431559 tableWriteBufferBlobs
15441560 }
15451561 | WB. null tableWriteBuffer = pure Nothing
15461562 | otherwise = Just <$> do
1547- ! n <- incrUniqCounter uc
1548- let ! ln = LevelNo 1
1549- ! cache = diskCachePolicyForLevel confDiskCachePolicy ln
1550- ! alloc = bloomFilterAllocForLevel conf ln
1551- ! indexType = indexTypeForRun confFencePointerIndex
1552- ! path = Paths. runPath (Paths. activeDir root)
1553- (uniqueToRunNumber n)
1554- Run. fromWriteBuffer hfs hbio
1555- cache
1556- alloc
1557- indexType
1558- path
1563+ ! uniq <- incrUniqCounter uc
1564+ let (! runParams, ! runPaths) = mergingRunParamsForLevel
1565+ (Paths. activeDir root) conf uniq (LevelNo 1 )
1566+ Run. fromWriteBuffer
1567+ hfs hbio
1568+ runParams runPaths
15591569 tableWriteBuffer
15601570 tableWriteBufferBlobs
15611571
0 commit comments