use of com.aerospike.client.Record in project aerospike-client-java by aerospike.
the class RWTaskSync method get.
@Override
protected void get(Key key, String binName) {
Record record;
if (counters.read.latency != null) {
long begin = System.nanoTime();
record = client.get(args.readPolicy, key, binName);
long elapsed = System.nanoTime() - begin;
counters.read.latency.add(elapsed);
} else {
record = client.get(args.readPolicy, key, binName);
}
processRead(key, record);
}
use of com.aerospike.client.Record in project aerospike-client-java by aerospike.
the class BatchOperate method batchReadOperateComplex.
/**
* Read results using varying read operations in one batch.
*/
private void batchReadOperateComplex(AerospikeClient client, Parameters params) {
console.info("batchReadOperateComplex");
Expression exp1 = Exp.build(Exp.mul(Exp.intBin(BinName1), Exp.intBin(BinName2)));
Expression exp2 = Exp.build(Exp.add(Exp.intBin(BinName1), Exp.intBin(BinName2)));
Expression exp3 = Exp.build(Exp.sub(Exp.intBin(BinName1), Exp.intBin(BinName2)));
// Batch uses pointer reference to quickly determine if operations are repeated and can therefore
// be optimized, but using varargs directly always creates a new reference. Therefore, save operation
// array so we have one pointer reference per operation array.
Operation[] ops1 = Operation.array(ExpOperation.read(ResultName1, exp1, ExpReadFlags.DEFAULT));
Operation[] ops2 = Operation.array(ExpOperation.read(ResultName1, exp2, ExpReadFlags.DEFAULT));
Operation[] ops3 = Operation.array(ExpOperation.read(ResultName1, exp3, ExpReadFlags.DEFAULT));
Operation[] ops4 = Operation.array(ExpOperation.read(ResultName1, exp2, ExpReadFlags.DEFAULT), ExpOperation.read(ResultName2, exp3, ExpReadFlags.DEFAULT));
List<BatchRead> records = new ArrayList<BatchRead>();
records.add(new BatchRead(new Key(params.namespace, params.set, KeyPrefix + 1), ops1));
// The following record is optimized (namespace,set,ops are only sent once) because
// namespace, set and ops all have the same pointer references as the previous entry.
records.add(new BatchRead(new Key(params.namespace, params.set, KeyPrefix + 2), ops1));
records.add(new BatchRead(new Key(params.namespace, params.set, KeyPrefix + 3), ops2));
records.add(new BatchRead(new Key(params.namespace, params.set, KeyPrefix + 4), ops3));
records.add(new BatchRead(new Key(params.namespace, params.set, KeyPrefix + 5), ops4));
// Execute batch.
client.get(null, records);
// Show results.
int count = 0;
for (BatchRead record : records) {
Record rec = record.record;
Object v1 = rec.getValue(ResultName1);
Object v2 = rec.getValue(ResultName2);
console.info("Result[%d]: %s, %s", count++, v1, v2);
}
}
use of com.aerospike.client.Record in project aerospike-client-java by aerospike.
the class Expire method expireExample.
/**
* Write and twice read an expiration record.
*/
private void expireExample(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "expirekey ");
Bin bin = new Bin(params.getBinName("expirebin"), "expirevalue");
console.info("Put: namespace=%s set=%s key=%s bin=%s value=%s expiration=2", key.namespace, key.setName, key.userKey, bin.name, bin.value);
// Specify that record expires 2 seconds after it's written.
WritePolicy writePolicy = new WritePolicy();
writePolicy.expiration = 2;
client.put(writePolicy, key, bin);
// Read the record before it expires, showing it is there.
console.info("Get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey);
Record record = client.get(params.policy, key, bin.name);
if (record == null) {
throw new Exception(String.format("Failed to get record: 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 record successful: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, received);
} else {
throw new Exception(String.format("Expire record mismatch: Expected %s. Received %s.", expected, received));
}
// Read the record after it expires, showing it's gone.
console.info("Sleeping for 3 seconds ...");
Thread.sleep(3 * 1000);
record = client.get(params.policy, key, bin.name);
if (record == null) {
console.info("Expiry of record successful. Record not found.");
} else {
console.error("Found record when it should have expired.");
}
}
use of com.aerospike.client.Record in project aerospike-client-java by aerospike.
the class Append method runExample.
/**
* Append string to an existing string.
*/
@Override
public void runExample(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "appendkey");
String binName = params.getBinName("appendbin");
// Delete record if it already exists.
client.delete(params.writePolicy, key);
Bin bin = new Bin(binName, "Hello");
console.info("Initial append will create record. Initial value is " + bin.value + '.');
client.append(params.writePolicy, key, bin);
bin = new Bin(binName, " World");
console.info("Append \"" + bin.value + "\" to existing record.");
client.append(params.writePolicy, key, bin);
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 = "Hello World";
if (received.equals(expected)) {
console.info("Append successful: ns=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, received);
} else {
console.error("Append mismatch: Expected %s. Received %s.", expected, received);
}
}
use of com.aerospike.client.Record in project aerospike-client-java by aerospike.
the class AsyncBatch method batchReadComplex.
/**
* Read records with varying namespaces, bin names and read types in one batch.
* This requires Aerospike Server version >= 3.6.0
*/
private void batchReadComplex() throws Exception {
// Batch gets into one call.
// Batch allows multiple namespaces in one call, but example test environment may only have one namespace.
String[] bins = new String[] { binName };
List<BatchRead> records = new ArrayList<BatchRead>();
records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 1), bins));
records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 2), true));
records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 3), true));
records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 4), false));
records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 5), true));
records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 6), true));
records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 7), bins));
// This record should be found, but the requested bin will not be found.
records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 8), new String[] { "binnotfound" }));
// This record should not be found.
records.add(new BatchRead(new Key(params.namespace, params.set, "keynotfound"), bins));
// Execute batch.
client.get(eventLoop, new BatchListListener() {
public void onSuccess(List<BatchRead> records) {
// Show results.
int found = 0;
for (BatchRead record : records) {
Key key = record.key;
Record rec = record.record;
if (rec != null) {
found++;
console.info("Record: ns=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, binName, rec.getValue(binName));
} else {
console.info("Record not found: ns=%s set=%s key=%s bin=%s", key.namespace, key.setName, key.userKey, binName);
}
}
if (found != 8) {
console.error("Records found mismatch. Expected %d. Received %d.", 8, found);
}
}
public void onFailure(AerospikeException e) {
console.error("Batch read complex failed: " + Util.getErrorMessage(e));
}
}, null, records);
}
Aggregations