Fork me on GitHub
#datalevin
<
2022-09-22
>
roklenarcic15:09:27

If I want to transact an entity but only if it doesn’t exist yet, how do I manage that?

Huahai16:09:42

try look up the entity, if got nothing, transact

roklenarcic16:09:27

Doesn’t that have an obvious race condition?

Huahai16:09:51

not obvious to me

roklenarcic16:09:55

Two parties check for presence simultaneously. Both see that it’s not there’ both transact

Huahai16:09:33

writes are serialized, two entities will be written, or one will has an error

Huahai16:09:51

both are expected behavior

roklenarcic16:09:36

Or imagine a counter. Two theads try to increment. Both read value 1. Both will transact to value 2

Huahai16:09:34

it will end up with 2, as expected.

Huahai16:09:41

if you are talking about atomicity, we have an issue for that

Huahai16:09:04

We do miss a function that does both read and write atomically. So you are right. I wonder why datomic doesn’t have these?

Huahai16:09:33

sure, we can add such functionality.

Huahai16:09:57

or more generally, maybe expose the transaction explicitly, e.g. introduce something like (start-transaction), (end-transaction), so whatever you do inside, will be part of the transaction

roklenarcic16:09:31

Datomic has transaction functions for this I think

Huahai16:09:59

we have transaction functions too, but that’s not the same thing

Huahai16:09:58

at the KV db level, we need to expose transaction start and end, so as to ensure the correctness.

Huahai16:09:07

Some of these work is being done in the query branch, but maybe I should move them to the main branch.

Huahai16:09:44

this work does introduce a lot of changes, it would be messy to merge. I would wait for the finish of the rewrite.

Huahai17:09:59

On a second thought, the rewrite still has a lot more work remaining, I should move this part to the main branch first, since it is about correctness in multiple threads situation.

Huahai17:09:58

OK, I will work on this.

Huahai17:09:38

We will not change existing API, instead, we will introduce (start-tx …) and (end-tx …) for both KV and Datalog DB.

Huahai17:09:16

So people can do whatever they want in between and be considered as part of the same transaction, ensuring correctness.

Huahai17:09:07

so in your case, you will get 3, instead of 2, as desired.

roklenarcic17:09:52

That’s great

Huahai11:12:20

with-transaction-kv and with-transaction macro have been implemented to do this for both KV and Datalog DB. So this is resolved.