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. Structs 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
FieldsModifier and TypeFieldDescriptionprotected final DataType[]protected final booleanprotected final boolean -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionObject[]Read an instance ofTfrom the buffersrc.decode(PositionedByteRange src, int index) Read the field atindex.intencode(PositionedByteRange dst, Object[] val) Write instancevalinto bufferdst.Inform consumers over what type thisDataTypeoperates.intencodedLength(Object[] val) Inform consumers how long the encodedbyte[]will be.getOrder()Retrieve the sortOrderimposed by this data type, or null when natural ordering is not preserved.booleanIndicates whether this instance supports encoding null values.booleanIndicates whether this instance writes encodedbyte[]'s which preserve the natural sort order of the unencoded value.booleanIndicates whether this instance is able to skip over it's encoded value.Retrieve anIteratorover the values encoded insrc.intskip(PositionedByteRange src) Skipsrc's position forward over one encoded value.
-
Field Details
-
fields
-
isOrderPreserving
-
isSkippable
-
-
Constructor Details
-
Struct
Create a newStructinstance defined as the sequence ofHDataTypes inmemberTypes.A
StructisorderPreservingwhen all of its fields areorderPreserving. AStructisskippablewhen all of its fields areskippable.
-
-
Method Details
-
isOrderPreserving
Description copied from interface:DataTypeIndicates whether this instance writes encodedbyte[]'s which preserve the natural sort order of the unencoded value.- Specified by:
isOrderPreservingin interfaceDataType<Object[]>- Returns:
truewhen natural order is preserved,falseotherwise.
-
getOrder
Description copied from interface:DataTypeRetrieve the sortOrderimposed 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:DataTypeIndicates whether this instance supports encoding null values. This depends on the implementation details of the encoding format. AllDataTypes that support null should treat null as comparing less than any non-null value for default sort ordering purposes.- Specified by:
isNullablein interfaceDataType<Object[]>- Returns:
truewhen null is supported,falseotherwise.
-
isSkippable
Description copied from interface:DataTypeIndicates whether this instance is able to skip over it's encoded value.DataTypes that are not skippable can only be used as the right-most field of aStruct.- Specified by:
isSkippablein interfaceDataType<Object[]>
-
encodedLength
Description copied from interface:DataTypeInform consumers how long the encodedbyte[]will be.- Specified by:
encodedLengthin interfaceDataType<Object[]>- Parameters:
val- The value to check.- Returns:
- the number of bytes required to encode
val.a
-
encodedClass
Description copied from interface:DataTypeInform consumers over what type thisDataTypeoperates. Useful when working with bareDataTypeinstances.- Specified by:
encodedClassin interfaceDataType<Object[]>
-
iterator
Retrieve anIteratorover the values encoded insrc.src's position is consumed by consuming this iterator. -
skip
Description copied from interface:DataTypeSkipsrc's position forward over one encoded value. -
decode
Description copied from interface:DataTypeRead an instance ofTfrom the buffersrc. -
decode
Read the field atindex.src's position is not affected. -
encode
Description copied from interface:DataTypeWrite instancevalinto bufferdst.
-