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  
19  package org.apache.hadoop.hbase.codec.prefixtree.row.data;
20  
21  import java.io.IOException;
22  import java.util.List;
23  
24  import org.apache.hadoop.hbase.Cell;
25  import org.apache.hadoop.hbase.CellComparator;
26  import org.apache.hadoop.hbase.KeyValue;
27  import org.apache.hadoop.hbase.codec.prefixtree.row.BaseTestRowData;
28  import org.apache.hadoop.hbase.codec.prefixtree.scanner.CellScannerPosition;
29  import org.apache.hadoop.hbase.codec.prefixtree.scanner.CellSearcher;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.junit.Assert;
32  
33  import com.google.common.collect.Lists;
34  
35  public class TestRowDataSearcherRowMiss extends BaseTestRowData{
36  
37    static byte[]
38        //don't let the rows share any common prefix bytes
39        A = Bytes.toBytes("A"),
40        AA = Bytes.toBytes("AA"),
41        AAA = Bytes.toBytes("AAA"),
42        B = Bytes.toBytes("B"),
43        cf = Bytes.toBytes("fam"),
44        cq = Bytes.toBytes("cq0"),
45        v = Bytes.toBytes("v0");
46  
47    static long
48      ts = 55L;
49  
50    static List<KeyValue> d = Lists.newArrayList();
51    static{
52      d.add(new KeyValue(A, cf, cq, ts, v));
53      d.add(new KeyValue(AA, cf, cq, ts, v));
54      d.add(new KeyValue(AAA, cf, cq, ts, v));
55      d.add(new KeyValue(B, cf, cq, ts, v));
56    }
57  
58  	@Override
59  	public List<KeyValue> getInputs() {
60  		return d;
61  	}
62  
63  	@Override
64  	public void individualSearcherAssertions(CellSearcher searcher) {
65      assertRowOffsetsCorrect();
66  
67      searcher.resetToBeforeFirstEntry();
68  
69      //test first cell
70      try {
71        searcher.advance();
72      } catch (IOException e) {
73        throw new RuntimeException(e);
74      }
75      Cell first = searcher.current();
76      Assert.assertTrue(CellComparator.equals(d.get(0), first));
77  
78      //test first cell in second row
79      Assert.assertTrue(searcher.positionAt(d.get(1)));
80      Assert.assertTrue(CellComparator.equals(d.get(1), searcher.current()));
81  
82      testBetween1and2(searcher);
83      testBetween2and3(searcher);
84    }
85  
86  	/************ private methods, call from above *******************/
87  
88  	private void assertRowOffsetsCorrect(){
89  	  Assert.assertEquals(4, getRowStartIndexes().size());
90  	}
91  
92  	private void testBetween1and2(CellSearcher searcher){
93      CellScannerPosition p;//reuse
94      Cell betweenAAndAAA = new KeyValue(AA, cf, cq, ts-2, v);
95  
96      //test exact
97      Assert.assertFalse(searcher.positionAt(betweenAAndAAA));
98  
99      //test atOrBefore
100     p = searcher.positionAtOrBefore(betweenAAndAAA);
101     Assert.assertEquals(CellScannerPosition.BEFORE, p);
102     Assert.assertTrue(CellComparator.equals(searcher.current(), d.get(1)));
103 
104     //test atOrAfter
105     p = searcher.positionAtOrAfter(betweenAAndAAA);
106     Assert.assertEquals(CellScannerPosition.AFTER, p);
107     Assert.assertTrue(CellComparator.equals(searcher.current(), d.get(2)));
108 	}
109 
110   private void testBetween2and3(CellSearcher searcher){
111     CellScannerPosition p;//reuse
112     Cell betweenAAAndB = new KeyValue(AAA, cf, cq, ts-2, v);
113 
114     //test exact
115     Assert.assertFalse(searcher.positionAt(betweenAAAndB));
116 
117     //test atOrBefore
118     p = searcher.positionAtOrBefore(betweenAAAndB);
119     Assert.assertEquals(CellScannerPosition.BEFORE, p);
120     Assert.assertTrue(CellComparator.equals(searcher.current(), d.get(2)));
121 
122     //test atOrAfter
123     p = searcher.positionAtOrAfter(betweenAAAndB);
124     Assert.assertEquals(CellScannerPosition.AFTER, p);
125     Assert.assertTrue(CellComparator.equals(searcher.current(), d.get(3)));
126   }
127 
128 }