HBASE-4197 TestCoprocessorInterface demonstrates CustomScanner usage

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1158067 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Zhihong Yu 2011-08-15 23:57:45 +00:00
parent f68b180a9f
commit 4fae472fe1
1 changed files with 54 additions and 0 deletions

View File

@ -21,6 +21,8 @@
package org.apache.hadoop.hbase.coprocessor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -30,8 +32,11 @@ import org.apache.hadoop.hbase.HBaseTestCase;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost;
import org.apache.hadoop.hbase.regionserver.HRegion;
import org.apache.hadoop.hbase.regionserver.RegionScanner;
import org.apache.hadoop.hbase.regionserver.SplitTransaction;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.PairOfSameType;
@ -46,6 +51,41 @@ public class TestCoprocessorInterface extends HBaseTestCase {
private static final HBaseTestingUtility TEST_UTIL =
new HBaseTestingUtility();
private static class CustomScanner implements RegionScanner {
private RegionScanner delegate;
public CustomScanner(RegionScanner delegate) {
this.delegate = delegate;
}
@Override
public boolean next(List<KeyValue> results) throws IOException {
return delegate.next(results);
}
@Override
public boolean next(List<KeyValue> result, int limit) throws IOException {
return delegate.next(result, limit);
}
@Override
public void close() throws IOException {
delegate.close();
}
@Override
public HRegionInfo getRegionInfo() {
return delegate.getRegionInfo();
}
@Override
public boolean isFilterDone() {
return delegate.isFilterDone();
}
}
public static class CoprocessorImpl extends BaseRegionObserver {
private boolean startCalled;
@ -112,6 +152,12 @@ public class TestCoprocessorInterface extends HBaseTestCase {
postSplitCalled = true;
}
@Override
public RegionScanner postScannerOpen(final ObserverContext<RegionCoprocessorEnvironment> e,
final Scan scan, final RegionScanner s) throws IOException {
return new CustomScanner(s);
}
boolean wasStarted() {
return startCalled;
}
@ -160,6 +206,14 @@ public class TestCoprocessorInterface extends HBaseTestCase {
region.getLog().closeAndDelete();
Coprocessor c = region.getCoprocessorHost().
findCoprocessor(CoprocessorImpl.class.getName());
// HBASE-4197
Scan s = new Scan();
RegionScanner scanner = regions[0].getCoprocessorHost().postScannerOpen(s, regions[0].getScanner(s));
assertTrue(scanner instanceof CustomScanner);
// this would throw an exception before HBASE-4197
scanner.next(new ArrayList<KeyValue>());
assertTrue("Coprocessor not started", ((CoprocessorImpl)c).wasStarted());
assertTrue("Coprocessor not stopped", ((CoprocessorImpl)c).wasStopped());
assertTrue(((CoprocessorImpl)c).wasOpened());