Index: src/java/org/apache/hadoop/hbase/filter/WritableByteArrayComparable.java =================================================================== --- src/java/org/apache/hadoop/hbase/filter/WritableByteArrayComparable.java (revision 0) +++ src/java/org/apache/hadoop/hbase/filter/WritableByteArrayComparable.java (revision 0) @@ -0,0 +1,13 @@ +/* + * $Id$ + * Created on Apr 17, 2008 + * + */ +package org.apache.hadoop.hbase.filter; + +import org.apache.hadoop.io.Writable; + +public interface WritableByteArrayComparable extends Writable, + Comparable { + // Not methods, just tie the two interfaces together. +} Property changes on: src/java/org/apache/hadoop/hbase/filter/WritableByteArrayComparable.java ___________________________________________________________________ Name: svn:keywords + Revision Date Id Index: src/java/org/apache/hadoop/hbase/filter/RangeRowFilter.java =================================================================== --- src/java/org/apache/hadoop/hbase/filter/RangeRowFilter.java (revision 0) +++ src/java/org/apache/hadoop/hbase/filter/RangeRowFilter.java (revision 0) @@ -0,0 +1,190 @@ +/** + * 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.filter; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.SortedMap; + +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.apache.hadoop.io.ObjectWritable; +import org.apache.hadoop.io.Text; + +/** + * Row filter which filters on column value ranges. Instances specify a + * columnName, comparison operator, and value. The filter accepts rows iff the + * column's value passes the comparison operator when compared with the filter's + * value. All comparisons are lexicographic. + */ +public class RangeRowFilter implements RowFilterInterface { + + /** Comparison operator. */ + public enum CompareOp { + LESS, LESS_OR_EQUAL, EQUAL, NOT_EQUAL, GREATER_OR_EQUAL, GREATER; + } + + private Text columnName; + private CompareOp compareOp; + private byte[] value; + private WritableByteArrayComparable comparator; + + RangeRowFilter() { + // for Writable + } + + /** + * Constructor. + * + * @param columnName + * name of column + * @param compareOp + * operator + * @param value + * value to compare column values against + */ + public RangeRowFilter(final String columnName, final CompareOp compareOp, + final byte[] value) { + this.columnName = new Text(columnName); + this.compareOp = compareOp; + this.value = value; + } + + /** + * Constructor. + * + * @param columnName + * name of column + * @param compareOp + * operator + * @param value + * value to compare column values against + */ + public RangeRowFilter(final String columnName, final CompareOp compareOp, + final WritableByteArrayComparable comparator) { + this.columnName = new Text(columnName); + this.compareOp = compareOp; + this.comparator = comparator; + } + + /** {@inheritDoc} */ + public boolean filterRowKey(final Text rowKey) { + return false; + } + + /** {@inheritDoc} */ + public boolean filterColumn(final Text rowKey, final Text colKey, + final byte[] data) { + if (!colKey.equals(columnName)) { + return false; + } + + int compareResult; + if (comparator != null) { + compareResult = comparator.compareTo(data); + } else { + compareResult = compare(value, data); + } + + switch (compareOp) { + case LESS: + return compareResult <= 0; + case LESS_OR_EQUAL: + return compareResult < 0; + case EQUAL: + return compareResult != 0; + case NOT_EQUAL: + return compareResult == 0; + case GREATER_OR_EQUAL: + return compareResult > 0; + case GREATER: + return compareResult >= 0; + default: + throw new RuntimeException("Unknown Compare op " + compareOp.name()); + } + } + + /** {@inheritDoc} */ + public boolean filterAllRemaining() { + return false; + } + + /** {@inheritDoc} */ + public boolean filterRow(final SortedMap columns) { + // Don't let rows through if they don't have the column we are checking + return !columns.containsKey(columnName); + } + + private int compare(final byte[] b1, final byte[] b2) { + int len = Math.min(b1.length, b2.length); + + for (int i = 0; i < len; i++) { + if (b1[i] != b2[i]) { + return b1[i] - b2[i]; + } + } + return b1.length - b2.length; + } + + /** {@inheritDoc} */ + public boolean processAlways() { + return false; + } + + /** {@inheritDoc} */ + public void reset() { + // Nothing. + } + + public void rowProcessed(final boolean filtered, final Text key) { + // Nothing + } + + /** {@inheritDoc} */ + public void validate(final Text[] columns) { + // Nothing + } + + /** {@inheritDoc} */ + public void readFields(final DataInput in) throws IOException { + int length = in.readInt(); + value = new byte[length]; + in.readFully(value); + columnName = new Text(in.readUTF()); + compareOp = CompareOp.valueOf(in.readUTF()); + comparator = (WritableByteArrayComparable) ObjectWritable.readObject(in, + new HBaseConfiguration()); + } + + /** {@inheritDoc} */ + public void write(final DataOutput out) throws IOException { + if (value == null) { + out.writeInt(0); + } else { + out.writeInt(value.length); + out.write(value); + } + out.writeUTF(columnName.toString()); + out.writeUTF(compareOp.name()); + ObjectWritable.writeObject(out, comparator, + WritableByteArrayComparable.class, new HBaseConfiguration()); + } + +} Property changes on: src/java/org/apache/hadoop/hbase/filter/RangeRowFilter.java ___________________________________________________________________ Name: svn:keywords + Revision Date Id