Search in sources :

Example 11 with Expression

use of com.aerospike.client.exp.Expression in project aerospike-client-java by aerospike.

the class TestExpOperation method expReadOnWriteEvalError.

@Test
public void expReadOnWriteEvalError() {
    Expression wexp = Exp.build(Exp.intBin(binD));
    Expression rexp = Exp.build(Exp.intBin(binA));
    Record record = client.operate(null, keyA, ExpOperation.write(binD, wexp, ExpWriteFlags.DEFAULT), ExpOperation.read(expVar, rexp, ExpReadFlags.DEFAULT));
    assertRecordFound(keyA, record);
    // System.out.println(record);
    AerospikeException ae = assertThrows(AerospikeException.class, new ThrowingRunnable() {

        public void run() {
            client.operate(null, keyB, ExpOperation.write(binD, wexp, ExpWriteFlags.DEFAULT), ExpOperation.read(expVar, rexp, ExpReadFlags.DEFAULT));
        }
    });
    assertEquals(ResultCode.OP_NOT_APPLICABLE, ae.getResultCode());
    // Add NO_FAIL.
    record = client.operate(null, keyB, ExpOperation.read(expVar, rexp, ExpReadFlags.EVAL_NO_FAIL));
    assertRecordFound(keyB, record);
// System.out.println(record);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Expression(com.aerospike.client.exp.Expression) Record(com.aerospike.client.Record) ThrowingRunnable(org.junit.function.ThrowingRunnable) Test(org.junit.Test)

Example 12 with Expression

use of com.aerospike.client.exp.Expression 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.
    // bin * 8
    Expression exp = Exp.build(Exp.mul(Exp.intBin(BinName), Exp.val(8)));
    Operation[] ops = Operation.array(ExpOperation.read(BinName, exp, ExpReadFlags.DEFAULT));
    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), ops));
    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++;
                    if (count != 4 && count != 6 && count <= 7) {
                        Object value = rec.getValue(BinName);
                        if (!assertEquals(ValuePrefix + count, value)) {
                            notifyComplete();
                            return;
                        }
                    } else if (count == 6) {
                        int value = rec.getInt(BinName);
                        if (!assertEquals(48, value)) {
                            notifyComplete();
                            return;
                        }
                    } else {
                        Object value = rec.getValue(BinName);
                        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) ArrayList(java.util.ArrayList) BatchRead(com.aerospike.client.BatchRead) ListOperation(com.aerospike.client.cdt.ListOperation) ExpOperation(com.aerospike.client.exp.ExpOperation) Operation(com.aerospike.client.Operation) BatchListListener(com.aerospike.client.listener.BatchListListener) Expression(com.aerospike.client.exp.Expression) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 13 with Expression

use of com.aerospike.client.exp.Expression in project aerospike-client-java by aerospike.

the class TestExpOperation method expReturnsBlob.

@Test
public void expReturnsBlob() {
    byte[] bytes = new byte[] { 0x78, 0x78, 0x78 };
    Expression exp = Exp.build(Exp.val(bytes));
    Record record = client.operate(null, keyA, ExpOperation.write(binC, exp, ExpWriteFlags.DEFAULT), Operation.get(binC), ExpOperation.read(expVar, exp, ExpReadFlags.DEFAULT));
    assertRecordFound(keyA, record);
    // System.out.println(record);
    List<?> results = record.getList(binC);
    byte[] val = (byte[]) results.get(1);
    String resultString = "bytes not equal";
    assertArrayEquals(resultString, bytes, val);
    val = (byte[]) record.getValue(expVar);
    assertArrayEquals(resultString, bytes, val);
    record = client.operate(null, keyA, ExpOperation.read(expVar, exp, ExpReadFlags.DEFAULT));
    val = (byte[]) record.getValue(expVar);
    assertArrayEquals(resultString, bytes, val);
}
Also used : Expression(com.aerospike.client.exp.Expression) Record(com.aerospike.client.Record) Test(org.junit.Test)

Example 14 with Expression

use of com.aerospike.client.exp.Expression in project aerospike-client-java by aerospike.

the class TestExpOperation method expWritePolicyError.

