I'm looking for an efficient way to implement compare-and-swap operation in Berkeley DB. Right now I'm using really old version, however at first glance even the latest one (distributed from Oracle website) does not have a single method for such operation.
I was looking for a some kind of method like
replace(Transaction, Key, ExpectedValue, NewValue)
with the following semantics: DB gets value associated with a given key, if this value exists and it is equal to ExpectedValue then this value will be changed to NewValue, otherwise method returns unsuccessful OperationStatus.
Looks like there is no method like that, so I'm wondering how is this supposed to be done in the most efficient way.
Right now I'm using the following approach: I do
db.get(null, key) -> {currentValue, version}
db.put(null, key, {currentValue, newRandomIdVersion})
db.get(null, key)
I compare value and version, if they match I do final update erasing old version. If any step fail, the whole process restarts.
I feel that this is very suboptimal - am I wrong?