Search in sources :

Example 16 with Record

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

the class AsyncQuery method parseRow.

@Override
protected void parseRow(Key key) throws AerospikeException {
    if (resultCode != 0) {
        throw new AerospikeException(resultCode);
    }
    Record record = parseRecord();
    listener.onRecord(key, record);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Record(com.aerospike.client.Record)

Example 17 with Record

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

the class AsyncScanPartition method parseRow.

@Override
protected void parseRow(Key key) {
    if ((info3 & Command.INFO3_PARTITION_DONE) != 0) {
        // specified partition will need to be requested on the scan retry.
        if (resultCode == 0) {
            tracker.partitionDone(nodePartitions, generation);
        }
        return;
    }
    if (resultCode != 0) {
        throw new AerospikeException(resultCode);
    }
    Record record = parseRecord();
    listener.onRecord(key, record);
    tracker.setDigest(nodePartitions, key);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Record(com.aerospike.client.Record)

Example 18 with Record

use of com.aerospike.client.Record 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 19 with Record

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

the class Batch method batchReadHeaders.

/**
 * Read record header data in one batch.
 */
private void batchReadHeaders(AerospikeClient client, Parameters params, String keyPrefix, int size) throws Exception {
    // Batch gets into one call.
    Key[] keys = new Key[size];
    for (int i = 0; i < size; i++) {
        keys[i] = new Key(params.namespace, params.set, keyPrefix + (i + 1));
    }
    Record[] records = client.getHeader(null, keys);
    for (int i = 0; i < records.length; i++) {
        Key key = keys[i];
        Record record = records[i];
        Level level = Level.ERROR;
        int generation = 0;
        int expiration = 0;
        if (record != null && (record.generation > 0 || record.expiration > 0)) {
            level = Level.INFO;
            generation = record.generation;
            expiration = record.expiration;
        }
        console.write(level, "Record: ns=%s set=%s key=%s generation=%d expiration=%d", key.namespace, key.setName, key.userKey, generation, expiration);
    }
    if (records.length != size) {
        console.error("Record size mismatch. Expected %d. Received %d.", size, records.length);
    }
}
Also used : Record(com.aerospike.client.Record) Level(com.aerospike.client.Log.Level) Key(com.aerospike.client.Key)

Example 20 with Record

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

the class Generation method runExample.

/**
 * Exercise record generation functionality.
 */
@Override
public void runExample(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "genkey");
    String binName = params.getBinName("genbin");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    // Set some values for the same record.
    Bin bin = new Bin(binName, "genvalue1");
    console.info("Put: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, bin.value);
    client.put(params.writePolicy, key, bin);
    bin = new Bin(binName, "genvalue2");
    console.info("Put: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, bin.value);
    client.put(params.writePolicy, key, bin);
    // Retrieve record and its generation count.
    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 = bin.value.toString();
    if (received.equals(expected)) {
        console.info("Get successful: namespace=%s set=%s key=%s bin=%s value=%s generation=%d", key.namespace, key.setName, key.userKey, bin.name, received, record.generation);
    } else {
        throw new Exception(String.format("Get mismatch: Expected %s. Received %s.", expected, received));
    }
    // Set record and fail if it's not the expected generation.
    bin = new Bin(binName, "genvalue3");
    console.info("Put: namespace=%s set=%s key=%s bin=%s value=%s expected generation=%d", key.namespace, key.setName, key.userKey, bin.name, bin.value, record.generation);
    WritePolicy writePolicy = new WritePolicy();
    writePolicy.generationPolicy = GenerationPolicy.EXPECT_GEN_EQUAL;
    writePolicy.generation = record.generation;
    client.put(writePolicy, key, bin);
    // Set record with invalid generation and check results .
    bin = new Bin(binName, "genvalue4");
    writePolicy.generation = 9999;
    console.info("Put: namespace=%s set=%s key=%s bin=%s value=%s expected generation=%d", key.namespace, key.setName, key.userKey, bin.name, bin.value, writePolicy.generation);
    try {
        client.put(writePolicy, key, bin);
        throw new Exception("Should have received generation error instead of success.");
    } catch (AerospikeException ae) {
        if (ae.getResultCode() == ResultCode.GENERATION_ERROR) {
            console.info("Success: Generation error returned as expected.");
        } else {
            throw new Exception(String.format("Unexpected set return code: namespace=%s set=%s key=%s bin=%s value=%s code=%s", key.namespace, key.setName, key.userKey, bin.name, bin.value, ae.getResultCode()));
        }
    }
    // Verify results.
    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));
    }
    received = record.getValue(bin.name);
    expected = "genvalue3";
    if (received.equals(expected)) {
        console.info("Get successful: namespace=%s set=%s key=%s bin=%s value=%s generation=%d", key.namespace, key.setName, key.userKey, bin.name, received, record.generation);
    } else {
        throw new Exception(String.format("Get mismatch: Expected %s. Received %s.", expected, received));
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) AerospikeException(com.aerospike.client.AerospikeException) WritePolicy(com.aerospike.client.policy.WritePolicy)

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