|
17 | 17 |
|
18 | 18 | package com.alipay.oceanbase.hbase;
|
19 | 19 |
|
20 |
| -import com.alipay.oceanbase.hbase.exception.FeatureNotSupportedException; |
21 | 20 | import org.apache.hadoop.hbase.*;
|
22 | 21 | import org.apache.hadoop.hbase.client.*;
|
23 | 22 | import org.apache.hadoop.hbase.filter.*;
|
@@ -5619,6 +5618,243 @@ public void testAppend() throws IOException {
|
5619 | 5618 | }
|
5620 | 5619 | }
|
5621 | 5620 |
|
| 5621 | + @Test |
| 5622 | + public void testCellTTL() throws Exception { |
| 5623 | + String key1 = "key1"; |
| 5624 | + String column1 = "cf1"; |
| 5625 | + String column2 = "cf2"; |
| 5626 | + String column3 = "cf3"; |
| 5627 | + String family = "cellTTLFamily"; |
| 5628 | + String value1 = "v1"; |
| 5629 | + String value2 = "v2"; |
| 5630 | + String app = "app"; |
| 5631 | + |
| 5632 | + Result r; |
| 5633 | + Put put1 = new Put(key1.getBytes()); |
| 5634 | + put1.addColumn(family.getBytes(), column1.getBytes(), toBytes(11L)); |
| 5635 | + put1.setTTL(5000); |
| 5636 | + Put put2 = new Put(key1.getBytes()); |
| 5637 | + put2.addColumn(family.getBytes(), column1.getBytes(), toBytes(22L)); |
| 5638 | + put2.addColumn(family.getBytes(), column2.getBytes(), toBytes(33L)); |
| 5639 | + put2.setTTL(10000); |
| 5640 | + Put put3 = new Put(key1.getBytes()); |
| 5641 | + put3.addColumn(family.getBytes(), column1.getBytes(), toBytes(11L)); |
| 5642 | + put3.setTTL(-3000); |
| 5643 | + Put put4 = new Put(key1.getBytes()); |
| 5644 | + put4.addColumn(family.getBytes(), column1.getBytes(), toBytes(11L)); |
| 5645 | + put4.setTTL(0); |
| 5646 | + Put errorPut = new Put(key1.getBytes()); |
| 5647 | + errorPut.addColumn("family1".getBytes(), column1.getBytes(), toBytes(11L)); |
| 5648 | + errorPut.setTTL(10); |
| 5649 | + |
| 5650 | + Get get = new Get(key1.getBytes()); |
| 5651 | + get.addFamily(family.getBytes()); |
| 5652 | + get.setMaxVersions(10); |
| 5653 | + try { |
| 5654 | + tryPut(hTable, errorPut); |
| 5655 | + } catch (Exception e) { |
| 5656 | + assertTrue(e.getCause().toString().contains("Unknown column 'TTL'")); |
| 5657 | + } |
| 5658 | + // test put and get |
| 5659 | + tryPut(hTable, put1); |
| 5660 | + tryPut(hTable, put2); |
| 5661 | + tryPut(hTable, put3); |
| 5662 | + tryPut(hTable, put4); |
| 5663 | + r = hTable.get(get); |
| 5664 | + assertEquals(3, r.size()); |
| 5665 | + Thread.sleep(5000); |
| 5666 | + r = hTable.get(get); |
| 5667 | + assertEquals(2, r.size()); |
| 5668 | + Thread.sleep(5000); |
| 5669 | + r = hTable.get(get); |
| 5670 | + assertEquals(0, r.size()); |
| 5671 | + |
| 5672 | + // test increment |
| 5673 | + tryPut(hTable, put1); |
| 5674 | + tryPut(hTable, put2); |
| 5675 | + Thread.sleep(1000); |
| 5676 | + Increment increment = new Increment(key1.getBytes()); |
| 5677 | + increment.addColumn(family.getBytes(), column1.getBytes(), 1L); |
| 5678 | + increment.addColumn(family.getBytes(), column2.getBytes(), 2L); |
| 5679 | + increment.addColumn(family.getBytes(), column3.getBytes(), 5L); |
| 5680 | + increment.setTTL(-5000); |
| 5681 | + hTable.increment(increment); |
| 5682 | + increment.setTTL(5000); |
| 5683 | + hTable.increment(increment); |
| 5684 | + get.setMaxVersions(1); |
| 5685 | + r = hTable.get(get); |
| 5686 | + |
| 5687 | + assertEquals(3, r.size()); |
| 5688 | + assertEquals(23L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5689 | + column1.getBytes()).get(0)))); |
| 5690 | + assertEquals(35L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5691 | + column2.getBytes()).get(0)))); |
| 5692 | + assertEquals(5L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5693 | + column3.getBytes()).get(0)))); |
| 5694 | + |
| 5695 | + Thread.sleep(10000); |
| 5696 | + r = hTable.get(get); |
| 5697 | + assertEquals(0, r.size()); |
| 5698 | + |
| 5699 | + increment = new Increment(key1.getBytes()); |
| 5700 | + increment.addColumn(family.getBytes(), column1.getBytes(), 1L); |
| 5701 | + increment.addColumn(family.getBytes(), column2.getBytes(), 2L); |
| 5702 | + increment.setTTL(5000); |
| 5703 | + hTable.increment(increment); |
| 5704 | + r = hTable.get(get); |
| 5705 | + |
| 5706 | + assertEquals(2, r.size()); |
| 5707 | + assertEquals(1L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5708 | + column1.getBytes()).get(0)))); |
| 5709 | + assertEquals(2L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5710 | + column2.getBytes()).get(0)))); |
| 5711 | + |
| 5712 | + Thread.sleep(5000); |
| 5713 | + r = hTable.get(get); |
| 5714 | + assertEquals(0, r.size()); |
| 5715 | + |
| 5716 | + tryPut(hTable, put1); |
| 5717 | + tryPut(hTable, put2); |
| 5718 | + increment.addColumn(family.getBytes(), column1.getBytes(), 4L); |
| 5719 | + hTable.increment(increment); |
| 5720 | + |
| 5721 | + r = hTable.get(get); |
| 5722 | + assertEquals(2, r.size()); |
| 5723 | + assertEquals(26L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5724 | + column1.getBytes()).get(0)))); |
| 5725 | + assertEquals(35L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5726 | + column2.getBytes()).get(0)))); |
| 5727 | + |
| 5728 | + // test append |
| 5729 | + Thread.sleep(10000); |
| 5730 | + r = hTable.get(get); |
| 5731 | + assertEquals(0, r.size()); |
| 5732 | + |
| 5733 | + put3 = new Put(key1.getBytes()); |
| 5734 | + put3.addColumn(family.getBytes(), column1.getBytes(), toBytes(value1)); |
| 5735 | + put3.addColumn(family.getBytes(), column2.getBytes(), toBytes(value2)); |
| 5736 | + put3.setTTL(10000); |
| 5737 | + tryPut(hTable, put3); |
| 5738 | + |
| 5739 | + Append append = new Append(key1.getBytes()); |
| 5740 | + KeyValue kv = new KeyValue(key1.getBytes(), family.getBytes(), column1.getBytes(), |
| 5741 | + app.getBytes()); |
| 5742 | + append.add(kv); |
| 5743 | + append.setTTL(-3000); |
| 5744 | + hTable.append(append); |
| 5745 | + append.setTTL(3000); |
| 5746 | + hTable.append(append); |
| 5747 | + |
| 5748 | + r = hTable.get(get); |
| 5749 | + assertEquals(2, r.size()); |
| 5750 | + assertEquals( |
| 5751 | + value1 + app, |
| 5752 | + Bytes.toString(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5753 | + column1.getBytes()).get(0)))); |
| 5754 | + |
| 5755 | + Thread.sleep(3000); |
| 5756 | + r = hTable.get(get); |
| 5757 | + assertEquals(2, r.size()); |
| 5758 | + assertEquals( |
| 5759 | + value1, |
| 5760 | + Bytes.toString(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5761 | + column1.getBytes()).get(0)))); |
| 5762 | + |
| 5763 | + Thread.sleep(7000); |
| 5764 | + r = hTable.get(get); |
| 5765 | + assertEquals(0, r.size()); |
| 5766 | + |
| 5767 | + append.add(family.getBytes(), column1.getBytes(), app.getBytes()); |
| 5768 | + hTable.append(append); |
| 5769 | + r = hTable.get(get); |
| 5770 | + assertEquals(1, r.size()); |
| 5771 | + assertEquals( |
| 5772 | + app, |
| 5773 | + Bytes.toString(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5774 | + column1.getBytes()).get(0)))); |
| 5775 | + |
| 5776 | + Thread.sleep(3000); |
| 5777 | + append.add(family.getBytes(), column2.getBytes(), app.getBytes()); |
| 5778 | + hTable.append(append); |
| 5779 | + r = hTable.get(get); |
| 5780 | + assertEquals(2, r.size()); |
| 5781 | + assertEquals( |
| 5782 | + app, |
| 5783 | + Bytes.toString(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5784 | + column1.getBytes()).get(0)))); |
| 5785 | + assertEquals( |
| 5786 | + app, |
| 5787 | + Bytes.toString(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5788 | + column2.getBytes()).get(0)))); |
| 5789 | + |
| 5790 | + // test checkAndMutate |
| 5791 | + Thread.sleep(3000); |
| 5792 | + r = hTable.get(get); |
| 5793 | + assertEquals(0, r.size()); |
| 5794 | + |
| 5795 | + tryPut(hTable, put1); |
| 5796 | + RowMutations rowMutations = new RowMutations(key1.getBytes()); |
| 5797 | + rowMutations.add(put2); |
| 5798 | + Delete delete = new Delete(key1.getBytes()); |
| 5799 | + delete.addColumn(family.getBytes(), column1.getBytes()); |
| 5800 | + rowMutations.add(delete); |
| 5801 | + boolean succ = hTable.checkAndMutate(key1.getBytes(), family.getBytes(), |
| 5802 | + column1.getBytes(), CompareFilter.CompareOp.EQUAL, toBytes(11L), rowMutations); |
| 5803 | + assertTrue(succ); |
| 5804 | + r = hTable.get(get); |
| 5805 | + assertEquals(r.size(), 2); |
| 5806 | + assertEquals(11L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5807 | + column1.getBytes()).get(0)))); |
| 5808 | + assertEquals(33L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5809 | + column2.getBytes()).get(0)))); |
| 5810 | + |
| 5811 | + Thread.sleep(10000); |
| 5812 | + r = hTable.get(get); |
| 5813 | + assertEquals(r.size(), 0); |
| 5814 | + |
| 5815 | + tryPut(hTable, put1); |
| 5816 | + rowMutations = new RowMutations(key1.getBytes()); |
| 5817 | + put4 = new Put(key1.getBytes()); |
| 5818 | + put4.addColumn(family.getBytes(), column1.getBytes(), toBytes(22L)); |
| 5819 | + put4.addColumn(family.getBytes(), column2.getBytes(), toBytes(33L)); |
| 5820 | + put4.setTTL(10000); |
| 5821 | + rowMutations.add(put4); |
| 5822 | + succ = hTable.checkAndMutate(key1.getBytes(), family.getBytes(), column1.getBytes(), |
| 5823 | + CompareFilter.CompareOp.EQUAL, toBytes(1L), rowMutations); |
| 5824 | + assertFalse(succ); |
| 5825 | + succ = hTable.checkAndMutate(key1.getBytes(), family.getBytes(), column1.getBytes(), |
| 5826 | + CompareFilter.CompareOp.EQUAL, toBytes(11L), rowMutations); |
| 5827 | + assertTrue(succ); |
| 5828 | + |
| 5829 | + r = hTable.get(get); |
| 5830 | + assertEquals(r.size(), 2); |
| 5831 | + assertEquals(22L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5832 | + column1.getBytes()).get(0)))); |
| 5833 | + assertEquals(33L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5834 | + column2.getBytes()).get(0)))); |
| 5835 | + |
| 5836 | + Thread.sleep(5000); |
| 5837 | + r = hTable.get(get); |
| 5838 | + assertEquals(2, r.size()); |
| 5839 | + assertEquals(22L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5840 | + column1.getBytes()).get(0)))); |
| 5841 | + assertEquals(33L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 5842 | + column2.getBytes()).get(0)))); |
| 5843 | + |
| 5844 | + Thread.sleep(5000); |
| 5845 | + r = hTable.get(get); |
| 5846 | + assertEquals(r.size(), 0); |
| 5847 | + put1 = new Put(key1.getBytes()); |
| 5848 | + put1.addColumn(family.getBytes(), column1.getBytes(), toBytes(11L)); |
| 5849 | + tryPut(hTable, put1); |
| 5850 | + |
| 5851 | + increment = new Increment(key1.getBytes()); |
| 5852 | + increment.addColumn(family.getBytes(), column1.getBytes(), 1L); |
| 5853 | + hTable.increment(increment); |
| 5854 | + r = hTable.get(get); |
| 5855 | + assertEquals(r.size(), 1); |
| 5856 | + } |
| 5857 | + |
5622 | 5858 | @Test
|
5623 | 5859 | public void testIncrement() throws IOException {
|
5624 | 5860 | String column = "incrementColumn";
|
@@ -5883,17 +6119,17 @@ public void testFamilyBlank() throws Exception {
|
5883 | 6119 | try {
|
5884 | 6120 | hTable.append(append);
|
5885 | 6121 | fail();
|
5886 |
| - } catch (FeatureNotSupportedException e) { |
5887 |
| - Assert.assertTrue(e.getMessage().contains("family is empty")); |
| 6122 | + } catch (IllegalArgumentException e) { |
| 6123 | + Assert.assertTrue(e.getMessage().contains("zero columns specified")); |
5888 | 6124 | }
|
5889 | 6125 |
|
5890 | 6126 | Increment increment = new Increment(key.getBytes());
|
5891 | 6127 | // increment.addColumn(null, null, 1);
|
5892 | 6128 | try {
|
5893 | 6129 | hTable.increment(increment);
|
5894 | 6130 | fail();
|
5895 |
| - } catch (FeatureNotSupportedException e) { |
5896 |
| - Assert.assertTrue(e.getMessage().contains("family is empty")); |
| 6131 | + } catch (IllegalArgumentException e) { |
| 6132 | + Assert.assertTrue(e.getMessage().contains("zero columns specified")); |
5897 | 6133 | }
|
5898 | 6134 | }
|
5899 | 6135 |
|
|
0 commit comments