Search in sources :

Example 1 with MultiResult

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;
}
Also used : MultiResult(com.hazelcast.query.impl.getters.MultiResult) GenericRecord(com.hazelcast.nio.serialization.GenericRecord) PortableInternalGenericRecord(com.hazelcast.internal.serialization.impl.portable.PortableInternalGenericRecord) LinkedList(java.util.LinkedList) PortableInternalGenericRecord(com.hazelcast.internal.serialization.impl.portable.PortableInternalGenericRecord)

Example 2 with 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));
    }
}
Also used : MultiResult(com.hazelcast.query.impl.getters.MultiResult) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 3 with MultiResult

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));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) MultiResult(com.hazelcast.query.impl.getters.MultiResult) SchemaService(com.hazelcast.internal.serialization.impl.compact.SchemaService) SerializationConfig(com.hazelcast.config.SerializationConfig) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) GenericRecordQueryReader(com.hazelcast.internal.serialization.impl.GenericRecordQueryReader) GroupObject(com.hazelcast.internal.serialization.impl.compact.reader.CompactValueReaderTestStructure.GroupObject) PrimitiveObject(com.hazelcast.internal.serialization.impl.compact.reader.CompactValueReaderTestStructure.PrimitiveObject) NestedGroupObject(com.hazelcast.internal.serialization.impl.compact.reader.CompactValueReaderTestStructure.NestedGroupObject) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Data(com.hazelcast.internal.serialization.Data) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Aggregations

MultiResult (com.hazelcast.query.impl.getters.MultiResult)3 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)2 SlowTest (com.hazelcast.test.annotation.SlowTest)2 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 List (java.util.List)2 Test (org.junit.Test)2 CompactSerializationConfig (com.hazelcast.config.CompactSerializationConfig)1 SerializationConfig (com.hazelcast.config.SerializationConfig)1 Data (com.hazelcast.internal.serialization.Data)1 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)1 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)1 GenericRecordQueryReader (com.hazelcast.internal.serialization.impl.GenericRecordQueryReader)1 SchemaService (com.hazelcast.internal.serialization.impl.compact.SchemaService)1 GroupObject (com.hazelcast.internal.serialization.impl.compact.reader.CompactValueReaderTestStructure.GroupObject)1 NestedGroupObject (com.hazelcast.internal.serialization.impl.compact.reader.CompactValueReaderTestStructure.NestedGroupObject)1 PrimitiveObject (com.hazelcast.internal.serialization.impl.compact.reader.CompactValueReaderTestStructure.PrimitiveObject)1 PortableInternalGenericRecord (com.hazelcast.internal.serialization.impl.portable.PortableInternalGenericRecord)1 GenericRecord (com.hazelcast.nio.serialization.GenericRecord)1 LinkedList (java.util.LinkedList)1