Merge pull request #2498 from corda/aslemmer-fix-cache-duplicate-detection-on-evict

CORDA-1003: Fix duplicate detection on cache evict
This commit is contained in:
Andras Slemmer 2018-02-12 10:13:16 +00:00 committed by GitHub
commit a9109e0855
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)
}
}