Skip to content

Commit 160ad8c

Browse files
authored
Merge pull request #149 from stuBirdFly/client_1.3
Fix the error caused by putting a KeyValue with a delete type
2 parents fa561de + 88da8e3 commit 160ad8c

File tree

3 files changed

+121
-100
lines changed

3 files changed

+121
-100
lines changed

src/main/java/com/alipay/oceanbase/hbase/OHTable.java

Lines changed: 53 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,24 +1966,23 @@ private ObTableQuery buildObTableQuery(final Get get, Collection<byte[]> columnQ
19661966
public static ObTableBatchOperation buildObTableBatchOperation(List<Mutation> rowList,
19671967
List<byte[]> qualifiers) {
19681968
ObTableBatchOperation batch = new ObTableBatchOperation();
1969-
OHOpType opType;
1969+
ObTableOperationType opType;
19701970
Map<String, Integer> indexMap = new HashMap<>();
19711971
for (Mutation row : rowList) {
19721972
if (row instanceof Put) {
1973-
opType = OHOpType.Put;
1973+
opType = INSERT_OR_UPDATE;
19741974
} else if (row instanceof Delete) {
1975-
opType = OHOpType.Delete;
1975+
opType = DEL;
19761976
} else if (row instanceof Increment) {
1977-
opType = OHOpType.Increment;
1977+
opType = INCREMENT;
19781978
} else if (row instanceof Append) {
1979-
opType = OHOpType.Append;
1979+
opType = APPEND;
19801980
} else {
19811981
throw new FeatureNotSupportedException("not supported other type");
19821982
}
19831983
Set<Map.Entry<byte[], List<KeyValue>>> familyCellMap = row.getFamilyMap().entrySet();
1984-
19851984
for (Map.Entry<byte[], List<KeyValue>> familyWithCells : familyCellMap) {
1986-
if (opType == OHOpType.Increment || opType == OHOpType.Append) {
1985+
if (opType == INCREMENT || opType == APPEND) {
19871986
indexMap.clear();
19881987
for (int i = 0; i < familyWithCells.getValue().size(); i++) {
19891988
Cell cell = familyWithCells.getValue().get(i);
@@ -1992,15 +1991,17 @@ public static ObTableBatchOperation buildObTableBatchOperation(List<Mutation> ro
19921991
}
19931992
for (Map.Entry<String, Integer> entry : indexMap.entrySet()) {
19941993
qualifiers.add(entry.getKey().getBytes());
1995-
batch.addTableOperation(buildObTableOperation(familyWithCells.getValue().get(entry.getValue()), opType, row.getTTL()));
1994+
batch.addTableOperation(buildObTableOperation(
1995+
familyWithCells.getValue().get(entry.getValue()), opType,
1996+
row.getTTL()));
19961997
}
19971998
} else {
19981999
for (KeyValue cell : familyWithCells.getValue()) {
1999-
batch.addTableOperation(buildObTableOperation(cell, opType, row.getTTL()));
2000+
batch.addTableOperation(
2001+
buildObTableOperation(cell, opType, row.getTTL()));
20002002
}
20012003
}
20022004
}
2003-
20042005
}
20052006
batch.setSamePropertiesNames(true);
20062007
return batch;
@@ -2010,10 +2011,8 @@ private com.alipay.oceanbase.rpc.mutation.Mutation buildMutation(KeyValue kv,
20102011
ObTableOperationType operationType,
20112012
boolean isTableGroup, Long TTL) {
20122013
KeyValue.Type kvType = KeyValue.Type.codeToType(kv.getType());
2013-
switch (operationType) {
2014-
case INSERT_OR_UPDATE:
2015-
case APPEND:
2016-
case INCREMENT:
2014+
switch (kvType) {
2015+
case Put:
20172016
String[] property_columns = V_COLUMNS;
20182017
Object[] property = new Object[] { CellUtil.cloneValue(kv) };
20192018
if (TTL != Long.MAX_VALUE) {
@@ -2024,36 +2023,29 @@ private com.alipay.oceanbase.rpc.mutation.Mutation buildMutation(KeyValue kv,
20242023
ROW_KEY_COLUMNS,
20252024
new Object[] { kv.getRow(), kv.getQualifier(), kv.getTimestamp() },
20262025
property_columns, property);
2027-
case DEL:
2028-
switch (kvType) {
2029-
case Delete:
2030-
return com.alipay.oceanbase.rpc.mutation.Mutation.getInstance(DEL,
2031-
ROW_KEY_COLUMNS,
2032-
new Object[] { kv.getRow(), kv.getQualifier(), kv.getTimestamp() },
2033-
null, null);
2034-
case Maximum:
2035-
return com.alipay.oceanbase.rpc.mutation.Mutation.getInstance(DEL,
2036-
ROW_KEY_COLUMNS,
2037-
new Object[] { kv.getRow(), null, -kv.getTimestamp() }, null, null);
2038-
case DeleteColumn:
2039-
return com.alipay.oceanbase.rpc.mutation.Mutation.getInstance(DEL,
2040-
ROW_KEY_COLUMNS,
2041-
new Object[] { kv.getRow(), kv.getQualifier(), -kv.getTimestamp() },
2042-
null, null);
2043-
case DeleteFamily:
2044-
return com.alipay.oceanbase.rpc.mutation.Mutation.getInstance(DEL,
2045-
ROW_KEY_COLUMNS,
2046-
new Object[] { kv.getRow(), isTableGroup ? kv.getQualifier() : null,
2047-
-kv.getTimestamp() }, null, null);
2048-
case DeleteFamilyVersion:
2049-
return com.alipay.oceanbase.rpc.mutation.Mutation.getInstance(
2050-
DEL,
2051-
ROW_KEY_COLUMNS,
2052-
new Object[] { kv.getRow(), isTableGroup ? kv.getQualifier() : null,
2053-
kv.getTimestamp() }, null, null);
2054-
default:
2055-
throw new IllegalArgumentException("illegal mutation type " + kvType);
2056-
}
2026+
case Delete:
2027+
return com.alipay.oceanbase.rpc.mutation.Mutation.getInstance(DEL, ROW_KEY_COLUMNS,
2028+
new Object[] { kv.getRow(), kv.getQualifier(), kv.getTimestamp() }, null, null);
2029+
case Maximum:
2030+
return com.alipay.oceanbase.rpc.mutation.Mutation.getInstance(DEL, ROW_KEY_COLUMNS,
2031+
new Object[] { kv.getRow(), null, -kv.getTimestamp() }, null, null);
2032+
case DeleteColumn:
2033+
return com.alipay.oceanbase.rpc.mutation.Mutation
2034+
.getInstance(DEL, ROW_KEY_COLUMNS,
2035+
new Object[] { kv.getRow(), kv.getQualifier(), -kv.getTimestamp() }, null,
2036+
null);
2037+
case DeleteFamily:
2038+
return com.alipay.oceanbase.rpc.mutation.Mutation.getInstance(
2039+
DEL,
2040+
ROW_KEY_COLUMNS,
2041+
new Object[] { kv.getRow(), isTableGroup ? kv.getQualifier() : null,
2042+
-kv.getTimestamp() }, null, null);
2043+
case DeleteFamilyVersion:
2044+
return com.alipay.oceanbase.rpc.mutation.Mutation.getInstance(
2045+
DEL,
2046+
ROW_KEY_COLUMNS,
2047+
new Object[] { kv.getRow(), isTableGroup ? kv.getQualifier() : null,
2048+
kv.getTimestamp() }, null, null);
20572049
default:
20582050
throw new IllegalArgumentException("illegal mutation type " + operationType);
20592051
}
@@ -2178,48 +2170,35 @@ private BatchOperation buildBatchOperation(String tableName, List<? extends Row>
21782170
return batch;
21792171
}
21802172

2181-
public static ObTableOperation buildObTableOperation(KeyValue kv, OHOpType operationType,
2173+
public static ObTableOperation buildObTableOperation(KeyValue kv,
2174+
ObTableOperationType operationType,
21822175
Long TTL) {
2176+
KeyValue.Type kvType = KeyValue.Type.codeToType(kv.getType());
21832177
String[] property_columns = V_COLUMNS;
21842178
Object[] property = new Object[] { CellUtil.cloneValue(kv) };
21852179
if (TTL != Long.MAX_VALUE) {
21862180
property_columns = PROPERTY_COLUMNS;
21872181
property = new Object[] { CellUtil.cloneValue(kv), TTL };
21882182
}
2189-
switch (operationType) {
2183+
switch (kvType) {
21902184
case Put:
2191-
case Increment:
2192-
case Append:
2193-
ObTableOperationType type;
2194-
if (operationType == OHOpType.Put) {
2195-
type = INSERT_OR_UPDATE;
2196-
} else if (operationType == OHOpType.Increment) {
2197-
type = INCREMENT;
2198-
} else {
2199-
type = APPEND;
2200-
}
22012185
return getInstance(
2202-
type,
2186+
operationType,
22032187
new Object[] { CellUtil.cloneRow(kv), CellUtil.cloneQualifier(kv),
22042188
kv.getTimestamp() }, property_columns, property);
22052189
case Delete:
2206-
KeyValue.Type delType = KeyValue.Type.codeToType(kv.getTypeByte());
2207-
if (delType == KeyValue.Type.Delete) {
2208-
return getInstance(
2209-
DEL,
2210-
new Object[] { CellUtil.cloneRow(kv), CellUtil.cloneQualifier(kv),
2211-
kv.getTimestamp() }, null, null);
2212-
} else if (delType == KeyValue.Type.DeleteColumn) {
2213-
return getInstance(
2214-
DEL,
2215-
new Object[] { CellUtil.cloneRow(kv), CellUtil.cloneQualifier(kv),
2216-
-kv.getTimestamp() }, null, null);
2217-
} else if (delType == KeyValue.Type.DeleteFamily) {
2218-
return getInstance(DEL, new Object[] { kv.getRow(), null, -kv.getTimestamp() },
2219-
null, null);
2220-
} else {
2221-
throw new IllegalArgumentException("illegal delete type " + operationType);
2222-
}
2190+
return getInstance(
2191+
DEL,
2192+
new Object[] { CellUtil.cloneRow(kv), CellUtil.cloneQualifier(kv),
2193+
kv.getTimestamp() }, null, null);
2194+
case DeleteColumn:
2195+
return getInstance(
2196+
DEL,
2197+
new Object[] { CellUtil.cloneRow(kv), CellUtil.cloneQualifier(kv),
2198+
-kv.getTimestamp() }, null, null);
2199+
case DeleteFamily:
2200+
return getInstance(DEL, new Object[] { kv.getRow(), null, -kv.getTimestamp() },
2201+
null, null);
22232202
default:
22242203
throw new IllegalArgumentException("illegal mutation type " + operationType);
22252204
}
@@ -2331,8 +2310,4 @@ public byte[][] getEndKeys() throws IOException {
23312310
public Pair<byte[][], byte[][]> getStartEndKeys() throws IOException {
23322311
return new Pair<>(getStartKeys(), getEndKeys());
23332312
}
2334-
2335-
public enum OHOpType {
2336-
Put, Append, Delete, Increment
2337-
}
23382313
}

src/test/java/com/alipay/oceanbase/hbase/HTableTestBase.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5108,6 +5108,52 @@ public void testAppend() throws IOException {
51085108
}
51095109
}
51105110

5111+
@Test
5112+
public void testHbasePutDeleteCell() throws Exception {
5113+
final byte[] rowKey = Bytes.toBytes("12345");
5114+
final byte[] family = Bytes.toBytes("family1");
5115+
5116+
Put put = new Put(rowKey);
5117+
put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a"));
5118+
put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b"));
5119+
put.add(family, Bytes.toBytes("C"), Bytes.toBytes("c"));
5120+
put.add(family, Bytes.toBytes("D"), Bytes.toBytes("d"));
5121+
hTable.put(put);
5122+
// get row back and assert the values
5123+
Get get = new Get(rowKey);
5124+
get.addFamily(family);
5125+
Result result = hTable.get(get);
5126+
assertTrue("Column A value should be a",
5127+
Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a"));
5128+
assertTrue("Column B value should be b",
5129+
Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b"));
5130+
assertTrue("Column C value should be c",
5131+
Bytes.toString(result.getValue(family, Bytes.toBytes("C"))).equals("c"));
5132+
assertTrue("Column D value should be d",
5133+
Bytes.toString(result.getValue(family, Bytes.toBytes("D"))).equals("d"));
5134+
// put the same row again with C column deleted
5135+
put = new Put(rowKey);
5136+
put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a1"));
5137+
put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b1"));
5138+
KeyValue marker = new KeyValue(rowKey, family, Bytes.toBytes("C"),
5139+
HConstants.LATEST_TIMESTAMP, KeyValue.Type.DeleteColumn);
5140+
put.add(family, Bytes.toBytes("D"), Bytes.toBytes("d1"));
5141+
put.add(marker);
5142+
hTable.put(put);
5143+
// get row back and assert the values
5144+
get = new Get(rowKey);
5145+
get.addFamily(family);
5146+
result = hTable.get(get);
5147+
assertTrue("Column A value should be a1",
5148+
Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a1"));
5149+
assertTrue("Column B value should be b1",
5150+
Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b1"));
5151+
System.out.println(result.getValue(family, Bytes.toBytes("C")));
5152+
assertTrue("Column C should not exist", result.getValue(family, Bytes.toBytes("C")) == null);
5153+
assertTrue("Column D value should be d1",
5154+
Bytes.toString(result.getValue(family, Bytes.toBytes("D"))).equals("d1"));
5155+
}
5156+
51115157
@Test
51125158
public void testCellTTL() throws Exception {
51135159
String key1 = "key1";

src/test/java/com/alipay/oceanbase/hbase/util/ObHTableTestUtil.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public class ObHTableTestUtil {
7070

7171
public static void prepareClean(List<String> tableGroupList) throws Exception {
7272
for (String tableGroup : tableGroupList) {
73-
tableNameList.addAll(getOTableNameList(tableGroup));
73+
tableNameList.addAll(getOHTableNameList(tableGroup));
7474
}
7575
}
7676

@@ -120,30 +120,30 @@ public static OHTableClient newOHTableClient(String tableName) {
120120
return new OHTableClient(tableName, newConfiguration());
121121
}
122122

123-
static public List<String> getOTableNameList(String tableGroup) throws IOException {
124-
// 读取建表语句
125-
List<String> res = new LinkedList<>();
126-
String sql = new String(Files.readAllBytes(Paths.get(NativeHBaseUtil.SQL_PATH)));
127-
String[] sqlList = sql.split(";");
128-
Map<String, HTableDescriptor> tableMap = new LinkedHashMap<>();
129-
for (String singleSql : sqlList) {
130-
String realTableName;
131-
if (singleSql.contains("CREATE TABLE ")) {
132-
singleSql.trim();
133-
String[] splits = singleSql.split(" ");
134-
String tableGroupName = splits[2].substring(1, splits[2].length() - 1);
135-
if (tableGroupName.contains(":")) {
136-
String[] tmpStr = tableGroupName.split(":", 2);
137-
tableGroupName = tmpStr[1];
138-
}
139-
realTableName = tableGroupName.split("\\$", 2)[0];
140-
if (realTableName.equals(tableGroup)) {
141-
res.add(tableGroupName);
142-
}
123+
static public List<String> getOHTableNameList(String tableGroup) throws IOException {
124+
// 读取建表语句
125+
List<String> res = new LinkedList<>();
126+
String sql = new String(Files.readAllBytes(Paths.get(NativeHBaseUtil.SQL_PATH)));
127+
String[] sqlList = sql.split(";");
128+
Map<String, HTableDescriptor> tableMap = new LinkedHashMap<>();
129+
for (String singleSql : sqlList) {
130+
String realTableName;
131+
if (singleSql.contains("CREATE TABLE ")) {
132+
singleSql.trim();
133+
String[] splits = singleSql.split(" ");
134+
String tableGroupName = splits[2].substring(1, splits[2].length() - 1);
135+
if (tableGroupName.contains(":")) {
136+
String[] tmpStr = tableGroupName.split(":", 2);
137+
tableGroupName = tmpStr[1];
138+
}
139+
realTableName = tableGroupName.split("\\$", 2)[0];
140+
if (realTableName.equals(tableGroup)) {
141+
res.add(tableGroupName);
143142
}
144143
}
145-
return res;
146144
}
145+
return res;
146+
}
147147

148148
static public Connection getConnection() {
149149
try {

0 commit comments

Comments
 (0)