use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class Serialize method testComplex.
/**
* Write complex object using standard java serializer.
*/
public void testComplex(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "serialcomplexkey");
// Delete record if it already exists.
client.delete(params.writePolicy, key);
console.info("Initialize complex object");
ArrayList<Object> inner = new ArrayList<Object>();
inner.add("string2");
inner.add(8);
HashMap<Object, Object> innerMap = new HashMap<Object, Object>();
innerMap.put("a", 1);
innerMap.put(2, "b");
innerMap.put("list", inner);
ArrayList<Object> list = new ArrayList<Object>();
list.add("string1");
list.add(4);
list.add(inner);
list.add(innerMap);
Bin bin = new Bin(params.getBinName("complexbin"), new Value.BlobValue(list));
console.info("Write complex object using serializer.");
client.put(params.writePolicy, key, bin);
console.info("Read complex object using serializer.");
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;
try {
received = (List<?>) record.getValue(bin.name);
} catch (Exception e) {
throw new Exception(String.format("Failed to parse returned value: namespace=%s set=%s key=%s bin=%s", key.namespace, key.setName, key.userKey, bin.name));
}
if (received != null && received.equals(list)) {
console.info("Data matched: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, received);
} else {
console.error("Data mismatch");
console.error("Expected " + list);
console.error("Received " + received);
}
console.info("Read complex object successful.");
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class Serialize method testList.
/**
* Write list using standard java serializer.
*/
public void testList(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "seriallistkey");
// Delete record if it already exists.
client.delete(params.writePolicy, key);
console.info("Initialize list");
ArrayList<String> list = new ArrayList<String>();
list.add("string1");
list.add("string2");
list.add("string3");
Bin bin = new Bin(params.getBinName("serialbin"), (Object) list);
console.info("Write list using serializer.");
client.put(params.writePolicy, key, bin);
console.info("Read list using serializer.");
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));
}
List<?> received;
try {
received = (List<?>) record.getValue(bin.name);
} catch (Exception e) {
throw new Exception(String.format("Failed to parse returned value: namespace=%s set=%s key=%s bin=%s", key.namespace, key.setName, key.userKey, bin.name));
}
if (received.size() != 3) {
throw new Exception(String.format("Array length mismatch: Expected=%d Received=%d", 3, received.size()));
}
for (int i = 0; i < received.size(); i++) {
String expected = "string" + (i + 1);
if (!received.get(i).equals(expected)) {
Object obj = received.get(i);
throw new Exception(String.format("Mismatch: index=%d expected=%s received=%s", i, expected, obj));
}
}
console.info("Read list successful.");
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class StoreKey method writeRecords.
private void writeRecords(AerospikeClient client, Parameters params, String keyPrefix, String binName, int size) throws Exception {
console.info("Write " + size + " records with store user key option.");
WritePolicy policy = new WritePolicy();
policy.sendKey = true;
for (int i = 1; i <= size; i++) {
Key key = new Key(params.namespace, params.set, keyPrefix + i);
Bin bin = new Bin(binName, i);
client.put(policy, key, bin);
}
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class Touch method runExample.
/**
* Demonstrate touch command.
*/
@Override
public void runExample(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "touchkey");
Bin bin = new Bin(params.getBinName("touchbin"), "touchvalue");
console.info("Create record with 2 second expiration.");
WritePolicy writePolicy = new WritePolicy();
writePolicy.expiration = 2;
client.put(writePolicy, key, bin);
console.info("Touch same record with 5 second expiration.");
writePolicy.expiration = 5;
Record record = client.operate(writePolicy, key, Operation.touch(), Operation.getHeader());
if (record == null) {
throw new Exception(String.format("Failed to get: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, null));
}
if (record.expiration == 0) {
throw new Exception(String.format("Failed to get record expiration: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey));
}
console.info("Sleep 3 seconds.");
Thread.sleep(3000);
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));
}
console.info("Success. Record still exists.");
console.info("Sleep 4 seconds.");
Thread.sleep(4000);
record = client.get(params.policy, key, bin.name);
if (record == null) {
console.info("Success. Record expired as expected.");
} 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 TestOperateList method operateList2.
@Test
public void operateList2() {
Key key = new Key(args.namespace, args.set, "oplkey2");
client.delete(null, key);
List<Value> itemList = new ArrayList<Value>();
itemList.add(Value.get(12));
itemList.add(Value.get(-8734));
itemList.add(Value.get("my string"));
Record record = client.operate(null, key, ListOperation.appendItems(binName, itemList), Operation.put(new Bin("otherbin", "hello")));
assertRecordFound(key, record);
record = client.operate(null, key, ListOperation.insert(binName, -1, Value.get(8)), Operation.append(new Bin("otherbin", Value.get("goodbye"))), Operation.get("otherbin"), ListOperation.getRange(binName, 0, 4), ListOperation.getRange(binName, 3));
assertRecordFound(key, record);
// System.out.println("Record: " + record);
String val = record.getString("otherbin");
assertEquals("hellogoodbye", val);
List<?> list = record.getList(binName);
long size = (Long) list.get(0);
assertEquals(4, size);
List<?> rangeList = (List<?>) list.get(1);
long lval = (Long) rangeList.get(0);
assertEquals(12, lval);
lval = (Long) rangeList.get(1);
assertEquals(-8734, lval);
lval = (Long) rangeList.get(2);
assertEquals(8, lval);
val = (String) rangeList.get(3);
assertEquals("my string", val);
rangeList = (List<?>) list.get(2);
val = (String) rangeList.get(0);
assertEquals("my string", val);
}
Aggregations