Search in sources :

Example 1 with Expression

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

the class BatchOperate method batchReadOperateComplex.

/**
 * Read results using varying read operations in one batch.
 */
private void batchReadOperateComplex(AerospikeClient client, Parameters params) {
    console.info("batchReadOperateComplex");
    Expression exp1 = Exp.build(Exp.mul(Exp.intBin(BinName1), Exp.intBin(BinName2)));
    Expression exp2 = Exp.build(Exp.add(Exp.intBin(BinName1), Exp.intBin(BinName2)));
    Expression exp3 = Exp.build(Exp.sub(Exp.intBin(BinName1), Exp.intBin(BinName2)));
    // Batch uses pointer reference to quickly determine if operations are repeated and can therefore
    // be optimized, but using varargs directly always creates a new reference. Therefore, save operation
    // array so we have one pointer reference per operation array.
    Operation[] ops1 = Operation.array(ExpOperation.read(ResultName1, exp1, ExpReadFlags.DEFAULT));
    Operation[] ops2 = Operation.array(ExpOperation.read(ResultName1, exp2, ExpReadFlags.DEFAULT));
    Operation[] ops3 = Operation.array(ExpOperation.read(ResultName1, exp3, ExpReadFlags.DEFAULT));
    Operation[] ops4 = Operation.array(ExpOperation.read(ResultName1, exp2, ExpReadFlags.DEFAULT), ExpOperation.read(ResultName2, exp3, ExpReadFlags.DEFAULT));
    List<BatchRead> records = new ArrayList<BatchRead>();
    records.add(new BatchRead(new Key(params.namespace, params.set, KeyPrefix + 1), ops1));
    // The following record is optimized (namespace,set,ops are only sent once) because
    // namespace, set and ops all have the same pointer references as the previous entry.
    records.add(new BatchRead(new Key(params.namespace, params.set, KeyPrefix + 2), ops1));
    records.add(new BatchRead(new Key(params.namespace, params.set, KeyPrefix + 3), ops2));
    records.add(new BatchRead(new Key(params.namespace, params.set, KeyPrefix + 4), ops3));
    records.add(new BatchRead(new Key(params.namespace, params.set, KeyPrefix + 5), ops4));
    // Execute batch.
    client.get(null, records);
    // Show results.
    int count = 0;
    for (BatchRead record : records) {
        Record rec = record.record;
        Object v1 = rec.getValue(ResultName1);
        Object v2 = rec.getValue(ResultName2);
        console.info("Result[%d]: %s, %s", count++, v1, v2);
    }
}
Also used : Expression(com.aerospike.client.exp.Expression) ArrayList(java.util.ArrayList) BatchRead(com.aerospike.client.BatchRead) Record(com.aerospike.client.Record) ListOperation(com.aerospike.client.cdt.ListOperation) ExpOperation(com.aerospike.client.exp.ExpOperation) Operation(com.aerospike.client.Operation) Key(com.aerospike.client.Key)

Example 2 with Expression

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

the class TestBatch method batchReadComplex.

@Test
public void batchReadComplex() {
    // 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(null, records);
    assertBatchBinEqual(records, BinName, 0);
    assertBatchBinEqual(records, BinName, 1);
    assertBatchBinEqual(records, BinName, 2);
    assertBatchRecordExists(records, BinName, 3);
    assertBatchBinEqual(records, BinName, 4);
    BatchRead batch = records.get(5);
    assertRecordFound(batch.key, batch.record);
    int v = batch.record.getInt(BinName);
    assertEquals(48, v);
    assertBatchBinEqual(records, BinName, 6);
    batch = records.get(7);
    assertRecordFound(batch.key, batch.record);
    Object val = batch.record.getValue("binnotfound");
    if (val != null) {
        fail("Unexpected batch bin value received");
    }
    batch = records.get(8);
    if (batch.record != null) {
        fail("Unexpected batch record received");
    }
}
Also used : Expression(com.aerospike.client.exp.Expression) 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) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 3 with Expression

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

the class TestQueryExecute method queryExecuteOperateExp.

@Test
public void queryExecuteOperateExp() {
    String binName = "foo";
    Expression exp = Exp.build(Exp.val("bar"));
    int begin = 3;
    int end = 9;
    Statement stmt = new Statement();
    stmt.setNamespace(args.namespace);
    stmt.setSetName(args.set);
    stmt.setFilter(Filter.range(binName1, begin, end));
    ExecuteTask task = client.execute(null, stmt, ExpOperation.write(binName, exp, ExpWriteFlags.DEFAULT));
    task.waitTillComplete(3000, 3000);
    stmt = new Statement();
    stmt.setNamespace(args.namespace);
    stmt.setSetName(args.set);
    stmt.setFilter(Filter.range(binName1, begin, end));
    RecordSet rs = client.query(null, stmt);
    try {
        int count = 0;
        while (rs.next()) {
            Record record = rs.getRecord();
            String value = record.getString(binName);
            if (value == null) {
                fail("Bin " + binName + " not found");
            }
            if (!value.equals("bar")) {
                fail("Data mismatch. Expected bar. Received " + value);
            }
            count++;
        }
        assertEquals(end - begin + 1, count);
    } finally {
        rs.close();
    }
}
Also used : Expression(com.aerospike.client.exp.Expression) Statement(com.aerospike.client.query.Statement) Record(com.aerospike.client.Record) RecordSet(com.aerospike.client.query.RecordSet) ExecuteTask(com.aerospike.client.task.ExecuteTask) Test(org.junit.Test)

Example 4 with Expression

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

the class TestListExp method expReturnsList.

@Test
public void expReturnsList() {
    List<Value> list = new ArrayList<Value>();
    list.add(Value.get("a"));
    list.add(Value.get("b"));
    list.add(Value.get("c"));
    list.add(Value.get("d"));
    Expression exp = Exp.build(Exp.val(list));
    Record record = client.operate(null, keyA, ExpOperation.write(binC, exp, ExpWriteFlags.DEFAULT), Operation.get(binC), ExpOperation.read("var", exp, ExpReadFlags.DEFAULT));
    // System.out.println(record);
    List<?> results = record.getList(binC);
    assertEquals(2, results.size());
    List<?> rlist = (List<?>) results.get(1);
    assertEquals(4, rlist.size());
    List<?> results2 = record.getList("var");
    assertEquals(4, results2.size());
}
Also used : Expression(com.aerospike.client.exp.Expression) Value(com.aerospike.client.Value) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 5 with Expression

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

the class TestExpOperation method expMerge.

@Test
public void expMerge() {
    Expression e = Exp.build(Exp.eq(Exp.intBin(binA), Exp.val(0)));
    Expression eand = Exp.build(Exp.and(Exp.expr(e), Exp.eq(Exp.intBin(binD), Exp.val(2))));
    Expression eor = Exp.build(Exp.or(Exp.expr(e), Exp.eq(Exp.intBin(binD), Exp.val(2))));
    Record record = client.operate(null, keyA, ExpOperation.read("res1", eand, ExpReadFlags.DEFAULT), ExpOperation.read("res2", eor, ExpReadFlags.DEFAULT));
    assertRecordFound(keyA, record);
    boolean res1 = record.getBoolean("res1");
    assertFalse(res1);
    boolean res2 = record.getBoolean("res2");
    assertTrue(res2);
}
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