Search in sources :

Example 1 with BatchRead

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

the class Command method setBatchRead.

public final void setBatchRead(BatchPolicy policy, List<BatchRead> records, BatchNode batch) {
    // Estimate full row size
    final int[] offsets = batch.offsets;
    final int max = batch.offsetsSize;
    final int fieldCountRow = policy.sendSetName ? 2 : 1;
    BatchRead prev = null;
    begin();
    int fieldCount = 1;
    CommandExp exp = getCommandExp(policy);
    if (exp != null) {
        dataOffset += exp.size();
        fieldCount++;
    }
    dataOffset += FIELD_HEADER_SIZE + 5;
    for (int i = 0; i < max; i++) {
        final BatchRead record = records.get(offsets[i]);
        final Key key = record.key;
        final String[] binNames = record.binNames;
        final Operation[] ops = record.ops;
        dataOffset += key.digest.length + 4;
        // results in more space used. The batch will still be correct.
        if (prev != null && prev.key.namespace == key.namespace && (!policy.sendSetName || prev.key.setName == key.setName) && prev.binNames == binNames && prev.readAllBins == record.readAllBins && prev.ops == ops) {
            // Can set repeat previous namespace/bin names to save space.
            dataOffset++;
        } else {
            // Estimate full header, namespace and bin names.
            dataOffset += Buffer.estimateSizeUtf8(key.namespace) + FIELD_HEADER_SIZE + 6;
            if (policy.sendSetName) {
                dataOffset += Buffer.estimateSizeUtf8(key.setName) + FIELD_HEADER_SIZE;
            }
            if (binNames != null) {
                for (String binName : binNames) {
                    estimateOperationSize(binName);
                }
            } else if (ops != null) {
                for (Operation op : ops) {
                    estimateReadOperationSize(op);
                }
            }
            prev = record;
        }
    }
    sizeBuffer();
    int readAttr = Command.INFO1_READ;
    if (policy.readModeAP == ReadModeAP.ALL) {
        readAttr |= Command.INFO1_READ_MODE_AP_ALL;
    }
    writeHeaderRead(policy, totalTimeout, readAttr | Command.INFO1_BATCH, fieldCount, 0);
    if (exp != null) {
        dataOffset = exp.write(this);
    }
    final int fieldSizeOffset = dataOffset;
    // Need to update size at end
    writeFieldHeader(0, policy.sendSetName ? FieldType.BATCH_INDEX_WITH_SET : FieldType.BATCH_INDEX);
    Buffer.intToBytes(max, dataBuffer, dataOffset);
    dataOffset += 4;
    dataBuffer[dataOffset++] = (policy.allowInline) ? (byte) 1 : (byte) 0;
    prev = null;
    for (int i = 0; i < max; i++) {
        final int index = offsets[i];
        Buffer.intToBytes(index, dataBuffer, dataOffset);
        dataOffset += 4;
        final BatchRead record = records.get(index);
        final Key key = record.key;
        final String[] binNames = record.binNames;
        final Operation[] ops = record.ops;
        final byte[] digest = key.digest;
        System.arraycopy(digest, 0, dataBuffer, dataOffset, digest.length);
        dataOffset += digest.length;
        // results in more space used. The batch will still be correct.
        if (prev != null && prev.key.namespace == key.namespace && (!policy.sendSetName || prev.key.setName == key.setName) && prev.binNames == binNames && prev.readAllBins == record.readAllBins && prev.ops == ops) {
            // Can set repeat previous namespace/bin names to save space.
            // repeat
            dataBuffer[dataOffset++] = 1;
        } else {
            // Write full header, namespace and bin names.
            // do not repeat
            dataBuffer[dataOffset++] = 0;
            if (binNames != null && binNames.length != 0) {
                dataBuffer[dataOffset++] = (byte) readAttr;
                writeBatchFields(policy, key, fieldCountRow, binNames.length);
                for (String binName : binNames) {
                    writeOperation(binName, Operation.Type.READ);
                }
            } else if (ops != null) {
                int offset = dataOffset++;
                writeBatchFields(policy, key, fieldCountRow, ops.length);
                dataBuffer[offset] = (byte) writeOperations(ops, readAttr);
            } else {
                dataBuffer[dataOffset++] = (byte) (readAttr | (record.readAllBins ? Command.INFO1_GET_ALL : Command.INFO1_NOBINDATA));
                writeBatchFields(policy, key, fieldCountRow, 0);
            }
            prev = record;
        }
    }
    // Write real field size.
    Buffer.intToBytes(dataOffset - MSG_TOTAL_HEADER_SIZE - 4, dataBuffer, fieldSizeOffset);
    end();
    compress(policy);
}
Also used : BatchRead(com.aerospike.client.BatchRead) Operation(com.aerospike.client.Operation) CommandExp(com.aerospike.client.exp.CommandExp) Key(com.aerospike.client.Key)

Example 2 with BatchRead

use of com.aerospike.client.BatchRead 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 3 with BatchRead

use of com.aerospike.client.BatchRead 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 4 with BatchRead

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

the class TestBatch method assertBatchRecordExists.

private void assertBatchRecordExists(List<BatchRead> list, String binName, int i) {
    BatchRead batch = list.get(i);
    assertRecordFound(batch.key, batch.record);
    assertNotEquals(0, batch.record.generation);
    assertNotEquals(0, batch.record.expiration);
}
Also used : BatchRead(com.aerospike.client.BatchRead)

Example 5 with BatchRead

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

Aggregations

BatchRead (com.aerospike.client.BatchRead)8 Key (com.aerospike.client.Key)6 ArrayList (java.util.ArrayList)5 Operation (com.aerospike.client.Operation)4 Record (com.aerospike.client.Record)4 ListOperation (com.aerospike.client.cdt.ListOperation)3 ExpOperation (com.aerospike.client.exp.ExpOperation)3 Expression (com.aerospike.client.exp.Expression)3 AerospikeException (com.aerospike.client.AerospikeException)2 BatchListListener (com.aerospike.client.listener.BatchListListener)2 Test (org.junit.Test)2 CommandExp (com.aerospike.client.exp.CommandExp)1