Search in sources :

Example 46 with Operation

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

the class MapOperation method putItems.

/**
 * Create map put items operation
 * Server writes each map item to map bin and returns map size.
 * <p>
 * The required map policy dictates the type of map to create when it does not exist.
 * The map policy also specifies the flags used when writing items to the map.
 * See policy {@link com.aerospike.client.cdt.MapPolicy}.
 */
public static Operation putItems(MapPolicy policy, String binName, Map<Value, Value> map, CTX... ctx) {
    Packer packer = new Packer();
    if (policy.flags != 0) {
        Pack.init(packer, ctx);
        packer.packArrayBegin(4);
        packer.packInt(MapOperation.PUT_ITEMS);
        packer.packValueMap(map);
        packer.packInt(policy.attributes);
        packer.packInt(policy.flags);
    } else {
        if (policy.itemsCommand == REPLACE_ITEMS) {
            // Replace doesn't allow map attributes because it does not create on non-existing key.
            Pack.init(packer, ctx);
            packer.packArrayBegin(2);
            packer.packInt(policy.itemsCommand);
            packer.packValueMap(map);
        } else {
            Pack.init(packer, ctx);
            packer.packArrayBegin(3);
            packer.packInt(policy.itemsCommand);
            packer.packValueMap(map);
            packer.packInt(policy.attributes);
        }
    }
    byte[] bytes = packer.toByteArray();
    return new Operation(Operation.Type.MAP_MODIFY, binName, Value.get(bytes));
}
Also used : Operation(com.aerospike.client.Operation) Packer(com.aerospike.client.util.Packer)

Example 47 with Operation

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

the class Command method setOperate.

public final void setOperate(WritePolicy policy, Key key, OperateArgs args) {
    begin();
    int fieldCount = estimateKeySize(policy, key);
    CommandExp exp = getCommandExp(policy);
    if (exp != null) {
        dataOffset += exp.size();
        fieldCount++;
    }
    dataOffset += args.size;
    sizeBuffer();
    writeHeaderReadWrite(policy, args.readAttr, args.writeAttr, fieldCount, args.operations.length);
    writeKey(policy, key);
    if (exp != null) {
        dataOffset = exp.write(this);
    }
    for (Operation operation : args.operations) {
        writeOperation(operation);
    }
    end();
    compress(policy);
}
Also used : Operation(com.aerospike.client.Operation) CommandExp(com.aerospike.client.exp.CommandExp)

Example 48 with Operation

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

the class Command method setQuery.

public final void setQuery(Policy policy, Statement statement, boolean write, NodePartitions nodePartitions) {
    byte[] functionArgBuffer = null;
    int fieldCount = 0;
    int filterSize = 0;
    int binNameSize = 0;
    int partsFullSize = 0;
    int partsPartialSize = 0;
    long maxRecords = 0;
    begin();
    if (statement.getNamespace() != null) {
        dataOffset += Buffer.estimateSizeUtf8(statement.getNamespace()) + FIELD_HEADER_SIZE;
        fieldCount++;
    }
    if (statement.getIndexName() != null) {
        dataOffset += Buffer.estimateSizeUtf8(statement.getIndexName()) + FIELD_HEADER_SIZE;
        fieldCount++;
    }
    if (statement.getSetName() != null) {
        dataOffset += Buffer.estimateSizeUtf8(statement.getSetName()) + FIELD_HEADER_SIZE;
        fieldCount++;
    }
    // Allocate space for TaskId field.
    dataOffset += 8 + FIELD_HEADER_SIZE;
    fieldCount++;
    Filter filter = statement.getFilter();
    String[] binNames = statement.getBinNames();
    if (filter != null) {
        IndexCollectionType type = filter.getCollectionType();
        if (type != IndexCollectionType.DEFAULT) {
            dataOffset += FIELD_HEADER_SIZE + 1;
            fieldCount++;
        }
        dataOffset += FIELD_HEADER_SIZE;
        // num filters
        filterSize++;
        filterSize += filter.estimateSize();
        dataOffset += filterSize;
        fieldCount++;
        // Query bin names are specified as a field (Scan bin names are specified later as operations)
        if (binNames != null && binNames.length > 0) {
            dataOffset += FIELD_HEADER_SIZE;
            // num bin names
            binNameSize++;
            for (String binName : binNames) {
                binNameSize += Buffer.estimateSizeUtf8(binName) + 1;
            }
            dataOffset += binNameSize;
            fieldCount++;
        }
    } else {
        // Calling query with no filters is more efficiently handled by a primary index scan.
        if (nodePartitions != null) {
            partsFullSize = nodePartitions.partsFull.size() * 2;
            partsPartialSize = nodePartitions.partsPartial.size() * 20;
            maxRecords = nodePartitions.recordMax;
        }
        if (partsFullSize > 0) {
            dataOffset += partsFullSize + FIELD_HEADER_SIZE;
            fieldCount++;
        }
        if (partsPartialSize > 0) {
            dataOffset += partsPartialSize + FIELD_HEADER_SIZE;
            fieldCount++;
        }
        // Estimate max records size;
        if (maxRecords > 0) {
            dataOffset += 8 + FIELD_HEADER_SIZE;
            fieldCount++;
        }
        // Estimate scan timeout size.
        dataOffset += 4 + FIELD_HEADER_SIZE;
        fieldCount++;
        // Estimate records per second size.
        if (statement.getRecordsPerSecond() > 0) {
            dataOffset += 4 + FIELD_HEADER_SIZE;
            fieldCount++;
        }
    }
    PredExp[] predExp = statement.getPredExp();
    CommandExp exp = (predExp != null) ? new CommandPredExp(predExp) : getCommandExp(policy);
    if (exp != null) {
        dataOffset += exp.size();
        fieldCount++;
    }
    if (statement.getFunctionName() != null) {
        // udf type
        dataOffset += FIELD_HEADER_SIZE + 1;
        dataOffset += Buffer.estimateSizeUtf8(statement.getPackageName()) + FIELD_HEADER_SIZE;
        dataOffset += Buffer.estimateSizeUtf8(statement.getFunctionName()) + FIELD_HEADER_SIZE;
        if (statement.getFunctionArgs().length > 0) {
            functionArgBuffer = Packer.pack(statement.getFunctionArgs());
        } else {
            functionArgBuffer = new byte[0];
        }
        dataOffset += FIELD_HEADER_SIZE + functionArgBuffer.length;
        fieldCount += 4;
    }
    // Operations (used in query execute) and bin names (used in scan/query) are mutually exclusive.
    Operation[] operations = statement.getOperations();
    int operationCount = 0;
    if (operations != null) {
        for (Operation operation : operations) {
            estimateOperationSize(operation);
        }
        operationCount = operations.length;
    } else if (binNames != null && filter == null) {
        for (String binName : binNames) {
            estimateOperationSize(binName);
        }
        operationCount = binNames.length;
    }
    sizeBuffer();
    if (write) {
        writeHeaderWrite((WritePolicy) policy, Command.INFO2_WRITE, fieldCount, operationCount);
    } else {
        QueryPolicy qp = (QueryPolicy) policy;
        int readAttr = qp.includeBinData ? Command.INFO1_READ : Command.INFO1_READ | Command.INFO1_NOBINDATA;
        writeHeaderRead(policy, totalTimeout, readAttr, fieldCount, operationCount);
    }
    if (statement.getNamespace() != null) {
        writeField(statement.getNamespace(), FieldType.NAMESPACE);
    }
    if (statement.getIndexName() != null) {
        writeField(statement.getIndexName(), FieldType.INDEX_NAME);
    }
    if (statement.getSetName() != null) {
        writeField(statement.getSetName(), FieldType.TABLE);
    }
    // Write taskId field
    writeField(statement.getTaskId(), FieldType.TRAN_ID);
    if (filter != null) {
        IndexCollectionType type = filter.getCollectionType();
        if (type != IndexCollectionType.DEFAULT) {
            writeFieldHeader(1, FieldType.INDEX_TYPE);
            dataBuffer[dataOffset++] = (byte) type.ordinal();
        }
        writeFieldHeader(filterSize, FieldType.INDEX_RANGE);
        dataBuffer[dataOffset++] = (byte) 1;
        dataOffset = filter.write(dataBuffer, dataOffset);
        // Query bin names are specified as a field (Scan bin names are specified later as operations)
        if (binNames != null && binNames.length > 0) {
            writeFieldHeader(binNameSize, FieldType.QUERY_BINLIST);
            dataBuffer[dataOffset++] = (byte) binNames.length;
            for (String binName : binNames) {
                int len = Buffer.stringToUtf8(binName, dataBuffer, dataOffset + 1);
                dataBuffer[dataOffset] = (byte) len;
                dataOffset += len + 1;
            }
        }
    } else {
        // Calling query with no filters is more efficiently handled by a primary index scan.
        if (partsFullSize > 0) {
            writeFieldHeader(partsFullSize, FieldType.PID_ARRAY);
            for (PartitionStatus part : nodePartitions.partsFull) {
                Buffer.shortToLittleBytes(part.id, dataBuffer, dataOffset);
                dataOffset += 2;
            }
        }
        if (partsPartialSize > 0) {
            writeFieldHeader(partsPartialSize, FieldType.DIGEST_ARRAY);
            for (PartitionStatus part : nodePartitions.partsPartial) {
                System.arraycopy(part.digest, 0, dataBuffer, dataOffset, 20);
                dataOffset += 20;
            }
        }
        if (maxRecords > 0) {
            writeField(maxRecords, FieldType.SCAN_MAX_RECORDS);
        }
        // Write scan socket idle timeout.
        writeField(policy.socketTimeout, FieldType.SCAN_TIMEOUT);
        // Write records per second.
        if (statement.getRecordsPerSecond() > 0) {
            writeField(statement.getRecordsPerSecond(), FieldType.RECORDS_PER_SECOND);
        }
    }
    if (exp != null) {
        dataOffset = exp.write(this);
    }
    if (statement.getFunctionName() != null) {
        writeFieldHeader(1, FieldType.UDF_OP);
        dataBuffer[dataOffset++] = (statement.returnData()) ? (byte) 1 : (byte) 2;
        writeField(statement.getPackageName(), FieldType.UDF_PACKAGE_NAME);
        writeField(statement.getFunctionName(), FieldType.UDF_FUNCTION);
        writeField(functionArgBuffer, FieldType.UDF_ARGLIST);
    }
    if (operations != null) {
        for (Operation operation : operations) {
            writeOperation(operation);
        }
    } else if (binNames != null && filter == null) {
        // Scan bin names are specified after all fields.
        for (String binName : binNames) {
            writeOperation(binName, Operation.Type.READ);
        }
    }
    end();
}
Also used : PredExp(com.aerospike.client.query.PredExp) PartitionStatus(com.aerospike.client.query.PartitionStatus) Operation(com.aerospike.client.Operation) QueryPolicy(com.aerospike.client.policy.QueryPolicy) Filter(com.aerospike.client.query.Filter) IndexCollectionType(com.aerospike.client.query.IndexCollectionType) CommandExp(com.aerospike.client.exp.CommandExp)

