CORDA-1003: Fix duplicate detection on cache evict

This commit is contained in:
Andras Slemmer 2018-02-09 18:23:42 +00:00
parent d072f6c275
commit cfc90a221b
2 changed files with 16 additions and 2 deletions

View File

@ -57,6 +57,7 @@ abstract class AppendOnlyPersistentMapBase<K, V, E, out EK>(
// Depending on 'store' method, this may insert without checking key duplication or it may avoid inserting a duplicated key.
val existingInDb = store(key, value)
if (existingInDb != null) { // Always reuse an existing value from the storage of a duplicated key.
isUnique = false
Optional.of(existingInDb)
} else {
Optional.of(value)

View File

@ -172,9 +172,22 @@ class DBTransactionStorageTests {
assertEquals(expected, actual)
}
private fun newTransactionStorage() {
@Test
fun `duplicates are detected when transaction is evicted from cache`() {
newTransactionStorage(cacheSizeBytesOverride = 0)
val transaction = newTransaction()
database.transaction {
transactionStorage = DBTransactionStorage(NodeConfiguration.defaultTransactionCacheSize)
val firstInserted = transactionStorage.addTransaction(transaction)
val secondInserted = transactionStorage.addTransaction(transaction)
require(firstInserted) { "We inserted a fresh transaction" }
require(!secondInserted) { "Second time should be redundant" }
println("$firstInserted $secondInserted")
}
}
private fun newTransactionStorage(cacheSizeBytesOverride: Long? = null) {
database.transaction {
transactionStorage = DBTransactionStorage(cacheSizeBytesOverride ?: NodeConfiguration.defaultTransactionCacheSize)
}
}