/**
   * lz4 壓縮
   *
   * @param value
   * @return
   */
  public static byte[] lz4(byte[] value) {
    byte[] result = new byte[0];
    try {
      LZ4Factory factory = LZ4Factory.fastestInstance();
      LZ4Compressor compressor = factory.fastCompressor();
      //
      final int INTEGER_BYTES = 4;
      int length = value.length;
      int maxCompressedLength = compressor.maxCompressedLength(length);
      byte[] buff = new byte[INTEGER_BYTES + maxCompressedLength];
      // 用4個byte紀錄原始長度
      buff[0] = (byte) (length >>> 24);
      buff[1] = (byte) (length >>> 16);
      buff[2] = (byte) (length >>> 8);
      buff[3] = (byte) (length);

      // 壓縮後長度
      int written = compressor.compress(value, 0, length, buff, INTEGER_BYTES, maxCompressedLength);
      // 新長度=4+壓縮後長度
      int newLength = INTEGER_BYTES + written;
      result = new byte[newLength];
      System.arraycopy(buff, 0, result, 0, newLength);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return result;
  }
Пример #2
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;
  }