Example 49 with Operation

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

the class TestOperateHll method assertSetUnion.

public void assertSetUnion(List<List<Value>> vals, int nIndexBits, boolean folding, boolean allowFolding) {
    String msg = "Fail - nIndexBits " + nIndexBits;
    HLLPolicy p = HLLPolicy.Default;
    HLLPolicy u = HLLPolicy.Default;
    if (allowFolding) {
        u = new HLLPolicy(HLLWriteFlags.ALLOW_FOLD);
    }
    long unionExpected = 0;
    boolean folded = false;
    for (int i = 0; i < keys.length; i++) {
        int ix = nIndexBits;
        if (folding) {
            ix -= i;
            if (ix < minNIndexBits) {
                ix = minNIndexBits;
            }
            if (ix < nIndexBits) {
                folded = true;
            }
        }
        List<Value> subVals = vals.get(i);
        unionExpected += subVals.size();
        Record record = assertSuccess(msg, keys[i], Operation.delete(), HLLOperation.add(p, binName, subVals, ix), HLLOperation.getCount(binName));
        List<?> resultList = record.getList(binName);
        long count = (Long) resultList.get(1);
        assertHLLCount(msg, ix, count, subVals.size());
    }
    ArrayList<HLLValue> hlls = new ArrayList<HLLValue>();
    for (int i = 0; i < keys.length; i++) {
        Record record = assertSuccess(msg, keys[i], Operation.get(binName), HLLOperation.getCount(binName));
        List<?> resultList = record.getList(binName);
        HLLValue hll = (HLLValue) resultList.get(0);
        assertNotEquals(null, hll);
        hlls.add(hll);
    }
    Operation[] ops = new Operation[] { Operation.delete(), HLLOperation.init(p, binName, nIndexBits), HLLOperation.setUnion(u, binName, hlls), HLLOperation.getCount(binName), // And recreate it to test creating empty.
    Operation.delete(), HLLOperation.setUnion(p, binName, hlls), HLLOperation.getCount(binName) };
    if (folded && !allowFolding) {
        assertThrows(msg, key, AerospikeException.class, ResultCode.OP_NOT_APPLICABLE, ops);
        return;
    }
    Record recordUnion = assertSuccess(msg, key, ops);
    List<?> unionResultList = recordUnion.getList(binName);
    long unionCount = (Long) unionResultList.get(2);
    long unionCount2 = (Long) unionResultList.get(4);
    assertHLLCount(msg, nIndexBits, unionCount, unionExpected);
    assertEquals(msg, unionCount, unionCount2);
    for (int i = 0; i < keys.length; i++) {
        List<Value> subVals = vals.get(i);
        Record record = assertSuccess(msg, key, HLLOperation.add(p, binName, subVals, nIndexBits), HLLOperation.getCount(binName));
        List<?> resultList = record.getList(binName);
        long nAdded = (Long) resultList.get(0);
        long count = (Long) resultList.get(1);
        assertEquals(msg, 0, nAdded);
        assertEquals(msg, unionCount, count);
        assertHLLCount(msg, nIndexBits, count, unionExpected);
    }
}
Also used : HLLValue(com.aerospike.client.Value.HLLValue) ArrayList(java.util.ArrayList) Operation(com.aerospike.client.Operation) HLLOperation(com.aerospike.client.operation.HLLOperation) HLLValue(com.aerospike.client.Value.HLLValue) StringValue(com.aerospike.client.Value.StringValue) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) HLLPolicy(com.aerospike.client.operation.HLLPolicy)

