Skip to content

Commit d4e7ada

Browse files
authored
Merge pull request #653 from IntersectMBO/wenkokke/simple
doc: add Database.LSMTree.Simple
2 parents 1a6ad4c + a51e90d commit d4e7ada

File tree

10 files changed

+1222
-55
lines changed

10 files changed

+1222
-55
lines changed

lsm-tree.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ library
286286
Database.LSMTree.Internal.WriteBufferWriter
287287
Database.LSMTree.Monoidal
288288
Database.LSMTree.Normal
289+
Database.LSMTree.Simple
289290

290291
build-depends:
291292
, base >=4.14 && <4.22

scripts/test-cabal-docspec.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ if [ "${SKIP_CABAL_BUILD}" = "" ]; then
2020
fi
2121
fi
2222
cabal-docspec \
23+
--extra-package directory \
2324
--extra-package lsm-tree:prototypes \
24-
-XNumericUnderscores \
2525
-XOverloadedStrings \
26-
-XTypeApplications
26+
-XNumericUnderscores \
27+
-XTypeApplications \
28+
-XScopedTypeVariables

src-control/Control/ActionRegistry.hs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module Control.ActionRegistry (
2727
, AbortActionRegistryError (..)
2828
, AbortActionRegistryReason (..)
2929
, getReasonExitCaseException
30+
, mapExceptionWithActionRegistry
3031
-- * Registering actions #registeringActions#
3132
-- $registering-actions
3233
, withRollback
@@ -42,6 +43,8 @@ import Control.Monad.Primitive
4243
import Data.Kind
4344
import Data.List.NonEmpty (NonEmpty (..))
4445
import qualified Data.List.NonEmpty as NE
46+
import Data.Maybe (fromMaybe)
47+
import Data.Monoid (First (..))
4548
import Data.Primitive.MutVar
4649

4750
#ifdef NO_IGNORE_ASSERTS
@@ -408,6 +411,46 @@ runActions = go []
408411
Left e -> go (mkActionError e a : es) as
409412
Right _ -> go es as
410413

414+
{-# SPECIALISE mapExceptionWithActionRegistry ::
415+
(Exception e1, Exception e2)
416+
=> (e1 -> e2)
417+
-> IO a
418+
-> IO a #-}
419+
-- | As 'Control.Exception.mapException', but aware of the structure of
420+
-- 'AbortActionRegistryError' and 'CommitActionRegistryError'.
421+
mapExceptionWithActionRegistry ::
422+
(Exception e1, Exception e2, MonadCatch m)
423+
=> (e1 -> e2)
424+
-> m a
425+
-> m a
426+
mapExceptionWithActionRegistry f action = action `catch` (throwIO . mapSomeException)
427+
where
428+
-- TODO: This erases the `ExceptionContext` of the underlying exception.
429+
-- Unfortunately, the API exposed by `io-classes` does not currently
430+
-- have the primitives to preserve the `ExceptionContext`.
431+
mapSomeException :: SomeException -> SomeException
432+
mapSomeException e =
433+
fromMaybe e . getFirst . mconcat . fmap First $
434+
[ toException . f <$> fromException e
435+
, toException . mapAbortActionRegistryError <$> fromException e
436+
, toException . mapCommitActionRegistryError <$> fromException e
437+
]
438+
439+
mapAbortActionRegistryError :: AbortActionRegistryError -> AbortActionRegistryError
440+
mapAbortActionRegistryError = \case
441+
AbortActionRegistryError reason es ->
442+
AbortActionRegistryError (mapAbortActionRegistryReason reason) (mapActionError mapSomeException <$> es)
443+
444+
mapAbortActionRegistryReason :: AbortActionRegistryReason -> AbortActionRegistryReason
445+
mapAbortActionRegistryReason = \case
446+
ReasonExitCaseException e -> ReasonExitCaseException (mapSomeException e)
447+
ReasonExitCaseAbort -> ReasonExitCaseAbort
448+
449+
mapCommitActionRegistryError :: CommitActionRegistryError -> CommitActionRegistryError
450+
mapCommitActionRegistryError = \case
451+
CommitActionRegistryError es ->
452+
CommitActionRegistryError (mapActionError mapSomeException <$> es)
453+
411454
{-------------------------------------------------------------------------------
412455
Registering actions
413456
-------------------------------------------------------------------------------}

src/Database/LSMTree/Internal.hs

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ import Data.List.NonEmpty (NonEmpty (..))
111111
import qualified Data.List.NonEmpty as NE
112112
import Data.Map.Strict (Map)
113113
import qualified Data.Map.Strict as Map
114-
import Data.Maybe (catMaybes, fromMaybe, maybeToList)
115-
import Data.Monoid (First (..))
114+
import Data.Maybe (catMaybes, maybeToList)
116115
import qualified Data.Set as Set
117116
import Data.Typeable
118117
import qualified Data.Vector as V
@@ -1408,45 +1407,8 @@ wrapFileCorruptedErrorAsSnapshotCorruptedError ::
14081407
=> SnapshotName
14091408
-> m a
14101409
-> m a
1411-
wrapFileCorruptedErrorAsSnapshotCorruptedError snapshotName action =
1412-
action `catches` handlers
1413-
where
1414-
handlers :: [Handler m a]
1415-
handlers =
1416-
[ Handler $ throwIO . wrapFileCorruptedError
1417-
, Handler $ throwIO . wrapAbortActionRegistryError
1418-
, Handler $ throwIO . wrapCommitActionRegistryError
1419-
]
1420-
1421-
-- TODO: This erases the `ExceptionContext` of the underlying `FileCorruptedError`,
1422-
-- `AbortActionRegistryError`, or `CommitActionRegistryError`.
1423-
-- Unfortunately, the API exposed by `io-classes` does not currently expose
1424-
-- any primitives that could be used to preserve the `ExceptionContext`.
1425-
wrapSomeException :: SomeException -> SomeException
1426-
wrapSomeException e =
1427-
fromMaybe e . getFirst . mconcat . fmap First $
1428-
[ toException . wrapFileCorruptedError <$> fromException e
1429-
, toException . wrapAbortActionRegistryError <$> fromException e
1430-
, toException . wrapCommitActionRegistryError <$> fromException e
1431-
]
1432-
1433-
wrapFileCorruptedError :: FileCorruptedError -> SnapshotCorruptedError
1434-
wrapFileCorruptedError = ErrSnapshotCorrupted snapshotName
1435-
1436-
wrapAbortActionRegistryError :: AbortActionRegistryError -> AbortActionRegistryError
1437-
wrapAbortActionRegistryError = \case
1438-
AbortActionRegistryError reason es ->
1439-
AbortActionRegistryError (wrapAbortActionRegistryReason reason) (mapActionError wrapSomeException <$> es)
1440-
1441-
wrapAbortActionRegistryReason :: AbortActionRegistryReason -> AbortActionRegistryReason
1442-
wrapAbortActionRegistryReason = \case
1443-
ReasonExitCaseException e -> ReasonExitCaseException (wrapSomeException e)
1444-
ReasonExitCaseAbort -> ReasonExitCaseAbort
1445-
1446-
wrapCommitActionRegistryError :: CommitActionRegistryError -> CommitActionRegistryError
1447-
wrapCommitActionRegistryError = \case
1448-
CommitActionRegistryError es ->
1449-
CommitActionRegistryError (mapActionError wrapSomeException <$> es)
1410+
wrapFileCorruptedErrorAsSnapshotCorruptedError snapshotName =
1411+
mapExceptionWithActionRegistry (ErrSnapshotCorrupted snapshotName)
14501412

14511413
{-# SPECIALISE doesSnapshotExist ::
14521414
Session IO h

src/Database/LSMTree/Internal/Snapshot.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,18 @@ newtype SnapshotLabel = SnapshotLabel Text
9595
deriving newtype (NFData, IsString)
9696

9797
-- TODO: revisit if we need three table types.
98-
data SnapshotTableType = SnapNormalTable | SnapMonoidalTable | SnapFullTable
98+
data SnapshotTableType
99+
= SnapSimpleTable
100+
| SnapFullTable
101+
| SnapNormalTable
102+
| SnapMonoidalTable
99103
deriving stock (Eq, Show)
100104

101105
instance NFData SnapshotTableType where
106+
rnf SnapSimpleTable = ()
107+
rnf SnapFullTable = ()
102108
rnf SnapNormalTable = ()
103109
rnf SnapMonoidalTable = ()
104-
rnf SnapFullTable = ()
105110

106111
data SnapshotMetaData = SnapshotMetaData {
107112
-- | See 'SnapshotLabel'.

src/Database/LSMTree/Internal/Snapshot/Codec.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,17 +247,19 @@ instance DecodeVersioned SnapshotLabel where
247247
-- TableType
248248

249249
instance Encode SnapshotTableType where
250-
encode SnapNormalTable = encodeWord 0
251-
encode SnapMonoidalTable = encodeWord 1
252-
encode SnapFullTable = encodeWord 2
250+
encode SnapSimpleTable = encodeWord 0
251+
encode SnapFullTable = encodeWord 1
252+
encode SnapNormalTable = encodeWord 2
253+
encode SnapMonoidalTable = encodeWord 3
253254

254255
instance DecodeVersioned SnapshotTableType where
255256
decodeVersioned V0 = do
256257
tag <- decodeWord
257258
case tag of
258-
0 -> pure SnapNormalTable
259-
1 -> pure SnapMonoidalTable
260-
2 -> pure SnapFullTable
259+
0 -> pure SnapSimpleTable
260+
1 -> pure SnapFullTable
261+
2 -> pure SnapNormalTable
262+
3 -> pure SnapMonoidalTable
261263
_ -> fail ("[SnapshotTableType] Unexpected tag: " <> show tag)
262264

263265
instance Encode SnapshotRun where

0 commit comments

Comments
 (0)