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

org.ehcache.impl.internal.events.InvocationScopedEventSink 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.impl.internal.events;

import org.ehcache.ValueSupplier;
import org.ehcache.event.EventType;
import org.ehcache.core.spi.store.events.StoreEventFilter;
import org.ehcache.core.spi.store.events.StoreEventListener;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.BlockingQueue;

import static org.ehcache.impl.internal.events.StoreEvents.createEvent;
import static org.ehcache.impl.internal.events.StoreEvents.evictEvent;
import static org.ehcache.impl.internal.events.StoreEvents.expireEvent;
import static org.ehcache.impl.internal.events.StoreEvents.removeEvent;
import static org.ehcache.impl.internal.events.StoreEvents.updateEvent;

/**
 * InvocationScopedEventSink
 */
class InvocationScopedEventSink implements CloseableStoreEventSink {

  private final Set> filters;
  private final boolean ordered;
  private final BlockingQueue>[] orderedQueues;
  private final Set> listeners;
  private final Deque> events = new ArrayDeque>(4);

  InvocationScopedEventSink(Set> filters, boolean ordered,
                            BlockingQueue>[] orderedQueues,
                            Set> listeners) {
    this.filters = filters;
    this.ordered = ordered;
    this.orderedQueues = orderedQueues;
    this.listeners = listeners;
  }

  @Override
  public void removed(K key, ValueSupplier value) {
    V removedValue = value.value();
    if (acceptEvent(EventType.REMOVED, key, removedValue, null)) {
      handleEvent(key, new FireableStoreEventHolder(removeEvent(key, removedValue)));
    }
  }

  @Override
  public void updated(K key, ValueSupplier oldValue, V newValue) {
    V oldValueValue = oldValue.value();
    if (acceptEvent(EventType.UPDATED, key, oldValueValue, newValue)) {
      handleEvent(key, new FireableStoreEventHolder(updateEvent(key, oldValueValue, newValue)));
    }
  }

  @Override
  public void expired(K key, ValueSupplier value) {
    V expired = value.value();
    if (acceptEvent(EventType.EXPIRED, key, expired, null)) {
      handleEvent(key, new FireableStoreEventHolder(expireEvent(key, expired)));
    }
  }

  @Override
  public void created(K key, V value) {
    if (acceptEvent(EventType.CREATED, key, null, value)) {
      handleEvent(key, new FireableStoreEventHolder(createEvent(key, value)));
    }
  }

  @Override
  public void evicted(K key, ValueSupplier value) {
    V evicted = value.value();
    if (acceptEvent(EventType.EVICTED, key, evicted, null)) {
      handleEvent(key, new FireableStoreEventHolder(evictEvent(key, evicted)));
    }
  }

  protected boolean acceptEvent(EventType type, K key, V oldValue, V newValue) {
    for (StoreEventFilter filter : filters) {
      if (!filter.acceptEvent(type, key, oldValue, newValue)) {
        return false;
      }
    }
    return true;
  }

  @Override
  public void close() {
    if (ordered) {
      fireOrdered(listeners, events);
    } else {
      for (FireableStoreEventHolder fireableEvent : events) {
        for (StoreEventListener listener : listeners) {
          fireableEvent.fireOn(listener);
        }
      }
    }
  }

  @Override
  public void closeOnFailure() {
    for (FireableStoreEventHolder fireableEvent : events) {
      fireableEvent.markFailed();
    }

    close();
  }

  @Override
  public void reset() {
    Iterator> iterator = events.iterator();
    while (iterator.hasNext()) {
      FireableStoreEventHolder next = iterator.next();
      if (ordered) {
        BlockingQueue> orderedQueue = getOrderedQueue(next);
        orderedQueue.remove(next);
        fireWaiters(listeners, orderedQueue);
      }
      iterator.remove();
    }
  }

  protected Deque> getEvents() {
    return events;
  }

  protected void handleEvent(K key, FireableStoreEventHolder event) {
    events.add(event);
    if (ordered) {
      try {
        getOrderedQueue(event).put(event);
      } catch (InterruptedException e) {
        events.removeLast();
        Thread.currentThread().interrupt();
      }
    }
  }

  private BlockingQueue> getOrderedQueue(FireableStoreEventHolder event) {
    int i = Math.abs(event.eventKeyHash() % orderedQueues.length);
    return orderedQueues[i];
  }

  private void fireOrdered(Set> listeners, Deque> events) {
    for (FireableStoreEventHolder fireableEvent : events) {
      fireableEvent.markFireable();

      BlockingQueue> orderedQueue = getOrderedQueue(fireableEvent);
      FireableStoreEventHolder head = orderedQueue.peek();
      if (head == fireableEvent) {
        // Need to fire my event, plus any it was blocking
        if (head.markFired()) {
          // Only proceed if I am the one marking fired
          // Do not notify failed events
          for (StoreEventListener listener : listeners) {
            head.fireOn(listener);
          }
          orderedQueue.poll(); // Remove the event I just handled
        } else {
          // Someone else fired it - stopping there
          // Lost the fire race - may need to wait for full processing
          fireableEvent.waitTillFired();
        }
        fireWaiters(listeners, orderedQueue);
      } else {
        // Waiting for another thread to fire - once that happens, done for this event
        fireableEvent.waitTillFired();
      }
    }
  }

  private void fireWaiters(Set> listeners, BlockingQueue> orderedQueue) {
    FireableStoreEventHolder head;
    while ((head = orderedQueue.peek()) != null && head.isFireable()) {
      if (head.markFired()) {
        // Only proceed if I am the one marking fired
        // Do not notify failed events
        for (StoreEventListener listener : listeners) {
          head.fireOn(listener);
        }
        orderedQueue.poll(); // Remove the event I just handled
      } else {
        // Someone else fired it - stopping there
        break;
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy