Skip to content

Commit b6935e1

Browse files
authored
Merge pull request #418 from IntersectMBO/recursion-ninja/update-HasFS-HasBlockIO
Refactoring APIs involving HasIO & HasBlockIO
2 parents 80249d3 + 9b68375 commit b6935e1

File tree

19 files changed

+459
-521
lines changed

19 files changed

+459
-521
lines changed

bench/macro/lsm-tree-bench-lookups.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ lookupsEnv ::
329329
-> FS.HasFS IO FS.HandleIO
330330
-> FS.HasBlockIO IO FS.HandleIO
331331
-> Run.RunDataCaching
332-
-> IO ( V.Vector (Run IO (FS.Handle FS.HandleIO))
332+
-> IO ( V.Vector (Run IO FS.HandleIO)
333333
, V.Vector (Bloom SerialisedKey)
334334
, V.Vector IndexCompact
335335
, V.Vector (FS.Handle FS.HandleIO)
@@ -345,7 +345,7 @@ lookupsEnv runSizes keyRng0 hfs hbio caching = do
345345

346346
-- create the runs
347347
rbs <- sequence
348-
[ RunBuilder.new hfs
348+
[ RunBuilder.new hfs hbio
349349
(RunFsPaths (FS.mkFsPath []) (RunNumber i))
350350
(NumEntries numEntries)
351351
(RunAllocFixed benchmarkNumBitsPerEntry)
@@ -361,7 +361,7 @@ lookupsEnv runSizes keyRng0 hfs hbio caching = do
361361
flip VUM.imapM_ mvecLocal $ \ !j !k -> do
362362
-- progress
363363
when (j .&. 0xFFFF == 0) (putStr ".")
364-
void $ RunBuilder.addKeyOp hfs rb (serialiseKey k) (Insert zero)
364+
void $ RunBuilder.addKeyOp rb (serialiseKey k) (Insert zero)
365365
pure (i+n)
366366
)
367367
0
@@ -370,7 +370,7 @@ lookupsEnv runSizes keyRng0 hfs hbio caching = do
370370

371371
-- return runs
372372
runs <- V.fromList <$>
373-
mapM (Run.fromMutable hfs hbio caching (RefCount 1)) rbs
373+
mapM (Run.fromMutable caching (RefCount 1)) rbs
374374
let blooms = V.map Run.runFilter runs
375375
indexes = V.map Run.runIndex runs
376376
handles = V.map Run.runKOpsFile runs
@@ -465,7 +465,7 @@ benchLookupsIO ::
465465
-> ResolveSerialisedValue
466466
-> WB.WriteBuffer
467467
-> WBB.WriteBufferBlobs IO h
468-
-> V.Vector (Run IO (FS.Handle h))
468+
-> V.Vector (Run IO h)
469469
-> V.Vector (Bloom SerialisedKey)
470470
-> V.Vector IndexCompact
471471
-> V.Vector (FS.Handle h)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ lookupsInBatchesEnv ::
180180
, ArenaManager RealWorld
181181
, FS.HasFS IO FS.HandleIO
182182
, FS.HasBlockIO IO FS.HandleIO
183-
, V.Vector (Run IO (FS.Handle FS.HandleIO))
183+
, V.Vector (Run IO FS.HandleIO)
184184
, V.Vector SerialisedKey
185185
)
186186
lookupsInBatchesEnv Config {..} = do
@@ -212,7 +212,7 @@ lookupsInBatchesCleanup ::
212212
, ArenaManager RealWorld
213213
, FS.HasFS IO FS.HandleIO
214214
, FS.HasBlockIO IO FS.HandleIO
215-
, V.Vector (Run IO (FS.Handle FS.HandleIO))
215+
, V.Vector (Run IO FS.HandleIO)
216216
, V.Vector SerialisedKey
217217
)
218218
-> IO ()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ merge ::
228228
-> Config
229229
-> Run.RunFsPaths
230230
-> InputRuns
231-
-> IO (Run IO (FS.Handle (FS.HandleIO)))
231+
-> IO (Run IO FS.HandleIO)
232232
merge fs hbio Config {..} targetPaths runs = do
233233
let f = fromMaybe const mergeMappend
234234
m <- fromMaybe (error "empty inputs, no merge created") <$>
@@ -241,7 +241,7 @@ outputRunPaths = RunFsPaths (FS.mkFsPath []) (RunNumber 0)
241241
inputRunPaths :: [Run.RunFsPaths]
242242
inputRunPaths = RunFsPaths (FS.mkFsPath []) . RunNumber <$> [1..]
243243

244-
type InputRuns = V.Vector (Run IO (FS.Handle FS.HandleIO))
244+
type InputRuns = V.Vector (Run IO FS.HandleIO)
245245

246246
type Mappend = SerialisedValue -> SerialisedValue -> SerialisedValue
247247

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ instance NoThunks (Unsliced a) where
184184
Run
185185
-------------------------------------------------------------------------------}
186186

