public PutResultImpl<K, V> prevKv(KeyValue<K, V> kv) { if (null != prevKv) { prevKv.close(); } prevKv = kv; return this; }
private void applyDeleteOp(long revision, DeleteOp<byte[], byte[]> op, boolean ignoreSmallerRevision, MVCCStoreImpl<byte[], byte[]> localStore) { try (DeleteResult<byte[], byte[]> result = localStore.delete(revision, op)) { if (Code.OK == result.code() || (ignoreSmallerRevision && Code.SMALLER_REVISION == result.code())) { return; } throw new MVCCStoreException(result.code(), "Failed to apply command " + op + " at revision " + revision + " to the state store " + localStore.name()); } }
private void applyPutOp(long revision, PutOp<byte[], byte[]> op, boolean ignoreSmallerRevision, MVCCStoreImpl<byte[], byte[]> localStore) { try (PutResult<byte[], byte[]> result = localStore.put(revision, op)) { if (Code.OK == result.code() || (ignoreSmallerRevision && Code.SMALLER_REVISION == result.code())) { return; } throw new MVCCStoreException(result.code(), "Failed to apply command " + op + " at revision " + revision + " to the state store " + localStore.name()); } }
default CompletableFuture<V> delete(K pKey, K lKey) { return delete(pKey, lKey, Options.deleteAndGet()) .thenApply(result -> { try { List<KeyValue<K, V>> prevKvs = result.prevKvs(); if (prevKvs.isEmpty()) { return null; } else { return retain(prevKvs.get(0).value()); } } finally { result.close(); } }); }
default CompletableFuture<Long> incrementAndGet(K key, long amount) { return increment(key, amount, Options.incrementAndGet()) .thenApply(result -> { try { return result.totalAmount(); } finally { result.close(); } }); }
@Override default CompletableFuture<List<KeyValue<K, V>>> range(K key, K endKey) { RangeOp<K, V> op = newRange(key, endKey); return range(op).thenCompose(result -> { try { if (Code.OK == result.code()) { return FutureUtils.value(result.getKvsAndClear()); } else { return failWithCode(result.code(), "Failed to retrieve range [" + key + ", " + endKey + "] from store " + name()); } } finally { result.close(); } }); }
default CompletableFuture<Void> put(K key, V value) { if (value == null) { return delete(key).thenApply(ignored -> null); } return put(key, value, Options.blindPut()) .thenApply(result -> { try { return (Void) null; } finally { result.close(); } }); }
default CompletableFuture<Void> increment(K key, long amount) { return increment(key, amount, Options.blindIncrement()) .thenApply(result -> { try { return (Void) null; } finally { result.close(); } }); }
synchronized DeleteResult<K, V> processDelete(long revision, DeleteOp<K, V> op) { checkStoreOpen(); WriteBatch batch = new WriteBatch(); DeleteResult<K, V> result = null; try { result = delete(revision, batch, op, true); executeBatch(batch); return result; } catch (StateStoreRuntimeException e) { if (null != result) { result.close(); } throw e; } finally { RocksUtils.close(batch); } }
private void applyIncrOp(long revision, IncrementOp<byte[], byte[]> op, boolean ignoreSmallerRevision, MVCCStoreImpl<byte[], byte[]> localStore) { try (IncrementResult<byte[], byte[]> result = localStore.increment(revision, op)) { if (Code.OK == result.code() || (ignoreSmallerRevision && Code.SMALLER_REVISION == result.code())) { return; } throw new MVCCStoreException(result.code(), "Failed to apply command " + op + " at revision " + revision + " to the state store " + localStore.name()); } }
default CompletableFuture<Long> getAndIncrement(K pKey, K lKey, long amount) { return increment(pKey, lKey, amount, Options.incrementAndGet()) .thenApply(result -> { try { return result.totalAmount() - amount; } finally { result.close(); } }); }
default CompletableFuture<Void> put(K pKey, K lKey, V value) { if (value == null) { return delete(pKey, lKey).thenApply(ignored -> null); } return put(pKey, lKey, value, Options.blindPut()) .thenApply(result -> { try { return (Void) null; } finally { result.close(); } }); }
default CompletableFuture<Void> increment(K pKey, K lKey, long amount) { return increment(pKey, lKey, amount, Options.blindIncrement()) .thenApply(result -> { try { return (Void) null; } finally { result.close(); } }); }
@Override protected void reset() { if (null != prevKv) { prevKv.close(); prevKv = null; } super.reset(); }
void delete(K key, long revision) { try (DeleteOp<K, V> op = opFactory.newDelete( key, Options.delete())) { try (DeleteResult<K, V> result = delete(revision, op)) { if (Code.OK != result.code()) { throw new MVCCStoreException(result.code(), "Failed to delete key=" + key + "from state store " + name); } } } }
void put(K key, V value, long revision) { try (PutOp<K, V> op = opFactory.newPut( key, value, Options.blindPut())) { try (PutResult<K, V> result = put(revision, op)) { if (Code.OK != result.code()) { throw new MVCCStoreException(result.code(), "Failed to put (" + key + ", " + value + ", " + revision + ") to state store " + name); } } } }
void increment(K key, long amount, long revision) { try (IncrementOp<K, V> op = opFactory.newIncrement(key, amount, Options.blindIncrement())) { try (IncrementResult<K, V> result = increment(revision, op)) { if (Code.OK != result.code()) { throw new MVCCStoreException(result.code(), "Failed to increment (" + key + ", " + amount + ") to state store " + name); } } } }
default CompletableFuture<Long> incrementAndGet(K pKey, K lKey, long amount) { return increment(pKey, lKey, amount, Options.incrementAndGet()) .thenApply(result -> { try { return result.totalAmount(); } finally { result.close(); } }); }
default CompletableFuture<Long> getAndIncrement(K key, long amount) { return increment(key, amount, Options.incrementAndGet()) .thenApply(result -> { try { return result.totalAmount() - amount; } finally { result.close(); } }); }