Search in sources :

Example 1 with PredExp

use of com.aerospike.client.query.PredExp in project aerospike-client-java by aerospike.

the class Command method setQuery.

public final void setQuery(Policy policy, Statement statement, boolean write) {
    byte[] functionArgBuffer = null;
    int fieldCount = 0;
    int filterSize = 0;
    int binNameSize = 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) {
            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.
        // Estimate scan options size.
        dataOffset += 2 + FIELD_HEADER_SIZE;
        fieldCount++;
    }
    PredExp[] predExp = statement.getPredExp();
    int predSize = 0;
    if (predExp != null) {
        dataOffset += FIELD_HEADER_SIZE;
        predSize = PredExp.estimateSize(predExp);
        dataOffset += predSize;
        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;
    }
    if (filter == null) {
        if (binNames != null) {
            for (String binName : binNames) {
                estimateOperationSize(binName);
            }
        }
    }
    sizeBuffer();
    int operationCount = (filter == null && binNames != null) ? binNames.length : 0;
    if (write) {
        writeHeader((WritePolicy) policy, Command.INFO1_READ, Command.INFO2_WRITE, fieldCount, operationCount);
    } else {
        QueryPolicy qp = (QueryPolicy) policy;
        int readAttr = qp.includeBinData ? Command.INFO1_READ : Command.INFO1_READ | Command.INFO1_NOBINDATA;
        writeHeader(policy, readAttr, 0, 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
    writeFieldHeader(8, FieldType.TRAN_ID);
    Buffer.longToBytes(statement.getTaskId(), dataBuffer, dataOffset);
    dataOffset += 8;
    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) {
            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.
        writeFieldHeader(2, FieldType.SCAN_OPTIONS);
        byte priority = (byte) policy.priority.ordinal();
        priority <<= 4;
        dataBuffer[dataOffset++] = priority;
        dataBuffer[dataOffset++] = (byte) 100;
    }
    if (predExp != null) {
        writeFieldHeader(predSize, FieldType.PREDEXP);
        dataOffset = PredExp.write(predExp, dataBuffer, dataOffset);
    }
    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);
    }
    // Scan bin names are specified after all fields.
    if (filter == null) {
        if (binNames != null) {
            for (String binName : binNames) {
                writeOperation(binName, Operation.Type.READ);
            }
        }
    }
    end();
}
Also used : PredExp(com.aerospike.client.query.PredExp) Filter(com.aerospike.client.query.Filter) IndexCollectionType(com.aerospike.client.query.IndexCollectionType) QueryPolicy(com.aerospike.client.policy.QueryPolicy)

Example 2 with PredExp

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

Aggregations

QueryPolicy (com.aerospike.client.policy.QueryPolicy)2 Filter (com.aerospike.client.query.Filter)2 IndexCollectionType (com.aerospike.client.query.IndexCollectionType)2 PredExp (com.aerospike.client.query.PredExp)2 Operation (com.aerospike.client.Operation)1 CommandExp (com.aerospike.client.exp.CommandExp)1 PartitionStatus (com.aerospike.client.query.PartitionStatus)1