Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1e3d475f3d7100607aee55f42a13e4558f8fc3d3 (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
/*******************************************************************************
 * 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.producer;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.jdk.core.type.Triplet;
import org.eclipse.osee.jdbc.JdbcClient;
import org.eclipse.osee.jdbc.JdbcStatement;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.OrcsSession;
import org.eclipse.osee.orcs.data.AttributeTypes;
import org.eclipse.osee.orcs.data.BranchReadable;
import org.eclipse.osee.orcs.db.internal.callable.AbstractDatastoreCallable;
import org.eclipse.osee.orcs.db.internal.search.indexer.IndexingTaskConsumer;
import org.eclipse.osee.orcs.db.internal.sql.join.IdJoinQuery;
import org.eclipse.osee.orcs.db.internal.sql.join.SqlJoinFactory;
import org.eclipse.osee.orcs.db.internal.sql.join.TagQueueJoinQuery;
import org.eclipse.osee.orcs.search.IndexerCollector;

/**
 * @author Roberto E. Escobar
 */
public final class IndexBranchesDatabaseCallable extends AbstractDatastoreCallable<Integer> {
   private static final int BATCH_SIZE = 1000;

   private static final String FIND_ALL_TAGGABLE_ATTRIBUTES =
      "SELECT att.gamma_id FROM osee_join_id oji, osee_attribute att WHERE oji.query_id = ? AND oji.id = att.attr_type_id";

   private static final String COUNT_ALL_TAGGABLE_ATTRIBUTES = FIND_ALL_TAGGABLE_ATTRIBUTES.replace("att.gamma_id",
      "count(1)");

   private static final String FIND_MISSING =
      FIND_ALL_TAGGABLE_ATTRIBUTES + " AND att.gamma_id NOT IN (SELECT gamma_id FROM osee_search_tags)";

   private static final String COUNT_MISSING = FIND_MISSING.replaceFirst("att.gamma_id", "count(1)");

   private static final String FIND_TAGGABLE_ATTRIBUTES_BY_BRANCH =
      "SELECT DISTINCT att.gamma_id FROM osee_join_id jid1, osee_join_id jid2, osee_txs txs, osee_attribute att WHERE jid1.query_id = ? AND jid1.id = txs.branch_id AND txs.gamma_id = att.gamma_id AND att.attr_type_id = jid2.id and jid2.query_id = ?";

   private static final String COUNT_TAGGABLE_ATTRIBUTES_BY_BRANCH = FIND_TAGGABLE_ATTRIBUTES_BY_BRANCH.replace(
      "DISTINCT att.gamma_id", "count(DISTINCT att.gamma_id)");

   private static final String FIND_MISSING_BY_BRANCH =
      FIND_TAGGABLE_ATTRIBUTES_BY_BRANCH + " AND att.gamma_id NOT IN (SELECT gamma_id FROM osee_search_tags)";

   private static final String COUNT_MISSING_BY_BRANCH =
      COUNT_TAGGABLE_ATTRIBUTES_BY_BRANCH + " AND att.gamma_id NOT IN (SELECT gamma_id FROM osee_search_tags)";

   private final SqlJoinFactory joinFactory;
   private final AttributeTypes types;
   private final IndexingTaskConsumer consumer;
   private final IndexerCollector collector;
   private final Collection<BranchReadable> branches;
   private final Collection<? extends IAttributeType> typesToTag;
   private final boolean tagOnlyMissingGammas;

   public IndexBranchesDatabaseCallable(Log logger, OrcsSession session, JdbcClient service, SqlJoinFactory joinFactory, AttributeTypes types, IndexingTaskConsumer consumer, IndexerCollector collector, Collection<? extends IAttributeType> typesToTag, Collection<BranchReadable> branches, boolean tagOnlyMissingGammas) {
      super(logger, session, service);
      this.joinFactory = joinFactory;
      this.types = types;
      this.consumer = consumer;
      this.collector = collector;
      this.typesToTag = typesToTag;
      this.branches = branches;
      this.tagOnlyMissingGammas = tagOnlyMissingGammas;
   }

   @Override
   public Integer call() throws Exception {
      getLogger().info(getParamInfo());

      Set<Long> branchUuids = new HashSet<Long>();
      for (BranchReadable branch : branches) {
         branchUuids.add(branch.getUuid());
      }

      IdJoinQuery branchJoin = joinFactory.createIdJoinQuery();
      IdJoinQuery typeJoin = joinFactory.createIdJoinQuery();
      try {
         Triplet<String, String, Object[]> data = createQueries(branchUuids, branchJoin, typeJoin);
         String countQuery = data.getFirst();
         String searchQuery = data.getSecond();
         Object[] params = data.getThird();

         for (IAttributeType attributeType : typesToTag) {
            if (types.isTaggable(attributeType)) {
               typeJoin.add(attributeType.getGuid());
            }
         }

         typeJoin.store();
         branchJoin.store();

         if (collector != null) {
            int totalAttributes = getJdbcClient().runPreparedQueryFetchObject(-1, countQuery, params);
            collector.onIndexTotalTaskItems(totalAttributes);
         }

         fetchAndProcessGammas(searchQuery, params);
      } finally {
         typeJoin.delete();
         branchJoin.delete();
      }
      return branchUuids.size();
   }

   public void storeAndAddQueryId(TagQueueJoinQuery joinQuery) throws Exception {
      if (!joinQuery.isEmpty()) {
         joinQuery.store();
         consumer.submitTaskId(getSession(), types, collector, joinQuery.getQueryId());
      }
   }

   private void fetchAndProcessGammas(String query, Object... params) throws Exception {
      JdbcStatement chStmt = getJdbcClient().getStatement();
      try {
         chStmt.runPreparedQuery(query, params);
         TagQueueJoinQuery joinQuery = joinFactory.createTagQueueJoinQuery();
         while (chStmt.next()) {
            long gammaId = chStmt.getLong("gamma_id");
            joinQuery.add(gammaId);
            if (joinQuery.size() >= BATCH_SIZE) {
               storeAndAddQueryId(joinQuery);
               joinQuery = joinFactory.createTagQueueJoinQuery();
            }
         }
         storeAndAddQueryId(joinQuery);
      } finally {
         chStmt.close();
      }
   }

   private String getParamInfo() {
      StringBuilder builder = new StringBuilder();
      builder.append("Tagging");
      if (tagOnlyMissingGammas) {
         builder.append(" (Only Missing)");
      }
      builder.append(" Attributes for ");
      if (branches.isEmpty()) {
         builder.append("All Branches");
      } else {
         builder.append("Branch(es) ").append(branches);
      }
      return builder.toString();
   }

   private Triplet<String, String, Object[]> createQueries(Collection<Long> branchUuids, IdJoinQuery branchJoin, IdJoinQuery typeJoin) {
      Object[] params;
      String countQuery;
      String searchQuery;
      if (branchUuids.isEmpty()) {
         params = new Object[] {typeJoin.getQueryId()};
         if (tagOnlyMissingGammas) {
            countQuery = COUNT_MISSING;
            searchQuery = FIND_MISSING;
         } else {
            countQuery = COUNT_ALL_TAGGABLE_ATTRIBUTES;
            searchQuery = FIND_ALL_TAGGABLE_ATTRIBUTES;
         }
      } else {
         for (Long id : branchUuids) {
            branchJoin.add(id);
         }
         params = new Object[] {branchJoin.getQueryId(), typeJoin.getQueryId()};
         if (tagOnlyMissingGammas) {
            countQuery = COUNT_MISSING_BY_BRANCH;
            searchQuery = FIND_MISSING_BY_BRANCH;
         } else {
            countQuery = COUNT_TAGGABLE_ATTRIBUTES_BY_BRANCH;
            searchQuery = FIND_TAGGABLE_ATTRIBUTES_BY_BRANCH;
         }
      }
      return new Triplet<String, String, Object[]>(countQuery, searchQuery, params);
   }
}

Back to the top