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

org.onosproject.store.primitives.DefaultConsistentMap Maven / Gradle / Ivy

There is a newer version: 2.7.0
Show newest version
package org.onosproject.store.primitives;

/*
 * Copyright 2016 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;

import org.onlab.util.Tools;
import org.onosproject.store.service.AsyncConsistentMap;
import org.onosproject.store.service.ConsistentMap;
import org.onosproject.store.service.ConsistentMapException;
import org.onosproject.store.service.ConsistentMapException.ConcurrentModification;
import org.onosproject.store.service.MapEventListener;
import org.onosproject.store.service.Synchronous;
import org.onosproject.store.service.Versioned;

import com.google.common.base.Throwables;

/**
 * Default implementation of {@code ConsistentMap}.
 *
 * @param  type of key.
 * @param  type of value.
 */
public class DefaultConsistentMap extends Synchronous> implements ConsistentMap {

    private static final int MAX_DELAY_BETWEEN_RETY_MILLS = 50;
    private final AsyncConsistentMap asyncMap;
    private final long operationTimeoutMillis;
    private Map javaMap;

    public DefaultConsistentMap(AsyncConsistentMap asyncMap, long operationTimeoutMillis) {
        super(asyncMap);
        this.asyncMap = asyncMap;
        this.operationTimeoutMillis = operationTimeoutMillis;
    }

    @Override
    public int size() {
        return complete(asyncMap.size());
    }

    @Override
    public boolean isEmpty() {
        return complete(asyncMap.isEmpty());
    }

    @Override
    public boolean containsKey(K key) {
        return complete(asyncMap.containsKey(key));
    }

    @Override
    public boolean containsValue(V value) {
        return complete(asyncMap.containsValue(value));
    }

    @Override
    public Versioned get(K key) {
        return complete(asyncMap.get(key));
    }

    @Override
    public Versioned computeIfAbsent(K key,
            Function mappingFunction) {
        return computeIf(key, Objects::isNull, (k, v) -> mappingFunction.apply(k));
    }

    @Override
    public Versioned computeIfPresent(K key,
            BiFunction remappingFunction) {
        return computeIf(key, Objects::nonNull, remappingFunction);
    }

    @Override
    public Versioned compute(K key,
            BiFunction remappingFunction) {
        return computeIf(key, v -> true, remappingFunction);
    }

    @Override
    public Versioned computeIf(K key,
            Predicate condition,
            BiFunction remappingFunction) {
        return Tools.retryable(() -> complete(asyncMap.computeIf(key, condition, remappingFunction)),
                               ConcurrentModification.class,
                               Integer.MAX_VALUE,
                               MAX_DELAY_BETWEEN_RETY_MILLS).get();
    }

    @Override
    public Versioned put(K key, V value) {
        return complete(asyncMap.put(key, value));
    }

    @Override
    public Versioned putAndGet(K key, V value) {
        return complete(asyncMap.putAndGet(key, value));
    }

    @Override
    public Versioned remove(K key) {
        return complete(asyncMap.remove(key));
    }

    @Override
    public void clear() {
        complete(asyncMap.clear());
    }

    @Override
    public Set keySet() {
        return complete(asyncMap.keySet());
    }

    @Override
    public Collection> values() {
        return complete(asyncMap.values());
    }

    @Override
    public Set>> entrySet() {
        return complete(asyncMap.entrySet());
    }

    @Override
    public Versioned putIfAbsent(K key, V value) {
        return complete(asyncMap.putIfAbsent(key, value));
    }

    @Override
    public boolean remove(K key, V value) {
        return complete(asyncMap.remove(key, value));
    }

    @Override
    public boolean remove(K key, long version) {
        return complete(asyncMap.remove(key, version));
    }

    @Override
    public Versioned replace(K key, V value) {
        return complete(asyncMap.replace(key, value));
    }

    @Override
    public boolean replace(K key, V oldValue, V newValue) {
        return complete(asyncMap.replace(key, oldValue, newValue));
    }

    @Override
    public boolean replace(K key, long oldVersion, V newValue) {
        return complete(asyncMap.replace(key, oldVersion, newValue));
    }

    @Override
    public void addListener(MapEventListener listener) {
        complete(asyncMap.addListener(listener));
    }

    @Override
    public void removeListener(MapEventListener listener) {
        complete(asyncMap.addListener(listener));
    }

    @Override
    public Map asJavaMap() {
        synchronized (this) {
            if (javaMap == null) {
                javaMap = new ConsistentMapBackedJavaMap<>(this);
            }
        }
        return javaMap;
    }

    @Override
    public String toString() {
        return asJavaMap().toString();
    }

    private  T complete(CompletableFuture future) {
        try {
            return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new ConsistentMapException.Interrupted();
        } catch (TimeoutException e) {
            throw new ConsistentMapException.Timeout();
        } catch (ExecutionException e) {
            Throwables.propagateIfPossible(e.getCause());
            throw new ConsistentMapException(e.getCause());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy