Search in sources :

Example 56 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 57 with AerospikeException

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

the class Util method readResource.

public static byte[] readResource(ClassLoader resourceLoader, String resourcePath) {
    try {
        URL url = resourceLoader.getResource(resourcePath);
        if (url == null) {
            throw new IllegalArgumentException("Resource: " + resourcePath + " not found");
        }
        InputStream is = url.openStream();
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
            byte[] bytes = new byte[8192];
            int length;
            while ((length = is.read(bytes)) > 0) {
                bos.write(bytes, 0, length);
            }
            return bos.toByteArray();
        } finally {
            is.close();
        }
    } catch (Exception e) {
        throw new AerospikeException("Failed to read resource " + resourcePath, e);
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) EOFException(java.io.EOFException) AerospikeException(com.aerospike.client.AerospikeException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) ParseException(java.text.ParseException)

Example 58 with AerospikeException

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

Example 59 with AerospikeException

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

the class Replace method runReplaceOnlyExample.

public void runReplaceOnlyExample(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "replaceonlykey");
    Bin bin = new Bin("bin", "value");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    console.info("Replace record requiring that it exists: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey);
    try {
        WritePolicy policy = new WritePolicy();
        policy.recordExistsAction = RecordExistsAction.REPLACE_ONLY;
        client.put(policy, key, bin);
        console.error("Failure. This command should have resulted in an error.");
    } catch (AerospikeException ae) {
        if (ae.getResultCode() == ResultCode.KEY_NOT_FOUND_ERROR) {
            console.info("Success. Key not found error returned as expected.");
        } else {
            throw ae;
        }
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy)

Example 60 with AerospikeException

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

the class TestAsyncQuery method initialize.

@BeforeClass
public static void initialize() {
    Policy policy = new Policy();
    // Do not timeout on index create.
    policy.socketTimeout = 0;
    try {
        IndexTask task = client.createIndex(policy, args.namespace, args.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) BeforeClass(org.junit.BeforeClass)

Aggregations

AerospikeException (com.aerospike.client.AerospikeException)175 Record (com.aerospike.client.Record)58 Test (org.junit.Test)58 Policy (com.aerospike.client.policy.Policy)57 Key (com.aerospike.client.Key)56 WritePolicy (com.aerospike.client.policy.WritePolicy)42 ThrowingRunnable (org.junit.function.ThrowingRunnable)41 Bin (com.aerospike.client.Bin)32 IndexTask (com.aerospike.client.task.IndexTask)29 BatchPolicy (com.aerospike.client.policy.BatchPolicy)28 BeforeClass (org.junit.BeforeClass)14 IOException (java.io.IOException)12 Node (com.aerospike.client.cluster.Node)11 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)8 RegisterTask (com.aerospike.client.task.RegisterTask)7 Expression (com.aerospike.client.exp.Expression)6 SocketTimeoutException (java.net.SocketTimeoutException)6 AerospikeClient (com.aerospike.client.AerospikeClient)5 Value (com.aerospike.client.Value)5