Search in sources :

Example 11 with Record

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

the class RWTaskSync method get.

@Override
protected void get(Key key, String binName) {
    Record record;
    if (counters.read.latency != null) {
        long begin = System.nanoTime();
        record = client.get(args.readPolicy, key, binName);
        long elapsed = System.nanoTime() - begin;
        counters.read.latency.add(elapsed);
    } else {
        record = client.get(args.readPolicy, key, binName);
    }
    processRead(key, record);
}
Also used : Record(com.aerospike.client.Record)

Example 12 with Record

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

the class BatchOperate method batchReadOperateComplex.

/**
 * Read results using varying read operations in one batch.
 */
private void batchReadOperateComplex(AerospikeClient client, Parameters params) {
    console.info("batchReadOperateComplex");
    Expression exp1 = Exp.build(Exp.mul(Exp.intBin(BinName1), Exp.intBin(BinName2)));
    Expression exp2 = Exp.build(Exp.add(Exp.intBin(BinName1), Exp.intBin(BinName2)));
    Expression exp3 = Exp.build(Exp.sub(Exp.intBin(BinName1), Exp.intBin(BinName2)));
    // Batch uses pointer reference to quickly determine if operations are repeated and can therefore
    // be optimized, but using varargs directly always creates a new reference. Therefore, save operation
    // array so we have one pointer reference per operation array.
    Operation[] ops1 = Operation.array(ExpOperation.read(ResultName1, exp1, ExpReadFlags.DEFAULT));
    Operation[] ops2 = Operation.array(ExpOperation.read(ResultName1, exp2, ExpReadFlags.DEFAULT));
    Operation[] ops3 = Operation.array(ExpOperation.read(ResultName1, exp3, ExpReadFlags.DEFAULT));
    Operation[] ops4 = Operation.array(ExpOperation.read(ResultName1, exp2, ExpReadFlags.DEFAULT), ExpOperation.read(ResultName2, exp3, ExpReadFlags.DEFAULT));
    List<BatchRead> records = new ArrayList<BatchRead>();
    records.add(new BatchRead(new Key(params.namespace, params.set, KeyPrefix + 1), ops1));
    // The following record is optimized (namespace,set,ops are only sent once) because
    // namespace, set and ops all have the same pointer references as the previous entry.
    records.add(new BatchRead(new Key(params.namespace, params.set, KeyPrefix + 2), ops1));
    records.add(new BatchRead(new Key(params.namespace, params.set, KeyPrefix + 3), ops2));
    records.add(new BatchRead(new Key(params.namespace, params.set, KeyPrefix + 4), ops3));
    records.add(new BatchRead(new Key(params.namespace, params.set, KeyPrefix + 5), ops4));
    // Execute batch.
    client.get(null, records);
    // Show results.
    int count = 0;
    for (BatchRead record : records) {
        Record rec = record.record;
        Object v1 = rec.getValue(ResultName1);
        Object v2 = rec.getValue(ResultName2);
        console.info("Result[%d]: %s, %s", count++, v1, v2);
    }
}
Also used : Expression(com.aerospike.client.exp.Expression) ArrayList(java.util.ArrayList) BatchRead(com.aerospike.client.BatchRead) Record(com.aerospike.client.Record) ListOperation(com.aerospike.client.cdt.ListOperation) ExpOperation(com.aerospike.client.exp.ExpOperation) Operation(com.aerospike.client.Operation) Key(com.aerospike.client.Key)

Example 13 with Record

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

the class Expire method expireExample.

/**
 * Write and twice read an expiration record.
 */
private void expireExample(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "expirekey ");
    Bin bin = new Bin(params.getBinName("expirebin"), "expirevalue");
    console.info("Put: namespace=%s set=%s key=%s bin=%s value=%s expiration=2", key.namespace, key.setName, key.userKey, bin.name, bin.value);
    // Specify that record expires 2 seconds after it's written.
    WritePolicy writePolicy = new WritePolicy();
    writePolicy.expiration = 2;
    client.put(writePolicy, key, bin);
    // Read the record before it expires, showing it is there.
    console.info("Get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey);
    Record record = client.get(params.policy, key, bin.name);
    if (record == null) {
        throw new Exception(String.format("Failed to get record: 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 record successful: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, received);
    } else {
        throw new Exception(String.format("Expire record mismatch: Expected %s. Received %s.", expected, received));
    }
    // Read the record after it expires, showing it's gone.
    console.info("Sleeping for 3 seconds ...");
    Thread.sleep(3 * 1000);
    record = client.get(params.policy, key, bin.name);
    if (record == null) {
        console.info("Expiry of record successful. Record not found.");
    } else {
        console.error("Found record when it should have expired.");
    }
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy)

Example 14 with Record

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

Example 15 with Record

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

the class AsyncBatch method batchReadComplex.

/**
 * Read records with varying namespaces, bin names and read types in one batch.
 * This requires Aerospike Server version >= 3.6.0
 */
private void batchReadComplex() throws Exception {
    // Batch gets into one call.
    // Batch allows multiple namespaces in one call, but example test environment may only have one namespace.
    String[] bins = new String[] { binName };
    List<BatchRead> records = new ArrayList<BatchRead>();
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 1), bins));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 2), true));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 3), true));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 4), false));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 5), true));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 6), true));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 7), bins));
    // This record should be found, but the requested bin will not be found.
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 8), new String[] { "binnotfound" }));
    // This record should not be found.
    records.add(new BatchRead(new Key(params.namespace, params.set, "keynotfound"), bins));
    // Execute batch.
    client.get(eventLoop, new BatchListListener() {

        public void onSuccess(List<BatchRead> records) {
            // Show results.
            int found = 0;
            for (BatchRead record : records) {
                Key key = record.key;
                Record rec = record.record;
                if (rec != null) {
                    found++;
                    console.info("Record: ns=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, binName, rec.getValue(binName));
                } else {
                    console.info("Record not found: ns=%s set=%s key=%s bin=%s", key.namespace, key.setName, key.userKey, binName);
                }
            }
            if (found != 8) {
                console.error("Records found mismatch. Expected %d. Received %d.", 8, found);
            }
        }

        public void onFailure(AerospikeException e) {
            console.error("Batch read complex failed: " + Util.getErrorMessage(e));
        }
    }, null, records);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) BatchListListener(com.aerospike.client.listener.BatchListListener) ArrayList(java.util.ArrayList) BatchRead(com.aerospike.client.BatchRead) 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