Skip to content

Commit 57dede1

Browse files
committed
Inline Update injection/projection functions at use sites.
That is, inline the injection/projections from the `D.L.I.Entry` module at their use sites.
1 parent dcad1ed commit 57dede1

File tree

5 files changed

+48
-41
lines changed

5 files changed

+48
-41
lines changed

src/Database/LSMTree/Internal/Entry.hs

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ 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
@@ -89,36 +84,6 @@ newtype NumEntries = NumEntries Int
8984
unNumEntries :: NumEntries -> Int
9085
unNumEntries (NumEntries x) = x
9186

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-
12287
{-------------------------------------------------------------------------------
12388
Value resolution/merging
12489
-------------------------------------------------------------------------------}
@@ -149,12 +114,12 @@ combinesNormal = NE.head
149114
resolveEntriesNormal ::
150115
NonEmpty (Entry v blob)
151116
-> Maybe (Normal.Update v blob)
152-
resolveEntriesNormal es = entryToUpdateNormal (combinesNormal es)
117+
resolveEntriesNormal _ = error "about to be removed"
153118

154119
-- | Returns 'Nothing' if the combined entries can not be mapped to an
155120
-- 'Monoidal.Update'.
156121
resolveEntriesMonoidal ::
157122
(v -> v -> v)
158123
-> NonEmpty (Entry v blob)
159124
-> Maybe (Monoidal.Update v)
160-
resolveEntriesMonoidal f es = entryToUpdateMonoidal (combinesMonoidal f es)
125+
resolveEntriesMonoidal _ _ = error "about to be removed"

src/Database/LSMTree/Monoidal.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,13 @@ updates es (Internal.MonoidalTable th) = do
424424
th
425425
where
426426
serialiseEntry = bimap Internal.serialiseKey serialiseOp
427-
serialiseOp = first Internal.serialiseValue . Entry.updateToEntryMonoidal
427+
serialiseOp = first Internal.serialiseValue . updateToEntry
428+
429+
updateToEntry :: Update v -> Entry.Entry v blob
430+
updateToEntry = \case
431+
Insert v -> Entry.Insert v
432+
Mupsert v -> Entry.Mupdate v
433+
Delete -> Entry.Delete
428434

429435
{-# SPECIALISE inserts ::
430436
(SerialiseKey k, SerialiseValue v, ResolveValue v)

src/Database/LSMTree/Normal.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,13 @@ updates es (Internal.NormalTable th) = do
513513
where
514514
serialiseEntry = bimap Internal.serialiseKey serialiseOp
515515
serialiseOp = bimap Internal.serialiseValue Internal.serialiseBlob
516-
. Entry.updateToEntryNormal
516+
. updateToEntry
517+
518+
updateToEntry :: Update v blob -> Entry.Entry v blob
519+
updateToEntry = \case
520+
Insert v Nothing -> Entry.Insert v
521+
Insert v (Just b) -> Entry.InsertWithBlob v b
522+
Delete -> Entry.Delete
517523

518524
{-# SPECIALISE inserts ::
519525
(SerialiseKey k, SerialiseValue v, SerialiseValue blob)

test/Test/Database/LSMTree/Internal/Entry.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,33 @@ newtype BlobSpanSG = BlobSpanSG BlobSpan
116116
instance Arbitrary BlobSpanSG where
117117
arbitrary = coerce (BlobSpan <$> arbitrary <*> arbitrary)
118118
shrink = coerce $ \(BlobSpan x y) -> [ BlobSpan x' y' | (x', y') <- shrink2 (x, y) ]
119+
120+
{-------------------------------------------------------------------------------
121+
Injections/projections
122+
-------------------------------------------------------------------------------}
123+
124+
updateToEntryNormal :: Normal.Update v blob -> Entry v blob
125+
updateToEntryNormal = \case
126+
Normal.Insert v Nothing -> Insert v
127+
Normal.Insert v (Just b) -> InsertWithBlob v b
128+
Normal.Delete -> Delete
129+
130+
_entryToUpdateNormal :: Entry v blob -> Maybe (Normal.Update v blob)
131+
_entryToUpdateNormal = \case
132+
Insert v -> Just (Normal.Insert v Nothing)
133+
InsertWithBlob v b -> Just (Normal.Insert v (Just b))
134+
Mupdate _ -> Nothing
135+
Delete -> Just Normal.Delete
136+
137+
updateToEntryMonoidal :: Monoidal.Update v -> Entry v blob
138+
updateToEntryMonoidal = \case
139+
Monoidal.Insert v -> Insert v
140+
Monoidal.Mupsert v -> Mupdate v
141+
Monoidal.Delete -> Delete
142+
143+
_entryToUpdateMonoidal :: Entry v blob -> Maybe (Monoidal.Update v)
144+
_entryToUpdateMonoidal = \case
145+
Insert v -> Just (Monoidal.Insert v)
146+
InsertWithBlob _ _ -> Nothing
147+
Mupdate v -> Just (Monoidal.Mupsert v)
148+
Delete -> Just Monoidal.Delete

test/Test/Database/LSMTree/Internal/Run.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import Database.LSMTree.Extras.RunData
2828
import Database.LSMTree.Internal.BlobRef (BlobSpan (..))
2929
import qualified Database.LSMTree.Internal.CRC32C as CRC
3030
import Database.LSMTree.Internal.Entry
31-
import qualified Database.LSMTree.Internal.Normal as N
3231
import qualified Database.LSMTree.Internal.RawBytes as RB
3332
import Database.LSMTree.Internal.RawPage
3433
import Database.LSMTree.Internal.Run as Run
@@ -80,7 +79,8 @@ testSingleInsert sessionRoot key val mblob =
8079
let fs = FsIO.ioHasFS (FS.MountPoint sessionRoot) in
8180
FS.withIOHasBlockIO fs FS.defaultIOCtxParams $ \hbio -> do
8281
-- flush write buffer
83-
let wb = Map.singleton key (updateToEntryNormal (N.Insert val mblob))
82+
let e = case mblob of Nothing -> Insert val; Just blob -> InsertWithBlob val blob
83+
wb = Map.singleton key e
8484
withRun fs hbio (simplePath 42) (RunData wb) $ \_ -> do
8585
-- check all files have been written
8686
let activeDir = sessionRoot

0 commit comments

Comments
 (0)