All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jnosql.diana.hazelcast.key.DefaultHazelcastBucketManager Maven / Gradle / Ivy

The newest version!
/*
 *  Copyright (c) 2017 Otávio Santana and others
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   and Apache License v2.0 which accompanies this distribution.
 *   The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *   and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
 *
 *   You may elect to redistribute this code under either of these licenses.
 *
 *   Contributors:
 *
 *   Otavio Santana
 */
package org.jnosql.diana.hazelcast.key;

import com.hazelcast.core.IMap;
import com.hazelcast.query.Predicate;
import com.hazelcast.query.SqlPredicate;
import org.jnosql.diana.api.Value;
import org.jnosql.diana.api.key.KeyValueEntity;

import java.time.Duration;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.StreamSupport;

import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;

/**
 * The default implementation of hazelcast bucket manager
 */
class DefaultHazelcastBucketManager implements HazelcastBucketManager {

    private final IMap map;

    DefaultHazelcastBucketManager(IMap map) {
        this.map = map;
    }

    @Override
    public  void put(K key, V value) {
        map.put(key, value);
    }

    @Override
    public  void put(KeyValueEntity entity) throws NullPointerException {
        map.put(entity.getKey(), entity.getValue().get());
    }

    @Override
    public  void put(KeyValueEntity entity, Duration ttl) {
        map.put(entity.getKey(), entity.getValue().get(), ttl.toMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public  void put(Iterable> entities) throws NullPointerException {
        StreamSupport.stream(entities.spliterator(), false).forEach(this::put);
    }

    @Override
    public  void put(Iterable> entities, Duration ttl) throws NullPointerException, UnsupportedOperationException {
        StreamSupport.stream(entities.spliterator(), false).forEach(kv -> this.put(kv, ttl));
    }

    @Override
    public  Optional get(K key) throws NullPointerException {
        Object value = map.get(key);
        if (value == null) {
            return Optional.empty();
        }
        return Optional.ofNullable(Value.of(value));
    }

    @Override
    public  Iterable get(Iterable keys) throws NullPointerException {
        return StreamSupport.stream(keys.spliterator(), false).map((Function) map::get).filter(Objects::nonNull)
                .map(Value::of).collect(toList());
    }

    @Override
    public  void remove(K key) {
        map.remove(key);
    }

    @Override
    public  void remove(Iterable keys) {
        StreamSupport.stream(keys.spliterator(), false).forEach(this::remove);
    }

    @Override
    public void close() {
    }

    @Override
    public Collection sql(String query) throws NullPointerException {
        requireNonNull(query, "sql is required");
        return sql(new SqlPredicate(query));
    }

    @Override
    public Collection sql(String query, Map params) throws NullPointerException {
        requireNonNull(query, "sql is required");
        requireNonNull(params, "params is required");
        final StringBuilder finalQuery = new StringBuilder(query);
        final Consumer> consumer = e -> {
            String key = ":" + e.getKey();
            int indexOf = query.indexOf(key);
            finalQuery.replace(indexOf, indexOf + key.length(), e.getValue().toString());
        };
        params.entrySet().forEach(consumer);
        return sql(new SqlPredicate(finalQuery.toString()));
    }

    @Override
    public  Collection sql(Predicate predicate) throws NullPointerException {
        requireNonNull(predicate, "predicate is required");
        Collection values = map.values(predicate);
        return values.stream().map(Value::of).collect(toList());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy