1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.regionserver.querymatcher;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.hbase.Cell;
30 import org.apache.hadoop.hbase.HConstants;
31 import org.apache.hadoop.hbase.KeepDeletedCells;
32 import org.apache.hadoop.hbase.KeyValue;
33 import org.apache.hadoop.hbase.KeyValueUtil;
34 import org.apache.hadoop.hbase.regionserver.ScanInfo;
35 import org.apache.hadoop.hbase.regionserver.querymatcher.ScanQueryMatcher.MatchCode;
36 import org.apache.hadoop.hbase.testclassification.RegionServerTests;
37 import org.apache.hadoop.hbase.testclassification.SmallTests;
38 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
39 import org.junit.Test;
40 import org.junit.experimental.categories.Category;
41
42 @Category({ RegionServerTests.class, SmallTests.class })
43 public class TestUserScanQueryMatcher extends AbstractTestScanQueryMatcher {
44
45 private static final Log LOG = LogFactory.getLog(TestUserScanQueryMatcher.class);
46
47
48
49
50
51
52 @Test
53 public void testNeverIncludeFakeCell() throws IOException {
54 long now = EnvironmentEdgeManager.currentTime();
55
56 UserScanQueryMatcher qm = UserScanQueryMatcher.create(scan,
57 new ScanInfo(this.conf, fam2, 10, 1, ttl, KeepDeletedCells.FALSE, 0, rowComparator),
58 get.getFamilyMap().get(fam2), now - ttl, now, null);
59 Cell kv = new KeyValue(row1, fam2, col2, 1, data);
60 Cell cell = KeyValueUtil.createLastOnRowCol(kv);
61 qm.setToNewRow(kv);
62 MatchCode code = qm.match(cell);
63 assertFalse(code.compareTo(MatchCode.SEEK_NEXT_COL) != 0);
64 }
65
66 @Test
67 public void testMatchExplicitColumns() throws IOException {
68
69
70
71
72 List<MatchCode> expected = new ArrayList<ScanQueryMatcher.MatchCode>();
73 expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
74 expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL);
75 expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
76 expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL);
77 expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_ROW);
78 expected.add(ScanQueryMatcher.MatchCode.DONE);
79
80 long now = EnvironmentEdgeManager.currentTime();
81
82 UserScanQueryMatcher qm = UserScanQueryMatcher.create(scan,
83 new ScanInfo(this.conf, fam2, 0, 1, ttl, KeepDeletedCells.FALSE, 0, rowComparator),
84 get.getFamilyMap().get(fam2), now - ttl, now, null);
85
86 List<KeyValue> memstore = new ArrayList<KeyValue>();
87 memstore.add(new KeyValue(row1, fam2, col1, 1, data));
88 memstore.add(new KeyValue(row1, fam2, col2, 1, data));
89 memstore.add(new KeyValue(row1, fam2, col3, 1, data));
90 memstore.add(new KeyValue(row1, fam2, col4, 1, data));
91 memstore.add(new KeyValue(row1, fam2, col5, 1, data));
92
93 memstore.add(new KeyValue(row2, fam1, col1, data));
94
95 List<ScanQueryMatcher.MatchCode> actual = new ArrayList<ScanQueryMatcher.MatchCode>();
96 KeyValue k = memstore.get(0);
97 qm.setToNewRow(k);
98
99 for (KeyValue kv : memstore) {
100 actual.add(qm.match(kv));
101 }
102
103 assertEquals(expected.size(), actual.size());
104 for (int i = 0; i < expected.size(); i++) {
105 LOG.debug("expected " + expected.get(i) + ", actual " + actual.get(i));
106 assertEquals(expected.get(i), actual.get(i));
107 }
108 }
109
110 @Test
111 public void testMatch_Wildcard() throws IOException {
112
113
114
115
116 List<MatchCode> expected = new ArrayList<ScanQueryMatcher.MatchCode>();
117 expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
118 expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
119 expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
120 expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
121 expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
122 expected.add(ScanQueryMatcher.MatchCode.DONE);
123
124 long now = EnvironmentEdgeManager.currentTime();
125 UserScanQueryMatcher qm = UserScanQueryMatcher.create(scan,
126 new ScanInfo(this.conf, fam2, 0, 1, ttl, KeepDeletedCells.FALSE, 0, rowComparator), null,
127 now - ttl, now, null);
128
129 List<KeyValue> memstore = new ArrayList<KeyValue>();
130 memstore.add(new KeyValue(row1, fam2, col1, 1, data));
131 memstore.add(new KeyValue(row1, fam2, col2, 1, data));
132 memstore.add(new KeyValue(row1, fam2, col3, 1, data));
133 memstore.add(new KeyValue(row1, fam2, col4, 1, data));
134 memstore.add(new KeyValue(row1, fam2, col5, 1, data));
135 memstore.add(new KeyValue(row2, fam1, col1, 1, data));
136
137 List<ScanQueryMatcher.MatchCode> actual = new ArrayList<ScanQueryMatcher.MatchCode>();
138
139 KeyValue k = memstore.get(0);
140 qm.setToNewRow(k);
141
142 for (KeyValue kv : memstore) {
143 actual.add(qm.match(kv));
144 }
145
146 assertEquals(expected.size(), actual.size());
147 for (int i = 0; i < expected.size(); i++) {
148 LOG.debug("expected " + expected.get(i) + ", actual " + actual.get(i));
149 assertEquals(expected.get(i), actual.get(i));
150 }
151 }
152
153
154
155
156
157
158
159 @Test
160 public void testMatch_ExpiredExplicit() throws IOException {
161
162 long testTTL = 1000;
163 MatchCode[] expected = new MatchCode[] { ScanQueryMatcher.MatchCode.SEEK_NEXT_COL,
164 ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL,
165 ScanQueryMatcher.MatchCode.SEEK_NEXT_COL,
166 ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL,
167 ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW, ScanQueryMatcher.MatchCode.DONE };
168
169 long now = EnvironmentEdgeManager.currentTime();
170 UserScanQueryMatcher qm = UserScanQueryMatcher.create(scan,
171 new ScanInfo(this.conf, fam2, 0, 1, testTTL, KeepDeletedCells.FALSE, 0, rowComparator),
172 get.getFamilyMap().get(fam2), now - testTTL, now, null);
173
174 KeyValue[] kvs = new KeyValue[] { new KeyValue(row1, fam2, col1, now - 100, data),
175 new KeyValue(row1, fam2, col2, now - 50, data),
176 new KeyValue(row1, fam2, col3, now - 5000, data),
177 new KeyValue(row1, fam2, col4, now - 500, data),
178 new KeyValue(row1, fam2, col5, now - 10000, data),
179 new KeyValue(row2, fam1, col1, now - 10, data) };
180
181 KeyValue k = kvs[0];
182 qm.setToNewRow(k);
183
184 List<MatchCode> actual = new ArrayList<MatchCode>(kvs.length);
185 for (KeyValue kv : kvs) {
186 actual.add(qm.match(kv));
187 }
188
189 assertEquals(expected.length, actual.size());
190 for (int i = 0; i < expected.length; i++) {
191 LOG.debug("expected " + expected[i] + ", actual " + actual.get(i));
192 assertEquals(expected[i], actual.get(i));
193 }
194 }
195
196
197
198
199
200
201
202 @Test
203 public void testMatch_ExpiredWildcard() throws IOException {
204
205 long testTTL = 1000;
206 MatchCode[] expected = new MatchCode[] { ScanQueryMatcher.MatchCode.INCLUDE,
207 ScanQueryMatcher.MatchCode.INCLUDE, ScanQueryMatcher.MatchCode.SEEK_NEXT_COL,
208 ScanQueryMatcher.MatchCode.INCLUDE, ScanQueryMatcher.MatchCode.SEEK_NEXT_COL,
209 ScanQueryMatcher.MatchCode.DONE };
210
211 long now = EnvironmentEdgeManager.currentTime();
212 UserScanQueryMatcher qm = UserScanQueryMatcher.create(scan,
213 new ScanInfo(this.conf, fam2, 0, 1, testTTL, KeepDeletedCells.FALSE, 0, rowComparator), null,
214 now - testTTL, now, null);
215
216 KeyValue[] kvs = new KeyValue[] { new KeyValue(row1, fam2, col1, now - 100, data),
217 new KeyValue(row1, fam2, col2, now - 50, data),
218 new KeyValue(row1, fam2, col3, now - 5000, data),
219 new KeyValue(row1, fam2, col4, now - 500, data),
220 new KeyValue(row1, fam2, col5, now - 10000, data),
221 new KeyValue(row2, fam1, col1, now - 10, data) };
222 KeyValue k = kvs[0];
223 qm.setToNewRow(k);
224
225 List<ScanQueryMatcher.MatchCode> actual = new ArrayList<ScanQueryMatcher.MatchCode>(kvs.length);
226 for (KeyValue kv : kvs) {
227 actual.add(qm.match(kv));
228 }
229
230 assertEquals(expected.length, actual.size());
231 for (int i = 0; i < expected.length; i++) {
232 LOG.debug("expected " + expected[i] + ", actual " + actual.get(i));
233 assertEquals(expected[i], actual.get(i));
234 }
235 }
236 }