Skip to content

Commit 8a324d4

Browse files
committed
Public API: add union debt/credits types and related functions
1 parent 24a5676 commit 8a324d4

File tree

4 files changed

+108
-5
lines changed

4 files changed

+108
-5
lines changed

src/Database/LSMTree.hs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ module Database.LSMTree (
8383
-- * Table union
8484
, union
8585
, unions
86+
, UnionDebt (..)
87+
, remainingUnionDebt
88+
, UnionCredits (..)
89+
, supplyUnionCredits
8690

8791
-- * Serialisation
8892
, SerialiseKey
@@ -112,8 +116,8 @@ import Data.Typeable (Proxy (..), Typeable, eqT, type (:~:) (Refl))
112116
import qualified Data.Vector as V
113117
import Database.LSMTree.Common (BlobRef (BlobRef), IOLike, Range (..),
114118
SerialiseKey, SerialiseValue, Session, SnapshotName,
115-
closeSession, deleteSnapshot, listSnapshots, openSession,
116-
withSession)
119+
UnionCredits (..), UnionDebt (..), closeSession,
120+
deleteSnapshot, listSnapshots, openSession, withSession)
117121
import qualified Database.LSMTree.Common as Common
118122
import qualified Database.LSMTree.Internal as Internal
119123
import qualified Database.LSMTree.Internal.BlobRef as Internal
@@ -552,6 +556,22 @@ unions (t :| ts) =
552556
| Just Refl <- eqT @h @h' = pure t'
553557
| otherwise = throwIO (Internal.ErrUnionsTableTypeMismatch 0 i)
554558

