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

org.infinispan.functional.impl.ReadWriteMapImpl Maven / Gradle / Ivy

package org.infinispan.functional.impl;

import org.infinispan.commands.functional.ReadWriteKeyCommand;
import org.infinispan.commands.functional.ReadWriteKeyValueCommand;
import org.infinispan.commands.functional.ReadWriteManyCommand;
import org.infinispan.commands.functional.ReadWriteManyEntriesCommand;
import org.infinispan.commons.api.functional.EntryView.ReadWriteEntryView;
import org.infinispan.commons.api.functional.FunctionalMap.ReadWriteMap;
import org.infinispan.commons.api.functional.Listeners.ReadWriteListeners;
import org.infinispan.commons.api.functional.Param;
import org.infinispan.commons.api.functional.Param.FutureMode;
import org.infinispan.commons.api.functional.Traversable;
import org.infinispan.commons.util.CloseableIteratorSet;
import org.infinispan.commons.util.Experimental;
import org.infinispan.context.InvocationContext;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.Function;

import static org.infinispan.functional.impl.Params.withFuture;

/**
 * Read-write map implementation.
 *
 * @since 8.0
 */
@Experimental
public final class ReadWriteMapImpl extends AbstractFunctionalMap implements ReadWriteMap {
   private static final Log log = LogFactory.getLog(ReadWriteMapImpl.class);
   private final Params params;

   private ReadWriteMapImpl(Params params, FunctionalMapImpl functionalMap) {
      super(functionalMap);
      this.params = params;
   }

   public static  ReadWriteMap create(FunctionalMapImpl functionalMap) {
      return new ReadWriteMapImpl<>(Params.from(functionalMap.params.params), functionalMap);
   }

   private static  ReadWriteMap create(Params params, FunctionalMapImpl functionalMap) {
      return new ReadWriteMapImpl<>(params, functionalMap);
   }

   @Override
   public  CompletableFuture eval(K key, Function, R> f) {
      log.tracef("Invoked eval(k=%s, %s)", key, params);
      Param futureMode = params.get(FutureMode.ID);
      ReadWriteKeyCommand cmd = fmap.cmdFactory().buildReadWriteKeyCommand(key, f, params);
      InvocationContext ctx = fmap.invCtxFactory().createInvocationContext(true, 1);
      ctx.setLockOwner(cmd.getKeyLockOwner());
      return withFuture(futureMode, fmap.asyncExec(), () -> (R) fmap.chain().invoke(ctx, cmd));
   }

   @Override
   public  CompletableFuture eval(K key, V value, BiFunction, R> f) {
      log.tracef("Invoked eval(k=%s, v=%s, %s)", key, value, params);
      Param futureMode = params.get(FutureMode.ID);
      ReadWriteKeyValueCommand cmd = fmap.cmdFactory().buildReadWriteKeyValueCommand(key, value, f, params);
      InvocationContext ctx = fmap.invCtxFactory().createInvocationContext(true, 1);
      ctx.setLockOwner(cmd.getKeyLockOwner());
      return withFuture(futureMode, fmap.asyncExec(), () -> (R) fmap.chain().invoke(ctx, cmd));
   }

   @Override
   public  Traversable evalMany(Map entries, BiFunction, R> f) {
      log.tracef("Invoked evalMany(entries=%s, %s)", entries, params);
      ReadWriteManyEntriesCommand cmd = fmap.cmdFactory().buildReadWriteManyEntriesCommand(entries, f, params);
      InvocationContext ctx = fmap.invCtxFactory().createInvocationContext(true, entries.size());
      return Traversables.of(((List) fmap.chain().invoke(ctx, cmd)).stream());
   }

   @Override
   public  Traversable evalMany(Set keys, Function, R> f) {
      log.tracef("Invoked evalMany(keys=%s, %s)", keys, params);
      ReadWriteManyCommand cmd = fmap.cmdFactory().buildReadWriteManyCommand(keys, f, params);
      InvocationContext ctx = fmap.invCtxFactory().createInvocationContext(true, keys.size());
      return Traversables.of(((List) fmap.chain().invoke(ctx, cmd)).stream());
   }

   @Override
   public  Traversable evalAll(Function, R> f) {
      log.tracef("Invoked evalAll(%s)", params);
      CloseableIteratorSet keys = fmap.cache.keySet();
      ReadWriteManyCommand cmd = fmap.cmdFactory().buildReadWriteManyCommand(keys, f, params);
      InvocationContext ctx = fmap.invCtxFactory().createInvocationContext(true, keys.size());
      return Traversables.of(((List) fmap.chain().invoke(ctx, cmd)).stream());
   }

   @Override
   public ReadWriteListeners listeners() {
      return fmap.notifier();
   }

   @Override
   public ReadWriteMap withParams(Param... ps) {
      if (ps == null || ps.length == 0)
         return this;

      if (params.containsAll(ps))
         return this; // We already have all specified params

      return create(params.addAll(ps), fmap);
   }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy