Skip to content

Commit 5903a16

Browse files
authored
Merge pull request #1733 from IntersectMBO/rename-cache-cachestatus
Rename Cache -> CacheStatus & CacheNew -> CacheUpdateAction
2 parents d116e8c + 9fe1d23 commit 5903a16

File tree

16 files changed

+235
-236
lines changed

16 files changed

+235
-236
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: 82 additions & 82 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 (..), CacheStatistics (..), CacheStatus (..), CacheUpdateAction (..), 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 ->
87-
CacheNew ->
86+
CacheStatus ->
87+
CacheUpdateAction ->
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 cacheUA rewardAddr = do
91+
eiAddrId <- queryRewardAccountWithCacheRetBs cacheStatus cacheUA 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 ->
99-
CacheNew ->
98+
CacheStatus ->
99+
CacheUpdateAction ->
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 cacheUA nw cred =
104+
queryOrInsertRewardAccount cacheStatus cacheUA $ 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,87 +123,87 @@ insertStakeAddress rewardAddr stakeCredBs =
123123
queryRewardAccountWithCacheRetBs ::
124124
forall m.
125125
MonadIO m =>
126-
Cache ->
127-
CacheNew ->
126+
CacheStatus ->
127+
CacheUpdateAction ->
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 cacheUA rwdAcc =
131+
queryStakeAddrWithCacheRetBs cacheStatus cacheUA (Ledger.raNetwork rwdAcc) (Ledger.raCredential rwdAcc)
132132

133133
queryStakeAddrWithCache ::
134134
forall m.
135135
MonadIO m =>
136-
Cache ->
137-
CacheNew ->
136+
CacheStatus ->
137+
CacheUpdateAction ->
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 cacheUA nw cred =
142+
mapLeft fst <$> queryStakeAddrWithCacheRetBs cacheStatus cacheUA nw cred
143143

144144
queryStakeAddrWithCacheRetBs ::
145145
forall m.
146146
MonadIO m =>
147-
Cache ->
148-
CacheNew ->
147+
CacheStatus ->
148+
CacheUpdateAction ->
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 cacheUA 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)
159-
(mAddrId, mp') <- queryStakeAddrAux cacheNew mp (cStats ci) nw cred
159+
(mAddrId, mp') <- queryStakeAddrAux cacheUA mp (cStats ci) nw cred
160160
liftIO $ atomically $ writeTVar (cStakeCreds ci) mp'
161161
pure mAddrId
162162

163163
queryStakeAddrAux ::
164164
MonadIO m =>
165-
CacheNew ->
165+
CacheUpdateAction ->
166166
StakeAddrCache ->
167167
StrictTVar IO CacheStatistics ->
168168
Network ->
169169
StakeCred ->
170170
ReaderT SqlBackend m (Either (DB.LookupFail, ByteString) DB.StakeAddressId, StakeAddrCache)
171-
queryStakeAddrAux cacheNew mp sts nw cred =
171+
queryStakeAddrAux cacheUA mp sts nw cred =
172172
case Map.lookup cred mp of
173173
Just addrId -> do
174174
liftIO $ hitCreds sts
175-
case cacheNew of
176-
EvictAndReturn -> pure (Right addrId, Map.delete cred mp)
175+
case cacheUA of
176+
EvictAndUpdateCache -> pure (Right addrId, Map.delete cred mp)
177177
_ -> pure (Right addrId, mp)
178178
Nothing -> do
179179
liftIO $ missCreds sts
180180
let !bs = Ledger.serialiseRewardAccount (Ledger.RewardAccount nw cred)
181181
mAddrId <- mapLeft (,bs) <$> queryStakeAddress bs
182-
case (mAddrId, cacheNew) of
183-
(Right addrId, CacheNew) -> pure (Right addrId, Map.insert cred addrId mp)
182+
case (mAddrId, cacheUA) of
183+
(Right addrId, UpdateCache) -> pure (Right addrId, Map.insert cred addrId mp)
184184
(Right addrId, _) -> pure (Right addrId, mp)
185185
(err, _) -> pure (err, mp)
186186

187187
queryPoolKeyWithCache ::
188188
MonadIO m =>
189-
Cache ->
190-
CacheNew ->
189+
CacheStatus ->
190+
CacheUpdateAction ->
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 cacheUA 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
204204
liftIO $ hitPools (cStats ci)
205205
-- hit so we can't cache even with 'CacheNew'
206-
when (cacheNew == EvictAndReturn) $
206+
when (cacheUA == EvictAndUpdateCache) $
207207
liftIO $
208208
atomically $
209209
modifyTVar (cPools ci) $
@@ -216,7 +216,7 @@ queryPoolKeyWithCache cache cacheNew hsh =
216216
Nothing -> pure $ Left (DB.DbLookupMessage "PoolKeyHash")
217217
Just phId -> do
218218
-- missed so we can't evict even with 'EvictAndReturn'
219-
when (cacheNew == CacheNew) $
219+
when (cacheUA == UpdateCache) $
220220
liftIO $
221221
atomically $
222222
modifyTVar (cPools ci) $
@@ -225,24 +225,24 @@ queryPoolKeyWithCache cache cacheNew hsh =
225225

226226
insertPoolKeyWithCache ::
227227
(MonadBaseControl IO m, MonadIO m) =>
228-
Cache ->
229-
CacheNew ->
228+
CacheStatus ->
229+
CacheUpdateAction ->
230230
PoolKeyHash ->
231231
ReaderT SqlBackend m DB.PoolHashId
232-
insertPoolKeyWithCache cache cacheNew pHash =
233-
case cache of
234-
UninitiatedCache ->
232+
insertPoolKeyWithCache cacheStatus cacheUA 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
244244
liftIO $ hitPools (cStats ci)
245-
when (cacheNew == EvictAndReturn) $
245+
when (cacheUA == EvictAndUpdateCache) $
246246
liftIO $
247247
atomically $
248248
modifyTVar (cPools ci) $
@@ -256,7 +256,7 @@ insertPoolKeyWithCache cache cacheNew pHash =
256256
{ DB.poolHashHashRaw = Generic.unKeyHashRaw pHash
257257
, DB.poolHashView = Generic.unKeyHashView pHash
258258
}
259-
when (cacheNew == CacheNew) $
259+
when (cacheUA == UpdateCache) $
260260
liftIO $
261261
atomically $
262262
modifyTVar (cPools ci) $
@@ -267,13 +267,13 @@ queryPoolKeyOrInsert ::
267267
(MonadBaseControl IO m, MonadIO m) =>
268268
Text ->
269269
Trace IO Text ->
270-
Cache ->
271-
CacheNew ->
270+
CacheStatus ->
271+
CacheUpdateAction ->
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 cacheUA logsWarning hsh = do
276+
pk <- queryPoolKeyWithCache cacheStatus cacheUA 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 cacheUA 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)