use of io.atomix.core.map.impl.ConsistentMapOperations.PUT in project atomix by atomix.
the class ConsistentMapServiceTest method testSnapshot.
@Test
@SuppressWarnings("unchecked")
public void testSnapshot() throws Exception {
ConsistentMapService service = new TestConsistentMapService();
service.put(new DefaultCommit<>(2, PUT, new Put("foo", "Hello world!".getBytes(), 1000), mock(Session.class), System.currentTimeMillis()));
Buffer buffer = HeapBuffer.allocate();
service.backup(buffer);
service = new TestConsistentMapService();
service.restore(buffer.flip());
Versioned<byte[]> value = service.get(new DefaultCommit<>(2, GET, new Get("foo"), mock(Session.class), System.currentTimeMillis()));
assertNotNull(value);
assertArrayEquals("Hello world!".getBytes(), value.value());
assertNotNull(service.entries().get("foo").timer);
}
use of io.atomix.core.map.impl.ConsistentMapOperations.PUT in project atomix by atomix.
the class ConsistentMapProxy method computeIf.
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<Versioned<byte[]>> computeIf(String key, Predicate<? super byte[]> condition, BiFunction<? super String, ? super byte[], ? extends byte[]> remappingFunction) {
return get(key).thenCompose(r1 -> {
byte[] existingValue = r1 == null ? null : r1.value();
// if the condition evaluates to false, return existing value.
if (!condition.test(existingValue)) {
return CompletableFuture.completedFuture(r1);
}
byte[] computedValue;
try {
computedValue = remappingFunction.apply(key, existingValue);
} catch (Exception e) {
return Futures.exceptionalFuture(e);
}
if (computedValue == null && r1 == null) {
return CompletableFuture.completedFuture(null);
}
if (r1 == null) {
return proxy.<Put, MapEntryUpdateResult<String, byte[]>>invoke(PUT_IF_ABSENT, serializer()::encode, new Put(key, computedValue, 0), serializer()::decode).whenComplete((r, e) -> throwIfLocked(r)).thenCompose(r -> checkLocked(r)).thenApply(result -> new Versioned<>(computedValue, result.version()));
} else if (computedValue == null) {
return proxy.<RemoveVersion, MapEntryUpdateResult<String, byte[]>>invoke(REMOVE_VERSION, serializer()::encode, new RemoveVersion(key, r1.version()), serializer()::decode).whenComplete((r, e) -> throwIfLocked(r)).thenCompose(r -> checkLocked(r)).thenApply(v -> null);
} else {
return proxy.<ReplaceVersion, MapEntryUpdateResult<String, byte[]>>invoke(REPLACE_VERSION, serializer()::encode, new ReplaceVersion(key, r1.version(), computedValue), serializer()::decode).whenComplete((r, e) -> throwIfLocked(r)).thenCompose(r -> checkLocked(r)).thenApply(result -> result.status() == MapEntryUpdateResult.Status.OK ? new Versioned(computedValue, result.version()) : result.result());
}
});
}
Aggregations