@Test
public void expWritePolicyError() {
    Expression wexp = Exp.build(Exp.add(Exp.intBin(binA), Exp.val(4)));
    AerospikeException ae = assertThrows(AerospikeException.class, new ThrowingRunnable() {

        public void run() {
            client.operate(null, keyA, ExpOperation.write(binC, wexp, ExpWriteFlags.UPDATE_ONLY));
        }
    });
    assertEquals(ResultCode.BIN_NOT_FOUND, ae.getResultCode());
    Record record = client.operate(null, keyA, ExpOperation.write(binC, wexp, ExpWriteFlags.UPDATE_ONLY | ExpWriteFlags.POLICY_NO_FAIL));
    assertRecordFound(keyA, record);
    // System.out.println(record);
    record = client.operate(null, keyA, ExpOperation.write(binC, wexp, ExpWriteFlags.CREATE_ONLY));
    assertRecordFound(keyA, record);
    // System.out.println(record);
    ae = assertThrows(AerospikeException.class, new ThrowingRunnable() {

        public void run() {
            client.operate(null, keyA, ExpOperation.write(binC, wexp, ExpWriteFlags.CREATE_ONLY));
        }
    });
    assertEquals(ResultCode.BIN_EXISTS_ERROR, ae.getResultCode());
    record = client.operate(null, keyA, ExpOperation.write(binC, wexp, ExpWriteFlags.CREATE_ONLY | ExpWriteFlags.POLICY_NO_FAIL));
    assertRecordFound(keyA, record);
    Expression dexp = Exp.build(Exp.nil());
    ae = assertThrows(AerospikeException.class, new ThrowingRunnable() {

        public void run() {
            client.operate(null, keyA, ExpOperation.write(binC, dexp, ExpWriteFlags.DEFAULT));
        }
    });
    assertEquals(ResultCode.OP_NOT_APPLICABLE, ae.getResultCode());
    record = client.operate(null, keyA, ExpOperation.write(binC, dexp, ExpWriteFlags.POLICY_NO_FAIL));
    assertRecordFound(keyA, record);
    record = client.operate(null, keyA, ExpOperation.write(binC, dexp, ExpWriteFlags.ALLOW_DELETE));
    assertRecordFound(keyA, record);
    record = client.operate(null, keyA, ExpOperation.write(binC, wexp, ExpWriteFlags.CREATE_ONLY));
    assertRecordFound(keyA, record);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Expression(com.aerospike.client.exp.Expression) Record(com.aerospike.client.Record) ThrowingRunnable(org.junit.function.ThrowingRunnable) Test(org.junit.Test)

Example 15 with Expression

use of com.aerospike.client.exp.Expression in project aerospike-client-java by aerospike.

the class TestExpOperation method expReturnsNil.

@Test
public void expReturnsNil() {
    Expression exp = Exp.build(Exp.nil());
    Record record = client.operate(null, keyA, ExpOperation.read(expVar, exp, ExpReadFlags.DEFAULT), Operation.get(binC));
    assertRecordFound(keyA, record);
    // System.out.println(record);
    Object val = record.getValue(expVar);
    assertEquals(null, val);
}
Also used : Expression(com.aerospike.client.exp.Expression) Record(com.aerospike.client.Record) Test(org.junit.Test)

Aggregations

Expression (com.aerospike.client.exp.Expression)19 Record (com.aerospike.client.Record)18 Test (org.junit.Test)17 AerospikeException (com.aerospike.client.AerospikeException)6 ThrowingRunnable (org.junit.function.ThrowingRunnable)5 Key (com.aerospike.client.Key)4 ArrayList (java.util.ArrayList)4 BatchRead (com.aerospike.client.BatchRead)3 Operation (com.aerospike.client.Operation)3 ListOperation (com.aerospike.client.cdt.ListOperation)3 ExpOperation (com.aerospike.client.exp.ExpOperation)3 Value (com.aerospike.client.Value)1 HLLValue (com.aerospike.client.Value.HLLValue)1 BatchListListener (com.aerospike.client.listener.BatchListListener)1 RecordSet (com.aerospike.client.query.RecordSet)1 Statement (com.aerospike.client.query.Statement)1 ExecuteTask (com.aerospike.client.task.ExecuteTask)1 List (java.util.List)1