187-
deriving stock instance Generic (Run m (Handle h))
188-
deriving anyclass instance (Typeable (PrimState m), Typeable h)
189-
=> NoThunks (Run m (Handle h))
187+
deriving stock instance Generic (Run m h)
188+
deriving anyclass instance (Typeable m, Typeable (PrimState m), Typeable h)
189+
=> NoThunks (Run m h)
190190

191191
deriving stock instance Generic RunDataCaching
192192
deriving anyclass instance NoThunks RunDataCaching
@@ -259,11 +259,11 @@ deriving anyclass instance ( Typeable m, Typeable (PrimState m), Typeable h
259259
, NoThunks (StrictMVar m (MergingRunState m h))
260260
) => NoThunks (TableContent m h)
261261

262-
deriving stock instance Generic (LevelsCache m (Handle h))
263-
deriving anyclass instance (Typeable (PrimState m), Typeable h)
264-
=> NoThunks (LevelsCache m (Handle h))
262+
deriving stock instance Generic (LevelsCache m h)
263+
deriving anyclass instance (Typeable m, Typeable (PrimState m), Typeable h)
264+
=> NoThunks (LevelsCache m h)
265265

266-
deriving stock instance Generic (Level m h)
266+
deriving stock instance Generic (Level m h)
267267
deriving anyclass instance ( Typeable m, Typeable (PrimState m), Typeable h
268268
, NoThunks (StrictMVar m (MergingRunState m h))
269269
) => NoThunks (Level m h)
@@ -292,13 +292,13 @@ deriving anyclass instance NoThunks NumEntries
292292
RunBuilder
293293
-------------------------------------------------------------------------------}
294294

295-
deriving stock instance Generic (RunBuilder s (Handle h))
296-
deriving anyclass instance (Typeable s, Typeable h)
297-
=> NoThunks (RunBuilder s (Handle h))
295+
deriving stock instance Generic (RunBuilder m h)
296+
deriving anyclass instance (Typeable m, Typeable (PrimState m), Typeable h)
297+
=> NoThunks (RunBuilder m h)
298298

299-
deriving stock instance Generic (ChecksumHandle s (Handle h))
299+
deriving stock instance Generic (ChecksumHandle s h)
300300
deriving anyclass instance (Typeable s, Typeable h)
301-
=> NoThunks (ChecksumHandle s (Handle h))
301+
=> NoThunks (ChecksumHandle s h)
302302

303303
{-------------------------------------------------------------------------------
304304
RunAcc
@@ -348,35 +348,35 @@ deriving anyclass instance NoThunks Merge.MergeState
348348
Readers
349349
-------------------------------------------------------------------------------}
350350

351-
deriving stock instance Generic (Readers m (Handle h))
351+
deriving stock instance Generic (Readers m h)
352352
deriving anyclass instance (Typeable m, Typeable (PrimState m), Typeable h)
353-
=> NoThunks (Readers m (Handle h))
353+
=> NoThunks (Readers m h)
354354

355-
deriving stock instance Generic (Reader m (Handle h))
355+
deriving stock instance Generic (Reader m h)
356356
instance (Typeable m, Typeable (PrimState m), Typeable h)
357-
=> NoThunks (Reader m (Handle h)) where
358-
showTypeOf (_ :: Proxy (Reader m (Handle h))) = "Reader"
357+
=> NoThunks (Reader m h) where
358+
showTypeOf (_ :: Proxy (Reader m h)) = "Reader"
359359
wNoThunks ctx = \case
360360
ReadRun r -> noThunks ctx r
361361
ReadBuffer var -> noThunks ctx (OnlyCheckWhnf var) -- contents intentionally lazy
362362

363363
deriving stock instance Generic ReaderNumber
364364
deriving anyclass instance NoThunks ReaderNumber
365365

