Search in sources :

Example 26 with QueryPolicy

use of com.aerospike.client.policy.QueryPolicy in project aerospike-client-java by aerospike.

the class TestQueryFilterExp method queryMap3.

@Test
public void queryMap3() {
    int begin = 1;
    int end = 10;
    Statement stmt = new Statement();
    stmt.setNamespace(args.namespace);
    stmt.setSetName(setName);
    stmt.setFilter(Filter.range(binName, begin, end));
    // Map bin does not contains key "D"
    QueryPolicy policy = new QueryPolicy();
    policy.filterExp = Exp.build(Exp.eq(MapExp.getByKey(MapReturnType.COUNT, Exp.Type.INT, Exp.val("D"), Exp.mapBin("mapbin")), Exp.val(0)));
    RecordSet rs = client.query(policy, stmt);
    try {
        int count = 0;
        while (rs.next()) {
            // System.out.println(rs.getRecord().toString());
            count++;
        }
        assertEquals(8, count);
    } finally {
        rs.close();
    }
}
Also used : Statement(com.aerospike.client.query.Statement) RecordSet(com.aerospike.client.query.RecordSet) QueryPolicy(com.aerospike.client.policy.QueryPolicy) Test(org.junit.Test)

Example 27 with QueryPolicy

use of com.aerospike.client.policy.QueryPolicy in project aerospike-client-java by aerospike.

the class TestQueryFilterExp method queryMemorySize.

@Test
public void queryMemorySize() {
    int begin = 1;
    int end = 10;
    Statement stmt = new Statement();
    stmt.setNamespace(args.namespace);
    stmt.setSetName(setName);
    stmt.setFilter(Filter.range(binName, begin, end));
    // storage-engine could be disk/device for which memorySize() returns zero.
    // This just tests that the expression was sent correctly
    // because all device sizes are effectively allowed.
    QueryPolicy policy = new QueryPolicy();
    policy.filterExp = Exp.build(Exp.ge(Exp.memorySize(), Exp.val(0)));
    RecordSet rs = client.query(policy, stmt);
    try {
        int count = 0;
        while (rs.next()) {
            count++;
        }
        assertEquals(10, count);
    } finally {
        rs.close();
    }
}
Also used : Statement(com.aerospike.client.query.Statement) RecordSet(com.aerospike.client.query.RecordSet) QueryPolicy(com.aerospike.client.policy.QueryPolicy) Test(org.junit.Test)

Example 28 with QueryPolicy

use of com.aerospike.client.policy.QueryPolicy 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 29 with QueryPolicy

use of com.aerospike.client.policy.QueryPolicy in project aerospike-client-java by aerospike.

the class QueryExp method runQuery3.

private void runQuery3(AerospikeClient client, Parameters params, String binName) throws Exception {
    int begin = 20;
    int end = 30;
    console.info("Query Predicate: bin3 contains string with 'prefix' and 'suffix'");
    Statement stmt = new Statement();
    stmt.setNamespace(params.namespace);
    stmt.setSetName(params.set);
    stmt.setFilter(Filter.range(binName, begin, end));
    QueryPolicy policy = new QueryPolicy(client.queryPolicyDefault);
    policy.filterExp = Exp.build(Exp.regexCompare("prefix.*suffix", RegexFlag.ICASE | RegexFlag.NEWLINE, Exp.stringBin("bin3")));
    RecordSet rs = client.query(policy, stmt);
    try {
        while (rs.next()) {
            Record record = rs.getRecord();
            console.info("Record: " + record.toString());
        }
    } finally {
        rs.close();
    }
}
Also used : Statement(com.aerospike.client.query.Statement) Record(com.aerospike.client.Record) RecordSet(com.aerospike.client.query.RecordSet) QueryPolicy(com.aerospike.client.policy.QueryPolicy)

Example 30 with QueryPolicy

use of com.aerospike.client.policy.QueryPolicy in project aerospike-client-java by aerospike.

the class QueryExp method runQuery2.

private void runQuery2(AerospikeClient client, Parameters params, String binName) throws Exception {
    int begin = 10;
    int end = 40;
    console.info("Query Predicate: Record updated in 2020");
    Calendar beginTime = new GregorianCalendar(2020, 0, 1);
    Calendar endTime = new GregorianCalendar(2021, 0, 1);
    Statement stmt = new Statement();
    stmt.setNamespace(params.namespace);
    stmt.setSetName(params.set);
    stmt.setFilter(Filter.range(binName, begin, end));
    QueryPolicy policy = new QueryPolicy(client.queryPolicyDefault);
    policy.filterExp = Exp.build(Exp.and(Exp.ge(Exp.lastUpdate(), Exp.val(beginTime)), Exp.lt(Exp.lastUpdate(), Exp.val(endTime))));
    RecordSet rs = client.query(policy, stmt);
    try {
        while (rs.next()) {
            Record record = rs.getRecord();
            console.info("Record: " + record.toString());
        }
    } finally {
        rs.close();
    }
}
Also used : Statement(com.aerospike.client.query.Statement) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Record(com.aerospike.client.Record) RecordSet(com.aerospike.client.query.RecordSet) QueryPolicy(com.aerospike.client.policy.QueryPolicy)

Aggregations

QueryPolicy (com.aerospike.client.policy.QueryPolicy)31 RecordSet (com.aerospike.client.query.RecordSet)28 Statement (com.aerospike.client.query.Statement)28 Test (org.junit.Test)25 Record (com.aerospike.client.Record)3 Filter (com.aerospike.client.query.Filter)2 IndexCollectionType (com.aerospike.client.query.IndexCollectionType)2 PredExp (com.aerospike.client.query.PredExp)2 ArrayList (java.util.ArrayList)2 GregorianCalendar (java.util.GregorianCalendar)2 Operation (com.aerospike.client.Operation)1 CommandExp (com.aerospike.client.exp.CommandExp)1 PartitionStatus (com.aerospike.client.query.PartitionStatus)1 Calendar (java.util.Calendar)1