Skip to content

Commit

Permalink
EC-546 - FastSync performance improvements - EphemDataSource refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Mrozek committed Aug 3, 2018
1 parent 7cb8886 commit e36a8a5
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package io.iohk.ethereum.db.dataSource

class EphemDataSource(var storage: Map[IndexedSeq[Byte], IndexedSeq[Byte]]) extends DataSource {
import java.nio.ByteBuffer

class EphemDataSource(var storage: Map[ByteBuffer, Array[Byte]]) extends DataSource {

/**
* key.drop to remove namespace prefix from the key
* @return key values paris from this storage
*/
def getAll(namespace: Namespace): Seq[(IndexedSeq[Byte], IndexedSeq[Byte])] =
storage.toSeq.map{case (key, value) => (key.drop(namespace.length), value)}
storage.toSeq.map{case (key, value) => (key.array().drop(namespace.length).toIndexedSeq, value.toIndexedSeq)}

override def get(namespace: Namespace, key: Key): Option[Value] = storage.get(namespace ++: key)
override def get(namespace: Namespace, key: Key): Option[Value] = storage.get(ByteBuffer.wrap((namespace ++ key).toArray)).map(_.toIndexedSeq)

override def update(namespace: Namespace, toRemove: Seq[Key], toUpsert: Seq[(Key, Value)]): DataSource = {
val afterRemoval = toRemove.foldLeft(storage)((storage, key) => storage - (namespace ++ key))
val afterRemoval = toRemove.foldLeft(storage)((storage, key) => storage - ByteBuffer.wrap((namespace ++ key).toArray))
val afterUpdate = toUpsert.foldLeft(afterRemoval)((storage, toUpdate) =>
storage + ((namespace ++ toUpdate._1) -> toUpdate._2))
storage + (ByteBuffer.wrap((namespace ++ toUpdate._1).toArray) -> toUpdate._2.toArray))
storage = afterUpdate
this
}
Expand All @@ -29,15 +31,15 @@ class EphemDataSource(var storage: Map[IndexedSeq[Byte], IndexedSeq[Byte]]) exte
override def destroy(): Unit = ()

override def updateOptimized(toRemove: Seq[Array[Byte]], toUpsert: Seq[(Array[Byte], Array[Byte])]): DataSource = {
val afterRemoval = toRemove.foldLeft(storage)((storage, key) => storage - key.toIndexedSeq)
val afterRemoval = toRemove.foldLeft(storage)((storage, key) => storage - ByteBuffer.wrap(key))
val afterUpdate = toUpsert.foldLeft(afterRemoval)((storage, toUpdate) =>
storage + (toUpdate._1.toIndexedSeq -> toUpdate._2.toIndexedSeq))
storage + (ByteBuffer.wrap(toUpdate._1) -> toUpdate._2))
storage = afterUpdate
this

}

override def getOptimized(key: Array[Byte]): Option[Array[Byte]] = storage.get(key.toIndexedSeq).map(_.toArray)
override def getOptimized(key: Array[Byte]): Option[Array[Byte]] = storage.get(ByteBuffer.wrap(key))
}

object EphemDataSource {
Expand Down

0 comments on commit e36a8a5

Please sign in to comment.