use of io.atomix.core.tree.DocumentTree in project atomix by atomix.
the class DocumentTreeProxyBuilder method buildAsync.
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<DocumentTree<V>> buildAsync() {
PrimitiveProtocol protocol = protocol();
PartitionGroup<?> partitions = managementService.getPartitionService().getPartitionGroup(protocol);
Map<PartitionId, CompletableFuture<AsyncDocumentTree<V>>> trees = Maps.newConcurrentMap();
for (Partition partition : partitions.getPartitions()) {
trees.put(partition.id(), partition.getPrimitiveClient().newProxy(name(), primitiveType(), protocol).connect().thenApply(proxy -> {
DocumentTreeProxy rawTree = new DocumentTreeProxy(proxy);
return new TranscodingAsyncDocumentTree<>(rawTree, serializer()::encode, serializer()::decode);
}));
}
Partitioner<DocumentPath> partitioner = key -> {
int bucket = (key == null) ? 0 : Math.abs(Hashing.murmur3_32().hashUnencodedChars(key.pathElements().size() == 1 ? key.pathElements().get(0) : key.pathElements().get(1)).asInt()) % NUM_BUCKETS;
return partitions.getPartitionIds().get(Hashing.consistentHash(bucket, partitions.getPartitionIds().size()));
};
return Futures.allOf(Lists.newArrayList(trees.values())).thenApply(t -> {
AsyncDocumentTree<V> tree = new PartitionedAsyncDocumentTree<>(name(), Maps.transformValues(trees, v -> v.getNow(null)), partitioner);
if (relaxedReadConsistency()) {
tree = new CachingAsyncDocumentTree<>(tree);
}
return tree.sync();
});
}
Aggregations