Example 50 with Operation

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

the class TestOperateBit method assertBitModifyRegion.

// Exhaustive modify tests verified with all read ops.
public void assertBitModifyRegion(int bin_sz, int offset, int set_sz, byte[] expected, boolean is_insert, Operation... operations) {
    client.delete(null, key);
    byte[] initial = new byte[bin_sz];
    for (int i = 0; i < bin_sz; i++) {
        initial[i] = (byte) 0xFF;
    }
    client.put(null, key, new Bin(binName, initial));
    int int_sz = 64;
    if (set_sz < int_sz) {
        int_sz = set_sz;
    }
    int bin_bit_sz = bin_sz * 8;
    if (is_insert) {
        bin_bit_sz += set_sz;
    }
    Operation[] full_ops = Arrays.copyOf(operations, operations.length + 7);
    full_ops[full_ops.length - 7] = BitOperation.lscan(binName, offset, set_sz, true);
    full_ops[full_ops.length - 6] = BitOperation.rscan(binName, offset, set_sz, true);
    full_ops[full_ops.length - 5] = BitOperation.getInt(binName, offset, int_sz, false);
    full_ops[full_ops.length - 4] = BitOperation.count(binName, offset, set_sz);
    full_ops[full_ops.length - 3] = BitOperation.lscan(binName, 0, bin_bit_sz, false);
    full_ops[full_ops.length - 2] = BitOperation.rscan(binName, 0, bin_bit_sz, false);
    full_ops[full_ops.length - 1] = BitOperation.get(binName, offset, set_sz);
    Record record = client.operate(null, key, full_ops);
    List<?> result_list = record.getList(binName);
    long lscan1_result = (Long) result_list.get(result_list.size() - 7);
    long rscan1_result = (Long) result_list.get(result_list.size() - 6);
    long getint_result = (Long) result_list.get(result_list.size() - 5);
    long count_result = (Long) result_list.get(result_list.size() - 4);
    long lscan_result = (Long) result_list.get(result_list.size() - 3);
    long rscan_result = (Long) result_list.get(result_list.size() - 2);
    byte[] actual = (byte[]) result_list.get(result_list.size() - 1);
    String err_output = String.format("bin_sz %d offset %d set_sz %d", bin_sz, offset, set_sz);
    assertEquals("lscan1 - " + err_output, -1, lscan1_result);
    assertEquals("rscan1 - " + err_output, -1, rscan1_result);
    assertEquals("getint - " + err_output, 0, getint_result);
    assertEquals("count - " + err_output, 0, count_result);
    assertEquals("lscan - " + err_output, offset, lscan_result);
    assertEquals("rscan - " + err_output, offset + set_sz - 1, rscan_result);
    assertArrayEquals("op - " + err_output, expected, actual);
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Operation(com.aerospike.client.Operation) BitOperation(com.aerospike.client.operation.BitOperation)

Aggregations

Operation (com.aerospike.client.Operation)51 Packer (com.aerospike.client.util.Packer)36 Record (com.aerospike.client.Record)6 ArrayList (java.util.ArrayList)6 Key (com.aerospike.client.Key)5 BatchRead (com.aerospike.client.BatchRead)4 CommandExp (com.aerospike.client.exp.CommandExp)4 ListOperation (com.aerospike.client.cdt.ListOperation)3 ExpOperation (com.aerospike.client.exp.ExpOperation)3 Expression (com.aerospike.client.exp.Expression)3 HLLOperation (com.aerospike.client.operation.HLLOperation)3 HLLPolicy (com.aerospike.client.operation.HLLPolicy)3 List (java.util.List)2 Test (org.junit.Test)2 AerospikeException (com.aerospike.client.AerospikeException)1 Bin (com.aerospike.client.Bin)1 Value (com.aerospike.client.Value)1 HLLValue (com.aerospike.client.Value.HLLValue)1 StringValue (com.aerospike.client.Value.StringValue)1 BatchListListener (com.aerospike.client.listener.BatchListListener)1