Implement scatter/gather on module shards
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / ClientSnapshot.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.databroker.actors.dds;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.util.concurrent.FluentFuture;
12 import java.util.Optional;
13 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
14 import org.opendaylight.controller.cluster.datastore.utils.RootScatterGather;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17
18 /**
19  * Snapshot of the datastore state. Note this snapshot is not consistent across shards because sub-shard snapshots are
20  * created lazily.
21  *
22  * @author Robert Varga
23  */
24 @Beta
25 public class ClientSnapshot extends AbstractClientHandle<AbstractProxyTransaction> {
26     // Hidden to prevent outside instantiation
27     ClientSnapshot(final AbstractClientHistory parent, final TransactionIdentifier transactionId) {
28         super(parent, transactionId);
29     }
30
31     public FluentFuture<Boolean> exists(final YangInstanceIdentifier path) {
32         return ensureProxy(path).exists(path);
33     }
34
35     public FluentFuture<Optional<NormalizedNode>> read(final YangInstanceIdentifier path) {
36         return path.isEmpty() ? readRoot() : ensureProxy(path).read(path);
37     }
38
39     private FluentFuture<Optional<NormalizedNode>> readRoot() {
40         return RootScatterGather.gather(parent().actorUtils(), ensureAllProxies()
41             .map(proxy -> proxy.read(YangInstanceIdentifier.empty())));
42     }
43
44     @Override
45     final AbstractProxyTransaction createProxy(final Long shard) {
46         return parent().createSnapshotProxy(getIdentifier(), shard);
47     }
48 }