Skip to content

Commit 620bf52

Browse files
committed
Clear the active directory on session restore
1 parent 7f80e9a commit 620bf52

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

src/Database/LSMTree/Internal.hs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ import Control.Monad.Primitive
8080
import Control.TempRegistry
8181
import Control.Tracer
8282
import Data.Arena (ArenaManager, newArenaManager)
83-
import Data.Char (isNumber)
8483
import Data.Foldable
8584
import Data.Functor.Compose (Compose (..))
8685
import Data.Kind
@@ -405,8 +404,8 @@ openSession tr hfs hbio dir = do
405404

406405
releaseLock lockFile = forM_ (Compose lockFile) $ \lockFile' -> FS.hUnlock lockFile'
407406

408-
mkSession lockFile x = do
409-
counterVar <- newUniqCounter x
407+
mkSession lockFile = do
408+
counterVar <- newUniqCounter 0
410409
openTablesVar <- newMVar Map.empty
411410
openCursorsVar <- newMVar Map.empty
412411
sessionVar <- RW.new $ SessionOpen $ SessionEnv {
@@ -424,29 +423,20 @@ openSession tr hfs hbio dir = do
424423
traceWith tr TraceNewSession
425424
FS.createDirectory hfs activeDirPath
426425
FS.createDirectory hfs snapshotsDirPath
427-
mkSession sessionFileLock 0
426+
mkSession sessionFileLock
428427

429428
restoreSession sessionFileLock = do
430429
traceWith tr TraceRestoreSession
431430
-- If the layouts are wrong, we throw an exception, and the lock file
432431
-- is automatically released by bracketOnError.
433432
checkTopLevelDirLayout
433+
434+
FS.removeDirectoryRecursive hfs activeDirPath -- TODO: exceptions safety
435+
FS.createDirectory hfs activeDirPath
436+
434437
checkActiveDirLayout
435438
checkSnapshotsDirLayout
436-
-- TODO: remove once we have proper snapshotting. Before that, we must
437-
-- prevent name clashes with runs that are still present in the active
438-
-- directory by starting the unique counter at a strictly higher number
439-
-- than the name of any run in the active directory. When we do
440-
-- snapshoting properly, then we'll hard link files into the active
441-
-- directory under new names/numbers, and so session counters will
442-
-- always be able to start at 0.
443-
files <- FS.listDirectory hfs activeDirPath
444-
let (x :: Int) | Set.null files = 0
445-
-- TODO: read is not very robust, but it is only a
446-
-- temporary solution
447-
| otherwise = maximum [ read (takeWhile isNumber f)
448-
| f <- Set.toList files ]
449-
mkSession sessionFileLock (fromIntegral x)
439+
mkSession sessionFileLock
450440

451441
-- Check that the active directory and snapshots directory exist. We assume
452442
-- the lock file already exists at this point.
@@ -460,12 +450,10 @@ openSession tr hfs hbio dir = do
460450
FS.doesDirectoryExist hfs snapshotsDirPath >>= \b ->
461451
unless b $ throwIO (SessionDirMalformed (FS.mkFsErrorPath hfs snapshotsDirPath))
462452

463-
-- Nothing to check: runs are verified when loading a table, not when
464-
-- a session is restored.
465-
--
466-
-- TODO: when we implement proper snapshotting, the files in the active
467-
-- directory should be ignored and cleaned up.
468-
checkActiveDirLayout = pure ()
453+
-- The active directory should be empty
454+
checkActiveDirLayout = do
455+
contents <- FS.listDirectory hfs activeDirPath
456+
unless (Set.null contents) $ throwIO (SessionDirMalformed (FS.mkFsErrorPath hfs activeDirPath))
469457

470458
-- Nothing to check: snapshots are verified when they are loaded, not when a
471459
-- session is restored.

0 commit comments

Comments
 (0)