Top Description Inners Fields Constructors Methods
org.xerial.snappy.pool

public final Class CachingBufferPool

extends Object
implements BufferPool
Class Inheritance
All Implemented Interfaces
org.xerial.snappy.pool.BufferPool
Imports
java.lang.ref.SoftReference, java.nio.ByteBuffer, java.util.concurrent.ConcurrentHashMap, .ConcurrentLinkedDeque, .ConcurrentMap

A BufferPool implementation which caches values at fixed sizes.

Pooled instances are held as SoftReference to allow GC if necessary.

The current fixed sizes are calculated as follows:

Author
Brett Okken

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static interface

Field Summary

Modifier and TypeField and Description
private static final CachingBufferPool.IntFunction<byte[]>
private final ConcurrentMap<Integer, ConcurrentLinkedDeque<SoftReference<ByteBuffer>>>
private final ConcurrentMap<Integer, ConcurrentLinkedDeque<SoftReference<byte[]>>>
private static final CachingBufferPool.IntFunction<ByteBuffer>
private static final CachingBufferPool

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv static int
adjustSize(int size)

public byte[]
allocateArray(int
The minimum size array required. Must be >= 0.
size
)

Implements org.xerial.snappy.pool.BufferPool.allocateArray.

Returns a byte[] of size or greater length.

public ByteBuffer
allocateDirect(int
The minimum size buffer required. Must be >= 0.
size
)

Implements org.xerial.snappy.pool.BufferPool.allocateDirect.

Returns a direct ByteBuffer of size or greater capacity.

public static BufferPool

Returns:

instance of CachingBufferPool for using cached buffers.
getInstance
()

Returns instance of CachingBufferPool for using cached buffers.

private static <E> E
private static <E> ConcurrentLinkedDeque<SoftReference<E>>
public void
releaseArray(byte[]
Instance to return to pool. Must not be null. Must not be returned more than 1 time. Must not be used by caller after return.
buffer
)

Implements org.xerial.snappy.pool.BufferPool.releaseArray.

Returns instance to pool for potential future reuse.

public void
releaseDirect(ByteBuffer
Instance to return to pool. Must not be null. Must not be returned more than 1 time. Must not be used by caller after return.
buffer
)

Implements org.xerial.snappy.pool.BufferPool.releaseDirect.

Returns instance to pool for potential future reuse.

private static <E> void
private static int
roundToPowers(int number, int bits)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

ARRAY_FUNCTIONback to summary
private static final CachingBufferPool.IntFunction<byte[]> ARRAY_FUNCTION
buffersback to summary
private final ConcurrentMap<Integer, ConcurrentLinkedDeque<SoftReference<ByteBuffer>>> buffers
bytesback to summary
private final ConcurrentMap<Integer, ConcurrentLinkedDeque<SoftReference<byte[]>>> bytes
DBB_FUNCTIONback to summary
private static final CachingBufferPool.IntFunction<ByteBuffer> DBB_FUNCTION
INSTANCEback to summary
private static final CachingBufferPool INSTANCE

Constructor Detail

CachingBufferPoolback to summary
private CachingBufferPool()

Method Detail

adjustSizeback to summary
pack-priv static int adjustSize(int size)
allocateArrayback to summary
public byte[] allocateArray(int size)

Implements org.xerial.snappy.pool.BufferPool.allocateArray.

Doc from org.xerial.snappy.pool.BufferPool.allocateArray.

Returns a byte[] of size or greater length.

Parameters
size:int

The minimum size array required. Must be >= 0.

Returns:byte[]

A byte[] with length of at least size.

Annotations
@Override
allocateDirectback to summary
public ByteBuffer allocateDirect(int size)

Implements org.xerial.snappy.pool.BufferPool.allocateDirect.

Doc from org.xerial.snappy.pool.BufferPool.allocateDirect.

Returns a direct ByteBuffer of size or greater capacity.

Parameters
size:int

The minimum size buffer required. Must be >= 0.

Returns:ByteBuffer

A ByteBuffer of size or greater capacity.

Annotations
@Override
getInstanceback to summary
public static BufferPool getInstance()

Returns instance of CachingBufferPool for using cached buffers.

Returns:BufferPool

instance of CachingBufferPool for using cached buffers.

getOrCreateback to summary
private static <E> E getOrCreate(final int size, final ConcurrentMap<Integer, ConcurrentLinkedDeque<SoftReference<E>>> map, final CachingBufferPool.IntFunction<E> creator)
optimisticGetEntryback to summary
private static <E> ConcurrentLinkedDeque<SoftReference<E>> optimisticGetEntry(Integer key, ConcurrentMap<Integer, ConcurrentLinkedDeque<SoftReference<E>>> map)
releaseArrayback to summary
public void releaseArray(byte[] buffer)

Implements org.xerial.snappy.pool.BufferPool.releaseArray.

Doc from org.xerial.snappy.pool.BufferPool.releaseArray.

Returns instance to pool for potential future reuse.

Must not be returned more than 1 time. Must not be used by caller after return.

Parameters
buffer:byte[]

Instance to return to pool. Must not be null. Must not be returned more than 1 time. Must not be used by caller after return.

Annotations
@Override
releaseDirectback to summary
public void releaseDirect(ByteBuffer buffer)

Implements org.xerial.snappy.pool.BufferPool.releaseDirect.

Doc from org.xerial.snappy.pool.BufferPool.releaseDirect.

Returns instance to pool for potential future reuse.

Must not be returned more than 1 time. Must not be used by caller after return.

Parameters
buffer:ByteBuffer

Instance to return to pool. Must not be null. Must not be returned more than 1 time. Must not be used by caller after return.

Annotations
@Override
returnValueback to summary
private static <E> void returnValue(E value, Integer size, ConcurrentMap<Integer, ConcurrentLinkedDeque<SoftReference<E>>> map)
roundToPowersback to summary
private static int roundToPowers(int number, int bits)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.xerial.snappy.pool back to summary

private Interface CachingBufferPool.IntFunction<E>


Method Summary

Modifier and TypeMethod and Description
public E
create(int size)

Method Detail

createback to summary
public E create(int size)