Search in sources :

Example 6 with BatchRead

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

the class TestAsyncBatch method asyncBatchReadComplex.

@Test
public void asyncBatchReadComplex() 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(args.namespace, args.set, keyPrefix + 1), bins));
    records.add(new BatchRead(new Key(args.namespace, args.set, keyPrefix + 2), true));
    records.add(new BatchRead(new Key(args.namespace, args.set, keyPrefix + 3), true));
    records.add(new BatchRead(new Key(args.namespace, args.set, keyPrefix + 4), false));
    records.add(new BatchRead(new Key(args.namespace, args.set, keyPrefix + 5), true));
    records.add(new BatchRead(new Key(args.namespace, args.set, keyPrefix + 6), true));
    records.add(new BatchRead(new Key(args.namespace, args.set, keyPrefix + 7), bins));
    // This record should be found, but the requested bin will not be found.
    records.add(new BatchRead(new Key(args.namespace, args.set, keyPrefix + 8), new String[] { "binnotfound" }));
    // This record should not be found.
    records.add(new BatchRead(new Key(args.namespace, args.set, "keynotfound"), bins));
    // Execute batch.
    client.get(eventLoop, new BatchListListener() {

        public void onSuccess(List<BatchRead> records) {
            // Show results.
            int found = 0;
            int count = 0;
            for (BatchRead record : records) {
                Record rec = record.record;
                count++;
                if (rec != null) {
                    found++;
                    Object value = rec.getValue(binName);
                    if (count != 4 && count <= 7) {
                        if (!assertEquals(valuePrefix + count, value)) {
                            notifyComplete();
                            return;
                        }
                    } else {
                        if (!assertNull(value)) {
                            notifyComplete();
                            return;
                        }
                    }
                }
            }
            assertEquals(8, found);
            notifyComplete();
        }

        public void onFailure(AerospikeException e) {
            setError(e);
            notifyComplete();
        }
    }, null, records);
    waitTillComplete();
}
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) Test(org.junit.Test)

Example 7 with BatchRead

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

the class Batch 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(AerospikeClient client, Parameters params, String keyPrefix, String binName) 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(null, 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);
    }
}
Also used : ArrayList(java.util.ArrayList) BatchRead(com.aerospike.client.BatchRead) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Aggregations

BatchRead (com.aerospike.client.BatchRead)7 Key (com.aerospike.client.Key)5 ArrayList (java.util.ArrayList)4 Record (com.aerospike.client.Record)3 AerospikeException (com.aerospike.client.AerospikeException)2 BatchListListener (com.aerospike.client.listener.BatchListListener)2 Test (org.junit.Test)2