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.omid.transaction;
19  
20  import com.google.common.collect.ImmutableList;
21  import org.apache.hadoop.hbase.Cell;
22  import org.apache.hadoop.hbase.CellUtil;
23  import org.apache.hadoop.hbase.KeyValue;
24  import org.apache.hadoop.hbase.util.Bytes;
25  import org.mortbay.log.Log;
26  import org.testng.annotations.Test;
27  
28  import java.util.ArrayList;
29  import java.util.Arrays;
30  import java.util.Collection;
31  import java.util.List;
32  
33  import static org.testng.Assert.assertEquals;
34  
35  @Test(groups = "noHBase")
36  public class TestColumnIterator {
37  
38      final byte[] row = Bytes.toBytes("row");
39      private final byte[] family1 = Bytes.toBytes("f1");
40      private final byte[] family2 = Bytes.toBytes("f2");
41      private final byte[] qualifier1 = Bytes.toBytes("c1");
42      private final byte[] qualifier2 = Bytes.toBytes("c2");
43      final byte[] data = Bytes.toBytes("data");
44  
45      private final List<Cell> cells = new ArrayList<Cell>(
46              Arrays.asList(
47                      // Group 1 (3 elems but grouping should filter shadow cell, so check for 2)
48                      new KeyValue(row, family1, qualifier1, 0, data),
49                      new KeyValue(row, family1, qualifier1, 1, data),
50                      new KeyValue(row, family1, CellUtils.addShadowCellSuffix(qualifier1), 0, data),
51                      // Group 2 (2 elems but grouping should filter shadow cell, so check for 1)
52                      new KeyValue(row, family1, qualifier2, 0, data),
53                      new KeyValue(row, family1, CellUtils.addShadowCellSuffix(qualifier2), 0, data),
54                      // Group 3 (2 elems but grouping should filter shadow cell, so check for 1)
55                      new KeyValue(row, family2, qualifier1, 0, data),
56                      new KeyValue(row, family2, CellUtils.addShadowCellSuffix(qualifier1), 0, data)
57              )
58      );
59  
60      @Test
61      public void testGroupingCellsByColumnFilteringShadowCells() {
62  
63          ImmutableList<Collection<Cell>> groupedColumnsWithoutShadowCells =
64                  TTable.groupCellsByColumnFilteringShadowCells(cells);
65          Log.info("Column Groups " + groupedColumnsWithoutShadowCells);
66          assertEquals(groupedColumnsWithoutShadowCells.size(), 3, "Should be 3 column groups");
67          int group1Counter = 0;
68          int group2Counter = 0;
69          int group3Counter = 0;
70          for (Collection<Cell> columns : groupedColumnsWithoutShadowCells) {
71              for (Cell cell : columns) {
72                  byte[] cellFamily = CellUtil.cloneFamily(cell);
73                  byte[] cellQualifier = CellUtil.cloneQualifier(cell);
74                  // Group 1
75                  if (Bytes.equals(cellFamily, family1) &&
76                          Bytes.equals(cellQualifier, qualifier1)) {
77                      group1Counter++;
78                  }
79                  // Group 2
80                  if (Bytes.equals(cellFamily, family1) &&
81                          Bytes.equals(cellQualifier, qualifier2)) {
82                      group2Counter++;
83                  }
84                  // Group 3
85                  if (Bytes.equals(cellFamily, family2) &&
86                          Bytes.equals(cellQualifier, qualifier1)) {
87                      group3Counter++;
88                  }
89              }
90          }
91  
92          assertEquals(group1Counter, 2, "Group 1 should have 2 elems");
93          assertEquals(group2Counter, 1, "Group 2 should have 1 elems");
94          assertEquals(group3Counter, 1, "Group 3 should have 1 elems");
95      }
96  }