示例#1
0
  /** Serialize the write. */
  private ByteBuffer serialize(
      final TableKey key, final TableValue value, final WriteOptions options) {
    int size;
    int optionSize = 0;
    byte[] optionBuf = null;
    byte[] keyData = key.serialize();
    byte[] valueData = value.getBytes();

    int maxCompressed = compressor.maxCompressedLength(valueData.length);
    byte[] compressed = new byte[maxCompressed];
    int actualCompressed =
        compressor.compress(valueData, 0, valueData.length, compressed, 0, maxCompressed);
    size =
        (Short.SIZE / 8)
            + 4 * (Integer.SIZE / 8)
            + (Long.SIZE / 8)
            + keyData.length
            + actualCompressed;

    if (options != null) {
      optionBuf = OptionsSerializer.getBytes(options);
      if (optionBuf != null) {
        optionSize = optionBuf.length;
      } else {
        optionSize = 0;
      }
      size += optionSize;
    }

    ByteBuffer buf = ByteBuffer.allocateDirect(size);
    buf.putLong(System.currentTimeMillis());
    buf.putShort(MAGIC_NUMBER); // Magic number.

    buf.putInt(keyData.length);
    buf.putInt(valueData.length);
    buf.putInt(actualCompressed);
    buf.putInt(optionSize);

    buf.put(keyData);
    buf.put(compressed, 0, actualCompressed);

    if (optionBuf != null) {
      buf.put(optionBuf);
    }

    return buf;
  }