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

org.ehcache.loaderwriter.writebehind.LocalHeapWriteBehindQueue Maven / Gradle / Ivy

There is a newer version: 3.10.8
Show newest version
/*
 * Copyright Terracotta, Inc.
 *
 * 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 org.ehcache.loaderwriter.writebehind;

import java.util.ArrayList;
import java.util.List;

import org.ehcache.function.BiFunction;
import org.ehcache.internal.concurrent.ConcurrentHashMap;
import org.ehcache.loaderwriter.writebehind.operations.SingleOperation;
import org.ehcache.spi.loaderwriter.CacheLoaderWriter;
import org.ehcache.spi.loaderwriter.WriteBehindConfiguration;

/**
 * @author Geert Bevin
 * @author Tim wu
 *
 */
public class LocalHeapWriteBehindQueue extends AbstractWriteBehindQueue {
  
  private List> waiting = new ArrayList>();
  private final ConcurrentHashMap> latestOperation = new ConcurrentHashMap>(); 

  LocalHeapWriteBehindQueue(WriteBehindConfiguration config, CacheLoaderWriter cacheLoaderWriter) {
    super(config, cacheLoaderWriter);
  }
  
  protected SingleOperation getLatestOperation(K key) {
    return latestOperation.get(key);
  }
  
  protected void removeOperation(final SingleOperation operation) {
    latestOperation.computeIfPresent(operation.getKey(), new BiFunction, SingleOperation>() {

      @Override
      public SingleOperation apply(K t, SingleOperation oldOperation) {
        if(oldOperation == null) {
          return null; // when trying to remove non existent operation
        }
        if(oldOperation == operation) {
          return null;
        }
        return oldOperation;
      }
    });
  }
  
  @Override
  protected List> quarantineItems() {
    List> quarantined = waiting;
    waiting = new ArrayList>();
    return quarantined;
  }

  @Override
  protected void addItem(SingleOperation operation) {
    latestOperation.put(operation.getKey(), operation);
    waiting.add(operation);    
  }

  @Override
  protected void reinsertUnprocessedItems(List> operations) {
    List> newQueue = new ArrayList>(operations);
    newQueue.addAll(waiting);
    waiting = newQueue;
  }

  @Override
  public long getQueueSize() {
    return waiting.size();
  }

}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy