@@ -12,6 +12,7 @@ import scorex.transaction.{BlockChain, TransactionModule}
12
12
import scorex .utils .ScorexLogging
13
13
14
14
import scala .collection .JavaConversions ._
15
+ import scala .collection .concurrent .TrieMap
15
16
import scala .util .{Failure , Success , Try }
16
17
17
18
/**
@@ -26,6 +27,9 @@ class StoredBlockchain(db: MVStore)
26
27
val blocks : MVMap [Int , Array [Byte ]] = database.openMap(" blocks" )
27
28
val signatures : MVMap [Int , BlockId ] = database.openMap(" signatures" )
28
29
val signaturesReverse : MVMap [BlockId , Int ] = database.openMap(" signaturesReverse" )
30
+ private val BlocksCacheSizeLimit : Int = 1000
31
+ private var blocksCacheSize : Int = 0
32
+ private val blocksCache : TrieMap [Int , Option [Block ]] = TrieMap .empty
29
33
30
34
// TOOD remove when no blockchains without signaturesReverse remains
31
35
if (signaturesReverse.size() != signatures.size()) {
@@ -46,10 +50,19 @@ class StoredBlockchain(db: MVStore)
46
50
signaturesReverse.put(block.uniqueId, height)
47
51
}
48
52
49
- def readBlock (height : Int ): Option [Block ] =
50
- Try (Option (blocks.get(height))).toOption.flatten.flatMap(b => Block .parseBytes(b).toOption)
53
+ def readBlock (height : Int ): Option [Block ] = {
54
+ if (blocksCacheSize > BlocksCacheSizeLimit ) {
55
+ blocksCacheSize = 0
56
+ blocksCache.clear()
57
+ } else {
58
+ blocksCacheSize = blocksCacheSize + 1
59
+ }
60
+ blocksCache.getOrElseUpdate(height,
61
+ Try (Option (blocks.get(height))).toOption.flatten.flatMap(b => Block .parseBytes(b).toOption))
62
+ }
51
63
52
64
def deleteBlock (height : Int ): Unit = {
65
+ blocksCache.remove(height)
53
66
blocks.remove(height)
54
67
val vOpt = Option (signatures.remove(height))
55
68
vOpt.map(v => signaturesReverse.remove(v))
0 commit comments