Skip to content

Commit 638e82e

Browse files
committed
Prototype: add UnionDebt and UnionCredits newtypes, update documentation
1 parent ec24627 commit 638e82e

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

prototypes/ScheduledMerges.hs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ module ScheduledMerges (
6767
NominalDebt(..),
6868
Run,
6969
runSize,
70+
UnionCredits (..),
7071
supplyCreditsMergingTree,
72+
UnionDebt(..),
7173
remainingDebtMergingTree,
7274
mergek,
7375
mergeBatchSize,
@@ -840,25 +842,47 @@ unions lsms = do
840842
c <- newSTRef 0
841843
return (LSMHandle c lsmr)
842844

843-
-- | An upper bound on the number of merging steps that need to be performed
844-
-- until the delayed work of performing a 'union' is completed. This debt can
845-
-- be paid off using 'supplyUnionCredits'. This includes the cost for existing
846-
-- merges that were part of the union's input tables.
847-
remainingUnionDebt :: LSM s -> ST s Debt
845+
-- | The /current/ upper bound on the number of 'UnionCredits' that have to be
846+
-- supplied before a 'union' is completed.
847+
--
848+
-- The union debt is the number of merging steps that need to be performed /at
849+
-- most/ until the delayed work of performing a 'union' is completed. This
850+
-- includes the cost of completing merges that were part of the union's input
851+
-- tables.
852+
newtype UnionDebt = UnionDebt Debt
853+
deriving stock (Show, Eq, Ord)
854+
deriving newtype Num
855+
856+
-- | Return the current union debt. This debt can be reduced until it is paid
857+
-- off using 'supplyUnionCredits'.
858+
remainingUnionDebt :: LSM s -> ST s UnionDebt
848859
remainingUnionDebt (LSMHandle _ lsmr) = do
849860
LSMContent _ _ ul <- readSTRef lsmr
850-
case ul of
861+
UnionDebt <$> case ul of
851862
NoUnion -> return 0
852863
Union tree d -> checkedUnionDebt tree d
853864

854-
-- | Supplying credits leads to union merging work being performed in batches.
855-
-- This reduces the debt returned by 'remainingUnionDebt'.
856-
supplyUnionCredits :: LSM s -> Credit -> ST s Credit
857-
supplyUnionCredits (LSMHandle scr lsmr) credits
858-
| credits <= 0 = return 0
865+
-- | Credits are used to pay off 'UnionDebt', completing a 'union' in the
866+
-- process.
867+
--
868+
-- A union credit corresponds to a single merging step being performed.
869+
newtype UnionCredits = UnionCredits Credit
870+
deriving stock (Show, Eq, Ord)
871+
deriving newtype Num
872+
873+
-- | Supply union credits to reduce union debt.
874+
--
875+
-- Supplying union credits leads to union merging work being performed in
876+
-- batches. This reduces the union debt returned by 'remainingUnionDebt'. Union
877+
-- debt will be reduced by /at least/ the number of supplied union credits. It
878+
-- is therefore advisable to query 'remainingUnionDebt' every once in a while to
879+
-- see what the current debt is.
880+
supplyUnionCredits :: LSM s -> UnionCredits -> ST s UnionCredits
881+
supplyUnionCredits (LSMHandle scr lsmr) (UnionCredits credits)
882+
| credits <= 0 = return (UnionCredits 0)
859883
| otherwise = do
860884
content@(LSMContent _ _ ul) <- readSTRef lsmr
861-
case ul of
885+
UnionCredits <$> case ul of
862886
NoUnion ->
863887
return credits
864888
Union tree debtRef -> do

prototypes/ScheduledMergesTest.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,14 @@ prop_union kopss = length (filter (not . null) kopss) > 1 QC.==>
184184
ts <- traverse (mkTable tr) kopss
185185
t <- LSM.unions ts
186186

187-
debt <- LSM.remainingUnionDebt t
188-
_ <- LSM.supplyUnionCredits t debt
187+
debt@(UnionDebt x) <- LSM.remainingUnionDebt t
188+
_ <- LSM.supplyUnionCredits t (UnionCredits x)
189189
debt' <- LSM.remainingUnionDebt t
190190

191191
rep <- dumpRepresentation t
192192
return $ QC.counterexample (show (debt, debt')) $ QC.conjoin
193-
[ debt =/= 0
194-
, debt' === 0
193+
[ debt =/= UnionDebt 0
194+
, debt' === UnionDebt 0
195195
, hasUnionWith isCompleted rep
196196
]
197197
where

prototypes/ScheduledMergesTestQLS.hs

Lines changed: 5 additions & 2 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 Control.Monad.ST
@@ -73,7 +75,7 @@ modelMupsert :: ModelLSM -> Key -> Value -> ModelOp ()
7375
modelLookup :: ModelLSM -> Key -> ModelOp (LookupResult Value Blob)
7476
modelDuplicate :: ModelLSM -> ModelOp ModelLSM
7577
modelUnions :: [ModelLSM] -> ModelOp ModelLSM
76-
modelSupplyUnion :: ModelLSM -> NonNegative Credit -> ModelOp ()
78+
modelSupplyUnion :: ModelLSM -> NonNegative UnionCredits -> ModelOp ()
7779
modelDump :: ModelLSM -> ModelOp (Map Key (Value, Maybe Blob))
7880

7981
modelNew Model {mlsms} =
@@ -150,7 +152,7 @@ instance StateModel (Lockstep Model) where
150152
-> Action (Lockstep Model) (LSM RealWorld)
151153

152154
ASupplyUnion :: ModelVar Model (LSM RealWorld)
153-
-> NonNegative Credit
155+
-> NonNegative UnionCredits
154156
-> Action (Lockstep Model) ()
155157

156158
ADump :: ModelVar Model (LSM RealWorld)
@@ -318,6 +320,7 @@ instance InLockstep Model where
318320

319321
shrinkWithVars _ctx _model _action = []
320322

323+
deriving newtype instance Arbitrary UnionCredits
321324

322325
instance RunLockstep Model IO where
323326
observeReal _ action result =

0 commit comments

Comments
 (0)