Search in sources :

Example 1 with GET

use of io.atomix.core.map.impl.ConsistentMapOperations.GET 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);
}
Also used : Buffer(io.atomix.storage.buffer.Buffer) HeapBuffer(io.atomix.storage.buffer.HeapBuffer) Get(io.atomix.core.map.impl.ConsistentMapOperations.Get) Put(io.atomix.core.map.impl.ConsistentMapOperations.Put) Test(org.junit.Test)

Example 2 with GET

use of io.atomix.core.map.impl.ConsistentMapOperations.GET 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());
        }
    });
}
Also used : TransactionId(io.atomix.core.transaction.TransactionId) BiFunction(java.util.function.BiFunction) REMOVE_LISTENER(io.atomix.core.map.impl.ConsistentMapOperations.REMOVE_LISTENER) MapEventListener(io.atomix.core.map.MapEventListener) IS_EMPTY(io.atomix.core.map.impl.ConsistentMapOperations.IS_EMPTY) Remove(io.atomix.core.map.impl.ConsistentMapOperations.Remove) KEY_SET(io.atomix.core.map.impl.ConsistentMapOperations.KEY_SET) Duration(java.time.Duration) Map(java.util.Map) COMMIT(io.atomix.core.map.impl.ConsistentMapOperations.COMMIT) ENTRY_SET(io.atomix.core.map.impl.ConsistentMapOperations.ENTRY_SET) REPLACE_VERSION(io.atomix.core.map.impl.ConsistentMapOperations.REPLACE_VERSION) ADD_LISTENER(io.atomix.core.map.impl.ConsistentMapOperations.ADD_LISTENER) ReplaceVersion(io.atomix.core.map.impl.ConsistentMapOperations.ReplaceVersion) Predicate(java.util.function.Predicate) REMOVE(io.atomix.core.map.impl.ConsistentMapOperations.REMOVE) Collection(java.util.Collection) RemoveValue(io.atomix.core.map.impl.ConsistentMapOperations.RemoveValue) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) CONTAINS_KEY(io.atomix.core.map.impl.ConsistentMapOperations.CONTAINS_KEY) PUT_AND_GET(io.atomix.core.map.impl.ConsistentMapOperations.PUT_AND_GET) Versioned(io.atomix.utils.time.Versioned) ContainsValue(io.atomix.core.map.impl.ConsistentMapOperations.ContainsValue) CHANGE(io.atomix.core.map.impl.ConsistentMapEvents.CHANGE) List(java.util.List) GET_OR_DEFAULT(io.atomix.core.map.impl.ConsistentMapOperations.GET_OR_DEFAULT) KryoNamespaces(io.atomix.utils.serializer.KryoNamespaces) TransactionPrepare(io.atomix.core.map.impl.ConsistentMapOperations.TransactionPrepare) Entry(java.util.Map.Entry) Get(io.atomix.core.map.impl.ConsistentMapOperations.Get) Serializer(io.atomix.utils.serializer.Serializer) GET(io.atomix.core.map.impl.ConsistentMapOperations.GET) ConcurrentModificationException(java.util.ConcurrentModificationException) GET_ALL_PRESENT(io.atomix.core.map.impl.ConsistentMapOperations.GET_ALL_PRESENT) REMOVE_VALUE(io.atomix.core.map.impl.ConsistentMapOperations.REMOVE_VALUE) GetOrDefault(io.atomix.core.map.impl.ConsistentMapOperations.GetOrDefault) ConsistentMapException(io.atomix.core.map.ConsistentMapException) PUT_IF_ABSENT(io.atomix.core.map.impl.ConsistentMapOperations.PUT_IF_ABSENT) TransactionCommit(io.atomix.core.map.impl.ConsistentMapOperations.TransactionCommit) CompletableFuture(java.util.concurrent.CompletableFuture) CLEAR(io.atomix.core.map.impl.ConsistentMapOperations.CLEAR) HashSet(java.util.HashSet) AsyncConsistentMap(io.atomix.core.map.AsyncConsistentMap) ConsistentMap(io.atomix.core.map.ConsistentMap) MapEvent(io.atomix.core.map.MapEvent) Replace(io.atomix.core.map.impl.ConsistentMapOperations.Replace) KryoNamespace(io.atomix.utils.serializer.KryoNamespace) REPLACE_VALUE(io.atomix.core.map.impl.ConsistentMapOperations.REPLACE_VALUE) ROLLBACK(io.atomix.core.map.impl.ConsistentMapOperations.ROLLBACK) REMOVE_VERSION(io.atomix.core.map.impl.ConsistentMapOperations.REMOVE_VERSION) TransactionRollback(io.atomix.core.map.impl.ConsistentMapOperations.TransactionRollback) Futures(io.atomix.utils.concurrent.Futures) ContainsKey(io.atomix.core.map.impl.ConsistentMapOperations.ContainsKey) Executor(java.util.concurrent.Executor) AbstractAsyncPrimitive(io.atomix.primitive.impl.AbstractAsyncPrimitive) RemoveVersion(io.atomix.core.map.impl.ConsistentMapOperations.RemoveVersion) SIZE(io.atomix.core.map.impl.ConsistentMapOperations.SIZE) CONTAINS_VALUE(io.atomix.core.map.impl.ConsistentMapOperations.CONTAINS_VALUE) REPLACE(io.atomix.core.map.impl.ConsistentMapOperations.REPLACE) VALUES(io.atomix.core.map.impl.ConsistentMapOperations.VALUES) PREPARE(io.atomix.core.map.impl.ConsistentMapOperations.PREPARE) Put(io.atomix.core.map.impl.ConsistentMapOperations.Put) PUT(io.atomix.core.map.impl.ConsistentMapOperations.PUT) PrimitiveProxy(io.atomix.primitive.proxy.PrimitiveProxy) TransactionLog(io.atomix.core.transaction.TransactionLog) GetAllPresent(io.atomix.core.map.impl.ConsistentMapOperations.GetAllPresent) ReplaceValue(io.atomix.core.map.impl.ConsistentMapOperations.ReplaceValue) RemoveVersion(io.atomix.core.map.impl.ConsistentMapOperations.RemoveVersion) Versioned(io.atomix.utils.time.Versioned) ReplaceVersion(io.atomix.core.map.impl.ConsistentMapOperations.ReplaceVersion) ConcurrentModificationException(java.util.ConcurrentModificationException) ConsistentMapException(io.atomix.core.map.ConsistentMapException) Put(io.atomix.core.map.impl.ConsistentMapOperations.Put)

