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