Skip to main content
summaryrefslogtreecommitdiffstats
blob: 61d8e8abff048a87edbf3d03c68fa5e12fcd034d (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*******************************************************************************
 * Copyright (c) 2010 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.OseeDataStoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.core.operation.OperationReporter;
import org.eclipse.osee.framework.database.IOseeDatabaseServiceProvider;
import org.eclipse.osee.framework.database.core.AbstractDbTxOperation;
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.core.JoinUtility.ExportImportJoinQuery;
import org.eclipse.osee.framework.database.internal.Activator;

/**
 * @author Ryan D. Brooks
 */
public class ConsolidateArtifactVersionTxOperation extends AbstractDbTxOperation {
   private static final String SELECT_ARTIFACT_VERSIONS =
         "select * from osee_artifact_version order by art_id, gamma_id";

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

   private static final String UPDATE_CONFLICTS =
         "update osee_conflict set %s = (select gamma_id from osee_artifact_version where conflict_id = art_id) where conflict_type = 3";

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

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

   private static final String DELETE_ARTIFACT_VERSIONS = "delete from osee_artifact_version where gamma_id = ?";

   private static final String SET_BASELINE_TRANSACTION =
         "UPDATE osee_branch ob SET ob.baseline_transaction_id = (SELECT otd.transaction_id FROM osee_tx_details otd WHERE otd.branch_id = ob.branch_id AND otd.tx_type = 1)";

   private static final String POPULATE_ARTS =
         "insert into osee_artifact(gamma_id, art_id, art_type_id, guid, human_readable_id) select gamma_id, art.art_id, art_type_id, guid, human_readable_id from osee_artifact art, osee_artifact_version arv where art.art_id = arv.art_id and not exists (select 1 from osee_artifact arts where art.art_id = arts.art_id)";

   private static final String FIND_ARTIFACT_MODS =
         "select * from osee_artifact art, osee_txs txs where art.gamma_id = txs.gamma_id order by art_id, branch_id, transaction_id";

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

   private List<Long[]> deleteArtifactVersionData;
   private final List<Long> obsoleteGammas = new ArrayList<Long>();
   private final List<Object[]> addressingToDelete = new ArrayList<Object[]>(13000);
   private final List<Object[]> updateAddressingData = new ArrayList<Object[]>(5000);
   private final List<Object[]> updateTxsCurrentModData = new ArrayList<Object[]>(5000);
   private ExportImportJoinQuery gammaJoin;
   private OseeConnection connection;
   private int previousArtifactId;
   private long netGamma;
   private IOseeStatement chStmt;
   private long previousNetGammaId;
   private int previousBranchId;
   private int previuosTransactionId;
   private final OperationReporter reporter;
   private int updateTxsCounter;
   private int deleteTxsCounter;

   public ConsolidateArtifactVersionTxOperation(IOseeDatabaseServiceProvider provider, OperationReporter reporter) {
      super(provider, "Consolidate Artifact Versions", Activator.PLUGIN_ID);
      this.reporter = reporter;
   }

   private void init() throws OseeCoreException {
      deleteArtifactVersionData = new ArrayList<Long[]>(14000);
      obsoleteGammas.clear();
      updateAddressingData.clear();
      addressingToDelete.clear();
      previousArtifactId = -1;
      previousNetGammaId = -1;
      previousBranchId = -1;
      previuosTransactionId = -1;
      updateTxsCounter = 0;
      deleteTxsCounter = 0;
      chStmt = getDatabaseService().getStatement(connection);
      gammaJoin = JoinUtility.createExportImportJoinQuery();
   }

   private void findArtifactMods() throws OseeCoreException {
      List<Address> mods = new ArrayList<Address>();
      try {
         chStmt.runPreparedQuery(10000, FIND_ARTIFACT_MODS);
         while (chStmt.next()) {
            int artifactId = chStmt.getInt("art_id");
            int branchId = chStmt.getInt("branch_id");

            if (previousArtifactId != artifactId || previousBranchId != branchId) {
               if (mods.size() > 0) {
                  consolidateMods(mods);
                  mods.clear();
               }
               previousArtifactId = artifactId;
               previousBranchId = branchId;
            }
            mods.add(new Address(false, -1, -1, chStmt.getInt("transaction_id"), chStmt.getInt("gamma_id"),
                  ModificationType.getMod(chStmt.getInt("mod_type")),
                  TxChange.getChangeType(chStmt.getInt("tx_current"))));
         }
      } finally {
         if (chStmt != null) {
            chStmt.close();
         }
      }
   }

   private void consolidateMods(List<Address> mods) {
      ModificationType mod0 = mods.get(0).getModType();
      boolean knownCase = false;
      if (mods.size() == 1) {
         if (mod0 == ModificationType.MODIFIED) {
            knownCase = true;
            updateTxsCurrentModData.add(new Object[] {ModificationType.NEW.getValue(),
                  mods.get(0).getTxCurrent().getValue(), mods.get(0).getTransactionId(), mods.get(0).getGammaId()});
         } else {
            knownCase = true;
         }
      } else {
         ModificationType mod1 = mods.get(1).getModType();
         if (mod0.matches(ModificationType.NEW, ModificationType.INTRODUCED, ModificationType.MERGED)) {
            if (mods.size() == 2 && mod1.matches(ModificationType.DELETED, ModificationType.MERGED)) {
               knownCase = true;
            } else if (mods.size() == 3) {
               ModificationType mod2 = mods.get(2).getModType();
               if (mod1 == ModificationType.DELETED && mod2 == ModificationType.DELETED) {
                  knownCase = true;
                  // must purge most recent delete and set previous one to current
                  updateTxsCurrentModData.add(new Object[] {mods.get(1).getModType().getValue(),
                        TxChange.DELETED.getValue(), mods.get(1).getTransactionId(), mods.get(1).getGammaId()});
                  addressingToDelete.add(new Object[] {mods.get(2).getTransactionId(), mods.get(2).getGammaId()});
               } else if (mod1 == ModificationType.MERGED && mod2 == ModificationType.DELETED) {
                  knownCase = true;
               }
            }
         }
      }
      if (!knownCase) {
         reporter.report(String.format("unknown case: artifact id: %d branch_id: %d", previousArtifactId,
               previousBranchId));
      }
   }

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

      if (true) {
         findArtifactMods();

         reporter.report("updateTxsCurrentModData size: " + updateTxsCurrentModData.size());
         reporter.report("addressingToDelete size: " + addressingToDelete.size());

         getDatabaseService().runBatchUpdate(connection, prepareSql(UPDATE_TXS_MOD_CURRENT, false),
               updateTxsCurrentModData);
         getDatabaseService().runBatchUpdate(connection, prepareSql(DELETE_TXS, false), addressingToDelete);

         return;
      }

      findObsoleteGammas();
      reporter.report("gamma join size: " + gammaJoin.size());

      reporter.report("Number of artifact version rows deleted: " + getDatabaseService().runBatchUpdate(connection,
            DELETE_ARTIFACT_VERSIONS, deleteArtifactVersionData));
      deleteArtifactVersionData = null;

      gammaJoin.store(connection);

      updataConflicts("source_gamma_id");
      updataConflicts("dest_gamma_id");

      setBaselineTransactions();

      populateArts();

      determineAffectedAddressingAndFix(false);
      //determineAffectedAddressingAndFix(true);

      gammaJoin.delete(connection);
   }

   private void updataConflicts(String columnName) throws OseeCoreException {
      int count = getDatabaseService().runPreparedUpdate(connection, String.format(UPDATE_CONFLICTS, columnName));
      reporter.report(String.format("updated %s in %d rows", columnName, count));
   }

   private void setBaselineTransactions() throws OseeCoreException {
      int count = getDatabaseService().runPreparedUpdate(connection, SET_BASELINE_TRANSACTION);
      reporter.report(String.format("updated %d baseline transactions", count));
   }

   private void populateArts() throws OseeCoreException {
      int count = getDatabaseService().runPreparedUpdate(connection, POPULATE_ARTS);
      reporter.report(String.format("inserted %d rows into osee_artifact", count));
   }

   private void findObsoleteGammas() throws OseeCoreException {
      try {
         chStmt.runPreparedQuery(10000, SELECT_ARTIFACT_VERSIONS);
         while (chStmt.next()) {
            int artifactId = chStmt.getInt("art_id");

            if (isNextConceptualArtifact(artifactId)) {
               consolidate();
               initNextConceptualArtifact(artifactId);
            } else {
               obsoleteGammas.add(chStmt.getLong("gamma_id"));
            }
         }
      } finally {
         if (chStmt != null) {
            chStmt.close();
         }
      }
   }

   private void consolidate() {
      if (obsoleteGammas.size() > 0) {
         gammaJoin.add(netGamma, netGamma);
         for (Long obsoleteGamma : obsoleteGammas) {
            gammaJoin.add(netGamma, obsoleteGamma);
            deleteArtifactVersionData.add(new Long[] {obsoleteGamma});
         }
      }
   }

   private void determineAffectedAddressingAndFix(boolean archived) throws OseeCoreException {
      try {
         reporter.report("query id: " + gammaJoin.getQueryId());
         chStmt.runPreparedQuery(10000, String.format(SELECT_ADDRESSING, archived ? "_archived" : ""),
               gammaJoin.getQueryId());

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

            if (isNextArtifactGroup(netGammaId, branchId)) {
               writeAddressingChanges(archived, false);
               if (modType == ModificationType.MODIFIED) {
                  addToUpdateAddresssing(ModificationType.NEW, netGammaId, modType, transactionId, obsoleteGammaId);
               } else if (modType.matches(ModificationType.NEW, ModificationType.INTRODUCED, ModificationType.DELETED,
                     ModificationType.MERGED)) {
                  addToUpdateAddresssing(modType, netGammaId, modType, transactionId, obsoleteGammaId);
               } else {
                  throw new OseeStateException("unexpected mod type: " + modType);
               }
            } else {
               if (modType.matches(ModificationType.NEW, ModificationType.INTRODUCED, ModificationType.MODIFIED)) {
                  addressingToDelete.add(new Object[] {transactionId, obsoleteGammaId});
               } else if (modType == ModificationType.DELETED || modType == ModificationType.MERGED) {
                  if (previuosTransactionId != transactionId) { // can't use a gamma (netGammaId) more than once in a transaction
                     addToUpdateAddresssing(modType, netGammaId, modType, transactionId, obsoleteGammaId);
                  }
               } else {
                  throw new OseeStateException("unexpected mod type: " + modType);
               }
            }

            previousNetGammaId = netGammaId;
            previousBranchId = branchId;
            previuosTransactionId = transactionId;
         }
      } finally {
         if (chStmt != null) {
            chStmt.close();
         }
      }

      writeAddressingChanges(archived, true);
   }

   private void addToUpdateAddresssing(ModificationType netModType, long netGammaId, ModificationType modType, int transactionId, long obsoleteGammaId) {
      if (obsoleteGammaId != netGammaId || modType != netModType) {
         updateAddressingData.add(new Object[] {netGammaId, netModType.getValue(), transactionId, obsoleteGammaId});
      }
   }

   private boolean isNextArtifactGroup(long netGammaId, int branchId) {
      return previousNetGammaId != netGammaId || previousBranchId != branchId;
   }

   private String prepareSql(String sql, boolean archived) {
      return String.format(sql, archived ? "_archived" : "");
   }

   private void writeAddressingChanges(boolean archived, boolean force) throws OseeDataStoreException {
      String archivedStr = archived ? "_archived" : "";
      if (addressingToDelete.size() > 99960 || force) {
         deleteTxsCounter +=
               getDatabaseService().runBatchUpdate(connection, prepareSql(DELETE_TXS, archived), addressingToDelete);
         reporter.report("Number of txs" + archivedStr + " rows deleted: " + deleteTxsCounter);
         addressingToDelete.clear();
      }

      if (updateAddressingData.size() > 99960 || force) {
         updateTxsCounter +=
               getDatabaseService().runBatchUpdate(connection, prepareSql(UPDATE_TXS_GAMMAS, archived),
                     updateAddressingData);
         reporter.report("Number of txs" + archivedStr + " rows updated: " + updateTxsCounter);

         updateAddressingData.clear();
      }
   }

   private boolean isNextConceptualArtifact(int artifactId) {
      return previousArtifactId != artifactId;
   }

   private void initNextConceptualArtifact(int artifactId) throws OseeCoreException {
      obsoleteGammas.clear();
      previousArtifactId = artifactId;
      netGamma = chStmt.getInt("gamma_id");
   }
}

Back to the top