Skip to main content
summaryrefslogtreecommitdiffstats
blob: e39b5136077db9d214bd78b71ce60e6f7a8361c4 (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) 2009 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.framework.database.operation;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.core.enums.ModificationType;
import org.eclipse.osee.framework.core.enums.TxChange;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.core.operation.OperationLogger;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.database.core.AbstractDbTxOperation;
import org.eclipse.osee.framework.database.core.ConnectionHandler;
import org.eclipse.osee.framework.database.core.ExportImportJoinQuery;
import org.eclipse.osee.framework.database.core.IOseeStatement;
import org.eclipse.osee.framework.database.core.JoinUtility;
import org.eclipse.osee.framework.database.core.OseeConnection;
import org.eclipse.osee.framework.database.internal.Activator;
import org.eclipse.osee.framework.jdk.core.util.Strings;

/**
 * @author Ryan D. Brooks
 */
public class ConsolidateRelationsTxOperation extends AbstractDbTxOperation {
   private static final String SELECT_RELATIONS =
      "select * from osee_relation_link order by rel_link_type_id, a_art_id, b_art_id, gamma_id";

   private static final String SELECT_RELATION_ADDRESSING =
      "select txs.*, idj.id1 as net_gamma_id from osee_join_export_import idj, osee_txs txs where idj.query_id = ? and idj.id2 = txs.gamma_id order by net_gamma_id, transaction_id, id2";

   private static final String UPDATE_TXS_GAMMAS =
      "update osee_txs set gamma_id = ?, mod_type = ?, tx_current = ? where transaction_id = ? and gamma_id = ?";

   private static final String DELETE_TXS = "delete from osee_txs where transaction_id = ? and gamma_id = ?";

   private static final String DELETE_RELATIONS = "delete from osee_relation_link where gamma_id = ?";

   private final List<Long[]> relationDeleteData = new ArrayList<Long[]>(14000);
   private final List<Long> obsoleteGammas = new ArrayList<Long>();
   private final StringBuilder addressingBackup = new StringBuilder(100000);
   private final List<Object[]> addressingToDelete = new ArrayList<Object[]>(13000);
   private final List<Object[]> updateAddressingData = new ArrayList<Object[]>(5000);
   private ExportImportJoinQuery gammaJoin;
   private OseeConnection connection;
   private int previousRelationTypeId;
   private int previousArtifactAId;
   private int previousArtiafctBId;
   private long netGamma;
   private String netRationale;
   boolean materiallyDifferent;
   boolean updateAddressing;
   private int counter;
   private IOseeStatement chStmt;

   long previousNetGammaId;
   long previousObsoleteGammaId;
   int previousTransactionId;
   ModificationType netModType;
   TxChange netTxCurrent;

   public ConsolidateRelationsTxOperation(IOseeDatabaseService databaseService, OperationLogger logger) {
      super(databaseService, "Consolidate Relations", Activator.PLUGIN_ID, logger);
   }

   private void init() throws OseeCoreException {
      previousRelationTypeId = -1;
      previousArtifactAId = -1;
      previousArtiafctBId = -1;
      materiallyDifferent = true;
      relationDeleteData.clear();
      obsoleteGammas.clear();
      updateAddressingData.clear();
      addressingToDelete.clear();
      addressingBackup.delete(0, 999999999);
      updateAddressing = false;

      previousNetGammaId = -1;
      previousTransactionId = -1;
      chStmt = ConnectionHandler.getStatement();
      gammaJoin = JoinUtility.createExportImportJoinQuery();

      counter = 0;
   }

   @Override
   protected void doTxWork(IProgressMonitor monitor, OseeConnection connection) throws OseeCoreException {
      this.connection = connection;
      log("Consolidating relations:");
      init();

      findObsoleteRelations();

      log("gamma join size: " + gammaJoin.size());

      determineAffectedAddressing();

      updateGammas();
      log("...done.");
   }

   private void findObsoleteRelations() throws OseeCoreException {
      try {
         chStmt.runPreparedQuery(10000, SELECT_RELATIONS);
         while (chStmt.next()) {
            int relationTypeId = chStmt.getInt("rel_link_type_id");
            int artifactAId = chStmt.getInt("a_art_id");
            int artiafctBId = chStmt.getInt("b_art_id");

            if (isNextConceptualRelation(relationTypeId, artifactAId, artiafctBId)) {
               consolidate();
               initNextConceptualRelation(relationTypeId, artifactAId, artiafctBId);
            } else {
               obsoleteGammas.add(chStmt.getLong("gamma_id"));
               relationMateriallyDiffers(chStmt);
            }
         }
      } finally {
         chStmt.close();
      }
   }

   private void consolidate() {
      if (!materiallyDifferent && obsoleteGammas.size() > 0) {
         gammaJoin.add(netGamma, netGamma);
         for (Long obsoleteGamma : obsoleteGammas) {
            gammaJoin.add(netGamma, obsoleteGamma);
            relationDeleteData.add(new Long[] {obsoleteGamma});
         }
      }
      if (materiallyDifferent) {
         counter++;
         log("rel type: " + previousRelationTypeId + "   A art id: " + previousArtifactAId + "   B art id: " + previousArtiafctBId);
      }
   }

