use of com.hazelcast.query.impl.getters.MultiResult in project hazelcast by hazelcast.
the class GenericRecordQueryReader method read.
public Object read(String fieldPath) throws IOException {
if (fieldPath == null) {
throw new IllegalArgumentException("field path can not be null");
}
if (fieldPath.endsWith(".")) {
throw new IllegalArgumentException("Malformed path " + fieldPath);
}
if (rootRecord.hasField(fieldPath)) {
return readLeaf(rootRecord, fieldPath);
}
LinkedList<Object> results = new LinkedList<>();
results.add(rootRecord);
MultiResult<Object> multiResult = new MultiResult<>(results);
int begin = 0;
int end = StringUtil.indexOf(fieldPath, '.');
// handle the paths except leaf
while (end != -1) {
String path = fieldPath.substring(begin, end);
if (path.length() == 0) {
throw new IllegalArgumentException("The token's length cannot be zero: " + fieldPath);
}
begin = end + 1;
end = StringUtil.indexOf(fieldPath, '.', begin);
ListIterator<Object> iterator = results.listIterator();
String fieldName = extractAttributeNameNameWithoutArguments(path);
if (!path.contains("]")) {
// ex: attribute
while (iterator.hasNext()) {
InternalGenericRecord record = (InternalGenericRecord) iterator.next();
if (!record.hasField(fieldName)) {
iterator.remove();
multiResult.setNullOrEmptyTarget(true);
continue;
}
InternalGenericRecord subGenericRecord = (InternalGenericRecord) record.getGenericRecord(fieldName);
if (subGenericRecord == null) {
iterator.remove();
multiResult.setNullOrEmptyTarget(true);
continue;
}
iterator.set(subGenericRecord);
}
} else if (path.endsWith("[any]")) {
// ex: attribute any
while (iterator.hasNext()) {
InternalGenericRecord record = (InternalGenericRecord) iterator.next();
iterator.remove();
if (!record.hasField(fieldName)) {
multiResult.setNullOrEmptyTarget(true);
continue;
}
GenericRecord[] genericRecords = record.getArrayOfGenericRecord(fieldName);
if (genericRecords == null || genericRecords.length == 0) {
multiResult.setNullOrEmptyTarget(true);
continue;
}
for (GenericRecord genericRecord : genericRecords) {
if (genericRecord != null) {
iterator.add(genericRecord);
} else {
multiResult.setNullOrEmptyTarget(true);
}
}
}
} else {
// ex: attribute[2]
int index = Integer.parseInt(extractArgumentsFromAttributeName(path));
while (iterator.hasNext()) {
InternalGenericRecord record = (InternalGenericRecord) iterator.next();
if (!record.hasField(fieldName)) {
iterator.remove();
multiResult.setNullOrEmptyTarget(true);
continue;
}
GenericRecord genericRecord = record.getGenericRecordFromArray(fieldName, index);
if (genericRecord != null) {
iterator.set(genericRecord);
} else {
iterator.remove();
multiResult.setNullOrEmptyTarget(true);
}
}
}
}
// last loop that we have skipped
String path = fieldPath.substring(begin);
if (path.length() == 0) {
throw new IllegalArgumentException("The token's length cannot be zero: " + fieldPath);
}
ListIterator<Object> iterator = results.listIterator();
String fieldName = extractAttributeNameNameWithoutArguments(path);
if (!path.contains("]")) {
// ex: attribute
while (iterator.hasNext()) {
InternalGenericRecord record = (InternalGenericRecord) iterator.next();
Object leaf = readLeaf(record, fieldName);
iterator.set(leaf);
}
} else if (path.endsWith("[any]")) {
// ex: attribute any
while (iterator.hasNext()) {
InternalGenericRecord record = (InternalGenericRecord) iterator.next();
iterator.remove();
Object leaves = readLeaf(record, fieldName);
if (leaves == null) {
multiResult.setNullOrEmptyTarget(true);
} else if (leaves instanceof Object[]) {
Object[] array = (Object[]) leaves;
if (array.length == 0) {
multiResult.setNullOrEmptyTarget(true);
continue;
}
for (Object leaf : array) {
iterator.add(leaf);
}
} else {
assert leaves.getClass().isArray() : "parameter is not an array";
if (!ExtractorHelper.reducePrimitiveArrayInto(iterator::add, leaves)) {
multiResult.setNullOrEmptyTarget(true);
}
}
}
} else {
// ex: attribute[2]
int index = Integer.parseInt(extractArgumentsFromAttributeName(path));
while (iterator.hasNext()) {
GenericRecord record = (GenericRecord) iterator.next();
Object leaf = readIndexed((InternalGenericRecord) record, fieldName, index);
iterator.set(leaf);
}
}
if (multiResult.isNullEmptyTarget()) {
results.addFirst(null);
} else if (results.size() == 1) {
return results.get(0);
}
return multiResult;
}
use of com.hazelcast.query.impl.getters.MultiResult in project hazelcast by hazelcast.
the class DefaultPortableReaderSpecTest method executeTestScenario.
@Test
@SuppressWarnings("unchecked")
public void executeTestScenario() throws Exception {
// handle result
Object resultToMatch = expectedResult;
if (expectedResult instanceof Class) {
// expected exception case
expected.expect(isA((Class) expectedResult));
} else if (expectedResult instanceof List) {
// just convenience -> if result is a list if will be compared to an array, so it has to be converted
resultToMatch = ((List) resultToMatch).toArray();
}
// print test scenario for debug purposes
// it makes debugging easier since all scenarios are generated
printlnScenarioDescription(resultToMatch);
// assert the condition
Object result = reader(inputObject).read(pathToRead);
if (result instanceof MultiResult) {
MultiResult multiResult = (MultiResult) result;
if (multiResult.getResults().size() == 1 && multiResult.getResults().get(0) == null && multiResult.isNullEmptyTarget()) {
// explode null in case of a single multi-result target result
result = null;
} else {
// in case of multi result while invoking generic "read" method deal with the multi results
result = ((MultiResult) result).getResults().toArray();
}
assertThat(result, equalTo(resultToMatch));
} else {
assertThat(result, equalTo(resultToMatch));
}
}
use of com.hazelcast.query.impl.getters.MultiResult in project hazelcast by hazelcast.
the class CompactStreamSerializerValueReaderSpecTest method executeTestScenario.
@Test
@SuppressWarnings("unchecked")
public void executeTestScenario() throws Exception {
// handle result
Object resultToMatch = expectedResult;
if (expectedResult instanceof Class) {
// expected exception case
expected.expect(isA((Class) expectedResult));
} else if (expectedResult instanceof List) {
// just convenience -> if result is a list if will be compared to an array, so it has to be converted
resultToMatch = ((List) resultToMatch).toArray();
}
// print test scenario for debug purposes
// it makes debugging easier since all scenarios are generated
printlnScenarioDescription(resultToMatch);
SchemaService schemaService = CompactTestUtil.createInMemorySchemaService();
SerializationConfig serializationConfig = new SerializationConfig();
serializationConfig.setCompactSerializationConfig(new CompactSerializationConfig().setEnabled(true));
InternalSerializationService ss = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).setSchemaService(schemaService).build();
Data data = ss.toData(inputObject);
GenericRecordQueryReader reader = new GenericRecordQueryReader(ss.readAsInternalGenericRecord(data));
Object result = reader.read(pathToRead);
if (result instanceof MultiResult) {
MultiResult multiResult = (MultiResult) result;
if (multiResult.getResults().size() == 1 && multiResult.getResults().get(0) == null && multiResult.isNullEmptyTarget()) {
// explode null in case of a single multi-result target result
result = null;
} else {
// in case of multi result while invoking generic "read" method deal with the multi results
result = ((MultiResult) result).getResults().toArray();
}
}
assertThat(result, equalTo(resultToMatch));
}
Aggregations