Skip to content

Commit 98fad1f

Browse files
committed
Add unit tests for the trivial case of querying and supplying union credits
The trivial case is when the table has no union level, as then there is no union debt, and supplying credits returns them all as leftovers. And implement these trivial cases.
1 parent c41a1b6 commit 98fad1f

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/Database/LSMTree/Internal.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,8 +1630,10 @@ remainingUnionDebt :: (MonadSTM m, MonadThrow m) => Table m h -> m UnionDebt
16301630
remainingUnionDebt t = do
16311631
traceWith (tableTracer t) TraceRemainingUnionDebt
16321632
withOpenTable t $ \tEnv -> do
1633-
RW.withReadAccess (tableContent tEnv) $ \_tableContent -> do
1634-
error "remainingUnionDebt: not yet implemented"
1633+
RW.withReadAccess (tableContent tEnv) $ \tableContent ->
1634+
case tableUnionLevel tableContent of
1635+
NoUnion -> pure (UnionDebt 0)
1636+
Union{} -> error "remainingUnionDebt: not yet implemented"
16351637

16361638
-- | See 'Database.LSMTree.Normal.UnionCredits'.
16371639
newtype UnionCredits = UnionCredits Int
@@ -1644,5 +1646,7 @@ supplyUnionCredits t credits = do
16441646
traceWith (tableTracer t) $ TraceSupplyUnionCredits credits
16451647
withOpenTable t $ \tEnv -> do
16461648
-- TODO: should this be acquiring read or write access?
1647-
RW.withWriteAccess (tableContent tEnv) $ \_tableContent -> do
1648-
error "supplyUnionCredits: not yet implemented"
1649+
RW.withWriteAccess (tableContent tEnv) $ \tableContent ->
1650+
case tableUnionLevel tableContent of
1651+
NoUnion -> pure (tableContent, credits) -- all leftovers
1652+
Union{} -> error "supplyUnionCredits: not yet implemented"

test/Database/LSMTree/Model/Session.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,9 @@ supplyUnionCredits ::
844844
-> UnionCredits
845845
-> m UnionCredits
846846
supplyUnionCredits t@Table{..} c@(UnionCredits credits)
847-
| credits <= 0 = pure c
847+
| credits <= 0 = do
848+
_ <- guardTableIsOpen t
849+
pure c
848850
| otherwise = do
849851
(updc, table) <- guardTableIsOpen t
850852
when (isUnionDescendant == IsUnionDescendant) $

test/Test/Database/LSMTree/UnitTests.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ tests =
3434
, testCase "unit_twoTableTypes" unit_twoTableTypes
3535
, testCase "unit_snapshots" unit_snapshots
3636
, testCase "unit_unions_1" unit_unions_1
37+
, testCase "unit_union_credits" unit_union_credits
3738
]
3839

3940
unit_blobs :: (String -> IO ()) -> Assertion
@@ -174,6 +175,21 @@ unit_unions_1 =
174175
r'' <- lookups table'' [Key1 17]
175176
V.map ignoreBlobRef r'' @?= [Found (Value1 44)]
176177

178+
-- | Querying or supplying union credits to non-union tables is trivial.
179+
unit_union_credits :: Assertion
180+
unit_union_credits =
181+
withTempIOHasBlockIO "test" $ \hfs hbio ->
182+
withSession nullTracer hfs hbio (FS.mkFsPath []) $ \sess ->
183+
withTable @_ @Key1 @Value1 @Blob1 sess defaultTableConfig $ \table -> do
184+
inserts table [(Key1 17, Value1 42, Nothing)]
185+
186+
-- The table is not the result of a union, so the debt is always 0,
187+
UnionDebt debt <- remainingUnionDebt table
188+
debt @?= 0
189+
190+
-- and supplying credits returns them all as leftovers.
191+
UnionCredits leftover <- supplyUnionCredits table (UnionCredits 42)
192+
leftover @?= 42
177193

178194
ignoreBlobRef :: Functor f => f (BlobRef m b) -> f ()
179195
ignoreBlobRef = fmap (const ())

0 commit comments

Comments
 (0)