From 30e66cfa3e8e7d09f553adbaa699edbd05993a4e Mon Sep 17 00:00:00 2001 From: Cameron McKenzie Date: Wed, 18 Jun 2014 11:28:23 +1000 Subject: [PATCH 1/2] CURATOR-93 - Added compress() option for transactions. --- .../transaction/TransactionCreateBuilder.java | 5 +- .../framework/imps/CreateBuilderImpl.java | 18 +++++- .../imps/TestCompressionInTransaction.java | 61 +++++++++++++++++++ 3 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 curator-framework/src/test/java/org/apache/curator/framework/imps/TestCompressionInTransaction.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 f269daf9e3..51f4cb7e24 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 @@ -19,12 +19,15 @@ 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; public interface TransactionCreateBuilder extends PathAndBytesable, CreateModable>, - ACLPathAndBytesable + ACLPathAndBytesable, + Compressible> { } 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 fa95b2c07f..b5d5f97bf4 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 @@ -87,16 +87,28 @@ public ACLPathAndBytesable withMode(CreateMode mode) CreateBuilderImpl.this.withMode(mode); return this; } - + + @Override + public ACLPathAndBytesable compressed() + { + CreateBuilderImpl.this.compressed(); + return this; + } + @Override public CuratorTransactionBridge forPath(String path) throws Exception { return forPath(path, client.getDefaultData()); - } + } @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.create(fixedPath, data, acling.getAclList(path), createMode), OperationType.CREATE, path); return curatorTransaction; 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 new file mode 100644 index 0000000000..f5adaffa46 --- /dev/null +++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestCompressionInTransaction.java @@ -0,0 +1,61 @@ +/** + * 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 org.apache.curator.test.BaseClassForTests; +import org.apache.curator.utils.CloseableUtils; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +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 testSimple() throws Exception + { + final String path1 = "/a"; + final String path2 = "/a/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().compressed().forPath(path2, data2).and().commit(); + + Assert.assertNotEquals(data1, client.getData().forPath(path1)); + Assert.assertEquals(data1.length, client.getData().decompressed().forPath(path1).length); + + Assert.assertNotEquals(data2, client.getData().forPath(path2)); + Assert.assertEquals(data2.length, client.getData().decompressed().forPath(path2).length); + } + finally + { + CloseableUtils.closeQuietly(client); + } + } +} From 2df9b97dfdd8f322efcb5facf0ba933c2125e4ef Mon Sep 17 00:00:00 2001 From: Cameron McKenzie Date: Thu, 19 Jun 2014 08:06:43 +1000 Subject: [PATCH 2/2] 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. --- .../transaction/TransactionCreateBuilder.java | 1 - .../TransactionSetDataBuilder.java | 4 +- .../framework/imps/CreateBuilderImpl.java | 2 +- .../framework/imps/SetDataBuilderImpl.java | 14 +++ .../imps/TestCompressionInTransaction.java | 100 +++++++++++++++++- 5 files changed, 116 insertions(+), 5 deletions(-) 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 51f4cb7e24..6ac30692db 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 @@ 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; 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 14fd0051ef..777537a3ec 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, - Versionable> + Versionable>, + Compressible> { } 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 b5d5f97bf4..3cf23b89dd 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 @@ public ACLPathAndBytesable compressed() public CuratorTransactionBridge forPath(String path) throws Exception { return forPath(path, client.getDefaultData()); - } + } @Override public CuratorTransactionBridge forPath(String path, byte[] data) throws Exception 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 c88ea552cb..8e93cbf84d 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 @@ 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.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 @@ TransactionSetDataBuilder asTransactionSetDataBuilder(final CuratorTransaction @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 @@ public PathAndBytesable withVersion(int version) SetDataBuilderImpl.this.withVersion(version); return this; } + + @Override + public PathAndBytesable compressed() { + compress = true; + + return this; + } }; } 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 f5adaffa46..c18af99c9f 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,10 +26,73 @@ 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 { @@ -48,10 +111,43 @@ public void testSimple() throws Exception 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 {