Skip to content

Commit f4f9085

Browse files
committed
rename Cache type to CacheStatus
1 parent d116e8c commit f4f9085

File tree

16 files changed

+147
-148
lines changed

16 files changed

+147
-148
lines changed

cardano-db-sync/src/Cardano/DbSync/Api.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import qualified Cardano.Chain.Genesis as Byron
5555
import Cardano.Crypto.ProtocolMagic (ProtocolMagicId (..))
5656
import qualified Cardano.Db as DB
5757
import Cardano.DbSync.Api.Types
58-
import Cardano.DbSync.Cache.Types (newEmptyCache, uninitiatedCache)
58+
import Cardano.DbSync.Cache.Types (newEmptyCache, useNoCache)
5959
import Cardano.DbSync.Config.Cardano
6060
import Cardano.DbSync.Config.Shelley
6161
import Cardano.DbSync.Config.Types
@@ -382,7 +382,7 @@ mkSyncEnv ::
382382
IO SyncEnv
383383
mkSyncEnv trce backend connectionString syncOptions protoInfo nw nwMagic systemStart syncNodeConfigFromFile syncNP ranMigrations runMigrationFnc = do
384384
dbCNamesVar <- newTVarIO =<< dbConstraintNamesExists backend
385-
cache <- if soptCache syncOptions then newEmptyCache 250000 50000 else pure uninitiatedCache
385+
cache <- if soptCache syncOptions then newEmptyCache 250000 50000 else pure useNoCache
386386
consistentLevelVar <- newTVarIO Unchecked
387387
fixDataVar <- newTVarIO $ if ranMigrations then DataFixRan else NoneFixRan
388388
indexesVar <- newTVarIO $ enpForceIndexes syncNP

cardano-db-sync/src/Cardano/DbSync/Api/Types.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Cardano.DbSync.Api.Types (
1414
) where
1515

1616
import qualified Cardano.Db as DB
17-
import Cardano.DbSync.Cache.Types (Cache)
17+
import Cardano.DbSync.Cache.Types (CacheStatus)
1818
import Cardano.DbSync.Config.Types (SyncNodeConfig)
1919
import Cardano.DbSync.Ledger.Types (HasLedgerEnv)
2020
import Cardano.DbSync.LocalStateQuery (NoLedgerEnv)
@@ -39,7 +39,7 @@ import Ouroboros.Network.Magic (NetworkMagic (..))
3939

4040
data SyncEnv = SyncEnv
4141
{ envBackend :: !SqlBackend
42-
, envCache :: !Cache
42+
, envCache :: !CacheStatus
4343
, envConnectionString :: !ConnectionString
4444
, envConsistentLevel :: !(StrictTVar IO ConsistentLevel)
4545
, envDbConstraints :: !(StrictTVar IO DB.ManualDbConstraints)

cardano-db-sync/src/Cardano/DbSync/Cache.hs

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Cardano.BM.Trace
3030
import qualified Cardano.Db as DB
3131
import Cardano.DbSync.Cache.Epoch (rollbackMapEpochInCache)
3232
import qualified Cardano.DbSync.Cache.LRU as LRU
33-
import Cardano.DbSync.Cache.Types (Cache (..), CacheInternal (..), CacheNew (..), CacheStatistics (..), StakeAddrCache, initCacheStatistics)
33+
import Cardano.DbSync.Cache.Types (CacheInternal (..), CacheNew (..), CacheStatistics (..), CacheStatus (..), StakeAddrCache, initCacheStatistics)
3434
import qualified Cardano.DbSync.Era.Shelley.Generic.Util as Generic
3535
import Cardano.DbSync.Era.Shelley.Query
3636
import Cardano.DbSync.Era.Util
@@ -67,41 +67,41 @@ import Ouroboros.Consensus.Cardano.Block (StandardCrypto)
6767
-- NOTE: BlockId is cleaned up on rollbacks, since it may get reinserted on
6868
-- a different id.
6969
-- NOTE: Other tables are not cleaned up since they are not rollbacked.
70-
rollbackCache :: MonadIO m => Cache -> DB.BlockId -> ReaderT SqlBackend m ()
71-
rollbackCache UninitiatedCache _ = pure ()
72-
rollbackCache (Cache cache) blockId = do
70+
rollbackCache :: MonadIO m => CacheStatus -> DB.BlockId -> ReaderT SqlBackend m ()
71+
rollbackCache NoCache _ = pure ()
72+
rollbackCache (ActiveCache cache) blockId = do
7373
liftIO $ do
7474
atomically $ writeTVar (cPrevBlock cache) Nothing
7575
atomically $ modifyTVar (cDatum cache) LRU.cleanup
7676
void $ rollbackMapEpochInCache cache blockId
7777

78-
getCacheStatistics :: Cache -> IO CacheStatistics
78+
getCacheStatistics :: CacheStatus -> IO CacheStatistics
7979
getCacheStatistics cs =
8080
case cs of
81-
UninitiatedCache -> pure initCacheStatistics
82-
Cache ci -> readTVarIO (cStats ci)
81+
NoCache -> pure initCacheStatistics
82+
ActiveCache ci -> readTVarIO (cStats ci)
8383

8484
queryOrInsertRewardAccount ::
8585
(MonadBaseControl IO m, MonadIO m) =>
86-
Cache ->
86+
CacheStatus ->
8787
CacheNew ->
8888
Ledger.RewardAccount StandardCrypto ->
8989
ReaderT SqlBackend m DB.StakeAddressId
90-
queryOrInsertRewardAccount cache cacheNew rewardAddr = do
91-
eiAddrId <- queryRewardAccountWithCacheRetBs cache cacheNew rewardAddr
90+
queryOrInsertRewardAccount cacheStatus cacheNew rewardAddr = do
91+
eiAddrId <- queryRewardAccountWithCacheRetBs cacheStatus cacheNew rewardAddr
9292
case eiAddrId of
9393
Left (_err, bs) -> insertStakeAddress rewardAddr (Just bs)
9494
Right addrId -> pure addrId
9595

9696
queryOrInsertStakeAddress ::
9797
(MonadBaseControl IO m, MonadIO m) =>
98-
Cache ->
98+
CacheStatus ->
9999
CacheNew ->
100100
Network ->
101101
StakeCred ->
102102
ReaderT SqlBackend m DB.StakeAddressId
103-
queryOrInsertStakeAddress cache cacheNew nw cred =
104-
queryOrInsertRewardAccount cache cacheNew $ Ledger.RewardAccount nw cred
103+
queryOrInsertStakeAddress cacheStatus cacheNew nw cred =
104+
queryOrInsertRewardAccount cacheStatus cacheNew $ Ledger.RewardAccount nw cred
105105

106106
-- If the address already exists in the table, it will not be inserted again (due to
107107
-- the uniqueness constraint) but the function will return the 'StakeAddressId'.
@@ -123,38 +123,38 @@ insertStakeAddress rewardAddr stakeCredBs =
123123
queryRewardAccountWithCacheRetBs ::
124124
forall m.
125125
MonadIO m =>
126-
Cache ->
126+
CacheStatus ->
127127
CacheNew ->
128128
Ledger.RewardAccount StandardCrypto ->
129129
ReaderT SqlBackend m (Either (DB.LookupFail, ByteString) DB.StakeAddressId)
130-
queryRewardAccountWithCacheRetBs cache cacheNew rwdAcc =
131-
queryStakeAddrWithCacheRetBs cache cacheNew (Ledger.raNetwork rwdAcc) (Ledger.raCredential rwdAcc)
130+
queryRewardAccountWithCacheRetBs cacheStatus cacheNew rwdAcc =
131+
queryStakeAddrWithCacheRetBs cacheStatus cacheNew (Ledger.raNetwork rwdAcc) (Ledger.raCredential rwdAcc)
132132

133133
queryStakeAddrWithCache ::
134134
forall m.
135135
MonadIO m =>
136-
Cache ->
136+
CacheStatus ->
137137
CacheNew ->
138138
Network ->
139139
StakeCred ->
140140
ReaderT SqlBackend m (Either DB.LookupFail DB.StakeAddressId)
141-
queryStakeAddrWithCache cache cacheNew nw cred =
142-
mapLeft fst <$> queryStakeAddrWithCacheRetBs cache cacheNew nw cred
141+
queryStakeAddrWithCache cacheStatus cacheNew nw cred =
142+
mapLeft fst <$> queryStakeAddrWithCacheRetBs cacheStatus cacheNew nw cred
143143

144144
queryStakeAddrWithCacheRetBs ::
145145
forall m.
146146
MonadIO m =>
147-
Cache ->
147+
CacheStatus ->
148148
CacheNew ->
149149
Network ->
150150
StakeCred ->
151151
ReaderT SqlBackend m (Either (DB.LookupFail, ByteString) DB.StakeAddressId)
152-
queryStakeAddrWithCacheRetBs cache cacheNew nw cred = do
153-
case cache of
154-
UninitiatedCache -> do
152+
queryStakeAddrWithCacheRetBs cacheStatus cacheNew nw cred = do
153+
case cacheStatus of
154+
NoCache -> do
155155
let !bs = Ledger.serialiseRewardAccount (Ledger.RewardAccount nw cred)
156156
mapLeft (,bs) <$> queryStakeAddress bs
157-
Cache ci -> do
157+
ActiveCache ci -> do
158158
mp <- liftIO $ readTVarIO (cStakeCreds ci)
159159
(mAddrId, mp') <- queryStakeAddrAux cacheNew mp (cStats ci) nw cred
160160
liftIO $ atomically $ writeTVar (cStakeCreds ci) mp'
@@ -186,18 +186,18 @@ queryStakeAddrAux cacheNew mp sts nw cred =
186186

187187
queryPoolKeyWithCache ::
188188
MonadIO m =>
189-
Cache ->
189+
CacheStatus ->
190190
CacheNew ->
191191
PoolKeyHash ->
192192
ReaderT SqlBackend m (Either DB.LookupFail DB.PoolHashId)
193-
queryPoolKeyWithCache cache cacheNew hsh =
194-
case cache of
195-
UninitiatedCache -> do
193+
queryPoolKeyWithCache cacheStatus cacheNew hsh =
194+
case cacheStatus of
195+
NoCache -> do
196196
mPhId <- queryPoolHashId (Generic.unKeyHashRaw hsh)
197197
case mPhId of
198198
Nothing -> pure $ Left (DB.DbLookupMessage "PoolKeyHash")
199199
Just phId -> pure $ Right phId
200-
Cache ci -> do
200+
ActiveCache ci -> do
201201
mp <- liftIO $ readTVarIO (cPools ci)
202202
case Map.lookup hsh mp of
203203
Just phId -> do
@@ -225,19 +225,19 @@ queryPoolKeyWithCache cache cacheNew hsh =
225225

226226
insertPoolKeyWithCache ::
227227
(MonadBaseControl IO m, MonadIO m) =>
228-
Cache ->
228+
CacheStatus ->
229229
CacheNew ->
230230
PoolKeyHash ->
231231
ReaderT SqlBackend m DB.PoolHashId
232-
insertPoolKeyWithCache cache cacheNew pHash =
233-
case cache of
234-
UninitiatedCache ->
232+
insertPoolKeyWithCache cacheStatus cacheNew pHash =
233+
case cacheStatus of
234+
NoCache ->
235235
DB.insertPoolHash $
236236
DB.PoolHash
237237
{ DB.poolHashHashRaw = Generic.unKeyHashRaw pHash
238238
, DB.poolHashView = Generic.unKeyHashView pHash
239239
}
240-
Cache ci -> do
240+
ActiveCache ci -> do
241241
mp <- liftIO $ readTVarIO (cPools ci)
242242
case Map.lookup pHash mp of
243243
Just phId -> do
@@ -267,13 +267,13 @@ queryPoolKeyOrInsert ::
267267
(MonadBaseControl IO m, MonadIO m) =>
268268
Text ->
269269
Trace IO Text ->
270-
Cache ->
270+
CacheStatus ->
271271
CacheNew ->
272272
Bool ->
273273
PoolKeyHash ->
274274
ReaderT SqlBackend m DB.PoolHashId
275-
queryPoolKeyOrInsert txt trce cache cacheNew logsWarning hsh = do
276-
pk <- queryPoolKeyWithCache cache cacheNew hsh
275+
queryPoolKeyOrInsert txt trce cacheStatus cacheNew logsWarning hsh = do
276+
pk <- queryPoolKeyWithCache cacheStatus cacheNew hsh
277277
case pk of
278278
Right poolHashId -> pure poolHashId
279279
Left err -> do
@@ -289,21 +289,21 @@ queryPoolKeyOrInsert txt trce cache cacheNew logsWarning hsh = do
289289
, txt
290290
, ". We will assume that the pool exists and move on."
291291
]
292-
insertPoolKeyWithCache cache cacheNew hsh
292+
insertPoolKeyWithCache cacheStatus cacheNew hsh
293293

294294
queryMAWithCache ::
295295
MonadIO m =>
296-
Cache ->
296+
CacheStatus ->
297297
PolicyID StandardCrypto ->
298298
AssetName ->
299299
ReaderT SqlBackend m (Either (ByteString, ByteString) DB.MultiAssetId)
300-
queryMAWithCache cache policyId asset =
301-
case cache of
302-
UninitiatedCache -> do
300+
queryMAWithCache cacheStatus policyId asset =
301+
case cacheStatus of
302+
NoCache -> do
303303
let !policyBs = Generic.unScriptHash $ policyID policyId
304304
let !assetNameBs = Generic.unAssetName asset
305305
maybe (Left (policyBs, assetNameBs)) Right <$> DB.queryMultiAssetId policyBs assetNameBs
306-
Cache ci -> do
306+
ActiveCache ci -> do
307307
mp <- liftIO $ readTVarIO (cMultiAssets ci)
308308
case LRU.lookup (policyId, asset) mp of
309309
Just (maId, mp') -> do
@@ -323,13 +323,13 @@ queryMAWithCache cache policyId asset =
323323
queryPrevBlockWithCache ::
324324
MonadIO m =>
325325
Text ->
326-
Cache ->
326+
CacheStatus ->
327327
ByteString ->
328328
ExceptT SyncNodeError (ReaderT SqlBackend m) DB.BlockId
329-
queryPrevBlockWithCache msg cache hsh =
330-
case cache of
331-
UninitiatedCache -> liftLookupFail msg $ DB.queryBlockId hsh
332-
Cache ci -> do
329+
queryPrevBlockWithCache msg cacheStatus hsh =
330+
case cacheStatus of
331+
NoCache -> liftLookupFail msg $ DB.queryBlockId hsh
332+
ActiveCache ci -> do
333333
mCachedPrev <- liftIO $ readTVarIO (cPrevBlock ci)
334334
case mCachedPrev of
335335
-- if the cached block matches the requested hash, we return its db id.
@@ -351,13 +351,13 @@ queryPrevBlockWithCache msg cache hsh =
351351

352352
insertBlockAndCache ::
353353
(MonadIO m, MonadBaseControl IO m) =>
354-
Cache ->
354+
CacheStatus ->
355355
DB.Block ->
356356
ReaderT SqlBackend m DB.BlockId
357-
insertBlockAndCache cache block =
358-
case cache of
359-
UninitiatedCache -> DB.insertBlock block
360-
Cache ci -> do
357+
insertBlockAndCache cacheStatus block =
358+
case cacheStatus of
359+
NoCache -> DB.insertBlock block
360+
ActiveCache ci -> do
361361
bid <- DB.insertBlock block
362362
liftIO $ do
363363
missPrevBlock (cStats ci)
@@ -366,13 +366,13 @@ insertBlockAndCache cache block =
366366

367367
queryDatum ::
368368
MonadIO m =>
369-
Cache ->
369+
CacheStatus ->
370370
DataHash ->
371371
ReaderT SqlBackend m (Maybe DB.DatumId)
372-
queryDatum cache hsh = do
373-
case cache of
374-
UninitiatedCache -> DB.queryDatum $ Generic.dataHashToBytes hsh
375-
Cache ci -> do
372+
queryDatum cacheStatus hsh = do
373+
case cacheStatus of
374+
NoCache -> DB.queryDatum $ Generic.dataHashToBytes hsh
375+
ActiveCache ci -> do
376376
mp <- liftIO $ readTVarIO (cDatum ci)
377377
case LRU.lookup hsh mp of
378378
Just (datumId, mp') -> do
@@ -387,15 +387,15 @@ queryDatum cache hsh = do
387387
-- This assumes the entry is not cached.
388388
insertDatumAndCache ::
389389
(MonadIO m, MonadBaseControl IO m) =>
390-
Cache ->
390+
CacheStatus ->
391391
DataHash ->
392392
DB.Datum ->
393393
ReaderT SqlBackend m DB.DatumId
394-
insertDatumAndCache cache hsh dt = do
394+
insertDatumAndCache cacheStatus hsh dt = do
395395
datumId <- DB.insertDatum dt
396-
case cache of
397-
UninitiatedCache -> pure datumId
398-
Cache ci -> do
396+
case cacheStatus of
397+
NoCache -> pure datumId
398+
ActiveCache ci -> do
399399
liftIO $
400400
atomically $
401401
modifyTVar (cDatum ci) $

0 commit comments

Comments
 (0)