diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/RegionAsTable.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/RegionAsTable.java index f65bc5d..4462845 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/RegionAsTable.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/RegionAsTable.java @@ -22,18 +22,29 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.concurrent.ExecutorService; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; +import org.apache.hadoop.hbase.HBaseIOException; +import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.HRegionInfo; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Append; +import org.apache.hadoop.hbase.client.BufferedMutator; +import org.apache.hadoop.hbase.client.BufferedMutatorParams; +import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Durability; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.Increment; import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.RegionLocator; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Row; @@ -44,6 +55,8 @@ import org.apache.hadoop.hbase.client.coprocessor.Batch.Call; import org.apache.hadoop.hbase.client.coprocessor.Batch.Callback; import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel; +import org.apache.hadoop.hbase.security.User; +import org.apache.hadoop.hbase.util.FSUtils; import com.google.protobuf.Descriptors.MethodDescriptor; import com.google.protobuf.Message; @@ -61,6 +74,7 @@ import com.google.protobuf.ServiceException; * over the network. */ public class RegionAsTable implements Table { + private static final Log LOG = LogFactory.getLog(RegionAsTable.class); private final Region region; /** @@ -321,4 +335,117 @@ public class RegionAsTable implements Table { throws IOException { throw new UnsupportedOperationException(); } + + /** + * A faked Connection for {@link RegionAsTable}. Opens a region on instantiation. Configure with + * properties: hbase.RegionAsTableConnection.tableName and + * hbase.RegionAsTableConnection.columnFamilyNames. + */ + public static class RegionAsTableConnection implements Connection { + private final Configuration conf; + @SuppressWarnings("unused") + private final ExecutorService executorService; + @SuppressWarnings("unused") + private final User user; + private boolean aborted = false; + private boolean closed = false; + private final HRegion region; + private static final String DEFAULT_TABLENAME = "tableName"; + private static final String [] DEFAULT_COLUMNFAMILY_NAMES = new String [] {"columnFamilyName"}; + + public RegionAsTableConnection(Configuration conf, ExecutorService executorService, User user) + throws IOException { + this.conf = conf; + this.executorService = executorService; + this.user = user; + TableName tableName = + TableName.valueOf(this.conf.get("hbase.RegionAsTableConnection.tableName", + DEFAULT_TABLENAME)); + HTableDescriptor htd = new HTableDescriptor(tableName); + String [] columnFamilyNames = + this.conf.getStrings("hbase.RegionAsTableConnection.columnFamilyNames", + DEFAULT_COLUMNFAMILY_NAMES); + for (String name: columnFamilyNames) { + htd.addFamily(new HColumnDescriptor(name)); + } + HRegionInfo hri = new HRegionInfo(tableName); + this.region = HRegion.openHRegion(FSUtils.getRootDir(this.conf), hri, htd, null, this.conf); + } + + @Override + public void abort(String why, Throwable e) { + LOG.info("Aborting because " + e.getMessage()); + try { + this.region.close(true); + } catch (IOException ioe) { + LOG.error("Failed close on abort", ioe); + } + this.aborted = true; + } + + @Override + public boolean isAborted() { + return this.aborted; + } + + @Override + public Configuration getConfiguration() { + return this.conf; + } + + @Override + public Table getTable(TableName tableName) throws IOException { + if (isClosed()) throw new ConnectionClosedException(); + return new RegionAsTable(this.region); + } + + @Override + public Table getTable(TableName tableName, ExecutorService pool) throws IOException { + if (isClosed()) throw new ConnectionClosedException(); + return new RegionAsTable(this.region); + } + + @Override + public BufferedMutator getBufferedMutator(TableName tableName) throws IOException { + if (isClosed()) throw new ConnectionClosedException(); + // TODO Auto-generated method stub + return null; + } + + @Override + public BufferedMutator getBufferedMutator(BufferedMutatorParams params) throws IOException { + if (isClosed()) throw new ConnectionClosedException(); + // TODO Auto-generated method stub + return null; + } + + @Override + public RegionLocator getRegionLocator(TableName tableName) throws IOException { + if (isClosed()) throw new ConnectionClosedException(); + // TODO Auto-generated method stub + return null; + } + + @Override + public Admin getAdmin() throws IOException { + if (isClosed()) throw new ConnectionClosedException(); + // TODO Auto-generated method stub + return null; + } + + @Override + public void close() throws IOException { + this.region.close(); + this.closed = true; + } + + @Override + public boolean isClosed() { + return this.closed; + } + } + + public static class ConnectionClosedException extends HBaseIOException { + private static final long serialVersionUID = 4466598286149150975L; + } } \ No newline at end of file