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

io.atomix.core.multimap.impl.DelegatingAsyncDistributedMultimap Maven / Gradle / Ivy

There is a newer version: 3.2.0-alpha12
Show newest version
/*
 * Copyright 2018-present Open Networking Foundation
 *
 * 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.
 */
package io.atomix.core.multimap.impl;

import com.google.common.collect.Maps;
import io.atomix.core.collection.AsyncDistributedCollection;
import io.atomix.core.map.AsyncDistributedMap;
import io.atomix.core.map.impl.TranscodingAsyncDistributedMap;
import io.atomix.core.multimap.AsyncAtomicMultimap;
import io.atomix.core.multimap.AsyncDistributedMultimap;
import io.atomix.core.multimap.AtomicMultimapEvent;
import io.atomix.core.multimap.AtomicMultimapEventListener;
import io.atomix.core.multimap.DistributedMultimap;
import io.atomix.core.multimap.DistributedMultimapType;
import io.atomix.core.multimap.MultimapEvent;
import io.atomix.core.multimap.MultimapEventListener;
import io.atomix.core.multiset.AsyncDistributedMultiset;
import io.atomix.core.set.AsyncDistributedSet;
import io.atomix.primitive.PrimitiveType;
import io.atomix.primitive.impl.DelegatingAsyncPrimitive;
import io.atomix.utils.time.Versioned;

import java.time.Duration;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

/**
 * Distributed multimap implementation that delegates to an atomic multimap.
 */
public class DelegatingAsyncDistributedMultimap extends DelegatingAsyncPrimitive implements AsyncDistributedMultimap {
  private final AsyncAtomicMultimap atomicMultimap;
  private final Map, AtomicMultimapEventListener> listenerMap = Maps.newConcurrentMap();

  public DelegatingAsyncDistributedMultimap(AsyncAtomicMultimap atomicMultimap) {
    super(atomicMultimap);
    this.atomicMultimap = atomicMultimap;
  }

  @Override
  public PrimitiveType type() {
    return DistributedMultimapType.instance();
  }

  @Override
  public CompletableFuture size() {
    return atomicMultimap.size();
  }

  @Override
  public CompletableFuture isEmpty() {
    return atomicMultimap.isEmpty();
  }

  @Override
  public CompletableFuture containsKey(K key) {
    return atomicMultimap.containsKey(key);
  }

  @Override
  public CompletableFuture containsValue(V value) {
    return atomicMultimap.containsValue(value);
  }

  @Override
  public CompletableFuture containsEntry(K key, V value) {
    return atomicMultimap.containsEntry(key, value);
  }

  @Override
  public CompletableFuture put(K key, V value) {
    return atomicMultimap.put(key, value);
  }

  @Override
  public CompletableFuture remove(K key, V value) {
    return atomicMultimap.remove(key, value);
  }

  @Override
  public CompletableFuture removeAll(K key, Collection values) {
    return atomicMultimap.removeAll(key, values);
  }

  @Override
  public CompletableFuture> removeAll(K key) {
    return atomicMultimap.removeAll(key).thenApply(Versioned::valueOrNull);
  }

  @Override
  public CompletableFuture putAll(K key, Collection values) {
    return atomicMultimap.putAll(key, values);
  }

  @Override
  public CompletableFuture> replaceValues(K key, Collection values) {
    return atomicMultimap.replaceValues(key, values).thenApply(Versioned::valueOrNull);
  }

  @Override
  public CompletableFuture clear() {
    return atomicMultimap.clear();
  }

  @Override
  public CompletableFuture> get(K key) {
    return atomicMultimap.get(key).thenApply(Versioned::valueOrNull);
  }

  @Override
  public AsyncDistributedSet keySet() {
    return atomicMultimap.keySet();
  }

  @Override
  public AsyncDistributedMultiset keys() {
    return atomicMultimap.keys();
  }

  @Override
  public AsyncDistributedMultiset values() {
    return atomicMultimap.values();
  }

  @Override
  public AsyncDistributedCollection> entries() {
    return atomicMultimap.entries();
  }

  @Override
  public AsyncDistributedMap> asMap() {
    return new TranscodingAsyncDistributedMap<>(atomicMultimap.asMap(), k -> k, k -> k, v -> new Versioned<>(v, 0), Versioned::valueOrNull);
  }

  @Override
  public CompletableFuture addListener(MultimapEventListener listener, Executor executor) {
    AtomicMultimapEventListener atomicListener = new InternalAtomicMultimapEventListener(listener);
    if (listenerMap.putIfAbsent(listener, atomicListener) == null) {
      return atomicMultimap.addListener(atomicListener, executor);
    }
    return CompletableFuture.completedFuture(null);
  }

  @Override
  public CompletableFuture removeListener(MultimapEventListener listener) {
    AtomicMultimapEventListener atomicListener = listenerMap.remove(listener);
    if (atomicListener != null) {
      return atomicMultimap.removeListener(atomicListener);
    }
    return CompletableFuture.completedFuture(null);
  }

  @Override
  public DistributedMultimap sync(Duration operationTimeout) {
    return new BlockingDistributedMultimap<>(this, operationTimeout.toMillis());
  }

  private class InternalAtomicMultimapEventListener implements AtomicMultimapEventListener {
    private final MultimapEventListener mapListener;

    InternalAtomicMultimapEventListener(MultimapEventListener mapListener) {
      this.mapListener = mapListener;
    }

    @Override
    public void event(AtomicMultimapEvent event) {
      mapListener.event(new MultimapEvent<>(MultimapEvent.Type.valueOf(event.type().name()), event.key(), event.newValue(), event.oldValue()));
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy