44{- HLINT ignore "Use unless" -}
55
66module 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
3737import System.FS.API (HasFS )
3838import 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
5561blobRefSpanSize = 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
8392withWeakBlobRef ::
8493 (MonadMask m , PrimMonad m )
8594 => WeakBlobRef m h
86- -> (BlobRef m h -> m a )
95+ -> (RawBlobRef m h -> m a )
8796 -> m a
8897withWeakBlobRef 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--
96105withWeakBlobRefs ::
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
101110withWeakBlobRefs wrefs = bracket (deRefWeakBlobRefs wrefs) removeReferences
102111
103112{-# SPECIALISE deRefWeakBlobRef ::
104113 WeakBlobRef IO h
105- -> IO (BlobRef IO h) #-}
114+ -> IO (RawBlobRef IO h) #-}
106115deRefWeakBlobRef ::
107116 (MonadThrow m , PrimMonad m )
108117 => WeakBlobRef m h
109- -> m (BlobRef m h )
118+ -> m (RawBlobRef m h )
110119deRefWeakBlobRef (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)) #-}
118127deRefWeakBlobRefs ::
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 ))
123132deRefWeakBlobRefs 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 ()
136145removeReference = 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 ()
140149removeReferences = V. mapM_ removeReference
141150
142151{-# SPECIALISE readBlob ::
143152 HasFS IO h
144- -> BlobRef IO h
153+ -> RawBlobRef IO h
145154 -> IO SerialisedBlob #-}
146155readBlob ::
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
165174readBlobIOOp ::
166175 P. MutableByteArray s -> Int
167- -> BlobRef m h
176+ -> RawBlobRef m h
168177 -> FS. IOOp s h
169178readBlobIOOp buf bufoff
170- BlobRef {
179+ RawBlobRef {
171180 blobRefFile,
172181 blobRefSpan = BlobSpan {blobSpanOffset, blobSpanSize}
173182 } =
0 commit comments