Skip to content

Commit

Permalink
Cache objects in HistoryStorage. fix #332
Browse files Browse the repository at this point in the history
  • Loading branch information
catena2w committed Jul 16, 2018
1 parent d528e18 commit d61040f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
package org.ergoplatform.nodeView.history.storage

import com.google.common.cache.CacheBuilder
import io.iohk.iodb.{ByteArrayWrapper, Store}
import org.ergoplatform.modifiers.ErgoPersistentModifier
import org.ergoplatform.modifiers.history.HistoryModifierSerializer
import org.ergoplatform.settings.Algos
import scorex.core.ModifierId
import scorex.core.utils.{ScorexEncoding, ScorexLogging}

import scala.util.{Failure, Success}
import scalacache._
import scalacache.guava._
import scalacache.modes.try_.mode

class HistoryStorage(indexStore: Store, objectsStore: ObjectsStore) extends ScorexLogging with AutoCloseable
with ScorexEncoding {

def modifierById(id: ModifierId): Option[ErgoPersistentModifier] = objectsStore.get(id)
.flatMap { bBytes =>
HistoryModifierSerializer.parseBytes(bBytes) match {
case Success(b) =>
Some(b)
case Failure(e) =>
log.warn(s"Failed to parse modifier ${encoder.encode(id)} from db (bytes are: ${bBytes.mkString("-")}): ", e)
None
}
// TODO move size to config?
val underlyingGuavaCache = CacheBuilder.newBuilder().maximumSize(100L).build[String, Entry[ErgoPersistentModifier]]
implicit val modifiersCache: Cache[ErgoPersistentModifier] = GuavaCache(underlyingGuavaCache)

def modifierById(id: ModifierId): Option[ErgoPersistentModifier] = {
cachingF(Algos.encode(id))(ttl = None) {
HistoryModifierSerializer.parseBytes(objectsStore.get(id).get)
}
}.toOption

def getIndex(id: ByteArrayWrapper): Option[ByteArrayWrapper] = indexStore.get(id)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import scorex.core.validation.{ModifierValidator, RecoverableModifierError, Vali

import scala.util.{Failure, Try}


/**
* Trait that implements BlockSectionProcessor interfaces for regimes where the node
* downloads and process full blocks.
Expand Down

0 comments on commit d61040f

Please sign in to comment.