/*
* Aerospike Client - Java Library
*
* Copyright 2012 by Aerospike, Inc. All rights reserved.
*
* Availability of this source code to partners and customers includes
* redistribution rights covered by individual contract. Please check your
* contract for exact rights and responsibilities.
*/
package com.aerospike.client.query;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.aerospike.client.AerospikeException;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.ResultCode;
import com.aerospike.client.cluster.Node;
import com.aerospike.client.command.Buffer;
import com.aerospike.client.command.Command;
public final class QueryRecordCommand extends QueryCommand {
private final RecordSet recordSet;
public QueryRecordCommand(Node node, RecordSet recordSet) {
super(node);
this.recordSet = recordSet;
}
protected boolean parseRecordResults(int receiveSize)
throws AerospikeException, IOException {
// Read/parse remaining message bytes one record at a time.
receiveOffset = 0;
while (receiveOffset < receiveSize) {
readBytes(MSG_REMAINING_HEADER_SIZE);
int resultCode = receiveBuffer[5] & 0xFF;
if (resultCode != 0) {
if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR) {
return false;
}
throw new AerospikeException(resultCode);
}
byte info3 = receiveBuffer[3];
// If this is the end marker of the response, do not proceed further
if ((info3 & Command.INFO3_LAST) == Command.INFO3_LAST) {
return false;
}
int generation = Buffer.bytesToInt(receiveBuffer, 6);
int expiration = Buffer.bytesToInt(receiveBuffer, 10);
int fieldCount = Buffer.bytesToShort(receiveBuffer, 18);
int opCount = Buffer.bytesToShort(receiveBuffer, 20);
Key key = parseKey(fieldCount);
// Parse bins.
Map<String,Object> bins = null;
for (int i = 0 ; i < opCount; i++) {
readBytes(8);
int opSize = Buffer.bytesToInt(receiveBuffer, 0);
byte particleType = receiveBuffer[5];
byte nameSize = receiveBuffer[7];
readBytes(nameSize);
String name = Buffer.utf8ToString(receiveBuffer, 0, nameSize);
int particleBytesSize = (int) (opSize - (4 + nameSize));
readBytes(particleBytesSize);
Object value = Buffer.bytesToParticle(particleType, receiveBuffer, 0, particleBytesSize);
if (bins == null) {
bins = new HashMap<String,Object>();
}
bins.put(name, value);
}
Record record = new Record(bins, null, generation, expiration);
if (! valid) {
throw new AerospikeException.QueryTerminated();
}
if (! recordSet.put(new KeyRecord(key, record))) {
stop();
throw new AerospikeException.QueryTerminated();
}
}
return true;
}
}