use of io.atomix.utils.serializer.Serializer in project atomix by atomix.
the class ConsistentTreeMapProxyBuilder method buildAsync.
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<ConsistentTreeMap<V>> buildAsync() {
PrimitiveProtocol protocol = protocol();
return managementService.getPartitionService().getPartitionGroup(protocol).getPartition(name()).getPrimitiveClient().newProxy(name(), primitiveType(), protocol).connect().thenApply(proxy -> {
ConsistentTreeMapProxy rawMap = new ConsistentTreeMapProxy(proxy);
Serializer serializer = serializer();
return new TranscodingAsyncConsistentTreeMap<V, byte[]>(rawMap, value -> value == null ? null : serializer.encode(value), bytes -> serializer.decode(bytes)).sync();
});
}
use of io.atomix.utils.serializer.Serializer in project atomix by atomix.
the class AtomicCounterMapProxyBuilder method buildAsync.
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<AtomicCounterMap<K>> buildAsync() {
PrimitiveProtocol protocol = protocol();
return managementService.getPartitionService().getPartitionGroup(protocol).getPartition(name()).getPrimitiveClient().newProxy(name(), primitiveType(), protocol).connect().thenApply(proxy -> {
AtomicCounterMapProxy rawMap = new AtomicCounterMapProxy(proxy);
Serializer serializer = serializer();
return new TranscodingAsyncAtomicCounterMap<K, String>(rawMap, key -> BaseEncoding.base16().encode(serializer.encode(key)), string -> serializer.decode(BaseEncoding.base16().decode(string))).sync();
});
}
use of io.atomix.utils.serializer.Serializer 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());
}
});
}
use of io.atomix.utils.serializer.Serializer in project atomix by atomix.
the class ConsistentMapProxyBuilder method buildAsync.
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<ConsistentMap<K, V>> buildAsync() {
PrimitiveProtocol protocol = protocol();
PartitionGroup<?> partitions = managementService.getPartitionService().getPartitionGroup(protocol);
Map<PartitionId, CompletableFuture<AsyncConsistentMap<byte[], byte[]>>> maps = Maps.newConcurrentMap();
for (Partition partition : partitions.getPartitions()) {
maps.put(partition.id(), partition.getPrimitiveClient().newProxy(name(), primitiveType(), protocol).connect().thenApply(proxy -> new TranscodingAsyncConsistentMap<>(new ConsistentMapProxy(proxy), BaseEncoding.base16()::encode, BaseEncoding.base16()::decode, Function.identity(), Function.identity())));
}
Partitioner<byte[]> partitioner = key -> {
int bucket = Math.abs(Hashing.murmur3_32().hashBytes(key).asInt()) % NUM_BUCKETS;
return partitions.getPartitionIds().get(Hashing.consistentHash(bucket, partitions.getPartitionIds().size()));
};
return Futures.allOf(Lists.newArrayList(maps.values())).thenApply(m -> {
AsyncConsistentMap<byte[], byte[]> partitionedMap = new PartitionedAsyncConsistentMap<>(name(), Maps.transformValues(maps, v -> v.getNow(null)), partitioner);
Serializer serializer = serializer();
AsyncConsistentMap<K, V> map = new TranscodingAsyncConsistentMap<>(partitionedMap, key -> serializer.encode(key), bytes -> serializer.decode(bytes), value -> value == null ? null : serializer.encode(value), bytes -> serializer.decode(bytes));
if (!nullValues()) {
map = new NotNullAsyncConsistentMap<>(map);
}
if (relaxedReadConsistency()) {
map = new CachingAsyncConsistentMap<>(map);
}
if (readOnly()) {
map = new UnmodifiableAsyncConsistentMap<>(map);
}
return map.sync();
});
}
use of io.atomix.utils.serializer.Serializer in project atomix by atomix.
the class ConsistentMultimapProxyBuilder method buildAsync.
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<ConsistentMultimap<K, V>> buildAsync() {
PrimitiveProtocol protocol = protocol();
return managementService.getPartitionService().getPartitionGroup(protocol).getPartition(name()).getPrimitiveClient().newProxy(name(), primitiveType(), protocol).connect().thenApply(proxy -> {
AsyncConsistentMultimap<String, byte[]> rawMap = new ConsistentSetMultimapProxy(proxy);
Serializer serializer = serializer();
return new TranscodingAsyncConsistentMultimap<K, V, String, byte[]>(rawMap, key -> BaseEncoding.base16().encode(serializer.encode(key)), string -> serializer.decode(BaseEncoding.base16().decode(string)), value -> serializer.encode(value), bytes -> serializer.decode(bytes)).sync();
});
}
Aggregations