com.basho.riak.client
Class DefaultRiakObject

java.lang.Object
  extended by com.basho.riak.client.DefaultRiakObject
All Implemented Interfaces:
IRiakObject, Iterable<RiakLink>

public class DefaultRiakObject
extends Object
implements IRiakObject

The default implementation of IRiakObject

Is as immutable as possible. Value, content-type, links and user meta data are all mutable. It is safe to use the instances of this class from multiple threads.

Due to the large number of arguments to the constructor the *best* way to create instances is with a RiakObjectBuilder.

Author:
russell

Field Summary
static String DEFAULT_CONTENT_TYPE
          The default content type assigned when persisted in Riak if non is provided.
 
Constructor Summary
DefaultRiakObject(String bucket, String key, VClock vclock, String vtag, Date lastModified, String contentType, byte[] value, Collection<RiakLink> links, Map<String,String> userMeta, RiakIndexes indexes)
          Large number of arguments due to largely immutable nature.
 
Method Summary
 IRiakObject addIndex(String index, int value)
          Add an int index to this objects indexes.
 IRiakObject addIndex(String index, String value)
          Add a String index to this objects indexes.
 IRiakObject addLink(RiakLink link)
          Add link to this RiakObject's links.
 IRiakObject addUsermeta(String key, String value)
          Adds the key, value to the collection of user meta data for this object.
 Map<BinIndex,Set<String>> allBinIndexes()
          Secondary indexes for this object.
 Map<IntIndex,Set<Integer>> allIntIndexes()
          Secondary indexes for this object.
 Set<String> getBinIndex(String name)
          Get a copy of the values for the given bin index
 String getBucket()
          The name of this objects bucket
 String getContentType()
          The content-type of this object's value.
 Set<Integer> getIntIndex(String name)
          Get a copy of the values for the given bin index
 String getKey()
          The object's key.
 Date getLastModified()
          The last modified date as held by Riak.
 List<RiakLink> getLinks()
          A List of RiakLinks from this object.
 Map<String,String> getMeta()
          NOTE: a copy is returned.
 String getUsermeta(String key)
          Get the user meta data item for that key.
 byte[] getValue()
          The value.
 String getValueAsString()
          Convenience method.
 VClock getVClock()
          This objects Vector Clock.
 String getVClockAsString()
          String copy of this object's vector clock.
 String getVtag()
          If this object has a version tag (if it is one of a set of siblings)
 boolean hasLink(RiakLink riakLink)
          Does this object have that link?
 boolean hasLinks()
          Does this object link to any others?
 boolean hasUsermeta()
          Does this object have any user meta data?
 boolean hasUsermeta(String key)
          Does this object have a meta data item for that key?
 Iterator<RiakLink> iterator()
          an UnmodifiableIterator view on the RiakLinks
 int numLinks()
          How many RiakLinks does this object have.
 IRiakObject removeBinIndex(String index)
          Remove a BinIndex from this RiakObject.
 IRiakObject removeIntIndex(String index)
          Remove a IntIndex from this RiakObject.
 IRiakObject removeLink(RiakLink link)
          Remove a RiakLink from this RiakObject.
 IRiakObject removeUsermeta(String key)
          Remove that item of user meta data.
 void setContentType(String contentType)
          Set the content-type of this object's payload.
 void setValue(byte[] value)
          Note: Copies the value.
 void setValue(String value)
          Convenience method that will result in value being turned into a byte[] array using charset utf-8 and also will result in charset=utf-8 being appended to the content-type for this object
 Iterable<Map.Entry<String,String>> userMetaEntries()
          return an unmodifiable view of the user meta entries.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CONTENT_TYPE

public static String DEFAULT_CONTENT_TYPE
The default content type assigned when persisted in Riak if non is provided.

Constructor Detail

DefaultRiakObject

public DefaultRiakObject(String bucket,
                         String key,
                         VClock vclock,
                         String vtag,
                         Date lastModified,
                         String contentType,
                         byte[] value,
                         Collection<RiakLink> links,
                         Map<String,String> userMeta,
                         RiakIndexes indexes)
Large number of arguments due to largely immutable nature. Use RiakObjectBuilder to create instances.

