|
17 | 17 | */ |
18 | 18 | package org.apache.hadoop.hbase.wal; |
19 | 19 |
|
| 20 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 21 | +import static org.hamcrest.Matchers.hasSize; |
20 | 22 | import static org.junit.Assert.assertEquals; |
21 | 23 | import static org.junit.Assert.assertTrue; |
22 | 24 |
|
|
26 | 28 | import java.util.TreeMap; |
27 | 29 | import org.apache.hadoop.fs.Path; |
28 | 30 | import org.apache.hadoop.hbase.Cell; |
| 31 | +import org.apache.hadoop.hbase.CellBuilderFactory; |
| 32 | +import org.apache.hadoop.hbase.CellBuilderType; |
29 | 33 | import org.apache.hadoop.hbase.HBaseTestingUtility; |
30 | | -import org.apache.hadoop.hbase.KeyValue; |
31 | 34 | import org.apache.hadoop.hbase.TableName; |
32 | 35 | import org.apache.hadoop.hbase.client.RegionInfo; |
33 | 36 | import org.apache.hadoop.hbase.client.RegionInfoBuilder; |
@@ -82,33 +85,42 @@ public void doTest(TableName tableName) throws Exception { |
82 | 85 |
|
83 | 86 | for (int i = 0; i < total; i++) { |
84 | 87 | WALEdit kvs = new WALEdit(); |
85 | | - kvs.add(new KeyValue(row, family, Bytes.toBytes(i), value)); |
| 88 | + kvs.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setType(Cell.Type.Put) |
| 89 | + .setRow(row).setFamily(family).setQualifier(Bytes.toBytes(i)).setValue(value).build()); |
| 90 | + kvs.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY) |
| 91 | + .setType(Cell.Type.DeleteFamily).setRow(row).setFamily(family).build()); |
86 | 92 | wal.appendData(regionInfo, new WALKeyImpl(regionInfo.getEncodedNameAsBytes(), tableName, |
87 | 93 | System.currentTimeMillis(), mvcc, scopes), kvs); |
| 94 | + wal.sync(); |
88 | 95 | } |
89 | | - wal.sync(); |
90 | 96 | final Path walPath = AbstractFSWALProvider.getCurrentFileName(wal); |
91 | 97 | wals.shutdown(); |
92 | 98 |
|
93 | 99 | // Confirm the WAL can be read back |
94 | | - WAL.Reader reader = wals.createReader(TEST_UTIL.getTestFileSystem(), walPath); |
95 | | - int count = 0; |
96 | | - WAL.Entry entry = new WAL.Entry(); |
97 | | - while (reader.next(entry) != null) { |
98 | | - count++; |
99 | | - List<Cell> cells = entry.getEdit().getCells(); |
100 | | - assertTrue("Should be one KV per WALEdit", cells.size() == 1); |
101 | | - for (Cell cell : cells) { |
102 | | - assertTrue("Incorrect row", Bytes.equals(cell.getRowArray(), cell.getRowOffset(), |
103 | | - cell.getRowLength(), row, 0, row.length)); |
104 | | - assertTrue("Incorrect family", Bytes.equals(cell.getFamilyArray(), cell.getFamilyOffset(), |
105 | | - cell.getFamilyLength(), family, 0, family.length)); |
106 | | - assertTrue("Incorrect value", Bytes.equals(cell.getValueArray(), cell.getValueOffset(), |
107 | | - cell.getValueLength(), value, 0, value.length)); |
| 100 | + try (WAL.Reader reader = wals.createReader(TEST_UTIL.getTestFileSystem(), walPath)) { |
| 101 | + int count = 0; |
| 102 | + WAL.Entry entry = new WAL.Entry(); |
| 103 | + while (reader.next(entry) != null) { |
| 104 | + count++; |
| 105 | + List<Cell> cells = entry.getEdit().getCells(); |
| 106 | + assertThat("Should be two KVs per WALEdit", cells, hasSize(2)); |
| 107 | + Cell putCell = cells.get(0); |
| 108 | + assertEquals(Cell.Type.Put, putCell.getType()); |
| 109 | + assertTrue("Incorrect row", Bytes.equals(putCell.getRowArray(), putCell.getRowOffset(), |
| 110 | + putCell.getRowLength(), row, 0, row.length)); |
| 111 | + assertTrue("Incorrect family", Bytes.equals(putCell.getFamilyArray(), |
| 112 | + putCell.getFamilyOffset(), putCell.getFamilyLength(), family, 0, family.length)); |
| 113 | + assertTrue("Incorrect value", Bytes.equals(putCell.getValueArray(), |
| 114 | + putCell.getValueOffset(), putCell.getValueLength(), value, 0, value.length)); |
| 115 | + |
| 116 | + Cell deleteCell = cells.get(1); |
| 117 | + assertEquals(Cell.Type.DeleteFamily, deleteCell.getType()); |
| 118 | + assertTrue("Incorrect row", Bytes.equals(deleteCell.getRowArray(), |
| 119 | + deleteCell.getRowOffset(), deleteCell.getRowLength(), row, 0, row.length)); |
| 120 | + assertTrue("Incorrect family", Bytes.equals(deleteCell.getFamilyArray(), |
| 121 | + deleteCell.getFamilyOffset(), deleteCell.getFamilyLength(), family, 0, family.length)); |
108 | 122 | } |
| 123 | + assertEquals("Should have read back as many KVs as written", total, count); |
109 | 124 | } |
110 | | - assertEquals("Should have read back as many KVs as written", total, count); |
111 | | - reader.close(); |
112 | 125 | } |
113 | | - |
114 | 126 | } |
0 commit comments