|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * OBKV HBase Client Framework |
| 4 | + * %% |
| 5 | + * Copyright (C) 2025 OceanBase Group |
| 6 | + * %% |
| 7 | + * OBKV HBase Client Framework is licensed under Mulan PSL v2. |
| 8 | + * You can use this software according to the terms and conditions of the Mulan PSL v2. |
| 9 | + * You may obtain a copy of Mulan PSL v2 at: |
| 10 | + * http://license.coscl.org.cn/MulanPSL2 |
| 11 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, |
| 12 | + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, |
| 13 | + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. |
| 14 | + * See the Mulan PSL v2 for more details. |
| 15 | + * #L% |
| 16 | + */ |
| 17 | + |
| 18 | +package com.alipay.oceanbase.hbase.secondary; |
| 19 | + |
| 20 | +import com.alipay.oceanbase.hbase.OHTableClient; |
| 21 | +import com.alipay.oceanbase.hbase.util.ObHTableTestUtil; |
| 22 | +import org.apache.hadoop.hbase.Cell; |
| 23 | +import org.apache.hadoop.hbase.client.*; |
| 24 | +import org.apache.hadoop.hbase.filter.BinaryComparator; |
| 25 | +import org.apache.hadoop.hbase.filter.CompareFilter; |
| 26 | +import org.apache.hadoop.hbase.filter.ValueFilter; |
| 27 | +import org.junit.*; |
| 28 | + |
| 29 | +import java.util.*; |
| 30 | + |
| 31 | +import static com.alipay.oceanbase.hbase.constants.OHConstants.HBASE_HTABLE_QUERY_HOT_ONLY; |
| 32 | +import static com.alipay.oceanbase.hbase.util.ObHTableSecondaryPartUtil.*; |
| 33 | +import static com.alipay.oceanbase.hbase.util.ObHTableTestUtil.FOR_EACH; |
| 34 | +import static com.alipay.oceanbase.hbase.util.TableTemplateManager.*; |
| 35 | +import static org.apache.hadoop.hbase.util.Bytes.toBytes; |
| 36 | + |
| 37 | + |
| 38 | +public class OHTableShareStorageSeriesTest { |
| 39 | + private static List<String> tableNames = new LinkedList<String>(); |
| 40 | + private static Map<String, List<String>> group2tableNames = new LinkedHashMap<>(); |
| 41 | + |
| 42 | + |
| 43 | + @BeforeClass |
| 44 | + public static void before() throws Exception { |
| 45 | + openDistributedExecute(); |
| 46 | + for (TableType type : NORMAL_SERIES_PARTITIONED_TABLES) { |
| 47 | + createTables(type, tableNames, group2tableNames, true); |
| 48 | + } |
| 49 | + for (TableType type : NORMAL_SERIES_PARTITIONED_TABLES) { |
| 50 | + alterTables(type, tableNames, group2tableNames, true); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @AfterClass |
| 55 | + public static void finish() throws Exception { |
| 56 | + closeDistributedExecute(); |
| 57 | + } |
| 58 | + |
| 59 | + @Before |
| 60 | + public void prepareCase() throws Exception { |
| 61 | + truncateTables(tableNames, group2tableNames); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + public static void testGetImpl(String tableName) throws Exception { |
| 66 | + OHTableClient hTable = ObHTableTestUtil.newOHTableClient(getTableName(tableName)); |
| 67 | + hTable.init(); |
| 68 | + |
| 69 | + // 0. prepare data - 100 key |
| 70 | + long recordCount = 100; |
| 71 | + String family = getColumnFamilyName(tableName); |
| 72 | + String key = "Key"; |
| 73 | + String column = "Column"; |
| 74 | + String value = "Value"; |
| 75 | + long curTs = System.currentTimeMillis(); |
| 76 | + for (int i = 0; i < recordCount; i++) { |
| 77 | + Put put = new Put(toBytes(key + i)); |
| 78 | + put.add(family.getBytes(), (column + i).getBytes(), curTs, toBytes(value + i)); |
| 79 | + hTable.put(put); |
| 80 | + } |
| 81 | + |
| 82 | + // 1. get, expect less than 100 key |
| 83 | + long getCount = 0; |
| 84 | + for (int i = 0; i < recordCount; i++) { |
| 85 | + Get get = new Get((key + i).getBytes()); |
| 86 | + get.setAttribute(HBASE_HTABLE_QUERY_HOT_ONLY, "true".getBytes()); |
| 87 | + get.addColumn(family.getBytes(), (column + i).getBytes()); |
| 88 | + Result r = hTable.get(get); |
| 89 | + Cell cells[] = r.rawCells(); |
| 90 | + getCount += cells.length; |
| 91 | + } |
| 92 | + Assert.assertTrue(getCount < recordCount); |
| 93 | + |
| 94 | + hTable.close(); |
| 95 | + } |
| 96 | + |
| 97 | + public static void testScanImpl(String tableName) throws Exception { |
| 98 | + OHTableClient hTable = ObHTableTestUtil.newOHTableClient(getTableName(tableName)); |
| 99 | + hTable.init(); |
| 100 | + |
| 101 | + // 0. prepare data - 100 key |
| 102 | + long recordCount = 100; |
| 103 | + String family = getColumnFamilyName(tableName); |
| 104 | + String key = "Key"; |
| 105 | + String column = "Column"; |
| 106 | + String value = "Value"; |
| 107 | + long curTs = System.currentTimeMillis(); |
| 108 | + for (int i = 0; i < recordCount; i++) { |
| 109 | + Put put = new Put(toBytes(key + i)); |
| 110 | + put.add(family.getBytes(), (column).getBytes(), curTs, toBytes(value + i)); |
| 111 | + hTable.put(put); |
| 112 | + } |
| 113 | + |
| 114 | + // 1. scan not specify column |
| 115 | + { |
| 116 | + Scan scan = new Scan(key.getBytes(), (key+recordCount).getBytes()); |
| 117 | + scan.addFamily(family.getBytes()); |
| 118 | + ResultScanner scanner = hTable.getScanner(scan); |
| 119 | + int count = 0; |
| 120 | + for (Result result : scanner) { |
| 121 | + for (Cell cell : result.rawCells()) { |
| 122 | + count++; |
| 123 | + } |
| 124 | + } |
| 125 | + Assert.assertTrue(count < recordCount); |
| 126 | + } |
| 127 | + |
| 128 | + // 2. scan specify column |
| 129 | + { |
| 130 | + Scan scan = new Scan(key.getBytes(), (key+recordCount).getBytes()); |
| 131 | + scan.addColumn(family.getBytes(), column.getBytes()); |
| 132 | + ResultScanner scanner = hTable.getScanner(scan); |
| 133 | + int count = 0; |
| 134 | + for (Result result : scanner) { |
| 135 | + for (Cell cell : result.rawCells()) { |
| 136 | + count++; |
| 137 | + } |
| 138 | + } |
| 139 | + Assert.assertTrue(count < recordCount); |
| 140 | + } |
| 141 | + |
| 142 | + // 3. scan specify versions |
| 143 | + { |
| 144 | + Scan scan = new Scan(key.getBytes(), (key+recordCount).getBytes()); |
| 145 | + scan.setMaxVersions(2); |
| 146 | + scan.addColumn(family.getBytes(), column.getBytes()); |
| 147 | + ResultScanner scanner = hTable.getScanner(scan); |
| 148 | + int count = 0; |
| 149 | + for (Result result : scanner) { |
| 150 | + for (Cell cell : result.rawCells()) { |
| 151 | + count++; |
| 152 | + } |
| 153 | + } |
| 154 | + Assert.assertTrue(count < recordCount); |
| 155 | + } |
| 156 | + |
| 157 | + // 4. scan specify time range |
| 158 | + { |
| 159 | + Scan scan = new Scan(key.getBytes(), (key+recordCount).getBytes()); |
| 160 | + scan.setMaxVersions(2); |
| 161 | + scan.setTimeStamp(curTs); |
| 162 | + scan.addColumn(family.getBytes(), column.getBytes()); |
| 163 | + ResultScanner scanner = hTable.getScanner(scan); |
| 164 | + int count = 0; |
| 165 | + for (Result result : scanner) { |
| 166 | + for (Cell cell : result.rawCells()) { |
| 167 | + count++; |
| 168 | + } |
| 169 | + } |
| 170 | + Assert.assertTrue(count < recordCount); |
| 171 | + } |
| 172 | + |
| 173 | + hTable.close(); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + public void testGet() throws Throwable { |
| 178 | + FOR_EACH(tableNames, OHTableShareStorageSeriesTest::testGetImpl); |
| 179 | + } |
| 180 | + |
| 181 | + |
| 182 | + @Test |
| 183 | + public void testScan() throws Throwable { |
| 184 | + FOR_EACH(tableNames, OHTableShareStorageSeriesTest::testScanImpl); |
| 185 | + } |
| 186 | + |
| 187 | +} |
0 commit comments