HBASE-26567 Remove IndexType from ChunkCreator (#3947)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / regionserver / TestSCVFWithMiniCluster.java
blob2688cbfc54a541a47febc520098484fb34b69408
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.regionserver;
20 import static junit.framework.Assert.assertEquals;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26 import org.apache.hadoop.hbase.CompareOperator;
27 import org.apache.hadoop.hbase.HBaseClassTestRule;
28 import org.apache.hadoop.hbase.HBaseTestingUtil;
29 import org.apache.hadoop.hbase.TableExistsException;
30 import org.apache.hadoop.hbase.TableName;
31 import org.apache.hadoop.hbase.TableNotFoundException;
32 import org.apache.hadoop.hbase.client.Admin;
33 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
34 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
35 import org.apache.hadoop.hbase.client.Durability;
36 import org.apache.hadoop.hbase.client.Put;
37 import org.apache.hadoop.hbase.client.Result;
38 import org.apache.hadoop.hbase.client.ResultScanner;
39 import org.apache.hadoop.hbase.client.Scan;
40 import org.apache.hadoop.hbase.client.Table;
41 import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
42 import org.apache.hadoop.hbase.filter.BinaryComparator;
43 import org.apache.hadoop.hbase.filter.Filter;
44 import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
45 import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
46 import org.apache.hadoop.hbase.testclassification.MediumTests;
47 import org.apache.hadoop.hbase.testclassification.RegionServerTests;
48 import org.apache.hadoop.hbase.util.Bytes;
49 import org.junit.AfterClass;
50 import org.junit.BeforeClass;
51 import org.junit.ClassRule;
52 import org.junit.Test;
53 import org.junit.experimental.categories.Category;
55 @Category({RegionServerTests.class, MediumTests.class})
57 * This test verifies that the scenarios illustrated by HBASE-10850 work
58 * w.r.t. essential column family optimization
60 public class TestSCVFWithMiniCluster {
62 @ClassRule
63 public static final HBaseClassTestRule CLASS_RULE =
64 HBaseClassTestRule.forClass(TestSCVFWithMiniCluster.class);
66 private static final TableName HBASE_TABLE_NAME = TableName.valueOf("TestSCVFWithMiniCluster");
68 private static final byte[] FAMILY_A = Bytes.toBytes("a");
69 private static final byte[] FAMILY_B = Bytes.toBytes("b");
71 private static final byte[] QUALIFIER_FOO = Bytes.toBytes("foo");
72 private static final byte[] QUALIFIER_BAR = Bytes.toBytes("bar");
74 private static Table htable;
76 private static Filter scanFilter;
78 private int expected = 1;
80 @BeforeClass
81 public static void setUp() throws Exception {
82 HBaseTestingUtil util = new HBaseTestingUtil();
84 util.startMiniCluster(1);
86 Admin admin = util.getAdmin();
87 destroy(admin, HBASE_TABLE_NAME);
88 create(admin, HBASE_TABLE_NAME, FAMILY_A, FAMILY_B);
89 admin.close();
90 htable = util.getConnection().getTable(HBASE_TABLE_NAME);
92 /* Add some values */
93 List<Put> puts = new ArrayList<>();
95 /* Add a row with 'a:foo' = false */
96 Put put = new Put(Bytes.toBytes("1"));
97 put.setDurability(Durability.SKIP_WAL);
98 put.addColumn(FAMILY_A, QUALIFIER_FOO, Bytes.toBytes("false"));
99 put.addColumn(FAMILY_A, QUALIFIER_BAR, Bytes.toBytes("_flag_"));
100 put.addColumn(FAMILY_B, QUALIFIER_FOO, Bytes.toBytes("_flag_"));
101 put.addColumn(FAMILY_B, QUALIFIER_BAR, Bytes.toBytes("_flag_"));
102 puts.add(put);
104 /* Add a row with 'a:foo' = true */
105 put = new Put(Bytes.toBytes("2"));
106 put.setDurability(Durability.SKIP_WAL);
107 put.addColumn(FAMILY_A, QUALIFIER_FOO, Bytes.toBytes("true"));
108 put.addColumn(FAMILY_A, QUALIFIER_BAR, Bytes.toBytes("_flag_"));
109 put.addColumn(FAMILY_B, QUALIFIER_FOO, Bytes.toBytes("_flag_"));
110 put.addColumn(FAMILY_B, QUALIFIER_BAR, Bytes.toBytes("_flag_"));
111 puts.add(put);
113 /* Add a row with 'a:foo' qualifier not set */
114 put = new Put(Bytes.toBytes("3"));
115 put.setDurability(Durability.SKIP_WAL);
116 put.addColumn(FAMILY_A, QUALIFIER_BAR, Bytes.toBytes("_flag_"));
117 put.addColumn(FAMILY_B, QUALIFIER_FOO, Bytes.toBytes("_flag_"));
118 put.addColumn(FAMILY_B, QUALIFIER_BAR, Bytes.toBytes("_flag_"));
119 puts.add(put);
121 htable.put(puts);
123 * We want to filter out from the scan all rows that do not have the column 'a:foo' with value
124 * 'false'. Only row with key '1' should be returned in the scan.
126 scanFilter = new SingleColumnValueFilter(FAMILY_A, QUALIFIER_FOO, CompareOperator.EQUAL,
127 new BinaryComparator(Bytes.toBytes("false")));
128 ((SingleColumnValueFilter) scanFilter).setFilterIfMissing(true);
131 @AfterClass
132 public static void tearDown() throws Exception {
133 htable.close();
136 private void verify(Scan scan) throws IOException {
137 ResultScanner scanner = htable.getScanner(scan);
138 Iterator<Result> it = scanner.iterator();
140 /* Then */
141 int count = 0;
142 try {
143 while (it.hasNext()) {
144 it.next();
145 count++;
147 } finally {
148 scanner.close();
150 assertEquals(expected, count);
153 * Test the filter by adding all columns of family A in the scan. (OK)
155 @Test
156 public void scanWithAllQualifiersOfFamiliyA() throws IOException {
157 /* Given */
158 Scan scan = new Scan();
159 scan.addFamily(FAMILY_A);
160 scan.setFilter(scanFilter);
162 verify(scan);
166 * Test the filter by adding all columns of family A and B in the scan. (KO: row '3' without
167 * 'a:foo' qualifier is returned)
169 @Test
170 public void scanWithAllQualifiersOfBothFamilies() throws IOException {
171 /* When */
172 Scan scan = new Scan();
173 scan.setFilter(scanFilter);
175 verify(scan);
179 * Test the filter by adding 2 columns of family A and 1 column of family B in the scan. (KO: row
180 * '3' without 'a:foo' qualifier is returned)
182 @Test
183 public void scanWithSpecificQualifiers1() throws IOException {
184 /* When */
185 Scan scan = new Scan();
186 scan.addColumn(FAMILY_A, QUALIFIER_FOO);
187 scan.addColumn(FAMILY_A, QUALIFIER_BAR);
188 scan.addColumn(FAMILY_B, QUALIFIER_BAR);
189 scan.addColumn(FAMILY_B, QUALIFIER_FOO);
190 scan.setFilter(scanFilter);
192 verify(scan);
196 * Test the filter by adding 1 column of family A (the one used in the filter) and 1 column of
197 * family B in the scan. (OK)
199 @Test
200 public void scanWithSpecificQualifiers2() throws IOException {
201 /* When */
202 Scan scan = new Scan();
203 scan.addColumn(FAMILY_A, QUALIFIER_FOO);
204 scan.addColumn(FAMILY_B, QUALIFIER_BAR);
205 scan.setFilter(scanFilter);
207 verify(scan);
211 * Test the filter by adding 2 columns of family A in the scan. (OK)
213 @Test
214 public void scanWithSpecificQualifiers3() throws IOException {
215 /* When */
216 Scan scan = new Scan();
217 scan.addColumn(FAMILY_A, QUALIFIER_FOO);
218 scan.addColumn(FAMILY_A, QUALIFIER_BAR);
219 scan.setFilter(scanFilter);
221 verify(scan);
224 private static void create(Admin admin, TableName tableName, byte[]... families)
225 throws IOException {
226 TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
227 for (byte[] family : families) {
228 ColumnFamilyDescriptor familyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(family)
229 .setMaxVersions(1).setCompressionType(Algorithm.GZ).build();
230 builder.setColumnFamily(familyDescriptor);
232 try {
233 admin.createTable(builder.build());
234 } catch (TableExistsException tee) {
235 /* Ignore */
239 private static void destroy(Admin admin, TableName tableName) throws IOException {
240 try {
241 admin.disableTable(tableName);
242 admin.deleteTable(tableName);
243 } catch (TableNotFoundException tnfe) {
244 /* Ignore */