Initial work on updated transaction APIs
Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/d1e17eb2 Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/d1e17eb2 Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/d1e17eb2 Branch: refs/heads/CURATOR-215 Commit: d1e17eb2adeceb632a5b7e08159a864b4827c1f7 Parents: 0d6d82a Author: randgalt <[email protected]> Authored: Sat May 9 12:39:14 2015 -0500 Committer: Scott Blum <[email protected]> Committed: Wed Aug 12 17:29:42 2015 -0400 ---------------------------------------------------------------------- .../curator/framework/CuratorFramework.java | 8 ++ .../transaction/CuratorMultiTransaction.java | 26 ++++ .../CuratorMultiTransactionMain.java | 30 ++++ .../imps/CuratorMultiTransactionImpl.java | 139 +++++++++++++++++++ 4 files changed, 203 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/d1e17eb2/curator-framework/src/main/java/org/apache/curator/framework/CuratorFramework.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/main/java/org/apache/curator/framework/CuratorFramework.java b/curator-framework/src/main/java/org/apache/curator/framework/CuratorFramework.java index 7de49ce..f14253f 100644 --- a/curator-framework/src/main/java/org/apache/curator/framework/CuratorFramework.java +++ b/curator-framework/src/main/java/org/apache/curator/framework/CuratorFramework.java @@ -21,6 +21,7 @@ package org.apache.curator.framework; import org.apache.curator.CuratorZookeeperClient; import org.apache.curator.framework.api.*; +import org.apache.curator.framework.api.transaction.CuratorMultiTransaction; import org.apache.curator.framework.api.transaction.CuratorTransaction; import org.apache.curator.framework.imps.CuratorFrameworkState; import org.apache.curator.framework.listen.Listenable; @@ -143,6 +144,13 @@ public interface CuratorFramework extends Closeable public CuratorTransaction inTransaction(); /** + * Start a transaction builder + * + * @return builder object + */ + public CuratorMultiTransaction transaction(); + + /** * Perform a sync on the given path - syncs are always in the background * * @param path the path http://git-wip-us.apache.org/repos/asf/curator/blob/d1e17eb2/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/CuratorMultiTransaction.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/CuratorMultiTransaction.java b/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/CuratorMultiTransaction.java new file mode 100644 index 0000000..605ad2e --- /dev/null +++ b/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/CuratorMultiTransaction.java @@ -0,0 +1,26 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.curator.framework.api.transaction; + +import org.apache.curator.framework.api.Backgroundable; + +public interface CuratorMultiTransaction + extends Backgroundable<CuratorMultiTransactionMain> +{ +} http://git-wip-us.apache.org/repos/asf/curator/blob/d1e17eb2/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/CuratorMultiTransactionMain.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/CuratorMultiTransactionMain.java b/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/CuratorMultiTransactionMain.java new file mode 100644 index 0000000..02c59af --- /dev/null +++ b/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/CuratorMultiTransactionMain.java @@ -0,0 +1,30 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.curator.framework.api.transaction; + +import org.apache.zookeeper.Op; +import org.apache.zookeeper.OpResult; +import java.util.List; + +public interface CuratorMultiTransactionMain +{ + List<OpResult> forOperations(Op... operations) throws Exception; + + List<OpResult> forOperations(List<Op> operations) throws Exception; +} http://git-wip-us.apache.org/repos/asf/curator/blob/d1e17eb2/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java new file mode 100644 index 0000000..0ba3dc7 --- /dev/null +++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java @@ -0,0 +1,139 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.curator.framework.imps; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import org.apache.curator.RetryLoop; +import org.apache.curator.TimeTrace; +import org.apache.curator.framework.api.BackgroundCallback; +import org.apache.curator.framework.api.transaction.CuratorMultiTransaction; +import org.apache.curator.framework.api.transaction.CuratorMultiTransactionMain; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.OpResult; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.Executor; + +public class CuratorMultiTransactionImpl implements + CuratorMultiTransaction, + CuratorMultiTransactionMain, + BackgroundOperation<Void> +{ + private final CuratorFrameworkImpl client; + private Backgrounding backgrounding = new Backgrounding(); + + public CuratorMultiTransactionImpl(CuratorFrameworkImpl client) + { + this.client = client; + } + + @Override + public CuratorMultiTransactionMain inBackground() + { + backgrounding = new Backgrounding(true); + return this; + } + + @Override + public CuratorMultiTransactionMain inBackground(Object context) + { + backgrounding = new Backgrounding(context); + return this; + } + + @Override + public CuratorMultiTransactionMain inBackground(BackgroundCallback callback) + { + backgrounding = new Backgrounding(callback); + return this; + } + + @Override + public CuratorMultiTransactionMain inBackground(BackgroundCallback callback, Object context) + { + backgrounding = new Backgrounding(callback, context); + return this; + } + + @Override + public CuratorMultiTransactionMain inBackground(BackgroundCallback callback, Executor executor) + { + backgrounding = new Backgrounding(callback, executor); + return this; + } + + @Override + public CuratorMultiTransactionMain inBackground(BackgroundCallback callback, Object context, Executor executor) + { + backgrounding = new Backgrounding(client, callback, context, executor); + return this; + } + + @Override + public List<OpResult> forOperations(Op... operations) throws Exception + { + List<Op> ops = (operations != null) ? Arrays.asList(operations) : Lists.<Op>newArrayList(); + return forOperations(ops); + } + + @Override + public List<OpResult> forOperations(List<Op> operations) throws Exception + { + operations = Preconditions.checkNotNull(operations, "operations cannot be null"); + Preconditions.checkArgument(!operations.isEmpty(), "operations list cannot be empty"); + + if ( backgrounding.inBackground() ) + { + client.processBackgroundOperation(new OperationAndData<>(this, null, backgrounding.getCallback(), null, backgrounding.getContext()), null); + return null; + } + else + { + return forOperationsInForeground(operations); + } + } + + @Override + public void performBackgroundOperation(OperationAndData<Void> data) throws Exception + { + + } + + private List<OpResult> forOperationsInForeground(final List<Op> operations) throws Exception + { + TimeTrace trace = client.getZookeeperClient().startTracer("CuratorMultiTransactionImpl-Foreground"); + List<OpResult> responseData = RetryLoop.callWithRetry + ( + client.getZookeeperClient(), + new Callable<List<OpResult>>() + { + @Override + public List<OpResult> call() throws Exception + { + return client.getZooKeeper().multi(operations); + } + } + ); + trace.commit(); + + return responseData; + } +}
