*/
package org.opendaylight.controller.cluster.databroker.actors.dds;
+import static com.google.common.base.Preconditions.checkState;
+
import com.google.common.annotations.Beta;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.FluentFuture;
import java.util.Collection;
+import java.util.Map;
import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
-import org.opendaylight.mdsal.dom.api.DOMDataTreeCursor;
-import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
+import org.opendaylight.controller.cluster.datastore.utils.RootScatterGather;
import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
/**
*/
@Beta
public class ClientTransaction extends AbstractClientHandle<AbstractProxyTransaction> {
-
- private ClientTransactionCursor cursor;
-
ClientTransaction(final AbstractClientHistory parent, final TransactionIdentifier transactionId) {
super(parent, transactionId);
}
- private AbstractProxyTransaction ensureTransactionProxy(final YangInstanceIdentifier path) {
- return ensureProxy(path);
+ public FluentFuture<Boolean> exists(final YangInstanceIdentifier path) {
+ return ensureProxy(path).exists(path);
}
- public DOMDataTreeWriteCursor openCursor() {
- Preconditions.checkState(cursor == null, "Transaction %s has open cursor", getIdentifier());
- cursor = new ClientTransactionCursor(this);
- return cursor;
+ public FluentFuture<Optional<NormalizedNode>> read(final YangInstanceIdentifier path) {
+ return path.isEmpty() ? readRoot() : ensureProxy(path).read(path);
}
- public FluentFuture<Boolean> exists(final YangInstanceIdentifier path) {
- return ensureTransactionProxy(path).exists(path);
+ private FluentFuture<Optional<NormalizedNode>> readRoot() {
+ return RootScatterGather.gather(parent().actorUtils(), ensureAllProxies()
+ .map(proxy -> proxy.read(YangInstanceIdentifier.empty())));
}
- public FluentFuture<Optional<NormalizedNode<?, ?>>> read(final YangInstanceIdentifier path) {
- return ensureTransactionProxy(path).read(path);
+ public void delete(final YangInstanceIdentifier path) {
+ if (path.isEmpty()) {
+ ensureAllProxies().forEach(proxy -> proxy.delete(YangInstanceIdentifier.empty()));
+ } else {
+ ensureProxy(path).delete(path);
+ }
}
- public void delete(final YangInstanceIdentifier path) {
- ensureTransactionProxy(path).delete(path);
+ public void merge(final YangInstanceIdentifier path, final NormalizedNode data) {
+ if (path.isEmpty()) {
+ mergeRoot(RootScatterGather.castRootNode(data));
+ } else {
+ ensureProxy(path).merge(path, data);
+ }
+ }
+
+ private void mergeRoot(final @NonNull ContainerNode rootData) {
+ if (!rootData.isEmpty()) {
+ RootScatterGather.scatterTouched(rootData, this::ensureProxy).forEach(
+ scattered -> scattered.shard().merge(YangInstanceIdentifier.empty(), scattered.container()));
+ }
+ }
+
+ public void write(final YangInstanceIdentifier path, final NormalizedNode data) {
+ if (path.isEmpty()) {
+ writeRoot(RootScatterGather.castRootNode(data));
+ } else {
+ ensureProxy(path).write(path, data);
+ }
}
- public void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
- ensureTransactionProxy(path).merge(path, data);
+ private void writeRoot(final @NonNull ContainerNode rootData) {
+ RootScatterGather.scatterAll(rootData, this::ensureProxy, ensureAllProxies()).forEach(
+ scattered -> scattered.shard().write(YangInstanceIdentifier.empty(), scattered.container()));
}
- public void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
- ensureTransactionProxy(path).write(path, data);
+ private AbstractProxyTransaction ensureProxy(final PathArgument childId) {
+ return ensureProxy(YangInstanceIdentifier.create(childId));
}
public DOMStoreThreePhaseCommitCohort ready() {
- final Collection<AbstractProxyTransaction> toReady = ensureClosed();
- Preconditions.checkState(toReady != null, "Attempted to submit a closed transaction %s", this);
+ final Map<Long, AbstractProxyTransaction> participants = ensureClosed();
+ checkState(participants != null, "Attempted to submit a closed transaction %s", this);
+ final Collection<AbstractProxyTransaction> toReady = participants.values();
toReady.forEach(AbstractProxyTransaction::seal);
+
+ final TransactionIdentifier txId = getIdentifier();
+ final AbstractClientHistory parent = parent();
+ parent.onTransactionShardsBound(txId, participants.keySet());
+
final AbstractTransactionCommitCohort cohort;
switch (toReady.size()) {
case 0:
- cohort = new EmptyTransactionCommitCohort(parent(), getIdentifier());
+ cohort = new EmptyTransactionCommitCohort(parent, txId);
break;
case 1:
- cohort = new DirectTransactionCommitCohort(parent(), getIdentifier(),
- Iterables.getOnlyElement(toReady));
+ cohort = new DirectTransactionCommitCohort(parent, txId, toReady.iterator().next());
break;
default:
- cohort = new ClientTransactionCommitCohort(parent(), getIdentifier(), toReady);
+ cohort = new ClientTransactionCommitCohort(parent, txId, toReady);
break;
}
- return parent().onTransactionReady(this, cohort);
+ return parent.onTransactionReady(this, cohort);
}
@Override
final AbstractProxyTransaction createProxy(final Long shard) {
return parent().createTransactionProxy(getIdentifier(), shard);
}
-
- void closeCursor(final @NonNull DOMDataTreeCursor cursorToClose) {
- if (cursorToClose.equals(this.cursor)) {
- this.cursor = null;
- }
- }
}