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

org.eclipse.jnosql.mapping.keyvalue.AbstractKeyValueTemplate Maven / Gradle / Ivy

/*
 *  Copyright (c) 2022 Contributors to the Eclipse Foundation
 *   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.eclipse.jnosql.mapping.keyvalue;


import jakarta.nosql.QueryMapper;
import org.eclipse.jnosql.communication.Value;
import org.eclipse.jnosql.communication.keyvalue.BucketManager;
import org.eclipse.jnosql.communication.keyvalue.KeyValueEntity;

import java.time.Duration;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static java.util.Objects.requireNonNull;

/**
 * This class provides a skeletal implementation of the {@link KeyValueTemplate} interface,
 * to minimize the effort required to implement this interface.
 */
public abstract class AbstractKeyValueTemplate implements KeyValueTemplate {

    protected abstract KeyValueEntityConverter getConverter();

    protected abstract BucketManager getManager();

    protected abstract KeyValueEventPersistManager getEventManager();

    @Override
    public  T put(T entity) {
        requireNonNull(entity, "entity is required");
        return persist(entity, (keyValueEntity) -> getManager().put(keyValueEntity));
    }

    @Override
    public  T put(T entity, Duration ttl) {
        requireNonNull(entity, "entity is required");
        requireNonNull(ttl, "ttl class is required");
        return persist(entity, (keyValueEntity) -> getManager().put(keyValueEntity, ttl));
    }

    @Override
    public  Iterable insert(Iterable entities) {
        return put(entities);
    }

    @Override
    public  Iterable insert(Iterable entities, Duration ttl) {
        return put(entities, ttl);
    }

    @Override
    public  T update(T entity) {
        return put(entity);
    }

    @Override
    public  Iterable update(Iterable entities) {
        return put(entities);
    }

    @Override
    public  T insert(T entity) {
        return put(entity);
    }

    @Override
    public  T insert(T entity, Duration ttl) {
        return put(entity, ttl);
    }

    @Override
    public  Optional get(K key, Class type) {
        requireNonNull(key, "key is required");
        requireNonNull(type, "entity class is required");

        Optional value = getManager().get(key);
        return value.map(v -> getConverter().toEntity(type, KeyValueEntity.of(key, v)))
                .filter(Objects::nonNull).map(e -> {
                    getEventManager().firePostEntity(e);
                    return e;
                });
    }

    @Override
    public  Iterable get(Iterable keys, Class type) {
        requireNonNull(keys, "keys is required");
        requireNonNull(type, "type class is required");
        return StreamSupport.stream(keys.spliterator(), false)
                .map(k -> getManager().get(k)
                        .map(v -> KeyValueEntity.of(k, v)))
                .filter(Optional::isPresent)
                .map(e -> getConverter().toEntity(type, e.get()))
                .collect(Collectors.toList());
    }


    @Override
    public  void delete(K key) {
        requireNonNull(key, "key is required");
        getManager().delete(key);
    }

    @Override
    public  void delete(Iterable keys) {
        requireNonNull(keys, "keys is required");
        getManager().delete(keys);
    }

    @Override
    public  Optional find(Class type, K id) {
        return this.get(id, type);
    }

    @Override
    public  void delete(Class type, K id) {
        this.delete(id);
    }

    @Override
    public  QueryMapper.MapperFrom select(Class type) {
        throw new UnsupportedOperationException("Key value database type does not have support for mapping query");
    }

    @Override
    public  QueryMapper.MapperDeleteFrom delete(Class type) {
        throw new UnsupportedOperationException("Key value database type does not have support for mapping query");
    }

    protected  T persist(T entity, Consumer persistAction) {
        return Stream.of(entity)
                .map(toUnary(getEventManager()::firePreEntity))
                .map(getConverter()::toKeyValue)
                .map(toUnary(persistAction))
                .map(it -> getConverter().toEntity((Class) entity.getClass(), it))
                .map(toUnary(getEventManager()::firePostEntity))
                .findFirst()
                .orElseThrow();
    }

    private  UnaryOperator toUnary(Consumer consumer) {
        return t -> {
            consumer.accept(t);
            return t;
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy