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) {
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();
}
use of com.aerospike.client.policy.QueryPolicy in project aerospike-client-java by aerospike.
the class TestQueryFilterSet method queryKeyInt.
@Test
public void queryKeyInt() {
Statement stmt = new Statement();
stmt.setNamespace(args.namespace);
stmt.setSetName(set3);
QueryPolicy policy = new QueryPolicy();
policy.filterExp = Exp.build(Exp.lt(Exp.key(Exp.Type.INT), Exp.val(35)));
RecordSet rs = client.query(policy, stmt);
try {
int count = 0;
while (rs.next()) {
// System.out.println(rs.getKey().toString() + " - " + rs.getRecord().toString());
count++;
}
assertEquals(4, count);
} finally {
rs.close();
}
}
use of com.aerospike.client.policy.QueryPolicy in project aerospike-client-java by aerospike.
the class TestQueryFilterSet method queryDouble.
@Test
public void queryDouble() {
Statement stmt = new Statement();
stmt.setNamespace(args.namespace);
stmt.setSetName(set2);
QueryPolicy policy = new QueryPolicy();
policy.filterExp = Exp.build(Exp.gt(Exp.floatBin(binB), Exp.val(21.5)));
RecordSet rs = client.query(policy, stmt);
try {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(1, count);
} finally {
rs.close();
}
}
use of com.aerospike.client.policy.QueryPolicy in project aerospike-client-java by aerospike.
the class TestQueryFilterSet method queryKeyExists.
@Test
public void queryKeyExists() {
Statement stmt = new Statement();
stmt.setNamespace(args.namespace);
stmt.setSetName(set3);
QueryPolicy policy = new QueryPolicy();
policy.filterExp = Exp.build(Exp.keyExists());
RecordSet rs = client.query(policy, stmt);
try {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(20, count);
} finally {
rs.close();
}
}
use of com.aerospike.client.policy.QueryPolicy in project aerospike-client-java by aerospike.
the class TestQueryGeo method queryGeo1.
@Test
public void queryGeo1() {
String region = "{ \"type\": \"Point\", \"coordinates\": [ -122.0986857, 37.4214209 ] }";
Statement stmt = new Statement();
stmt.setNamespace(args.namespace);
stmt.setSetName(setNameRegions);
QueryPolicy policy = new QueryPolicy();
policy.filterExp = Exp.build(Exp.geoCompare(Exp.geoBin("loc"), Exp.geo(region)));
RecordSet rs = client.query(policy, stmt);
try {
int count = 0;
while (rs.next()) {
// System.out.println(rs.getRecord().toString());
count++;
}
assertEquals(5, count);
} finally {
rs.close();
}
}
Aggregations