/*
 * Druid - a distributed column store.
 * Copyright 2012 - 2015 Metamarkets Group Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.druid.client.cache;

import com.google.common.primitives.Ints;
import net.jpountz.lz4.LZ4Compressor;
import net.jpountz.lz4.LZ4Decompressor;
import net.jpountz.lz4.LZ4Factory;
import net.spy.memcached.transcoders.SerializingTranscoder;

import java.nio.ByteBuffer;

public class LZ4Transcoder extends SerializingTranscoder
{

  private final LZ4Factory lz4Factory;

  public LZ4Transcoder()
  {
    super();
    lz4Factory = LZ4Factory.fastestJavaInstance();
  }

  public LZ4Transcoder(int max)
  {
    super(max);
    lz4Factory = LZ4Factory.fastestJavaInstance();
  }

  @Override
  protected byte[] compress(byte[] in)
  {
    if (in == null) {
      throw new NullPointerException("Can't compress null");
    }

    LZ4Compressor compressor = lz4Factory.fastCompressor();

    byte[] out = new byte[compressor.maxCompressedLength(in.length)];
    int compressedLength = compressor.compress(in, 0, in.length, out, 0);

    getLogger().debug("Compressed %d bytes to %d", in.length, compressedLength);

    return ByteBuffer.allocate(Ints.BYTES + compressedLength)
                     .putInt(in.length)
                     .put(out, 0, compressedLength)
                     .array();
  }

  @Override
  protected byte[] decompress(byte[] in)
  {
    byte[] out = null;
    if(in != null) {
      LZ4Decompressor decompressor = lz4Factory.decompressor();

      int size = ByteBuffer.wrap(in).getInt();

      out = new byte[size];
      decompressor.decompress(in, Ints.BYTES, out, 0, out.length);
    }
    return out == null ? null : out;
  }
}

@@ -833,16 +833,60 @@
 actory;%0A
+import net.jpountz.lz4.LZ4FastDecompressor;%0A
 import n
@@ -1064,16 +1064,47 @@
 4Factory
+ = LZ4Factory.fastestInstance()
 ;%0A%0A  pub
@@ -1144,59 +1144,8 @@
 ();%0A
-    lz4Factory = LZ4Factory.fastestJavaInstance();%0A
   %7D%0A
@@ -1201,59 +1201,8 @@
 x);%0A
-    lz4Factory = LZ4Factory.fastestJavaInstance();%0A
   %7D%0A
@@ -1935,16 +1935,20 @@
      LZ4
+Fast
 Decompre
@@ -1978,17 +1978,21 @@
 Factory.
-d
+fastD
 ecompres

Update LZ4Transcoder to match Compressed strategy factory type.