From 37e8dc96d78644fef714e997fae699aaffe11beb Mon Sep 17 00:00:00 2001 From: Sergey Galkin Date: Fri, 20 Jan 2017 15:08:12 +0200 Subject: [PATCH] Make RiakObject (and all it's stuff) to be Serializable to use it from Spark --- .../com/basho/riak/client/api/cap/VClock.java | 4 ++- .../riak/client/core/query/RiakObject.java | 4 ++- .../query/UserMetadata/RiakUserMetadata.java | 4 ++- .../core/query/indexes/BigIntIndex.java | 1 + .../core/query/indexes/LongIntIndex.java | 1 + .../client/core/query/indexes/RawIndex.java | 1 + .../client/core/query/indexes/RiakIndex.java | 16 +++++++++- .../core/query/indexes/RiakIndexes.java | 4 ++- .../core/query/indexes/StringBinIndex.java | 18 ++++++++++- .../client/core/query/links/RiakLink.java | 4 ++- .../client/core/query/links/RiakLinks.java | 4 ++- .../riak/client/core/util/BinaryValue.java | 7 +++- .../client/api/commands/StoreValueTest.java | 10 ++---- .../client/core/query/RiakObjectTest.java | 32 ++++++++++++++++--- 14 files changed, 89 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/basho/riak/client/api/cap/VClock.java b/src/main/java/com/basho/riak/client/api/cap/VClock.java index f2233bb9b..526618e7b 100644 --- a/src/main/java/com/basho/riak/client/api/cap/VClock.java +++ b/src/main/java/com/basho/riak/client/api/cap/VClock.java @@ -15,13 +15,15 @@ */ package com.basho.riak.client.api.cap; +import java.io.Serializable; + /** * Access the opaque Riak vector clock as either a String or array of bytes. * * @author Russel Brown * @since 1.0 */ -public interface VClock +public interface VClock extends Serializable { /** * Get the bytes that make up this VClock. diff --git a/src/main/java/com/basho/riak/client/core/query/RiakObject.java b/src/main/java/com/basho/riak/client/core/query/RiakObject.java index 33820dde8..d026c7ce0 100644 --- a/src/main/java/com/basho/riak/client/core/query/RiakObject.java +++ b/src/main/java/com/basho/riak/client/core/query/RiakObject.java @@ -23,6 +23,7 @@ import com.basho.riak.client.core.util.BinaryValue; import com.basho.riak.client.core.util.CharsetUtils; +import java.io.Serializable; import java.nio.charset.Charset; /** @@ -49,7 +50,7 @@ * @author Brian Roach * @since 2.0 */ -public final class RiakObject +public final class RiakObject implements Serializable { /** * The default content type assigned when storing in Riak if one is not @@ -58,6 +59,7 @@ public final class RiakObject * @see RiakObject#setContentType(java.lang.String) */ public final static String DEFAULT_CONTENT_TYPE = "application/octet-stream"; + private static final long serialVersionUID = 484390882043340231L; // Mutable types. // Worth noting here is that changes to the contents of this diff --git a/src/main/java/com/basho/riak/client/core/query/UserMetadata/RiakUserMetadata.java b/src/main/java/com/basho/riak/client/core/query/UserMetadata/RiakUserMetadata.java index ece2ddb03..f196c763a 100644 --- a/src/main/java/com/basho/riak/client/core/query/UserMetadata/RiakUserMetadata.java +++ b/src/main/java/com/basho/riak/client/core/query/UserMetadata/RiakUserMetadata.java @@ -18,6 +18,7 @@ import com.basho.riak.client.core.util.BinaryValue; import com.basho.riak.client.core.util.DefaultCharset; +import java.io.Serializable; import java.nio.charset.Charset; import java.util.Collections; import java.util.Map; @@ -41,8 +42,9 @@ * @see com.basho.riak.client.core.query.RiakObject#getUserMeta() * @since 2.0 */ -public class RiakUserMetadata +public class RiakUserMetadata implements Serializable { + private static final long serialVersionUID = 9001811266201347973L; private final ConcurrentHashMap meta = new ConcurrentHashMap<>(); /** diff --git a/src/main/java/com/basho/riak/client/core/query/indexes/BigIntIndex.java b/src/main/java/com/basho/riak/client/core/query/indexes/BigIntIndex.java index 5e25462ce..4b3e2e412 100644 --- a/src/main/java/com/basho/riak/client/core/query/indexes/BigIntIndex.java +++ b/src/main/java/com/basho/riak/client/core/query/indexes/BigIntIndex.java @@ -26,6 +26,7 @@ */ public class BigIntIndex extends RiakIndex { + private static final long serialVersionUID = -1815784710534656508L; private BigIntIndex(Name name) { super(name); diff --git a/src/main/java/com/basho/riak/client/core/query/indexes/LongIntIndex.java b/src/main/java/com/basho/riak/client/core/query/indexes/LongIntIndex.java index aa095a3e9..07d87fac8 100644 --- a/src/main/java/com/basho/riak/client/core/query/indexes/LongIntIndex.java +++ b/src/main/java/com/basho/riak/client/core/query/indexes/LongIntIndex.java @@ -34,6 +34,7 @@ */ public class LongIntIndex extends RiakIndex { + private static final long serialVersionUID = 6311824439922246390L; private LongIntIndex(Name name) { super(name); diff --git a/src/main/java/com/basho/riak/client/core/query/indexes/RawIndex.java b/src/main/java/com/basho/riak/client/core/query/indexes/RawIndex.java index b7609bfbb..4a75e7723 100644 --- a/src/main/java/com/basho/riak/client/core/query/indexes/RawIndex.java +++ b/src/main/java/com/basho/riak/client/core/query/indexes/RawIndex.java @@ -30,6 +30,7 @@ */ public class RawIndex extends RiakIndex { + private static final long serialVersionUID = -9062911855629713886L; private RawIndex(Name name) { super(name); diff --git a/src/main/java/com/basho/riak/client/core/query/indexes/RiakIndex.java b/src/main/java/com/basho/riak/client/core/query/indexes/RiakIndex.java index e0f454bc1..593d3a67d 100644 --- a/src/main/java/com/basho/riak/client/core/query/indexes/RiakIndex.java +++ b/src/main/java/com/basho/riak/client/core/query/indexes/RiakIndex.java @@ -16,6 +16,8 @@ package com.basho.riak.client.core.query.indexes; import com.basho.riak.client.core.util.BinaryValue; + +import java.io.Serializable; import java.util.Collection; import java.util.Collections; import java.util.ConcurrentModificationException; @@ -50,7 +52,7 @@ * href="http://docs.basho.com/riak/latest/dev/using/2i/">Using Secondary * Indexes in Riak */ -public abstract class RiakIndex implements Iterable +public abstract class RiakIndex implements Iterable, Serializable { private final Set values; private final IndexType type; @@ -443,4 +445,16 @@ final Name copyFrom(RiakIndex otherIndex) abstract T createIndex(); } + +// protected final void doWriteObkject(java.io.ObjectOutputStream stream) throws java.io.IOException +// { +// stream.writeUTF(name); +// stream.writeObject(type); +// stream.writeObject(values); +// } +// +// protected final void doReadObject(java.io.ObjectInputStream stream) +// throws java.io.IOException, ClassNotFoundException +// { +// } } diff --git a/src/main/java/com/basho/riak/client/core/query/indexes/RiakIndexes.java b/src/main/java/com/basho/riak/client/core/query/indexes/RiakIndexes.java index c38515a8b..ccb93bd29 100644 --- a/src/main/java/com/basho/riak/client/core/query/indexes/RiakIndexes.java +++ b/src/main/java/com/basho/riak/client/core/query/indexes/RiakIndexes.java @@ -17,6 +17,7 @@ import com.basho.riak.client.core.query.RiakObject; +import java.io.Serializable; import java.util.Iterator; import java.util.List; import java.util.concurrent.ConcurrentHashMap; @@ -92,8 +93,9 @@ * @since 2.0 * @see RiakObject#getIndexes() */ -public class RiakIndexes implements Iterable> +public class RiakIndexes implements Iterable>, Serializable { + private static final long serialVersionUID = -2931049191878682591L; private final ConcurrentHashMap> indexes = new ConcurrentHashMap<>(); /** diff --git a/src/main/java/com/basho/riak/client/core/query/indexes/StringBinIndex.java b/src/main/java/com/basho/riak/client/core/query/indexes/StringBinIndex.java index e5017b1ce..ba6ab8b89 100644 --- a/src/main/java/com/basho/riak/client/core/query/indexes/StringBinIndex.java +++ b/src/main/java/com/basho/riak/client/core/query/indexes/StringBinIndex.java @@ -46,7 +46,8 @@ */ public class StringBinIndex extends RiakIndex { - private final Charset charset; + private static final long serialVersionUID = -5151153157935896563L; + private transient Charset charset; private StringBinIndex(Name name) { @@ -110,4 +111,19 @@ StringBinIndex createIndex() return new StringBinIndex(this); } } + + private void writeObject(java.io.ObjectOutputStream stream) + throws java.io.IOException + { + stream.defaultWriteObject(); + stream.writeUTF(charset.name()); + } + + private void readObject(java.io.ObjectInputStream stream) + throws java.io.IOException, ClassNotFoundException + { + stream.defaultReadObject(); + final String charsetName = stream.readUTF(); + charset = Charset.forName(charsetName); + } } diff --git a/src/main/java/com/basho/riak/client/core/query/links/RiakLink.java b/src/main/java/com/basho/riak/client/core/query/links/RiakLink.java index ff859a4f4..9d6402458 100644 --- a/src/main/java/com/basho/riak/client/core/query/links/RiakLink.java +++ b/src/main/java/com/basho/riak/client/core/query/links/RiakLink.java @@ -18,6 +18,7 @@ import com.basho.riak.client.core.util.BinaryValue; import com.basho.riak.client.core.util.DefaultCharset; +import java.io.Serializable; import java.nio.charset.Charset; /** @@ -36,8 +37,9 @@ * @author Brian Roach * @since 1.0 */ -public class RiakLink +public class RiakLink implements Serializable { + private static final long serialVersionUID = -4016542401563611460L; private final BinaryValue bucket; private final BinaryValue key; private final BinaryValue tag; diff --git a/src/main/java/com/basho/riak/client/core/query/links/RiakLinks.java b/src/main/java/com/basho/riak/client/core/query/links/RiakLinks.java index e039234a6..9348cae39 100644 --- a/src/main/java/com/basho/riak/client/core/query/links/RiakLinks.java +++ b/src/main/java/com/basho/riak/client/core/query/links/RiakLinks.java @@ -15,6 +15,7 @@ */ package com.basho.riak.client.core.query.links; +import java.io.Serializable; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -27,8 +28,9 @@ * @see RiakLink * @since 2.0 */ -public class RiakLinks implements Iterable +public class RiakLinks implements Iterable, Serializable { + private static final long serialVersionUID = 8826161124877321843L; private final Set links = Collections.newSetFromMap(new ConcurrentHashMap()); /** diff --git a/src/main/java/com/basho/riak/client/core/util/BinaryValue.java b/src/main/java/com/basho/riak/client/core/util/BinaryValue.java index 549eed556..46ea91749 100644 --- a/src/main/java/com/basho/riak/client/core/util/BinaryValue.java +++ b/src/main/java/com/basho/riak/client/core/util/BinaryValue.java @@ -15,6 +15,10 @@ */ package com.basho.riak.client.core.util; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; import java.nio.charset.Charset; import java.util.Arrays; @@ -39,12 +43,13 @@ * @author Brian Roach * @since 2.0 */ -public final class BinaryValue +public final class BinaryValue implements Serializable { /** * It is expected that UTF-8 charset is available. */ private static final Charset theUTF8 = Charset.forName("UTF-8"); + private static final long serialVersionUID = 3976425010879879957L; private final byte[] data; diff --git a/src/test/java/com/basho/riak/client/api/commands/StoreValueTest.java b/src/test/java/com/basho/riak/client/api/commands/StoreValueTest.java index 8548cc450..0f14e606e 100644 --- a/src/test/java/com/basho/riak/client/api/commands/StoreValueTest.java +++ b/src/test/java/com/basho/riak/client/api/commands/StoreValueTest.java @@ -29,11 +29,7 @@ import com.basho.riak.client.core.query.Namespace; import com.basho.riak.client.core.query.RiakObject; import com.basho.riak.client.core.query.RiakObjectTest; -import com.basho.riak.client.core.query.indexes.StringBinIndex; -import com.basho.riak.client.core.query.links.RiakLink; -import com.basho.riak.client.core.util.BinaryValue; import com.basho.riak.protobuf.RiakKvPB; -import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.mockito.ArgumentCaptor; @@ -79,7 +75,7 @@ public void init() throws Exception when(mockFuture.isDone()).thenReturn(true); when(mockCluster.execute(any(FutureOperation.class))).thenReturn(mockFuture); client = new RiakClient(mockCluster); - riakObject = RiakObjectTest.CreateFilledObject(); + riakObject = RiakObjectTest.createFilledObject(); } @Test @@ -114,8 +110,8 @@ public void testStore() throws ExecutionException, InterruptedException @Test public void testEqualsWithRiakObject() { - final RiakObject riakObject1 = RiakObjectTest.CreateFilledObject(); - final RiakObject riakObject2 = RiakObjectTest.CreateFilledObject(); + final RiakObject riakObject1 = RiakObjectTest.createFilledObject(); + final RiakObject riakObject2 = RiakObjectTest.createFilledObject(); final StoreValue value1 = filledStoreValue(riakObject1); final StoreValue value2 = filledStoreValue(riakObject2); diff --git a/src/test/java/com/basho/riak/client/core/query/RiakObjectTest.java b/src/test/java/com/basho/riak/client/core/query/RiakObjectTest.java index b7ee3ea0b..af07c9658 100644 --- a/src/test/java/com/basho/riak/client/core/query/RiakObjectTest.java +++ b/src/test/java/com/basho/riak/client/core/query/RiakObjectTest.java @@ -2,12 +2,14 @@ import com.basho.riak.client.api.cap.BasicVClock; import com.basho.riak.client.api.cap.VClock; -import com.basho.riak.client.api.commands.kv.StoreValue; -import com.basho.riak.client.core.query.indexes.StringBinIndex; +import com.basho.riak.client.core.query.indexes.*; import com.basho.riak.client.core.query.links.RiakLink; import com.basho.riak.client.core.util.BinaryValue; import org.junit.Test; +import java.io.*; +import java.math.BigInteger; + import static org.junit.Assert.assertEquals; public class RiakObjectTest @@ -17,17 +19,37 @@ public class RiakObjectTest @Test public void testEqualsWithRiakObject() { - final RiakObject riakObject1 = CreateFilledObject(); - final RiakObject riakObject2 = CreateFilledObject(); + final RiakObject riakObject1 = createFilledObject(); + final RiakObject riakObject2 = createFilledObject(); assertEquals(riakObject1, riakObject2); } - public static RiakObject CreateFilledObject() + @Test + public void checkSerialization() throws IOException, ClassNotFoundException { + final RiakObject ro = createFilledObject(); + + final ByteArrayOutputStream bos = new ByteArrayOutputStream(); + final ObjectOutputStream out = new ObjectOutputStream(bos); + + out.writeObject(ro); + out.close(); + + final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); + final ObjectInputStream in = new ObjectInputStream(bis); + + final RiakObject ro2 = (RiakObject) in.readObject(); + assertEquals(ro, ro2); + } + + public static RiakObject createFilledObject() { final RiakObject result = new RiakObject(); result.setValue(BinaryValue.create(new byte[] {'O', '_', 'o'})); result.getIndexes().getIndex(StringBinIndex.named("foo")).add("bar"); + result.getIndexes().getIndex(LongIntIndex.named("foo-long")).add(2l); + result.getIndexes().getIndex(BigIntIndex.named("foo-bint")).add(BigInteger.ONE); + result.getIndexes().getIndex(RawIndex.named("foo-raw", IndexType.BUCKET)).add(BinaryValue.create("binary-value")); result.getLinks().addLink(new RiakLink("bucket", "linkkey", "linktag")); result.getUserMeta().put("foo", "bar"); result.setVTag("vtag");