CURATOR-93 - Added compression in transaction functionality to the TransationSetDataBuilder and added unit tests for setting compressed data in a transaction. Updated unit tests for the create compressed in transaction cases to explicitly compare data rather than just the data's length.
Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/2df9b97d Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/2df9b97d Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/2df9b97d Branch: refs/heads/master Commit: 2df9b97dfdd8f322efcb5facf0ba933c2125e4ef Parents: 30e66cf Author: Cameron McKenzie <came...@unico.com.au> Authored: Thu Jun 19 08:06:43 2014 +1000 Committer: Cameron McKenzie <came...@unico.com.au> Committed: Thu Jun 19 08:06:43 2014 +1000 ---------------------------------------------------------------------- .../transaction/TransactionCreateBuilder.java | 1 - .../transaction/TransactionSetDataBuilder.java | 4 +- .../framework/imps/CreateBuilderImpl.java | 2 +- .../framework/imps/SetDataBuilderImpl.java | 14 +++ .../imps/TestCompressionInTransaction.java | 100 ++++++++++++++++++- 5 files changed, 116 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/2df9b97d/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/TransactionCreateBuilder.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/TransactionCreateBuilder.java b/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/TransactionCreateBuilder.java index 51f4cb7..6ac3069 100644 --- a/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/TransactionCreateBuilder.java +++ b/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/TransactionCreateBuilder.java @@ -20,7 +20,6 @@ package org.apache.curator.framework.api.transaction; import org.apache.curator.framework.api.ACLPathAndBytesable; import org.apache.curator.framework.api.Compressible; -import org.apache.curator.framework.api.CreateBackgroundModeACLable; import org.apache.curator.framework.api.CreateModable; import org.apache.curator.framework.api.PathAndBytesable; http://git-wip-us.apache.org/repos/asf/curator/blob/2df9b97d/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/TransactionSetDataBuilder.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/TransactionSetDataBuilder.java b/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/TransactionSetDataBuilder.java index 14fd005..777537a 100644 --- a/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/TransactionSetDataBuilder.java +++ b/curator-framework/src/main/java/org/apache/curator/framework/api/transaction/TransactionSetDataBuilder.java @@ -18,11 +18,13 @@ */ package org.apache.curator.framework.api.transaction; +import org.apache.curator.framework.api.Compressible; import org.apache.curator.framework.api.PathAndBytesable; import org.apache.curator.framework.api.Versionable; public interface TransactionSetDataBuilder extends PathAndBytesable<CuratorTransactionBridge>, - Versionable<PathAndBytesable<CuratorTransactionBridge>> + Versionable<PathAndBytesable<CuratorTransactionBridge>>, + Compressible<PathAndBytesable<CuratorTransactionBridge>> { } http://git-wip-us.apache.org/repos/asf/curator/blob/2df9b97d/curator-framework/src/main/java/org/apache/curator/framework/imps/CreateBuilderImpl.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/CreateBuilderImpl.java b/curator-framework/src/main/java/org/apache/curator/framework/imps/CreateBuilderImpl.java index b5d5f97..3cf23b8 100644 --- a/curator-framework/src/main/java/org/apache/curator/framework/imps/CreateBuilderImpl.java +++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/CreateBuilderImpl.java @@ -99,7 +99,7 @@ class CreateBuilderImpl implements CreateBuilder, BackgroundOperation<PathAndByt public CuratorTransactionBridge forPath(String path) throws Exception { return forPath(path, client.getDefaultData()); - } + } @Override public CuratorTransactionBridge forPath(String path, byte[] data) throws Exception http://git-wip-us.apache.org/repos/asf/curator/blob/2df9b97d/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java b/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java index c88ea55..8e93cbf 100644 --- a/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java +++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java @@ -20,6 +20,7 @@ package org.apache.curator.framework.imps; import org.apache.curator.RetryLoop; import org.apache.curator.TimeTrace; +import org.apache.curator.framework.api.ACLPathAndBytesable; import org.apache.curator.framework.api.BackgroundCallback; import org.apache.curator.framework.api.BackgroundPathAndBytesable; import org.apache.curator.framework.api.CuratorEvent; @@ -33,6 +34,7 @@ import org.apache.curator.framework.api.transaction.TransactionSetDataBuilder; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.Op; import org.apache.zookeeper.data.Stat; + import java.util.concurrent.Callable; import java.util.concurrent.Executor; @@ -58,6 +60,11 @@ class SetDataBuilderImpl implements SetDataBuilder, BackgroundOperation<PathAndB @Override public CuratorTransactionBridge forPath(String path, byte[] data) throws Exception { + if ( compress ) + { + data = client.getCompressionProvider().compress(path, data); + } + String fixedPath = client.fixForNamespace(path); transaction.add(Op.setData(fixedPath, data, version), OperationType.SET_DATA, path); return curatorTransaction; @@ -75,6 +82,13 @@ class SetDataBuilderImpl implements SetDataBuilder, BackgroundOperation<PathAndB SetDataBuilderImpl.this.withVersion(version); return this; } + + @Override + public PathAndBytesable<CuratorTransactionBridge> compressed() { + compress = true; + + return this; + } }; } http://git-wip-us.apache.org/repos/asf/curator/blob/2df9b97d/curator-framework/src/test/java/org/apache/curator/framework/imps/TestCompressionInTransaction.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestCompressionInTransaction.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestCompressionInTransaction.java index f5adaff..c18af99 100644 --- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestCompressionInTransaction.java +++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestCompressionInTransaction.java @@ -26,11 +26,74 @@ import org.apache.curator.framework.api.CompressionProvider; import org.apache.curator.retry.RetryOneTime; import org.testng.Assert; import org.testng.annotations.Test; + import java.util.concurrent.atomic.AtomicInteger; public class TestCompressionInTransaction extends BaseClassForTests { @Test + public void testSetData() throws Exception + { + final String path = "/a"; + final byte[] data = "here's a string".getBytes(); + + CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); + try + { + client.start(); + + //Create uncompressed data in a transaction + client.inTransaction().create().forPath(path, data).and().commit(); + Assert.assertEquals(data, client.getData().forPath(path)); + + //Create compressed data in transaction + client.inTransaction().setData().compressed().forPath(path, data).and().commit(); + Assert.assertEquals(data, client.getData().decompressed().forPath(path)); + } + finally + { + CloseableUtils.closeQuietly(client); + } + } + + @Test + public void testSetCompressedAndUncompressed() throws Exception + { + final String path1 = "/a"; + final String path2 = "/b"; + + final byte[] data1 = "here's a string".getBytes(); + final byte[] data2 = "here's another string".getBytes(); + + CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); + try + { + client.start(); + + //Create the nodes + client.inTransaction().create().compressed().forPath(path1).and(). + create().forPath(path2).and().commit(); + + //Check they exist + Assert.assertNotNull(client.checkExists().forPath(path1)); + Assert.assertNotNull(client.checkExists().forPath(path2)); + + //Set the nodes, path1 compressed, path2 uncompressed. + client.inTransaction().setData().compressed().forPath(path1, data1).and(). + setData().forPath(path2, data2).and().commit(); + + Assert.assertNotEquals(data1, client.getData().forPath(path1)); + Assert.assertEquals(data1, client.getData().decompressed().forPath(path1)); + + Assert.assertEquals(data2, client.getData().forPath(path2)); + } + finally + { + CloseableUtils.closeQuietly(client); + } + } + + @Test public void testSimple() throws Exception { final String path1 = "/a"; @@ -48,10 +111,43 @@ public class TestCompressionInTransaction extends BaseClassForTests create().compressed().forPath(path2, data2).and().commit(); Assert.assertNotEquals(data1, client.getData().forPath(path1)); - Assert.assertEquals(data1.length, client.getData().decompressed().forPath(path1).length); + Assert.assertEquals(data1, client.getData().decompressed().forPath(path1)); Assert.assertNotEquals(data2, client.getData().forPath(path2)); - Assert.assertEquals(data2.length, client.getData().decompressed().forPath(path2).length); + Assert.assertEquals(data2, client.getData().decompressed().forPath(path2)); + } + finally + { + CloseableUtils.closeQuietly(client); + } + } + + /** + * Test the case where both uncompressed and compressed data is generated in + * the same transaction + * @throws Exception + */ + @Test + public void testCreateCompressedAndUncompressed() throws Exception + { + final String path1 = "/a"; + final String path2 = "/b"; + + final byte[] data1 = "here's a string".getBytes(); + final byte[] data2 = "here's another string".getBytes(); + + CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); + try + { + client.start(); + + client.inTransaction().create().compressed().forPath(path1, data1).and(). + create().forPath(path2, data2).and().commit(); + + Assert.assertNotEquals(data1, client.getData().forPath(path1)); + Assert.assertEquals(data1, client.getData().decompressed().forPath(path1)); + + Assert.assertEquals(data2, client.getData().forPath(path2)); } finally {