Search in sources :

Example 51 with Record

use of com.aerospike.client.Record 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));
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 52 with Record

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

the class UserDefinedFunction method appendListUsingUdf.

private void appendListUsingUdf(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "udfkey5");
    String binName = params.getBinName("udfbin5");
    String value = "appended value";
    client.execute(params.writePolicy, key, "record_example", "appendListBin", Value.get(binName), Value.get(value));
    Record record = client.get(params.policy, key, binName);
    if (record != null) {
        Object received = record.getValue(binName);
        if (received != null && received instanceof List<?>) {
            List<?> list = (List<?>) received;
            if (list.size() == 5) {
                Object obj = list.get(4);
                if (obj.equals(value)) {
                    console.info("UDF data matched: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, binName, received);
                    return;
                }
            }
        }
        console.error("UDF data mismatch");
        console.error("Expected: " + value);
        console.error("Received: " + received);
    } else {
        console.error("Failed to find record: " + key.userKey);
    }
}
Also used : Record(com.aerospike.client.Record) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.aerospike.client.Key)

Example 53 with Record

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

the class UserDefinedFunction method writeUsingUdf.

private void writeUsingUdf(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "udfkey1");
    Bin bin = new Bin(params.getBinName("udfbin1"), "string value");
    client.execute(params.writePolicy, key, "record_example", "writeBin", Value.get(bin.name), bin.value);
    Record record = client.get(params.policy, key, bin.name);
    String expected = bin.value.toString();
    String received = record.getString(bin.name);
    if (received != null && received.equals(expected)) {
        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: Expected %s. Received %s.", expected, received);
    }
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 54 with Record

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

the class TestAsyncBatch method asyncBatchGetHeaders.

@Test
public void asyncBatchGetHeaders() throws Exception {
    client.getHeader(eventLoop, new RecordArrayListener() {

        public void onSuccess(Key[] keys, Record[] records) {
            if (assertEquals(Size, records.length)) {
                for (int i = 0; i < records.length; i++) {
                    Record record = records[i];
                    if (!assertRecordFound(keys[i], record)) {
                        break;
                    }
                    if (!assertGreaterThanZero(record.generation)) {
                        break;
                    }
                    if (!assertGreaterThanZero(record.expiration)) {
                        break;
                    }
                }
            }
            notifyComplete();
        }

        public void onFailure(AerospikeException e) {
            setError(e);
            notifyComplete();
        }
    }, null, sendKeys);
    waitTillComplete();
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) RecordArrayListener(com.aerospike.client.listener.RecordArrayListener) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 55 with Record

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

the class TestAsyncBatch method asyncBatchListOperate.

@Test
public void asyncBatchListOperate() throws Exception {
    client.get(eventLoop, new RecordArrayListener() {

        public void onSuccess(Key[] keys, Record[] records) {
            if (assertEquals(Size, records.length)) {
                for (int i = 0; i < records.length; i++) {
                    Record record = records[i];
                    List<?> results = record.getList(ListBin);
                    long size = (Long) results.get(0);
                    long val = (Long) results.get(1);
                    assertEquals(i + 1, size);
                    assertEquals(i * (i + 1), val);
                }
            }
            notifyComplete();
        }

        public void onFailure(AerospikeException e) {
            setError(e);
            notifyComplete();
        }
    }, null, sendKeys, ListOperation.size(ListBin), ListOperation.getByIndex(ListBin, -1, ListReturnType.VALUE));
    waitTillComplete();
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) RecordArrayListener(com.aerospike.client.listener.RecordArrayListener) Record(com.aerospike.client.Record) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.aerospike.client.Key) Test(org.junit.Test)

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