/*
 * Druid - a distributed column store.
 * Copyright (C) 2012, 2013  Metamarkets Group Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package io.druid.offheap;

import com.google.common.base.Supplier;
import com.metamx.common.logger.Logger;
import io.druid.collections.StupidPool;

import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicLong;


public class OffheapBufferPool extends StupidPool<ByteBuffer>
{
  private static final Logger log = new Logger(OffheapBufferPool.class);

  public OffheapBufferPool(final int computationBufferSize)
  {
    super(
        new Supplier<ByteBuffer>()
        {
          final AtomicLong count = new AtomicLong(0);

          @Override
          public ByteBuffer get()
          {
            log.info(
                "Allocating new intermediate processing buffer[%,d] of size[%,d]",
                count.getAndIncrement(), computationBufferSize
            );
            return ByteBuffer.allocateDirect(computationBufferSize);
          }
        }
    );
  }
}

@@ -962,16 +962,101 @@
 dPool;%0A%0A
+import java.lang.reflect.InvocationTargetException;%0Aimport java.lang.reflect.Method;%0A
 import j
@@ -1262,16 +1262,779 @@
 lass);%0A%0A
+  private static final Method bufferCleaner;%0A  private static final Method clean;%0A%0A  static %7B%0A    try %7B%0A      bufferCleaner = Class.forName(%22java.nio.DirectByteBuffer%22).getMethod(%22cleaner%22);%0A      bufferCleaner.setAccessible(true);%0A      clean = Class.forName(%22sum.misc.Cleaner%22).getMethod(%22clean%22);%0A      clean.setAccessible(true);%0A    %7D catch(ClassNotFoundException %7C NoSuchMethodException e) %7B%0A      throw new RuntimeException(%22Unable to access ByteBuffer release methods%22);%0A    %7D%0A  %7D%0A%0A  private static void releaseBuffer(ByteBuffer buf) %7B%0A    try %7B%0A      Object cleaner = bufferCleaner.invoke(buf);%0A      clean.invoke(cleaner);%0A    %7D catch(IllegalAccessException %7C InvocationTargetException e) %7B%0A      log.error(e, %22Unable to release ByteBuffer%22);%0A    %7D%0A  %7D%0A%0A
   public

add code to release ByteBuffers