use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class ListMap method testMapComplex.
/**
* Write/Read HashMap<Object,Object> directly instead of relying on java serializer.
*/
private void testMapComplex(AerospikeClient client, Parameters params) throws Exception {
console.info("Read/Write HashMap<Object,Object>");
Key key = new Key(params.namespace, params.set, "mapkey2");
client.delete(params.writePolicy, key);
byte[] blob = new byte[] { 3, 52, 125 };
List<Integer> list = new ArrayList<Integer>();
list.add(100034);
list.add(12384955);
list.add(3);
list.add(512);
HashMap<Object, Object> map = new HashMap<Object, Object>();
map.put("key1", "string1");
map.put("key2", 2);
map.put("key3", blob);
// map.put("key4", Value.getAsList(list)) works too
map.put("key4", list);
map.put("key5", true);
map.put("key6", false);
Bin bin = new Bin(params.getBinName("mapbin2"), map);
client.put(params.writePolicy, key, bin);
Record record = client.get(params.policy, key, bin.name);
Map<?, ?> receivedMap = (Map<?, ?>) record.getValue(bin.name);
validateSize(6, receivedMap.size());
validate("string1", receivedMap.get("key1"));
// Server convert numbers to long, so must expect long.
validate(2L, receivedMap.get("key2"));
validate(blob, (byte[]) receivedMap.get("key3"));
List<?> receivedInner = (List<?>) receivedMap.get("key4");
validateSize(4, receivedInner.size());
validate(100034L, receivedInner.get(0));
validate(12384955L, receivedInner.get(1));
validate(3L, receivedInner.get(2));
validate(512L, receivedInner.get(3));
validate(true, receivedMap.get("key5"));
validate(false, receivedMap.get("key6"));
console.info("Read/Write HashMap<Object,Object> successful");
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class ListMap method testListStrings.
/**
* Write/Read ArrayList<String> directly instead of relying on java serializer.
*/
private void testListStrings(AerospikeClient client, Parameters params) throws Exception {
console.info("Read/Write ArrayList<String>");
Key key = new Key(params.namespace, params.set, "listkey1");
client.delete(params.writePolicy, key);
ArrayList<String> list = new ArrayList<String>();
list.add("string1");
list.add("string2");
list.add("string3");
Bin bin = new Bin(params.getBinName("listbin1"), list);
client.put(params.writePolicy, key, bin);
Record record = client.get(params.policy, key, bin.name);
List<?> receivedList = (List<?>) record.getValue(bin.name);
validateSize(3, receivedList.size());
validate("string1", receivedList.get(0));
validate("string2", receivedList.get(1));
validate("string3", receivedList.get(2));
console.info("Read/Write ArrayList<String> successful.");
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class Operate method runExample.
/**
* Demonstrate multiple operations on a single record in one call.
*/
@Override
public void runExample(AerospikeClient client, Parameters params) throws Exception {
// Write initial record.
Key key = new Key(params.namespace, params.set, "opkey");
Bin bin1 = new Bin("optintbin", 7);
Bin bin2 = new Bin("optstringbin", "string value");
console.info("Put: namespace=%s set=%s key=%s bin1=%s value1=%s bin2=%s value2=%s", key.namespace, key.setName, key.userKey, bin1.name, bin1.value, bin2.name, bin2.value);
client.put(params.writePolicy, key, bin1, bin2);
// Add integer, write new string and read record.
Bin bin3 = new Bin(bin1.name, 4);
Bin bin4 = new Bin(bin2.name, "new string");
console.info("Add: " + bin3.value);
console.info("Write: " + bin4.value);
console.info("Read:");
Record record = client.operate(params.writePolicy, key, Operation.add(bin3), Operation.put(bin4), Operation.get());
if (record == null) {
throw new Exception(String.format("Failed to get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey));
}
validateBin(key, record, bin3.name, 11, record.getInt(bin3.name));
validateBin(key, record, bin4.name, bin4.value.toString(), record.getValue(bin4.name));
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class OperateList method runSimpleExample.
/**
* Simple example of list functionality.
*/
public void runSimpleExample(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "listkey");
String binName = params.getBinName("listbin");
// Delete record if it already exists.
client.delete(params.writePolicy, key);
List<Value> inputList = new ArrayList<Value>();
inputList.add(Value.get(55));
inputList.add(Value.get(77));
// Write values to empty list.
Record record = client.operate(params.writePolicy, key, ListOperation.appendItems(binName, inputList));
console.info("Record: " + record);
// Pop value from end of list and also return new size of list.
record = client.operate(params.writePolicy, key, ListOperation.pop(binName, -1), ListOperation.size(binName));
console.info("Record: " + record);
// There should be one result for each list operation on the same list bin.
// In this case, there are two list operations (pop and size), so there
// should be two results.
List<?> list = record.getList(binName);
for (Object value : list) {
console.info("Received: " + value);
}
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class OperateMap method runSimpleExample.
/**
* Simple example of map operate functionality.
*/
public void runSimpleExample(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "mapkey");
String binName = params.getBinName("mapbin");
// Delete record if it already exists.
client.delete(params.writePolicy, key);
Map<Value, Value> inputMap = new HashMap<Value, Value>();
inputMap.put(Value.get(1), Value.get(55));
inputMap.put(Value.get(2), Value.get(33));
// Write values to empty map.
Record record = client.operate(params.writePolicy, key, MapOperation.putItems(MapPolicy.Default, binName, inputMap));
console.info("Record: " + record);
// Pop value from map and also return new size of map.
record = client.operate(params.writePolicy, key, MapOperation.removeByKey(binName, Value.get(1), MapReturnType.VALUE), MapOperation.size(binName));
console.info("Record: " + record);
// There should be one result for each map operation on the same map bin.
// In this case, there are two map operations (pop and size), so there
// should be two results.
List<?> list = record.getList(binName);
for (Object value : list) {
console.info("Received: " + value);
}
}
Aggregations