Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make RiakObject (and all it's stuff) to be Serializable #698

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/com/basho/riak/client/api/cap/VClock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <russelldb at basho dot com>
* @since 1.0
*/
public interface VClock
public interface VClock extends Serializable
{
/**
* Get the bytes that make up this VClock.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -49,7 +50,7 @@
* @author Brian Roach <roach at basho dot com>
* @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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<BinaryValue, BinaryValue> meta = new ConcurrentHashMap<>();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/
public class BigIntIndex extends RiakIndex<BigInteger>
{
private static final long serialVersionUID = -1815784710534656508L;
private BigIntIndex(Name name)
{
super(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*/
public class LongIntIndex extends RiakIndex<Long>
{
private static final long serialVersionUID = 6311824439922246390L;
private LongIntIndex(Name name)
{
super(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/
public class RawIndex extends RiakIndex<BinaryValue>
{
private static final long serialVersionUID = -9062911855629713886L;
private RawIndex(Name name)
{
super(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,7 +52,7 @@
* href="http://docs.basho.com/riak/latest/dev/using/2i/">Using Secondary
* Indexes in Riak</a>
*/
public abstract class RiakIndex<T> implements Iterable<T>
public abstract class RiakIndex<T> implements Iterable<T>, Serializable
{
private final Set<BinaryValue> values;
private final IndexType type;
Expand Down Expand Up @@ -443,4 +445,16 @@ final Name<T> copyFrom(RiakIndex<?> otherIndex)

abstract T createIndex();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this section?

// 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
// {
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -92,8 +93,9 @@
* @since 2.0
* @see RiakObject#getIndexes()
*/
public class RiakIndexes implements Iterable<RiakIndex<?>>
public class RiakIndexes implements Iterable<RiakIndex<?>>, Serializable
{
private static final long serialVersionUID = -2931049191878682591L;
private final ConcurrentHashMap<String, RiakIndex<?>> indexes = new ConcurrentHashMap<>();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
*/
public class StringBinIndex extends RiakIndex<String>
{
private final Charset charset;
private static final long serialVersionUID = -5151153157935896563L;
private transient Charset charset;

private StringBinIndex(Name name)
{
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -36,8 +37,9 @@
* @author Brian Roach <roach at basho dot com>
* @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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.basho.riak.client.core.query.links;

import java.io.Serializable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -27,8 +28,9 @@
* @see RiakLink
* @since 2.0
*/
public class RiakLinks implements Iterable<RiakLink>
public class RiakLinks implements Iterable<RiakLink>, Serializable
{
private static final long serialVersionUID = 8826161124877321843L;
private final Set<RiakLink> links = Collections.newSetFromMap(new ConcurrentHashMap<RiakLink, Boolean>());

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -39,12 +43,13 @@
* @author Brian Roach <roach at basho dot com>
* @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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
32 changes: 27 additions & 5 deletions src/test/java/com/basho/riak/client/core/query/RiakObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");
Expand Down