Skip to content

Commit 081628f

Browse files
committed
Put the IsSession class in its own module
1 parent a2a03c6 commit 081628f

File tree

4 files changed

+82
-77
lines changed

4 files changed

+82
-77
lines changed

lsm-tree.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ test-suite lsm-tree-test
343343
hs-source-dirs: test
344344
main-is: Main.hs
345345
other-modules:
346+
Database.LSMTree.Class.Common
346347
Database.LSMTree.Class.Monoidal
347348
Database.LSMTree.Class.Normal
348349
Database.LSMTree.Model
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{-# LANGUAGE TypeFamilies #-}
2+
3+
module Database.LSMTree.Class.Common (
4+
C
5+
, C_
6+
, IsSession (..)
7+
, SessionArgs (..)
8+
, withSession
9+
, module Types
10+
) where
11+
12+
import Control.Monad.Class.MonadThrow (MonadThrow (..))
13+
import Control.Tracer (nullTracer)
14+
import Data.Kind (Constraint, Type)
15+
import Data.Typeable (Typeable)
16+
import Database.LSMTree.Common as Types (IOLike, Range (..),
17+
SerialiseKey, SerialiseValue, SnapshotLabel (..),
18+
SnapshotName)
19+
import qualified Database.LSMTree.Common as R
20+
import System.FS.API (FsPath, HasFS)
21+
import System.FS.BlockIO.API (HasBlockIO)
22+
23+
-- | Model-specific constraints
24+
type C k v blob = (C_ k, C_ v, C_ blob)
25+
type C_ a = (Show a, Eq a, Typeable a)
26+
27+
-- | Class abstracting over session operations.
28+
--
29+
type IsSession :: ((Type -> Type) -> Type) -> Constraint
30+
class IsSession s where
31+
data SessionArgs s :: (Type -> Type) -> Type
32+
33+
openSession ::
34+
IOLike m
35+
=> SessionArgs s m
36+
-> m (s m)
37+
38+
closeSession ::
39+
IOLike m
40+
=> s m
41+
-> m ()
42+
43+
deleteSnapshot ::
44+
IOLike m
45+
=> s m
46+
-> SnapshotName
47+
-> m ()
48+
49+
listSnapshots ::
50+
IOLike m
51+
=> s m
52+
-> m [SnapshotName]
53+
54+
withSession :: (IOLike m, IsSession s) => SessionArgs s m -> (s m -> m a) -> m a
55+
withSession seshArgs = bracket (openSession seshArgs) closeSession
56+
57+
{-------------------------------------------------------------------------------
58+
Real instance
59+
-------------------------------------------------------------------------------}
60+
61+
instance IsSession R.Session where
62+
data SessionArgs R.Session m where
63+
SessionArgs ::
64+
forall m h. Typeable h
65+
=> HasFS m h -> HasBlockIO m h -> FsPath
66+
-> SessionArgs R.Session m
67+
68+
openSession (SessionArgs hfs hbio dir) = do
69+
R.openSession nullTracer hfs hbio dir
70+
closeSession = R.closeSession
71+
deleteSnapshot = R.deleteSnapshot
72+
listSnapshots = R.listSnapshots

test/Database/LSMTree/Class/Monoidal.hs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,26 @@
33
-- | An abstraction of the monoidal LSM API, instantiated by both the real
44
-- implementation and a model (see "Database.LSMTree.Model.IO.Monoidal").
55
module Database.LSMTree.Class.Monoidal (
6-
C
7-
, C_
8-
, IsSession (..)
9-
, SessionArgs (..)
10-
, withSession
11-
, IsTable (..)
6+
IsTable (..)
127
, withTableNew
138
, withTableFromSnapshot
149
, withTableDuplicate
1510
, withTableUnion
1611
, withCursor
12+
, module Common
1713
, module Types
1814
) where
1915

2016
import Control.Monad.Class.MonadThrow (MonadThrow (..))
2117
import Data.Kind (Constraint, Type)
22-
import Data.Typeable (Proxy (Proxy), Typeable)
18+
import Data.Typeable (Proxy (..))
2319
import qualified Data.Vector as V
2420
import Data.Void (Void)
25-
import Database.LSMTree.Class.Normal (IsSession (..),
26-
SessionArgs (..), withSession)
27-
import Database.LSMTree.Common as Types (IOLike, Range (..),
28-
SerialiseKey, SerialiseValue, SnapshotLabel (..),
29-
SnapshotName)
21+
import Database.LSMTree.Class.Common as Common
3022
import Database.LSMTree.Monoidal as Types (LookupResult (..),
3123
QueryResult (..), ResolveValue, Update (..))
3224
import qualified Database.LSMTree.Monoidal as R
3325

34-
-- | Model-specific constraints
35-
type C k v blob = (C_ k, C_ v, C_ blob)
36-
type C_ a = (Show a, Eq a, Typeable a)
37-
3826
-- | Class abstracting over table operations.
3927
--
4028
type IsTable :: ((Type -> Type) -> Type -> Type -> Type) -> Constraint

test/Database/LSMTree/Class/Normal.hs

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,23 @@
33
-- | An abstraction of the normal LSM API, instantiated by both the real
44
-- implementation and a model (see "Database.LSMTree.Model.IO.Normal").
55
module Database.LSMTree.Class.Normal (
6-
C
7-
, C_
8-
, IsSession (..)
9-
, SessionArgs (..)
10-
, withSession
11-
, IsTable (..)
6+
IsTable (..)
127
, withTableNew
138
, withTableFromSnapshot
149
, withTableDuplicate
1510
, withCursor
11+
, module Common
1612
, module Types
1713
) where
1814

19-
import Control.Monad.Class.MonadThrow (bracket)
20-
import Control.Tracer (nullTracer)
15+
import Control.Monad.Class.MonadThrow (MonadThrow (..))
2116
import Data.Kind (Constraint, Type)
22-
import Data.Typeable (Proxy (Proxy), Typeable)
17+
import Data.Typeable (Proxy (..))
2318
import qualified Data.Vector as V
24-
import Database.LSMTree.Common as Types (IOLike, Range (..),
25-
SerialiseKey, SerialiseValue, SnapshotLabel (..),
26-
SnapshotName)
19+
import Database.LSMTree.Class.Common as Common
2720
import Database.LSMTree.Normal as Types (LookupResult (..),
2821
QueryResult (..), Update (..))
2922
import qualified Database.LSMTree.Normal as R
30-
import System.FS.API (FsPath, HasFS)
31-
import System.FS.BlockIO.API (HasBlockIO)
32-
33-
-- | Model-specific constraints
34-
type C k v blob = (C_ k, C_ v, C_ blob)
35-
type C_ a = (Show a, Eq a, Typeable a)
36-
37-
-- | Class abstracting over session operations.
38-
--
39-
type IsSession :: ((Type -> Type) -> Type) -> Constraint
40-
class IsSession s where
41-
data SessionArgs s :: (Type -> Type) -> Type
42-
43-
openSession ::
44-
IOLike m
45-
=> SessionArgs s m
46-
-> m (s m)
47-
48-
closeSession ::
49-
IOLike m
50-
=> s m
51-
-> m ()
52-
53-
deleteSnapshot ::
54-
IOLike m
55-
=> s m
56-
-> SnapshotName
57-
-> m ()
58-
59-
listSnapshots ::
60-
IOLike m
61-
=> s m
62-
-> m [SnapshotName]
63-
64-
withSession :: (IOLike m, IsSession s) => SessionArgs s m -> (s m -> m a) -> m a
65-
withSession seshArgs = bracket (openSession seshArgs) closeSession
6623

6724
-- | Class abstracting over table operations.
6825
--
@@ -268,19 +225,6 @@ withCursor offset hdl = bracket (newCursor offset hdl) (closeCursor (Proxy @h))
268225
Real instance
269226
-------------------------------------------------------------------------------}
270227

271-
instance IsSession R.Session where
272-
data SessionArgs R.Session m where
273-
SessionArgs ::
274-
forall m h. Typeable h
275-
=> HasFS m h -> HasBlockIO m h -> FsPath
276-
-> SessionArgs R.Session m
277-
278-
openSession (SessionArgs hfs hbio dir) = do
279-
R.openSession nullTracer hfs hbio dir
280-
closeSession = R.closeSession
281-
deleteSnapshot = R.deleteSnapshot
282-
listSnapshots = R.listSnapshots
283-
284228
instance IsTable R.Table where
285229
type Session R.Table = R.Session
286230
type TableConfig R.Table = R.TableConfig

0 commit comments

Comments
 (0)