Class Struct
Struct
is a simple DataType
for implementing "compound rowkey" and "compound
qualifier" schema design strategies.
Encoding
Struct
member values are encoded onto the target byte[] in the order in which they are
declared. A Struct
may be used as a member of another Struct
. Struct
s are
not nullable
but their component fields may be.
Trailing Nulls
Struct
treats the right-most nullable field members as special. Rather than writing null
values to the output buffer, Struct
omits those records all together. When reading back a
value, it will look for the scenario where the end of the buffer has been reached but there are
still nullable fields remaining in the Struct
definition. When this happens, it will
produce null entries for the remaining values. For example:
StructBuilder builder = new StructBuilder() .add(OrderedNumeric.ASCENDING) // nullable .add(OrderedString.ASCENDING) // nullable Struct shorter = builder.toStruct(); Struct longer = builder.add(OrderedNumeric.ASCENDING) // nullable .toStruct(); PositionedByteRange buf1 = new SimplePositionedByteRange(7); PositionedByteRange buf2 = new SimplePositionedByteRange(7); Object[] val = new Object[] { BigDecimal.ONE, "foo" }; shorter.encode(buf1, val); // write short value with short Struct buf1.setPosition(0); // reset position marker, prepare for read longer.decode(buf1); // => { BigDecimal.ONE, "foo", null } ; long Struct reads implied null longer.encode(buf2, val); // write short value with long struct Bytes.equals(buf1.getBytes(), buf2.getBytes()); // => true; long Struct skips writing null
Sort Order
Struct
instances sort according to the composite order of their fields, that is,
left-to-right and depth-first. This can also be thought of as lexicographic comparison of
concatenated members.
StructIterator
is provided as a convenience for consuming the sequence of values. Users
may find it more appropriate to provide their own custom DataType
for encoding
application objects rather than using this Object[]
implementation. Examples are provided
in test.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionprotected final DataType[]
protected final boolean
protected final boolean
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionObject[]
Read an instance ofT
from the buffersrc
.decode
(PositionedByteRange src, int index) Read the field atindex
.int
encode
(PositionedByteRange dst, Object[] val) Write instanceval
into bufferdst
.Inform consumers over what type thisDataType
operates.int
encodedLength
(Object[] val) Inform consumers how long the encodedbyte[]
will be.getOrder()
Retrieve the sortOrder
imposed by this data type, or null when natural ordering is not preserved.boolean
Indicates whether this instance supports encoding null values.boolean
Indicates whether this instance writes encodedbyte[]
's which preserve the natural sort order of the unencoded value.boolean
Indicates whether this instance is able to skip over it's encoded value.Retrieve anIterator
over the values encoded insrc
.int
skip
(PositionedByteRange src) Skipsrc
's position forward over one encoded value.
-
Field Details
-
fields
-
isOrderPreserving
-
isSkippable
-
-
Constructor Details
-
Struct
Create a newStruct
instance defined as the sequence ofHDataType
s inmemberTypes
.A
Struct
isorderPreserving
when all of its fields areorderPreserving
. AStruct
isskippable
when all of its fields areskippable
.
-
-
Method Details
-
isOrderPreserving
Description copied from interface:DataType
Indicates whether this instance writes encodedbyte[]
's which preserve the natural sort order of the unencoded value.- Specified by:
isOrderPreserving
in interfaceDataType<Object[]>
- Returns:
true
when natural order is preserved,false
otherwise.
-
getOrder
Description copied from interface:DataType
Retrieve the sortOrder
imposed by this data type, or null when natural ordering is not preserved. Value is either ascending or descending. Default is assumed to beOrder.ASCENDING
. -
isNullable
Description copied from interface:DataType
Indicates whether this instance supports encoding null values. This depends on the implementation details of the encoding format. AllDataType
s that support null should treat null as comparing less than any non-null value for default sort ordering purposes.- Specified by:
isNullable
in interfaceDataType<Object[]>
- Returns:
true
when null is supported,false
otherwise.
-
isSkippable
Description copied from interface:DataType
Indicates whether this instance is able to skip over it's encoded value.DataType
s that are not skippable can only be used as the right-most field of aStruct
.- Specified by:
isSkippable
in interfaceDataType<Object[]>
-
encodedLength
Description copied from interface:DataType
Inform consumers how long the encodedbyte[]
will be.- Specified by:
encodedLength
in interfaceDataType<Object[]>
- Parameters:
val
- The value to check.- Returns:
- the number of bytes required to encode
val
.a
-
encodedClass
Description copied from interface:DataType
Inform consumers over what type thisDataType
operates. Useful when working with bareDataType
instances.- Specified by:
encodedClass
in interfaceDataType<Object[]>
-
iterator
Retrieve anIterator
over the values encoded insrc
.src
's position is consumed by consuming this iterator. -
skip
Description copied from interface:DataType
Skipsrc
's position forward over one encoded value. -
decode
Description copied from interface:DataType
Read an instance ofT
from the buffersrc
. -
decode
Read the field atindex
.src
's position is not affected. -
encode
Description copied from interface:DataType
Write instanceval
into bufferdst
.
-