Search in sources :

Example 1 with RecordSequenceListener

use of com.aerospike.client.listener.RecordSequenceListener in project aerospike-client-java by aerospike.

the class AsyncScanPage method runScan.

private void runScan(AerospikeClient client, EventLoop eventLoop) {
    int pageSize = 30;
    console.info("Scan max " + pageSize + " records.");
    ScanPolicy policy = new ScanPolicy();
    policy.maxRecords = pageSize;
    PartitionFilter filter = PartitionFilter.all();
    RecordSequenceListener listener = new RecordSequenceListener() {

        private int count = 0;

        @Override
        public void onRecord(Key key, Record record) throws AerospikeException {
            count++;
        }

        @Override
        public void onSuccess() {
            console.info("Records returned: " + count);
            notifyComplete();
        }

        @Override
        public void onFailure(AerospikeException e) {
            console.error("Scan failed: " + Util.getErrorMessage(e));
            notifyComplete();
        }
    };
    client.scanPartitions(eventLoop, listener, policy, filter, params.namespace, setName);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) PartitionFilter(com.aerospike.client.query.PartitionFilter) RecordSequenceListener(com.aerospike.client.listener.RecordSequenceListener) ScanPolicy(com.aerospike.client.policy.ScanPolicy) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 2 with RecordSequenceListener

use of com.aerospike.client.listener.RecordSequenceListener in project aerospike-client-java by aerospike.

the class AsyncScan method runExample.

/**
 * Asynchronous scan example.
 */
@Override
public void runExample(AerospikeClient client, EventLoop eventLoop) {
    console.info("Asynchronous scan: namespace=" + params.namespace + " set=" + params.set);
    recordCount = 0;
    final long begin = System.currentTimeMillis();
    ScanPolicy policy = new ScanPolicy();
    client.scanAll(eventLoop, new RecordSequenceListener() {

        @Override
        public void onRecord(Key key, Record record) throws AerospikeException {
            recordCount++;
            if ((recordCount % 10000) == 0) {
                console.info("Records " + recordCount);
            }
        }

        @Override
        public void onSuccess() {
            long end = System.currentTimeMillis();
            double seconds = (double) (end - begin) / 1000.0;
            console.info("Total records returned: " + recordCount);
            console.info("Elapsed time: " + seconds + " seconds");
            double performance = Math.round((double) recordCount / seconds);
            console.info("Records/second: " + performance);
            notifyComplete();
        }

        @Override
        public void onFailure(AerospikeException e) {
            console.error("Scan failed: " + Util.getErrorMessage(e));
            notifyComplete();
        }
    }, policy, params.namespace, params.set);
    // Wait until scan finishes before closing cluster.  This is only necessary
    // when running the async scan example from the command line (which closes the
    // cluster after control is relinquished by this example).
    // The async scan will continue to run after cluster close if the scan was
    // initiated before cluster close.  The problem is cluster close shuts down
    // cluster tending immediately so any data partition migrations will not be
    // received by the client when performing the scan.
    waitTillComplete();
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) RecordSequenceListener(com.aerospike.client.listener.RecordSequenceListener) ScanPolicy(com.aerospike.client.policy.ScanPolicy) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 3 with RecordSequenceListener

use of com.aerospike.client.listener.RecordSequenceListener in project aerospike-client-java by aerospike.

the class AsyncQuery method runQuery.

private void runQuery(AerospikeClient client, EventLoop eventLoop, final String binName) {
    int begin = 26;
    int end = 34;
    console.info("Query for: ns=%s set=%s bin=%s >= %s <= %s", params.namespace, params.set, binName, begin, end);
    Statement stmt = new Statement();
    stmt.setNamespace(params.namespace);
    stmt.setSetName(params.set);
    stmt.setBinNames(binName);
    stmt.setFilter(Filter.range(binName, begin, end));
    final AtomicInteger count = new AtomicInteger();
    client.query(eventLoop, new RecordSequenceListener() {

        public void onRecord(Key key, Record record) throws AerospikeException {
            int result = record.getInt(binName);
            console.info("Record found: ns=%s set=%s bin=%s digest=%s value=%s", key.namespace, key.setName, binName, Buffer.bytesToHexString(key.digest), result);
            count.incrementAndGet();
        }

        public void onSuccess() {
            int size = count.get();
            if (size != 9) {
                console.error("Query count mismatch. Expected 9. Received " + size);
            }
            notifyComplete();
        }

        public void onFailure(AerospikeException e) {
            console.error("Query failed: " + Util.getErrorMessage(e));
            notifyComplete();
        }
    }, null, stmt);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) RecordSequenceListener(com.aerospike.client.listener.RecordSequenceListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Statement(com.aerospike.client.query.Statement) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 4 with RecordSequenceListener

use of com.aerospike.client.listener.RecordSequenceListener in project aerospike-client-java by aerospike.

the class TestAsyncQuery method runQuery.

private void runQuery() {
    int begin = 26;
    int end = 34;
    Statement stmt = new Statement();
    stmt.setNamespace(args.namespace);
    stmt.setSetName(args.set);
    stmt.setBinNames(binName);
    stmt.setFilter(Filter.range(binName, begin, end));
    final AtomicInteger count = new AtomicInteger();
    client.query(eventLoop, new RecordSequenceListener() {

        public void onRecord(Key key, Record record) throws AerospikeException {
            int result = record.getInt(binName);
            assertBetween(26, 34, result);
            count.incrementAndGet();
        }

        public void onSuccess() {
            int size = count.get();
            assertEquals(9, size);
            notifyComplete();
        }

        public void onFailure(AerospikeException e) {
            setError(e);
            notifyComplete();
        }
    }, null, stmt);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) RecordSequenceListener(com.aerospike.client.listener.RecordSequenceListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Statement(com.aerospike.client.query.Statement) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 5 with RecordSequenceListener

use of com.aerospike.client.listener.RecordSequenceListener in project aerospike-client-java by aerospike.

the class TestAsyncBatch method asyncBatchGetSequence.

@Test
public void asyncBatchGetSequence() throws Exception {
    client.get(eventLoop, new RecordSequenceListener() {

        public void onRecord(Key key, Record record) {
            if (assertRecordFound(key, record)) {
                Object value = record.getValue(BinName);
                assertNotNull(value);
            }
        }

        public void onSuccess() {
            notifyComplete();
        }

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

Aggregations

AerospikeException (com.aerospike.client.AerospikeException)6 Key (com.aerospike.client.Key)6 Record (com.aerospike.client.Record)6 RecordSequenceListener (com.aerospike.client.listener.RecordSequenceListener)6 ScanPolicy (com.aerospike.client.policy.ScanPolicy)3 Statement (com.aerospike.client.query.Statement)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2 PartitionFilter (com.aerospike.client.query.PartitionFilter)1