use of com.aerospike.client.AerospikeException 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.AerospikeException in project aerospike-client-java by aerospike.
the class Util method readResource.
public static byte[] readResource(ClassLoader resourceLoader, String resourcePath) {
try {
URL url = resourceLoader.getResource(resourcePath);
if (url == null) {
throw new IllegalArgumentException("Resource: " + resourcePath + " not found");
}
InputStream is = url.openStream();
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
byte[] bytes = new byte[8192];
int length;
while ((length = is.read(bytes)) > 0) {
bos.write(bytes, 0, length);
}
return bos.toByteArray();
} finally {
is.close();
}
} catch (Exception e) {
throw new AerospikeException("Failed to read resource " + resourcePath, e);
}
}
use of com.aerospike.client.AerospikeException 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);
}
use of com.aerospike.client.AerospikeException in project aerospike-client-java by aerospike.
the class Replace method runReplaceOnlyExample.
public void runReplaceOnlyExample(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "replaceonlykey");
Bin bin = new Bin("bin", "value");
// Delete record if it already exists.
client.delete(params.writePolicy, key);
console.info("Replace record requiring that it exists: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey);
try {
WritePolicy policy = new WritePolicy();
policy.recordExistsAction = RecordExistsAction.REPLACE_ONLY;
client.put(policy, key, bin);
console.error("Failure. This command should have resulted in an error.");
} catch (AerospikeException ae) {
if (ae.getResultCode() == ResultCode.KEY_NOT_FOUND_ERROR) {
console.info("Success. Key not found error returned as expected.");
} else {
throw ae;
}
}
}
use of com.aerospike.client.AerospikeException in project aerospike-client-java by aerospike.
the class TestAsyncQuery method initialize.
@BeforeClass
public static void initialize() {
Policy policy = new Policy();
// Do not timeout on index create.
policy.socketTimeout = 0;
try {
IndexTask task = client.createIndex(policy, args.namespace, args.set, indexName, binName, IndexType.NUMERIC);
task.waitTillComplete();
} catch (AerospikeException ae) {
if (ae.getResultCode() != ResultCode.INDEX_ALREADY_EXISTS) {
throw ae;
}
}
}
Aggregations