Repository: curator Updated Branches: refs/heads/CURATOR-3.0 69b5a6581 -> f1e6183e4
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/9fd56ce3 Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/9fd56ce3 Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/9fd56ce3 Branch: refs/heads/CURATOR-3.0 Commit: 9fd56ce396e03347dd44e6498cd37c9f1ed16be0 Parents: 3016ce2 Author: randgalt <[email protected]> Authored: Sat May 9 12:39:14 2015 -0500 Committer: randgalt <[email protected]> Committed: Sat May 9 12:39:14 2015 -0500 ---------------------------------------------------------------------- .../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/9fd56ce3/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 9c23ddb..850e6e3 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; @@ -129,6 +130,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/9fd56ce3/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/9fd56ce3/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/9fd56ce3/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; + } +}