Parameters:
bucket - the bucket the object is stored in
key - the key it is stored under
vclock - the vclock, if available
vtag - the version tag, if relevant
lastModified - the last modified date from Riak (if relevant)
contentType - the content-type of the value
value - a byte[] of the data payload to store in Riak. Note: this is cloned on construction of this instance.
links - the List of RiakLinks from this object. Note: this is copied.
userMeta - the Map of user meta data to store/stored with this object. Note: this is copied.
indexes - the RiakIndexes for this object. These will be copied to a new RiakIndexes
Method Detail

getBucket

public String getBucket()
Description copied from interface: IRiakObject
The name of this objects bucket

Specified by:
getBucket in interface IRiakObject
Returns:
the bucket name.

getVClock

public VClock getVClock()
Description copied from interface: IRiakObject
This objects Vector Clock. See the basho wiki for more on vector clocks

Specified by:
getVClock in interface IRiakObject
Returns:
the VClock for this object.

getKey

public String getKey()
Description copied from interface: IRiakObject
The object's key.

Specified by:
getKey in interface IRiakObject
Returns:
The objects key.

getVtag

public String getVtag()
Description copied from interface: IRiakObject
If this object has a version tag (if it is one of a set of siblings)

Specified by:
getVtag in interface IRiakObject
Returns:
the vtag, if present.

getLastModified

public Date getLastModified()
Description copied from interface: IRiakObject
The last modified date as held by Riak.

Specified by:
getLastModified in interface IRiakObject
Returns:
the last modified date as returned from Riak.

getContentType

public String getContentType()
Description copied from interface: IRiakObject
The content-type of this object's value.

Specified by:
getContentType in interface IRiakObject
Returns:
the Objects Content-Type. If you don't set this it defaults to DEFAULT_CONTENT_TYPE

getMeta

public Map<String,String> getMeta()
NOTE: a copy is returned. Mutating the return value will not effect the state of this instance.

Specified by:
getMeta in interface IRiakObject
Returns:
the Map of meta data for this object.
See Also:
IRiakObject.getMeta()

getValue

public byte[] getValue()
Description copied from interface: IRiakObject
The value.

Specified by:
getValue in interface IRiakObject
Returns:
a *cop* of this object's data payload.
See Also:
IRiakObject.getValue()

setValue

public void setValue(byte[] value)
Note: Copies the value.

Specified by:
setValue in interface IRiakObject
Parameters:
a - byte[] to store in Riak.

setValue

public void setValue(String value)
Description copied from interface: IRiakObject
Convenience method that will result in value being turned into a byte[] array using charset utf-8 and also will result in charset=utf-8 being appended to the content-type for this object

Specified by:
setValue in interface IRiakObject
Parameters:
value - the String value

setContentType

public void setContentType(String contentType)
Description copied from interface: IRiakObject
Set the content-type of this object's payload.

Specified by:
setContentType in interface IRiakObject
Parameters:
contentType - the content-type of this object's value (EG text/plain;charset=utf-8)

iterator

public Iterator<RiakLink> iterator()
an UnmodifiableIterator view on the RiakLinks

Specified by:
iterator in interface Iterable<RiakLink>

addLink

public IRiakObject addLink(RiakLink link)
Description copied from interface: IRiakObject
Add link to this RiakObject's links.

Specified by:
addLink in interface IRiakObject
Parameters:
link - a RiakLink to add.
Returns:
this RiakObject.

removeLink

public IRiakObject removeLink(RiakLink link)
Description copied from interface: IRiakObject
Remove a RiakLink from this RiakObject.

Specified by:
removeLink in interface IRiakObject
Parameters:
link - the RiakLink to remove
Returns:
this RiakObject

hasLinks

public boolean hasLinks()
Description copied from interface: IRiakObject
Does this object link to any others?

Specified by:
hasLinks in interface IRiakObject
Returns:
true if this object has any links, false otherwise.

numLinks

public int numLinks()
Description copied from interface: IRiakObject
How many RiakLinks does this object have.

Specified by:
numLinks in interface IRiakObject
Returns:
the number of links from this object.

getLinks

public List<RiakLink> getLinks()
Description copied from interface: IRiakObject
A List of RiakLinks from this object. See also Link Walking on the basho site.

Specified by:
getLinks in interface IRiakObject
Returns:
The List of RiakLinks from this object
See Also:
RiakLink, LinkWalk

hasLink

public boolean hasLink(RiakLink riakLink)
Description copied from interface: IRiakObject
Does this object have that link?

Specified by:
hasLink in interface IRiakObject
Parameters:
riakLink - a RiakLink
Returns:
true if this object's link collection contains the passed RiakLink, false otherwise.