Aggregations

Get (io.atomix.core.map.impl.ConsistentMapOperations.Get)2 Put (io.atomix.core.map.impl.ConsistentMapOperations.Put)2 AsyncConsistentMap (io.atomix.core.map.AsyncConsistentMap)1 ConsistentMap (io.atomix.core.map.ConsistentMap)1 ConsistentMapException (io.atomix.core.map.ConsistentMapException)1 MapEvent (io.atomix.core.map.MapEvent)1 MapEventListener (io.atomix.core.map.MapEventListener)1 CHANGE (io.atomix.core.map.impl.ConsistentMapEvents.CHANGE)1 ADD_LISTENER (io.atomix.core.map.impl.ConsistentMapOperations.ADD_LISTENER)1 CLEAR (io.atomix.core.map.impl.ConsistentMapOperations.CLEAR)1 COMMIT (io.atomix.core.map.impl.ConsistentMapOperations.COMMIT)1 CONTAINS_KEY (io.atomix.core.map.impl.ConsistentMapOperations.CONTAINS_KEY)1 CONTAINS_VALUE (io.atomix.core.map.impl.ConsistentMapOperations.CONTAINS_VALUE)1 ContainsKey (io.atomix.core.map.impl.ConsistentMapOperations.ContainsKey)1 ContainsValue (io.atomix.core.map.impl.ConsistentMapOperations.ContainsValue)1 ENTRY_SET (io.atomix.core.map.impl.ConsistentMapOperations.ENTRY_SET)1 GET (io.atomix.core.map.impl.ConsistentMapOperations.GET)1 GET_ALL_PRESENT (io.atomix.core.map.impl.ConsistentMapOperations.GET_ALL_PRESENT)1 GET_OR_DEFAULT (io.atomix.core.map.impl.ConsistentMapOperations.GET_OR_DEFAULT)1 GetAllPresent (io.atomix.core.map.impl.ConsistentMapOperations.GetAllPresent)1