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.
Tuple
s and will sort in Java in
the same order in which they would sort in FoundationDB. Tuple
s 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[]
), String
s, Number
s, UUID
s,
boolean
s, List
s, Versionstamp
s, other Tuple
s, and null
.
Float
and Double
instances will be serialized as single- and double-precision
numbers respectively, and BigInteger
s 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 Number
s 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[]
, String
s, UUID
s, and
nested List
s and Tuple
s can be null
,
whereas numbers (e.g., long
s and double
s) 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 thisTuple
with aboolean
appended as the last element.add
(byte[] b) Creates a copy of thisTuple
with abyte
array appended as the last element.add
(byte[] b, int offset, int length) Creates a copy of thisTuple
with abyte
array appended as the last element.add
(double d) Creates a copy of thisTuple
with adouble
appended as the last element.add
(float f) Creates a copy of thisTuple
with afloat
appended as the last element.add
(long l) Creates a copy of thisTuple
with along
appended as the last element.Creates a copy of thisTuple
with aTuple
appended as the last element.add
(Versionstamp v) Creates a copy of thisTuple
with aVersionstamp
object appended as the last element.Creates a copy of thisTuple
with aString
appended as the last element.add
(BigInteger bi) Creates a copy of thisTuple
with aBigInteger
appended as the last element.Creates a copy of thisTuple
with aList
appended as the last element.Creates a copy of thisTuple
with aUUID
appended as the last element.Create a copy of thisTuple
with all elements from antherTuple
appended.Create a copy of thisTuple
with a list of items appended.Creates a copy of thisTuple
with an appended last element.int
Compare the byte-array representation of thisTuple
against another.boolean
Tests for equality with anotherTuple
.static Tuple
Creates a newTuple
from a variable number of elements.static Tuple
fromBytes
(byte[] bytes) Construct a newTuple
with elements decoded from a suppliedbyte
array.static Tuple
fromBytes
(byte[] bytes, int offset, int length) Construct a newTuple
with elements decoded from a suppliedbyte
array.static Tuple
Creates a newTuple
from a variable number of elements.static Tuple
Efficiently creates a newTuple
from a list of objects.static Tuple
fromStream
(Stream<?> items) Efficiently creates a newTuple
from aStream
of objects.get
(int index) Gets an indexed item without forcing a type.getBigInteger
(int index) Gets an indexed item as aBigInteger
.boolean
getBoolean
(int index) Gets an indexed item as aboolean
.byte[]
getBytes
(int index) Gets an indexed item as abyte[]
.double
getDouble
(int index) Gets an indexed item as adouble
.float
getFloat
(int index) Gets an indexed item as afloat
.getItems()
Gets the unserialized contents of thisTuple
.long
getLong
(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
.int
Get 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
.int
hashCode()
Returns a hash code value for thisTuple
.boolean
Determines if there is aVersionstamp
included in thisTuple
that has not had its transaction version set.boolean
isEmpty()
Determine if thisTuple
contains no elements.iterator()
Gets anIterator
over theObjects
in thisTuple
.byte[]
pack()
Get an encoded representation of thisTuple
.byte[]
pack
(byte[] prefix) Get an encoded representation of thisTuple
.void
packInto
(ByteBuffer dest) Pack an encoded representation of thisTuple
onto the end of the givenByteBuffer
.byte[]
Get an encoded representation of thisTuple
for use withMutationType.SET_VERSIONSTAMPED_KEY
.byte[]
packWithVersionstamp
(byte[] prefix) Get an encoded representation of thisTuple
for use withMutationType.SET_VERSIONSTAMPED_KEY
.popBack()
Creates a newTuple
with the last item of thisTuple
removed.popFront()
Creates a newTuple
with the first item of thisTuple
removed.range()
Returns a range representing all keys that encodeTuple
s strictly starting with thisTuple
.range
(byte[] prefix) Returns a range representing all keys that encodeTuple
s strictly starting with the given prefix followed by thisTuple
.int
size()
Gets the number of elements in thisTuple
.stream()
Gets aStream
of 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, wait
Methods 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 thisTuple
with aString
appended as the last element.- Parameters:
s
- theString
to append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuple
with along
appended as the last element.- Parameters:
l
- the number to append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuple
with abyte
array appended as the last element.- Parameters:
b
- thebyte
s to append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuple
with aboolean
appended as the last element.- Parameters:
b
- theboolean
to append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuple
with aUUID
appended as the last element.- Parameters:
uuid
- theUUID
to append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuple
with aBigInteger
appended as the last element. AsTuple
s cannot containnull
numeric types, aNullPointerException
is raised if anull
argument is passed.- Parameters:
bi
- theBigInteger
to append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuple
with afloat
appended as the last element.- Parameters:
f
- thefloat
to append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuple
with adouble
appended as the last element.- Parameters:
d
- thedouble
to append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuple
with aVersionstamp
object appended as the last element.- Parameters:
v
- theVersionstamp
to append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuple
with aList
appended 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
- theList
to append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuple
with aTuple
appended 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
- theTuple
to append- Returns:
- a newly created
Tuple
-
add
Creates a copy of thisTuple
with abyte
array appended as the last element.- Parameters:
b
- thebyte
s to appendoffset
- the starting index ofb
to addlength
- the number of elements ofb
to copy into thisTuple
- Returns:
- a newly created
Tuple
-
addAll
Create a copy of thisTuple
with a list of items appended. -
addAll
Create a copy of thisTuple
with all elements from antherTuple
appended.- Parameters:
other
- theTuple
whose elements should be appended- Returns:
- a newly created
Tuple
-
pack
public byte[] pack()Get an encoded representation of thisTuple
. Each element is encoded tobyte
s and concatenated. Note that once aTuple
has 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 tobyte
s and concatenated, and then the prefix supplied is prepended to the array. Note that once aTuple
has 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
Tuple
prepended by theprefix
-
packInto
Pack an encoded representation of thisTuple
onto the end of the givenByteBuffer
. It is up to the caller to ensure that there is enough space allocated within the buffer to avoidBufferOverflowException
s. The client may callgetPackedSize()
to determine how large thisTuple
will be once packed in order to allocate sufficient memory. Note that unlikepack()
, the serialized representation of thisTuple
is not stored, so calling this function multiple times with the sameTuple
requires serializing theTuple
multiple times.
This method will throw an error if there are any incompleteVersionstamp
s in thisTuple
.- Parameters:
dest
- the destinationByteBuffer
for the encodedTuple
-
packWithVersionstamp
public byte[] packWithVersionstamp()Get an encoded representation of thisTuple
for 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
Tuple
for use with versionstamp ops. - Throws:
IllegalArgumentException
- if there is not exactly one incompleteVersionstamp
included in thisTuple
-
packWithVersionstamp
public byte[] packWithVersionstamp(byte[] prefix) Get an encoded representation of thisTuple
for use withMutationType.SET_VERSIONSTAMPED_KEY
. There must be exactly one incompleteVersionstamp
instance within thisTuple
or this will throw anIllegalArgumentException
. Each element is encoded tobyte
s and concatenated, the prefix is then prepended to the array, and then the index of the packed incompleteVersionstamp
is appended as a little-endian integer. This can then be passed as the key toTransaction.mutate()
with theSET_VERSIONSTAMPED_KEY
MutationType
, and the transaction's version will then be filled in at commit time.
Note that once aTuple
has 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
Tuple
for use with versionstamp ops. - Throws:
IllegalArgumentException
- if there is not exactly one incompleteVersionstamp
included in thisTuple
-
getItems
Gets the unserialized contents of thisTuple
.- Returns:
- the elements that make up this
Tuple
.
-
stream
Gets aStream
of the unserialized contents of thisTuple
.- Returns:
- a
Stream
of the elements that make up thisTuple
.
-
iterator
Gets anIterator
over theObjects
in thisTuple
. ThisIterator
is unmodifiable and will throw an exception ifremove()
is called. -
fromBytes
Construct a newTuple
with elements decoded from a suppliedbyte
array. 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
- encodedTuple
source- Returns:
- a new
Tuple
constructed by deserializing the providedbyte
array - Throws:
IllegalArgumentException
- ifbytes
does not represent a validTuple
-
fromBytes
Construct a newTuple
with elements decoded from a suppliedbyte
array. 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
- encodedTuple
sourceoffset
- starting offset of byte array of encoded datalength
- length of encoded data within the source- Returns:
- a new
Tuple
constructed by deserializing the specified slice of the providedbyte
array - Throws:
IllegalArgumentException
- ifoffset
orlength
are negative or would exceed the size of the array or ifbytes
does 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 thisTuple
contains no elements.- Returns:
true
if thisTuple
contains no elements,false
otherwise
-
getLong
public long getLong(int index) Gets an indexed item as along
. This function will not do type conversion and so will throw aClassCastException
if 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
index
as along
- Throws:
ClassCastException
- if the element atindex
is not aNumber
NullPointerException
- if the element atindex
isnull
-
getBytes
public byte[] getBytes(int index) Gets an indexed item as abyte[]
. This function will not do type conversion and so will throw aClassCastException
if the tuple element is not abyte
array.- Parameters:
index
- the location of the element to return- Returns:
- the item at
index
as abyte[]
- Throws:
ClassCastException
- if the element atindex
is not aNumber
-
getString
Gets an indexed item as aString
. This function will not do type conversion and so will throw aClassCastException
if the tuple element is not ofString
type.- Parameters:
index
- the location of the element to return- Returns:
- the item at
index
as aString
- Throws:
ClassCastException
- if the element atindex
is not aString
-
getBigInteger
Gets an indexed item as aBigInteger
. This function will not do type conversion and so will throw aClassCastException
if the tuple element is not of aNumber
type. 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
index
as aBigInteger
- Throws:
ClassCastException
- if the element atindex
is 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 aClassCastException
if 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
index
as afloat
- Throws:
ClassCastException
- if the element atindex
is 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 aClassCastException
if 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
index
as adouble
- Throws:
ClassCastException
- if the element atindex
is 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 aClassCastException
if 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
index
as aboolean
- Throws:
ClassCastException
- if the element atindex
is not aBoolean
NullPointerException
- if the element atindex
isnull
-
getUUID
Gets an indexed item as aUUID
. This function will not do type conversion and so will throw aClassCastException
if 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
index
as aUUID
- Throws:
ClassCastException
- if the element atindex
is not aUUID
-
getVersionstamp
Gets an indexed item as aVersionstamp
. This function will not do type conversion and so will throw aClassCastException
if 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
index
as aVersionstamp
- Throws:
ClassCastException
- if the element atindex
is not aVersionstamp
-
getNestedList
Gets an indexed item as aList
. This function will not do type conversion and so will throw aClassCastException
if the element is not aList
orTuple
. The element at the index may benull
.- Parameters:
index
- the location of the item to return- Returns:
- the item at
index
as aList
- Throws:
ClassCastException
- if the element atindex
is not aList
or aTuple
-
getNestedTuple
Gets an indexed item as aTuple
. This function will not do type conversion and so will throw aClassCastException
if the element is not aList
orTuple
. The element at the index may benull
.- Parameters:
index
- the location of the item to return- Returns:
- the item at
index
as aList
- Throws:
ClassCastException
- if the element atindex
is not aTuple
or 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 newTuple
with the first item of thisTuple
removed.- Returns:
- a newly created
Tuple
without the first item of thisTuple
- Throws:
IllegalStateException
- if thisTuple
is empty
-
popBack
Creates a newTuple
with the last item of thisTuple
removed.- Returns:
- a newly created
Tuple
without the last item of thisTuple
- Throws:
IllegalStateException
- if thisTuple
is empty
-
range
Returns a range representing all keys that encodeTuple
s strictly starting with thisTuple
.
For example:Tuple t = Tuple.from("a", "b"); Range r = t.range();
r
includes all tuples ("a", "b", ...)
This function will throw an error if thisTuple
contains an incompleteVersionstamp
.- Returns:
- the range of keys containing all possible keys that have this
Tuple
as a strict prefix
-
range
Returns a range representing all keys that encodeTuple
s 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());
r
contains all tuples ("c", "a", "b", ...)
This function will throw an error if thisTuple
contains 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
prefix
followed by thisTuple
as a strict prefix
-
hasIncompleteVersionstamp
public boolean hasIncompleteVersionstamp()Determines if there is aVersionstamp
included in thisTuple
that has not had its transaction version set. It will search through nestedTuple
s contained within thisTuple
. It will not throw an error if it finds multiple incompleteVersionstamp
instances.- Returns:
- whether there is at least one incomplete
Versionstamp
included 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 thisTuple
and does not pack everything into a singleTuple
. The return value of this function is stored within thisTuple
after 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 incompleteVersionstamp
in thisTuple
.- Returns:
- the number of bytes in the packed representation of this
Tuple
-
compareTo
Compare the byte-array representation of thisTuple
against another. This method will sortTuple
s 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:
compareTo
in interfaceComparable<Tuple>
- Parameters:
t
- theTuple
against which to compare- Returns:
- a negative integer, zero, or a positive integer when this
Tuple
is 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 underlyingTuple
to 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 useTuple
s with hash-based data structures such asHashSet
s orHashMap
s. -
equals
Tests for equality with anotherTuple
. If the passed object is not aTuple
this 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 newTuple
from a variable number of elements. The elements must follow the type guidelines fromadd
, and so can only beString
s,byte[]
s,Number
s,UUID
s,Boolean
s,List
s,Tuple
s, ornull
s.- Parameters:
items
- the elements from which to create theTuple
- Returns:
- a new
Tuple
with the given items as its elements
-
fromList
Efficiently creates a newTuple
from a list of objects. The elements must follow the type guidelines fromadd
, and so can only beString
s,byte[]
s,Number
s,UUID
s,Boolean
s,List
s,Tuple
s, ornull
s.- Parameters:
items
- the elements from which to create theTuple
.- Returns:
- a new
Tuple
with the given items as its elements
-
fromStream
Efficiently creates a newTuple
from aStream
of objects. The elements must follow the type guidelines fromadd
, and so can only beString
s,byte[]
s,Number
s,UUID
s,Boolean
s,List
s,Tuple
s, ornull
s. Note that this class will consume all elements from theStream
.- Parameters:
items
- theStream
of items from which to create theTuple
- Returns:
- a new
Tuple
with the given items as its elements
-
from
Creates a newTuple
from a variable number of elements. The elements must follow the type guidelines fromadd
, and so can only beString
s,byte[]
s,Number
s,UUID
s,Boolean
s,List
s,Tuple
s, ornull
s.- Parameters:
items
- the elements from which to create theTuple
- Returns:
- a new
Tuple
with the given items as its elements
-