Skip to content

Commit e38797c

Browse files
authored
Merge pull request #437 from IntersectMBO/jdral/cleanups
Cleanups related to write buffer updates and value resolution in the core library.
2 parents c361efb + 0c3e466 commit e38797c

File tree

10 files changed

+175
-258
lines changed

10 files changed

+175
-258
lines changed

bench/micro/Bench/Database/LSMTree/Internal/WriteBuffer.hs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
module Bench.Database.LSMTree.Internal.WriteBuffer (benchmarks) where
22

3-
import Control.DeepSeq (NFData (..))
3+
import Control.DeepSeq (NFData (..), rwhnf)
4+
import Control.Exception (assert)
45
import Criterion.Main (Benchmark, bench, bgroup)
56
import qualified Criterion.Main as Cr
67
import Data.Bifunctor (first)
78
import qualified Data.Foldable as Fold
89
import qualified Data.List as List
9-
import Data.Maybe (fromMaybe)
10+
import Data.Maybe (fromMaybe, isJust, isNothing)
1011
import Data.Word (Word64)
1112
import Database.LSMTree.Extras.Orphans ()
1213
import Database.LSMTree.Extras.Random (frequency, randomByteStringR)
@@ -16,9 +17,6 @@ import Database.LSMTree.Internal.Entry
1617
import Database.LSMTree.Internal.Serialise
1718
import Database.LSMTree.Internal.WriteBuffer (WriteBuffer)
1819
import qualified Database.LSMTree.Internal.WriteBuffer as WB
19-
import qualified Database.LSMTree.Monoidal as Monoidal
20-
import qualified Database.LSMTree.Normal as Normal
21-
import GHC.Generics
2220
import System.Random (StdGen, mkStdGen, uniform)
2321

2422
benchmarks :: Benchmark
@@ -110,19 +108,13 @@ benchWriteBuffer conf@Config{name} =
110108
]
111109

112110
insert :: InputKOps -> WriteBuffer
113-
insert (NormalInputs kops) =
114-
Fold.foldl' (\wb (k, e) -> WB.addEntryNormal k e wb) WB.empty kops
115-
insert (MonoidalInputs kops mappendVal) =
116-
Fold.foldl' (\wb (k, e) -> WB.addEntryMonoidal mappendVal k e wb) WB.empty kops
117-
118-
data InputKOps
119-
= NormalInputs
120-
![(SerialisedKey, Normal.Update SerialisedValue BlobSpan)]
121-
| MonoidalInputs
122-
![(SerialisedKey, Monoidal.Update SerialisedValue)]
123-
!Mappend
124-
deriving stock Generic
125-
deriving anyclass NFData
111+
insert (InputKOps kops mappendVal) =
112+
Fold.foldl' (\wb (k, e) -> WB.addEntry mappendVal k e wb) WB.empty kops
113+
114+
data InputKOps = InputKOps [(SerialisedKey, Entry SerialisedValue BlobSpan)] Mappend
115+
116+
instance NFData InputKOps where
117+
rnf (InputKOps kops mappendVal) = rnf kops `seq` rwhnf mappendVal
126118

127119
type Mappend = SerialisedValue -> SerialisedValue -> SerialisedValue
128120

