MtasSolrBaseList.java

  1. package mtas.solr.handler.util;

  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Objects;
  9. import java.util.stream.Collectors;

  10. import org.apache.solr.common.util.SimpleOrderedMap;

  11. /**
  12.  * The Class MtasSolrBaseList.
  13.  */
  14. public abstract class MtasSolrBaseList {

  15.   /** The list. */
  16.   protected List<MtasSolrStatus> list;

  17.   /** The index. */
  18.   protected Map<String, MtasSolrStatus> index;

  19.   /** The enabled. */
  20.   private boolean enabled = true;

  21.   /** The Constant NAME_ENABLED. */
  22.   private final static String NAME_ENABLED = "enabled";

  23.   /** The Constant NAME_LIST. */
  24.   private final static String NAME_LIST = "list";

  25.   /** The Constant NAME_SIZE_TOTAL. */
  26.   private final static String NAME_SIZE_TOTAL = "sizeTotal";

  27.   /** The Constant NAME_SIZE_NORMAL. */
  28.   private final static String NAME_SIZE_NORMAL = "sizeNormal";

  29.   /** The Constant NAME_SIZE_SHARDREQUESTS. */
  30.   private final static String NAME_SIZE_SHARDREQUESTS = "sizeShardRequests";

  31.   /**
  32.    * Instantiates a new mtas solr base list.
  33.    */
  34.   public MtasSolrBaseList() {
  35.     list = Collections.synchronizedList(new ArrayList<MtasSolrStatus>());
  36.     index = Collections.synchronizedMap(new HashMap<>());
  37.   }

  38.   /**
  39.    * Gets the.
  40.    *
  41.    * @param key the key
  42.    * @return the mtas solr status
  43.    * @throws IOException Signals that an I/O exception has occurred.
  44.    */
  45.   public final MtasSolrStatus get(String key) throws IOException {
  46.     return index.get(Objects.requireNonNull(key, "no key provided"));
  47.   }

  48.   /**
  49.    * Adds the.
  50.    *
  51.    * @param status the status
  52.    * @throws IOException Signals that an I/O exception has occurred.
  53.    */
  54.   public void add(MtasSolrStatus status) throws IOException {
  55.     Objects.requireNonNull(status);
  56.     if (enabled) {
  57.       list.add(status);
  58.       if (!index.containsKey(status.key())) {
  59.         index.put(status.key(), status);
  60.         garbageCollect();
  61.       } else {
  62.         garbageCollect();
  63.         // retry
  64.         MtasSolrStatus oldStatus = index.get(status.key());
  65.         if (oldStatus == null) {
  66.           index.put(status.key(), status);
  67.         } else if(oldStatus.finished()) {
  68.           remove(oldStatus);
  69.           index.put(status.key(), status);
  70.         } else {
  71.           throw new IOException("key " + status.key() + " already exists");
  72.         }
  73.       }
  74.     }
  75.   }

  76.   /**
  77.    * Removes the.
  78.    *
  79.    * @param status the status
  80.    */
  81.   public final void remove(MtasSolrStatus status) {
  82.     Objects.requireNonNull(status);
  83.     list.remove(status);
  84.     index.remove(status.key());
  85.   }

  86.   /**
  87.    * Garbage collect.
  88.    */
  89.   public abstract void garbageCollect();

  90.   /**
  91.    * Reset.
  92.    */
  93.   public final void reset() {
  94.     list.clear();
  95.     index.clear();
  96.   }

  97.   /**
  98.    * Update key.
  99.    *
  100.    * @param key the key
  101.    * @throws IOException Signals that an I/O exception has occurred.
  102.    */
  103.   public final void updateKey(String key) throws IOException {
  104.     Objects.requireNonNull(key, "old key required");
  105.     if (index.containsKey(key)) {
  106.       MtasSolrStatus status = index.get(key);
  107.       index.remove(key);
  108.       if (!index.containsKey(status.key())) {
  109.         index.put(status.key(), status);
  110.       } else {
  111.         throw new IOException("key already exists");
  112.       }
  113.     }
  114.   }

  115.   /**
  116.    * Sets the enabled.
  117.    *
  118.    * @param flag the new enabled
  119.    */
  120.   public final void setEnabled(boolean flag) {
  121.     if (enabled != flag) {
  122.       reset();
  123.       enabled = flag;
  124.     }
  125.   }

  126.   /**
  127.    * Enabled.
  128.    *
  129.    * @return true, if successful
  130.    */
  131.   public final boolean enabled() {
  132.     return enabled;
  133.   }

  134.   /**
  135.    * Creates the list output.
  136.    *
  137.    * @param shardRequests the shard requests
  138.    * @return the simple ordered map
  139.    */
  140.   public SimpleOrderedMap<Object> createListOutput(boolean shardRequests) {
  141.     garbageCollect();
  142.     SimpleOrderedMap<Object> output = new SimpleOrderedMap<>();
  143.     output.add(NAME_ENABLED, enabled());
  144.     output.add(NAME_ENABLED, true);
  145.     int numberTotal = list.size();
  146.     ListData listData = new ListData();
  147.     if (numberTotal > 0) {
  148.       // create list
  149.       list.stream()
  150.           .collect(Collectors.collectingAndThen(Collectors.toList(), lst -> {
  151.             Collections.reverse(lst);
  152.             return lst.stream();
  153.           })).forEach((item) -> {
  154.             if (item.shardRequest()) {
  155.               listData.addShardRequest();
  156.               if (shardRequests) {
  157.                 listData.outputList.add(item.createItemOutput());
  158.               }
  159.             } else {
  160.               listData.addNormal();
  161.               listData.outputList.add(item.createItemOutput());
  162.             }
  163.           });
  164.     }
  165.     output.add(NAME_SIZE_TOTAL, numberTotal);
  166.     output.add(NAME_SIZE_NORMAL, listData.numberNormal);
  167.     output.add(NAME_SIZE_SHARDREQUESTS, listData.numberShardRequests);
  168.     output.add(NAME_LIST, listData.outputList);
  169.     return output;
  170.   }

  171.   /**
  172.    * The Class ListData.
  173.    */
  174.   static class ListData {
  175.    
  176.     /** The output list. */
  177.     private List<SimpleOrderedMap<Object>> outputList;
  178.    
  179.     /** The number normal. */
  180.     private int numberNormal;
  181.    
  182.     /** The number shard requests. */
  183.     private int numberShardRequests;

  184.     /**
  185.      * Instantiates a new list data.
  186.      */
  187.     public ListData() {
  188.       outputList = new ArrayList<>();
  189.       numberNormal = 0;
  190.       numberShardRequests = 0;
  191.     }

  192.     /**
  193.      * Adds the normal.
  194.      */
  195.     public void addNormal() {
  196.       numberNormal++;
  197.     }

  198.     /**
  199.      * Adds the shard request.
  200.      */
  201.     public void addShardRequest() {
  202.       numberShardRequests++;
  203.     }

  204.   }

  205. }