Skip to content

Commit 10c5d4c

Browse files
committed
prototype: make more type safe
1 parent 9ab54c6 commit 10c5d4c

File tree

3 files changed

+54
-27
lines changed

3 files changed

+54
-27
lines changed

prototypes/ScheduledMerges.hs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
module ScheduledMerges (
2020
-- * Main API
2121
LSM,
22-
Key, Value, Blob,
22+
Key (K), Value (V), resolveValue, Blob (B),
2323
new,
2424
LookupResult (..),
2525
lookup, lookups,
@@ -122,12 +122,20 @@ runSize = Map.size
122122
bufferSize :: Buffer -> Int
123123
bufferSize = Map.size
124124

125-
type Op = Update Value Blob
125+
type Op = Update Value Blob
126126

127-
type Key = Int
128-
type Value = Int
129-
type Blob = Int
127+
newtype Key = K Int
128+
deriving stock (Eq, Ord, Show)
129+
deriving newtype Enum
130130

131+
newtype Value = V Int
132+
deriving stock (Eq, Show)
133+
134+
resolveValue :: Value -> Value -> Value
135+
resolveValue (V x) (V y) = V (x + y)
136+
137+
newtype Blob = B Int
138+
deriving stock (Eq, Show)
131139

132140
-- | The size of the 4 tiering runs at each level are allowed to be:
133141
-- @4^(level-1) < size <= 4^level@
@@ -425,6 +433,7 @@ new = do
425433
inserts :: Tracer (ST s) Event -> LSM s -> [(Key, Value)] -> ST s ()
426434
inserts tr lsm kvs = updates tr lsm [ (k, Insert v Nothing) | (k,v) <- kvs ]
427435

436+
-- TODO: support (and test!) blobs
428437
insert :: Tracer (ST s) Event -> LSM s -> Key -> Value -> ST s ()
429438
insert tr lsm k v = update tr lsm k (Insert v Nothing)
430439

prototypes/ScheduledMergesTest.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ test_regression_empty_run =
2525
runWithTracer $ \tracer -> do
2626
stToIO $ do
2727
lsm <- LSM.new
28-
let ins k = LSM.insert tracer lsm k 0
29-
let del k = LSM.delete tracer lsm k
28+
let ins k = LSM.insert tracer lsm (K k) (V 0)
29+
let del k = LSM.delete tracer lsm (K k)
3030
-- run 1
3131
ins 0
3232
ins 1
@@ -80,7 +80,7 @@ test_merge_again_with_incoming =
8080
runWithTracer $ \tracer -> do
8181
stToIO $ do
8282
lsm <- LSM.new
83-
let ins k = LSM.insert tracer lsm k 0
83+
let ins k = LSM.insert tracer lsm (K k) (V 0)
8484
-- get something to 3rd level (so 2nd level is not levelling)
8585
-- (needs 5 runs to go to level 2 so the resulting run becomes too big)
8686
traverse_ ins [101..100+(5*16)]

prototypes/ScheduledMergesTestQLS.hs

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{-# LANGUAGE TypeFamilies #-}
22

3+
{-# OPTIONS_GHC -Wno-orphans #-}
4+
35
module ScheduledMergesTestQLS (tests) where
46

57
import Prelude hiding (lookup)
@@ -144,44 +146,44 @@ instance InLockstep Model where
144146
case findVars ctx (Proxy :: Proxy (LSM RealWorld)) of
145147
[] -> return (Some ANew)
146148
vars ->
147-
frequency $
148-
-- inserts of potentially fresh keys
149+
let kvars = findVars ctx (Proxy :: Proxy Key)
150+
existingKey = Left <$> elements kvars
151+
freshKey = Right <$> arbitrary @Key
152+
in frequency $
153+
-- inserts of potentially fresh keys
149154
[ (3, fmap Some $
150155
AInsert <$> elements vars
151-
<*> fmap Right arbitrarySizedNatural -- key
152-
<*> arbitrarySizedNatural) -- value
156+
<*> freshKey
157+
<*> arbitrary @Value)
153158
]
154-
-- inserts of the same keys as used earlier
159+
-- inserts of the same keys as used earlier
155160
++ [ (1, fmap Some $
156161
AInsert <$> elements vars
157-
<*> fmap Left (elements kvars) -- key var
158-
<*> arbitrarySizedNatural) -- value
159-
| let kvars = findVars ctx (Proxy :: Proxy Key)
160-
, not (null kvars)
162+
<*> existingKey
163+
<*> arbitrary @Value)
164+
| not (null kvars)
161165
]
162166
-- deletes of arbitrary keys:
163167
++ [ (1, fmap Some $
164168
ADelete <$> elements vars
165-
<*> fmap Right arbitrarySizedNatural) -- key value
169+
<*> freshKey)
166170
]
167171
-- deletes of the same key as inserted earlier:
168172
++ [ (1, fmap Some $
169173
ADelete <$> elements vars
170-
<*> fmap Left (elements kvars)) -- key var
171-
| let kvars = findVars ctx (Proxy :: Proxy Key)
172-
, not (null kvars)
174+
<*> existingKey)
175+
| not (null kvars)
173176
]
174177
-- lookup of arbitrary keys:
175178
++ [ (1, fmap Some $
176179
ALookup <$> elements vars
177-
<*> fmap Right arbitrarySizedNatural) -- key value
180+
<*> freshKey)
178181
]
179182
-- lookup of the same key as inserted earlier:
180183
++ [ (3, fmap Some $
181184
ALookup <$> elements vars
182-
<*> fmap Left (elements kvars)) -- key var
183-
| let kvars = findVars ctx (Proxy :: Proxy Key)
184-
, not (null kvars)
185+
<*> existingKey)
186+
| not (null kvars)
185187
]
186188
++ [ (1, fmap Some $
187189
ADump <$> elements vars)
@@ -194,13 +196,13 @@ instance InLockstep Model where
194196
[ Some $ AInsert var (Right k') v' | (k', v') <- shrink (k, v) ]
195197

196198
shrinkWithVars _ctx _model (AInsert var (Left _kv) v) =
197-
[ Some $ AInsert var (Right k) v | k <- shrink 100 ]
199+
[ Some $ AInsert var (Right k) v | k <- shrink (K 100) ]
198200

199201
shrinkWithVars _ctx _model (ADelete var (Right k)) =
200202
[ Some $ ADelete var (Right k') | k' <- shrink k ]
201203

202204
shrinkWithVars _ctx _model (ADelete var (Left _kv)) =
203-
[ Some $ ADelete var (Right k) | k <- shrink 100 ]
205+
[ Some $ ADelete var (Right k) | k <- shrink (K 100) ]
204206

205207
shrinkWithVars _ctx _model _action = []
206208

@@ -289,3 +291,19 @@ runModel action ctx m =
289291

290292
lookUpKeyVar :: ModelVar Model Key -> Key
291293
lookUpKeyVar var = case lookupVar ctx var of MInsert k -> k
294+
295+
-------------------------------------------------------------------------------
296+
-- Instances
297+
--
298+
299+
instance Arbitrary Key where
300+
arbitrary = K <$> arbitrarySizedNatural
301+
shrink (K v) = K <$> shrink v
302+
303+
instance Arbitrary Value where
304+
arbitrary = V <$> arbitrarySizedNatural
305+
shrink (V v) = V <$> shrink v
306+
307+
instance Arbitrary Blob where
308+
arbitrary = B <$> arbitrarySizedNatural
309+
shrink (B v) = B <$> shrink v

0 commit comments

Comments
 (0)