Skip to content

Commit 6dbeb6f

Browse files
committed
Remove redundant conversions in the unified model
The unified model can now use the `LookupResult`, `QueryResult` and `Update` types that are defined in the unified public API.
1 parent 6e2cf50 commit 6dbeb6f

File tree

4 files changed

+10
-72
lines changed

4 files changed

+10
-72
lines changed

test/Database/LSMTree/Model/IO.hs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ module Database.LSMTree.Model.IO (
1212
, Cursor (..)
1313
-- * helpers
1414
, runInOpenSession
15-
, convLookupResult
16-
, convQueryResult
17-
, convUpdate
1815
) where
1916

2017
import Control.Concurrent.Class.MonadSTM.Strict
@@ -70,20 +67,20 @@ instance Class.IsTable Table where
7067

7168
new s x = Table s <$> runInOpenSession s (Model.new x)
7269
close (Table s t) = runInOpenSession s (Model.close t)
73-
lookups (Table s t) x1 = fmap convLookupResult . fmap (fmap (BlobRef s)) <$>
70+
lookups (Table s t) x1 = fmap (fmap (BlobRef s)) <$>
7471
runInOpenSession s (Model.lookups x1 t)
75-
updates (Table s t) x1 = runInOpenSession s (Model.updates Model.getResolve (fmap (fmap convUpdate) x1) t)
72+
updates (Table s t) x1 = runInOpenSession s (Model.updates Model.getResolve x1 t)
7673
inserts (Table s t) x1 = runInOpenSession s (Model.inserts Model.getResolve x1 t)
7774
deletes (Table s t) x1 = runInOpenSession s (Model.deletes Model.getResolve x1 t)
7875
mupserts (Table s t) x1 = runInOpenSession s (Model.mupserts Model.getResolve x1 t)
7976

80-
rangeLookup (Table s t) x1 = fmap convQueryResult . fmap (fmap (BlobRef s)) <$>
77+
rangeLookup (Table s t) x1 = fmap (fmap (BlobRef s)) <$>
8178
runInOpenSession s (Model.rangeLookup x1 t)
8279
retrieveBlobs _ s x1 = runInOpenSession s (Model.retrieveBlobs (fmap _brBlobRef x1))
8380

8481
newCursor k (Table s t) = Cursor s <$> runInOpenSession s (Model.newCursor k t)
8582
closeCursor _ (Cursor s c) = runInOpenSession s (Model.closeCursor c)
86-
readCursor _ x1 (Cursor s c) = fmap convQueryResult . fmap (fmap (BlobRef s)) <$>
83+
readCursor _ x1 (Cursor s c) = fmap (fmap (BlobRef s)) <$>
8784
runInOpenSession s (Model.readCursor x1 c)
8885

8986
createSnapshot x1 x2 (Table s t) = runInOpenSession s (Model.createSnapshot x1 x2 t)
@@ -93,20 +90,3 @@ instance Class.IsTable Table where
9390

9491
union (Table s1 t1) (Table _s2 t2) =
9592
Table s1 <$> runInOpenSession s1 (Model.union Model.getResolve t1 t2)
96-
97-
convLookupResult :: Model.LookupResult v b -> Class.LookupResult v b
98-
convLookupResult = \case
99-
Model.NotFound -> Class.NotFound
100-
Model.Found v -> Class.Found v
101-
Model.FoundWithBlob v b -> Class.FoundWithBlob v b
102-
103-
convQueryResult :: Model.QueryResult k v b -> Class.QueryResult k v b
104-
convQueryResult = \case
105-
Model.FoundInQuery k v -> Class.FoundInQuery k v
106-
Model.FoundInQueryWithBlob k v b -> Class.FoundInQueryWithBlob k v b
107-
108-
convUpdate :: Class.Update v b -> Model.Update v b
109-
convUpdate = \case
110-
Class.Insert v b -> Model.Insert v b
111-
Class.Delete -> Model.Delete
112-
Class.Mupsert v -> Model.Mupsert v

test/Database/LSMTree/Model/Table.hs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,45 +52,13 @@ import qualified Data.Map.Strict as Map
5252
import Data.Monoid (First (..))
5353
import Data.Proxy (Proxy (Proxy))
5454
import qualified Data.Vector as V
55+
import Database.LSMTree (LookupResult (..), QueryResult (..),
56+
ResolveValue (..), Update (..))
5557
import Database.LSMTree.Common (Range (..), SerialiseKey (..),
5658
SerialiseValue (..))
5759
import Database.LSMTree.Internal.RawBytes (RawBytes)
58-
import Database.LSMTree.Monoidal (ResolveValue (..))
5960
import GHC.Exts (IsList (..))
6061

