Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HBASE-28540 Cache Results in org.apache.hadoop.hbase.rest.client.Remo… #5846

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -505,6 +506,8 @@ public TableDescriptor getDescriptor() throws IOException {
class Scanner implements ResultScanner {

String uri;
private Result[] cachedResults;
private int nextCachedResultsRow = 0;

public Scanner(Scan scan) throws IOException {
ScannerModel model;
Expand Down Expand Up @@ -540,11 +543,8 @@ public Scanner(Scan scan) throws IOException {
throw new IOException("scan request timed out");
}

@Override
public Result[] next(int nbRows) throws IOException {
public Result[] nextBatch() throws IOException {
StringBuilder sb = new StringBuilder(uri);
sb.append("?n=");
sb.append(nbRows);
for (int i = 0; i < maxRetries; i++) {
Response response = client.get(sb.toString(), Constants.MIMETYPE_PROTOBUF);
int code = response.getCode();
Expand All @@ -570,13 +570,31 @@ public Result[] next(int nbRows) throws IOException {
throw new IOException("scanner.next request timed out");
}

private boolean updateCachedResults() throws IOException {
if (cachedResults == null || nextCachedResultsRow >= cachedResults.length) {
nextCachedResultsRow = 0;
cachedResults = nextBatch();
}
return !(cachedResults == null || cachedResults.length < 1);
}

@Override
public Result[] next(int nbRows) throws IOException {
if (!updateCachedResults()) {
return null;
}
int endIndex = Math.min(cachedResults.length, nextCachedResultsRow + nbRows);
Result[] chunk = Arrays.copyOfRange(cachedResults, nextCachedResultsRow, endIndex);
nextCachedResultsRow = endIndex;
return chunk;
}

@Override
public Result next() throws IOException {
Result[] results = next(1);
if (results == null || results.length < 1) {
if (!updateCachedResults()) {
return null;
}
return results[0];
return cachedResults[nextCachedResultsRow++];
}

class Iter implements Iterator<Result> {
Expand Down