Skip to content

Commit 610b821

Browse files
committed
IsMergeType where isLastLevel, isUnion
1 parent 4c2200a commit 610b821

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

prototypes/ScheduledMerges.hs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,44 +121,43 @@ data MergingRunState = CompletedMerge !Run
121121
![Run] -- ^ inputs of the merge
122122
Run -- ^ output of the merge (lazily evaluated)
123123

124-
-- | Different ways of combining entries for the same key.
125-
--
126-
-- In level merges, a more recent entry overwrites any older ones (with the
127-
-- exception of monoidal updates 'Mupsert' modifying instead of overwriting).
128-
-- Union merges, on the other hand, follow the semantics of
129-
-- @Data.Map.unionWith (<>)@. Since the input runs are semantically treated like
130-
-- @Data.Map@s, deletes are ignored and inserts act like mupserts, so they need
131-
-- to be merged monoidally using 'resolveValue'.
132-
--
133-
-- A last level merge behaves differently from a mid-level merge: last level
134-
-- merges can actually remove delete operations, whereas mid-level merges must
135-
-- preserve them. This is orthogonal to the 'MergePolicy'. Since the union level
136-
-- is always the last, union merges merges can also drop deletes.
137-
data MergeType = MergeTypeMidLevel | MergeTypeLastLevel | MergeTypeUnion
138-
deriving stock (Eq, Show)
139-
140124
-- | Merges can exist in different parts of the LSM, each with different options
141125
-- for the exact merge operation performed.
142126
class Show t => IsMergeType t where
143-
toMergeType :: t -> MergeType
127+
isLastLevel :: t -> Bool
128+
isUnion :: t -> Bool
144129

145130
-- | Different types of merges created as part of a regular (non-union) level.
131+
--
132+
-- A last level merge behaves differently from a mid-level merge: last level
133+
-- merges can actually remove delete operations, whereas mid-level merges must
134+
-- preserve them. This is orthogonal to the 'MergePolicy'.
146135
data LevelMergeType = MergeMidLevel | MergeLastLevel
147136
deriving stock (Eq, Show)
148137

149138
instance IsMergeType LevelMergeType where
150-
toMergeType = \case
151-
MergeMidLevel -> MergeTypeMidLevel
152-
MergeLastLevel -> MergeTypeLastLevel
139+
isLastLevel = \case
140+
MergeMidLevel -> False
141+
MergeLastLevel -> True
142+
isUnion = const False
153143

154144
-- | Different types of merges created as part of the merging tree.
145+
--
146+
-- Union merges follow the semantics of @Data.Map.unionWith (<>)@. Since
147+
-- the input runs are semantically treated like @Data.Map@s, deletes are ignored
148+
-- and inserts act like mupserts, so they need to be merged monoidally using
149+
-- 'resolveValue'.
150+
--
151+
-- Trees can only exist on the union level, which is the last. Therefore, node
152+
-- merges can always drop deletes.
155153
data TreeMergeType = MergeLevel | MergeUnion
156154
deriving stock (Eq, Show)
157155

158156
instance IsMergeType TreeMergeType where
159-
toMergeType = \case
160-
MergeLevel -> MergeTypeLastLevel -- node merges are always like last level
161-
MergeUnion -> MergeTypeUnion
157+
isLastLevel = const True
158+
isUnion = \case
159+
MergeLevel -> False
160+
MergeUnion -> True
162161

163162
-- | An additional optional last level, created as a result of 'union'. It can
164163
-- not only contain an ongoing merge of multiple runs, but a nested tree of
@@ -452,10 +451,9 @@ newMergingRun mdebt mergeType runs = do
452451
MergingRun mergeType <$> newSTRef state
453452

454453
mergek :: IsMergeType t => t -> [Run] -> Run
455-
mergek t = case toMergeType t of
456-
MergeTypeMidLevel -> Map.unionsWith combine
457-
MergeTypeLastLevel -> Map.filter (/= Delete) . Map.unionsWith combine
458-
MergeTypeUnion -> Map.filter (/= Delete) . Map.unionsWith combineUnion
454+
mergek t =
455+
(if isLastLevel t then Map.filter (/= Delete) else id)
456+
. Map.unionsWith (if isUnion t then combineUnion else combine)
459457

460458
-- | Combines two entries that have been performed after another. Therefore, the
461459
-- newer one overwrites the old one (or modifies it for 'Mupsert').

0 commit comments

Comments
 (0)