addUsermeta

public IRiakObject addUsermeta(String key,
                               String value)
Description copied from interface: IRiakObject
Adds the key, value to the collection of user meta data for this object.

Specified by:
addUsermeta in interface IRiakObject
Returns:
this RiakObject.

hasUsermeta

public boolean hasUsermeta()
Description copied from interface: IRiakObject
Does this object have any user meta data?

Specified by:
hasUsermeta in interface IRiakObject
Returns:
if this IRiakObject has any user meta data items.

hasUsermeta

public boolean hasUsermeta(String key)
Description copied from interface: IRiakObject
Does this object have a meta data item for that key?

Specified by:
hasUsermeta in interface IRiakObject
Returns:
true if this IRiakObject's user meta data contains the supplied key

getUsermeta

public String getUsermeta(String key)
Description copied from interface: IRiakObject
Get the user meta data item for that key.

Specified by:
getUsermeta in interface IRiakObject
Parameters:
key - the name of the user meta data item
Returns:
a String of the user meta data item or null if no item present for the supplied key

removeUsermeta

public IRiakObject removeUsermeta(String key)
Description copied from interface: IRiakObject
Remove that item of user meta data.

Specified by:
removeUsermeta in interface IRiakObject
Parameters:
key - the key of the item to remove

userMetaEntries

public Iterable<Map.Entry<String,String>> userMetaEntries()
return an unmodifiable view of the user meta entries. Attempts to modify will throw UnsupportedOperationException.

Specified by:
userMetaEntries in interface IRiakObject
Returns:
an iterable view of the set of user meta data.
See Also:
Map.Entry

getVClockAsString

public String getVClockAsString()
Description copied from interface: IRiakObject
String copy of this object's vector clock.

Specified by:
getVClockAsString in interface IRiakObject
Returns:
A String of this objects Vector Clock

getValueAsString

public String getValueAsString()
Description copied from interface: IRiakObject
Convenience method. Will use the content-type to figure out the charset.

Specified by:
getValueAsString in interface IRiakObject
Returns:
the byte[] coerced to a String using the object's content-type

allBinIndexes

public Map<BinIndex,Set<String>> allBinIndexes()
Description copied from interface: IRiakObject
Secondary indexes for this object. See basho wiki for more details. TODO update doc link when 2i hits the wiki

Specified by:
allBinIndexes in interface IRiakObject
Returns:
a copy of the string indexes for this object.

getBinIndex

public Set<String> getBinIndex(String name)
Description copied from interface: IRiakObject
Get a copy of the values for the given bin index

Specified by:
getBinIndex in interface IRiakObject
Parameters:
name - the index name
Returns:
a Set of value

allIntIndexes

public Map<IntIndex,Set<Integer>> allIntIndexes()
Description copied from interface: IRiakObject
Secondary indexes for this object. See basho wiki for more details. TODO update doc link when 2i hits the wiki

Specified by:
allIntIndexes in interface IRiakObject
Returns:
a copy of the int indexes for this object

getIntIndex

public Set<Integer> getIntIndex(String name)
Description copied from interface: IRiakObject
Get a copy of the values for the given bin index

Specified by:
getIntIndex in interface IRiakObject
Parameters:
name - the index name
Returns:
a Set of value

addIndex

public IRiakObject addIndex(String index,
                            String value)
Description copied from interface: IRiakObject
Add a String index to this objects indexes.

Specified by:
addIndex in interface IRiakObject
Parameters:
index - index name
value - index value
Returns:
this RiakObject.

addIndex

public IRiakObject addIndex(String index,
                            int value)
Description copied from interface: IRiakObject
Add an int index to this objects indexes.

Specified by:
addIndex in interface IRiakObject
Parameters:
index - index name
value - index value
Returns:
this RiakObject.

removeBinIndex

public IRiakObject removeBinIndex(String index)
Description copied from interface: IRiakObject
Remove a BinIndex from this RiakObject.

Specified by:
removeBinIndex in interface IRiakObject
Parameters:
index - the name of the bin index to remove
Returns:
this RiakObject

removeIntIndex

public IRiakObject removeIntIndex(String index)
Description copied from interface: IRiakObject
Remove a IntIndex from this RiakObject.

Specified by:
removeIntIndex in interface IRiakObject
Parameters:
index - the name of the int index to remove
Returns:
this RiakObject


Copyright © 2012. All Rights Reserved.