1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.filter;
21
22 import java.io.DataInput;
23 import java.io.DataOutput;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.apache.hadoop.hbase.HBaseTestingUtility;
29 import org.apache.hadoop.hbase.HColumnDescriptor;
30 import org.apache.hadoop.hbase.HRegionInfo;
31 import org.apache.hadoop.hbase.HTableDescriptor;
32 import org.apache.hadoop.hbase.KeyValue;
33 import org.apache.hadoop.hbase.SmallTests;
34 import org.apache.hadoop.hbase.client.Get;
35 import org.apache.hadoop.hbase.client.Put;
36 import org.apache.hadoop.hbase.client.Scan;
37 import org.apache.hadoop.hbase.regionserver.HRegion;
38 import org.apache.hadoop.hbase.regionserver.InternalScanner;
39 import org.apache.hadoop.hbase.regionserver.wal.HLog;
40 import org.apache.hadoop.hbase.util.Bytes;
41 import org.junit.After;
42 import org.junit.Assert;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.experimental.categories.Category;
46
47
48
49
50
51 @Category(SmallTests.class)
52 public class TestInvocationRecordFilter {
53
54 private static final byte[] TABLE_NAME_BYTES = Bytes
55 .toBytes("invocationrecord");
56 private static final byte[] FAMILY_NAME_BYTES = Bytes.toBytes("mycf");
57
58 private static final byte[] ROW_BYTES = Bytes.toBytes("row");
59 private static final String QUALIFIER_PREFIX = "qualifier";
60 private static final String VALUE_PREFIX = "value";
61
62 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
63 private HRegion region;
64
65 @Before
66 public void setUp() throws Exception {
67 HTableDescriptor htd = new HTableDescriptor(TABLE_NAME_BYTES);
68 htd.addFamily(new HColumnDescriptor(FAMILY_NAME_BYTES));
69 HRegionInfo info = new HRegionInfo(TABLE_NAME_BYTES, null, null, false);
70 this.region = HRegion.createHRegion(info, TEST_UTIL.getDataTestDir(),
71 TEST_UTIL.getConfiguration(), htd);
72
73 Put put = new Put(ROW_BYTES);
74 for (int i = 0; i < 10; i += 2) {
75
76 put.add(FAMILY_NAME_BYTES, Bytes.toBytes(QUALIFIER_PREFIX + i), i,
77 Bytes.toBytes(VALUE_PREFIX + i));
78 }
79 this.region.put(put);
80 this.region.flushcache();
81 }
82
83 @Test
84 public void testFilterInvocation() throws Exception {
85 List<Integer> selectQualifiers = new ArrayList<Integer>();
86 List<Integer> expectedQualifiers = new ArrayList<Integer>();
87
88 selectQualifiers.add(-1);
89 verifyInvocationResults(selectQualifiers.toArray(new Integer[0]),
90 expectedQualifiers.toArray(new Integer[0]));
91
92 selectQualifiers.clear();
93
94 selectQualifiers.add(0);
95 expectedQualifiers.add(0);
96 verifyInvocationResults(selectQualifiers.toArray(new Integer[0]),
97 expectedQualifiers.toArray(new Integer[0]));
98
99 selectQualifiers.add(3);
100 verifyInvocationResults(selectQualifiers.toArray(new Integer[0]),
101 expectedQualifiers.toArray(new Integer[0]));
102
103 selectQualifiers.add(4);
104 expectedQualifiers.add(4);
105 verifyInvocationResults(selectQualifiers.toArray(new Integer[0]),
106 expectedQualifiers.toArray(new Integer[0]));
107
108 selectQualifiers.add(5);
109 verifyInvocationResults(selectQualifiers.toArray(new Integer[0]),
110 expectedQualifiers.toArray(new Integer[0]));
111
112 selectQualifiers.add(8);
113 expectedQualifiers.add(8);
114 verifyInvocationResults(selectQualifiers.toArray(new Integer[0]),
115 expectedQualifiers.toArray(new Integer[0]));
116 }
117
118 public void verifyInvocationResults(Integer[] selectQualifiers,
119 Integer[] expectedQualifiers) throws Exception {
120 Get get = new Get(ROW_BYTES);
121 for (int i = 0; i < selectQualifiers.length; i++) {
122 get.addColumn(FAMILY_NAME_BYTES,
123 Bytes.toBytes(QUALIFIER_PREFIX + selectQualifiers[i]));
124 }
125
126 get.setFilter(new InvocationRecordFilter());
127
128 List<KeyValue> expectedValues = new ArrayList<KeyValue>();
129 for (int i = 0; i < expectedQualifiers.length; i++) {
130 expectedValues.add(new KeyValue(ROW_BYTES, FAMILY_NAME_BYTES, Bytes
131 .toBytes(QUALIFIER_PREFIX + expectedQualifiers[i]),
132 expectedQualifiers[i], Bytes.toBytes(VALUE_PREFIX
133 + expectedQualifiers[i])));
134 }
135
136 Scan scan = new Scan(get);
137 List<KeyValue> actualValues = new ArrayList<KeyValue>();
138 List<KeyValue> temp = new ArrayList<KeyValue>();
139 InternalScanner scanner = this.region.getScanner(scan);
140 while (scanner.next(temp)) {
141 actualValues.addAll(temp);
142 temp.clear();
143 }
144 actualValues.addAll(temp);
145 Assert.assertTrue("Actual values " + actualValues
146 + " differ from the expected values:" + expectedValues,
147 expectedValues.equals(actualValues));
148 }
149
150 @After
151 public void tearDown() throws Exception {
152 HLog hlog = region.getLog();
153 region.close();
154 hlog.closeAndDelete();
155 }
156
157
158
159
160 private static class InvocationRecordFilter extends FilterBase {
161
162 private List<KeyValue> visitedKeyValues = new ArrayList<KeyValue>();
163
164 public void reset() {
165 visitedKeyValues.clear();
166 }
167
168 public ReturnCode filterKeyValue(KeyValue ignored) {
169 visitedKeyValues.add(ignored);
170 return ReturnCode.INCLUDE;
171 }
172
173 public void filterRow(List<KeyValue> kvs) {
174 kvs.clear();
175 kvs.addAll(visitedKeyValues);
176 }
177
178 public boolean hasFilterRow() {
179 return true;
180 }
181
182 @Override
183 public void readFields(DataInput arg0) throws IOException {
184
185 }
186
187 @Override
188 public void write(DataOutput arg0) throws IOException {
189
190 }
191 }
192 }