Skip to main content
Version: 5.7

Compact Serialization Binary Specification

The Compact Serialization Binary Specification defines the binary format of the Compact Serialization, the supported types, and the Compact Schema.

The supported data types include:

  • Fixed-size types like boolean, int8, int16, int32, int64, float32, and float64
  • Nullable versions of the fixed-size types
  • Variable-size types like string, decimal, time, date, timestamp, and timestamp with timezone
  • Arrays of the types listed above, and
  • Nested Compact serializable objects containing the types listed above, and arrays of them.

Note: Except fixed-size types, all other types are considered variable-size.

See Compact Serialization on implementing and using this Hazelcast-specific serialization option.

Introduction​

Every serialized user object consists of a header and data. Compact Serialization includes a schema that describes the serialized data and a schema ID, represented by an 8-byte long fingerprint, that uniquely identifies the schema. The schema and the data are separate entities that are connected by the schema ID.

Data Types​

The table below shows the data types that are supported by the Compact Serialization.

All primitive types, user types, array size, number of items, length of data, schema ID, and offsets are configured in Compact Serialization with a default endianness of BIG_ENDIAN.

You cannot redefine the endianness in Compact Serialization; it is inherited from existing configuration.

TypeJavaC++C#PythonNode.jsGoSQLDescriptionFixed Size
booleanbooleanboolboolboolbooleanboolBOOLEANEither true or false, represented by 1 bit as 1 or 0. Up to 8 booleans are packed into a single byte.Yes
int8byteint8_tsbyteintnumberint8TINYINT8-bit two's-complement signed integer.Yes
int16shortint16_tshortintnumberint16SMALLINT16-bit two's-complement signed integer.Yes
int32intint32_tintintnumberint32INTEGER32-bit two's-complement signed integer.Yes
int64longint64_tlongintLongint64BIGINT64-bit two's-complement signed integer.Yes
float32floatfloatfloatfloatnumberfloat32REAL32-bit IEEE 754 floating-point number.Yes
float64doubledoubledoublefloatnumberfloat64DOUBLE64-bit IEEE 754 floating-point number.Yes
stringStringoptional<std::string>string?Optional[str]string | null*stringSTRINGEither null, or the number of bytes in the string as an int32, followed by the UTF-8 string.No
decimalBigDecimaloptional<hazelcast::client::big_decimal>HBigDecimal?Optional[decimal.Decimal]BigDecimal | null*types.DecimalDECIMALEither null, or an arbitrary-precision floating-point number represented as unscaledValue x 10 ^ -scale. The unscaledValue is an array of int8 that holds the two's-complement binary representation in big-endian byte order, with the most significant byte in the zeroth element. The scale is a single int32.No
timeLocalTimeoptional<hazelcast::client::local_time>HLocalTime?Optional[datetime.time]LocalTime | null*types.LocalTimeTIMEEither null, or a time as HH-MI-SS-NN, stored as int8: hour, int8: minute, int8: seconds, and int32: nanoseconds.No (since it is nullable)
dateLocalDateoptional<hazelcast::client::local_date>HLocalDate?Optional[datetime.date]LocalDate | null*types.LocalDateDATEEither null, or a date as YYYY-MM-DD from -999999999-01-1 to 999999999-12-31, stored as int32: year, int8: month, and int8: dayOfMonth.No (since it is nullable)
timestampLocalDateTimeoptional<hazelcast::client::local_date_time>HLocalDateTime?Optional[datetime.datetime]LocalDateTime | null*types.LocalDateTimeTIMESTAMPEither null, or a date and time as YYYY-MM-DD-HH-MI-SS-NN, stored as int32: year, int8: month, int8: dayOfMonth, int8: hour, int8: minute, int8: seconds, and int32: nanoseconds.No (since it is nullable)
timestampWithTimeZoneOffsetDateTimeoptional<hazelcast::client::offset_date_time>HOffsetDateTime?Optional[datetime.datetime]OffsetDateTime | null*types.OffsetDateTimeTIMESTAMP WITH TIME ZONEEither null, or a date and time with a time zone offset as YYYY-MM-DD-HH-MI-SS-MM Zone, stored as int32: year, int8: month, int8: dayOfMonth, int8: hour, int8: minute, int8: seconds, int32: nanoseconds, and int32: offsetSeconds. The offset is in seconds, from -64800 to 64800 (±18 hours).No (since it is nullable)
compactTtemplate<typename T> optional<T>T?Optional[Any]T | nullinterface{}OBJECTA user-defined compact.No
boolean[]boolean[]optional<std::vector<bool>>bool[]?Optional[list[bool]]boolean[] | null[]boolArray of boolean values.No
int8[]byte[]optional<std::vector<int8_t>>sbyte[]?Optional[list[int]]Buffer | null[]int8Array of int8 values.No
int16[]short[]optional<std::vector<int16_t>>short[]?Optional[list[int]]number[] | null[]int16Array of int16 values.No
int32[]int[]optional<std::vector<int32_t>>int[]?Optional[list[int]]number[] | null[]int32Array of int32 values.No
int64[]long[]optional<std::vector<int64_t>>long[]?Optional[list[int]]Long[] | null[]int64Array of int64 values.No
float32[]float[]optional<std::vector<float>>float[]?Optional[list[float]]number[] | null[]float32Array of float32 values.No
float64[]double[]optional<std::vector<double>>double[]?Optional[list[float]]number[] | null[]float64Array of float64 values.No
string[]String[]optional<std::vector<optional<std::string>>>string?[]?Optional[list[Optional[str]]](string | null)[] | null[]*stringArray of string values.No
decimal[]BigDecimal[]optional<std::vector<optional<hazelcast::client::big_decimal>>>HBigDecimal?[]?Optional[list[Optional[decimal.Decimal]]](BigDecimal | null)[] | null[]*types.DecimalArray of decimal values.No
time[]LocalTime[]optional<std::vector<optional<hazelcast::client::local_time>>>HLocalTime?[]?Optional[list[Optional[datetime.time]]](LocalTime | null)[] | null[]*types.LocalTimeArray of time values.No
date[]LocalDate[]optional<std::vector<optional<hazelcast::client::local_date>>>HLocalDate?[]?Optional[list[Optional[datetime.date]]](LocalDate | null)[] | null[]*types.LocalDateArray of date values.No
timestamp[]LocalDateTime[]optional<std::vector<optional<hazelcast::client::local_date_time>>>HLocalDateTime?[]?Optional[list[Optional[datetime.datetime]]](LocalDateTime | null)[] | null[]*types.LocalDateTimeArray of timestamp values.No
timestampWithTimeZone[]OffsetDateTime[]optional<std::vector<optional<hazelcast::client::offset_date_time>>>HOffsetDateTime?[]?Optional[list[Optional[datetime.datetime]]](OffsetDateTime | null)[] | null[]*types.OffsetDateTimeArray of timestampWithTimeZone values.No
compact[]T[]template<typename T> optional<std::vector<optional<T>>>T?[]?Optional[list[Optional[Any]]](T | null)[] | null[]interface{}Array of compact values.No
nullable-booleanBooleanoptional<bool>bool?Optional[bool]boolean | null*boolEither null, or int8 1 for true and int8 0 for false.No
nullable-int8Byteoptional<int8_t>sbyte?Optional[int]number | null*int8An int8 that can also be null.No
nullable-int16Shortoptional<int16_t>short?Optional[int]number | null*int16An int16 that can also be null.No
nullable-int32Integeroptional<int32_t>int?Optional[int]number | null*int32An int32 that can also be null.No
nullable-int64Longoptional<int64_t>long?Optional[int]Long | null*int64An int64 that can also be null.No
nullable-float32Floatoptional<float>float?Optional[float]number | null*float32A float32 that can also be null.No
nullable-float64Doubleoptional<double>double?Optional[float]number | null*float64A float64 that can also be null.No
nullable-boolean[]Boolean[]optional<std::vector<optional<bool>>>bool?[]?Optional[list[Optional[bool]]](boolean | null)[] | null[]*boolArray of nullable boolean values.No
nullable-int8[]Byte[]optional<std::vector<optional<int8_t>>>sbyte?[]?Optional[list[Optional[int]]](number | null)[] | null[]*int8Array of nullable int8 values.No
nullable-int16[]Short[]optional<std::vector<optional<int16_t>>>short?[]?Optional[list[Optional[int]]](number | null)[] | null[]*int16Array of nullable int16 values.No
nullable-int32[]Integer[]optional<std::vector<optional<int32_t>>>int?[]?Optional[list[Optional[int]]](number | null)[] | null[]*int32Array of nullable int32 values.No
nullable-int64[]Long[]optional<std::vector<optional<int64_t>>>long?[]?Optional[list[Optional[int]]](Long | null)[] | null[]*int64Array of nullable int64 values.No
nullable-float32[]Float[]optional<std::vector<optional<float>>>float?[]?Optional[list[Optional[float]]](number | null)[] | null[]*float32Array of nullable float32 values.No
nullable-float64[]Double[]optional<std::vector<optional<double>>>double?[]?Optional[list[Optional[float]]](number | null)[] | null[]*float64Array of nullable float64 values.No

