Index: src/test/java/org/apache/hadoop/hbase/filter/TestFilter.java =================================================================== --- src/test/java/org/apache/hadoop/hbase/filter/TestFilter.java (revision 1033775) +++ src/test/java/org/apache/hadoop/hbase/filter/TestFilter.java (working copy) @@ -1255,8 +1255,6 @@ " rows", expectedRows, i); } - - private void verifyScanNoEarlyOut(Scan s, long expectedRows, long expectedKeys) throws IOException { @@ -1314,7 +1312,43 @@ kvs.length, idx); } + private void verifyScanFullNoValues(Scan s, KeyValue [] kvs) + throws IOException { + InternalScanner scanner = this.region.getScanner(s); + List results = new ArrayList(); + int row = 0; + int idx = 0; + for (boolean done = true; done; row++) { + done = scanner.next(results); + Arrays.sort(results.toArray(new KeyValue[results.size()]), + KeyValue.COMPARATOR); + if(results.isEmpty()) break; + assertTrue("Scanned too many keys! Only expected " + kvs.length + + " total but already scanned " + (results.size() + idx) + + (results.isEmpty() ? "" : "(" + results.get(0).toString() + ")"), + kvs.length >= idx + results.size()); + for(KeyValue kv : results) { + LOG.info("row=" + row + ", result=" + kv.toString() + + ", match=" + kvs[idx].toString()); + assertTrue("Row mismatch", + Bytes.equals(kv.getRow(), kvs[idx].getRow())); + assertTrue("Family mismatch", + Bytes.equals(kv.getFamily(), kvs[idx].getFamily())); + assertTrue("Qualifier mismatch", + Bytes.equals(kv.getQualifier(), kvs[idx].getQualifier())); + assertFalse("Value match (expecting no value in result)", + Bytes.equals(kv.getValue(), kvs[idx].getValue())); + assertTrue("Value in result is not empty", kv.getValue().length == 0); + idx++; + } + results.clear(); + } + LOG.info("Looked at " + row + " rows with " + idx + " keys"); + assertEquals("Expected " + kvs.length + " total keys but scanned " + idx, + kvs.length, idx); + } + public void testColumnPaginationFilter() throws Exception { // Set of KVs (page: 1; pageSize: 1) - the first set of 1 column per row @@ -1406,4 +1440,61 @@ verifyScan(s, expectedRows, 0); this.verifyScanFull(s, expectedKVs4); } + + public void testKeyOnlyFilter() throws Exception { + + // KVs in first 6 rows + KeyValue [] expectedKVs = { + // testRowOne-0 + new KeyValue(ROWS_ONE[0], FAMILIES[0], QUALIFIERS_ONE[0], VALUES[0]), + new KeyValue(ROWS_ONE[0], FAMILIES[0], QUALIFIERS_ONE[2], VALUES[0]), + new KeyValue(ROWS_ONE[0], FAMILIES[0], QUALIFIERS_ONE[3], VALUES[0]), + new KeyValue(ROWS_ONE[0], FAMILIES[1], QUALIFIERS_ONE[0], VALUES[0]), + new KeyValue(ROWS_ONE[0], FAMILIES[1], QUALIFIERS_ONE[2], VALUES[0]), + new KeyValue(ROWS_ONE[0], FAMILIES[1], QUALIFIERS_ONE[3], VALUES[0]), + // testRowOne-2 + new KeyValue(ROWS_ONE[2], FAMILIES[0], QUALIFIERS_ONE[0], VALUES[0]), + new KeyValue(ROWS_ONE[2], FAMILIES[0], QUALIFIERS_ONE[2], VALUES[0]), + new KeyValue(ROWS_ONE[2], FAMILIES[0], QUALIFIERS_ONE[3], VALUES[0]), + new KeyValue(ROWS_ONE[2], FAMILIES[1], QUALIFIERS_ONE[0], VALUES[0]), + new KeyValue(ROWS_ONE[2], FAMILIES[1], QUALIFIERS_ONE[2], VALUES[0]), + new KeyValue(ROWS_ONE[2], FAMILIES[1], QUALIFIERS_ONE[3], VALUES[0]), + // testRowOne-3 + new KeyValue(ROWS_ONE[3], FAMILIES[0], QUALIFIERS_ONE[0], VALUES[0]), + new KeyValue(ROWS_ONE[3], FAMILIES[0], QUALIFIERS_ONE[2], VALUES[0]), + new KeyValue(ROWS_ONE[3], FAMILIES[0], QUALIFIERS_ONE[3], VALUES[0]), + new KeyValue(ROWS_ONE[3], FAMILIES[1], QUALIFIERS_ONE[0], VALUES[0]), + new KeyValue(ROWS_ONE[3], FAMILIES[1], QUALIFIERS_ONE[2], VALUES[0]), + new KeyValue(ROWS_ONE[3], FAMILIES[1], QUALIFIERS_ONE[3], VALUES[0]), + // testRowTwo-0 + new KeyValue(ROWS_TWO[0], FAMILIES[0], QUALIFIERS_TWO[0], VALUES[1]), + new KeyValue(ROWS_TWO[0], FAMILIES[0], QUALIFIERS_TWO[2], VALUES[1]), + new KeyValue(ROWS_TWO[0], FAMILIES[0], QUALIFIERS_TWO[3], VALUES[1]), + new KeyValue(ROWS_TWO[0], FAMILIES[1], QUALIFIERS_TWO[0], VALUES[1]), + new KeyValue(ROWS_TWO[0], FAMILIES[1], QUALIFIERS_TWO[2], VALUES[1]), + new KeyValue(ROWS_TWO[0], FAMILIES[1], QUALIFIERS_TWO[3], VALUES[1]), + // testRowTwo-2 + new KeyValue(ROWS_TWO[2], FAMILIES[0], QUALIFIERS_TWO[0], VALUES[1]), + new KeyValue(ROWS_TWO[2], FAMILIES[0], QUALIFIERS_TWO[2], VALUES[1]), + new KeyValue(ROWS_TWO[2], FAMILIES[0], QUALIFIERS_TWO[3], VALUES[1]), + new KeyValue(ROWS_TWO[2], FAMILIES[1], QUALIFIERS_TWO[0], VALUES[1]), + new KeyValue(ROWS_TWO[2], FAMILIES[1], QUALIFIERS_TWO[2], VALUES[1]), + new KeyValue(ROWS_TWO[2], FAMILIES[1], QUALIFIERS_TWO[3], VALUES[1]), + // testRowTwo-3 + new KeyValue(ROWS_TWO[3], FAMILIES[0], QUALIFIERS_TWO[0], VALUES[1]), + new KeyValue(ROWS_TWO[3], FAMILIES[0], QUALIFIERS_TWO[2], VALUES[1]), + new KeyValue(ROWS_TWO[3], FAMILIES[0], QUALIFIERS_TWO[3], VALUES[1]), + new KeyValue(ROWS_TWO[3], FAMILIES[1], QUALIFIERS_TWO[0], VALUES[1]), + new KeyValue(ROWS_TWO[3], FAMILIES[1], QUALIFIERS_TWO[2], VALUES[1]), + new KeyValue(ROWS_TWO[3], FAMILIES[1], QUALIFIERS_TWO[3], VALUES[1]) + }; + + // Grab all 6 rows + long expectedRows = 6; + long expectedKeys = this.colsPerRow; + Scan s = new Scan(); + s.setFilter(new KeyOnlyFilter()); + verifyScan(s, expectedRows, expectedKeys); + verifyScanFullNoValues(s, expectedKVs); + } } Index: src/test/java/org/apache/hadoop/hbase/TestKeyValue.java =================================================================== --- src/test/java/org/apache/hadoop/hbase/TestKeyValue.java (revision 1033775) +++ src/test/java/org/apache/hadoop/hbase/TestKeyValue.java (working copy) @@ -322,4 +322,34 @@ assertKVLess(c, kvA_1, lastOnRowA); assertKVLess(c, firstOnRowA, lastOnRowA); } + + public void testConvertToKeyOnly() throws Exception { + long ts = 1; + byte [] value = Bytes.toBytes("a real value"); + byte [] evalue = new byte[0]; // empty value + + // verify key with a non-empty value works + KeyValue kv1 = new KeyValue(rowA, family, qualA, ts, value); + KeyValue kv1ko = kv1.clone(); + assertTrue(kv1.equals(kv1ko)); + kv1ko.convertToKeyOnly(); + // keys are still the same + assertTrue(kv1.equals(kv1ko)); + // but values are not + assertTrue(kv1.getValue().length != 0); + assertTrue(kv1ko.getValue().length == 0); + + // verify key with an already-empty value works + KeyValue kv2 = new KeyValue(rowA, family, qualA, ts, evalue); + KeyValue kv2ko = kv2.clone(); + assertTrue(kv2.equals(kv2ko)); + kv2ko.convertToKeyOnly(); + // they should still be equal + assertTrue(kv2.equals(kv2ko)); + // but they should have different underlying byte arrays + assertFalse(kv2.getBuffer() == kv2ko.getBuffer()); + // both with 0 length values + assertTrue(kv2.getValue().length == 0); + assertTrue(kv2ko.getValue().length == 0); + } } Index: src/main/java/org/apache/hadoop/hbase/filter/KeyOnlyFilter.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/filter/KeyOnlyFilter.java (revision 0) +++ src/main/java/org/apache/hadoop/hbase/filter/KeyOnlyFilter.java (revision 0) @@ -0,0 +1,47 @@ +/* + * Copyright 2010 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 org.apache.hadoop.hbase.KeyValue; + +/** + * A filter that will only return the key component of each KV (the value will + * be rewritten as empty). + *

+ * This filter can be used to grab all of the keys without having to also grab + * the values. + */ +public class KeyOnlyFilter extends FilterBase { + + public KeyOnlyFilter() {} + + @Override + public ReturnCode filterKeyValue(KeyValue kv) { + kv.convertToKeyOnly(); + return ReturnCode.INCLUDE; + } + + public void write(DataOutput out) throws IOException {} + + public void readFields(DataInput in) throws IOException {} +} Index: src/main/java/org/apache/hadoop/hbase/KeyValue.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/KeyValue.java (revision 1033775) +++ src/main/java/org/apache/hadoop/hbase/KeyValue.java (working copy) @@ -1189,6 +1189,25 @@ } /** + * Converts this KeyValue to only contain the key portion (the value is + * changed to be null). This method does a full copy of the backing byte + * array and does not modify the original byte array of this KeyValue. + *

+ * This method is used by {@link KeyOnlyFilter} and is an advanced feature of + * KeyValue, proceed with caution. + */ + public void convertToKeyOnly() { + // KV format: + // Rebuild as: <0/4> + byte [] newBuffer = new byte[getKeyLength() + (2 * Bytes.SIZEOF_INT)]; + System.arraycopy(this.bytes, this.offset, newBuffer, 0, newBuffer.length); + Bytes.putInt(newBuffer, Bytes.SIZEOF_INT, 0); + this.bytes = newBuffer; + this.offset = 0; + this.length = newBuffer.length; + } + + /** * Splits a column in family:qualifier form into separate byte arrays. *

* Not recommend to be used as this is old-style API. Index: src/main/java/org/apache/hadoop/hbase/io/HbaseObjectWritable.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/io/HbaseObjectWritable.java (revision 1033775) +++ src/main/java/org/apache/hadoop/hbase/io/HbaseObjectWritable.java (working copy) @@ -61,6 +61,7 @@ import org.apache.hadoop.hbase.filter.DependentColumnFilter; import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter; import org.apache.hadoop.hbase.filter.InclusiveStopFilter; +import org.apache.hadoop.hbase.filter.KeyOnlyFilter; import org.apache.hadoop.hbase.filter.PageFilter; import org.apache.hadoop.hbase.filter.PrefixFilter; import org.apache.hadoop.hbase.filter.QualifierFilter; @@ -201,6 +202,8 @@ addToMap(Increment.class, code++); + addToMap(KeyOnlyFilter.class, code++); + } private Class declaredClass;