Example code of atomic operations in Multimaps — FoundationDB 7.1 looks inefficient.
@fdb.transactional
def multi_subtract(tr, index, value):
v = tr[multi[index][value]]
if v.present() and struct.unpack('<q', str(v))[0] > 1:
tr.add(multi[index][value], struct.pack('<q', -1))
else:
del tr[multi[index][value]]
Should use compare_and_clear (recommended in the developer guide).
@fdb.transactional
def multi_subtract(tr, index, value):
tr.add(multi[index][value], struct.pack('<q', -1))
tr.compare_and_clear(multi[index][value], struct.pack('<q', 0))
Note
If a transaction uses both an atomic operation and a strictly serializable read on the same key, the benefits of using the atomic operation (for both conflict checking and performance) are lost.
Developer Guide — FoundationDB 7.1
Another, I think the next code
@fdb.transactional
def decrement(tr, counter):
tr.add(counter, struct.pack('<i', -1))
tr.compare_and_clear(counter, struct.pack('<i', -1))
is safer and easier than the original code in the developer guide.
@fdb.transactional
def decrement(tr, counter):
tr.add(counter, struct.pack('<i', -1))
tr.compare_and_clear(counter, struct.pack('<i', 0))