Deleting a key from DB and writing it again in the same transaction does not work
remcowesterhoud opened this issue · comments
Imagine key X is stored in the database. If we start a new transaction in which we will delete X, and later in the same transaction write X to the database again. The key will not be stored in the database.
@Test
public void shouldWriteKeyAfterDeletion() {
// given
oneKey.wrapLong(1);
oneValue.wrapLong(-1);
oneColumnFamily.put(oneKey, oneValue);
// when
assertThat(oneColumnFamily.get(oneKey).getValue()).isEqualTo(-1);
transactionContext.runInTransaction(
() -> {
oneColumnFamily.delete(oneKey);
oneValue.wrapLong(-2);
oneColumnFamily.put(oneKey, oneValue);
});
// then
assertThat(oneColumnFamily.exists(oneKey)).isTrue();
assertThat(oneColumnFamily.get(oneKey).getValue()).isEqualTo(-2);
}
The reason for this is that on commit of a transaction it will first process all the writes. After this it will process all the deletes. Therefore the order of the statements in the transactions is lost.
override fun commit() {
inCurrentTransaction = false
database.putAll(transactionCache)
deletedKeys.forEach { database.remove(it) }
deletedKeys.clear()
transactionCache.clear()
}