Type IDs​

Each type supported in the wire format has its type ID. The type IDs are used while constructing the schemas, performing type checks when accessing fields, and are exposed in a public API.

Nullable Primitives​

Nullable primitives are implemented as variable-sized types. The null values of nullable primitives are represented like null variable-sized fields, with the offset of -1 and no data.

The partition hash and the type ID are common for all serialization methods supported by Hazelcast, including Compact Serialization. Every serialized object has a header and the payload on the wire.

NameTypeDescription
Partition hashi32BIG_ENDIAN integer, used for key objects. Not applicable to value objects.
Type IDi32BIG_ENDIAN integer that determines the serializer to be used. -55 for compact.

Var-size Objects​

Var-size objects are user-defined objects whose binary representation consists of Header, Data, and Offsets sections, given in this order.

Header​

NameTypeDescription
Schema IDi64Schema Hash.
Data lengthi32Length of the Data Section.

Data​

NameDescription
Fixed-size FieldsFixed-size field offsets are deduced from the schema.
Variable-size Fields

Offsets​

NameTypeDescription
Variable-Size FieldOffset index 0u8/u16/i32The index of a field offset is written in the schema. Offsets of variable length fields. -1 for null
Variable-Size FieldOffset index 1u8/u16/i32The index of a field offset is written in the schema. Offsets of variable length fields. -1 for null
…​…​
Variable-Size FieldOffset index nu8/u16/i32The index of a field offset is written in the schema. Offsets of variable length fields. -1 for null

Note: If the composed data does not include any variable-size field in the schema, Variable-Size FieldOffset and DataLength will not exist on the wire. Similarly, if there is no fixed-size field in the schema, Fixed-Size Fields will not exist on the wire.

Variable-Size FieldOffset`s are calculated from the beginning of the `DATA SECTION shown in the table above.

Variable-Size FieldOffset sizes vary depending on the Data Length.

  • Data Length <= 254, offsets are u8 (255 is reserved for null)
  • Data Length <= 65534, offsets are u16 (65535 is reserved for null)
  • Otherwise, offsets are i32.

Length is written before offsets so that the binary can be skipped even when the schema cannot be found.

A Variable-Size FieldOffset is -1 if a Variable-Size field is null.

Fixed-Size Fields cannot be null.

Fixed-Size Fields​

The fixed-size fields are written after the Length field, consecutively. Fixed-size fields are accessed via their offsets that are calculated from the schema.

On the schema, the offset for a fixed-size field is determined as follows:

  • The first field always starts from offset 0.
  • Fields are ordered by their size in descending order.
  • When field sizes are the same, fields are ordered by the field name.
  • Each offset is calculated by adding the size of the last field to the last offset.

The only exception to the above rule is boolean fields. The size of a boolean is a byte, but 8 booleans can be packed into a single byte and these booleans can share the same offset and byte. To achieve that, extra information is stored in the schema (nothing extra on the data) for the bit index of the boolean fields. Boolean fields are written at the end of the fixed-size fields.

Variable-size Fields​

The offsets of variable-size fields are written at the end in the alphabetical order of the field names. Each variable-size field offset has an index, starting from 0, written in the schema. To read a variable-size field from the data, one should read the index of the offset from the schema. Then, the offset associated with the related index is read from the end of the data. The variable-size field can be read using this offset.

On the schema, the index for a variable-size field is determined as follows:

  • The fields are given the index incrementally, according to the order of the field names, starting from 0.

Based on the length of the serialized data, the offsets of the variable-size fields might be represented by 1, 2, or 4 bytes. That is, all variable-size field offsets are either 1, 2, or 4 bytes per serialized object, depending on the field size.

Schema​

NameType
type namestring
number of fieldsi32
name of field 0string
type ID of field 0i32
name of field 1string
type ID of field 1i32
…​…​
name of field nstring
type ID of field ni32

A schema keeps the type name of the Compact type and some fields. A schema represents the structure of a Compact serialized data and allows reading the data. The type name is used to differentiate the user types which have the same number of fields with the same types.

Only schema ID is written to serialized data, however, schemas need to be exchanged between a client and a member to be able to serialize that data. A schema orders its fields by field name during initialization, so that the schema in both the client and the member produces the same schema ID.

During initialization, field offsets and indexes are assigned as well. Offsets are pointers to the locations of the fields in the serialized data. Indexes are for enumerating variable-size fields' offsets. Variable-size fields' offsets are written in the serialized data. For details on how fixed-size field offsets are assigned, see Fixed-Size Fields. For details on how variable-size field indexes and offsets are assigned, see Variable-size Fields.

In the schema class, each field will either:

  • have a positive offset, if it is a fixed-size field
  • have a positive index if it is a variable-size field
  • have a positive bit offset if it is a boolean field, which is the offset within the byte given by the normal offset.

Schema ID​

We are using 64bit Rabin fingerprint to create a schema ID.

Rabin fingerprint is chosen mostly because it is recommended in Avro's documentation as follows.

At the opposite extreme, the smallest fingerprint recommended is a 64-bit Rabin fingerprint. Below, there is a provided pseudo-code for this algorithm that can be easily translated into any programming language. 64-bit fingerprints should guarantee uniqueness for schema caches of up to a million entries (for such a cache, the chance of a collision is 3E-8). It is not recommended to use shorter fingerprints, as the chances of collisions are too high (for example, with 32-bit fingerprints, a cache with as few as 100,000 schemas has a 50% chance of having a collision).

According to the quote, even with a schema cache with a million entries, the chance of a collision is very low. Therefore, there should not be a need to the change number of bits of the hashing algorithm soon.

The schema ID is calculated from the byte array representation of the schema described above.

The implementation is as follows:

java
long fingerprint64(byte[] buf) {
if (FP_TABLE == null) initFPTable();
long fp = EMPTY;
for (int i = 0; i < buf.length; i++)
fp = (fp >>> 8) ^ FP_TABLE[(int)(fp ^ buf[i]) & 0xff];
return fp;
}

static long EMPTY = 0xc15d213aa4d7a795L;
static long[] FP_TABLE = null;

void initFPTable() {
FP_TABLE = new long[256];
for (int i = 0; i < 256; i++) {
long fp = i;
for (int j = 0; j < 8; j++)
fp = (fp >>> 1) ^ (EMPTY & -(fp & 1L));
FP_TABLE[i] = fp;
}
}

Arrays​

Arrays of fixed-size items can not have null items. On the other hand, arrays of variable-size items may contain null items.

Array of Fixed-size Items​

NameType
Number of itemsi32
item 0item type
item 1item type
item 2item type
item nitem type

Array of Variable-size Items​

Consists of Header, Data, and Offsets sections in this order.

Header​

NameType
Data lengthi32
Number of itemsi32

Data​

NameType
Item 0item type
Item 1item type
…​…​
Item nitem type

Offsets​

NameType
Item 0 offsetu8/u16/i32
Item 1 offsetu8/u16/i32
…​…​
Item n offsetu8/u16/i32

An array can contain only a single type of item. In the case of Compact[], all the items must have the same schema, that is, their schema ID must be equal.

Offsets are calculated from the beginning of the Data section.

Data Length is the length of the Data section.

Offset sizes vary depending on the Data Length.

  • For Data Length <= 254, offsets are u8 (255 is reserved for null)
  • For 255 < Data Length <= 65534, offsets are u16 (65535 is reserved for null)
  • For Data Length > 65535, offsets are i32.

Variable-size items can be null. The corresponding offset will be set to -1 in that case.

Nullable Values​

Nullable fields can be null. The null values are represented with -1 offset in the binary, and no more data is written. On the other hand, non-nullable fields always take up space in the binary.

Fixed-size fields are non-nullable. Variable-size fields are nullable.