|
| 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 | + |
| 19 | +package com.alipay.oceanbase.hbase.secondary; |
| 20 | + |
| 21 | +import com.alipay.oceanbase.hbase.OHTableClient; |
| 22 | +import com.alipay.oceanbase.hbase.util.ObHTableTestUtil; |
| 23 | +import com.alipay.oceanbase.hbase.util.TableTemplateManager; |
| 24 | +import com.mysql.cj.exceptions.AssertionFailedException; |
| 25 | +import org.apache.hadoop.hbase.Cell; |
| 26 | +import org.apache.hadoop.hbase.client.Delete; |
| 27 | +import org.apache.hadoop.hbase.client.Get; |
| 28 | +import org.apache.hadoop.hbase.client.Put; |
| 29 | +import org.apache.hadoop.hbase.client.Result; |
| 30 | +import org.junit.*; |
| 31 | + |
| 32 | +import java.util.*; |
| 33 | + |
| 34 | +import static com.alipay.oceanbase.hbase.util.OHTableHotkeyThrottleUtil.OperationType.get; |
| 35 | +import static com.alipay.oceanbase.hbase.util.ObHTableSecondaryPartUtil.*; |
| 36 | +import static com.alipay.oceanbase.hbase.util.TableTemplateManager.TIMESERIES_TABLES; |
| 37 | +import static org.apache.hadoop.hbase.util.Bytes.toBytes; |
| 38 | +import static com.alipay.oceanbase.hbase.util.ObHTableTestUtil.*; |
| 39 | +import static org.junit.Assert.assertTrue; |
| 40 | +import static org.junit.Assert.fail; |
| 41 | + |
| 42 | +public class OHTableTimeSeriesDeleteTest { |
| 43 | + private static List<String> tableNames = new LinkedList<String>(); |
| 44 | + private static Map<String, List<String>> group2tableNames = new LinkedHashMap<>(); |
| 45 | + |
| 46 | + |
| 47 | + @BeforeClass |
| 48 | + public static void before() throws Exception { |
| 49 | + openDistributedExecute(); |
| 50 | + for (TableTemplateManager.TableType type : TIMESERIES_TABLES) { |
| 51 | + createTables(type, tableNames, group2tableNames, true); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @AfterClass |
| 56 | + public static void finish() throws Exception { |
| 57 | + closeDistributedExecute(); |
| 58 | + } |
| 59 | + |
| 60 | + @Before |
| 61 | + public void prepareCase() throws Exception { |
| 62 | + truncateTables(tableNames, group2tableNames); |
| 63 | + } |
| 64 | + |
| 65 | + public static void testDeleteColumnImpl(String tableName) throws Exception { |
| 66 | + OHTableClient hTable = ObHTableTestUtil.newOHTableClient(getTableName(tableName)); |
| 67 | + hTable.init(); |
| 68 | + |
| 69 | + String family = getColumnFamilyName(tableName); |
| 70 | + String key = "putKey"; |
| 71 | + String[] columns = {"putColumn1", "putColumn2"}; |
| 72 | + long curTs = System.currentTimeMillis(); |
| 73 | + long[] ts = {curTs, curTs+1}; // each column have two versions |
| 74 | + String[] values = {"version1", "version2"}; |
| 75 | + |
| 76 | + for (int i = 0; i < values.length; i++) { |
| 77 | + Put put = new Put(toBytes(key)); |
| 78 | + put.add(family.getBytes(), columns[i].getBytes(), ts[i], toBytes(values[i])); |
| 79 | + hTable.put(put); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + Get get = new Get(toBytes(key)); |
| 85 | + get.addColumn(family.getBytes(), columns[0].getBytes()); |
| 86 | + get.addColumn(family.getBytes(), columns[1].getBytes()); |
| 87 | + Result result = hTable.get(get); |
| 88 | + { |
| 89 | + final Cell[] cells = result.rawCells(); |
| 90 | + Assert(tableName, () -> Assert.assertEquals(cells.length, 2)); |
| 91 | + sortCells(cells); |
| 92 | + for (int i = 0; i < values.length; i++) { |
| 93 | + AssertKeyValue(key, columns[i], ts[i], values[i], cells[i]); |
| 94 | + } |
| 95 | + } |
| 96 | + { |
| 97 | + Delete delete = new Delete(toBytes(key)); |
| 98 | + delete.addColumn(family.getBytes(), columns[0].getBytes()); |
| 99 | + hTable.delete(delete); |
| 100 | + result = hTable.get(get); |
| 101 | + final Cell[] cells = result.rawCells(); |
| 102 | + Assert(tableName, () -> Assert.assertEquals(cells.length, 1)); |
| 103 | + AssertKeyValue(key, columns[1], ts[1], values[1], cells[0]); |
| 104 | + } |
| 105 | + |
| 106 | + { |
| 107 | + Delete delete = new Delete(toBytes(key)); |
| 108 | + delete.addColumn(family.getBytes(), columns[1].getBytes()); |
| 109 | + hTable.delete(delete); |
| 110 | + result = hTable.get(get); |
| 111 | + final Cell[] cells = result.rawCells(); |
| 112 | + Assert(tableName, () -> Assert.assertEquals(cells.length, 0)); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public static void testDeleteColumnsImpl(String tableName) throws Exception { |
| 117 | + OHTableClient hTable = ObHTableTestUtil.newOHTableClient(getTableName(tableName)); |
| 118 | + hTable.init(); |
| 119 | + |
| 120 | + String family = getColumnFamilyName(tableName); |
| 121 | + String key = "putKey"; |
| 122 | + String[] columns = {"putColumn1", "putColumn2"}; |
| 123 | + long curTs = System.currentTimeMillis(); |
| 124 | + long[] ts = {curTs, curTs+1}; // each column have two versions |
| 125 | + String[] values = {"version1", "version2"}; |
| 126 | + |
| 127 | + for (int i = 0; i < values.length; i++) { |
| 128 | + Put put = new Put(toBytes(key)); |
| 129 | + put.add(family.getBytes(), columns[i].getBytes(), ts[i] + i, toBytes(values[i] + i)); |
| 130 | + hTable.put(put); |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + Get get = new Get(toBytes(key)); |
| 136 | + get.addColumn(family.getBytes(), columns[0].getBytes()); |
| 137 | + get.addColumn(family.getBytes(), columns[1].getBytes()); |
| 138 | + Result result = hTable.get(get); |
| 139 | + { |
| 140 | + final Cell[] cells = result.rawCells(); |
| 141 | + Assert(tableName, () -> Assert.assertEquals(cells.length, 2)); |
| 142 | + sortCells(cells); |
| 143 | + for (int i = 0; i < values.length; i++) { |
| 144 | + AssertKeyValue(key, columns[i], ts[i] + i, values[i] + i, cells[i]); |
| 145 | + } |
| 146 | + } |
| 147 | + { |
| 148 | + Delete delete = new Delete(toBytes(key)); |
| 149 | + delete.addColumns(family.getBytes(), columns[0].getBytes()); |
| 150 | + hTable.delete(delete); |
| 151 | + result = hTable.get(get); |
| 152 | + final Cell[] cells = result.rawCells(); |
| 153 | + Assert(tableName, () -> Assert.assertEquals(cells.length, 1)); |
| 154 | + AssertKeyValue(key, columns[1], ts[1] + 1, values[1] + 1, cells[0]); |
| 155 | + } |
| 156 | + |
| 157 | + { |
| 158 | + Delete delete = new Delete(toBytes(key)); |
| 159 | + delete.addColumns(family.getBytes(), columns[1].getBytes()); |
| 160 | + hTable.delete(delete); |
| 161 | + result = hTable.get(get); |
| 162 | + final Cell[] cells = result.rawCells(); |
| 163 | + Assert(tableName, () -> Assert.assertEquals(cells.length, 0)); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + public static void testDeleteFamilyImpl(String tableName) throws Exception { |
| 168 | + OHTableClient hTable = ObHTableTestUtil.newOHTableClient(getTableName(tableName)); |
| 169 | + hTable.init(); |
| 170 | + |
| 171 | + String family = getColumnFamilyName(tableName); |
| 172 | + String key = "putKey"; |
| 173 | + String[] columns = {"putColumn1", "putColumn2"}; |
| 174 | + long curTs = System.currentTimeMillis(); |
| 175 | + long[] ts = {curTs, curTs+1}; // each column have two versions |
| 176 | + String[] values = {"version1", "version2"}; |
| 177 | + |
| 178 | + for (int i = 0; i < values.length; i++) { |
| 179 | + Put put = new Put(toBytes(key)); |
| 180 | + put.add(family.getBytes(), columns[i].getBytes(), ts[i] + i, toBytes(values[i] + i)); |
| 181 | + hTable.put(put); |
| 182 | + } |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | + Get get = new Get(toBytes(key)); |
| 187 | + get.addColumn(family.getBytes(), columns[0].getBytes()); |
| 188 | + get.addColumn(family.getBytes(), columns[1].getBytes()); |
| 189 | + Result result = hTable.get(get); |
| 190 | + { |
| 191 | + final Cell[] cells = result.rawCells(); |
| 192 | + Assert(tableName, () -> Assert.assertEquals(cells.length, 2)); |
| 193 | + sortCells(cells); |
| 194 | + for (int i = 0; i < values.length; i++) { |
| 195 | + AssertKeyValue(key, columns[i], ts[i] + i, values[i] + i, cells[i]); |
| 196 | + } |
| 197 | + } |
| 198 | + { |
| 199 | + Delete delete = new Delete(toBytes(key)); |
| 200 | + delete.addFamily(family.getBytes()); |
| 201 | + hTable.delete(delete); |
| 202 | + result = hTable.get(get); |
| 203 | + final Cell[] cells = result.rawCells(); |
| 204 | + Assert(tableName, () -> Assert.assertEquals(cells.length, 0)); |
| 205 | + } |
| 206 | + { |
| 207 | + Delete delete = new Delete(toBytes(key)); |
| 208 | + delete.addFamily(family.getBytes()); |
| 209 | + delete.addFamily("notExistFamily".getBytes()); |
| 210 | + try { |
| 211 | + hTable.delete(delete); |
| 212 | + fail(); |
| 213 | + } catch (Exception e) { |
| 214 | + assertTrue(e.getCause().getMessage().contains("timeseries hbase table with multi column family not supported")); |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + public static void testDeleteFamilyWithTimestampImpl(String tableName) throws Exception { |
| 220 | + OHTableClient hTable = ObHTableTestUtil.newOHTableClient(getTableName(tableName)); |
| 221 | + hTable.init(); |
| 222 | + |
| 223 | + String family = getColumnFamilyName(tableName); |
| 224 | + String key = "putKey"; |
| 225 | + String[] columns = {"putColumn1", "putColumn2"}; |
| 226 | + long curTs = System.currentTimeMillis(); |
| 227 | + |
| 228 | + String value = "value"; |
| 229 | + |
| 230 | + for (int i = 0; i < 3; i++) { |
| 231 | + Put put = new Put(toBytes(key)); |
| 232 | + put.add(family.getBytes(), columns[0].getBytes(), curTs + i, toBytes(value + i)); |
| 233 | + put.add(family.getBytes(), columns[1].getBytes(), curTs + i, toBytes(value + i)); |
| 234 | + hTable.put(put); |
| 235 | + } |
| 236 | + |
| 237 | + |
| 238 | + { |
| 239 | + Delete delete = new Delete(toBytes(key)); |
| 240 | + delete.addFamily(family.getBytes(), curTs + 1); |
| 241 | + hTable.delete(delete); |
| 242 | + Get get = new Get(toBytes(key)); |
| 243 | + get.addColumn(family.getBytes(), columns[0].getBytes()); |
| 244 | + get.addColumn(family.getBytes(), columns[1].getBytes()); |
| 245 | + Result result = hTable.get(get); |
| 246 | + final Cell[] cells = result.rawCells(); |
| 247 | + Assert(tableName, () -> Assert.assertEquals(cells.length, 2)); |
| 248 | + sortCells(cells); |
| 249 | + AssertKeyValue(key, columns[0], curTs + 2, value + 2, cells[0]); |
| 250 | + AssertKeyValue(key, columns[1], curTs + 2, value + 2, cells[1]); |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + public static void testDeleteFamilyVersionImpl(String tableName) throws Exception { |
| 255 | + OHTableClient hTable = ObHTableTestUtil.newOHTableClient(getTableName(tableName)); |
| 256 | + hTable.init(); |
| 257 | + |
| 258 | + String family = getColumnFamilyName(tableName); |
| 259 | + String key = "putKey"; |
| 260 | + String[] columns = {"putColumn1", "putColumn2"}; |
| 261 | + long curTs = System.currentTimeMillis(); |
| 262 | + |
| 263 | + String value = "value"; |
| 264 | + |
| 265 | + for (int i = 0; i < 3; i++) { |
| 266 | + Put put = new Put(toBytes(key)); |
| 267 | + put.add(family.getBytes(), columns[0].getBytes(), curTs + i, toBytes(value + i)); |
| 268 | + put.add(family.getBytes(), columns[1].getBytes(), curTs + i, toBytes(value + i)); |
| 269 | + hTable.put(put); |
| 270 | + } |
| 271 | + |
| 272 | + |
| 273 | + { |
| 274 | + Delete delete = new Delete(toBytes(key)); |
| 275 | + delete.addFamilyVersion(family.getBytes(), curTs + 1); |
| 276 | + hTable.delete(delete); |
| 277 | + Get get = new Get(toBytes(key)); |
| 278 | + get.addColumn(family.getBytes(), columns[0].getBytes()); |
| 279 | + get.addColumn(family.getBytes(), columns[1].getBytes()); |
| 280 | + Result result = hTable.get(get); |
| 281 | + final Cell[] cells = result.rawCells(); |
| 282 | + Assert(tableName, () -> Assert.assertEquals(cells.length, 2)); |
| 283 | + sortCells(cells); |
| 284 | + AssertKeyValue(key, columns[0], curTs + 2, value + 2, cells[0]); |
| 285 | + AssertKeyValue(key, columns[1], curTs + 2, value + 2, cells[1]); |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + @Test |
| 290 | + public void testDeleteColumn() throws Exception { |
| 291 | + for (String tableName : tableNames) { |
| 292 | + testDeleteColumnImpl(tableName); |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + @Test |
| 297 | + public void testDeleteColumns() throws Exception { |
| 298 | + for (String tableName : tableNames) { |
| 299 | + testDeleteColumnsImpl(tableName); |
| 300 | + } |
| 301 | + } |
| 302 | + |
| 303 | + @Test |
| 304 | + public void testDeleteFamily() throws Exception { |
| 305 | + for (String tableName : tableNames) { |
| 306 | + testDeleteFamilyImpl(tableName); |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + @Test |
| 311 | + public void testDeleteFamilyWithTimestamp() throws Exception { |
| 312 | + for (String tableName : tableNames) { |
| 313 | + testDeleteFamilyWithTimestampImpl(tableName); |
| 314 | + } |
| 315 | + } |
| 316 | + |
| 317 | + @Test |
| 318 | + public void testDeleteFamilyVersion() throws Exception { |
| 319 | + for (String tableName : tableNames) { |
| 320 | + testDeleteFamilyVersionImpl(tableName); |
| 321 | + } |
| 322 | + } |
| 323 | +} |
0 commit comments