/** * Creates a FijiTablePoolBuilder with for the specified Fiji instance and the default options. * * @param fiji TableFactory to be used for constructing tables for the table pool. A fiji * instance is the normal source for this. */ FijiTablePoolBuilder(FijiTableFactory fiji) { mFijiTableFactory = fiji; mMinSize = DEFAULT_MIN_POOL_SIZE; mMaxSize = DEFAULT_MAX_POOL_SIZE; mIdleTimeout = DEFAULT_IDLE_TIMEOUT; mIdlePollPeriod = DEFAULT_IDLE_POLL_PERIOD; mClock = Clock.getDefaultClock(); }
/** * Constructor. * @param table The table connection to wrap. * @param pool The pool that this Connection is associated with. */ public PooledFijiTable(FijiTable table, Pool pool) { mTable = table; mPool = pool; mLastAccessTime = pool.getClock().getTime(); }
/** {@inheritDoc} */ @Override public void release() throws IOException { final int counter = mRetainCount.decrementAndGet(); Preconditions.checkState(counter >= 1, "Cannot release FijiTable %s that was already returned: retain counter is now %s.", getURI(), counter); if (counter == 1) { mLastAccessTime = mPool.getClock().getTime(); mPool.returnConnection(this); } }
/** * Cleans any connections from the pool that have been idle, while maintaining the minimum pool * size. * * @param idleTimeout Milliseconds idle required to be closed and * removed from the pool. */ public synchronized void clean(long idleTimeout) { long currentTime = mClock.getTime(); Iterator<PooledFijiTable> iterator = mConnections.iterator(); while (iterator.hasNext() && mPoolSize > mMinSize) { PooledFijiTable connection = iterator.next(); if (currentTime - connection.getLastAccessTime() > idleTimeout) { final int counter = connection.mRetainCount.decrementAndGet(); Preconditions.checkState(counter == 0, "Cannot clean up FijiTable %s: retain counter is %s.", connection.getURI(), counter); LOG.info("Closing idle PooledFijiTable connection to {}.", connection.getURI()); iterator.remove(); connection.releaseUnderlyingFijiTable(); mPoolSize--; } } }