559+
{-# SPECIALISE remainingUnionDebt :: Table IO k v b -> IO UnionDebt #-}
560+
remainingUnionDebt :: IOLike m => Table m k v b -> m UnionDebt
561+
remainingUnionDebt (Internal.Table' t) =
562+
(\(Internal.UnionDebt x) -> UnionDebt x) <$>
563+
Internal.remainingUnionDebt t
564+
565+
{-# SPECIALISE supplyUnionCredits :: Table IO k v b -> UnionCredits -> IO UnionCredits #-}
566+
supplyUnionCredits ::
567+
IOLike m
568+
=> Table m k v b
569+
-> UnionCredits
570+
-> m UnionCredits
571+
supplyUnionCredits (Internal.Table' t) (UnionCredits credits) =
572+
(\(Internal.UnionCredits x) -> UnionCredits x) <$>
573+
Internal.supplyUnionCredits t (Internal.UnionCredits credits)
574+
555575
{-------------------------------------------------------------------------------
556576
Monoidal value resolution
557577
-------------------------------------------------------------------------------}

src/Database/LSMTree/Common.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ module Database.LSMTree.Common (
4343
, Internal.TableConfigOverride
4444
, Internal.configNoOverride
4545
, Internal.configOverrideDiskCachePolicy
46+
-- * Unions
47+
, UnionDebt (..)
48+
, UnionCredits (..)
4649
) where
4750

4851
import Control.Concurrent.Class.MonadMVar.Strict
@@ -259,3 +262,24 @@ data BlobRef m b where
259262

260263
instance Show (BlobRef m b) where
261264
showsPrec d (BlobRef b) = showsPrec d b
265+
266+
{-------------------------------------------------------------------------------
267+
Unions
268+
-------------------------------------------------------------------------------}
269+
270+
-- | The /current/ upper bound on the number of 'UnionCredits' that have to be
271+
-- supplied before a @union@ is completed.
272+
--
273+
-- The union debt is the number of merging steps that need to be performed /at
274+
-- most/ until the delayed work of performing a @union@ is completed. This
275+
-- includes the cost of completing merges that were part of the union's input
276+
-- tables.
277+
newtype UnionDebt = UnionDebt Int
278+
deriving stock (Show, Eq)
279+
280+
-- | Credits are used to pay off 'UnionDebt', completing a @union@ in the
281+
-- process.
282+
--
283+
-- A union credit corresponds to a single merging step being performed.
284+
newtype UnionCredits = UnionCredits Int
285+
deriving stock (Show, Eq)

src/Database/LSMTree/Monoidal.hs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ module Database.LSMTree.Monoidal (
100100
-- * Table union
101101
, union
102102
, unions
103+
, UnionDebt (..)
104+
, remainingUnionDebt
105+
, UnionCredits (..)
106+
, supplyUnionCredits
103107

104108
-- * Concurrency
105109
-- $concurrency
@@ -132,7 +136,8 @@ import Data.Proxy (Proxy (Proxy))
132136
import Data.Typeable (Typeable, eqT, type (:~:) (Refl))
133137
import qualified Data.Vector as V
134138
import Database.LSMTree.Common (IOLike, Range (..), SerialiseKey,
135-
SerialiseValue (..), Session, SnapshotName, closeSession,
139+
SerialiseValue (..), Session, SnapshotName,
140+
UnionCredits (..), UnionDebt (..), closeSession,
136141
deleteSnapshot, listSnapshots, openSession, withSession)
137142
import qualified Database.LSMTree.Common as Common
138143
import qualified Database.LSMTree.Internal as Internal
@@ -700,6 +705,31 @@ unions (t :| ts) =
700705
| Just Refl <- eqT @h @h' = pure t'
701706
| otherwise = throwIO (Internal.ErrUnionsTableTypeMismatch 0 i)
702707

708+
{-# SPECIALISE remainingUnionDebt :: Table IO k v -> IO UnionDebt #-}
709+
-- | Return the current union debt. This debt can be reduced until it is paid
710+
-- off using @supplyUnionCredits@.
711+
remainingUnionDebt :: IOLike m => Table m k v -> m UnionDebt
712+
remainingUnionDebt (Internal.MonoidalTable t) =
713+
(\(Internal.UnionDebt x) -> UnionDebt x) <$>
714+
Internal.remainingUnionDebt t
715+
716+
{-# SPECIALISE supplyUnionCredits :: Table IO k v -> UnionCredits -> IO UnionCredits #-}
717+
-- | Supply union credits to reduce union debt.
718+
--
719+
-- Supplying union credits leads to union merging work being performed in
720+
-- batches. This reduces the union debt returned by @remainingUnionDebt@. Union
721+
-- debt will be reduced by /at least/ the number of supplied union credits. It
722+
-- is therefore advisable to query @remainingUnionDebt@ every once in a while to
723+
-- see what the current debt is.
724+
supplyUnionCredits ::
725+
IOLike m
726+
=> Table m k v
727+
-> UnionCredits
728+
-> m UnionCredits
729+
supplyUnionCredits (Internal.MonoidalTable t) (UnionCredits credits) =
730+
(\(Internal.UnionCredits x) -> UnionCredits x) <$>
731+
Internal.supplyUnionCredits t (Internal.UnionCredits credits)
732+
703733
{-------------------------------------------------------------------------------
704734
Monoidal value resolution
705735
-------------------------------------------------------------------------------}

src/Database/LSMTree/Normal.hs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ module Database.LSMTree.Normal (
101101
-- * Table union
102102
, union
103103
, unions
104+
, UnionDebt (..)
105+
, remainingUnionDebt
106+
, UnionCredits (..)
107+
, supplyUnionCredits
104108

105109
-- * Concurrency #concurrency#
106110
-- $concurrency
@@ -123,8 +127,8 @@ import Data.Typeable (Typeable, eqT, type (:~:) (Refl))
123127
import qualified Data.Vector as V
124128
import Database.LSMTree.Common (BlobRef (BlobRef), IOLike, Range (..),
125129
SerialiseKey, SerialiseValue, Session, SnapshotName,
126-
closeSession, deleteSnapshot, listSnapshots, openSession,
127-
withSession)
130+
UnionCredits (..), UnionDebt (..), closeSession,
131+
deleteSnapshot, listSnapshots, openSession, withSession)
128132
import qualified Database.LSMTree.Common as Common
129133
import qualified Database.LSMTree.Internal as Internal
130134
import qualified Database.LSMTree.Internal.BlobRef as Internal
@@ -820,3 +824,28 @@ unions (t :| ts) =
820824
checkTableType _ i (Internal.NormalTable (t' :: Internal.Table m h'))
821825
| Just Refl <- eqT @h @h' = pure t'
822826
| otherwise = throwIO (Internal.ErrUnionsTableTypeMismatch 0 i)
827+
828+
{-# SPECIALISE remainingUnionDebt :: Table IO k v b -> IO UnionDebt #-}
829+
-- | Return the current union debt. This debt can be reduced until it is paid
830+
-- off using @supplyUnionCredits@.
831+
remainingUnionDebt :: IOLike m => Table m k v b -> m UnionDebt
832+
remainingUnionDebt (Internal.NormalTable t) =
833+
(\(Internal.UnionDebt x) -> UnionDebt x) <$>
834+
Internal.remainingUnionDebt t
835+
836+
{-# SPECIALISE supplyUnionCredits :: Table IO k v b -> UnionCredits -> IO UnionCredits #-}
837+
-- | Supply union credits to reduce union debt.
838+
--
839+
-- Supplying union credits leads to union merging work being performed in
840+
-- batches. This reduces the union debt returned by @remainingUnionDebt@. Union
841+
-- debt will be reduced by /at least/ the number of supplied union credits. It
842+
-- is therefore advisable to query @remainingUnionDebt@ every once in a while to
843+
-- see what the current debt is.
844+
supplyUnionCredits ::
845+
IOLike m
846+
=> Table m k v b
847+
-> UnionCredits
848+
-> m UnionCredits
849+
supplyUnionCredits (Internal.NormalTable t) (UnionCredits credits) =
850+
(\(Internal.UnionCredits x) -> UnionCredits x) <$>
851+
Internal.supplyUnionCredits t (Internal.UnionCredits credits)

0 commit comments

Comments
 (0)