Search in sources :

Example 16 with AerospikeException

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

the class AsyncQuery method createIndex.

private void createIndex(AerospikeClient client, String indexName, String binName) {
    console.info("Create index: ns=%s set=%s index=%s bin=%s", params.namespace, params.set, indexName, binName);
    Policy policy = new Policy();
    // Do not timeout on index create.
    policy.socketTimeout = 0;
    try {
        IndexTask task = client.createIndex(policy, params.namespace, params.set, indexName, binName, IndexType.NUMERIC);
        task.waitTillComplete();
    } catch (AerospikeException ae) {
        if (ae.getResultCode() != ResultCode.INDEX_ALREADY_EXISTS) {
            throw ae;
        }
    }
}
Also used : Policy(com.aerospike.client.policy.Policy) AerospikeException(com.aerospike.client.AerospikeException) IndexTask(com.aerospike.client.task.IndexTask)

Example 17 with AerospikeException

use of com.aerospike.client.AerospikeException 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)

Example 18 with AerospikeException

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

the class QueryAverage method createIndex.

private void createIndex(AerospikeClient client, Parameters params, String indexName, String binName) throws Exception {
    console.info("Create index: ns=%s set=%s index=%s bin=%s", params.namespace, params.set, indexName, binName);
    Policy policy = new Policy();
    // Do not timeout on index create.
    policy.socketTimeout = 0;
    try {
        IndexTask task = client.createIndex(policy, params.namespace, params.set, indexName, binName, IndexType.NUMERIC);
        task.waitTillComplete();
    } catch (AerospikeException ae) {
        if (ae.getResultCode() != ResultCode.INDEX_ALREADY_EXISTS) {
            throw ae;
        }
    }
}
Also used : Policy(com.aerospike.client.policy.Policy) AerospikeException(com.aerospike.client.AerospikeException) IndexTask(com.aerospike.client.task.IndexTask)

Example 19 with AerospikeException

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

the class QueryInteger method createIndex.

private void createIndex(AerospikeClient client, Parameters params, String indexName, String binName) throws Exception {
    console.info("Create index: ns=%s set=%s index=%s bin=%s", params.namespace, params.set, indexName, binName);
    Policy policy = new Policy();
    // Do not timeout on index create.
    policy.socketTimeout = 0;
    try {
        IndexTask task = client.createIndex(policy, params.namespace, params.set, indexName, binName, IndexType.NUMERIC);
        task.waitTillComplete();
    } catch (AerospikeException ae) {
        if (ae.getResultCode() != ResultCode.INDEX_ALREADY_EXISTS) {
            throw ae;
        }
    }
}
Also used : Policy(com.aerospike.client.policy.Policy) AerospikeException(com.aerospike.client.AerospikeException) IndexTask(com.aerospike.client.task.IndexTask)

Example 20 with AerospikeException

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

the class QueryPredExp method createIndex.

private void createIndex(AerospikeClient client, Parameters params, String indexName, String binName) throws Exception {
    console.info("Create index: ns=%s set=%s index=%s bin=%s", params.namespace, params.set, indexName, binName);
    Policy policy = new Policy();
    // Do not timeout on index create.
    policy.socketTimeout = 0;
    try {
        IndexTask task = client.createIndex(policy, params.namespace, params.set, indexName, binName, IndexType.NUMERIC);
        task.waitTillComplete();
    } catch (AerospikeException ae) {
        if (ae.getResultCode() != ResultCode.INDEX_ALREADY_EXISTS) {
            throw ae;
        }
    }
}
Also used : Policy(com.aerospike.client.policy.Policy) AerospikeException(com.aerospike.client.AerospikeException) IndexTask(com.aerospike.client.task.IndexTask)

Aggregations

AerospikeException (com.aerospike.client.AerospikeException)107 Key (com.aerospike.client.Key)46 Bin (com.aerospike.client.Bin)24 Policy (com.aerospike.client.policy.Policy)24 IndexTask (com.aerospike.client.task.IndexTask)24 Record (com.aerospike.client.Record)19 Test (org.junit.Test)16 Node (com.aerospike.client.cluster.Node)12 IOException (java.io.IOException)10 BeforeClass (org.junit.BeforeClass)10 WritePolicy (com.aerospike.client.policy.WritePolicy)7 HashMap (java.util.HashMap)7 Statement (com.aerospike.client.query.Statement)6 RegisterTask (com.aerospike.client.task.RegisterTask)6 ArrayList (java.util.ArrayList)6 AerospikeClient (com.aerospike.client.AerospikeClient)5 Value (com.aerospike.client.Value)5 RecordSequenceListener (com.aerospike.client.listener.RecordSequenceListener)5 RecordSet (com.aerospike.client.query.RecordSet)5 HashedWheelTimeout (com.aerospike.client.async.HashedWheelTimer.HashedWheelTimeout)4