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

com.avaje.ebeanservice.elastic.update.UpdateGroup Maven / Gradle / Ivy

package com.avaje.ebeanservice.elastic.update;

import com.avaje.ebean.DocStoreQueueEntry;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Groups index update events by queueId.
 *
 * Some nested path updates can overlap with index events so it is good to process these together as
 * a group and check for these overlaps (and skip unnecessary work).
 */
public class UpdateGroup {

  private final String queueId;

  private final List deleteIds = new ArrayList();

  private final List indexIds = new ArrayList();

  private final Map pathIds = new LinkedHashMap();

  protected UpdateGroup(String queueId) {
    this.queueId = queueId;
  }

  public String getQueueId() {
    return queueId;
  }

  public List getDeleteIds() {
    return deleteIds;
  }

  public List getIndexIds() {
    return indexIds;
  }

  public Map getNestedPathIds() {
    return pathIds;
  }

  private void addIndex(Object id) {
    indexIds.add(id);
  }

  private void addDelete(Object id) {
    deleteIds.add(id);
  }

  private void addNested(String path, Object beanId) {
    UpdateNested nested = pathIds.get(path);
    if (nested == null) {
      nested = new UpdateNested(path);
      pathIds.put(path, nested);
    }
    nested.addId(beanId);
  }

  protected void addEntry(DocStoreQueueEntry entry) {

    DocStoreQueueEntry.Action type = entry.getType();
    switch (type) {
      case DELETE:
        addDelete(entry.getBeanId());
        break;
      case INDEX:
        addIndex(entry.getBeanId());
        break;
      case NESTED:
        addNested(entry.getPath(), entry.getBeanId());
        break;
      default:
        throw new IllegalArgumentException("type " + type + " not handled");
    }

  }

}