use of com.aerospike.client.Record in project aerospike-client-java by aerospike.
the class AsyncQuery method parseRow.
@Override
protected void parseRow(Key key) throws AerospikeException {
if (resultCode != 0) {
throw new AerospikeException(resultCode);
}
Record record = parseRecord();
listener.onRecord(key, record);
}
use of com.aerospike.client.Record in project aerospike-client-java by aerospike.
the class AsyncScanPartition method parseRow.
@Override
protected void parseRow(Key key) {
if ((info3 & Command.INFO3_PARTITION_DONE) != 0) {
// specified partition will need to be requested on the scan retry.
if (resultCode == 0) {
tracker.partitionDone(nodePartitions, generation);
}
return;
}
if (resultCode != 0) {
throw new AerospikeException(resultCode);
}
Record record = parseRecord();
listener.onRecord(key, record);
tracker.setDigest(nodePartitions, key);
}
use of com.aerospike.client.Record in project aerospike-client-java by aerospike.
the class AsyncScanPage method runScan.
private void runScan(AerospikeClient client, EventLoop eventLoop) {
int pageSize = 30;
console.info("Scan max " + pageSize + " records.");
ScanPolicy policy = new ScanPolicy();
policy.maxRecords = pageSize;
PartitionFilter filter = PartitionFilter.all();
RecordSequenceListener listener = new RecordSequenceListener() {
private int count = 0;
@Override
public void onRecord(Key key, Record record) throws AerospikeException {
count++;
}
@Override
public void onSuccess() {
console.info("Records returned: " + count);
notifyComplete();
}
@Override
public void onFailure(AerospikeException e) {
console.error("Scan failed: " + Util.getErrorMessage(e));
notifyComplete();
}
};
client.scanPartitions(eventLoop, listener, policy, filter, params.namespace, setName);
}
use of com.aerospike.client.Record in project aerospike-client-java by aerospike.
the class Batch method batchReadHeaders.
/**
* Read record header data in one batch.
*/
private void batchReadHeaders(AerospikeClient client, Parameters params, String keyPrefix, int size) throws Exception {
// Batch gets into one call.
Key[] keys = new Key[size];
for (int i = 0; i < size; i++) {
keys[i] = new Key(params.namespace, params.set, keyPrefix + (i + 1));
}
Record[] records = client.getHeader(null, keys);
for (int i = 0; i < records.length; i++) {
Key key = keys[i];
Record record = records[i];
Level level = Level.ERROR;
int generation = 0;
int expiration = 0;
if (record != null && (record.generation > 0 || record.expiration > 0)) {
level = Level.INFO;
generation = record.generation;
expiration = record.expiration;
}
console.write(level, "Record: ns=%s set=%s key=%s generation=%d expiration=%d", key.namespace, key.setName, key.userKey, generation, expiration);
}
if (records.length != size) {
console.error("Record size mismatch. Expected %d. Received %d.", size, records.length);
}
}
use of com.aerospike.client.Record in project aerospike-client-java by aerospike.
the class Generation method runExample.
/**
* Exercise record generation functionality.
*/
@Override
public void runExample(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "genkey");
String binName = params.getBinName("genbin");
// Delete record if it already exists.
client.delete(params.writePolicy, key);
// Set some values for the same record.
Bin bin = new Bin(binName, "genvalue1");
console.info("Put: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, bin.value);
client.put(params.writePolicy, key, bin);
bin = new Bin(binName, "genvalue2");
console.info("Put: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, bin.value);
client.put(params.writePolicy, key, bin);
// Retrieve record and its generation count.
Record record = client.get(params.policy, key, bin.name);
if (record == null) {
throw new Exception(String.format("Failed to get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey));
}
Object received = record.getValue(bin.name);
String expected = bin.value.toString();
if (received.equals(expected)) {
console.info("Get successful: namespace=%s set=%s key=%s bin=%s value=%s generation=%d", key.namespace, key.setName, key.userKey, bin.name, received, record.generation);
} else {
throw new Exception(String.format("Get mismatch: Expected %s. Received %s.", expected, received));
}
// Set record and fail if it's not the expected generation.
bin = new Bin(binName, "genvalue3");
console.info("Put: namespace=%s set=%s key=%s bin=%s value=%s expected generation=%d", key.namespace, key.setName, key.userKey, bin.name, bin.value, record.generation);
WritePolicy writePolicy = new WritePolicy();
writePolicy.generationPolicy = GenerationPolicy.EXPECT_GEN_EQUAL;
writePolicy.generation = record.generation;
client.put(writePolicy, key, bin);
// Set record with invalid generation and check results .
bin = new Bin(binName, "genvalue4");
writePolicy.generation = 9999;
console.info("Put: namespace=%s set=%s key=%s bin=%s value=%s expected generation=%d", key.namespace, key.setName, key.userKey, bin.name, bin.value, writePolicy.generation);
try {
client.put(writePolicy, key, bin);
throw new Exception("Should have received generation error instead of success.");
} catch (AerospikeException ae) {
if (ae.getResultCode() == ResultCode.GENERATION_ERROR) {
console.info("Success: Generation error returned as expected.");
} else {
throw new Exception(String.format("Unexpected set return code: namespace=%s set=%s key=%s bin=%s value=%s code=%s", key.namespace, key.setName, key.userKey, bin.name, bin.value, ae.getResultCode()));
}
}
// Verify results.
record = client.get(params.policy, key, bin.name);
if (record == null) {
throw new Exception(String.format("Failed to get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey));
}
received = record.getValue(bin.name);
expected = "genvalue3";
if (received.equals(expected)) {
console.info("Get successful: namespace=%s set=%s key=%s bin=%s value=%s generation=%d", key.namespace, key.setName, key.userKey, bin.name, received, record.generation);
} else {
throw new Exception(String.format("Get mismatch: Expected %s. Received %s.", expected, received));
}
}
Aggregations