Skip to content

Commit d49ccbc

Browse files
committed
Rename internal BlobRef type to RawBlobRef
To better reflect that it does not itself maintain a reference count, and to distinguish it from a new type we will introduce soon: a strong blob ref. Then we will have a clear type distinction: raw, weak and strong.
1 parent 0034666 commit d49ccbc

File tree

9 files changed

+69
-60
lines changed

9 files changed

+69
-60
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,9 @@ deriving anyclass instance NoThunks RawOverflowPage
417417
BlobRef
418418
-------------------------------------------------------------------------------}
419419

420-
deriving stock instance Generic (BlobRef m h)
420+
deriving stock instance Generic (RawBlobRef m h)
421421
deriving anyclass instance (Typeable h, Typeable (PrimState m))
422-
=> NoThunks (BlobRef m h)
422+
=> NoThunks (RawBlobRef m h)
423423

424424
deriving stock instance Generic BlobSpan
425425
deriving anyclass instance NoThunks BlobSpan

src/Database/LSMTree/Internal/BlobRef.hs

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{- HLINT ignore "Use unless" -}
55

66
module Database.LSMTree.Internal.BlobRef (
7-
BlobRef (..)
7+
RawBlobRef (..)
88
, BlobSpan (..)
99
, blobRefSpanSize
1010
, WeakBlobRef (..)
@@ -37,31 +37,40 @@ import qualified System.FS.API as FS
3737
import System.FS.API (HasFS)
3838
import qualified System.FS.BlockIO.API as FS
3939

40-
-- | A handle-like reference to an on-disk blob. The blob can be retrieved based
41-
-- on the reference.
40+
41+
-- | A raw blob reference is a reference to a blob within a blob file.
4242
--
43-
-- See 'Database.LSMTree.Common.BlobRef' for more info.
44-
data BlobRef m h = BlobRef {
43+
-- The \"raw\" means that it does no reference counting, so does not maintain
44+
-- ownership of the 'BlobFile'. Thus these are only safe to use in the context
45+
-- of code that already (directly or indirectly) owns the blob file that the
46+
-- blob ref uses (such as within run merging).
47+
--
48+
-- Thus these cannot be handed out via the API. Use 'WeakBlobRef' for that.
49+
--
50+
data RawBlobRef m h = RawBlobRef {
4551
blobRefFile :: !(FS.Handle h)
4652
, blobRefCount :: {-# UNPACK #-} !(RefCounter m)
4753
, blobRefSpan :: {-# UNPACK #-} !BlobSpan
4854
}
4955
deriving stock (Show)
5056

51-
instance NFData h => NFData (BlobRef m h) where
52-
rnf (BlobRef a b c) = rnf a `seq` rnf b `seq` rnf c
57+
instance NFData h => NFData (RawBlobRef m h) where
58+
rnf (RawBlobRef a b c) = rnf a `seq` rnf b `seq` rnf c
5359

54-
blobRefSpanSize :: BlobRef m h -> Int
60+
blobRefSpanSize :: RawBlobRef m h -> Int
5561
blobRefSpanSize = fromIntegral . blobSpanSize . blobRefSpan
5662

57-
-- | A 'WeakBlobRef' is a weak reference to a blob file. These are the ones we
58-
-- can return in the public API and can outlive their parent table. They do not
59-
-- keep the file open using a reference count. So when we want to use our weak
60-
-- reference we have to dereference them to obtain a normal strong reference
61-
-- while we do the I\/O to read the blob. This ensures the file is not closed
62-
-- under our feet.
63+
-- | A \"weak\" reference to a blob within a blob file. These are the ones we
64+
-- can return in the public API and can outlive their parent table.
65+
--
66+
-- They are weak references in that they do not keep the file open using a
67+
-- reference count. So when we want to use our weak reference we have to
68+
-- dereference them to obtain a normal strong reference while we do the I\/O
69+
-- to read the blob. This ensures the file is not closed under our feet.
70+
--
71+
-- See 'Database.LSMTree.Common.BlobRef' for more info.
6372
--
64-
newtype WeakBlobRef m h = WeakBlobRef (BlobRef m h)
73+
newtype WeakBlobRef m h = WeakBlobRef (RawBlobRef m h)
6574
deriving newtype (Show, NFData)
6675

6776
-- | The 'WeakBlobRef' now points to a blob that is no longer available.
@@ -71,7 +80,7 @@ newtype WeakBlobRefInvalid = WeakBlobRefInvalid Int
7180

7281
{-# SPECIALISE withWeakBlobRef ::
7382
WeakBlobRef IO h
74-
-> (BlobRef IO h -> IO a)
83+
-> (RawBlobRef IO h -> IO a)
7584
-> IO a #-}
7685
-- | 'WeakBlobRef's are weak references. They do not keep the blob file open.
7786
-- Dereference a 'WeakBlobRef' to a strong 'BlobRef' to allow I\/O using
@@ -83,45 +92,45 @@ newtype WeakBlobRefInvalid = WeakBlobRefInvalid Int
8392
withWeakBlobRef ::
8493
(MonadMask m, PrimMonad m)
8594
=> WeakBlobRef m h
86-
-> (BlobRef m h -> m a)
95+
-> (RawBlobRef m h -> m a)
8796
-> m a
8897
withWeakBlobRef wref = bracket (deRefWeakBlobRef wref) removeReference
8998

9099
{-# SPECIALISE withWeakBlobRefs ::
91100
V.Vector (WeakBlobRef IO h)
92-
-> (V.Vector (BlobRef IO h) -> IO a)
101+
-> (V.Vector (RawBlobRef IO h) -> IO a)
93102
-> IO a #-}
94103
-- | The same as 'withWeakBlobRef' but for many references in one go.
95104
--
96105
withWeakBlobRefs ::
97106
(MonadMask m, PrimMonad m)
98107
=> V.Vector (WeakBlobRef m h)
99-
-> (V.Vector (BlobRef m h) -> m a)
108+
-> (V.Vector (RawBlobRef m h) -> m a)
100109
-> m a
101110
withWeakBlobRefs wrefs = bracket (deRefWeakBlobRefs wrefs) removeReferences
102111

103112
{-# SPECIALISE deRefWeakBlobRef ::
104113
WeakBlobRef IO h
105-
-> IO (BlobRef IO h) #-}
114+
-> IO (RawBlobRef IO h) #-}
106115
deRefWeakBlobRef ::
107116
(MonadThrow m, PrimMonad m)
108117
=> WeakBlobRef m h
109-
-> m (BlobRef m h)
118+
-> m (RawBlobRef m h)
110119
deRefWeakBlobRef (WeakBlobRef ref) = do
111120
ok <- RC.upgradeWeakReference (blobRefCount ref)
112121
when (not ok) $ throwIO (WeakBlobRefInvalid 0)
113122
pure ref
114123

115124
{-# SPECIALISE deRefWeakBlobRefs ::
116125
V.Vector (WeakBlobRef IO h)
117-
-> IO (V.Vector (BlobRef IO h)) #-}
126+
-> IO (V.Vector (RawBlobRef IO h)) #-}
118127
deRefWeakBlobRefs ::
119128
forall m h.
120129
(MonadMask m, PrimMonad m)
121130
=> V.Vector (WeakBlobRef m h)
122-
-> m (V.Vector (BlobRef m h))
131+
-> m (V.Vector (RawBlobRef m h))
123132
deRefWeakBlobRefs wrefs = do
124-
let refs :: V.Vector (BlobRef m h)
133+
let refs :: V.Vector (RawBlobRef m h)
125134
refs = coerce wrefs -- safely coerce away the newtype wrappers
126135
V.iforM_ wrefs $ \i (WeakBlobRef ref) -> do
127136
ok <- RC.upgradeWeakReference (blobRefCount ref)
@@ -131,24 +140,24 @@ deRefWeakBlobRefs wrefs = do
131140
throwIO (WeakBlobRefInvalid i)
132141
pure refs
133142

134-
{-# SPECIALISE removeReference :: BlobRef IO h -> IO () #-}
135-
removeReference :: (MonadMask m, PrimMonad m) => BlobRef m h -> m ()
143+
{-# SPECIALISE removeReference :: RawBlobRef IO h -> IO () #-}
144+
removeReference :: (MonadMask m, PrimMonad m) => RawBlobRef m h -> m ()
136145
removeReference = RC.removeReference . blobRefCount
137146

138-
{-# SPECIALISE removeReferences :: V.Vector (BlobRef IO h) -> IO () #-}
139-
removeReferences :: (MonadMask m, PrimMonad m) => V.Vector (BlobRef m h) -> m ()
147+
{-# SPECIALISE removeReferences :: V.Vector (RawBlobRef IO h) -> IO () #-}
148+
removeReferences :: (MonadMask m, PrimMonad m) => V.Vector (RawBlobRef m h) -> m ()
140149
removeReferences = V.mapM_ removeReference
141150

142151
{-# SPECIALISE readBlob ::
143152
HasFS IO h
144-
-> BlobRef IO h
153+
-> RawBlobRef IO h
145154
-> IO SerialisedBlob #-}
146155
readBlob ::
147156
(MonadThrow m, PrimMonad m)
148157
=> HasFS m h
149-
-> BlobRef m h
158+
-> RawBlobRef m h
150159
-> m SerialisedBlob
151-
readBlob fs BlobRef {
160+
readBlob fs RawBlobRef {
152161
blobRefFile,
153162
blobRefSpan = BlobSpan {blobSpanOffset, blobSpanSize}
154163
} = do
@@ -164,10 +173,10 @@ readBlob fs BlobRef {
164173

165174
readBlobIOOp ::
166175
P.MutableByteArray s -> Int
167-
-> BlobRef m h
176+
-> RawBlobRef m h
168177
-> FS.IOOp s h
169178
readBlobIOOp buf bufoff
170-
BlobRef {
179+
RawBlobRef {
171180
blobRefFile,
172181
blobRefSpan = BlobSpan {blobSpanOffset, blobSpanSize}
173182
} =

src/Database/LSMTree/Internal/Cursor.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ 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 (BlobRef,
12+
import Database.LSMTree.Internal.BlobRef (RawBlobRef,
1313
WeakBlobRef (..))
1414
import Database.LSMTree.Internal.Entry (Entry)
1515
import qualified Database.LSMTree.Internal.Entry as Entry
@@ -106,7 +106,7 @@ readEntriesWhile resolve keyIsWanted fromEntry readers n =
106106
-- Once we have a resolved entry, we still have to make sure it's not
107107
-- a 'Delete', since we only want to write values to the result vector.
108108
handleResolved :: SerialisedKey
109-
-> Entry SerialisedValue (BlobRef m h)
109+
-> Entry SerialisedValue (RawBlobRef m h)
110110
-> Readers.HasMore
111111
-> m (Maybe res, Readers.HasMore)
112112
handleResolved key entry hasMore =
@@ -122,7 +122,7 @@ readEntriesWhile resolve keyIsWanted fromEntry readers n =
122122
Readers.Drained -> return (Nothing, Readers.Drained)
123123

124124
toResult :: SerialisedKey
125-
-> Entry SerialisedValue (BlobRef m h)
125+
-> Entry SerialisedValue (RawBlobRef m h)
126126
-> Maybe res
127127
toResult key = \case
128128
Entry.Insert v -> Just $ fromEntry key v Nothing

src/Database/LSMTree/Internal/Merge.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import Data.Primitive.MutVar
3333
import Data.Traversable (for)
3434
import qualified Data.Vector as V
3535
import Data.Word
36-
import Database.LSMTree.Internal.BlobRef (BlobRef)
36+
import Database.LSMTree.Internal.BlobRef (RawBlobRef)
3737
import Database.LSMTree.Internal.Entry
3838
import Database.LSMTree.Internal.Run (Run, RunDataCaching)
3939
import qualified Database.LSMTree.Internal.Run as Run
@@ -396,14 +396,14 @@ writeReaderEntry level builder key entry@(Reader.EntryOverflow prefix page _ ove
396396
Level
397397
-> RunBuilder IO h
398398
-> SerialisedKey
399-
-> Entry SerialisedValue (BlobRef IO h)
399+
-> Entry SerialisedValue (RawBlobRef IO h)
400400
-> IO () #-}
401401
writeSerialisedEntry ::
402402
(MonadSTM m, MonadST m, MonadThrow m)
403403
=> Level
404404
-> RunBuilder m h
405405
-> SerialisedKey
406-
-> Entry SerialisedValue (BlobRef m h)
406+
-> Entry SerialisedValue (RawBlobRef m h)
407407
-> m ()
408408
writeSerialisedEntry level builder key entry =
409409
when (shouldWriteEntry level entry) $

src/Database/LSMTree/Internal/Run.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ 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 h
147+
mkBlobRefForRun :: Run m h -> BlobSpan -> RawBlobRef m h
148148
mkBlobRefForRun Run{runBlobFile} blobRefSpan =
149-
BlobRef {
149+
RawBlobRef {
150150
blobRefFile = blobFileHandle runBlobFile,
151151
blobRefCount = blobFileRefCounter runBlobFile,
152152
blobRefSpan

src/Database/LSMTree/Internal/RunBuilder.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import qualified Data.ByteString.Lazy as BSL
2222
import Data.Foldable (for_, traverse_)
2323
import Data.Primitive.PrimVar
2424
import Data.Word (Word64)
25-
import Database.LSMTree.Internal.BlobRef (BlobRef, BlobSpan (..))
25+
import Database.LSMTree.Internal.BlobRef (RawBlobRef, BlobSpan (..))
2626
import qualified Database.LSMTree.Internal.BlobRef as BlobRef
2727
import Database.LSMTree.Internal.BloomFilter (bloomFilterToLBS)
2828
import Database.LSMTree.Internal.CRC32C (CRC32C)
@@ -106,11 +106,11 @@ new fs hbio runBuilderFsPaths numEntries alloc = do
106106
{-# SPECIALISE addKeyOp ::
107107
RunBuilder IO h
108108
-> SerialisedKey
109-
-> Entry SerialisedValue (BlobRef IO h)
109+
-> Entry SerialisedValue (RawBlobRef IO h)
110110
-> IO () #-}
111111
-- | Add a key\/op pair.
112112
--
113-
-- In the 'InsertWithBlob' case, the 'BlobRef' identifies where the blob can be
113+
-- In the 'InsertWithBlob' case, the 'RawBlobRef' identifies where the blob can be
114114
-- found (which is either from a write buffer or another run). The blobs will
115115
-- be copied from their existing blob file into the new run's blob file.
116116
--
@@ -125,7 +125,7 @@ addKeyOp ::
125125
(MonadST m, MonadSTM m, MonadThrow m)
126126
=> RunBuilder m h
127127
-> SerialisedKey
128-
-> Entry SerialisedValue (BlobRef m h)
128+
-> Entry SerialisedValue (RawBlobRef 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,12 +275,12 @@ writeBlob RunBuilder{..} blob = do
275275

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

src/Database/LSMTree/Internal/RunReader.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import Data.Primitive.PrimVar
2929
import Data.Word (Word16, Word32)
3030
import Database.LSMTree.Internal.BitMath (ceilDivPageSize,
3131
mulPageSize, roundUpToPageSize)
32-
import Database.LSMTree.Internal.BlobRef (BlobRef (..))
32+
import Database.LSMTree.Internal.BlobRef (RawBlobRef (..))
3333
import qualified Database.LSMTree.Internal.Entry as E
3434
import qualified Database.LSMTree.Internal.IndexCompact as Index
3535
import Database.LSMTree.Internal.Page (PageNo (..), PageSpan (..),
@@ -167,12 +167,12 @@ data Result m h
167167

168168
data Entry m h =
169169
Entry
170-
!(E.Entry SerialisedValue (BlobRef m h))
170+
!(E.Entry SerialisedValue (RawBlobRef m h))
171171
| -- | A large entry. The caller might be interested in various different
172172
-- (redundant) representation, so we return all of them.
173173
EntryOverflow
174174
-- | The value is just a prefix, with the remainder in the overflow pages.
175-
!(E.Entry SerialisedValue (BlobRef m h))
175+
!(E.Entry SerialisedValue (RawBlobRef m h))
176176
-- | A page containing the single entry (or rather its prefix).
177177
!RawPage
178178
-- | Non-zero length of the overflow in bytes.
@@ -186,7 +186,7 @@ data Entry m h =
186186
![RawOverflowPage]
187187

188188
mkEntryOverflow ::
189-
E.Entry SerialisedValue (BlobRef m h)
189+
E.Entry SerialisedValue (RawBlobRef m h)
190190
-> RawPage
191191
-> Word32
192192
-> [RawOverflowPage]
@@ -198,7 +198,7 @@ mkEntryOverflow entryPrefix page len overflowPages =
198198
EntryOverflow entryPrefix page len overflowPages
199199

200200
{-# INLINE toFullEntry #-}
201-
toFullEntry :: Entry m h -> E.Entry SerialisedValue (BlobRef m h)
201+
toFullEntry :: Entry m h -> E.Entry SerialisedValue (RawBlobRef m h)
202202
toFullEntry = \case
203203
Entry e ->
204204
e
@@ -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 h)
255+
let entry' :: E.Entry SerialisedValue (RawBlobRef m h)
256256
entry' = fmap (Run.mkBlobRefForRun readerRun) entry
257257
overflowPages <- readOverflowPages readerHasFS readerKOpsHandle lenSuffix
258258
let rawEntry = mkEntryOverflow entry' page lenSuffix overflowPages

src/Database/LSMTree/Internal/RunReaders.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import Data.Maybe (catMaybes)
2626
import Data.Primitive.MutVar
2727
import Data.Traversable (for)
2828
import qualified Data.Vector as V
29-
import Database.LSMTree.Internal.BlobRef (BlobRef)
29+
import Database.LSMTree.Internal.BlobRef (RawBlobRef)
3030
import Database.LSMTree.Internal.Entry (Entry (..))
3131
import Database.LSMTree.Internal.Run (Run)
3232
import Database.LSMTree.Internal.RunReader (OffsetKey (..),
@@ -105,7 +105,7 @@ data Reader m h =
105105
-- TODO: more efficient representation? benchmark!
106106
| ReadBuffer !(MutVar (PrimState m) [KOp m h])
107107

108-
type KOp m h = (SerialisedKey, Entry SerialisedValue (BlobRef m h))
108+
type KOp m h = (SerialisedKey, Entry SerialisedValue (RawBlobRef m h))
109109

110110
{-# SPECIALISE new ::
111111
OffsetKey

src/Database/LSMTree/Internal/WriteBufferBlobs.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import qualified Data.Vector.Primitive as VP
4343
import Data.Word (Word64)
4444
import Database.LSMTree.Internal.BlobFile hiding (removeReference)
4545
import qualified Database.LSMTree.Internal.BlobFile as BlobFile
46-
import Database.LSMTree.Internal.BlobRef (BlobRef (..))
46+
import Database.LSMTree.Internal.BlobRef (RawBlobRef (..))
4747
import Database.LSMTree.Internal.Serialise
4848
import qualified System.FS.API as FS
4949
import System.FS.API (HasFS)
@@ -181,9 +181,9 @@ readBlob fs WriteBufferBlobs {blobFile} blobspan =
181181
-- | Helper function to make a 'BlobRef' that points into a 'WriteBufferBlobs'.
182182
mkBlobRef :: WriteBufferBlobs m h
183183
-> BlobSpan
184-
-> BlobRef m h
184+
-> RawBlobRef m h
185185
mkBlobRef WriteBufferBlobs {blobFile} blobRefSpan =
186-
BlobRef {
186+
RawBlobRef {
187187
blobRefFile = blobFileHandle blobFile,
188188
blobRefCount = blobFileRefCounter blobFile,
189189
blobRefSpan

0 commit comments

Comments
 (0)