Search in sources :

Example 36 with Record

use of com.aerospike.client.Record in project aerospike-client-java by aerospike.

the class OperateMap method runNestedListCreateExample.

public void runNestedListCreateExample(AerospikeClient client, Parameters params) {
    Key key = new Key(params.namespace, params.set, "mapkey3");
    String binName = params.getBinName("mapbin");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    List<Value> l1 = new ArrayList<Value>();
    l1.add(Value.get(7));
    l1.add(Value.get(9));
    l1.add(Value.get(5));
    Map<Value, Value> inputMap = new HashMap<Value, Value>();
    inputMap.put(Value.get("key1"), Value.get(l1));
    // Create maps.
    client.put(params.writePolicy, key, new Bin(binName, inputMap));
    // Create ordered list at map's "key2" only if list does not exist.
    // Append 2,1 to ordered list.
    CTX ctx = CTX.mapKey(Value.get("key2"));
    Record record = client.operate(params.writePolicy, key, ListOperation.create(binName, ListOrder.ORDERED, false, ctx), ListOperation.append(binName, Value.get(2), ctx), ListOperation.append(binName, Value.get(1), ctx), Operation.get(binName));
    record = client.get(params.policy, key);
    console.info("Record: " + record);
}
Also used : HashMap(java.util.HashMap) Bin(com.aerospike.client.Bin) CTX(com.aerospike.client.cdt.CTX) Value(com.aerospike.client.Value) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 37 with Record

use of com.aerospike.client.Record in project aerospike-client-java by aerospike.

the class Prepend method runExample.

/**
 * Prepend string to an existing string.
 */
@Override
public void runExample(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "prependkey");
    String binName = params.getBinName("prependbin");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    Bin bin = new Bin(binName, "World");
    console.info("Initial prepend will create record.  Initial value is " + bin.value + '.');
    client.prepend(params.writePolicy, key, bin);
    bin = new Bin(binName, "Hello ");
    console.info("Prepend \"" + bin.value + "\" to existing record.");
    client.prepend(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));
    }
    // The value received from the server is an unsigned byte stream.
    // Convert to an integer before comparing with expected.
    Object received = record.getValue(bin.name);
    String expected = "Hello World";
    if (received.equals(expected)) {
        console.info("Prepend successful: ns=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, received);
    } else {
        console.error("Prepend mismatch: Expected %s. Received %s.", expected, received);
    }
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 38 with Record

use of com.aerospike.client.Record in project aerospike-client-java by aerospike.

the class QueryExecute method validateRecords.

private void validateRecords(AerospikeClient client, Parameters params, String indexName, String binName1, String binName2, int size) throws Exception {
    int begin = 1;
    int end = size + 100;
    console.info("Validate records");
    Statement stmt = new Statement();
    stmt.setNamespace(params.namespace);
    stmt.setSetName(params.set);
    stmt.setFilter(Filter.range(binName1, begin, end));
    RecordSet rs = client.query(null, stmt);
    try {
        int[] expectedList = new int[] { 1, 2, 3, 104, 5, 106, 7, 108, -1, 10 };
        int expectedSize = size - 1;
        int count = 0;
        while (rs.next()) {
            Key key = rs.getKey();
            Record record = rs.getRecord();
            int value1 = record.getInt(binName1);
            int value2 = record.getInt(binName2);
            console.info("Record found: ns=%s set=%s bin1=%s value1=%s bin2=%s value2=%s", key.namespace, key.setName, binName1, value1, binName2, value2);
            int val1 = value1;
            if (val1 == 9) {
                console.error("Data mismatch. value1 " + val1 + " should not exist");
                break;
            }
            if (val1 == 5) {
                if (value2 != 0) {
                    console.error("Data mismatch. value2 " + value2 + " should be null");
                    break;
                }
            } else if (value1 != expectedList[value2 - 1]) {
                console.error("Data mismatch. Expected " + expectedList[value2 - 1] + ". Received " + value1);
                break;
            }
            count++;
        }
        if (count != expectedSize) {
            console.error("Query count mismatch. Expected " + expectedSize + ". Received " + count);
        }
    } finally {
        rs.close();
    }
}
Also used : Statement(com.aerospike.client.query.Statement) Record(com.aerospike.client.Record) RecordSet(com.aerospike.client.query.RecordSet) Key(com.aerospike.client.Key)

Example 39 with Record

use of com.aerospike.client.Record in project aerospike-client-java by aerospike.

the class OperateList method runNestedExample.

/**
 * Operate on a list of lists.
 */
public void runNestedExample(AerospikeClient client, Parameters params) {
    Key key = new Key(params.namespace, params.set, "listkey2");
    String binName = params.getBinName("listbin");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    List<Value> l1 = new ArrayList<Value>();
    l1.add(Value.get(7));
    l1.add(Value.get(9));
    l1.add(Value.get(5));
    List<Value> l2 = new ArrayList<Value>();
    l2.add(Value.get(1));
    l2.add(Value.get(2));
    l2.add(Value.get(3));
    List<Value> l3 = new ArrayList<Value>();
    l3.add(Value.get(6));
    l3.add(Value.get(5));
    l3.add(Value.get(4));
    l3.add(Value.get(1));
    List<Value> inputList = new ArrayList<Value>();
    inputList.add(Value.get(l1));
    inputList.add(Value.get(l2));
    inputList.add(Value.get(l3));
    // Create list.
    client.put(params.writePolicy, key, new Bin(binName, inputList));
    // Append value to last list and retrieve all lists.
    Record record = client.operate(params.writePolicy, key, ListOperation.append(binName, Value.get(11), CTX.listIndex(-1)), Operation.get(binName));
    record = client.get(params.policy, key);
    console.info("Record: " + record);
}
Also used : Bin(com.aerospike.client.Bin) Value(com.aerospike.client.Value) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 40 with Record

use of com.aerospike.client.Record in project aerospike-client-java by aerospike.

the class OperateList method runSimpleExample.

/**
 * Simple example of list functionality.
 */
public void runSimpleExample(AerospikeClient client, Parameters params) {
    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);
    }
}
Also used : Value(com.aerospike.client.Value) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Aggregations

Record (com.aerospike.client.Record)259 Key (com.aerospike.client.Key)140 Test (org.junit.Test)139 ArrayList (java.util.ArrayList)75 Bin (com.aerospike.client.Bin)70 AerospikeException (com.aerospike.client.AerospikeException)62 Value (com.aerospike.client.Value)54 WritePolicy (com.aerospike.client.policy.WritePolicy)47 List (java.util.List)41 HashMap (java.util.HashMap)36 ThrowingRunnable (org.junit.function.ThrowingRunnable)32 BatchPolicy (com.aerospike.client.policy.BatchPolicy)29 Policy (com.aerospike.client.policy.Policy)29 Statement (com.aerospike.client.query.Statement)23 RecordSet (com.aerospike.client.query.RecordSet)22 Expression (com.aerospike.client.exp.Expression)18 Map (java.util.Map)15 HLLValue (com.aerospike.client.Value.HLLValue)14 Collections.singletonList (java.util.Collections.singletonList)9 MapPolicy (com.aerospike.client.cdt.MapPolicy)7