Index: src/test/org/apache/hadoop/hbase/filter/TestPrefixRowFilter.java =================================================================== --- src/test/org/apache/hadoop/hbase/filter/TestPrefixRowFilter.java (revision 0) +++ src/test/org/apache/hadoop/hbase/filter/TestPrefixRowFilter.java (revision 0) @@ -0,0 +1,103 @@ +/** + * Copyright 2007 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.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.UnsupportedEncodingException; +import java.util.Map; +import java.util.TreeMap; + +import junit.framework.TestCase; + +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.io.Cell; +import org.apache.hadoop.hbase.regionserver.HLogEdit; +import org.apache.hadoop.hbase.util.Bytes; + +/** + * Tests for a prefix row filter + */ +public class TestPrefixRowFilter extends TestCase { + RowFilterInterface mainFilter; + final char FIRST_CHAR = 'a'; + final char LAST_CHAR = 'e'; + final String HOST_PREFIX = "org.apache.site-"; + static byte [] GOOD_BYTES = null; + + static { + try { + GOOD_BYTES = "abc".getBytes(HConstants.UTF8_ENCODING); + } catch (UnsupportedEncodingException e) { + fail(); + } + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.mainFilter = new PrefixRowFilter(Bytes.toBytes(HOST_PREFIX)); + } + + /** + * Tests filtering using a regex on the row key + * @throws Exception + */ + public void testPrefixOnRow() throws Exception { + prefixRowTests(mainFilter); + } + + /** + * Test serialization + * @throws Exception + */ + public void testSerialization() throws Exception { + // Decompose mainFilter to bytes. + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + DataOutputStream out = new DataOutputStream(stream); + mainFilter.write(out); + out.close(); + byte[] buffer = stream.toByteArray(); + + // Recompose filter. + DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer)); + RowFilterInterface newFilter = new PrefixRowFilter(); + newFilter.readFields(in); + + // Ensure the serialization preserved the filter by running all test. + prefixRowTests(newFilter); + } + + private void prefixRowTests(RowFilterInterface filter) throws Exception { + for (char c = FIRST_CHAR; c <= LAST_CHAR; c++) { + byte [] t = createRow(c); + assertFalse("Failed with characer " + c, filter.filterRowKey(t)); + } + String yahooSite = "com.yahoo.www"; + assertTrue("Failed with character " + + yahooSite, filter.filterRowKey(Bytes.toBytes(yahooSite))); + } + + private byte [] createRow(final char c) { + return Bytes.toBytes(HOST_PREFIX + Character.toString(c)); + } +} Index: src/java/org/apache/hadoop/hbase/filter/PrefixRowFilter.java =================================================================== --- src/java/org/apache/hadoop/hbase/filter/PrefixRowFilter.java (revision 0) +++ src/java/org/apache/hadoop/hbase/filter/PrefixRowFilter.java (revision 0) @@ -0,0 +1,103 @@ +/** + * Copyright 2007 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.io.Cell; +import org.apache.hadoop.hbase.util.Bytes; + +/** + * RowFilterInterface that filters everything that does not match a prefix + */ +public class PrefixRowFilter implements RowFilterInterface { + + protected byte[] prefix; + + /** + * Constructor that takes a row prefix to filter on + */ + public PrefixRowFilter(byte[] prefix) { + this.prefix = prefix; + } + + /** + * Default Constructor, filters nothing. Required for RPC + * deserialization + */ + @SuppressWarnings("unused") + public PrefixRowFilter() { } + + @SuppressWarnings("unused") + public void reset() { + // Nothing to reset + } + + @SuppressWarnings("unused") + public void rowProcessed(boolean filtered, byte [] key) { + // does not care + } + + public boolean processAlways() { + return false; + } + + public boolean filterAllRemaining() { + return false; + } + + public boolean filterRowKey(final byte [] rowKey) { + if (rowKey == null) + return true; + if (rowKey.length < prefix.length) + return true; + for(int i = 0;i < prefix.length;i++) + if (prefix[i] != rowKey[i]) + return true; + return false; + } + + @SuppressWarnings("unused") + public boolean filterColumn(final byte [] rowKey, final byte [] colunmName, + final byte[] columnValue) { + return false; + } + + @SuppressWarnings("unused") + public boolean filterRow(final SortedMap columns) { + return false; + } + + @SuppressWarnings("unused") + public void validate(final byte [][] columns) { + // does not do this + } + + public void readFields(final DataInput in) throws IOException { + prefix = Bytes.readByteArray(in); + } + + public void write(final DataOutput out) throws IOException { + Bytes.writeByteArray(out, prefix); + } +} Index: build.xml =================================================================== --- build.xml (revision 735207) +++ build.xml (working copy) @@ -444,7 +444,7 @@ + includes="**/TestPrefixRowFilter.java" excludes="**/${test.exclude}.java" />