| 1 | <html> |
| 2 | <body> |
| 3 | Bindings that use sequences of primitive fields, or tuples. |
| 4 | |
| 5 | |
| 6 | <h2><a name="formats">Tuple Formats</a></h2> |
| 7 | |
| 8 | <p>The serialization format for tuple bindings are designed for compactness, |
| 9 | serialization speed and proper default sorting.</p> |
| 10 | |
| 11 | <p>When a format is used for database keys, it is important to use default |
| 12 | sorting for best performance. Although a custom comparator may be specified |
| 13 | for a {@link com.sleepycat.db.DatabaseConfig#setBtreeComparator database} or |
| 14 | {@link <a |
| 15 | href="{@docRoot}/com/sleepycat/persist/model/KeyField.html#comparable">entity |
| 16 | index</a>}, custom comparators often reduce performance because comparators are |
| 17 | called very frequently during Btree operations.</p> |
| 18 | |
| 19 | <p>For proper default sorting, the byte array of the stored format must be |
| 20 | designed so that a byte-by-byte unsigned comparison results in the natural sort |
| 21 | order, as defined by the {@link java.lang.Comparable#compareTo} method of the |
| 22 | data type. For example, the natural sort order for integers is the standard |
| 23 | mathematical definition, and is implemented by {@code Integer.compareTo}, |
| 24 | {@code Long.compareTo}, etc. This is called <em>default natural |
| 25 | sorting</em>.</p> |
| 26 | |
| 27 | <p>Although most tuple formats provide default natural sorting, not all of them |
| 28 | do. Certain formats do not provide default natural sorting for historical |
| 29 | reasons (see the discussion of packed integer and float formats below.) Other |
| 30 | formats sacrifice default natural sorting for other performance factors (see |
| 31 | the discussion of BigDecimal formats below.)</p> |
| 32 | |
| 33 | |
| 34 | <h3><a name="stringFormats">String Formats</a></h3> |
| 35 | |
| 36 | <p>All {@code String} formats support default natural sorting.</p> |
| 37 | |
| 38 | <p>Strings are stored as a byte array of UTF encoded characters, either where |
| 39 | the length must be known by the application, or the byte array is |
| 40 | zero-terminated. The UTF encoding is described below.</p> |
| 41 | <ul> |
| 42 | <li>Null strings are UTF encoded as { 0xFF }, which is not allowed in a |
| 43 | standard UTF encoding. This allows null strings, as distinct from empty or |
| 44 | zero length strings, to be represented. Using default sorting, null strings |
| 45 | will be ordered last. |
| 46 | </li> |
| 47 | <li>Zero (0x0000) character values are UTF encoded as non-zero values, and |
| 48 | therefore embedded zeros in the string are supported. The sequence { 0xC0, |
| 49 | 0x80 } is used to encode a zero character. This UTF encoding is the same one |
| 50 | used by the native Java UTF libraries and is called |
| 51 | <a href="http://download.oracle.com/javase/6/docs/api/java/io/DataInput.html#modified-utf-8">Modified UTF-8</a>. |
| 52 | However, this encoding of zero does impact the lexicographical ordering, and |
| 53 | zeros will not be sorted first (the natural order) or last. |
| 54 | </li> |
| 55 | <li>For all character values other than zero, the standard UTF encoding is |
| 56 | used, and the default sorting is the same as the Unicode lexicographical |
| 57 | character ordering. |
| 58 | </li> |
| 59 | </ul> |
| 60 | |
| 61 | <p>Binding classes and methods are provided for zero-terminated and |
| 62 | known-length {@code String} values.</p> |
| 63 | <ul> |
| 64 | <li>Single-value binding classes for zero-terminated {@code String} |
| 65 | values.</li> |
| 66 | <ul> |
| 67 | <li>{@link com.sleepycat.bind.tuple.StringBinding}</li> |
| 68 | </ul> |
| 69 | <li>Multi-value binding methods for zero-terminated and known-length {@code |
| 70 | String} values.</li> |
| 71 | <ul> |
| 72 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeString(String)}</li> |
| 73 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readString}</li> |
| 74 | <li>{@link com.sleepycat.bind.tuple.TupleInput#getStringByteLength}</li> |
| 75 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeString(char[])}</li> |
| 76 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readString(char[])}</li> |
| 77 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readString(int)}</li> |
| 78 | </ul> |
| 79 | </ul> |
| 80 | |
| 81 | <h3><a name="integerFormats">Integer Formats</a></h3> |
| 82 | |
| 83 | <h4>Fixed Size Integer Formats</h4> |
| 84 | |
| 85 | <p>All fixed size integer formats support default natural sorting.</p> |
| 86 | |
| 87 | <p>The size of the stored value depends on the type, and ranges (as one would |
| 88 | expect) from 1 byte for type {@code byte} and class {@code Byte}, to 8 bytes for |
| 89 | type {@code long} and class {@code Long}.</p> |
| 90 | |
| 91 | <p>Signed numbers are stored in the buffer in MSB (most significant byte first) |
| 92 | order with their sign bit (high-order bit) inverted to cause negative numbers |
| 93 | to be sorted first when comparing values as unsigned byte arrays, as done in a |
| 94 | database.</p> |
| 95 | |
| 96 | <ul> |
| 97 | <li>Single-value binding classes for signed, fixed size integers.</li> |
| 98 | <ul> |
| 99 | <li>{@link com.sleepycat.bind.tuple.ByteBinding}</li> |
| 100 | <li>{@link com.sleepycat.bind.tuple.ShortBinding}</li> |
| 101 | <li>{@link com.sleepycat.bind.tuple.IntegerBinding}</li> |
| 102 | <li>{@link com.sleepycat.bind.tuple.LongBinding}</li> |
| 103 | </ul> |
| 104 | <li>Multi-value binding methods for signed, fixed size integers.</li> |
| 105 | <ul> |
| 106 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeByte}</li> |
| 107 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readByte}</li> |
| 108 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeShort}</li> |
| 109 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readShort}</li> |
| 110 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeInt}</li> |
| 111 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readInt}</li> |
| 112 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeLong}</li> |
| 113 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readLong}</li> |
| 114 | </ul> |
| 115 | </ul> |
| 116 | |
| 117 | <p>Unsigned numbers, including characters, are stored in MSB order with no |
| 118 | change to their sign bit. Arrays of characters and unsigned bytes may also be |
| 119 | stored and may be treated as {@code String} values. For booleans, {@code true} |
| 120 | is stored as the unsigned byte value one and {@code false} as the unsigned byte |
| 121 | value zero.</p> |
| 122 | |
| 123 | <ul> |
| 124 | <li>Single-value binding classes for characters and booleans.</li> |
| 125 | <ul> |
| 126 | <li>{@link com.sleepycat.bind.tuple.BooleanBinding}</li> |
| 127 | <li>{@link com.sleepycat.bind.tuple.CharacterBinding}</li> |
| 128 | </ul> |
| 129 | <li>Multi-value binding methods for unsigned, fixed size integers, characters |
| 130 | and booleans.</li> |
| 131 | <ul> |
| 132 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeBoolean}</li> |
| 133 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readBoolean}</li> |
| 134 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeChar}</li> |
| 135 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readChar}</li> |
| 136 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeUnsignedByte}</li> |
| 137 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readUnsignedByte}</li> |
| 138 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeUnsignedShort}</li> |
| 139 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readUnsignedShort}</li> |
| 140 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeUnsignedInt}</li> |
| 141 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readUnsignedInt}</li> |
| 142 | </ul> |
| 143 | <li>Multi-value binding methods for character arrays and unsigned byte arrays |
| 144 | that may be treated as {@code String} values.</li> |
| 145 | <ul> |
| 146 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeChars(String)}</li> |
| 147 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readChars(int)}</li> |
| 148 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeChars(char[])}</li> |
| 149 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readChars(char[])}</li> |
| 150 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeBytes(String)}</li> |
| 151 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readBytes(int)}</li> |
| 152 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeBytes(char[])}</li> |
| 153 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readBytes(char[])}</li> |
| 154 | </ul> |
| 155 | </ul> |
| 156 | |
| 157 | <h4>Packed Integer Formats</h4> |
| 158 | |
| 159 | <p>The packed integer format stores integers with small absolute values in a |
| 160 | single byte. The size increases as the absolute value increases, up to a |
| 161 | maximum of 5 bytes for {@code int} values and 9 bytes for {@code long} |
| 162 | values.</p> |
| 163 | |
| 164 | <p>The packed integer format can be used for integer values between {@link |
| 165 | java.lang.Long#MIN_VALUE} and {@link java.lang.Long#MAX_VALUE}. However, |
| 166 | different bindings and methods are provided for type {@code int} and {@code |
| 167 | long}, to avoid unsafe casting from {@code long} to {@code int} when {@code |
| 168 | int} values are used.</p> |
| 169 | |
| 170 | <p>Because the same packed format is used for {@code int} and {@code long} |
| 171 | values, stored {@code int} values may be expanded to {@code long} values |
| 172 | without introducing a format incompatibility. In other words, you can treat |
| 173 | previously stored packed {@code int} values as packed {@code long} values.</p> |
| 174 | |
| 175 | <p>Packed integer formats come in two varieties: those that support default |
| 176 | natural sorting and those that don't. The formats of the two varieties are |
| 177 | incompatible. For new applications, the format that supports default natural |
| 178 | sorting should normally be used. There is no performance advantage to using |
| 179 | the unsorted format.</p> |
| 180 | |
| 181 | <p>The format with support for default natural sorting stores values in the |
| 182 | inclusive range [-119,120] in a single byte.</p> |
| 183 | <ul> |
| 184 | <li>Single-value binding classes for packed integers with default natural |
| 185 | sorting.</li> |
| 186 | <ul> |
| 187 | <li>{@link com.sleepycat.bind.tuple.SortedPackedIntegerBinding}</li> |
| 188 | <li>{@link com.sleepycat.bind.tuple.SortedPackedLongBinding}</li> |
| 189 | </ul> |
| 190 | <li>Multi-value binding methods for packed integers with default natural |
| 191 | sorting.</li> |
| 192 | <ul> |
| 193 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeSortedPackedInt}</li> |
| 194 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readSortedPackedInt}</li> |
| 195 | <li>{@link com.sleepycat.bind.tuple.TupleInput#getSortedPackedIntByteLength}</li> |
| 196 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeSortedPackedLong}</li> |
| 197 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readSortedPackedLong}</li> |
| 198 | <li>{@link com.sleepycat.bind.tuple.TupleInput#getSortedPackedLongByteLength}</li> |
| 199 | </ul> |
| 200 | </ul> |
| 201 | |
| 202 | <p>The unsorted packed integer format is an older, legacy format that is used |
| 203 | internally and supported for compatibility. It stores values in the inclusive |
| 204 | range [-119,119] in a single byte. Because default natural sorting is not |
| 205 | supported, this format should not be used for keys. However, it so happens |
| 206 | that packed integers in the inclusive range [0,630] are sorted correctly by |
| 207 | default, and this may be useful for some applications.</p> |
| 208 | <ul> |
| 209 | <li>Single-value binding classes for legacy, unsorted packed integers.</li> |
| 210 | <ul> |
| 211 | <li>{@link com.sleepycat.bind.tuple.PackedIntegerBinding}</li> |
| 212 | <li>{@link com.sleepycat.bind.tuple.PackedLongBinding}</li> |
| 213 | </ul> |
| 214 | <li>Multi-value binding methods for legacy, unsorted packed integers.</li> |
| 215 | <ul> |
| 216 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writePackedInt}</li> |
| 217 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readPackedInt}</li> |
| 218 | <li>{@link com.sleepycat.bind.tuple.TupleInput#getPackedIntByteLength}</li> |
| 219 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writePackedLong}</li> |
| 220 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readPackedLong}</li> |
| 221 | <li>{@link com.sleepycat.bind.tuple.TupleInput#getPackedLongByteLength}</li> |
| 222 | </ul> |
| 223 | </ul> |
| 224 | |
| 225 | <h4>BigInteger Formats</h4> |
| 226 | |
| 227 | <p>All {@code BigInteger} formats support default natural sorting.</p> |
| 228 | |
| 229 | <p>{@code BigInteger} values are variable length and are stored as signed |
| 230 | values with a preceding byte length. The length has the same sign as the |
| 231 | value, in order to support default natural sorting.</p> |
| 232 | |
| 233 | <p>The length is stored as a 2-byte (short), fixed size, signed integer. |
| 234 | Supported values are therefore limited to those with a byte array ({@link |
| 235 | java.math.BigInteger#toByteArray}) representation with a size of 0x7fff bytes |
| 236 | or less. The maximum {@code BigInteger} value is (2<sup>0x3fff7</sup> - 1) and |
| 237 | the minimum value is (-2<sup>0x3fff7</sup>).</p> |
| 238 | |
| 239 | <ul> |
| 240 | <li>Single-value binding classes for {@code BigInteger} values.</li> |
| 241 | <ul> |
| 242 | <li>{@link com.sleepycat.bind.tuple.BigIntegerBinding}</li> |
| 243 | </ul> |
| 244 | <li>Multi-value binding methods for {@code BigInteger} values.</li> |
| 245 | <ul> |
| 246 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeBigInteger}</li> |
| 247 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readBigInteger}</li> |
| 248 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#getBigIntegerByteLength}</li> |
| 249 | </ul> |
| 250 | </ul> |
| 251 | |
| 252 | <h3><a name="floatFormats">Floating Point Formats</a></h3> |
| 253 | |
| 254 | <p>Floats and doubles are stored in a fixed size, 4 and 8 byte format, |
| 255 | respectively. Floats and doubles are stored using two different |
| 256 | representations: a representation with default natural sorting, and an |
| 257 | unsorted, integer-bit (IEEE 754) representation. For new applications, the |
| 258 | format that supports default natural sorting should normally be used. There is |
| 259 | no performance advantage to using the unsorted format.</p> |
| 260 | |
| 261 | <p>For {@code float} values, <code>Float.floatToIntBits</code> and the following |
| 262 | bit manipulations are used to convert the signed float value to a |
| 263 | representation that is sorted correctly by default.</p> |
| 264 | <pre> |
| 265 | int intVal = Float.floatToIntBits(val); |
| 266 | intVal ^= (intVal < 0) ? 0xffffffff : 0x80000000; |
| 267 | </pre> |
| 268 | |
| 269 | <p>For {@code double} values, <code>Float.doubleToLongBits</code> and the |
| 270 | following bit manipulations are used to convert the signed double value to a |
| 271 | representation that is sorted correctly by default.</p> |
| 272 | <pre> |
| 273 | long longVal = Double.doubleToLongBits(val); |
| 274 | longVal ^= (longVal < 0) ? 0xffffffffffffffffL : 0x8000000000000000L; |
| 275 | </pre> |
| 276 | |
| 277 | <p>In both cases, the resulting {@code int} or {@code long} value is stored as |
| 278 | an unsigned value.</p> |
| 279 | |
| 280 | <ul> |
| 281 | <li>Single-value binding classes for {@code float} and {@code double} values |
| 282 | with default natural sorting.</li> |
| 283 | <ul> |
| 284 | <li>{@link com.sleepycat.bind.tuple.SortedFloatBinding}</li> |
| 285 | <li>{@link com.sleepycat.bind.tuple.SortedDoubleBinding}</li> |
| 286 | </ul> |
| 287 | <li>Multi-value binding methods for {@code float} and {@code double} values |
| 288 | with default natural sorting.</li> |
| 289 | <ul> |
| 290 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeSortedFloat}</li> |
| 291 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readSortedFloat}</li> |
| 292 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeSortedDouble}</li> |
| 293 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readSortedDouble}</li> |
| 294 | </ul> |
| 295 | </ul> |
| 296 | |
| 297 | <p>The unsorted floating point format is an older, legacy format that is |
| 298 | supported for compatibility. With this format, only zero and positive values |
| 299 | have default natural sorting; negative values do not.</p> |
| 300 | |
| 301 | <ul> |
| 302 | <li>Single-value binding classes for legacy, unsorted {@code float} and {@code |
| 303 | double} values.</li> |
| 304 | <ul> |
| 305 | <li>{@link com.sleepycat.bind.tuple.FloatBinding}</li> |
| 306 | <li>{@link com.sleepycat.bind.tuple.DoubleBinding}</li> |
| 307 | </ul> |
| 308 | <li>Multi-value binding methods for legacy, unsorted {@code float} and {@code |
| 309 | double} values.</li> |
| 310 | <ul> |
| 311 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeFloat}</li> |
| 312 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readFloat}</li> |
| 313 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeDouble}</li> |
| 314 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readDouble}</li> |
| 315 | </ul> |
| 316 | </ul> |
| 317 | |
| 318 | <h3><a name="bigDecimalFormats">BigDecimal Formats</a></h3> |
| 319 | |
| 320 | <p>{@code BigDecimal} values are stored using two different, variable length |
| 321 | representations: a representation that supports default natural sorting, and an |
| 322 | unsorted representation. Differences between the two formats are: |
| 323 | <ul> |
| 324 | <li>The {@code BigDecimal} format with default natural sorting should normally |
| 325 | be used for database keys.</li> |
| 326 | <ul> |
| 327 | <li>Default natural sorting <em>is</em> supported.</li> |
| 328 | <li>The stored value is around 3 bytes larger than the unsorted format, |
| 329 | more or less, and is a minimum of 8 bytes.</li> |
| 330 | <li>More computation is required for serialization than the unsorted |
| 331 | format.</li> |
| 332 | <li>Trailing zeros after the decimal place are stripped, meaning that |
| 333 | precision is not preserved.</li> |
| 334 | </ul> |
| 335 | <li>The unsorted {@code BigDecimal} format should normally be used for non-key |
| 336 | values.</li> |
| 337 | <ul> |
| 338 | <li>Default natural sorting is <em>not</em> supported.</li> |
| 339 | <li>The stored value is around 3 bytes smaller than the sorted format, more |
| 340 | or less, and is a minimum of 3 bytes.</li> |
| 341 | <li>Less computation is required for serialization than the sorted |
| 342 | format.</li> |
| 343 | <li>Trailing zeros after the decimal place are preserved, meaning that |
| 344 | precision is preserved.</li> |
| 345 | </ul> |
| 346 | </ul> |
| 347 | |
| 348 | <p>Both formats store the scale or exponent separately from the unscaled value, |
| 349 | and the stored size does not increase proportionally as the absolute value of |
| 350 | the scale or exponent increases.</p> |
| 351 | |
| 352 | <ul> |
| 353 | <li>Single-value binding classes for {@code BigDecimal} values with default |
| 354 | natural sorting.</li> |
| 355 | <ul> |
| 356 | <li>{@link com.sleepycat.bind.tuple.SortedBigDecimalBinding}</li> |
| 357 | </ul> |
| 358 | <li>Multi-value binding methods for {@code BigDecimal} values with default |
| 359 | natural sorting.</li> |
| 360 | <ul> |
| 361 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeSortedBigDecimal}</li> |
| 362 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#getSortedBigDecimalMaxByteLength}</li> |
| 363 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readSortedBigDecimal}</li> |
| 364 | <li>{@link com.sleepycat.bind.tuple.TupleInput#getSortedBigDecimalByteLength}</li> |
| 365 | </ul> |
| 366 | <li>Single-value binding classes for unsorted {@code BigDecimal} values.</li> |
| 367 | <ul> |
| 368 | <li>{@link com.sleepycat.bind.tuple.BigDecimalBinding}</li> |
| 369 | </ul> |
| 370 | <li>Multi-value binding methods for unsorted {@code BigDecimal} values.</li> |
| 371 | <ul> |
| 372 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#writeBigDecimal}</li> |
| 373 | <li>{@link com.sleepycat.bind.tuple.TupleOutput#getBigDecimalMaxByteLength}</li> |
| 374 | <li>{@link com.sleepycat.bind.tuple.TupleInput#readBigDecimal}</li> |
| 375 | <li>{@link com.sleepycat.bind.tuple.TupleInput#getBigDecimalByteLength}</li> |
| 376 | </ul> |
| 377 | </ul> |
| 378 | |
| 379 | </body> |
| 380 | </html> |