@@ -29,7 +29,6 @@ module Ouroboros.Consensus.Storage.LedgerDB.V2.InMemory
2929 , implTakeSnapshot
3030 ) where
3131
32- import Cardano.Binary as CBOR
3332import qualified Codec.CBOR.Write as CBOR
3433import Codec.Serialise (decode )
3534import qualified Control.Monad as Monad
@@ -100,59 +99,138 @@ newInMemoryLedgerTablesHandle tracer someFS@(SomeHasFS hasFS) l = do
10099 traceWith tracer V2. TraceLedgerTablesHandleCreate
101100 pure
102101 LedgerTablesHandle
103- { close = do
104- p <- atomically $ swapTVar tv LedgerTablesHandleClosed
105- case p of
106- LedgerTablesHandleOpen {} -> traceWith tracer V2. TraceLedgerTablesHandleClose
107- _ -> pure ()
108- , duplicate = do
109- hs <- readTVarIO tv
110- ! x <- guardClosed hs $ newInMemoryLedgerTablesHandle tracer someFS
111- pure x
112- , read = \ _ keys -> do
113- hs <- readTVarIO tv
114- guardClosed
115- hs
116- (pure . flip (ltliftA2 (\ (ValuesMK v) (KeysMK k) -> ValuesMK $ v `Map.restrictKeys` k)) keys)
117- , readRange = \ _ (f, t) -> do
118- hs <- readTVarIO tv
102+ { close = implClose tracer tv
103+ , duplicate = implDuplicate tracer tv someFS
104+ , read = implRead tv
105+ , readRange = implReadRange tv
106+ , readAll = implReadAll tv
107+ , pushDiffs = implPushDiffs tv
108+ , takeHandleSnapshot = implTakeHandleSnapshot tv hasFS
109+ , tablesSize = implTablesSize tv
110+ }
111+
112+ {-# INLINE implClose #-}
113+ {-# INLINE implDuplicate #-}
114+ {-# INLINE implRead #-}
115+ {-# INLINE implReadRange #-}
116+ {-# INLINE implReadAll #-}
117+ {-# INLINE implPushDiffs #-}
118+ {-# INLINE implTakeHandleSnapshot #-}
119+ {-# INLINE implTablesSize #-}
120+
121+ implClose ::
122+ IOLike m =>
123+ Tracer m V2. FlavorImplSpecificTrace ->
124+ StrictTVar m (LedgerTablesHandleState l ) ->
125+ m ()
126+ implClose tracer tv = do
127+ p <- atomically $ swapTVar tv LedgerTablesHandleClosed
128+ case p of
129+ LedgerTablesHandleOpen {} -> traceWith tracer V2. TraceLedgerTablesHandleClose
130+ _ -> pure ()
131+
132+ implDuplicate ::
133+ ( IOLike m
134+ , HasLedgerTables l
135+ , CanUpgradeLedgerTables l
136+ , SerializeTablesWithHint l
137+ ) =>
138+ Tracer m V2. FlavorImplSpecificTrace ->
139+ StrictTVar m (LedgerTablesHandleState l ) ->
140+ SomeHasFS m ->
141+ m (LedgerTablesHandle m l )
142+ implDuplicate tracer tv someFS = do
143+ hs <- readTVarIO tv
144+ ! x <- guardClosed hs $ newInMemoryLedgerTablesHandle tracer someFS
145+ pure x
146+
147+ implRead ::
148+ ( IOLike m
149+ , HasLedgerTables l
150+ ) =>
151+ StrictTVar m (LedgerTablesHandleState l ) ->
152+ l EmptyMK ->
153+ LedgerTables l KeysMK ->
154+ m (LedgerTables l ValuesMK )
155+ implRead tv _ keys = do
156+ hs <- readTVarIO tv
157+ guardClosed
158+ hs
159+ (pure . flip (ltliftA2 (\ (ValuesMK v) (KeysMK k) -> ValuesMK $ v `Map.restrictKeys` k)) keys)
160+
161+ implReadRange ::
162+ (IOLike m , HasLedgerTables l ) =>
163+ StrictTVar m (LedgerTablesHandleState l ) ->
164+ l EmptyMK ->
165+ (Maybe (TxIn l ), Int ) ->
166+ m (LedgerTables l ValuesMK , Maybe (TxIn l ))
167+ implReadRange tv _ (f, t) = do
168+ hs <- readTVarIO tv
169+ guardClosed
170+ hs
171+ ( \ (LedgerTables (ValuesMK m)) ->
172+ let m' = Map. take t . (maybe id (\ g -> snd . Map. split g) f) $ m
173+ in pure (LedgerTables (ValuesMK m'), fst <$> Map. lookupMax m')
174+ )
175+
176+ implReadAll ::
177+ IOLike m =>
178+ StrictTVar m (LedgerTablesHandleState l ) ->
179+ l EmptyMK ->
180+ m (LedgerTables l ValuesMK )
181+ implReadAll tv _ = do
182+ hs <- readTVarIO tv
183+ guardClosed hs pure
184+
185+ implPushDiffs ::
186+ ( IOLike m
187+ , HasLedgerTables l
188+ , CanUpgradeLedgerTables l
189+ ) =>
190+ StrictTVar m (LedgerTablesHandleState l ) ->
191+ l mk1 ->
192+ l DiffMK ->
193+ m ()
194+ implPushDiffs tv st0 ! diffs =
195+ atomically $
196+ modifyTVar
197+ tv
198+ ( \ r ->
119199 guardClosed
120- hs
121- ( \ (LedgerTables (ValuesMK m)) ->
122- let m' = Map. take t . (maybe id (\ g -> snd . Map. split g) f) $ m
123- in pure (LedgerTables (ValuesMK m'), fst <$> Map. lookupMax m')
200+ r
201+ ( LedgerTablesHandleOpen
202+ . flip
203+ (ltliftA2 (\ (ValuesMK vals) (DiffMK d) -> ValuesMK (Diff. applyDiff vals d)))
204+ (projectLedgerTables diffs)
205+ . upgradeTables st0 diffs
124206 )
125- , readAll = \ _ -> do
126- hs <- readTVarIO tv
127- guardClosed hs pure
128- , pushDiffs = \ st0 ! diffs ->
129- atomically $
130- modifyTVar
131- tv
132- ( \ r ->
133- guardClosed
134- r
135- ( LedgerTablesHandleOpen
136- . flip
137- (ltliftA2 (\ (ValuesMK vals) (DiffMK d) -> ValuesMK (Diff. applyDiff vals d)))
138- (projectLedgerTables diffs)
139- . upgradeTables st0 diffs
140- )
141- )
142- , takeHandleSnapshot = \ hint snapshotName -> do
143- createDirectoryIfMissing hasFS True $ mkFsPath [snapshotName, " tables" ]
144- h <- readTVarIO tv
145- guardClosed h $
146- \ values ->
147- withFile hasFS (mkFsPath [snapshotName, " tables" , " tvar" ]) (WriteMode MustBeNew ) $ \ hf ->
148- fmap (Just . snd ) $
149- hPutAllCRC hasFS hf $
150- CBOR. toLazyByteString $
151- valuesMKEncoder hint values
152- , tablesSize = do
153- hs <- readTVarIO tv
154- guardClosed hs (pure . Just . Map. size . getValuesMK . getLedgerTables)
155- }
207+ )
208+
209+ implTakeHandleSnapshot ::
210+ (IOLike m , SerializeTablesWithHint l ) =>
211+ StrictTVar m (LedgerTablesHandleState l ) ->
212+ HasFS m h ->
213+ l EmptyMK ->
214+ String ->
215+ m (Maybe CRC )
216+ implTakeHandleSnapshot tv hasFS hint snapshotName = do
217+ createDirectoryIfMissing hasFS True $ mkFsPath [snapshotName, " tables" ]
218+ h <- readTVarIO tv
219+ guardClosed h $
220+ \ values ->
221+ withFile hasFS (mkFsPath [snapshotName, " tables" , " tvar" ]) (WriteMode MustBeNew ) $ \ hf ->
222+ fmap (Just . snd ) $
223+ hPutAllCRC hasFS hf $
224+ CBOR. toLazyByteString $
225+ valuesMKEncoder hint values
226+
227+ implTablesSize ::
228+ IOLike m =>
229+ StrictTVar m (LedgerTablesHandleState l ) ->
230+ m (Maybe Int )
231+ implTablesSize tv = do
232+ hs <- readTVarIO tv
233+ guardClosed hs (pure . Just . Map. size . getValuesMK . getLedgerTables)
156234
157235{- ------------------------------------------------------------------------------
158236 Snapshots
@@ -187,23 +265,7 @@ snapshotManager' ccfg tracer fs =
187265 , takeSnapshot = implTakeSnapshot ccfg tracer fs
188266 }
189267
190- writeSnapshot ::
191- MonadThrow m =>
192- SomeHasFS m ->
193- (ExtLedgerState blk EmptyMK -> Encoding ) ->
194- DiskSnapshot ->
195- StateRef m (ExtLedgerState blk ) ->
196- m ()
197- writeSnapshot fs@ (SomeHasFS hasFs) encLedger ds st = do
198- createDirectoryIfMissing hasFs True $ snapshotToDirPath ds
199- crc1 <- writeExtLedgerState fs encLedger (snapshotToStatePath ds) $ state st
200- crc2 <- takeHandleSnapshot (tables st) (state st) $ snapshotToDirName ds
201- writeSnapshotMetadata fs ds $
202- SnapshotMetadata
203- { snapshotBackend = UTxOHDMemSnapshot
204- , snapshotChecksum = maybe crc1 (crcOfConcat crc1) crc2
205- }
206-
268+ {-# INLINE implTakeSnapshot #-}
207269implTakeSnapshot ::
208270 ( IOLike m
209271 , LedgerDbSerialiseConstraints blk
@@ -215,20 +277,30 @@ implTakeSnapshot ::
215277 Maybe String ->
216278 StateRef m (ExtLedgerState blk ) ->
217279 m (Maybe (DiskSnapshot , RealPoint blk ))
218- implTakeSnapshot ccfg tracer hasFS suffix st = do
280+ implTakeSnapshot ccfg tracer shfs @ ( SomeHasFS hasFS) suffix st = do
219281 case pointToWithOriginRealPoint (castPoint (getTip $ state st)) of
220282 Origin -> return Nothing
221283 NotOrigin t -> do
222284 let number = unSlotNo (realPointSlot t)
223285 snapshot = DiskSnapshot number suffix
224- diskSnapshots <- defaultListSnapshots hasFS
286+ diskSnapshots <- defaultListSnapshots shfs
225287 if List. any (== DiskSnapshot number suffix) diskSnapshots
226288 then
227289 return Nothing
228290 else do
229291 encloseTimedWith (TookSnapshot snapshot t >$< tracer) $
230- writeSnapshot hasFS (encodeDiskExtLedgerState ccfg) snapshot st
292+ writeSnapshot snapshot
231293 return $ Just (snapshot, t)
294+ where
295+ writeSnapshot ds = do
296+ createDirectoryIfMissing hasFS True $ snapshotToDirPath ds
297+ crc1 <- writeExtLedgerState shfs (encodeDiskExtLedgerState ccfg) (snapshotToStatePath ds) $ state st
298+ crc2 <- takeHandleSnapshot (tables st) (state st) $ snapshotToDirName ds
299+ writeSnapshotMetadata shfs ds $
300+ SnapshotMetadata
301+ { snapshotBackend = UTxOHDMemSnapshot
302+ , snapshotChecksum = maybe crc1 (crcOfConcat crc1) crc2
303+ }
232304
233305-- | Read snapshot from disk.
234306--
0 commit comments