public HBaseScanSpec parseTree() { HBaseScanSpec parsedSpec = le.accept(this, null); if (parsedSpec != null) { parsedSpec = mergeScanSpecs("booleanAnd", this.groupScan.getHBaseScanSpec(), parsedSpec); /* * If RowFilter is THE filter attached to the scan specification, * remove it since its effect is also achieved through startRow and stopRow. */ Filter parsedFilter = HBaseUtils.deserializeFilter(parsedSpec.filter); if (parsedFilter instanceof RowFilter && ((RowFilter)parsedFilter).getComparator() instanceof BinaryComparator) { parsedSpec.filter = null; } } return parsedSpec; }
protected Scan constructScan(byte[] valuePrefix) throws IOException { FilterList list = new FilterList(); Filter filter = new SingleColumnValueFilter( FAMILY_NAME, COLUMN_ZERO, CompareFilter.CompareOp.EQUAL, new BinaryComparator(valuePrefix) ); list.addFilter(filter); if(opts.filterAll) { list.addFilter(new FilterAllFilter()); } Scan scan = new Scan(); scan.setCaching(opts.caching); if (opts.addColumns) { scan.addColumn(FAMILY_NAME, QUALIFIER_NAME); } else { scan.addFamily(FAMILY_NAME); } scan.setFilter(list); return scan; }
@Test public void testJira6912() throws Exception { TableName TABLE = TableName.valueOf("testJira6912"); Table foo = TEST_UTIL.createTable(TABLE, new byte[][] {FAMILY}, 10); List<Put> puts = new ArrayList<Put>(); for (int i=0;i !=100; i++){ Put put = new Put(Bytes.toBytes(i)); put.add(FAMILY, FAMILY, Bytes.toBytes(i)); puts.add(put); } foo.put(puts); // If i comment this out it works TEST_UTIL.flush(); Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(1)); scan.setStopRow(Bytes.toBytes(3)); scan.addColumn(FAMILY, FAMILY); scan.setFilter(new RowFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes(1)))); ResultScanner scanner = foo.getScanner(scan); Result[] bar = scanner.next(100); assertEquals(1, bar.length); }
@Test public void testCheckAndPut_wrongRowInPut() throws IOException { TableName tableName = TableName.valueOf(name.getMethodName()); this.region = initHRegion(tableName, this.getName(), CONF, COLUMNS); try { Put put = new Put(row2); put.add(fam1, qual1, value1); try { region.checkAndMutate(row, fam1, qual1, CompareOp.EQUAL, new BinaryComparator(value2), put, false); fail(); } catch (org.apache.hadoop.hbase.DoNotRetryIOException expected) { // expected exception. } } finally { HRegion.closeHRegion(this.region); this.region = null; } }
public static Range[] fromFilter(SingleColumnValueFilter filter) { if (!(filter.getComparator() instanceof BinaryComparator)) { return new Range[0]; } byte[] column = KeyValue.makeColumn(filter.getFamily(), filter.getQualifier()); CompareOp compareOp = filter.getOperator(); byte[] value = filter.getComparator().getValue(); if (compareOp == CompareOp.NOT_EQUAL) { return new Range[] { new Range(column, null, CompareOp.NO_OP, value, CompareOp.LESS), new Range(column, value, CompareOp.GREATER, null, CompareOp.NO_OP) }; } else { switch (compareOp) { case EQUAL: case GREATER_OR_EQUAL: case GREATER: return new Range[] { new Range(column, value, compareOp, null, CompareOp.NO_OP) }; case LESS: case LESS_OR_EQUAL: return new Range[] { new Range(column, null, CompareOp.NO_OP, value, compareOp) }; default: return new Range[0]; } } }
/** * {@inheritDoc} */ @Override public boolean checkAndPut(final byte [] row, final byte [] family, final byte [] qualifier, final byte [] value, final Put put) throws IOException { RegionServerCallable<Boolean> callable = new RegionServerCallable<Boolean>(connection, getName(), row) { @Override public Boolean call(int callTimeout) throws IOException { PayloadCarryingRpcController controller = rpcControllerFactory.newController(); controller.setPriority(tableName); controller.setCallTimeout(callTimeout); try { MutateRequest request = RequestConverter.buildMutateRequest( getLocation().getRegionInfo().getRegionName(), row, family, qualifier, new BinaryComparator(value), CompareType.EQUAL, put); MutateResponse response = getStub().mutate(controller, request); return Boolean.valueOf(response.getProcessed()); } catch (ServiceException se) { throw ProtobufUtil.getRemoteException(se); } } }; return rpcCallerFactory.<Boolean> newCaller().callWithRetries(callable, this.operationTimeout); }
/** * {@inheritDoc} */ @Override public boolean checkAndDelete(final byte [] row, final byte [] family, final byte [] qualifier, final byte [] value, final Delete delete) throws IOException { RegionServerCallable<Boolean> callable = new RegionServerCallable<Boolean>(connection, getName(), row) { @Override public Boolean call(int callTimeout) throws IOException { PayloadCarryingRpcController controller = rpcControllerFactory.newController(); controller.setPriority(tableName); controller.setCallTimeout(callTimeout); try { MutateRequest request = RequestConverter.buildMutateRequest( getLocation().getRegionInfo().getRegionName(), row, family, qualifier, new BinaryComparator(value), CompareType.EQUAL, delete); MutateResponse response = getStub().mutate(controller, request); return Boolean.valueOf(response.getProcessed()); } catch (ServiceException se) { throw ProtobufUtil.getRemoteException(se); } } }; return rpcCallerFactory.<Boolean> newCaller().callWithRetries(callable, this.operationTimeout); }
public ByteArrayComparableModel( ByteArrayComparable comparator) { String typeName = comparator.getClass().getSimpleName(); ComparatorType type = ComparatorType.valueOf(typeName); this.type = typeName; switch (type) { case BinaryComparator: case BinaryPrefixComparator: this.value = Base64.encodeBytes(comparator.getValue()); break; case BitComparator: this.value = Base64.encodeBytes(comparator.getValue()); this.op = ((BitComparator)comparator).getOperator().toString(); break; case NullComparator: break; case RegexStringComparator: case SubstringComparator: this.value = Bytes.toString(comparator.getValue()); break; default: throw new RuntimeException("unhandled filter type: " + type); } }
public HBaseScanSpec parseTree() { HBaseScanSpec parsedSpec = le.accept(this, null); if (parsedSpec != null) { parsedSpec = mergeScanSpecs("booleanAnd", this.groupScan.getHBaseScanSpec(), parsedSpec); /* * If RowFilter is THE filter attached to the scan specification, * remove it since its effect is also achieved through startRow and stopRow. */ Filter filter = parsedSpec.getFilter(); if (filter instanceof RowFilter && ((RowFilter)filter).getOperator() != CompareOp.NOT_EQUAL && ((RowFilter)filter).getComparator() instanceof BinaryComparator) { parsedSpec = new HBaseScanSpec(parsedSpec.getTableName(), parsedSpec.getStartRow(), parsedSpec.getStopRow(), null); } } return parsedSpec; }
@Test public void testJira6912() throws Exception { byte [] TABLE = Bytes.toBytes("testJira6912"); HTable foo = TEST_UTIL.createTable(TABLE, new byte[][] {FAMILY}, 10); List<Put> puts = new ArrayList<Put>(); for (int i=0;i !=100; i++){ Put put = new Put(Bytes.toBytes(i)); put.add(FAMILY, FAMILY, Bytes.toBytes(i)); puts.add(put); } foo.put(puts); // If i comment this out it works TEST_UTIL.flush(); Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(1)); scan.setStopRow(Bytes.toBytes(3)); scan.addColumn(FAMILY, FAMILY); scan.setFilter(new RowFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes(1)))); ResultScanner scanner = foo.getScanner(scan); Result[] bar = scanner.next(100); assertEquals(1, bar.length); }
public void testCheckAndPut_wrongRowInPut() throws IOException { this.region = initHRegion(tableName, this.getName(), conf, COLUMNS); try { Put put = new Put(row2); put.add(fam1, qual1, value1); try { boolean res = region.checkAndMutate(row, fam1, qual1, CompareOp.EQUAL, new BinaryComparator(value2), put, null, false); fail(); } catch (DoNotRetryIOException expected) { // expected exception. } } finally { HRegion.closeHRegion(this.region); this.region = null; } }
public Iterable<Edge> getEdges(String key, Object value) { try { Scan scan = new Scan(Bytes.toBytes(this.graphName+"_"+key), this.getEndKey(Bytes.toBytes(this.graphName+"_"+key))); scan.addFamily(Bytes.toBytes("edge")); scan.setFilter(new ValueFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(String.valueOf(value))))); return new HbaseIterable<Edge>(scan, HbaseGraphUtils.PROPERTIESTABLENAME, hbaseConf) { @Override public Edge next(Result result) { for(Cell cell : result.listCells()) { String id = Bytes.toString(CellUtil.cloneQualifier(cell)); return new HbaseEdge(id, graphName, hbaseConf); } return null; } }; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } }
public Iterable<Vertex> getVertices(String key, Object value) { try { Scan scan = new Scan(Bytes.toBytes(this.graphName+"_"+key), this.getEndKey(Bytes.toBytes(this.graphName+"_"+key))); scan.addFamily(Bytes.toBytes("vertex")); scan.setFilter(new ValueFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(String.valueOf(value))))); return new HbaseIterable<Vertex>(scan, HbaseGraphUtils.PROPERTIESTABLENAME, hbaseConf) { @Override public Vertex next(Result result) { for(Cell cell : result.listCells()) { String id = Bytes.toString(CellUtil.cloneQualifier(cell)); return new HbaseVertex(id, graphName, hbaseConf); } return null; } }; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } }
@Override public FilterSupportStatus isFilterSupported( FilterAdapterContext context, QualifierFilter filter) { // For range filters, we only support a single family: if (!CompareOp.EQUAL.equals(filter.getOperator()) && filter.getComparator() instanceof BinaryComparator && context.getScan().numFamilies() != 1) { return SINGLE_FAMILY_REQUIRED; } // Support binary comparators and regex comparators with equal compare op: if ( !(filter.getComparator() instanceof BinaryComparator) && !isRegexAndSupported(filter)) { return UNSUPPORTED_COMPARABLE; } return FilterSupportStatus.SUPPORTED; }
@Test public void latestVersionOnlyComparisonsAreDone() throws IOException { byte[] filterValue = Bytes.toBytes("foobar"); byte[] qualifier = Bytes.toBytes("someColumn"); byte[] family = Bytes.toBytes("f"); SingleColumnValueFilter filter = new SingleColumnValueFilter( family, qualifier, CompareFilter.CompareOp.EQUAL, new BinaryComparator(filterValue)); filter.setFilterIfMissing(false); filter.setLatestVersionOnly(true); RowFilter adaptedFilter = adapter.adapt( new FilterAdapterContext(new Scan()), filter); assertFilterIfNotMIssingMatches( family, qualifier, filterValue, 1 /* latest version only = true */, adaptedFilter); }
@Test public void allVersionComparisonAreDone() throws IOException { byte[] filterValue = Bytes.toBytes("foobar"); byte[] qualifier = Bytes.toBytes("someColumn"); byte[] family = Bytes.toBytes("f"); SingleColumnValueFilter filter = new SingleColumnValueFilter( family, qualifier, CompareFilter.CompareOp.EQUAL, new BinaryComparator(filterValue)); filter.setFilterIfMissing(false); filter.setLatestVersionOnly(false); RowFilter adaptedFilter = adapter.adapt( new FilterAdapterContext(new Scan()), filter); assertFilterIfNotMIssingMatches( family, qualifier, filterValue, Integer.MAX_VALUE /* latest version only = false */, adaptedFilter); }
/** * {@inheritDoc} */ @Override public boolean checkAndMutate(final byte [] row, final byte [] family, final byte [] qualifier, final CompareOp compareOp, final byte [] value, final RowMutations rm) throws IOException { RegionServerCallable<Boolean> callable = new RegionServerCallable<Boolean>(connection, getName(), row) { @Override public Boolean call(int callTimeout) throws IOException { PayloadCarryingRpcController controller = rpcControllerFactory.newController(); controller.setPriority(tableName); controller.setCallTimeout(callTimeout); try { CompareType compareType = CompareType.valueOf(compareOp.name()); MultiRequest request = RequestConverter.buildMutateRequest( getLocation().getRegionInfo().getRegionName(), row, family, qualifier, new BinaryComparator(value), compareType, rm); ClientProtos.MultiResponse response = getStub().multi(controller, request); return Boolean.valueOf(response.getProcessed()); } catch (ServiceException se) { throw ProtobufUtil.getRemoteException(se); } } }; return rpcCallerFactory.<Boolean> newCaller().callWithRetries(callable, this.operationTimeout); }
/** * {@inheritDoc} */ @Override public boolean checkAndPut(final byte [] row, final byte [] family, final byte [] qualifier, final byte [] value, final Put put) throws IOException { RegionServerCallable<Boolean> callable = new RegionServerCallable<Boolean>(connection, getName(), row) { public Boolean call() throws IOException { try { MutateRequest request = RequestConverter.buildMutateRequest( getLocation().getRegionInfo().getRegionName(), row, family, qualifier, new BinaryComparator(value), CompareType.EQUAL, put); PayloadCarryingRpcController rpcController = rpcControllerFactory.newController(); rpcController.setPriority(getTableName()); MutateResponse response = getStub().mutate(rpcController, request); return Boolean.valueOf(response.getProcessed()); } catch (ServiceException se) { throw ProtobufUtil.getRemoteException(se); } } }; return rpcCallerFactory.<Boolean> newCaller().callWithRetries(callable, this.operationTimeout); }
/** * {@inheritDoc} */ @Override public boolean checkAndDelete(final byte [] row, final byte [] family, final byte [] qualifier, final byte [] value, final Delete delete) throws IOException { RegionServerCallable<Boolean> callable = new RegionServerCallable<Boolean>(connection, getName(), row) { public Boolean call() throws IOException { try { MutateRequest request = RequestConverter.buildMutateRequest( getLocation().getRegionInfo().getRegionName(), row, family, qualifier, new BinaryComparator(value), CompareType.EQUAL, delete); PayloadCarryingRpcController rpcController = rpcControllerFactory.newController(); rpcController.setPriority(getTableName()); MutateResponse response = getStub().mutate(rpcController, request); return Boolean.valueOf(response.getProcessed()); } catch (ServiceException se) { throw ProtobufUtil.getRemoteException(se); } } }; return rpcCallerFactory.<Boolean> newCaller().callWithRetries(callable, this.operationTimeout); }
protected void applyDefaultFilterList(Scan scan) { List<Filter> filters = new ArrayList<Filter>(); SingleColumnValueFilter filter1 = new SingleColumnValueFilter( ColumnFamilyNameBytes, QName1, CompareOp.GREATER, new BinaryComparator(Bytes.toBytes(1L))); filters.add(filter1); SingleColumnValueFilter filter2 = new SingleColumnValueFilter( ColumnFamilyNameBytes, QName2, CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(32L))); filters.add(filter2); FilterList filterList = new FilterList(filters); scan.setFilter(filterList); }
@Test public void testScanWithFilter() throws IOException { prepareScanData(TRANSACTION_COLUMNS); writeData(COLUMN, lastTs(prewriteTs), ANOTHER_VALUE); ValueFilter valueFilter = new ValueFilter(CompareOp.EQUAL, new BinaryComparator(ANOTHER_VALUE)); PrefixFilter prefixFilter = new PrefixFilter(ANOTHER_ROW); FilterList filterList = new FilterList(); filterList.addFilter(valueFilter); filterList.addFilter(prefixFilter); ThemisScanner scanner = prepareScanner(TRANSACTION_COLUMNS, filterList); checkAndCloseScanner(scanner); filterList = new FilterList(Operator.MUST_PASS_ONE); filterList.addFilter(valueFilter); filterList.addFilter(prefixFilter); scanner = prepareScanner(TRANSACTION_COLUMNS, filterList); checkScanRow(new ColumnCoordinate[]{COLUMN_WITH_ANOTHER_ROW}, scanner.next()); Assert.assertEquals(1, scanner.next().size()); checkAndCloseScanner(scanner); }
@Test public void testJira6912() throws Exception { final TableName tableName = TableName.valueOf(name.getMethodName()); Table foo = TEST_UTIL.createTable(tableName, new byte[][] {FAMILY}, 10); List<Put> puts = new ArrayList<Put>(); for (int i=0;i !=100; i++){ Put put = new Put(Bytes.toBytes(i)); put.addColumn(FAMILY, FAMILY, Bytes.toBytes(i)); puts.add(put); } foo.put(puts); // If i comment this out it works TEST_UTIL.flush(); Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(1)); scan.setStopRow(Bytes.toBytes(3)); scan.addColumn(FAMILY, FAMILY); scan.setFilter(new RowFilter(CompareOperator.NOT_EQUAL, new BinaryComparator(Bytes.toBytes(1)))); ResultScanner scanner = foo.getScanner(scan); Result[] bar = scanner.next(100); assertEquals(1, bar.length); }
@Test public void testScanWithColumnsAndFilterAndVersion() throws IOException { TableName tableName = TableName.valueOf(name.getMethodName()); try (Table table = TEST_UTIL.createTable(tableName, FAMILY, 4)) { for (int i = 0; i < 4; i++) { Put put = new Put(ROW); put.addColumn(FAMILY, QUALIFIER, VALUE); table.put(put); } Scan scan = new Scan(); scan.addColumn(FAMILY, QUALIFIER); scan.setFilter(new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(QUALIFIER))); scan.readVersions(3); try (ResultScanner scanner = table.getScanner(scan)) { Result result = scanner.next(); assertEquals(3, result.size()); } } }
@Test public void testCheckAndPut_wrongRowInPut() throws IOException { this.region = initHRegion(tableName, method, CONF, COLUMNS); try { Put put = new Put(row2); put.addColumn(fam1, qual1, value1); try { region.checkAndMutate(row, fam1, qual1, CompareOperator.EQUAL, new BinaryComparator(value2), put, false); fail(); } catch (org.apache.hadoop.hbase.DoNotRetryIOException expected) { // expected exception. } } finally { HBaseTestingUtility.closeRegionAndWAL(this.region); this.region = null; } }
protected Scan constructScan(byte[] valuePrefix) throws IOException { FilterList list = new FilterList(); Filter filter = new SingleColumnValueFilter( FAMILY_NAME, COLUMN_ZERO, CompareOperator.EQUAL, new BinaryComparator(valuePrefix) ); list.addFilter(filter); if(opts.filterAll) { list.addFilter(new FilterAllFilter()); } Scan scan = new Scan().setCaching(opts.caching).setCacheBlocks(opts.cacheBlocks) .setAsyncPrefetch(opts.asyncPrefetch).setReadType(opts.scanReadType); if (opts.addColumns) { scan.addColumn(FAMILY_NAME, QUALIFIER_NAME); } else { scan.addFamily(FAMILY_NAME); } scan.setFilter(list); return scan; }
private boolean doCheckAndPut(final byte [] row, final byte [] family, final byte [] qualifier, final String opName, final byte [] value, final Put put) throws IOException { ClientServiceCallable<Boolean> callable = new ClientServiceCallable<Boolean>(this.connection, getName(), row, this.rpcControllerFactory.newController(), put.getPriority()) { @Override protected Boolean rpcCall() throws Exception { CompareType compareType = CompareType.valueOf(opName); MutateRequest request = RequestConverter.buildMutateRequest( getLocation().getRegionInfo().getRegionName(), row, family, qualifier, new BinaryComparator(value), compareType, put); MutateResponse response = doMutate(request); return Boolean.valueOf(response.getProcessed()); } }; return rpcCallerFactory.<Boolean> newCaller(this.writeRpcTimeoutMs) .callWithRetries(callable, this.operationTimeoutMs); }