61-
data LookupResult v b =
62-
NotFound
63-
| Found !v
64-
| FoundWithBlob !v !b
65-
deriving stock (Eq, Show, Functor, Foldable, Traversable)
66-
67-
instance Bifunctor LookupResult where
68-
first f = \case
69-
NotFound -> NotFound
70-
Found v -> Found (f v)
71-
FoundWithBlob v b -> FoundWithBlob (f v) b
72-
73-
second g = \case
74-
NotFound -> NotFound
75-
Found v -> Found v
76-
FoundWithBlob v b -> FoundWithBlob v (g b)
77-
78-
data QueryResult k v b =
79-
FoundInQuery !k !v
80-
| FoundInQueryWithBlob !k !v !b
81-
deriving stock (Eq, Show, Functor, Foldable, Traversable)
82-
83-
instance Bifunctor (QueryResult k) where
84-
bimap f g = \case
85-
FoundInQuery k v -> FoundInQuery k (f v)
86-
FoundInQueryWithBlob k v b -> FoundInQueryWithBlob k (f v) (g b)
87-
88-
data Update v b =
89-
Insert !v !(Maybe b)
90-
| Delete
91-
| Mupsert !v
92-
deriving stock (Show, Eq)
93-
9462
newtype ResolveSerialisedValue v =
9563
Resolve { resolveSerialised :: RawBytes -> RawBytes -> RawBytes }
9664

test/Test/Database/LSMTree/StateMachine.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -918,10 +918,10 @@ runModel lookUp = \case
918918
wrap MUnit
919919
. Model.runModelM (Model.close (getTable $ lookUp tableVar))
920920
Lookups ks tableVar ->
921-
wrap (MVector . fmap (MLookupResult . fmap MBlobRef . ModelIO.convLookupResult))
921+
wrap (MVector . fmap (MLookupResult . fmap MBlobRef))
922922
. Model.runModelM (Model.lookups ks (getTable $ lookUp tableVar))
923923
RangeLookup range tableVar ->
924-
wrap (MVector . fmap (MQueryResult . fmap MBlobRef . ModelIO.convQueryResult))
924+
wrap (MVector . fmap (MQueryResult . fmap MBlobRef))
925925
. Model.runModelM (Model.rangeLookup range (getTable $ lookUp tableVar))
926926
NewCursor offset tableVar ->
927927
wrap MCursor
@@ -930,11 +930,11 @@ runModel lookUp = \case
930930
wrap MUnit
931931
. Model.runModelM (Model.closeCursor (getCursor $ lookUp cursorVar))
932932
ReadCursor n cursorVar ->
933-
wrap (MVector . fmap (MQueryResult . fmap MBlobRef . ModelIO.convQueryResult))
933+
wrap (MVector . fmap (MQueryResult . fmap MBlobRef))
934934
. Model.runModelM (Model.readCursor n (getCursor $ lookUp cursorVar))
935935
Updates kups tableVar ->
936936
wrap MUnit
937-
. Model.runModelM (Model.updates Model.getResolve (fmap ModelIO.convUpdate <$> kups) (getTable $ lookUp tableVar))
937+
. Model.runModelM (Model.updates Model.getResolve kups (getTable $ lookUp tableVar))
938938
Inserts kins tableVar ->
939939
wrap MUnit
940940
. Model.runModelM (Model.inserts Model.getResolve kins (getTable $ lookUp tableVar))

test/Test/Database/LSMTree/StateMachine/Op.hs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import Control.Monad.Reader (ReaderT)
2020
import Control.Monad.State (StateT)
2121
import qualified Data.Vector as V
2222
import qualified Database.LSMTree.Class as Class
23-
import qualified Database.LSMTree.Model.Table as Model
2423
import GHC.Show (appPrec)
2524
import Test.QuickCheck.StateModel.Lockstep (InterpretOp, Operation)
2625
import qualified Test.QuickCheck.StateModel.Lockstep.Op as Op
@@ -165,12 +164,3 @@ instance HasBlobRef (Class.LookupResult v) where
165164
instance HasBlobRef (Class.QueryResult k v) where
166165
getBlobRef Class.FoundInQuery{} = Nothing
167166
getBlobRef (Class.FoundInQueryWithBlob _ _ blobref) = Just blobref
168-
169-
instance HasBlobRef (Model.LookupResult v) where
170-
getBlobRef Model.NotFound{} = Nothing
171-
getBlobRef Model.Found{} = Nothing
172-
getBlobRef (Model.FoundWithBlob _ blobref) = Just blobref
173-
174-
instance HasBlobRef (Model.QueryResult k v) where
175-
getBlobRef Model.FoundInQuery{} = Nothing
176-
getBlobRef (Model.FoundInQueryWithBlob _ _ blobref) = Just blobref

0 commit comments

Comments
 (0)