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.apache.hadoop.hbase.util.CollectionUtils;
32  import org.junit.Assert;
33  
34  import com.google.common.collect.Lists;
35  
36  public class TestRowDataSimple extends BaseTestRowData {
37  
38    static byte[]
39    // don't let the rows share any common prefix bytes
40        rowA = Bytes.toBytes("Arow"),
41        rowB = Bytes.toBytes("Brow"), cf = Bytes.toBytes("fam"),
42        cq0 = Bytes.toBytes("cq0"),
43        cq1 = Bytes.toBytes("cq1tail"),// make sure tail does not come back as liat
44        cq2 = Bytes.toBytes("dcq2"),// start with a different character
45        v0 = Bytes.toBytes("v0");
46  
47    static long ts = 55L;
48  
49    static List<KeyValue> d = Lists.newArrayList();
50    static {
51      d.add(new KeyValue(rowA, cf, cq0, ts, v0));
52      d.add(new KeyValue(rowA, cf, cq1, ts, v0));
53      d.add(new KeyValue(rowA, cf, cq2, ts, v0));
54      d.add(new KeyValue(rowB, cf, cq0, ts, v0));
55      d.add(new KeyValue(rowB, cf, cq1, ts, v0));
56      d.add(new KeyValue(rowB, cf, cq2, ts, v0));
57    }
58  
59    @Override
60    public List<KeyValue> getInputs() {
61      return d;
62    }
63  
64    @Override
65    public void individualSearcherAssertions(CellSearcher searcher) {
66      CellScannerPosition p;// reuse
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(3)));
80      Assert.assertTrue(CellComparator.equals(d.get(3), searcher.current()));
81  
82      Cell between4And5 = new KeyValue(rowB, cf, cq1, ts - 2, v0);
83  
84      // test exact
85      Assert.assertFalse(searcher.positionAt(between4And5));
86  
87      // test atOrBefore
88      p = searcher.positionAtOrBefore(between4And5);
89      Assert.assertEquals(CellScannerPosition.BEFORE, p);
90      Assert.assertTrue(CellComparator.equals(searcher.current(), d.get(4)));
91  
92      // test atOrAfter
93      p = searcher.positionAtOrAfter(between4And5);
94      Assert.assertEquals(CellScannerPosition.AFTER, p);
95      Assert.assertTrue(CellComparator.equals(searcher.current(), d.get(5)));
96  
97      // test when key falls before first key in block
98      Cell beforeFirst = new KeyValue(Bytes.toBytes("A"), cf, cq0, ts, v0);
99      Assert.assertFalse(searcher.positionAt(beforeFirst));
100     p = searcher.positionAtOrBefore(beforeFirst);
101     Assert.assertEquals(CellScannerPosition.BEFORE_FIRST, p);
102     p = searcher.positionAtOrAfter(beforeFirst);
103     Assert.assertEquals(CellScannerPosition.AFTER, p);
104     Assert.assertTrue(CellComparator.equals(searcher.current(), d.get(0)));
105     Assert.assertEquals(d.get(0), searcher.current());
106 
107     // test when key falls after last key in block
108     Cell afterLast = new KeyValue(Bytes.toBytes("z"), cf, cq0, ts, v0);// must be lower case z
109     Assert.assertFalse(searcher.positionAt(afterLast));
110     p = searcher.positionAtOrAfter(afterLast);
111     Assert.assertEquals(CellScannerPosition.AFTER_LAST, p);
112     p = searcher.positionAtOrBefore(afterLast);
113     Assert.assertEquals(CellScannerPosition.BEFORE, p);
114     Assert.assertTrue(CellComparator.equals(searcher.current(), CollectionUtils.getLast(d)));
115   }
116 
117 }