Skip to content

Commit 7190c11

Browse files
committed
add testcase for has auto-increment column in batchoperation
1 parent c879b38 commit 7190c11

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

src/test/java/com/alipay/oceanbase/rpc/ObTableClientAutoIncTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
import com.alipay.oceanbase.rpc.exception.ObTableException;
2323
import com.alipay.oceanbase.rpc.filter.ObCompareOp;
2424
import com.alipay.oceanbase.rpc.filter.ObTableValueFilter;
25+
import com.alipay.oceanbase.rpc.mutation.BatchOperation;
26+
import com.alipay.oceanbase.rpc.mutation.Insert;
27+
import com.alipay.oceanbase.rpc.mutation.Replace;
28+
import com.alipay.oceanbase.rpc.mutation.Row;
29+
import com.alipay.oceanbase.rpc.mutation.result.BatchOperationResult;
2530
import com.alipay.oceanbase.rpc.mutation.result.MutationResult;
2631
import com.alipay.oceanbase.rpc.property.Property;
2732
import com.alipay.oceanbase.rpc.protocol.payload.ResultCodes;
@@ -89,6 +94,13 @@ private void dropTable(String tableName) throws SQLException {
8994
statement.execute("drop table " + tableName);
9095
}
9196

97+
private void deleteTable(String tableName) throws SQLException {
98+
// use sql to drop table
99+
Connection connection = ObTableClientTestUtil.getConnection();
100+
Statement statement = connection.createStatement();
101+
statement.execute("delete from " + tableName);
102+
}
103+
92104
@Test
93105
// Test auto increment on rowkey
94106
public void testAutoIncrementRowkey() throws Exception {
@@ -564,4 +576,69 @@ public void testAutoColumnRowKey() throws Exception {
564576
} finally {
565577
}
566578
}
579+
580+
@Test
581+
public void test_autoinc_in_batch() throws Exception {
582+
// auto increment is rowKey
583+
test_autoinc_in_batch_inner("test_auto_increment_rk_batch", false, false);
584+
test_autoinc_in_batch_inner("test_auto_increment_rk_batch_ttl", false, false);
585+
test_autoinc_in_batch_inner("test_auto_increment_rk_batch", false, true);
586+
test_autoinc_in_batch_inner("test_auto_increment_rk_batch_ttl", false, true);
587+
// auto increment is not rowKey
588+
test_autoinc_in_batch_inner("test_auto_increment_batch", true, false);
589+
test_autoinc_in_batch_inner("test_auto_increment_batch_ttl", true, false);
590+
test_autoinc_in_batch_inner("test_auto_increment_batch", true, true);
591+
test_autoinc_in_batch_inner("test_auto_increment_batch_ttl", true, true);
592+
}
593+
594+
public void test_autoinc_in_batch_inner(String tableName, boolean useAutoinc, boolean useReplace) throws Exception {
595+
try {
596+
int batchSize = 10;
597+
BatchOperation batchOperation = client.batchOperation(tableName);
598+
for (int i = 0; i < batchSize; i++) {
599+
Long c1_val = Long.valueOf(i + 100);
600+
Long c2_val = useAutoinc ? null : Long.valueOf(i + 1000);
601+
String c3_val = String.format("col_%d", i);
602+
if (useReplace) {
603+
Replace replace = new Replace();
604+
replace.setRowKey(row(colVal("c1", c1_val)));
605+
replace.addMutateRow(row(colVal("c2", c2_val), colVal("c3", c3_val)));
606+
batchOperation.addOperation(replace);
607+
} else {
608+
Insert insert = new Insert();
609+
insert.setRowKey(row(colVal("c1", c1_val)));
610+
insert.addMutateRow(row(colVal("c2", c2_val), colVal("c3", c3_val)));
611+
batchOperation.addOperation(insert);
612+
}
613+
614+
}
615+
BatchOperationResult batchOperationResult = batchOperation.execute();
616+
for (int i = 0; i < batchSize; i++) {
617+
Assert.assertEquals(1, batchOperationResult.get(i).getAffectedRows());
618+
}
619+
// check result
620+
TableQuery tableQuery = client.query(tableName);
621+
tableQuery.select("c1", "c2", "c3");
622+
tableQuery.addScanRange(new Object[]{100L}, new Object[]{Long.valueOf(100 + batchSize - 1)});
623+
QueryResultSet result = tableQuery.execute();
624+
int i = 0;
625+
Long c2_val = -1L;
626+
while (result.next()) {
627+
Row row = result.getResultRow();
628+
Assert.assertEquals(Long.valueOf(i + 100), row.get("c1"));
629+
if (useAutoinc) {
630+
Assert.assertTrue(c2_val < (Long)row.get("c2"));
631+
c2_val = (Long)row.get("c2");
632+
} else {
633+
Assert.assertEquals(Long.valueOf(i + 1000), row.get("c2"));
634+
}
635+
Assert.assertEquals(String.format("col_%d", i), row.get("c3"));
636+
i++;
637+
}
638+
Assert.assertEquals(batchSize, i);
639+
} finally {
640+
deleteTable(tableName);
641+
}
642+
}
643+
567644
}

src/test/resources/ci.sql

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,4 +596,33 @@ CREATE TABLE IF NOT EXISTS `audit_test` (
596596
PRIMARY KEY (`c1`)
597597
);
598598

599+
CREATE TABLE IF NOT EXISTS `test_auto_increment_rk_batch` (
600+
`c1` bigint(20) NOT NULL AUTO_INCREMENT,
601+
`c2` bigint(20) NOT NULL,
602+
`c3` varchar(20) DEFAULT "hello",
603+
PRIMARY KEY (`c1`)
604+
);
605+
606+
CREATE TABLE IF NOT EXISTS `test_auto_increment_rk_batch_ttl` (
607+
`c1` bigint(20) NOT NULL AUTO_INCREMENT,
608+
`c2` bigint(20) NOT NULL,
609+
`c3` varchar(20) DEFAULT "hello",
610+
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
611+
PRIMARY KEY (`c1`)
612+
) TTL(update_time + INTERVAL 300 SECOND);
613+
614+
CREATE TABLE IF NOT EXISTS `test_auto_increment_batch` (
615+
`c1` bigint(20) NOT NULL,
616+
`c2` bigint(20) NOT NULL AUTO_INCREMENT,
617+
`c3` varchar(20) DEFAULT "hello",
618+
PRIMARY KEY (`c1`)
619+
);
620+
621+
CREATE TABLE IF NOT EXISTS `test_auto_increment_batch_ttl` (
622+
`c1` bigint(20) NOT NULL,
623+
`c2` bigint(20) NOT NULL AUTO_INCREMENT,
624+
`c3` varchar(20) DEFAULT "hello",
625+
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
626+
PRIMARY KEY (`c1`)
627+
) TTL(update_time + INTERVAL 300 SECOND);
599628
alter system set kv_hotkey_throttle_threshold = 50;

0 commit comments

Comments
 (0)