Search in sources :

Example 1 with TypeConverter

use of com.hazelcast.core.TypeConverter in project hazelcast by hazelcast.

the class DefaultDiscoveryService method buildProperties.

private Map<String, Comparable> buildProperties(DiscoveryStrategyFactory factory, DiscoveryStrategyConfig config, String className) {
    Collection<PropertyDefinition> propertyDefinitions = factory.getConfigurationProperties();
    if (propertyDefinitions == null) {
        return Collections.emptyMap();
    }
    Map<String, Comparable> properties = config.getProperties();
    Map<String, Comparable> mappedProperties = new HashMap<String, Comparable>();
    for (PropertyDefinition propertyDefinition : propertyDefinitions) {
        String propertyKey = propertyDefinition.key();
        Comparable value = properties.get(propertyKey);
        if (value == null) {
            if (!propertyDefinition.optional()) {
                throw new HazelcastException("Missing property '" + propertyKey + "' on discovery strategy '" + className + "' configuration");
            }
            continue;
        }
        TypeConverter typeConverter = propertyDefinition.typeConverter();
        Comparable mappedValue = typeConverter.convert(value);
        ValueValidator validator = propertyDefinition.validator();
        if (validator != null) {
            validator.validate(mappedValue);
        }
        mappedProperties.put(propertyKey, mappedValue);
    }
    return mappedProperties;
}
Also used : TypeConverter(com.hazelcast.core.TypeConverter) HazelcastException(com.hazelcast.core.HazelcastException) HashMap(java.util.HashMap) ValueValidator(com.hazelcast.config.properties.ValueValidator) PropertyDefinition(com.hazelcast.config.properties.PropertyDefinition)

Example 2 with TypeConverter

use of com.hazelcast.core.TypeConverter in project hazelcast by hazelcast.

the class ConverterCache method tryResolve.

@SuppressWarnings({ "checkstyle:cyclomaticcomplexity", "checkstyle:npathcomplexity", "checkstyle:returncount" })
private TypeConverter tryResolve(String attribute, UnresolvedConverter unresolved) {
    // The main idea here is to avoid scanning indexes on every invocation.
    // Unresolved converters are represented as UnresolvedConverter instances
    // and saved into the cache, so on the next invocation we don't need to
    // rescan the indexes.
    InternalIndex[] indexesSnapshot = indexes.getIndexes();
    if (indexesSnapshot.length == 0) {
        // no indexes at all
        return null;
    }
    if (unresolved != null) {
        // already marked as unresolved
        TypeConverter converter = unresolved.tryResolve();
        if (converter == null) {
            // still unresolved
            return null;
        }
        cache.put(attribute, converter);
        return converter;
    }
    // try non-composite index first, if any
    for (InternalIndex index : indexesSnapshot) {
        String[] components = index.getComponents();
        if (components.length != 1) {
            // composite index will be checked later.
            continue;
        }
        if (!components[0].equals(attribute)) {
            // not a component/attribute we are searching for
            continue;
        }
        TypeConverter converter = index.getConverter();
        if (isNull(converter)) {
            cache.put(attribute, new UnresolvedConverter(index, FULLY_UNRESOLVED));
            return null;
        } else {
            cache.put(attribute, converter);
            return converter;
        }
    }
    // scan composite indexes
    for (InternalIndex index : indexesSnapshot) {
        String[] components = index.getComponents();
        if (components.length == 1) {
            // not a composite index
            continue;
        }
        for (int i = 0; i < components.length; ++i) {
            String component = components[i];
            if (!component.equals(attribute)) {
                // not a component/attribute we are searching for
                continue;
            }
            CompositeConverter compositeConverter = (CompositeConverter) index.getConverter();
            if (compositeConverter == null) {
                // no converter available, mark component as unresolved
                cache.put(attribute, new UnresolvedConverter(index, i));
                return null;
            }
            TypeConverter converter = compositeConverter.getComponentConverter(i);
            if (converter == NULL_CONVERTER) {
                // null/transient converter available, mark component as unresolved
                cache.put(attribute, new UnresolvedConverter(index, i));
                return null;
            }
            // we found it
            cache.put(attribute, converter);
            return converter;
        }
    }
    // the attribute is not known by any index
    cache.put(attribute, new UnresolvedConverter(null, FULLY_UNRESOLVED));
    return null;
}
Also used : TypeConverter(com.hazelcast.core.TypeConverter)

