use of com.aerospike.client.Bin 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.Bin in project aerospike-client-java by aerospike.
the class Batch method writeRecords.
/**
* Write records individually.
*/
private void writeRecords(AerospikeClient client, Parameters params, String keyPrefix, String binName, String valuePrefix, int size) throws Exception {
for (int i = 1; i <= size; i++) {
Key key = new Key(params.namespace, params.set, keyPrefix + i);
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(params.writePolicy, key, bin);
}
}
use of com.aerospike.client.Bin 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.Bin 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);
}
}
use of com.aerospike.client.Bin in project aerospike-client-java by aerospike.
the class QueryGeoCollection method writeMapRecords.
private void writeMapRecords(AerospikeClient client, Parameters params, String keyPrefix, String binName, String binName2, String valuePrefix, int size) throws Exception {
for (int i = 0; i < size; i++) {
Key key = new Key(params.namespace, params.set, keyPrefix + i);
HashMap<String, Value> map = new HashMap<String, Value>();
for (int jj = 0; jj < 10; ++jj) {
double plat = 0.0 + (0.01 * i);
double plng = 0.0 + (0.10 * jj);
String geoString = generatePoint(plat, plng);
map.put(valuePrefix + "pointkey_" + i + "_" + jj, Value.getAsGeoJSON(geoString));
double rlat = 0.0 + (0.01 * i);
double rlng = 0.0 - (0.10 * jj);
geoString = generatePolygon(rlat, rlng);
map.put(valuePrefix + "regionkey_" + i + "_" + jj, Value.getAsGeoJSON(geoString));
}
Bin bin = new Bin(binName, map);
Bin bin2 = new Bin(binName2, "other_bin_value_" + i);
client.put(params.writePolicy, key, bin, bin2);
}
console.info("Write " + size + " records.");
}
Aggregations