Пример #1
0
 private Multimap<String, IndexAction> makeBulkByType(
     ArrayListMultimap<String, IndexAction> actions) {
   Multimap<String, IndexAction> bulks = LinkedListMultimap.create();
   for (IndexAction action : actions.values()) {
     bulks.put(action.getIndexType(), action);
   }
   return bulks;
 }
Пример #2
0
  @Override
  public void enqueue(List<IndexAction> actions) {

    if (actions.size() == 1) {
      /* Atomic update here */
      CountDownLatch latch = new CountDownLatch(1);
      IndexAction action = actions.get(0);
      action.setLatch(latch);
      try {
        this.offer(action, 1000, TimeUnit.SECONDS);
        latch.await(1500, TimeUnit.MILLISECONDS);
        // refresh the index.
        action.getIndex().refresh();
      } catch (InterruptedException e) {
        throw new IllegalStateException("ES update has been interrupted", e);
      }
    } else if (actions.size() > 1) {

      /* Purge actions that would be overridden  */
      Map<String, Integer> itemOffset = new HashMap<String, Integer>();
      ArrayListMultimap<String, IndexAction> itemActions = ArrayListMultimap.create();
      List<IndexAction> embeddedActions = new LinkedList<IndexAction>();
      for (IndexAction action : actions) {
        if (action.getClass().isAssignableFrom(EmbeddedIndexAction.class)) {
          embeddedActions.add(action);
        } else {
          String actionKey = action.getKey();
          Integer offset = 0;
          if (!itemOffset.containsKey(actionKey)) {
            itemOffset.put(actionKey, offset);
            itemActions.put(actionKey, action);
          } else {
            offset = itemOffset.get(actionKey);
            if (action.getClass().isAssignableFrom(KeyIndexAction.class)) {
              itemOffset.put(actionKey, 0);
              itemActions.get(actionKey).set(0, action);
            } else {
              itemActions.get(actionKey).set(offset, action);
            }
          }
        }
      }

      try {
        /* execute all item actions */
        Multimap<String, IndexAction> itemBulks = makeBulkByType(itemActions);
        CountDownLatch itemLatch = new CountDownLatch(itemBulks.size());
        for (IndexAction action : itemBulks.values()) {
          action.setLatch(itemLatch);
          this.offer(action, 1000, TimeUnit.SECONDS);
        }
        itemLatch.await(1500, TimeUnit.MILLISECONDS);

        /* and now push the embedded */
        CountDownLatch embeddedLatch = new CountDownLatch(embeddedActions.size());
        for (IndexAction action : embeddedActions) {
          action.setLatch(embeddedLatch);
          this.offer(action, 1000, TimeUnit.SECONDS);
        }
        embeddedLatch.await(1500, TimeUnit.MILLISECONDS);

        /* Finally refresh affected indexes */
        Set<String> refreshedIndexes = new HashSet<String>();
        for (IndexAction action : actions) {
          if (action.getIndex() != null
              && !refreshedIndexes.contains(action.getIndex().getIndexName())) {
            action.getIndex().refresh();
            refreshedIndexes.add(action.getIndex().getIndexName());
          }
        }

      } catch (InterruptedException e) {
        throw new IllegalStateException("ES update has been interrupted", e);
      }
    }
  }