1 /*
2 * Copyright 2011 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.util;
22
23 import org.apache.hadoop.hbase.KeyValue;
24 import org.apache.hadoop.io.RawComparator;
25
26 public class CompoundBloomFilterBase implements BloomFilterBase {
27
28 /**
29 * At read time, the total number of chunks. At write time, the number of
30 * chunks created so far. The first chunk has an ID of 0, and the current
31 * chunk has the ID of numChunks - 1.
32 */
33 protected int numChunks;
34
35 /**
36 * The Bloom filter version. There used to be a DynamicByteBloomFilter which
37 * had version 2.
38 */
39 public static final int VERSION = 3;
40
41 /** Target error rate for configuring the filter and for information */
42 protected float errorRate;
43
44 /** The total number of keys in all chunks */
45 protected long totalKeyCount;
46 protected long totalByteSize;
47 protected long totalMaxKeys;
48
49 /** Hash function type to use, as defined in {@link Hash} */
50 protected int hashType;
51
52 /** Comparator used to compare Bloom filter keys */
53 protected RawComparator<byte[]> comparator;
54
55 @Override
56 public long getMaxKeys() {
57 return totalMaxKeys;
58 }
59
60 @Override
61 public long getKeyCount() {
62 return totalKeyCount;
63 }
64
65 @Override
66 public long getByteSize() {
67 return totalByteSize;
68 }
69
70 private static final byte[] DUMMY = new byte[0];
71
72 /**
73 * Prepare an ordered pair of row and qualifier to be compared using
74 * KeyValue.KeyComparator. This is only used for row-column Bloom
75 * filters.
76 */
77 @Override
78 public byte[] createBloomKey(byte[] row, int roffset, int rlength,
79 byte[] qualifier, int qoffset, int qlength) {
80 if (qualifier == null)
81 qualifier = DUMMY;
82
83 // Make sure this does not specify a timestamp so that the default maximum
84 // (most recent) timestamp is used.
85 KeyValue kv = KeyValue.createFirstOnRow(row, roffset, rlength, DUMMY, 0, 0,
86 qualifier, qoffset, qlength);
87 return kv.getKey();
88 }
89
90 @Override
91 public RawComparator<byte[]> getComparator() {
92 return comparator;
93 }
94
95 }