|
22 | 22 | import com.alipay.oceanbase.rpc.exception.ObTableException; |
23 | 23 | import com.alipay.oceanbase.rpc.filter.ObCompareOp; |
24 | 24 | 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; |
25 | 30 | import com.alipay.oceanbase.rpc.mutation.result.MutationResult; |
26 | 31 | import com.alipay.oceanbase.rpc.property.Property; |
27 | 32 | import com.alipay.oceanbase.rpc.protocol.payload.ResultCodes; |
@@ -89,6 +94,13 @@ private void dropTable(String tableName) throws SQLException { |
89 | 94 | statement.execute("drop table " + tableName); |
90 | 95 | } |
91 | 96 |
|
| 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 | + |
92 | 104 | @Test |
93 | 105 | // Test auto increment on rowkey |
94 | 106 | public void testAutoIncrementRowkey() throws Exception { |
@@ -564,4 +576,69 @@ public void testAutoColumnRowKey() throws Exception { |
564 | 576 | } finally { |
565 | 577 | } |
566 | 578 | } |
| 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 | + |
567 | 644 | } |
0 commit comments