From ebf312873cbc9d42c6ac4180d59741d0cbee673c Mon Sep 17 00:00:00 2001 From: fabiotudone-da Date: Fri, 17 Jul 2020 16:07:54 +0200 Subject: [PATCH] Add `CachingDamlLedgerStateReaderWithFingerprints` factory method (#6769) CHANGELOG_BEGIN CHANGELOG_END --- .../CachingDamlLedgerStateReader.scala | 11 +++++--- ...amlLedgerStateReaderWithFingerprints.scala | 25 +++++++++++++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/validator/caching/CachingDamlLedgerStateReader.scala b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/validator/caching/CachingDamlLedgerStateReader.scala index ac351ea8039..ba87aa0e9f0 100644 --- a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/validator/caching/CachingDamlLedgerStateReader.scala +++ b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/validator/caching/CachingDamlLedgerStateReader.scala @@ -6,6 +6,7 @@ package com.daml.ledger.validator.caching import com.daml.caching.Cache import com.daml.ledger.participant.state.kvutils.DamlKvutils.{DamlStateKey, DamlStateValue} import com.daml.ledger.validator.LedgerStateOperations.Key +import com.daml.ledger.validator.caching.CachingDamlLedgerStateReader.StateCache import com.daml.ledger.validator.{ DamlLedgerStateReader, LedgerStateReader, @@ -29,7 +30,7 @@ trait QueryableReadSet { * package state values (which are too expensive to deserialize from bytes every time). */ class CachingDamlLedgerStateReader( - val cache: Cache[DamlStateKey, DamlStateValue], + val cache: StateCache, shouldCache: DamlStateKey => Boolean, keySerializationStrategy: StateKeySerializationStrategy, delegate: DamlLedgerStateReader)(implicit executionContext: ExecutionContext) @@ -69,16 +70,18 @@ class CachingDamlLedgerStateReader( } object CachingDamlLedgerStateReader { + + type StateCache = Cache[DamlStateKey, DamlStateValue] + private[validator] def apply( - cache: Cache[DamlStateKey, DamlStateValue], + cache: StateCache, cachingPolicy: CacheUpdatePolicy, ledgerStateOperations: LedgerStateReader, keySerializationStrategy: StateKeySerializationStrategy)( - implicit executionContext: ExecutionContext): CachingDamlLedgerStateReader = { + implicit executionContext: ExecutionContext): CachingDamlLedgerStateReader = new CachingDamlLedgerStateReader( cache, cachingPolicy.shouldCacheOnRead, keySerializationStrategy, new RawToDamlLedgerStateReaderAdapter(ledgerStateOperations, keySerializationStrategy)) - } } diff --git a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/validator/caching/CachingDamlLedgerStateReaderWithFingerprints.scala b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/validator/caching/CachingDamlLedgerStateReaderWithFingerprints.scala index c5d0297e372..19c6d9fd552 100644 --- a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/validator/caching/CachingDamlLedgerStateReaderWithFingerprints.scala +++ b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/validator/caching/CachingDamlLedgerStateReaderWithFingerprints.scala @@ -7,7 +7,12 @@ import com.daml.caching.{Cache, Weight} import com.daml.ledger.participant.state.kvutils.DamlKvutils.{DamlStateKey, DamlStateValue} import com.daml.ledger.participant.state.kvutils.{DamlKvutils, Fingerprint} import com.daml.ledger.validator.StateKeySerializationStrategy -import com.daml.ledger.validator.preexecution.DamlLedgerStateReaderWithFingerprints +import com.daml.ledger.validator.caching.CachingDamlLedgerStateReaderWithFingerprints.StateCacheWithFingerprints +import com.daml.ledger.validator.preexecution.{ + DamlLedgerStateReaderWithFingerprints, + LedgerStateReaderWithFingerprints, + RawToDamlLedgerStateReaderWithFingerprintsAdapter +} import com.google.protobuf.MessageLite import scala.concurrent.{ExecutionContext, Future} @@ -17,7 +22,7 @@ import scala.concurrent.{ExecutionContext, Future} * Caches only positive lookups, i.e., in case the values for a requested key are available on the ledger. */ class CachingDamlLedgerStateReaderWithFingerprints( - val cache: Cache[DamlStateKey, (DamlStateValue, Fingerprint)], + val cache: StateCacheWithFingerprints, shouldCache: DamlStateKey => Boolean, keySerializationStrategy: StateKeySerializationStrategy, delegate: DamlLedgerStateReaderWithFingerprints)(implicit executionContext: ExecutionContext) @@ -60,4 +65,20 @@ object CachingDamlLedgerStateReaderWithFingerprints { value._1.getSerializedSize.toLong + value._2.size() } + type StateCacheWithFingerprints = Cache[DamlStateKey, (DamlStateValue, Fingerprint)] + + def apply( + cache: StateCacheWithFingerprints, + cachingPolicy: CacheUpdatePolicy, + ledgerStateReaderWithFingerprints: LedgerStateReaderWithFingerprints, + keySerializationStrategy: StateKeySerializationStrategy)( + implicit executionContext: ExecutionContext): CachingDamlLedgerStateReaderWithFingerprints = + new CachingDamlLedgerStateReaderWithFingerprints( + cache, + cachingPolicy.shouldCacheOnRead, + keySerializationStrategy, + new RawToDamlLedgerStateReaderWithFingerprintsAdapter( + ledgerStateReaderWithFingerprints, + keySerializationStrategy) + ) }