@@ -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,51 @@ 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
848859remainingUnionDebt (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+ --
881+ -- This function returns any surplus of union credits as /leftover/ credits when
882+ -- a union has finished. In particular, if the returned number of credits is
883+ -- non-negative, then the union is finished.
884+ supplyUnionCredits :: LSM s -> UnionCredits -> ST s UnionCredits
885+ supplyUnionCredits (LSMHandle scr lsmr) (UnionCredits credits)
886+ | credits <= 0 = return (UnionCredits 0 )
859887 | otherwise = do
860888 content@ (LSMContent _ _ ul) <- readSTRef lsmr
861- case ul of
889+ UnionCredits <$> case ul of
862890 NoUnion ->
863891 return credits
864892 Union tree debtRef -> do
0 commit comments