Index: src/java/org/apache/hadoop/hbase/client/HTablePool.java =================================================================== --- src/java/org/apache/hadoop/hbase/client/HTablePool.java (revision 0) +++ src/java/org/apache/hadoop/hbase/client/HTablePool.java (revision 0) @@ -0,0 +1,83 @@ +/** + * Copyright 2008 The Apache Software Foundation + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.hadoop.hbase.client; + +import java.io.IOException; +import java.util.NoSuchElementException; +import java.util.concurrent.ConcurrentLinkedQueue; + +/** + * A simple pool of HTable instances. + */ +public class HTablePool { + private final byte[] tableName; + private final ConcurrentLinkedQueue pool = + new ConcurrentLinkedQueue(); + private final int maxSize; + private volatile int currentSize; + + /** + * Constructor + * @param tableName the table name + */ + public HTablePool(byte[] tableName) { + this.tableName = tableName; + this.maxSize = 10; + } + + /** + * Constructor + * @param tableName the table name + * @param maxSize maximum pool size + */ + public HTablePool(byte[] tableName, int maxSize) { + this.tableName = tableName; + this.maxSize = maxSize; + } + + /** + * Get a HTable instance, possibly from the pool, if one is available. + * @return HTable a HTable instance + * @throws IOException + */ + public HTable get() throws IOException { + HTable table; + try { + table = pool.remove(); + currentSize--; + } catch (NoSuchElementException e) { + table = new HTable(tableName); + } + return table; + } + + /** + * Return a HTable instance to the pool. + * @param table a HTable instance + */ + public void put(HTable table) { + if (currentSize < maxSize) { + pool.add(table); + currentSize++; + } + } + +}