Search in sources :

Example 6 with CompositeValue

use of com.hazelcast.query.impl.CompositeValue in project hazelcast by hazelcast.

the class CompositeIndexVisitor method generateRangePredicate.

@SuppressWarnings({ "checkstyle:cyclomaticcomplexity", "checkstyle:npathcomplexity" })
private static Predicate generateRangePredicate(InternalIndex index, Map<String, EqualPredicate> prefixes, int prefixLength, RangePredicate comparison, boolean fast) {
    assert !(comparison instanceof EqualPredicate);
    assert comparison.getFrom() != NULL && comparison.getTo() != NULL;
    String[] components = index.getComponents();
    boolean fullyMatched = components.length == prefixLength + 1;
    boolean hasFrom = comparison.getFrom() != null;
    boolean hasTo = comparison.getTo() != null;
    assert hasFrom || hasTo;
    assert hasFrom || !comparison.isFromInclusive();
    assert hasTo || !comparison.isToInclusive();
    Comparable[] from = new Comparable[components.length];
    Comparable[] to = new Comparable[components.length];
    for (int i = 0; i < prefixLength; ++i) {
        String attribute = components[i];
        Comparable value = fast ? prefixes.get(attribute).value : prefixes.remove(attribute).value;
        from[i] = value;
        to[i] = value;
    }
    // NULL since we want to exclude nulls on the comparison component itself
    from[prefixLength] = hasFrom ? comparison.getFrom() : NULL;
    to[prefixLength] = hasTo ? comparison.getTo() : POSITIVE_INFINITY;
    for (int i = prefixLength + 1; i < components.length; ++i) {
        from[i] = !hasFrom || comparison.isFromInclusive() ? NEGATIVE_INFINITY : POSITIVE_INFINITY;
        to[i] = !hasTo || comparison.isToInclusive() ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
    }
    return new CompositeRangePredicate(index, new CompositeValue(from), fullyMatched && comparison.isFromInclusive(), new CompositeValue(to), fullyMatched && comparison.isToInclusive(), prefixLength);
}
Also used : CompositeValue(com.hazelcast.query.impl.CompositeValue)

Example 7 with CompositeValue

use of com.hazelcast.query.impl.CompositeValue in project hazelcast by hazelcast.

the class CompositeRangePredicateTest method testComparison.

@Test
public void testComparison() {
    assertEquals(0, map.getLocalMapStats().getIndexedQueryCount());
    for (int i = 0; i < 100; ++i) {
        final Integer age = randomQueryAge();
        final Long height = randomQueryHeight(true);
        int prefixLength = random.nextInt(2) + 1;
        final CompositeValue from;
        final boolean fromInclusive;
        final CompositeValue to;
        final boolean toInclusive;
        final Predicate expected;
        switch(prefixLength) {
            case 1:
                final Long heightFrom = randomQueryHeight(true);
                final boolean heightFromInclusive = heightFrom != null && random.nextBoolean();
                final Long heightTo = randomQueryHeight(heightFrom != null);
                final boolean heightToInclusive = heightTo != null && random.nextBoolean();
                from = value(age, heightFrom != null ? heightFrom : NULL, heightFromInclusive ? NEGATIVE_INFINITY : POSITIVE_INFINITY);
                fromInclusive = false;
                to = value(age, heightTo != null ? heightTo : POSITIVE_INFINITY, heightToInclusive ? POSITIVE_INFINITY : NEGATIVE_INFINITY);
                toInclusive = false;
                expected = new Predicate<Integer, Person>() {

                    @SuppressWarnings("RedundantIfStatement")
                    @Override
                    public boolean apply(Map.Entry<Integer, Person> mapEntry) {
                        Person value = mapEntry.getValue();
                        if (!ObjectTestUtils.equals(value.age, age)) {
                            return false;
                        }
                        if (value.height == null) {
                            return false;
                        }
                        if (heightFrom != null) {
                            if (heightFromInclusive) {
                                if (value.height < heightFrom) {
                                    return false;
                                }
                            } else {
                                if (value.height <= heightFrom) {
                                    return false;
                                }
                            }
                        }
                        if (heightTo != null) {
                            if (heightToInclusive) {
                                if (value.height > heightTo) {
                                    return false;
                                }
                            } else {
                                if (value.height >= heightTo) {
                                    return false;
                                }
                            }
                        }
                        return true;
                    }
                };
                break;
            case 2:
                final Integer keyFrom = randomQueryKey(true);
                final boolean keyFromInclusive = keyFrom != null && random.nextBoolean();
                final Integer keyTo = randomQueryKey(keyFrom != null);
                final boolean keyToInclusive = keyTo != null && random.nextBoolean();
                from = value(age, height, keyFrom != null ? keyFrom : NULL);
                fromInclusive = keyFromInclusive;
                to = value(age, height, keyTo != null ? keyTo : POSITIVE_INFINITY);
                toInclusive = keyToInclusive;
                expected = new Predicate<Integer, Person>() {

                    @SuppressWarnings("RedundantIfStatement")
                    @Override
                    public boolean apply(Map.Entry<Integer, Person> mapEntry) {
                        Person value = mapEntry.getValue();
                        int key = mapEntry.getKey();
                        if (!ObjectTestUtils.equals(value.age, age)) {
                            return false;
                        }
                        if (!ObjectTestUtils.equals(value.height, height)) {
                            return false;
                        }
                        if (keyFrom != null) {
                            if (keyFromInclusive) {
                                if (key < keyFrom) {
                                    return false;
                                }
                            } else {
                                if (key <= keyFrom) {
                                    return false;
                                }
                            }
                        }
                        if (keyTo != null) {
                            if (keyToInclusive) {
                                if (key > keyTo) {
                                    return false;
                                }
                            } else {
                                if (key >= keyTo) {
                                    return false;
                                }
                            }
                        }
                        return true;
                    }
                };
                break;
            default:
                throw new IllegalStateException();
        }
        assertPredicate(expected, predicate(indexName, from, fromInclusive, to, toInclusive, prefixLength, "age", "height", "__key"));
    }
    assertEquals(100, map.getLocalMapStats().getIndexedQueryCount());
}
Also used : Predicate(com.hazelcast.query.Predicate) Map(java.util.Map) IMap(com.hazelcast.map.IMap) CompositeValue(com.hazelcast.query.impl.CompositeValue) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

CompositeValue (com.hazelcast.query.impl.CompositeValue)7 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 Test (org.junit.Test)3 IMap (com.hazelcast.map.IMap)2 Predicate (com.hazelcast.query.Predicate)2 Map (java.util.Map)2 Address (com.hazelcast.cluster.Address)1 IndexIterationPointer (com.hazelcast.internal.iteration.IndexIterationPointer)1 ArrayDataSerializableFactory (com.hazelcast.internal.serialization.impl.ArrayDataSerializableFactory)1 ConstructorFunction (com.hazelcast.internal.util.ConstructorFunction)1 PartitionIdSet (com.hazelcast.internal.util.collection.PartitionIdSet)1 MapFetchIndexOperationResult (com.hazelcast.map.impl.operation.MapFetchIndexOperation.MapFetchIndexOperationResult)1 IdentifiedDataSerializable (com.hazelcast.nio.serialization.IdentifiedDataSerializable)1 OperationServiceImpl (com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl)1