title: Compare and swap in Berkeley DB JE?
body: <p>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.</p>
  
  <p>I was looking for a some kind of method like </p>
  
  <pre><code>replace(Transaction, Key, ExpectedValue, NewValue) 
  </code></pre>
  
  <p>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. </p>
  
  <p>Looks like there is no method like that, so I'm wondering how is this supposed to be done in the most efficient way.</p>
  
  <p>Right now I'm using the following approach: I do </p>
  
  <pre><code>db.get(null, key) -&gt; {currentValue, version}
  db.put(null, key, {currentValue, newRandomIdVersion}) 
  db.get(null, key)
  </code></pre>
  
  <p>I compare value and version, if they match I do final update erasing old version. If any step fail, the whole process restarts.</p>
  
  <p>I feel that this is very suboptimal - am I wrong?</p>