@@ -69,6 +69,7 @@ module Database.LSMTree.Model.Session (
6969 , SnapshotName
7070 , createSnapshot
7171 , openSnapshot
72+ , corruptSnapshot
7273 , deleteSnapshot
7374 , listSnapshots
7475 -- * Multiple writable tables
@@ -241,6 +242,7 @@ runModelMWithInjectedErrors (Just _) _ onErrors st =
241242
242243data Err =
243244 ErrTableClosed
245+ | ErrSnapshotCorrupted
244246 | ErrSnapshotExists
245247 | ErrSnapshotDoesNotExist
246248 | ErrSnapshotWrongType
@@ -253,6 +255,8 @@ instance Show Err where
253255 showsPrec d = \ case
254256 ErrTableClosed ->
255257 showString " ErrTableClosed"
258+ ErrSnapshotCorrupted ->
259+ showString " ErrSnapshotCorrupted"
256260 ErrSnapshotExists ->
257261 showString " ErrSnapshotExists"
258262 ErrSnapshotDoesNotExist ->
@@ -270,6 +274,7 @@ instance Show Err where
270274
271275instance Eq Err where
272276 (==) ErrTableClosed ErrTableClosed = True
277+ (==) ErrSnapshotCorrupted ErrSnapshotCorrupted = True
273278 (==) ErrSnapshotExists ErrSnapshotExists = True
274279 (==) ErrSnapshotDoesNotExist ErrSnapshotDoesNotExist = True
275280 (==) ErrSnapshotWrongType ErrSnapshotWrongType = True
@@ -280,6 +285,7 @@ instance Eq Err where
280285 where
281286 _coveredAllCases x = case x of
282287 ErrTableClosed {} -> ()
288+ ErrSnapshotCorrupted {} -> ()
283289 ErrSnapshotExists {} -> ()
284290 ErrSnapshotDoesNotExist {} -> ()
285291 ErrSnapshotWrongType {} -> ()
@@ -540,7 +546,12 @@ invalidateBlobRefs Table{..} = do
540546 Snapshots
541547-------------------------------------------------------------------------------}
542548
543- data Snapshot = Snapshot TableConfig SnapshotLabel SomeTable
549+ data Snapshot = Snapshot
550+ { snapshotConfig :: TableConfig
551+ , snapshotLabel :: SnapshotLabel
552+ , snapshotTable :: SomeTable
553+ , snapshotCorrupted :: Bool
554+ }
544555 deriving stock Show
545556
546557createSnapshot ::
@@ -558,7 +569,7 @@ createSnapshot label name t@Table{..} = do
558569 when (Map. member name snaps) $
559570 throwError ErrSnapshotExists
560571 modify (\ m -> m {
561- snapshots = Map. insert name (Snapshot config label $ toSomeTable $ Model. snapshot table) (snapshots m)
572+ snapshots = Map. insert name (Snapshot config label ( toSomeTable $ Model. snapshot table) False ) (snapshots m)
562573 })
563574
564575openSnapshot ::
@@ -575,7 +586,9 @@ openSnapshot label name = do
575586 case Map. lookup name snaps of
576587 Nothing ->
577588 throwError ErrSnapshotDoesNotExist
578- Just (Snapshot conf label' tbl) -> do
589+ Just (Snapshot conf label' tbl corrupted) -> do
590+ when corrupted $
591+ throwError ErrSnapshotCorrupted
579592 when (label /= label') $
580593 throwError ErrSnapshotWrongType
581594 case fromSomeTable tbl of
@@ -590,6 +603,18 @@ openSnapshot label name = do
590603 Just table' ->
591604 newTableWith conf table'
592605
606+ corruptSnapshot ::
607+ (MonadState Model m , MonadError Err m )
608+ => SnapshotName
609+ -> m ()
610+ corruptSnapshot name = do
611+ snapshots <- gets snapshots
612+ if Map. notMember name snapshots
613+ then throwError ErrSnapshotDoesNotExist
614+ else modify $ \ m -> m {snapshots = Map. adjust corruptSnapshotEntry name snapshots}
615+ where
616+ corruptSnapshotEntry (Snapshot c l t _) = Snapshot c l t True
617+
593618deleteSnapshot ::
594619 (MonadState Model m , MonadError Err m )
595620 => SnapshotName
0 commit comments