366-
deriving stock instance Generic (ReadCtx m (Handle h))
366+
deriving stock instance Generic (ReadCtx m h)
367367
deriving anyclass instance (Typeable m, Typeable (PrimState m), Typeable h)
368-
=> NoThunks (ReadCtx m (Handle h))
368+
=> NoThunks (ReadCtx m h)
369369

370370
{-------------------------------------------------------------------------------
371371
Reader
372372
-------------------------------------------------------------------------------}
373373

374-
deriving stock instance Generic (RunReader m (Handle h))
375-
deriving anyclass instance (Typeable (PrimState m), Typeable h)
376-
=> NoThunks (RunReader m (Handle h))
374+
deriving stock instance Generic (RunReader m h)
375+
deriving anyclass instance (Typeable m, Typeable (PrimState m), Typeable h)
376+
=> NoThunks (RunReader m h)
377377

378378
deriving stock instance Generic (Reader.Entry m (Handle h))
379-
deriving anyclass instance (Typeable (PrimState m), Typeable h)
379+
deriving anyclass instance (Typeable m, Typeable (PrimState m), Typeable h)
380380
=> NoThunks (Reader.Entry m (Handle h))
381381

382382
{-------------------------------------------------------------------------------

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ withRun ::
5353
-> HasBlockIO IO h
5454
-> RunFsPaths
5555
-> SerialisedRunData
56-
-> (Run IO (Handle h) -> IO a)
56+
-> (Run IO h -> IO a)
5757
-> IO a
5858
withRun hfs hbio path rd = do
5959
bracket
@@ -67,7 +67,7 @@ withRuns ::
6767
=> HasFS IO h
6868
-> HasBlockIO IO h
6969
-> f (RunFsPaths, SerialisedRunData)
70-
-> (f (Run IO (Handle h)) -> IO a)
70+
-> (f (Run IO h) -> IO a)
7171
-> IO a
7272
withRuns hfs hbio xs = do
7373
bracket
@@ -85,7 +85,7 @@ unsafeFlushAsWriteBuffer ::
8585
-> HasBlockIO IO h
8686
-> RunFsPaths
8787
-> SerialisedRunData
88-
-> IO (Run IO (Handle h))
88+
-> IO (Run IO h)
8989
unsafeFlushAsWriteBuffer fs hbio fsPaths (RunData m) = do
9090
let blobpath = addExtension (runBlobPath fsPaths) ".wb"
9191
wbblobs <- WBB.new fs blobpath

src/Database/LSMTree/Internal.hs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -901,10 +901,10 @@ data CursorEnv m h = CursorEnv {
901901
-- However, the reference counts to the runs only get removed when calling
902902
-- 'closeCursor', as there might still be 'BlobRef's that need the
903903
-- corresponding run to stay alive.
904-
, cursorReaders :: !(Maybe (Readers.Readers m (Handle h)))
904+
, cursorReaders :: !(Maybe (Readers.Readers m h))
905905
-- | The runs held open by the cursor. We must remove a reference when the
906906
-- cursor gets closed.
907-
, cursorRuns :: !(V.Vector (Run m (Handle h)))
907+
, cursorRuns :: !(V.Vector (Run m h))
908908

909909
-- | The write buffer blobs, which like the runs, we have to keep open
910910
-- untile the cursor is closed.
@@ -943,9 +943,6 @@ newCursor !offsetKey th = withOpenTable th $ \thEnv -> do
943943
let cursorTracer = TraceCursor cursorId `contramap` sessionTracer cursorSession
944944
traceWith cursorTracer $ TraceCreateCursor (tableId thEnv)
945945

946-
let hfs = tableHasFS thEnv
947-
let hbio = tableHasBlockIO thEnv
948-
949946
-- We acquire a read-lock on the session open-state to prevent races, see
950947
-- 'sessionOpenTables'.
951948
withOpenSession cursorSession $ \_ -> do
@@ -954,9 +951,8 @@ newCursor !offsetKey th = withOpenTable th $ \thEnv -> do
954951
allocTableContent reg (tableContent thEnv)
955952
cursorReaders <-
956953
allocateMaybeTemp reg
957-
(Readers.new hfs hbio
958-
offsetKey (Just (wb, wbblobs)) cursorRuns)
959-
(Readers.close hfs hbio)
954+
(Readers.new offsetKey (Just (wb, wbblobs)) cursorRuns)
955+
Readers.close
960956
let cursorWBB = wbblobs
961957
cursorState <- newMVar (CursorOpen CursorEnv {..})
962958
let !cursor = Cursor {cursorState, cursorTracer}
@@ -997,9 +993,6 @@ closeCursor Cursor {..} = do
997993
modifyWithTempRegistry_ (takeMVar cursorState) (putMVar cursorState) $ \reg -> \case
998994
CursorClosed -> return CursorClosed
999995
CursorOpen CursorEnv {..} -> do
1000-
let hfs = sessionHasFS cursorSessionEnv
1001-
let hbio = sessionHasBlockIO cursorSessionEnv
1002-
1003996
-- This should be safe-ish, but it's still not ideal, because it doesn't
1004997
-- rule out sync exceptions in the cleanup operations.
1005998
-- In that case, the cursor ends up closed, but resources might not have
@@ -1008,7 +1001,7 @@ closeCursor Cursor {..} = do
10081001
modifyMVar_ (sessionOpenCursors cursorSessionEnv) $
10091002
pure . Map.delete cursorId
10101003

1011-
forM_ cursorReaders $ freeTemp reg . Readers.close hfs hbio
1004+
forM_ cursorReaders $ freeTemp reg . Readers.close
10121005
V.forM_ cursorRuns $ freeTemp reg . Run.removeReference
10131006
freeTemp reg (WBB.removeReference cursorWBB)
10141007
return CursorClosed
@@ -1067,9 +1060,7 @@ readCursorWhile resolve keyIsWanted n Cursor {..} fromEntry = do
10671060
-- a drained cursor will just return an empty vector
10681061
return (state, V.empty)
10691062
Just readers -> do
1070-
let hfs = sessionHasFS (cursorSessionEnv cursorEnv)
1071-
let hbio = sessionHasBlockIO (cursorSessionEnv cursorEnv)
1072-
(vec, hasMore) <- readCursorEntriesWhile hfs hbio resolve keyIsWanted fromEntry readers n
1063+
(vec, hasMore) <- readCursorEntriesWhile resolve keyIsWanted fromEntry readers n
10731064
-- if we drained the readers, remove them from the state
10741065
let !state' = case hasMore of
10751066
Readers.HasMore -> state
@@ -1078,12 +1069,10 @@ readCursorWhile resolve keyIsWanted n Cursor {..} fromEntry = do
10781069

10791070
{-# INLINE readCursorEntriesWhile #-}
10801071
{-# SPECIALISE readCursorEntriesWhile :: forall h res.
1081-
HasFS IO h
1082-
-> HasBlockIO IO h
1083-
-> ResolveSerialisedValue
1072+
ResolveSerialisedValue
10841073
-> (SerialisedKey -> Bool)
10851074
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef IO (Handle h)) -> res)
1086-
-> Readers.Readers IO (Handle h)
1075+
-> Readers.Readers IO h
10871076
-> Int
10881077
-> IO (V.Vector res, Readers.HasMore) #-}
10891078
-- | General notes on the code below:
@@ -1094,15 +1083,13 @@ readCursorWhile resolve keyIsWanted n Cursor {..} fromEntry = do
10941083
-- * there is probably opportunity for optimisations
10951084
readCursorEntriesWhile :: forall h m res.
10961085
(MonadFix m, MonadMask m, MonadST m, MonadSTM m)
1097-
=> HasFS m h
1098-
-> HasBlockIO m h
1099-
-> ResolveSerialisedValue
1086+
=> ResolveSerialisedValue
11001087
-> (SerialisedKey -> Bool)
11011088
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef m (Handle h)) -> res)
1102-
-> Readers.Readers m (Handle h)
1089+
-> Readers.Readers m h
11031090
-> Int
11041091
-> m (V.Vector res, Readers.HasMore)
1105-
readCursorEntriesWhile hfs hbio resolve keyIsWanted fromEntry readers n =
1092+
readCursorEntriesWhile resolve keyIsWanted fromEntry readers n =
11061093
flip (V.unfoldrNM' n) Readers.HasMore $ \case
11071094
Readers.Drained -> return (Nothing, Readers.Drained)
11081095
Readers.HasMore -> readEntryIfWanted
@@ -1117,7 +1104,7 @@ readCursorEntriesWhile hfs hbio resolve keyIsWanted fromEntry readers n =
11171104

11181105
readEntry :: m (Maybe res, Readers.HasMore)
11191106
readEntry = do
1120-
(key, readerEntry, hasMore) <- Readers.pop hfs hbio readers
1107+
(key, readerEntry, hasMore) <- Readers.pop readers
11211108
let !entry = Reader.toFullEntry readerEntry
11221109
case hasMore of
11231110
Readers.Drained -> do
@@ -1134,7 +1121,7 @@ readCursorEntriesWhile hfs hbio resolve keyIsWanted fromEntry readers n =
11341121

11351122
dropRemaining :: SerialisedKey -> m Readers.HasMore
11361123
dropRemaining key = do
1137-
(_, hasMore) <- Readers.dropWhileKey hfs hbio readers key
1124+
(_, hasMore) <- Readers.dropWhileKey readers key
11381125
return hasMore
11391126

11401127
-- Resolve a 'Mupsert' value with the other entries of the same key.
@@ -1148,7 +1135,7 @@ readCursorEntriesWhile hfs hbio resolve keyIsWanted fromEntry readers n =
11481135
-- No more entries for same key, done.
11491136
handleResolved key (Entry.Mupdate v) Readers.HasMore
11501137
else do
1151-
(_, nextEntry, hasMore) <- Readers.pop hfs hbio readers
1138+
(_, nextEntry, hasMore) <- Readers.pop readers
11521139
let resolved = Entry.combine resolve (Entry.Mupdate v)
11531140
(Reader.toFullEntry nextEntry)
11541141
case hasMore of

src/Database/LSMTree/Internal/CRC32C.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
{-# LANGUAGE OverloadedStrings #-}
44
{-# LANGUAGE ScopedTypeVariables #-}
55

6+
-- Needed by GHC <= 9.2 for newtype deriving Prim below
7+
{-# LANGUAGE DataKinds #-}
8+
{-# LANGUAGE UnboxedTuples #-}
9+
610
-- | Functionalty related to CRC-32C (Castagnoli) checksums:
711
--
812
-- * Support for calculating checksums while incrementally writing files.
@@ -65,6 +69,7 @@ import System.FS.BlockIO.API (ByteCount)
6569

6670
newtype CRC32C = CRC32C Word32
6771
deriving stock (Eq, Ord, Show)
72+
deriving newtype (Prim)
6873

6974

7075
initialCRC32C :: CRC32C

src/Database/LSMTree/Internal/Lookup.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ data ByteCountDiscrepancy = ByteCountDiscrepancy {
157157
-> ResolveSerialisedValue
158158
-> WB.WriteBuffer
159159
-> WBB.WriteBufferBlobs IO h
160-
-> V.Vector (Run IO (Handle h))
160+
-> V.Vector (Run IO h)
161161
-> V.Vector (Bloom SerialisedKey)
162162
-> V.Vector IndexCompact
163163
-> V.Vector (Handle h)
@@ -177,7 +177,7 @@ lookupsIO ::
177177
-> ResolveSerialisedValue
178178
-> WB.WriteBuffer
179179
-> WBB.WriteBufferBlobs m h
180-
-> V.Vector (Run m (Handle h)) -- ^ Runs @rs@
180+
-> V.Vector (Run m h) -- ^ Runs @rs@
181181
-> V.Vector (Bloom SerialisedKey) -- ^ The bloom filters inside @rs@
182182
-> V.Vector IndexCompact -- ^ The indexes inside @rs@
183183
-> V.Vector (Handle h) -- ^ The file handles to the key\/value files inside @rs@
@@ -202,7 +202,7 @@ lookupsIO !hbio !mgr !resolveV !wb !wbblobs !rs !blooms !indexes !kopsFiles !ks
202202
ResolveSerialisedValue
203203
-> WB.WriteBuffer
204204
-> WBB.WriteBufferBlobs IO h
205-
-> V.Vector (Run IO (Handle h))
205+
-> V.Vector (Run IO h)
206206
-> V.Vector SerialisedKey
207207
-> VP.Vector RunIxKeyIx
208208
-> V.Vector (IOOp RealWorld h)
@@ -221,7 +221,7 @@ intraPageLookups ::
221221
=> ResolveSerialisedValue
222222
-> WB.WriteBuffer
223223
-> WBB.WriteBufferBlobs m h
224-
-> V.Vector (Run m (Handle h))
224+
-> V.Vector (Run m h)
225225
-> V.Vector SerialisedKey
226226
-> VP.Vector RunIxKeyIx
227227
-> V.Vector (IOOp (PrimState m) h)

0 commit comments

Comments
 (0)