use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class Command method setBatchReadDirect.
public final void setBatchReadDirect(Policy policy, Key[] keys, BatchNode.BatchNamespace batch, String[] binNames, int readAttr) {
// Estimate buffer size
begin();
int byteSize = batch.offsetsSize * SyncCommand.DIGEST_SIZE;
dataOffset += Buffer.estimateSizeUtf8(batch.namespace) + FIELD_HEADER_SIZE + byteSize + FIELD_HEADER_SIZE;
if (binNames != null) {
for (String binName : binNames) {
estimateOperationSize(binName);
}
}
sizeBuffer();
int operationCount = (binNames == null) ? 0 : binNames.length;
writeHeader(policy, readAttr, 0, 2, operationCount);
writeField(batch.namespace, FieldType.NAMESPACE);
writeFieldHeader(byteSize, FieldType.DIGEST_RIPE_ARRAY);
int[] offsets = batch.offsets;
int max = batch.offsetsSize;
for (int i = 0; i < max; i++) {
Key key = keys[offsets[i]];
byte[] digest = key.digest;
System.arraycopy(digest, 0, dataBuffer, dataOffset, digest.length);
dataOffset += digest.length;
}
if (binNames != null) {
for (String binName : binNames) {
writeOperation(binName, Operation.Type.READ);
}
}
end();
}
use of com.aerospike.client.Key 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.Key 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));
}
}
use of com.aerospike.client.Key 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.Key in project aerospike-client-java by aerospike.
the class AsyncBatch method writeRecords.
/**
* Write records individually.
*/
private void writeRecords() {
WriteHandler handler = new WriteHandler(size);
for (int i = 1; i <= size; i++) {
Key key = sendKeys[i - 1];
Bin bin = new Bin(binName, valuePrefix + i);
console.info("Put: ns=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, bin.value);
client.put(eventLoop, handler, params.writePolicy, key, bin);
}
}
Aggregations