Skip to content

Commit

Permalink
Thrift2 should support DeleteFamilyVersion type
Browse files Browse the repository at this point in the history
Signed-off-by: tedyu <yuzhihong@gmail.com>
  • Loading branch information
openinx authored and tedyu committed Jul 25, 2017
1 parent e1cd59b commit c891642
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,27 +268,40 @@ public static Delete deleteFromThrift(TDelete in) {
if (in.isSetColumns()) {
out = new Delete(in.getRow());
for (TColumn column : in.getColumns()) {
if (column.isSetQualifier()) {
if (column.isSetTimestamp()) {
if (in.isSetDeleteType() &&
in.getDeleteType().equals(TDeleteType.DELETE_COLUMNS))
out.addColumns(column.getFamily(), column.getQualifier(), column.getTimestamp());
else
if (in.isSetDeleteType()) {
switch (in.getDeleteType()) {
case DELETE_COLUMN:
if (column.isSetTimestamp()) {
out.addColumn(column.getFamily(), column.getQualifier(), column.getTimestamp());
} else {
if (in.isSetDeleteType() &&
in.getDeleteType().equals(TDeleteType.DELETE_COLUMNS))
out.addColumns(column.getFamily(), column.getQualifier());
else
} else {
out.addColumn(column.getFamily(), column.getQualifier());
}
break;
case DELETE_COLUMNS:
if (column.isSetTimestamp()) {
out.addColumns(column.getFamily(), column.getQualifier(), column.getTimestamp());
} else {
out.addColumns(column.getFamily(), column.getQualifier());
}
break;
case DELETE_FAMILY:
if (column.isSetTimestamp()) {
out.addFamily(column.getFamily(), column.getTimestamp());
} else {
out.addFamily(column.getFamily());
}
break;
case DELETE_FAMILY_VERSION:
if (column.isSetTimestamp()) {
out.addFamilyVersion(column.getFamily(), column.getTimestamp());
} else {
throw new IllegalArgumentException(
"Timestamp is required for TDelete with DeleteFamilyVersion type");
}
break;
}

} else {
if (column.isSetTimestamp()) {
out.addFamily(column.getFamily(), column.getTimestamp());
} else {
out.addFamily(column.getFamily());
}
throw new IllegalArgumentException("DeleteType is required for TDelete");
}
}
} else {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ struct TResult {
*/
enum TDeleteType {
DELETE_COLUMN = 0,
DELETE_COLUMNS = 1
DELETE_COLUMNS = 1,
DELETE_FAMILY = 2,
DELETE_FAMILY_VERSION = 3
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Increment;
import org.apache.hadoop.hbase.client.Delete;
Expand Down Expand Up @@ -75,10 +78,8 @@
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.junit.experimental.categories.Category;
import org.junit.rules.TestName;

import java.io.IOException;
import java.io.InterruptedIOException;
Expand Down Expand Up @@ -440,6 +441,92 @@ public void testDeleteSingleTimestamp() throws Exception {
assertEquals(timestamp1, result.getColumnValues().get(0).getTimestamp());
}

@Test
public void testDeleteFamily() throws Exception {
ThriftHBaseServiceHandler handler = createHandler();
byte[] rowName = "testDeleteFamily".getBytes();
ByteBuffer table = wrap(tableAname);

long timestamp1 = System.currentTimeMillis() - 10;
long timestamp2 = System.currentTimeMillis();

List<TColumnValue> columnValues = new ArrayList<TColumnValue>();
TColumnValue columnValueA =
new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname));
columnValueA.setTimestamp(timestamp1);
columnValues.add(columnValueA);
TPut put = new TPut(wrap(rowName), columnValues);

put.setColumnValues(columnValues);

handler.put(table, put);
columnValueA.setTimestamp(timestamp2);
handler.put(table, put);

TGet get = new TGet(wrap(rowName));
get.setMaxVersions(2);
TResult result = handler.get(table, get);
assertEquals(2, result.getColumnValuesSize());

TDelete delete = new TDelete(wrap(rowName));
List<TColumn> deleteColumns = new ArrayList<TColumn>();
TColumn deleteColumn = new TColumn(wrap(familyAname));
deleteColumns.add(deleteColumn);
delete.setColumns(deleteColumns);
delete.setDeleteType(TDeleteType.DELETE_FAMILY);

handler.deleteSingle(table, delete);

get = new TGet(wrap(rowName));
result = handler.get(table, get);
assertArrayEquals(null, result.getRow());
assertEquals(0, result.getColumnValuesSize());
}

@Test
public void testDeleteFamilyVersion() throws Exception {
ThriftHBaseServiceHandler handler = createHandler();
byte[] rowName = "testDeleteFamilyVersion".getBytes();
ByteBuffer table = wrap(tableAname);

long timestamp1 = System.currentTimeMillis() - 10;
long timestamp2 = System.currentTimeMillis();

List<TColumnValue> columnValues = new ArrayList<TColumnValue>();
TColumnValue columnValueA =
new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname));
columnValueA.setTimestamp(timestamp1);
columnValues.add(columnValueA);
TPut put = new TPut(wrap(rowName), columnValues);

put.setColumnValues(columnValues);

handler.put(table, put);
columnValueA.setTimestamp(timestamp2);
handler.put(table, put);

TGet get = new TGet(wrap(rowName));
get.setMaxVersions(2);
TResult result = handler.get(table, get);
assertEquals(2, result.getColumnValuesSize());

TDelete delete = new TDelete(wrap(rowName));
List<TColumn> deleteColumns = new ArrayList<TColumn>();
TColumn deleteColumn = new TColumn(wrap(familyAname));
deleteColumn.setTimestamp(timestamp1);
deleteColumns.add(deleteColumn);
delete.setColumns(deleteColumns);
delete.setDeleteType(TDeleteType.DELETE_FAMILY_VERSION);

handler.deleteSingle(table, delete);

get = new TGet(wrap(rowName));
result = handler.get(table, get);
assertArrayEquals(rowName, result.getRow());
assertEquals(1, result.getColumnValuesSize());
assertEquals(timestamp2, result.getColumnValues().get(0).getTimestamp());
}

@Test
public void testIncrement() throws Exception {
ThriftHBaseServiceHandler handler = createHandler();
Expand Down

0 comments on commit c891642

Please sign in to comment.