@@ -187,22 +179,16 @@ configUTxO = defaultConfig {
187179
envInputKOps :: Config -> InputKOps
188180
envInputKOps config = do
189181
let kops = randomKOps config (mkStdGen 17)
190-
in case mappendVal config of
191-
Nothing -> NormalInputs (fmap (fmap expectNormal) kops)
192-
Just f -> MonoidalInputs (fmap (fmap expectMonoidal) kops) f
193-
where
194-
expectNormal e = fromMaybe (error ("invalid normal update: " <> show e))
195-
(entryToUpdateNormal e)
196-
expectMonoidal e = fromMaybe (error ("invalid monoidal update: " <> show e))
197-
(entryToUpdateMonoidal e)
182+
in InputKOps kops (fromMaybe const (mappendVal config))
198183

199184
-- | Generate keys and entries to insert into the write buffer.
200185
-- They are already serialised to exclude the cost from the benchmark.
201186
randomKOps ::
202187
Config
203188
-> StdGen -- ^ RNG
204189
-> [SerialisedKOp]
205-
randomKOps Config {..} = take nentries . List.unfoldr (Just . randomKOp)
190+
randomKOps Config {..} = take nentries . List.unfoldr (Just . randomKOp) .
191+
assert (if fmupserts > 0 then isJust mappendVal else isNothing mappendVal)
206192
where
207193
randomKOp :: Rnd SerialisedKOp
208194
randomKOp g = let (!k, !g') = randomKey g

lsm-tree.cabal

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ library
135135
Database.LSMTree.Internal.Lookup
136136
Database.LSMTree.Internal.Merge
137137
Database.LSMTree.Internal.MergeSchedule
138-
Database.LSMTree.Internal.Monoidal
139-
Database.LSMTree.Internal.Normal
140138
Database.LSMTree.Internal.PageAcc
141139
Database.LSMTree.Internal.PageAcc1
142140
Database.LSMTree.Internal.Paths

src/Database/LSMTree/Internal/Entry.hs

Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,14 @@ module Database.LSMTree.Internal.Entry (
55
, onBlobRef
66
, NumEntries (..)
77
, unNumEntries
8-
-- * Injections/projections
9-
, updateToEntryNormal
10-
, updateToEntryMonoidal
11-
, entryToUpdateNormal
12-
, entryToUpdateMonoidal
138
-- * Value resolution/merging
149
, combine
1510
, combineMaybe
16-
, combinesMonoidal
17-
, combinesNormal
18-
, resolveEntriesNormal
19-
, resolveEntriesMonoidal
2011
) where
2112

2213
import Control.DeepSeq (NFData (..))
2314
import Data.Bifoldable (Bifoldable (..))
2415
import Data.Bifunctor (Bifunctor (..))
25-
import Data.List.NonEmpty (NonEmpty)
26-
import qualified Data.List.NonEmpty as NE
27-
import qualified Database.LSMTree.Internal.Monoidal as Monoidal
28-
import qualified Database.LSMTree.Internal.Normal as Normal
2916

3017
data Entry v blobref
3118
= Insert !v
@@ -89,40 +76,14 @@ newtype NumEntries = NumEntries Int
8976
unNumEntries :: NumEntries -> Int
9077
unNumEntries (NumEntries x) = x
9178

92-
{-------------------------------------------------------------------------------
93-
Injections/projections
94-
-------------------------------------------------------------------------------}
95-
96-
updateToEntryNormal :: Normal.Update v blob -> Entry v blob
97-
updateToEntryNormal = \case
98-
Normal.Insert v Nothing -> Insert v
99-
Normal.Insert v (Just b) -> InsertWithBlob v b
100-
Normal.Delete -> Delete
101-
102-
entryToUpdateNormal :: Entry v blob -> Maybe (Normal.Update v blob)
103-
entryToUpdateNormal = \case
104-
Insert v -> Just (Normal.Insert v Nothing)
105-
InsertWithBlob v b -> Just (Normal.Insert v (Just b))
106-
Mupdate _ -> Nothing
107-
Delete -> Just Normal.Delete
108-
109-
updateToEntryMonoidal :: Monoidal.Update v -> Entry v blob
110-
updateToEntryMonoidal = \case
111-
Monoidal.Insert v -> Insert v
112-
Monoidal.Mupsert v -> Mupdate v
113-
Monoidal.Delete -> Delete
114-
115-
entryToUpdateMonoidal :: Entry v blob -> Maybe (Monoidal.Update v)
116-
entryToUpdateMonoidal = \case
117-
Insert v -> Just (Monoidal.Insert v)
118-
InsertWithBlob _ _ -> Nothing
119-
Mupdate v -> Just (Monoidal.Mupsert v)
120-
Delete -> Just Monoidal.Delete
121-
12279
{-------------------------------------------------------------------------------
12380
Value resolution/merging
12481
-------------------------------------------------------------------------------}
12582

83+
-- | As long as values are a semigroup, an Entry is too
84+
instance Semigroup v => Semigroup (Entry v blob) where
85+
e1 <> e2 = combine (<>) e1 e2
86+
12687
-- | Given a value-merge function, combine entries
12788
combine :: (v -> v -> v) -> Entry v blobref -> Entry v blobref -> Entry v blobref
12889
combine _ e@Delete _ = e
@@ -137,24 +98,3 @@ combineMaybe :: (v -> v -> v) -> Maybe (Entry v blobref) -> Maybe (Entry v blobr
13798
combineMaybe _ e1 Nothing = e1
13899
combineMaybe _ Nothing e2 = e2
139100
combineMaybe f (Just e1) (Just e2) = Just $! combine f e1 e2
140-
141-
combinesMonoidal :: (v -> v -> v) -> NonEmpty (Entry v blob) -> Entry v blob
142-
combinesMonoidal f = foldr1 (combine f) -- short-circuit fold
143-
144-
combinesNormal :: NonEmpty (Entry v blob) -> Entry v blob
145-
combinesNormal = NE.head
146-
147-
-- | Returns 'Nothing' if the combined entries can not be mapped to an
148-
-- 'Normal.Update'.
149-
resolveEntriesNormal ::
150-
NonEmpty (Entry v blob)
151-
-> Maybe (Normal.Update v blob)
152-
resolveEntriesNormal es = entryToUpdateNormal (combinesNormal es)
153-
154-
-- | Returns 'Nothing' if the combined entries can not be mapped to an
155-
-- 'Monoidal.Update'.
156-
resolveEntriesMonoidal ::
157-
(v -> v -> v)
158-
-> NonEmpty (Entry v blob)
159-
-> Maybe (Monoidal.Update v)
160-
resolveEntriesMonoidal f es = entryToUpdateMonoidal (combinesMonoidal f es)

src/Database/LSMTree/Internal/Monoidal.hs

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/Database/LSMTree/Internal/Normal.hs

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/Database/LSMTree/Internal/WriteBuffer.hs

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,6 @@
22
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
33

44
-- | The in-memory LSM level 0.
5-
--
6-
-- === TODO
7-
--
8-
-- This is temporary module header documentation. The module will be
9-
-- fleshed out more as we implement bits of it.
10-
--
11-
-- Related work packages: 5
12-
--
13-
-- This module includes in-memory parts parts for, amongst others,
14-
--
15-
-- * Incremental construction
16-
--
17-
-- * Updates (inserts, deletes, mupserts)
18-
--
19-
-- * Queries (lookups, range lookups)
20-
--
21-
-- The above list is a sketch. Functionality may move around, and the list is
22-
-- not exhaustive.
23-
--
245
module Database.LSMTree.Internal.WriteBuffer (
256
WriteBuffer,
267
empty,
@@ -29,10 +10,7 @@ module Database.LSMTree.Internal.WriteBuffer (
2910
toMap,
3011
fromList,
3112
toList,
32-
addEntries,
3313
addEntry,
34-
addEntryMonoidal,
35-
addEntryNormal,
3614
null,
3715
lookups,
3816
lookup,
@@ -46,8 +24,6 @@ import qualified Data.Map.Strict as Map
4624
import qualified Data.Vector as V
4725
import Database.LSMTree.Internal.BlobRef (BlobSpan)
4826
import Database.LSMTree.Internal.Entry
49-
import qualified Database.LSMTree.Internal.Monoidal as Monoidal
50-
import qualified Database.LSMTree.Internal.Normal as Normal
5127
import Database.LSMTree.Internal.Range (Range (..))
5228
import Database.LSMTree.Internal.Serialise
5329
import qualified Database.LSMTree.Internal.Vector as V
@@ -94,13 +70,6 @@ toList (WB m) = Map.assocs m
9470
Updates
9571
-------------------------------------------------------------------------------}
9672

97-
addEntries ::
98-
(SerialisedValue -> SerialisedValue -> SerialisedValue) -- ^ merge function
99-
-> V.Vector (SerialisedKey, Entry SerialisedValue BlobSpan)
100-
-> WriteBuffer
101-
-> WriteBuffer
102-
addEntries f es wb = V.foldl' (flip (uncurry (addEntry f))) wb es
103-
10473
addEntry ::
10574
(SerialisedValue -> SerialisedValue -> SerialisedValue) -- ^ merge function
10675
-> SerialisedKey
@@ -110,21 +79,6 @@ addEntry ::
11079
addEntry f k e (WB wb) =
11180
WB (Map.insertWith (combine f) k e wb)
11281

113-
addEntryMonoidal ::
114-
(SerialisedValue -> SerialisedValue -> SerialisedValue) -- ^ merge function
115-
-> SerialisedKey
116-
-> Monoidal.Update SerialisedValue
117-
-> WriteBuffer
118-
-> WriteBuffer
119-
addEntryMonoidal f k = addEntry f k . updateToEntryMonoidal
120-
121-
addEntryNormal ::
122-
SerialisedKey
123-
-> Normal.Update SerialisedValue BlobSpan
124-
-> WriteBuffer
125-
-> WriteBuffer
126-
addEntryNormal k = addEntry const k . updateToEntryNormal
127-
12882
{-------------------------------------------------------------------------------
12983
Querying
13084
-------------------------------------------------------------------------------}

0 commit comments

Comments
 (0)