Skip to content

Commit 24dfa6a

Browse files
committed
existsAll using batch get
1 parent b699555 commit 24dfa6a

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

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

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -467,16 +467,18 @@ public boolean exists(Get get) throws IOException {
467467
@Override
468468
public boolean[] existsAll(List<Get> gets) throws IOException {
469469
boolean[] ret = new boolean[gets.size()];
470-
if (ObGlobal.isHBaseBatchGetSupport()) {
471-
Result[] results = new Result[gets.size()];
472-
batch(gets, results);
473-
for (int i = 0; i < gets.size(); ++i) {
474-
ret[i] = !results[i].isEmpty();
475-
}
476-
} else {
477-
for (int i = 0; i < gets.size(); ++i) {
478-
ret[i] = exists(gets.get(i));
479-
}
470+
List<Get> newGets = new ArrayList<>();
471+
// if just checkExistOnly, batch get will not return any result or row count
472+
// therefore we have to set checkExistOnly as false and so the result can be returned
473+
// TODO: adjust ExistOnly in server when using batch get
474+
for (Get get : gets) {
475+
Get newGet = new Get(get);
476+
newGet.setCheckExistenceOnly(false);
477+
newGets.add(newGet);
478+
}
479+
Result[] results = get(newGets);
480+
for (int i = 0; i < results.length; ++i) {
481+
ret[i] = !results[i].isEmpty();
480482
}
481483
return ret;
482484
}
@@ -671,33 +673,13 @@ public void batch(final List<? extends Row> actions, final Object[] results) thr
671673
batchError.add((ObTableException) tmpResults.getResults().get(index), actions.get(i), null);
672674
} else if (actions.get(i) instanceof Get) {
673675
if (results != null) {
676+
// get results have been wrapped in MutationResult, need to fetch it
674677
if (tmpResults.getResults().get(index) instanceof MutationResult) {
675678
MutationResult mutationResult = (MutationResult) tmpResults.getResults().get(index);
676679
ObPayload innerResult = mutationResult.getResult();
677680
if (innerResult instanceof ObTableSingleOpResult) {
678681
ObTableSingleOpResult singleOpResult = (ObTableSingleOpResult) innerResult;
679-
ObTableSingleOpEntity singleOpEntity = singleOpResult.getEntity();
680-
List<ObObj> propertiesValues = singleOpEntity.getPropertiesValues();
681-
List<Cell> cells = new ArrayList<>();
682-
int valueIdx = 0;
683-
while (valueIdx < propertiesValues.size()) {
684-
byte[][] familyAndQualifier = new byte[2][];
685-
try {
686-
// split family and qualifier
687-
familyAndQualifier = OHBaseFuncUtils
688-
.extractFamilyFromQualifier((byte[]) propertiesValues.get(valueIdx + 1).getValue());
689-
} catch (Exception e) {
690-
throw new IOException(e);
691-
}
692-
KeyValue kv = new KeyValue((byte[]) propertiesValues.get(valueIdx).getValue(),//K
693-
familyAndQualifier[0], // family
694-
familyAndQualifier[1], // qualifiermat
695-
(Long) propertiesValues.get(valueIdx + 2).getValue(), // T
696-
(byte[]) propertiesValues.get(valueIdx + 3).getValue()// V
697-
);
698-
cells.add(kv);
699-
valueIdx += 4;
700-
}
682+
List<Cell> cells = generateGetResult(singleOpResult);
701683
results[i] = Result.create(cells);
702684
} else {
703685
throw new ObTableUnexpectedException("Unexpected type of result in MutationResult");
@@ -719,6 +701,37 @@ public void batch(final List<? extends Row> actions, final Object[] results) thr
719701
}
720702
}
721703

704+
private List<Cell> generateGetResult(ObTableSingleOpResult getResult) throws IOException {
705+
List<Cell> cells = new ArrayList<>();
706+
ObTableSingleOpEntity singleOpEntity = getResult.getEntity();
707+
// all values queried by this get are contained in properties
708+
// qualifier in batch get result is always appended after family
709+
List<ObObj> propertiesValues = singleOpEntity.getPropertiesValues();
710+
int valueIdx = 0;
711+
while (valueIdx < propertiesValues.size()) {
712+
// values in propertiesValues like: [ K, Q, T, V, K, Q, T, V ... ]
713+
// we need to retrieve K Q T V and construct them to cells: [ cell_0, cell_1, ... ]
714+
byte[][] familyAndQualifier = new byte[2][];
715+
try {
716+
// split family and qualifier
717+
familyAndQualifier = OHBaseFuncUtils
718+
.extractFamilyFromQualifier((byte[]) propertiesValues.get(valueIdx + 1).getValue());
719+
} catch (Exception e) {
720+
throw new IOException(e);
721+
}
722+
KeyValue kv = new KeyValue((byte[]) propertiesValues.get(valueIdx).getValue(),//K
723+
familyAndQualifier[0], // family
724+
familyAndQualifier[1], // qualifiermat
725+
(Long) propertiesValues.get(valueIdx + 2).getValue(), // T
726+
(byte[]) propertiesValues.get(valueIdx + 3).getValue()// V
727+
);
728+
cells.add(kv);
729+
valueIdx += 4;
730+
}
731+
return cells;
732+
}
733+
734+
722735
private String getTargetTableName(List<? extends Row> actions) {
723736
byte[] family = null;
724737
for (Row action : actions) {

0 commit comments

Comments
 (0)