Package com.apple.foundationdb.tuple
Class Tuple
java.lang.Object
com.apple.foundationdb.tuple.Tuple
- All Implemented Interfaces:
Comparable<Tuple>,Iterable<Object>
Represents a set of elements that make up a sortable, typed key. This object
is comparable with other
The FoundationDB tuple specification has a special type-code for
This class is not thread safe.
Tuples and will sort in Java in
the same order in which they would sort in FoundationDB. Tuples sort
first by the first element, then by the second, etc. This makes the tuple layer
ideal for building a variety of higher-level data models.Types
ATuple can
contain byte arrays (byte[]), Strings, Numbers, UUIDs,
booleans, Lists, Versionstamps, other Tuples, and null.
Float and Double instances will be serialized as single- and double-precision
numbers respectively, and BigIntegers within the range [-2^2040+1,
2^2040-1] are serialized without loss of precision (those outside the range
will raise an IllegalArgumentException). All other Numbers will be converted to
a long integral value, so the range will be constrained to
[-2^63, 2^63-1]. Note that for numbers outside this range the way that Java
truncates integral values may yield unexpected results.null values
The FoundationDB tuple specification has a special type-code for None; nil; or,
as Java would understand it, null.
The behavior of the layer in the presence of null varies by type with the intention
of matching expected behavior in Java. byte[], Strings, UUIDs, and
nested Lists and Tuples can be null,
whereas numbers (e.g., longs and doubles) and booleans cannot.
This means that the typed getters (getBytes(), getString()),
getUUID(), getNestedTuple(), getVersionstamp,
and getNestedList()) will return null if the entry at that location was
null and the typed adds (add(byte[]), add(String), add(Versionstamp)
add(Tuple), and add(List)) will accept null. The
typed get for integers and other typed getters, however, will throw a
NullPointerException if the entry in the Tuple was null at that position.This class is not thread safe.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(boolean b) Creates a copy of thisTuplewith abooleanappended as the last element.add(byte[] b) Creates a copy of thisTuplewith abytearray appended as the last element.add(byte[] b, int offset, int length) Creates a copy of thisTuplewith abytearray appended as the last element.add(double d) Creates a copy of thisTuplewith adoubleappended as the last element.add(float f) Creates a copy of thisTuplewith afloatappended as the last element.add(long l) Creates a copy of thisTuplewith alongappended as the last element.Creates a copy of thisTuplewith aTupleappended as the last element.add(Versionstamp v) Creates a copy of thisTuplewith aVersionstampobject appended as the last element.Creates a copy of thisTuplewith aStringappended as the last element.add(BigInteger bi) Creates a copy of thisTuplewith aBigIntegerappended as the last element.Creates a copy of thisTuplewith aListappended as the last element.Creates a copy of thisTuplewith aUUIDappended as the last element.Create a copy of thisTuplewith all elements from antherTupleappended.Create a copy of thisTuplewith a list of items appended.Creates a copy of thisTuplewith an appended last element.intCompare the byte-array representation of thisTupleagainst another.booleanTests for equality with anotherTuple.static TupleCreates a newTuplefrom a variable number of elements.static TuplefromBytes(byte[] bytes) Construct a newTuplewith elements decoded from a suppliedbytearray.static TuplefromBytes(byte[] bytes, int offset, int length) Construct a newTuplewith elements decoded from a suppliedbytearray.static TupleCreates a newTuplefrom a variable number of elements.static TupleEfficiently creates a newTuplefrom a list of objects.static TuplefromStream(Stream<?> items) Efficiently creates a newTuplefrom aStreamof objects.get(int index) Gets an indexed item without forcing a type.getBigInteger(int index) Gets an indexed item as aBigInteger.booleangetBoolean(int index) Gets an indexed item as aboolean.byte[]getBytes(int index) Gets an indexed item as abyte[].doublegetDouble(int index) Gets an indexed item as adouble.floatgetFloat(int index) Gets an indexed item as afloat.getItems()Gets the unserialized contents of thisTuple.longgetLong(int index) Gets an indexed item as along.getNestedList(int index) Gets an indexed item as aList.getNestedTuple(int index) Gets an indexed item as aTuple.intGet the number of bytes in the packed representation of thisTuple.getString(int index) Gets an indexed item as aString.getUUID(int index) Gets an indexed item as aUUID.getVersionstamp(int index) Gets an indexed item as aVersionstamp.inthashCode()Returns a hash code value for thisTuple.booleanDetermines if there is aVersionstampincluded in thisTuplethat has not had its transaction version set.booleanisEmpty()Determine if thisTuplecontains no elements.iterator()Gets anIteratorover theObjectsin thisTuple.byte[]pack()Get an encoded representation of thisTuple.byte[]pack(byte[] prefix) Get an encoded representation of thisTuple.voidpackInto(ByteBuffer dest) Pack an encoded representation of thisTupleonto the end of the givenByteBuffer.byte[]Get an encoded representation of thisTuplefor use withMutationType.SET_VERSIONSTAMPED_KEY.byte[]packWithVersionstamp(byte[] prefix) Get an encoded representation of thisTuplefor use withMutationType.SET_VERSIONSTAMPED_KEY.popBack()Creates a newTuplewith the last item of thisTupleremoved.popFront()Creates a newTuplewith the first item of thisTupleremoved.range()Returns a range representing all keys that encodeTuples strictly starting with thisTuple.range(byte[] prefix) Returns a range representing all keys that encodeTuples strictly starting with the given prefix followed by thisTuple.intsize()Gets the number of elements in thisTuple.stream()Gets aStreamof the unserialized contents of thisTuple.toString()Returns a string representing thisTuple.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
Tuple
public Tuple()Construct a new emptyTuple. After creation, items can be added with calls to the variations ofadd().
-
-
Method Details
-
addObject
-
add
Creates a copy of thisTuplewith aStringappended as the last element.- Parameters:
s- theStringto append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuplewith alongappended as the last element.- Parameters:
l- the number to append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuplewith abytearray appended as the last element.- Parameters:
b- thebytes to append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuplewith abooleanappended as the last element.- Parameters:
b- thebooleanto append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuplewith aUUIDappended as the last element.- Parameters:
uuid- theUUIDto append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuplewith aBigIntegerappended as the last element. AsTuples cannot containnullnumeric types, aNullPointerExceptionis raised if anullargument is passed.- Parameters:
bi- theBigIntegerto append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuplewith afloatappended as the last element.- Parameters:
f- thefloatto append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuplewith adoubleappended as the last element.- Parameters:
d- thedoubleto append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuplewith aVersionstampobject appended as the last element.- Parameters:
v- theVersionstampto append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuplewith aListappended as the last element. This does not add the elements individually (for that, useTuple.addAll). This adds the list as a single element nested within the outerTuple.- Parameters:
l- theListto append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuplewith aTupleappended as the last element. This does not add the elements individually (for that, useTuple.addAll). This adds the list as a single element nested within the outerTuple.- Parameters:
t- theTupleto append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuplewith abytearray appended as the last element.- Parameters:
b- thebytes to appendoffset- the starting index ofbto addlength- the number of elements ofbto copy into thisTuple- Returns:
- a newly created
Tuple
-
addAll
Create a copy of thisTuplewith a list of items appended. -
addAll
Create a copy of thisTuplewith all elements from antherTupleappended.- Parameters:
other- theTuplewhose elements should be appended- Returns:
- a newly created
Tuple
-
pack
public byte[] pack()Get an encoded representation of thisTuple. Each element is encoded tobytes and concatenated. Note that once aTuplehas been packed, its serialized representation is stored internally so that future calls to this function are faster than the initial call.- Returns:
- a packed representation of this
Tuple
-
pack
public byte[] pack(byte[] prefix) Get an encoded representation of thisTuple. Each element is encoded tobytes and concatenated, and then the prefix supplied is prepended to the array. Note that once aTuplehas been packed, its serialized representation is stored internally so that future calls to this function are faster than the initial call.- Parameters:
prefix- additional byte-array prefix to prepend to the packed bytes- Returns:
- a packed representation of this
Tupleprepended by theprefix
-
packInto
Pack an encoded representation of thisTupleonto the end of the givenByteBuffer. It is up to the caller to ensure that there is enough space allocated within the buffer to avoidBufferOverflowExceptions. The client may callgetPackedSize()to determine how large thisTuplewill be once packed in order to allocate sufficient memory. Note that unlikepack(), the serialized representation of thisTupleis not stored, so calling this function multiple times with the sameTuplerequires serializing theTuplemultiple times.
This method will throw an error if there are any incompleteVersionstamps in thisTuple.- Parameters:
dest- the destinationByteBufferfor the encodedTuple
-
packWithVersionstamp
public byte[] packWithVersionstamp()Get an encoded representation of thisTuplefor use withMutationType.SET_VERSIONSTAMPED_KEY. This works the same as theone-paramter version of this method, but it does not add any prefix to the array.- Returns:
- a packed representation of this
Tuplefor use with versionstamp ops. - Throws:
IllegalArgumentException- if there is not exactly one incompleteVersionstampincluded in thisTuple
-
packWithVersionstamp
public byte[] packWithVersionstamp(byte[] prefix) Get an encoded representation of thisTuplefor use withMutationType.SET_VERSIONSTAMPED_KEY. There must be exactly one incompleteVersionstampinstance within thisTupleor this will throw anIllegalArgumentException. Each element is encoded tobytes and concatenated, the prefix is then prepended to the array, and then the index of the packed incompleteVersionstampis appended as a little-endian integer. This can then be passed as the key toTransaction.mutate()with theSET_VERSIONSTAMPED_KEYMutationType, and the transaction's version will then be filled in at commit time.
Note that once aTuplehas been packed, its serialized representation is stored internally so that future calls to this function are faster than the initial call.- Parameters:
prefix- additional byte-array prefix to prepend to packed bytes.- Returns:
- a packed representation of this
Tuplefor use with versionstamp ops. - Throws:
IllegalArgumentException- if there is not exactly one incompleteVersionstampincluded in thisTuple
-
getItems
Gets the unserialized contents of thisTuple.- Returns:
- the elements that make up this
Tuple.
-
stream
Gets aStreamof the unserialized contents of thisTuple.- Returns:
- a
Streamof the elements that make up thisTuple.
-
iterator
Gets anIteratorover theObjectsin thisTuple. ThisIteratoris unmodifiable and will throw an exception ifremove()is called. -
fromBytes
Construct a newTuplewith elements decoded from a suppliedbytearray. The passed byte array must not benull. This will throw an exception if the passed byte array does not represent a validTuple. For example, this will throw an error if it encounters an unknown type code or if there is a packed element that appears to be truncated.- Parameters:
bytes- encodedTuplesource- Returns:
- a new
Tupleconstructed by deserializing the providedbytearray - Throws:
IllegalArgumentException- ifbytesdoes not represent a validTuple
-
fromBytes
Construct a newTuplewith elements decoded from a suppliedbytearray. The passed byte array must not benull. This will throw an exception if the specified slice of the passed byte array does not represent a validTuple. For example, this will throw an error if it encounters an unknown type code or if there is a packed element that appears to be truncated.- Parameters:
bytes- encodedTuplesourceoffset- starting offset of byte array of encoded datalength- length of encoded data within the source- Returns:
- a new
Tupleconstructed by deserializing the specified slice of the providedbytearray - Throws:
IllegalArgumentException- ifoffsetorlengthare negative or would exceed the size of the array or ifbytesdoes not represent a validTuple
-
size
public int size()Gets the number of elements in thisTuple.- Returns:
- the number of elements in this
Tuple
-
isEmpty
public boolean isEmpty()Determine if thisTuplecontains no elements.- Returns:
trueif thisTuplecontains no elements,falseotherwise
-
getLong
public long getLong(int index) Gets an indexed item as along. This function will not do type conversion and so will throw aClassCastExceptionif the element is not a number type. The element at the index may not benull.- Parameters:
index- the location of the item to return- Returns:
- the item at
indexas along - Throws:
ClassCastException- if the element atindexis not aNumberNullPointerException- if the element atindexisnull
-
getBytes
public byte[] getBytes(int index) Gets an indexed item as abyte[]. This function will not do type conversion and so will throw aClassCastExceptionif the tuple element is not abytearray.- Parameters:
index- the location of the element to return- Returns:
- the item at
indexas abyte[] - Throws:
ClassCastException- if the element atindexis not aNumber
-
getString
Gets an indexed item as aString. This function will not do type conversion and so will throw aClassCastExceptionif the tuple element is not ofStringtype.- Parameters:
index- the location of the element to return- Returns:
- the item at
indexas aString - Throws:
ClassCastException- if the element atindexis not aString
-
getBigInteger
Gets an indexed item as aBigInteger. This function will not do type conversion and so will throw aClassCastExceptionif the tuple element is not of aNumbertype. If the underlying type is a floating point value, this will lead to a loss of precision. The element at the index may not benull.- Parameters:
index- the location of the element to return- Returns:
- the item at
indexas aBigInteger - Throws:
ClassCastException- if the element atindexis not aNumber
-
getFloat
public float getFloat(int index) Gets an indexed item as afloat. This function will not do type conversion and so will throw aClassCastExceptionif the element is not a number type. The element at the index may not benull.- Parameters:
index- the location of the item to return- Returns:
- the item at
indexas afloat - Throws:
ClassCastException- if the element atindexis not aNumber
-
getDouble
public double getDouble(int index) Gets an indexed item as adouble. This function will not do type conversion and so will throw aClassCastExceptionif the element is not a number type. The element at the index may not benull.- Parameters:
index- the location of the item to return- Returns:
- the item at
indexas adouble - Throws:
ClassCastException- if the element atindexis not aNumber
-
getBoolean
public boolean getBoolean(int index) Gets an indexed item as aboolean. This function will not do type conversion and so will throw aClassCastExceptionif the element is not aBoolean. The element at the index may not benull.- Parameters:
index- the location of the item to return- Returns:
- the item at
indexas aboolean - Throws:
ClassCastException- if the element atindexis not aBooleanNullPointerException- if the element atindexisnull
-
getUUID
Gets an indexed item as aUUID. This function will not do type conversion and so will throw aClassCastExceptionif the element is not aUUID. The element at the index may benull.- Parameters:
index- the location of the item to return- Returns:
- the item at
indexas aUUID - Throws:
ClassCastException- if the element atindexis not aUUID
-
getVersionstamp
Gets an indexed item as aVersionstamp. This function will not do type conversion and so will throw aClassCastExceptionif the element is not aVersionstamp. The element at the index may benull.- Parameters:
index- the location of the item to return- Returns:
- the item at
indexas aVersionstamp - Throws:
ClassCastException- if the element atindexis not aVersionstamp
-
getNestedList
Gets an indexed item as aList. This function will not do type conversion and so will throw aClassCastExceptionif the element is not aListorTuple. The element at the index may benull.- Parameters:
index- the location of the item to return- Returns:
- the item at
indexas aList - Throws:
ClassCastException- if the element atindexis not aListor aTuple
-
getNestedTuple
Gets an indexed item as aTuple. This function will not do type conversion and so will throw aClassCastExceptionif the element is not aListorTuple. The element at the index may benull.- Parameters:
index- the location of the item to return- Returns:
- the item at
indexas aList - Throws:
ClassCastException- if the element atindexis not aTupleor aList
-
get
Gets an indexed item without forcing a type.- Parameters:
index- the index of the item to return- Returns:
- an item from the list, without forcing type conversion
-
popFront
Creates a newTuplewith the first item of thisTupleremoved.- Returns:
- a newly created
Tuplewithout the first item of thisTuple - Throws:
IllegalStateException- if thisTupleis empty
-
popBack
Creates a newTuplewith the last item of thisTupleremoved.- Returns:
- a newly created
Tuplewithout the last item of thisTuple - Throws:
IllegalStateException- if thisTupleis empty
-
range
Returns a range representing all keys that encodeTuples strictly starting with thisTuple.
For example:Tuple t = Tuple.from("a", "b"); Range r = t.range();rincludes all tuples ("a", "b", ...)
This function will throw an error if thisTuplecontains an incompleteVersionstamp.- Returns:
- the range of keys containing all possible keys that have this
Tupleas a strict prefix
-
range
Returns a range representing all keys that encodeTuples strictly starting with the given prefix followed by thisTuple.
For example:Tuple t = Tuple.from("a", "b"); Range r = t.range(Tuple.from("c").pack());rcontains all tuples ("c", "a", "b", ...)
This function will throw an error if thisTuplecontains an incompleteVersionstamp.- Parameters:
prefix- a byte prefix to precede all elements in the range- Returns:
- the range of keys containing all possible keys that have
prefixfollowed by thisTupleas a strict prefix
-
hasIncompleteVersionstamp
public boolean hasIncompleteVersionstamp()Determines if there is aVersionstampincluded in thisTuplethat has not had its transaction version set. It will search through nestedTuples contained within thisTuple. It will not throw an error if it finds multiple incompleteVersionstampinstances.- Returns:
- whether there is at least one incomplete
Versionstampincluded in thisTuple
-
getPackedSize
public int getPackedSize()Get the number of bytes in the packed representation of thisTuple. This is done by summing the serialized sizes of all of the elements of thisTupleand does not pack everything into a singleTuple. The return value of this function is stored within thisTupleafter this function has been called so that subsequent calls on the same object are fast. This method does not validate that there is not more than one incompleteVersionstampin thisTuple.- Returns:
- the number of bytes in the packed representation of this
Tuple
-
compareTo
Compare the byte-array representation of thisTupleagainst another. This method will sortTuples in the same order that they would be sorted as keys in FoundationDB. Returns a negative integer, zero, or a positive integer when this object's byte-array representation is found to be less than, equal to, or greater than the specifiedTuple.- Specified by:
compareToin interfaceComparable<Tuple>- Parameters:
t- theTupleagainst which to compare- Returns:
- a negative integer, zero, or a positive integer when this
Tupleis less than, equal, or greater than the parametert.
-
hashCode
public int hashCode()Returns a hash code value for thisTuple. Computing the hash code is fairly expensive as it involves packing the underlyingTupleto bytes. However, this value is memoized, so for any givenTuple, it only needs to be computed once. This means that it is generally safe to useTuples with hash-based data structures such asHashSets orHashMaps. -
equals
Tests for equality with anotherTuple. If the passed object is not aTuplethis returns false. If the object is aTuple, this returns true ifcompareTo()would return0. -
toString
Returns a string representing thisTuple. This contains human-readable representations of all of the elements of thisTuple. For most elements, this means using that object's default string representation. For byte-arrays, this means usingByteArrayUtil.printable()to produce a byte-string where most printable ASCII code points have been rendered as characters. -
fromItems
Creates a newTuplefrom a variable number of elements. The elements must follow the type guidelines fromadd, and so can only beStrings,byte[]s,Numbers,UUIDs,Booleans,Lists,Tuples, ornulls.- Parameters:
items- the elements from which to create theTuple- Returns:
- a new
Tuplewith the given items as its elements
-
fromList
Efficiently creates a newTuplefrom a list of objects. The elements must follow the type guidelines fromadd, and so can only beStrings,byte[]s,Numbers,UUIDs,Booleans,Lists,Tuples, ornulls.- Parameters:
items- the elements from which to create theTuple.- Returns:
- a new
Tuplewith the given items as its elements
-
fromStream
Efficiently creates a newTuplefrom aStreamof objects. The elements must follow the type guidelines fromadd, and so can only beStrings,byte[]s,Numbers,UUIDs,Booleans,Lists,Tuples, ornulls. Note that this class will consume all elements from theStream.- Parameters:
items- theStreamof items from which to create theTuple- Returns:
- a new
Tuplewith the given items as its elements
-
from
Creates a newTuplefrom a variable number of elements. The elements must follow the type guidelines fromadd, and so can only beStrings,byte[]s,Numbers,UUIDs,Booleans,Lists,Tuples, ornulls.- Parameters:
items- the elements from which to create theTuple- Returns:
- a new
Tuplewith the given items as its elements
-