| | |
| | | } |
| | | |
| | | @Override |
| | | public ByteString getRMW(final TreeName treeName, final ByteSequence key) |
| | | { |
| | | return read(treeName, key); |
| | | } |
| | | |
| | | @Override |
| | | public Cursor openCursor(final TreeName treeName) |
| | | { |
| | | try |
| | |
| | | } |
| | | |
| | | @Override |
| | | public boolean putIfAbsent(final TreeName treeName, final ByteSequence key, |
| | | final ByteSequence value) |
| | | { |
| | | try |
| | | { |
| | | // There is no CAS (Compare And Swap) operation to do here :) |
| | | // Following code is fine because Persistit provides snapshot isolation. |
| | | // If another thread tries to update the same key, we'll get a RollbackException |
| | | // And the write operation will be retried (see write() method in this class) |
| | | final Exchange ex = getExchangeFromCache(treeName); |
| | | bytesToKey(ex.getKey(), key); |
| | | ex.fetch(); |
| | | final Value exValue = ex.getValue(); |
| | | if (exValue.isDefined()) |
| | | { |
| | | return false; |
| | | } |
| | | bytesToValue(exValue, value); |
| | | ex.store(); |
| | | return true; |
| | | } |
| | | catch (final Exception e) |
| | | { |
| | | throw new StorageRuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public ByteString read(final TreeName treeName, final ByteSequence key) |
| | | { |
| | | try |
| | |
| | | final ByteSequence newValue = f.computeNewValue(oldValue); |
| | | if (!equals(newValue, oldValue)) |
| | | { |
| | | ex.getValue().clear().putByteArray(newValue.toByteArray()); |
| | | ex.store(); |
| | | if (newValue == null) |
| | | { |
| | | ex.remove(); |
| | | } |
| | | else |
| | | { |
| | | ex.getValue().clear().putByteArray(newValue.toByteArray()); |
| | | ex.store(); |
| | | } |
| | | return true; |
| | | } |
| | | return false; |