Example 3 with TypeConverter

use of com.hazelcast.core.TypeConverter in project hazelcast by hazelcast.

the class RangeVisitor method visit.

@Override
public Predicate visit(BetweenPredicate predicate, Indexes indexes) {
    TypeConverter converter = indexes.getConverter(predicate.attributeName);
    if (converter == null) {
        return predicate;
    }
    Comparable from = converter.convert(predicate.from);
    Comparable to = converter.convert(predicate.to);
    Order order = compare(from, to);
    switch(order) {
        case LESS:
            return predicate;
        case EQUAL:
            return Predicates.equal(predicate.attributeName, from);
        case GREATER:
            return Predicates.alwaysFalse();
        default:
            throw new IllegalStateException("Unexpected order: " + order);
    }
}
Also used : TypeConverter(com.hazelcast.core.TypeConverter)

Example 4 with TypeConverter

use of com.hazelcast.core.TypeConverter in project hazelcast by hazelcast.

the class EvaluateVisitor method visit.

@Override
public Predicate visit(NotEqualPredicate predicate, Indexes indexes) {
    Index index = indexes.matchIndex(predicate.attributeName, predicate.getClass(), IndexMatchHint.PREFER_UNORDERED, SKIP_PARTITIONS_COUNT_CHECK);
    if (index == null) {
        return predicate;
    }
    TypeConverter converter = index.getConverter();
    if (converter == null) {
        return predicate;
    }
    return new EvaluatePredicate(predicate, index.getName());
}
Also used : TypeConverter(com.hazelcast.core.TypeConverter) Index(com.hazelcast.query.impl.Index)

Example 5 with TypeConverter

use of com.hazelcast.core.TypeConverter in project hazelcast by hazelcast.

the class DiscoveryServicePropertiesUtilTest method nullProperty.

@Test
public void nullProperty() {
    // given
    Map<String, Comparable> properties = new HashMap<>(singletonMap(PROPERTY_KEY_1, null));
    TypeConverter typeConverter = new TypeConverter() {

        @Override
        public Comparable convert(Comparable value) {
            return value == null ? "hazel" : "cast";
        }
    };
    Collection<PropertyDefinition> propertyDefinitions = singletonList((PropertyDefinition) new SimplePropertyDefinition(PROPERTY_KEY_1, true, typeConverter));
    // when
    Map<String, Comparable> result = prepareProperties(properties, propertyDefinitions);
    // then
    assertEquals("hazel", result.get(PROPERTY_KEY_1));
}
Also used : TypeConverter(com.hazelcast.core.TypeConverter) SimplePropertyDefinition(com.hazelcast.config.properties.SimplePropertyDefinition) HashMap(java.util.HashMap) SimplePropertyDefinition(com.hazelcast.config.properties.SimplePropertyDefinition) PropertyDefinition(com.hazelcast.config.properties.PropertyDefinition) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

TypeConverter (com.hazelcast.core.TypeConverter)10 Index (com.hazelcast.query.impl.Index)4 PropertyDefinition (com.hazelcast.config.properties.PropertyDefinition)3 ValueValidator (com.hazelcast.config.properties.ValueValidator)2 HashMap (java.util.HashMap)2 InvalidConfigurationException (com.hazelcast.config.InvalidConfigurationException)1 SimplePropertyDefinition (com.hazelcast.config.properties.SimplePropertyDefinition)1 HazelcastException (com.hazelcast.core.HazelcastException)1 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 Test (org.junit.Test)1