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 java.util.HashSet;
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 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.functional.EntryView.ReadWriteEntryView;
import org.infinispan.functional.FunctionalMap.ReadWriteMap;
import org.infinispan.functional.Listeners.ReadWriteListeners;
import org.infinispan.functional.Param;
import org.infinispan.functional.Traversable;
import org.infinispan.commons.util.Experimental;
import org.infinispan.context.InvocationContext;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;

/**
 * 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 ReadWriteMapImpl(Params params, FunctionalMapImpl functionalMap) {
      super(params, functionalMap);
   }

   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);
      ReadWriteKeyCommand cmd = fmap.commandsFactory.buildReadWriteKeyCommand(key, f, params);
      InvocationContext ctx = getInvocationContext(true, 1);
      if (ctx.getLockOwner() == null) {
         ctx.setLockOwner(cmd.getKeyLockOwner());
      }
      return invokeAsync(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);
      ReadWriteKeyValueCommand cmd = fmap.commandsFactory.buildReadWriteKeyValueCommand(key, value, f, params);
      InvocationContext ctx = getInvocationContext(true, 1);
      if (ctx.getLockOwner() == null) {
         ctx.setLockOwner(cmd.getKeyLockOwner());
      }
      return invokeAsync(ctx, cmd);
   }

   @Override
   public  Traversable evalMany(Map entries, BiFunction, R> f) {
      log.tracef("Invoked evalMany(entries=%s, %s)", entries, params);
      ReadWriteManyEntriesCommand cmd = fmap.commandsFactory.buildReadWriteManyEntriesCommand(entries, f, params);
      InvocationContext ctx = getInvocationContext(true, entries.size());
      if (ctx.getLockOwner() == null) {
         ctx.setLockOwner(cmd.getKeyLockOwner());
      }
      return Traversables.of(((List) invokeAsync(ctx, cmd).join()).stream());
   }

   @Override
   public  Traversable evalMany(Set keys, Function, R> f) {
      log.tracef("Invoked evalMany(keys=%s, %s)", keys, params);
      ReadWriteManyCommand cmd = fmap.commandsFactory.buildReadWriteManyCommand(keys, f, params);
      InvocationContext ctx = getInvocationContext(true, keys.size());
      if (ctx.getLockOwner() == null) {
         ctx.setLockOwner(cmd.getKeyLockOwner());
      }
      return Traversables.of(((List) invokeAsync(ctx, cmd).join()).stream());
   }

   @Override
   public  Traversable evalAll(Function, R> f) {
      log.tracef("Invoked evalAll(%s)", params);
      // TODO: during commmand execution the set is iterated multiple times, and can execute remote operations
      // therefore we should rather have separate command (or different semantics for keys == null)
      Set keys = new HashSet<>(fmap.cache.keySet());
      ReadWriteManyCommand cmd = fmap.commandsFactory.buildReadWriteManyCommand(keys, f, params);
      InvocationContext ctx = getInvocationContext(true, keys.size());
      if (ctx.getLockOwner() == null) {
         ctx.setLockOwner(cmd.getKeyLockOwner());
      }
      return Traversables.of(((List) invokeAsync(ctx, cmd).join()).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