WiredTiger: Packing and Unpacking Data in Java

| Version 3.1.0

Fork me on GitHub Join my user group
Packing and Unpacking Data in Java

WiredTiger's data packing uses format strings similar to those specified in the Python struct module: http://docs.python.org/library/struct

The first character of the format string can be used to indicate the byte order, size and alignment of the packed data, according to the following table:

Character | Byte order | Size | Alignment
--- | --- | --- | ---
`.` | big-endian | packed | none
`>` | big-endian | standard | none
`<` | little-endian | standard | none
`@` | native | native | native

If the first character is not one of these, '.' (big-endian, packed) is assumed: it naturally sorts in lexicographic order, and the packed format uses variable-sized encoding of values to reduce the data size.

Note: little-endian format not yet supported in WiredTiger. Only the default big-endian, packed format is currently supported.

The remaining characters in the format string specify the type of each field to be packed into or unpacked from a byte array. See Column types for the list of supported types.

In Java, data is packed and unpacked using cursor put* and get* operations, for example:

/* Insert the records into the table. */
cursor = session.open_cursor("table:poptable", null, "append");
for (PopRecord p : popData) {
cursor.putValueString(p.country);
cursor.putValueShort(p.year);
cursor.putValueLong(p.population);
ret = cursor.insert();
}
ret = cursor.close();
/* Update records in the table. */
cursor = session.open_cursor("table:poptable", null, null);
while ((ret = cursor.next()) == 0) {
recno = cursor.getKeyRecord();
country = cursor.getValueString();
year = cursor.getValueShort();
population = cursor.getValueLong();
cursor.putValueString(country);
cursor.putValueShort(year);
cursor.putValueLong(population + 1);
}
ret = cursor.close();
/* List the records in the table. */
cursor = session.open_cursor("table:poptable", null, null);
while ((ret = cursor.next()) == 0) {
recno = cursor.getKeyRecord();
country = cursor.getValueString();
year = cursor.getValueShort();
population = cursor.getValueLong();
System.out.print("ID " + recno);
System.out.println(": country " + country + ", year " + year +
", population " + population);
}
ret = cursor.close();