voldemort.utils.pool
Class KeyedResourcePool<K,V>

java.lang.Object
  extended by voldemort.utils.pool.KeyedResourcePool<K,V>

public class KeyedResourcePool<K,V>
extends java.lang.Object

A simple implementation of a per-key resource pool.


Constructor Summary
KeyedResourcePool(ResourceFactory<K,V> objectFactory, ResourcePoolConfig config)
           
 
Method Summary
 void checkin(K key, V resource)
          Check the given resource back into the pool
 V checkout(K key)
          Checkout a resource if one is immediately available.
 void close()
          Close the pool.
 void close(K key)
           
static
<K,V> KeyedResourcePool<K,V>
create(ResourceFactory<K,V> factory)
          Create a new pool using the defaults
static
<K,V> KeyedResourcePool<K,V>
create(ResourceFactory<K,V> factory, ResourcePoolConfig config)
          Create a new pool
 int getCheckedInResourceCount()
          Get the count of resources for all pools currently checkedin
 int getCheckedInResourcesCount(K k)
          Return the number of resources for the given key that are currently sitting idle in the pool waiting to be checked out.
 int getTotalResourceCount()
          Get the count of all resources for all pools
 int getTotalResourceCount(K k)
          Return the total number of resources for the given key whether they are currently checked in or checked out.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

KeyedResourcePool

public KeyedResourcePool(ResourceFactory<K,V> objectFactory,
                         ResourcePoolConfig config)
Method Detail

create

public static <K,V> KeyedResourcePool<K,V> create(ResourceFactory<K,V> factory,
                                                  ResourcePoolConfig config)
Create a new pool

Type Parameters:
K - The type of the keys
V - The type of the values
Parameters:
factory - The factory that creates objects
config - The pool config
Returns:
The created pool

create

public static <K,V> KeyedResourcePool<K,V> create(ResourceFactory<K,V> factory)
Create a new pool using the defaults

Type Parameters:
K - The type of the keys
V - The type of the values
Parameters:
factory - The factory that creates objects
Returns:
The created pool

checkout

public V checkout(K key)
           throws java.lang.Exception
Checkout a resource if one is immediately available. If none is available and we have created fewer than the max size resources, then create a new one. If no resources are available and we are already at the max size then block for up to the maximum time specified. When we hit the maximum time, if we still have not retrieved a resource throw a TimeOutException. This method is guaranteed to either fail or return a valid resource in the pool timeout + object creation time.

Parameters:
key - The key to checkout the resource for
Returns:
The resource
Throws:
java.lang.Exception

checkin

public void checkin(K key,
                    V resource)
             throws java.lang.Exception
Check the given resource back into the pool

Parameters:
key - The key for the resource
resource - The resource
Throws:
java.lang.Exception

close

public void close()
Close the pool. This will destroy all checked in resource immediately. Once closed all attempts to checkout a new resource will fail. All resources checked in after close is called will be immediately destroyed.


close

public void close(K key)

getTotalResourceCount

public int getTotalResourceCount(K k)
Return the total number of resources for the given key whether they are currently checked in or checked out.

Parameters:
k - The key
Returns:
The count

getTotalResourceCount

public int getTotalResourceCount()
Get the count of all resources for all pools

Returns:
The count of resources

getCheckedInResourcesCount

public int getCheckedInResourcesCount(K k)
Return the number of resources for the given key that are currently sitting idle in the pool waiting to be checked out.

Parameters:
k - The key
Returns:
The count

getCheckedInResourceCount

public int getCheckedInResourceCount()
Get the count of resources for all pools currently checkedin

Returns:
The count of resources


Jay Kreps, Roshan Sumbaly, Alex Feinberg, Bhupesh Bansal, Lei Gao