View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.filter;
19  
20  import org.apache.hadoop.hbase.SmallTests;
21  import org.junit.Assert;
22  import org.junit.Test;
23  import org.junit.experimental.categories.Category;
24  
25  @Category(SmallTests.class)
26  public class TestFuzzyRowFilter {
27    @Test
28    public void testSatisfies() {
29      Assert.assertEquals(FuzzyRowFilter.SatisfiesCode.NEXT_EXISTS,
30              FuzzyRowFilter.satisfies(new byte[]{1, (byte) -128, 0, 0, 1}, // row to check
31                                       new byte[]{1, 0, 1}, // fuzzy row
32                                       new byte[]{0, 1, 0})); // mask
33  
34      Assert.assertEquals(FuzzyRowFilter.SatisfiesCode.YES,
35              FuzzyRowFilter.satisfies(new byte[]{1, (byte) -128, 1, 0, 1},
36                                       new byte[]{1, 0, 1},
37                                       new byte[]{0, 1, 0}));
38  
39      Assert.assertEquals(FuzzyRowFilter.SatisfiesCode.NEXT_EXISTS,
40              FuzzyRowFilter.satisfies(new byte[]{1, (byte) -128, 2, 0, 1},
41                                       new byte[]{1, 0, 1},
42                                       new byte[]{0, 1, 0}));
43  
44      Assert.assertEquals(FuzzyRowFilter.SatisfiesCode.NO_NEXT,
45              FuzzyRowFilter.satisfies(new byte[]{2, 3, 1, 1, 1},
46                                       new byte[]{1, 0, 1},
47                                       new byte[]{0, 1, 0}));
48  
49      Assert.assertEquals(FuzzyRowFilter.SatisfiesCode.YES,
50              FuzzyRowFilter.satisfies(new byte[]{1, 2, 1, 3, 3},
51                                       new byte[]{1, 2, 0, 3},
52                                       new byte[]{0, 0, 1, 0}));
53  
54      Assert.assertEquals(FuzzyRowFilter.SatisfiesCode.NEXT_EXISTS,
55              FuzzyRowFilter.satisfies(new byte[]{1, 1, 1, 3, 0}, // row to check
56                                       new byte[]{1, 2, 0, 3}, // fuzzy row
57                                       new byte[]{0, 0, 1, 0})); // mask
58  
59      Assert.assertEquals(FuzzyRowFilter.SatisfiesCode.NEXT_EXISTS,
60              FuzzyRowFilter.satisfies(new byte[]{1, 1, 1, 3, 0},
61                                       new byte[]{1, (byte) 245, 0, 3},
62                                       new byte[]{0, 0, 1, 0}));
63  
64      Assert.assertEquals(FuzzyRowFilter.SatisfiesCode.NO_NEXT,
65              FuzzyRowFilter.satisfies(new byte[]{1, (byte) 245, 1, 3, 0},
66                                       new byte[]{1, 1, 0, 3},
67                                       new byte[]{0, 0, 1, 0}));
68  
69      Assert.assertEquals(FuzzyRowFilter.SatisfiesCode.NO_NEXT,
70              FuzzyRowFilter.satisfies(new byte[]{1, 3, 1, 3, 0},
71                                       new byte[]{1, 2, 0, 3},
72                                       new byte[]{0, 0, 1, 0}));
73  
74      Assert.assertEquals(FuzzyRowFilter.SatisfiesCode.NO_NEXT,
75              FuzzyRowFilter.satisfies(new byte[]{2, 1, 1, 1, 0},
76                                       new byte[]{1, 2, 0, 3},
77                                       new byte[]{0, 0, 1, 0}));
78  
79      Assert.assertEquals(FuzzyRowFilter.SatisfiesCode.NEXT_EXISTS,
80              FuzzyRowFilter.satisfies(new byte[]{1, 2, 1, 0, 1},
81                                       new byte[]{0, 1, 2},
82                                       new byte[]{1, 0, 0}));
83    }
84  
85    @Test
86    public void testGetNextForFuzzyRule() {
87      assertNext(
88              new byte[]{0, 1, 2}, // fuzzy row
89              new byte[]{1, 0, 0}, // mask
90              new byte[]{1, 2, 1, 0, 1}, // current
91              new byte[]{2, 1, 2, 0, 0}); // expected next
92  
93      assertNext(
94              new byte[]{0, 1, 2}, // fuzzy row
95              new byte[]{1, 0, 0}, // mask
96              new byte[]{1, 1, 2, 0, 1}, // current
97              new byte[]{1, 1, 2, 0, 2}); // expected next
98  
99      assertNext(
100             new byte[]{0, 1, 0, 2, 0}, // fuzzy row
101             new byte[]{1, 0, 1, 0, 1}, // mask
102             new byte[]{1, 0, 2, 0, 1}, // current
103             new byte[]{1, 1, 0, 2, 0}); // expected next
104 
105     assertNext(
106             new byte[]{1, 0, 1},
107             new byte[]{0, 1, 0},
108             new byte[]{1, (byte) 128, 2, 0, 1},
109             new byte[]{1, (byte) 129, 1, 0, 0});
110 
111     assertNext(
112             new byte[]{0, 1, 0, 1},
113             new byte[]{1, 0, 1, 0},
114             new byte[]{5, 1, 0, 1},
115             new byte[]{5, 1, 1, 1});
116 
117     assertNext(
118             new byte[]{0, 1, 0, 1},
119             new byte[]{1, 0, 1, 0},
120             new byte[]{5, 1, 0, 1, 1},
121             new byte[]{5, 1, 0, 1, 2});
122 
123     assertNext(
124             new byte[]{0, 1, 0, 0}, // fuzzy row
125             new byte[]{1, 0, 1, 1}, // mask
126             new byte[]{5, 1, (byte) 255, 1}, // current
127             new byte[]{5, 1, (byte) 255, 2}); // expected next
128 
129     assertNext(
130             new byte[]{0, 1, 0, 1}, // fuzzy row
131             new byte[]{1, 0, 1, 0}, // mask
132             new byte[]{5, 1, (byte) 255, 1}, // current
133             new byte[]{6, 1, 0, 1}); // expected next
134 
135     assertNext(
136             new byte[]{0, 1, 0, 1}, // fuzzy row
137             new byte[]{1, 0, 1, 0}, // mask
138             new byte[]{5, 1, (byte) 255, 0}, // current
139             new byte[]{5, 1, (byte) 255, 1}); // expected next
140 
141     assertNext(
142             new byte[]{5, 1, 1, 0},
143             new byte[]{0, 0, 1, 1},
144             new byte[]{5, 1, (byte) 255, 1},
145             new byte[]{5, 1, (byte) 255, 2});
146 
147     assertNext(
148             new byte[]{1, 1, 1, 1},
149             new byte[]{0, 0, 1, 1},
150             new byte[]{1, 1, 2, 2},
151             new byte[]{1, 1, 2, 3});
152 
153     assertNext(
154             new byte[]{1, 1, 1, 1},
155             new byte[]{0, 0, 1, 1},
156             new byte[]{1, 1, 3, 2},
157             new byte[]{1, 1, 3, 3});
158 
159     assertNext(
160             new byte[]{1, 1, 1, 1},
161             new byte[]{1, 1, 1, 1},
162             new byte[]{1, 1, 2, 3},
163             new byte[]{1, 1, 2, 4});
164 
165     assertNext(
166             new byte[]{1, 1, 1, 1},
167             new byte[]{1, 1, 1, 1},
168             new byte[]{1, 1, 3, 2},
169             new byte[]{1, 1, 3, 3});
170 
171     assertNext(
172             new byte[]{1, 1, 0, 0},
173             new byte[]{0, 0, 1, 1},
174             new byte[]{0, 1, 3, 2},
175             new byte[]{1, 1, 0, 0});
176 
177     // No next for this one
178     Assert.assertNull(FuzzyRowFilter.getNextForFuzzyRule(
179             new byte[]{2, 3, 1, 1, 1}, // row to check
180             new byte[]{1, 0, 1}, // fuzzy row
181             new byte[]{0, 1, 0})); // mask
182     Assert.assertNull(FuzzyRowFilter.getNextForFuzzyRule(
183             new byte[]{1, (byte) 245, 1, 3, 0},
184             new byte[]{1, 1, 0, 3},
185             new byte[]{0, 0, 1, 0}));
186     Assert.assertNull(FuzzyRowFilter.getNextForFuzzyRule(
187             new byte[]{1, 3, 1, 3, 0},
188             new byte[]{1, 2, 0, 3},
189             new byte[]{0, 0, 1, 0}));
190     Assert.assertNull(FuzzyRowFilter.getNextForFuzzyRule(
191             new byte[]{2, 1, 1, 1, 0},
192             new byte[]{1, 2, 0, 3},
193             new byte[]{0, 0, 1, 0}));
194   }
195 
196   private void assertNext(byte[] fuzzyRow, byte[] mask, byte[] current, byte[] expected) {
197     byte[] nextForFuzzyRule = FuzzyRowFilter.getNextForFuzzyRule(current, fuzzyRow, mask);
198     Assert.assertArrayEquals(expected, nextForFuzzyRule);
199   }
200 
201 }