Skip to main content
summaryrefslogtreecommitdiffstats
blob: 11b379bfedc37757f3d5c4d84f2430a9deba92bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.orcs.db.internal.search.indexer.callable.consumer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.jdbc.JdbcClient;
import org.eclipse.osee.jdbc.JdbcConnection;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.OrcsSession;
import org.eclipse.osee.orcs.core.ds.IndexedResource;
import org.eclipse.osee.orcs.core.ds.OrcsDataHandler;
import org.eclipse.osee.orcs.data.AttributeTypes;
import org.eclipse.osee.orcs.db.internal.callable.AbstractDatastoreTxCallable;
import org.eclipse.osee.orcs.db.internal.search.indexer.IndexedResourceLoader;
import org.eclipse.osee.orcs.db.internal.search.tagger.TagCollector;
import org.eclipse.osee.orcs.db.internal.search.tagger.Tagger;
import org.eclipse.osee.orcs.db.internal.search.tagger.TaggingEngine;
import org.eclipse.osee.orcs.db.internal.sql.join.DatabaseJoinAccessor.JoinItem;
import org.eclipse.osee.orcs.search.IndexerCollector;

/**
 * @author Roberto E. Escobar
 */
public final class IndexingTaskDatabaseTxCallable extends AbstractDatastoreTxCallable<Long> {

   private static final String INSERT_SEARCH_TAG =
      "insert into osee_search_tags (gamma_id, coded_tag_id) values (?, ?)";

   private static final String DELETE_SEARCH_TAGS = "delete from osee_search_tags where gamma_id = ?";

   private final IndexedResourceLoader loader;
   private final TaggingEngine taggingEngine;
   private final IndexerCollector collector;
   private final int tagQueueQueryId;
   private final boolean isCacheAll;
   private final int cacheLimit;
   private final AttributeTypes attributeTypes;

   private final long waitStartTime;
   private long startTime;
   private long waitTime;

   public IndexingTaskDatabaseTxCallable(Log logger, OrcsSession session, JdbcClient jdbcClient, IndexedResourceLoader loader, TaggingEngine taggingEngine, IndexerCollector collector, int tagQueueQueryId, boolean isCacheAll, int cacheLimit, AttributeTypes attributeTypes) {
      super(logger, session, jdbcClient);
      waitStartTime = System.currentTimeMillis();

      this.loader = loader;
      this.taggingEngine = taggingEngine;
      this.collector = collector;
      this.tagQueueQueryId = tagQueueQueryId;
      this.cacheLimit = cacheLimit;
      this.isCacheAll = isCacheAll;
      this.attributeTypes = attributeTypes;
   }

   public int getTagQueueQueryId() {
      return tagQueueQueryId;
   }

   private OrcsDataHandler<IndexedResource> createCollector(final Collection<IndexedResource> sources) {
      return new OrcsDataHandler<IndexedResource>() {

         @Override
         public void onData(IndexedResource data) {
            sources.add(data);
         }
      };
   }

   @Override
   protected Long handleTxWork(JdbcConnection connection) throws OseeCoreException {
      getLogger().debug("Tagging: [%s]", getTagQueueQueryId());
      long totalTags = -1;
      try {
         Collection<IndexedResource> sources = new LinkedHashSet<>();
         OrcsDataHandler<IndexedResource> handler = createCollector(sources);
         loader.loadSource(handler, getTagQueueQueryId());

         if (!sources.isEmpty()) {
            try {
               deleteTags(connection, sources);
               totalTags = createTags(connection, sources);
               removeIndexingTaskFromQueue(connection);
            } catch (Exception ex) {
               throw new OseeCoreException(ex, "Unable to store tags - tagQueueQueryId [%d]", getTagQueueQueryId());
            }
         } else {
            getLogger().warn("Empty gamma query id: %s", getTagQueueQueryId());
         }
      } finally {
         getLogger().debug("End Tagging: [%s] totalTags[%s]", getTagQueueQueryId(), totalTags);
      }
      return totalTags;
   }

   private String getTaggerIdByTypeUuid(long typeUuid) throws OseeCoreException {
      IAttributeType type = attributeTypes.getByUuid(typeUuid);
      return attributeTypes.getTaggerId(type);
   }

   private long createTags(JdbcConnection connection, Collection<IndexedResource> sources) throws OseeCoreException {
      SearchTagCollector tagCollector = new SearchTagCollector();

      Set<Long> processed = new HashSet<>();

      Map<Long, Collection<Long>> toStore = new HashMap<>();
      for (IndexedResource source : sources) {
         long startItemTime = System.currentTimeMillis();
         Long gamma = source.getGammaId();
         if (processed.add(gamma)) {
            Set<Long> tags = new HashSet<>();
            toStore.put(gamma, tags);
            tagCollector.setCurrentTag(gamma, tags);
            try {
               long typeUuid = source.getTypeUuid();
               String taggerId = getTaggerIdByTypeUuid(typeUuid);
               if (taggingEngine.hasTagger(taggerId)) {
                  Tagger tagger = taggingEngine.getTagger(taggerId);
                  tagger.tagIt(source, tagCollector);
                  if (isStorageAllowed(toStore)) {
                     getLogger().debug("Stored a - [%s] - connectionId[%s] - [%s]", getTagQueueQueryId(), connection,
                        toStore);
                     storeTags(connection, toStore);
                  }
               } else {
                  getLogger().error("Field has invalid tagger[%s] provider and cannot be tagged - [Gamma: %s]",
                     taggerId, gamma);
               }
            } catch (Exception ex) {
               getLogger().error(ex, "Unable to tag - [%s]", gamma);
            } finally {
               long endItemTime = System.currentTimeMillis() - startItemTime;
               notifyOnIndexItemComplete(gamma, tags.size(), endItemTime);
            }
         }
      }

      if (!toStore.isEmpty()) {
         getLogger().debug("Stored b - [%s] - connectionId[%s] - [%s]", getTagQueueQueryId(), connection, toStore);
         storeTags(connection, toStore);
      }
      return tagCollector.getTotalTags();
   }

   @Override
   protected void handleTxException(Exception ex) {
      super.handleTxException(ex);
      if (collector != null) {
         collector.onIndexTaskError(getTagQueueQueryId(), ex);
      }
   }

   @Override
   protected void onExecutionStart() {
      super.onExecutionStart();
      startTime = System.currentTimeMillis();
      waitTime = startTime - waitStartTime;
   }

   @Override
   protected void onExecutionComplete() {
      super.onExecutionComplete();
      if (collector != null) {
         collector.onIndexTaskComplete(getTagQueueQueryId(), waitTime, System.currentTimeMillis() - startTime);
      }
   }

   private void removeIndexingTaskFromQueue(JdbcConnection connection) throws OseeCoreException {
      getJdbcClient().runPreparedUpdate(connection, JoinItem.TAG_GAMMA_QUEUE.getDeleteSql(), getTagQueueQueryId());
   }

   private boolean isStorageAllowed(Map<Long, Collection<Long>> searchTags) {
      int cummulative = 0;
      boolean needsStorage = false;
      for (Collection<Long> tags : searchTags.values()) {
         cummulative += tags.size();
         if (!isCacheAll && cummulative >= cacheLimit) {
            needsStorage = true;
            break;
         }
      }
      return needsStorage;
   }

   public int deleteTags(JdbcConnection connection, Collection<IndexedResource> sources) throws OseeCoreException {
      int numberDeleted = 0;
      if (!sources.isEmpty()) {
         List<Object[]> datas = new ArrayList<>();
         for (IndexedResource source : sources) {
            datas.add(new Object[] {source.getGammaId()});
         }
         numberDeleted = getJdbcClient().runBatchUpdate(connection, DELETE_SEARCH_TAGS, datas);
      }
      return numberDeleted;
   }

   private int storeTags(JdbcConnection connection, Map<Long, Collection<Long>> toStore) throws OseeCoreException {
      int updated = 0;
      if (!toStore.isEmpty()) {
         List<Object[]> data = new ArrayList<>();
         for (Entry<Long, Collection<Long>> entry : toStore.entrySet()) {
            Long gammaId = entry.getKey();
            for (Long codedTag : entry.getValue()) {
               data.add(new Object[] {gammaId, codedTag});
               getLogger().debug("Storing: gamma:[%s] tag:[%s]", gammaId, codedTag);
            }
         }
         toStore.clear();
         if (!data.isEmpty()) {
            updated += getJdbcClient().runBatchUpdate(connection, INSERT_SEARCH_TAG, data);
         }
      }
      return updated;
   }

   private void notifyOnIndexItemComplete(long gammaId, int totalTags, long processingTime) {
      if (collector != null) {
         collector.onIndexItemComplete(getTagQueueQueryId(), gammaId, totalTags, processingTime);
      }
   }

   private void notifyOnIndexItemAdded(long gammaId, String word, long codedTag) {
      if (collector != null) {
         collector.onIndexItemAdded(getTagQueueQueryId(), gammaId, word, codedTag);
      }
   }

   private final class SearchTagCollector implements TagCollector {

      private Long gammaId;
      private Set<Long> currentTag;
      private long totalTags;

      public SearchTagCollector() {
         this.totalTags = 0;
      }

      public void setCurrentTag(Long gammaId, Set<Long> currentTag) {
         this.gammaId = gammaId;
         this.currentTag = currentTag;
      }

      public long getTotalTags() {
         return totalTags;
      }

      @Override
      public void addTag(String word, Long codedTag) {
         if (currentTag != null && gammaId != null) {
            if (currentTag.add(codedTag)) {
               totalTags++;
               notifyOnIndexItemAdded(gammaId, word, codedTag);
            }
         }
      }
   }
}

Back to the top