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

org.infinispan.persistence.support.BatchModification Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.infinispan.persistence.support;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.infinispan.marshall.core.MarshalledEntry;

/**
 * A simple wrapper class, necessary for Transactional stores, which allows MarshalledEntries and Object keys to be passed
 * to a store implementation in order. This class also removes repeated operations on the same key in order to prevent
 * redundant operations on the underlying store.  For example a tx, {put(1, "Test"); remove(1);}, will be simply written
 * to the store as {remove(1);}.
 *
 * @author Ryan Emerson
 */
public class BatchModification {
   private final Map marshalledEntries = new HashMap<>();
   private final Set keysToRemove = new HashSet<>();
   private final Set affectedKeys;

   public BatchModification(Set affectedKeys) {
      this.affectedKeys = affectedKeys;
   }

   public void addMarshalledEntry(Object key, MarshalledEntry marshalledEntry) {
      keysToRemove.remove(key);
      marshalledEntries.put(key, marshalledEntry);
   }

   public void removeEntry(Object key) {
      marshalledEntries.remove(key);
      keysToRemove.add(key);
   }

   public Set getAffectedKeys() {
      return affectedKeys;
   }

   public Set getKeysToRemove() {
      return keysToRemove;
   }

   public Collection getMarshalledEntries() {
      return marshalledEntries.values();
   }
}