Skip to content

Commit 0034666

Browse files
committed
Refactor: change BlobRef m (Handle h) to BlobRef m h
data BlobRef m h previously contained: blobRefFile :: !h which meant allmost all uses of BlobRef had to be BlobRef m (Handle h) Now we make that internal: blobRefFile :: !(FS.Handle h) and so all uses are now simply BlobRef m h This continues with the trend to avoid having to use (Handle h) everywhere. This is a large but simple patch, that just deals with the fallout of this local change.
1 parent 3b801cc commit 0034666

File tree

16 files changed

+60
-64
lines changed

16 files changed

+60
-64
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,9 @@ deriving stock instance Generic (RunReader m h)
395395
deriving anyclass instance (Typeable m, Typeable (PrimState m), Typeable h)
396396
=> NoThunks (RunReader m h)
397397

398-
deriving stock instance Generic (Reader.Entry m (Handle h))
398+
deriving stock instance Generic (Reader.Entry m h)
399399
deriving anyclass instance (Typeable m, Typeable (PrimState m), Typeable h)
400-
=> NoThunks (Reader.Entry m (Handle h))
400+
=> NoThunks (Reader.Entry m h)
401401

402402
{-------------------------------------------------------------------------------
403403
RawPage
@@ -418,7 +418,7 @@ deriving anyclass instance NoThunks RawOverflowPage
418418
-------------------------------------------------------------------------------}
419419

420420
deriving stock instance Generic (BlobRef m h)
421-
deriving anyclass instance (NoThunks h, Typeable (PrimState m))
421+
deriving anyclass instance (Typeable h, Typeable (PrimState m))
422422
=> NoThunks (BlobRef m h)
423423

424424
deriving stock instance Generic BlobSpan

src/Database/LSMTree/Common.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import qualified Database.LSMTree.Internal.MergeSchedule as Internal
6363
import qualified Database.LSMTree.Internal.Paths as Internal
6464
import qualified Database.LSMTree.Internal.Range as Internal
6565
import Database.LSMTree.Internal.Serialise.Class
66-
import System.FS.API (FsPath, Handle, HasFS)
66+
import System.FS.API (FsPath, HasFS)
6767
import System.FS.BlockIO.API (HasBlockIO)
6868
import System.FS.IO (HandleIO)
6969

@@ -268,7 +268,7 @@ type BlobRef :: (Type -> Type) -> Type -> Type
268268
type role BlobRef nominal nominal
269269
data BlobRef m blob where
270270
BlobRef :: Typeable h
271-
=> Internal.WeakBlobRef m (Handle h)
271+
=> Internal.WeakBlobRef m h
272272
-> BlobRef m blob
273273

274274
instance Show (BlobRef m blob) where

src/Database/LSMTree/Internal.hs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ import Database.LSMTree.Internal.UniqCounter
116116
import qualified Database.LSMTree.Internal.WriteBuffer as WB
117117
import qualified Database.LSMTree.Internal.WriteBufferBlobs as WBB
118118
import qualified System.FS.API as FS
119-
import System.FS.API (FsError, FsErrorPath (..), FsPath, Handle,
120-
HasFS)
119+
import System.FS.API (FsError, FsErrorPath (..), FsPath, HasFS)
121120
import qualified System.FS.API.Lazy as FS
122121
import qualified System.FS.BlockIO.API as FS
123122
import System.FS.BlockIO.API (HasBlockIO)
@@ -724,14 +723,14 @@ close t = do
724723
ResolveSerialisedValue
725724
-> V.Vector SerialisedKey
726725
-> Table IO h
727-
-> IO (V.Vector (Maybe (Entry SerialisedValue (WeakBlobRef IO (Handle h))))) #-}
726+
-> IO (V.Vector (Maybe (Entry SerialisedValue (WeakBlobRef IO h)))) #-}
728727
-- | See 'Database.LSMTree.Normal.lookups'.
729728
lookups ::
730729
(MonadST m, MonadSTM m, MonadThrow m)
731730
=> ResolveSerialisedValue
732731
-> V.Vector SerialisedKey
733732
-> Table m h
734-
-> m (V.Vector (Maybe (Entry SerialisedValue (WeakBlobRef m (Handle h)))))
733+
-> m (V.Vector (Maybe (Entry SerialisedValue (WeakBlobRef m h))))
735734
lookups resolve ks t = do
736735
traceWith (tableTracer t) $ TraceLookups (V.length ks)
737736
withOpenTable t $ \thEnv ->
@@ -753,15 +752,15 @@ lookups resolve ks t = do
753752
ResolveSerialisedValue
754753
-> Range SerialisedKey
755754
-> Table IO h
756-
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef IO (Handle h)) -> res)
755+
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef IO h) -> res)
757756
-> IO (V.Vector res) #-}
758757
-- | See 'Database.LSMTree.Normal.rangeLookup'.
759758
rangeLookup ::
760759
(MonadFix m, MonadMask m, MonadMVar m, MonadST m, MonadSTM m)
761760
=> ResolveSerialisedValue
762761
-> Range SerialisedKey
763762
-> Table m h
764-
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef m (Handle h)) -> res)
763+
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef m h) -> res)
765764
-- ^ How to map to a query result, different for normal/monoidal
766765
-> m (V.Vector res)
767766
rangeLookup resolve range t fromEntry = do
@@ -828,12 +827,12 @@ updates resolve es t = do
828827

829828
{-# SPECIALISE retrieveBlobs ::
830829
Session IO h
831-
-> V.Vector (WeakBlobRef IO (FS.Handle h))
830+
-> V.Vector (WeakBlobRef IO h)
832831
-> IO (V.Vector SerialisedBlob) #-}
833832
retrieveBlobs ::
834-
(MonadFix m, MonadMask m, MonadST m, MonadSTM m)
833+
(MonadMask m, MonadST m, MonadSTM m)
835834
=> Session m h
836-
-> V.Vector (WeakBlobRef m (FS.Handle h))
835+
-> V.Vector (WeakBlobRef m h)
837836
-> m (V.Vector SerialisedBlob)
838837
retrieveBlobs sesh wrefs =
839838
withOpenSession sesh $ \seshEnv ->
@@ -1023,7 +1022,7 @@ closeCursor Cursor {..} = do
10231022
ResolveSerialisedValue
10241023
-> Int
10251024
-> Cursor IO h
1026-
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef IO (Handle h)) -> res)
1025+
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef IO h) -> res)
10271026
-> IO (V.Vector res) #-}
10281027
-- | See 'Database.LSMTree.Normal.readCursor'.
10291028
readCursor ::
@@ -1032,7 +1031,7 @@ readCursor ::
10321031
=> ResolveSerialisedValue
10331032
-> Int -- ^ Maximum number of entries to read
10341033
-> Cursor m h
1035-
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef m (Handle h)) -> res)
1034+
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef m h) -> res)
10361035
-- ^ How to map to a query result, different for normal/monoidal
10371036
-> m (V.Vector res)
10381037
readCursor resolve n cursor fromEntry =
@@ -1043,7 +1042,7 @@ readCursor resolve n cursor fromEntry =
10431042
-> (SerialisedKey -> Bool)
10441043
-> Int
10451044
-> Cursor IO h
1046-
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef IO (Handle h)) -> res)
1045+
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef IO h) -> res)
10471046
-> IO (V.Vector res) #-}
10481047
-- | @readCursorWhile _ p n cursor _@ reads elements until either:
10491048
--
@@ -1060,7 +1059,7 @@ readCursorWhile ::
10601059
-> (SerialisedKey -> Bool) -- ^ Only read as long as this predicate holds
10611060
-> Int -- ^ Maximum number of entries to read
10621061
-> Cursor m h
1063-
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef m (Handle h)) -> res)
1062+
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef m h) -> res)
10641063
-- ^ How to map to a query result, different for normal/monoidal
10651064
-> m (V.Vector res)
10661065
readCursorWhile resolve keyIsWanted n Cursor {..} fromEntry = do

src/Database/LSMTree/Internal/BlobRef.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import qualified System.FS.BlockIO.API as FS
4242
--
4343
-- See 'Database.LSMTree.Common.BlobRef' for more info.
4444
data BlobRef m h = BlobRef {
45-
blobRefFile :: !h
45+
blobRefFile :: !(FS.Handle h)
4646
, blobRefCount :: {-# UNPACK #-} !(RefCounter m)
4747
, blobRefSpan :: {-# UNPACK #-} !BlobSpan
4848
}
@@ -141,12 +141,12 @@ removeReferences = V.mapM_ removeReference
141141

142142
{-# SPECIALISE readBlob ::
143143
HasFS IO h
144-
-> BlobRef IO (FS.Handle h)
144+
-> BlobRef IO h
145145
-> IO SerialisedBlob #-}
146146
readBlob ::
147147
(MonadThrow m, PrimMonad m)
148148
=> HasFS m h
149-
-> BlobRef m (FS.Handle h)
149+
-> BlobRef m h
150150
-> m SerialisedBlob
151151
readBlob fs BlobRef {
152152
blobRefFile,
@@ -164,7 +164,7 @@ readBlob fs BlobRef {
164164

165165
readBlobIOOp ::
166166
P.MutableByteArray s -> Int
167-
-> BlobRef m (FS.Handle h)
167+
-> BlobRef m h
168168
-> FS.IOOp s h
169169
readBlobIOOp buf bufoff
170170
BlobRef {

src/Database/LSMTree/Internal/Cursor.hs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import Control.Monad.Class.MonadST (MonadST (..))
99
import Control.Monad.Class.MonadThrow
1010
import Control.Monad.Fix (MonadFix)
1111
import qualified Data.Vector as V
12-
import Database.LSMTree.Internal.BlobRef (WeakBlobRef (..))
13-
import qualified Database.LSMTree.Internal.BlobRef as BlobRef
12+
import Database.LSMTree.Internal.BlobRef (BlobRef,
13+
WeakBlobRef (..))
1414
import Database.LSMTree.Internal.Entry (Entry)
1515
import qualified Database.LSMTree.Internal.Entry as Entry
1616
import Database.LSMTree.Internal.Lookup (ResolveSerialisedValue)
@@ -19,13 +19,12 @@ import qualified Database.LSMTree.Internal.RunReaders as Readers
1919
import Database.LSMTree.Internal.Serialise (SerialisedKey,
2020
SerialisedValue)
2121
import qualified Database.LSMTree.Internal.Vector as V
22-
import System.FS.API (Handle)
2322

2423
{-# INLINE readEntriesWhile #-}
2524
{-# SPECIALISE readEntriesWhile :: forall h res.
2625
ResolveSerialisedValue
2726
-> (SerialisedKey -> Bool)
28-
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef IO (Handle h)) -> res)
27+
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef IO h) -> res)
2928
-> Readers.Readers IO h
3029
-> Int
3130
-> IO (V.Vector res, Readers.HasMore) #-}
@@ -39,7 +38,7 @@ readEntriesWhile :: forall h m res.
3938
(MonadFix m, MonadMask m, MonadST m, MonadSTM m)
4039
=> ResolveSerialisedValue
4140
-> (SerialisedKey -> Bool)
42-
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef m (Handle h)) -> res)
41+
-> (SerialisedKey -> SerialisedValue -> Maybe (WeakBlobRef m h) -> res)
4342
-> Readers.Readers m h
4443
-> Int
4544
-> m (V.Vector res, Readers.HasMore)
@@ -107,7 +106,7 @@ readEntriesWhile resolve keyIsWanted fromEntry readers n =
107106
-- Once we have a resolved entry, we still have to make sure it's not
108107
-- a 'Delete', since we only want to write values to the result vector.
109108
handleResolved :: SerialisedKey
110-
-> Entry SerialisedValue (BlobRef.BlobRef m (Handle h))
109+
-> Entry SerialisedValue (BlobRef m h)
111110
-> Readers.HasMore
112111
-> m (Maybe res, Readers.HasMore)
113112
handleResolved key entry hasMore =
@@ -123,7 +122,7 @@ readEntriesWhile resolve keyIsWanted fromEntry readers n =
123122
Readers.Drained -> return (Nothing, Readers.Drained)
124123

125124
toResult :: SerialisedKey
126-
-> Entry SerialisedValue (BlobRef.BlobRef m (Handle h))
125+
-> Entry SerialisedValue (BlobRef m h)
127126
-> Maybe res
128127
toResult key = \case
129128
Entry.Insert v -> Just $ fromEntry key v Nothing

src/Database/LSMTree/Internal/Lookup.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ data ByteCountDiscrepancy = ByteCountDiscrepancy {
163163
-> V.Vector IndexCompact
164164
-> V.Vector (Handle h)
165165
-> V.Vector SerialisedKey
166-
-> IO (V.Vector (Maybe (Entry SerialisedValue (WeakBlobRef IO (Handle h)))))
166+
-> IO (V.Vector (Maybe (Entry SerialisedValue (WeakBlobRef IO h))))
167167
#-}
168168
-- | Batched lookups in I\/O.
169169
--
@@ -183,7 +183,7 @@ lookupsIO ::
183183
-> V.Vector IndexCompact -- ^ The indexes inside @rs@
184184
-> V.Vector (Handle h) -- ^ The file handles to the key\/value files inside @rs@
185185
-> V.Vector SerialisedKey
186-
-> m (V.Vector (Maybe (Entry SerialisedValue (WeakBlobRef m (Handle h)))))
186+
-> m (V.Vector (Maybe (Entry SerialisedValue (WeakBlobRef m h))))
187187
lookupsIO !hbio !mgr !resolveV !wb !wbblobs !rs !blooms !indexes !kopsFiles !ks =
188188
assert precondition $
189189
withArena mgr $ \arena -> do
@@ -208,7 +208,7 @@ lookupsIO !hbio !mgr !resolveV !wb !wbblobs !rs !blooms !indexes !kopsFiles !ks
208208
-> VP.Vector RunIxKeyIx
209209
-> V.Vector (IOOp RealWorld h)
210210
-> VU.Vector IOResult
211-
-> IO (V.Vector (Maybe (Entry SerialisedValue (WeakBlobRef IO (Handle h)))))
211+
-> IO (V.Vector (Maybe (Entry SerialisedValue (WeakBlobRef IO h))))
212212
#-}
213213
-- | Intra-page lookups, and combining lookup results from multiple runs and
214214
-- the write buffer.
@@ -227,7 +227,7 @@ intraPageLookups ::
227227
-> VP.Vector RunIxKeyIx
228228
-> V.Vector (IOOp (PrimState m) h)
229229
-> VU.Vector IOResult
230-
-> m (V.Vector (Maybe (Entry SerialisedValue (WeakBlobRef m (Handle h)))))
230+
-> m (V.Vector (Maybe (Entry SerialisedValue (WeakBlobRef m h))))
231231
intraPageLookups !resolveV !wb !wbblobs !rs !ks !rkixs !ioops !ioress = do
232232
-- We accumulate results into the 'res' vector. When there are several
233233
-- lookup hits for the same key then we combine the results. The combining
@@ -256,7 +256,7 @@ intraPageLookups !resolveV !wb !wbblobs !rs !ks !rkixs !ioops !ioress = do
256256

257257
loop ::
258258
VM.MVector (PrimState m)
259-
(Maybe (Entry SerialisedValue (WeakBlobRef m (Handle h))))
259+
(Maybe (Entry SerialisedValue (WeakBlobRef m h)))
260260
-> Int
261261
-> m ()
262262
loop !res !ioopix

src/Database/LSMTree/Internal/Merge.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import Database.LSMTree.Internal.RunReaders (Readers)
4545
import qualified Database.LSMTree.Internal.RunReaders as Readers
4646
import Database.LSMTree.Internal.Serialise
4747
import GHC.Stack (HasCallStack)
48-
import qualified System.FS.API as FS
4948
import System.FS.API (HasFS)
5049
import System.FS.BlockIO.API (HasBlockIO)
5150

@@ -357,14 +356,14 @@ steps Merge {..} requestedSteps = assertStepsInvariant <$> do
357356
Level
358357
-> RunBuilder IO h
359358
-> SerialisedKey
360-
-> Reader.Entry IO (FS.Handle h)
359+
-> Reader.Entry IO h
361360
-> IO () #-}
362361
writeReaderEntry ::
363362
(MonadSTM m, MonadST m, MonadThrow m)
364363
=> Level
365364
-> RunBuilder m h
366365
-> SerialisedKey
367-
-> Reader.Entry m (FS.Handle h)
366+
-> Reader.Entry m h
368367
-> m ()
369368
writeReaderEntry level builder key (Reader.Entry entryFull) =
370369
-- Small entry.
@@ -397,14 +396,14 @@ writeReaderEntry level builder key entry@(Reader.EntryOverflow prefix page _ ove
397396
Level
398397
-> RunBuilder IO h
399398
-> SerialisedKey
400-
-> Entry SerialisedValue (BlobRef IO (FS.Handle h))
399+
-> Entry SerialisedValue (BlobRef IO h)
401400
-> IO () #-}
402401
writeSerialisedEntry ::
403402
(MonadSTM m, MonadST m, MonadThrow m)
404403
=> Level
405404
-> RunBuilder m h
406405
-> SerialisedKey
407-
-> Entry SerialisedValue (BlobRef m (FS.Handle h))
406+
-> Entry SerialisedValue (BlobRef m h)
408407
-> m ()
409408
writeSerialisedEntry level builder key entry =
410409
when (shouldWriteEntry level entry) $

src/Database/LSMTree/Internal/Run.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ removeReferenceN :: (PrimMonad m, MonadMask m) => Run m h -> Word64 -> m ()
144144
removeReferenceN r = RC.removeReferenceN (runRefCounter r)
145145

146146
-- | Helper function to make a 'BlobRef' that points into a 'Run'.
147-
mkBlobRefForRun :: Run m h -> BlobSpan -> BlobRef m (FS.Handle h)
147+
mkBlobRefForRun :: Run m h -> BlobSpan -> BlobRef m h
148148
mkBlobRefForRun Run{runBlobFile} blobRefSpan =
149149
BlobRef {
150150
blobRefFile = blobFileHandle runBlobFile,

src/Database/LSMTree/Internal/RunBuilder.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ new fs hbio runBuilderFsPaths numEntries alloc = do
106106
{-# SPECIALISE addKeyOp ::
107107
RunBuilder IO h
108108
-> SerialisedKey
109-
-> Entry SerialisedValue (BlobRef IO (FS.Handle h))
109+
-> Entry SerialisedValue (BlobRef IO h)
110110
-> IO () #-}
111111
-- | Add a key\/op pair.
112112
--
@@ -125,7 +125,7 @@ addKeyOp ::
125125
(MonadST m, MonadSTM m, MonadThrow m)
126126
=> RunBuilder m h
127127
-> SerialisedKey
128-
-> Entry SerialisedValue (BlobRef m (FS.Handle h))
128+
-> Entry SerialisedValue (BlobRef m h)
129129
-> m ()
130130
addKeyOp builder@RunBuilder{runBuilderAcc} key op = do
131131
-- TODO: the fmap entry here reallocates even when there are no blobs.
@@ -275,13 +275,13 @@ writeBlob RunBuilder{..} blob = do
275275

276276
{-# SPECIALISE copyBlob ::
277277
RunBuilder IO h
278-
-> BlobRef IO (FS.Handle h)
279-
-> IO BlobRef.BlobSpan #-}
278+
-> BlobRef IO h
279+
-> IO BlobSpan #-}
280280
copyBlob ::
281281
(MonadSTM m, MonadThrow m, PrimMonad m)
282282
=> RunBuilder m h
283-
-> BlobRef m (FS.Handle h)
284-
-> m BlobRef.BlobSpan
283+
-> BlobRef m h
284+
-> m BlobSpan
285285
copyBlob builder@RunBuilder {..} blobref = do
286286
blob <- BlobRef.readBlob runBuilderHasFS blobref
287287
writeBlob builder blob

src/Database/LSMTree/Internal/RunReader.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,13 @@ appendOverflow len overflowPages (SerialisedValue prefix) =
214214

215215
{-# SPECIALISE next ::
216216
RunReader IO h
217-
-> IO (Result IO (FS.Handle h)) #-}
217+
-> IO (Result IO h) #-}
218218
-- | Stop using the 'RunReader' after getting 'Empty', because the 'Reader' is
219219
-- automatically closed!
220220
next :: forall m h.
221221
(MonadCatch m, MonadSTM m, MonadST m)
222222
=> RunReader m h
223-
-> m (Result m (FS.Handle h))
223+
-> m (Result m h)
224224
next reader@RunReader {..} = do
225225
readMutVar readerCurrentPage >>= \case
226226
Nothing ->
@@ -229,7 +229,7 @@ next reader@RunReader {..} = do
229229
entryNo <- readPrimVar readerCurrentEntryNo
230230
go entryNo page
231231
where
232-
go :: Word16 -> RawPage -> m (Result m (FS.Handle h))
232+
go :: Word16 -> RawPage -> m (Result m h)
233233
go !entryNo !page =
234234
-- take entry from current page (resolve blob if necessary)
235235
case rawPageIndex page entryNo of
@@ -252,7 +252,7 @@ next reader@RunReader {..} = do
252252
IndexEntryOverflow key entry lenSuffix -> do
253253
-- TODO: we know that we need the next page, could already load?
254254
modifyPrimVar readerCurrentEntryNo (+1)
255-
let entry' :: E.Entry SerialisedValue (BlobRef m (FS.Handle h))
255+
let entry' :: E.Entry SerialisedValue (BlobRef m h)
256256
entry' = fmap (Run.mkBlobRefForRun readerRun) entry
257257
overflowPages <- readOverflowPages readerHasFS readerKOpsHandle lenSuffix
258258
let rawEntry = mkEntryOverflow entry' page lenSuffix overflowPages

0 commit comments

Comments
 (0)