@Override
public void processRequest(final Request rq) {
RangeLookup req = (RangeLookup) rq.getRequestMessage();
int limit = req.getFromLength();
ReusableBuffer data = rq.getData();
byte[] from = null;
byte[] to = null;
ReusableBuffer f = null;
ReusableBuffer t = null;
if (data != null) {
f = data.createViewBuffer();
f.limit(limit);
from = f.getData();
t = data.createViewBuffer();
t.position(limit);
to = t.getData();
}
Logging.logMessage(Logging.LEVEL_DEBUG, this, "RangeLookupReverseOperation:" +
"db %s, index %d, from %s, to %s.", req.getDatabaseName(), req.getIndexId(),
(from == null) ? "null" : new String(from), (to == null) ? "null" : new String(to));
try {
dbs.getDatabase(req.getDatabaseName()).reverseRangeLookup(req.getIndexId(),
from, to, null).registerListener(
new DatabaseRequestListener<ResultSet<byte[], byte[]>>() {
@Override
public void finished(ResultSet<byte[], byte[]> result, Object context) {
EntryMap.Builder r = EntryMap.newBuilder();
// estimate the result size
int size = 0;
List<Entry<byte[], byte[]>> tmp = new ArrayList<Entry<byte[], byte[]>>();
while (result.hasNext()) {
Entry<byte[], byte[]> entry = result.next();
size += entry.getKey().length + entry.getValue().length;
tmp.add(entry);
}
result.free();
// prepare the response
ReusableBuffer data = BufferPool.allocate(size);
for (Entry<byte[], byte[]> entry : tmp) {
r.addLength(entry.getKey().length);
r.addLength(entry.getValue().length);
data.put(entry.getKey());
data.put(entry.getValue());
}
data.flip();
rq.sendSuccess(r.build(), data);
}
@Override