22{-# LANGUAGE DataKinds #-}
33{-# LANGUAGE DeriveAnyClass #-}
44{-# LANGUAGE DeriveGeneric #-}
5+ {-# LANGUAGE DerivingVia #-}
56{-# LANGUAGE FlexibleContexts #-}
67{-# LANGUAGE LambdaCase #-}
78{-# LANGUAGE NamedFieldPuns #-}
@@ -33,7 +34,6 @@ import Data.Set (Set)
3334import qualified Data.Set as Set
3435import Data.Traversable (for )
3536import Data.Tuple (Solo (.. ))
36- import Data.Void
3737import Data.Word
3838import GHC.Generics
3939import NoThunks.Class
@@ -56,6 +56,8 @@ import Ouroboros.Consensus.Storage.LedgerDB.TraceEvent
5656import Ouroboros.Consensus.Storage.LedgerDB.V2.Args as V2
5757import Ouroboros.Consensus.Storage.LedgerDB.V2.Forker
5858import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.InMemory as InMemory
59+ import Ouroboros.Consensus.Storage.LedgerDB.V2.LSM (snapshotToStatePath )
60+ import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.LSM as LSM
5961import Ouroboros.Consensus.Storage.LedgerDB.V2.LedgerSeq
6062import Ouroboros.Consensus.Util.Args
6163import Ouroboros.Consensus.Util.CallStack
@@ -66,6 +68,8 @@ import Ouroboros.Network.Protocol.LocalStateQuery.Type
6668import System.FS.API
6769import Prelude hiding (read )
6870
71+ type SnapshotManagerV2 m blk = SnapshotManager m m blk (StateRef m (ExtLedgerState blk ))
72+
6973mkInitDb ::
7074 forall m blk .
7175 ( LedgerSupportsProtocol blk
@@ -75,10 +79,11 @@ mkInitDb ::
7579 , LedgerSupportsV2LedgerDB (LedgerState blk )
7680 ) =>
7781 Complete LedgerDbArgs m blk ->
78- Complete V2. LedgerDbFlavorArgs m ->
82+ HandleEnv m ->
7983 ResolveBlock m blk ->
84+ SnapshotManagerV2 m blk ->
8085 InitDB (LedgerSeq' m blk ) m blk
81- mkInitDb args flavArgs getBlock =
86+ mkInitDb args bss getBlock snapManager =
8287 InitDB
8388 { initFromGenesis = emptyF =<< lgrGenesis
8489 , initFromSnapshot =
@@ -112,9 +117,12 @@ mkInitDb args flavArgs getBlock =
112117 , ldbResolveBlock = getBlock
113118 , ldbQueryBatchSize = lgrQueryBatchSize
114119 , ldbOpenHandlesLock = lock
120+ , testLdbResourceKeys = case bss of
121+ InMemoryHandleEnv -> Nothing
122+ LSMHandleEnv (s, _) k -> Just (s, k)
115123 }
116124 h <- LDBHandle <$> newTVarIO (LedgerDBOpen env)
117- pure $ implMkLedgerDb h bss
125+ pure $ implMkLedgerDb h snapManager
118126 }
119127 where
120128 LedgerDbArgs
@@ -127,8 +135,6 @@ mkInitDb args flavArgs getBlock =
127135 , lgrRegistry
128136 } = args
129137
130- bss = case flavArgs of V2Args bss0 -> bss0
131-
132138 v2Tracer :: Tracer m V2. FlavorImplSpecificTrace
133139 v2Tracer = LedgerDBFlavorImplEvent . FlavorImplSpecificTraceV2 >$< lgrTracer
134140
@@ -137,17 +143,20 @@ mkInitDb args flavArgs getBlock =
137143 m (LedgerSeq' m blk )
138144 emptyF st =
139145 empty' st $ case bss of
140- InMemoryHandleArgs -> InMemory. newInMemoryLedgerTablesHandle v2Tracer lgrHasFS
141- LSMHandleArgs x -> absurd x
146+ InMemoryHandleEnv -> InMemory. newInMemoryLedgerTablesHandle v2Tracer lgrHasFS
147+ LSMHandleEnv (_, session) _ ->
148+ \ values -> do
149+ table <- LSM. tableFromValuesMK lgrRegistry session values
150+ LSM. newLSMLedgerTablesHandle v2Tracer lgrRegistry session table
142151
143152 loadSnapshot ::
144153 CodecConfig blk ->
145154 SomeHasFS m ->
146155 DiskSnapshot ->
147156 m (Either (SnapshotFailure blk ) (LedgerSeq' m blk , RealPoint blk ))
148157 loadSnapshot ccfg fs ds = case bss of
149- InMemoryHandleArgs -> runExceptT $ InMemory. loadSnapshot v2Tracer lgrRegistry ccfg fs ds
150- LSMHandleArgs x -> absurd x
158+ InMemoryHandleEnv -> runExceptT $ InMemory. loadSnapshot v2Tracer lgrRegistry ccfg fs ds
159+ LSMHandleEnv (_, session) _ -> runExceptT $ LSM. loadSnapshot v2Tracer lgrRegistry ccfg fs session ds
151160
152161implMkLedgerDb ::
153162 forall m l blk .
@@ -433,6 +442,10 @@ data LedgerDBEnv m l blk = LedgerDBEnv
433442 --
434443 -- * Modify 'ldbSeq' while holding a write lock, and then close the removed
435444 -- handles without any locking.
445+ , testLdbResourceKeys :: ! (Maybe (ResourceKey m , ResourceKey m ))
446+ -- ^ Resource keys used in the LSM backend so that the closing function used
447+ -- in tests can release such resources. These are the resource keys for the
448+ -- LSM session and the resource key for the BlockIO interface.
436449 }
437450 deriving Generic
438451
0 commit comments