Skip to content

Commit 87549dd

Browse files
authored
Merge pull request #546 from IntersectMBO/jdral/union-entry-semigroup
`Semigroup` and semantic tests for union and regular `Entry`s
2 parents 36e0e7e + afb675d commit 87549dd

File tree

5 files changed

+346
-109
lines changed

5 files changed

+346
-109
lines changed

prototypes/ScheduledMerges.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,9 @@ combine new_ old = case new_ of
470470
--
471471
-- See 'MergeUnion'.
472472
combineUnion :: Op -> Op -> Op
473+
combineUnion Delete (Mupsert v) = Insert v Nothing
473474
combineUnion Delete old = old
475+
combineUnion (Mupsert u) Delete = Insert u Nothing
474476
combineUnion new_ Delete = new_
475477
combineUnion (Mupsert v') (Mupsert v ) = Insert (resolveValue v' v) Nothing
476478
combineUnion (Mupsert v') (Insert v _) = Insert (resolveValue v' v) Nothing

src-extras/Database/LSMTree/Extras/Generators.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ instance Arbitrary (Unsliced SerialisedKey) where
549549

550550
instance Arbitrary BlobSpan where
551551
arbitrary = BlobSpan <$> arbitrary <*> arbitrary
552-
shrink (BlobSpan x y) = BlobSpan <$> shrink x <*> shrink y
552+
shrink (BlobSpan x y) = [ BlobSpan x' y' | (x', y') <- shrink (x, y) ]
553553

554554
{-------------------------------------------------------------------------------
555555
Merge

src/Database/LSMTree/Internal/Entry.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,11 @@ unNumEntries (NumEntries x) = x
9292
Value resolution/merging
9393
-------------------------------------------------------------------------------}
9494

95-
-- | As long as values are a semigroup, an Entry is too
96-
instance Semigroup v => Semigroup (Entry v b) where
97-
e1 <> e2 = combine (<>) e1 e2
98-
9995
-- | Given a value-merge function, combine entries. Only take a blob from the
10096
-- left entry.
97+
--
98+
-- Note: 'Entry' is a semigroup with 'combine' if the @(v -> v -> v)@ argument
99+
-- is associative.
101100
combine :: (v -> v -> v) -> Entry v b -> Entry v b -> Entry v b
102101
combine _ e@Delete _ = e
103102
combine _ e@Insert {} _ = e
@@ -111,10 +110,15 @@ combine f (Mupdate u) (Mupdate v) = Mupdate (f u v)
111110
-- has a value, the result should have a value (represented by 'Insert'). If
112111
-- both have a value, these values get combined monoidally. Only take a blob
113112
-- from the left entry.
113+
--
114+
-- Note: 'Entry' is a semigroup with 'combineUnion' if the @(v -> v -> v)@
115+
-- argument is associative.
114116
combineUnion :: (v -> v -> v) -> Entry v b -> Entry v b -> Entry v b
115117
combineUnion f = go
116118
where
119+
go Delete (Mupdate v) = Insert v
117120
go Delete e = e
121+
go (Mupdate u) Delete = Insert u
118122
go e Delete = e
119123
go (Insert u) (Insert v) = Insert (f u v)
120124
go (Insert u) (InsertWithBlob v _) = Insert (f u v)

0 commit comments

Comments
 (0)