   private void determineAffectedAddressing() throws OseeCoreException {
      gammaJoin.store();

      try {
         log("counter: " + counter);
         log("query id: " + gammaJoin.getQueryId());
         chStmt.runPreparedQuery(10000, SELECT_RELATION_ADDRESSING, gammaJoin.getQueryId());

         while (chStmt.next()) {
            long obsoleteGammaId = chStmt.getLong("gamma_id");
            int transactionId = chStmt.getInt("transaction_id");
            long netGammaId = chStmt.getLong("net_gamma_id");
            int modType = chStmt.getInt("mod_type");
            TxChange txCurrent = TxChange.getChangeType(chStmt.getInt("tx_current"));

            if (isNextAddressing(netGammaId, transactionId)) {
               if (updateAddressing) {
                  updateAddressingData.add(new Object[] {
                     previousNetGammaId,
                     netModType.getValue(),
                     netTxCurrent.getValue(),
                     previousTransactionId,
                     previousObsoleteGammaId});
               }
               updateAddressing = obsoleteGammaId != netGammaId;
               previousNetGammaId = netGammaId;
               previousObsoleteGammaId = obsoleteGammaId;
               previousTransactionId = transactionId;
               netModType = ModificationType.getMod(modType);
               netTxCurrent = txCurrent;
            } else {
               addressingToDelete.add(new Object[] {transactionId, obsoleteGammaId});
               computeNetAddressing(ModificationType.getMod(modType), txCurrent);
            }

            writeAddressingBackup(obsoleteGammaId, transactionId, netGammaId, modType, txCurrent);
         }
      } finally {
         chStmt.close();
      }
      gammaJoin.delete();
   }

   private boolean isNextAddressing(long netGammaId, int transactionId) {
      return previousNetGammaId != netGammaId || previousTransactionId != transactionId;
   }

   private void computeNetAddressing(ModificationType modificationType, TxChange txCurrent) throws OseeStateException {
      if (netTxCurrentNeedsUpdate(txCurrent)) {
         netTxCurrent = txCurrent;
         updateAddressing = true;
      }

      if (netModTypeNeedsUpdate(modificationType)) {
         netModType = modificationType;
         updateAddressing = true;
      } else if (!ignoreNetModType(modificationType)) {
         throw new OseeStateException("    modType [%s] != [%s]", modificationType, netModType);
      }
   }

   private boolean netTxCurrentNeedsUpdate(TxChange txCurrent) {
      if (txCurrent == netTxCurrent) {
         return false;
      }
      boolean needsUpdate = txCurrent == TxChange.NOT_CURRENT;
      needsUpdate |= txCurrent == TxChange.CURRENT && netTxCurrent.isDeleted();
      return needsUpdate || netTxCurrent == TxChange.DELETED && txCurrent == TxChange.ARTIFACT_DELETED;
   }

   private boolean netModTypeNeedsUpdate(ModificationType modificationType) {
      boolean needsUpdate = !modificationType.isDeleted() && netModType.isDeleted();
      needsUpdate |= netModType == ModificationType.NEW && modificationType == ModificationType.MODIFIED;
      return needsUpdate || netModType == ModificationType.DELETED && modificationType == ModificationType.ARTIFACT_DELETED;
   }

   private boolean ignoreNetModType(ModificationType modificationType) {
      boolean ignore = !netModType.isDeleted() && modificationType.isDeleted();
      ignore |= netModType == modificationType;
      ignore |= netModType == ModificationType.MODIFIED && modificationType == ModificationType.NEW;
      return ignore || netModType == ModificationType.ARTIFACT_DELETED && modificationType == ModificationType.DELETED;
   }

   private void updateGammas() throws OseeCoreException {
      log("Number of txs rows deleted: " + ConnectionHandler.runBatchUpdate(connection, DELETE_TXS, addressingToDelete));

      log("Number of relation rows deleted: " + ConnectionHandler.runBatchUpdate(connection, DELETE_RELATIONS,
         relationDeleteData));

      log("Number of txs rows updated: " + ConnectionHandler.runBatchUpdate(connection, UPDATE_TXS_GAMMAS,
         updateAddressingData));
   }

   private void writeAddressingBackup(long obsoleteGammaId, int transactionId, long netGammaId, int modType, TxChange txCurrent) {
      StringBuilder strB = new StringBuilder(30);

      strB.append(obsoleteGammaId);
      strB.append(",");
      strB.append(transactionId);
      strB.append(",");
      strB.append(netGammaId);
      strB.append(",");
      strB.append(modType);
      strB.append(",");
      strB.append(txCurrent.getValue());
      strB.append("\n");
      log(strB.toString());
   }

   private boolean isNextConceptualRelation(int relationTypeId, int artifactAId, int artiafctBId) {
      return previousRelationTypeId != relationTypeId || previousArtifactAId != artifactAId || previousArtiafctBId != artiafctBId;
   }

   private void relationMateriallyDiffers(IOseeStatement chStmt) throws OseeCoreException {
      if (!materiallyDifferent) {
         String currentRationale = chStmt.getString("rationale");
         materiallyDifferent |= Strings.isValid(currentRationale) && !currentRationale.equals(netRationale);
      }
   }

   private void initNextConceptualRelation(int relationTypeId, int artifactAId, int artiafctBId) throws OseeCoreException {
      obsoleteGammas.clear();
      previousRelationTypeId = relationTypeId;
      previousArtifactAId = artifactAId;
      previousArtiafctBId = artiafctBId;
      netGamma = chStmt.getInt("gamma_id");
      netRationale = chStmt.getString("rationale");
      materiallyDifferent = false;
   }
}

Back to the top