1 /**
2 * Copyright 2010 The Apache Software Foundation
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 package org.apache.hadoop.hbase.filter;
22
23 import org.apache.hadoop.hbase.KeyValue;
24
25 import java.util.ArrayList;
26
27 /**
28 * This filter is used to filter based on the column family. It takes an
29 * operator (equal, greater, not equal, etc) and a byte [] comparator for the
30 * column family portion of a key.
31 * <p/>
32 * This filter can be wrapped with {@link org.apache.hadoop.hbase.filter.WhileMatchFilter} and {@link org.apache.hadoop.hbase.filter.SkipFilter}
33 * to add more control.
34 * <p/>
35 * Multiple filters can be combined using {@link org.apache.hadoop.hbase.filter.FilterList}.
36 * <p/>
37 * If an already known column family is looked for, use {@link org.apache.hadoop.hbase.client.Get#addFamily(byte[])}
38 * directly rather than a filter.
39 */
40 public class FamilyFilter extends CompareFilter {
41 /**
42 * Writable constructor, do not use.
43 */
44 public FamilyFilter() {
45 }
46
47 /**
48 * Constructor.
49 *
50 * @param familyCompareOp the compare op for column family matching
51 * @param familyComparator the comparator for column family matching
52 */
53 public FamilyFilter(final CompareOp familyCompareOp,
54 final WritableByteArrayComparable familyComparator) {
55 super(familyCompareOp, familyComparator);
56 }
57
58 @Override
59 public ReturnCode filterKeyValue(KeyValue v) {
60 int familyLength = v.getFamilyLength();
61 if (familyLength > 0) {
62 if (doCompare(this.compareOp, this.comparator, v.getBuffer(),
63 v.getFamilyOffset(), familyLength)) {
64 return ReturnCode.SKIP;
65 }
66 }
67 return ReturnCode.INCLUDE;
68 }
69
70 public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
71 ArrayList arguments = CompareFilter.extractArguments(filterArguments);
72 CompareOp compareOp = (CompareOp)arguments.get(0);
73 WritableByteArrayComparable comparator = (WritableByteArrayComparable)arguments.get(1);
74 return new FamilyFilter(compareOp, comparator);
75 }
76 }