|
| 1 | +package org.ecocean; |
| 2 | + |
| 3 | + |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Properties; |
| 8 | +import java.util.concurrent.ExecutorService; |
| 9 | +import java.util.concurrent.Executors; |
| 10 | +import org.ecocean.ShepherdProperties; |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +public class IndexingManager { |
| 15 | + |
| 16 | + //The ExecutorService executes indexing jobs |
| 17 | + private final ExecutorService executor; |
| 18 | + |
| 19 | + //The indexingQueue is a List of Strings that represent the UUIDs of Base class-implementing objects |
| 20 | + //(Encounter, MarkedIndividual, Annotation, etc.) that need to be indexed or unindexed. |
| 21 | + //The queue ensures that overzealous calls from the WildbookLifecycleListener do not cause |
| 22 | + //unnecessary, duplicate indexing jobs. The UUIDs of the objects being ndexed are removed |
| 23 | + //from the queue once completed. |
| 24 | + private List<String> indexingQueue = Collections.synchronizedList(new ArrayList<String>()); |
| 25 | + |
| 26 | + public IndexingManager() { |
| 27 | + |
| 28 | + int numAllowedThreads = 4; |
| 29 | + Properties props = ShepherdProperties.getProperties("OpenSearch.properties", "", "context0"); |
| 30 | + if(props!=null) { |
| 31 | + String indexingNumAllowedThreads = props.getProperty("indexingNumAllowedThreads"); |
| 32 | + if(indexingNumAllowedThreads!=null) { |
| 33 | + Integer allowThreads = Integer.getInteger(indexingNumAllowedThreads); |
| 34 | + if(allowThreads!=null)numAllowedThreads = allowThreads.intValue(); |
| 35 | + } |
| 36 | + } |
| 37 | + executor = Executors.newFixedThreadPool(numAllowedThreads); |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + //Returns the indexing queue List of Strings |
| 42 | + public List<String> getIndexingQueue() { return indexingQueue; } |
| 43 | + |
| 44 | + /* |
| 45 | + * Adds a Base object to the queue for indexing or unindexing |
| 46 | + * @Base base The Base-class implementing object to be indexed or unindexed |
| 47 | + * @boolean unindex Whether the object is to be indexed or unindexed. |
| 48 | + */ |
| 49 | + public void addIndexingQueueEntry(Base base, boolean unindex) { |
| 50 | + String objectID = base.getId(); |
| 51 | + Class myClass = base.getClass(); |
| 52 | + if(!indexingQueue.contains(objectID)) { |
| 53 | + indexingQueue.add(objectID); |
| 54 | + |
| 55 | + //IMPORTANT - no persistent objects, such as the passed in Base can be referenced inside this method |
| 56 | + Runnable rn = new Runnable() { |
| 57 | + public void run() { |
| 58 | + Shepherd bgShepherd = new Shepherd("context0"); |
| 59 | + bgShepherd.setAction("IndexingManager_" + objectID); |
| 60 | + bgShepherd.beginDBTransaction(); |
| 61 | + try { |
| 62 | + Base base = (Base)bgShepherd.getPM().getObjectById(myClass, objectID); |
| 63 | + if(unindex) {base.opensearchUnindexDeep();} |
| 64 | + else{base.opensearchIndexDeep();} |
| 65 | + |
| 66 | + } |
| 67 | + catch (Exception e) { |
| 68 | + e.printStackTrace(); |
| 69 | + } |
| 70 | + finally { |
| 71 | + bgShepherd.rollbackAndClose(); |
| 72 | + } |
| 73 | + |
| 74 | + //remove from indexing queue |
| 75 | + if(indexingQueue.contains(objectID))indexingQueue.remove(objectID); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + executor.execute(rn); |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + //Removes an oject's UUID from the queue |
| 86 | + public void removeIndexingQueueEntry(String objectID) { |
| 87 | + if (indexingQueue.contains(objectID)) { |
| 88 | + indexingQueue.remove(objectID); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + //Resets the indexing queue |
| 93 | + public void resetIndexingQueuehWithInitialCapacity(int initialCapacity) { |
| 94 | + indexingQueue = null; |
| 95 | + indexingQueue = Collections.synchronizedList(new ArrayList<String>()); |
| 96 | + } |
| 97 | + |
| 98 | + public void shutdown() { |
| 99 | + if(executor!=null)executor.shutdown(); |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments