Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ea84cdfb0cb995bc4e2861c5b98aba48bb6ed846 (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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/**
 * Copyright (c) 2004 - 2011 Eike Stepper (Berlin, Germany) and others.
 * 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:
 *    Caspar De Groot - initial API and implementation
 */
package org.eclipse.emf.internal.cdo.util;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.CDOState;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.id.CDOIDTemp;
import org.eclipse.emf.cdo.common.id.CDOIDUtil;
import org.eclipse.emf.cdo.common.model.EMFUtil;
import org.eclipse.emf.cdo.common.revision.delta.CDOAddFeatureDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDOClearFeatureDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDOContainerFeatureDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDOFeatureDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDOListFeatureDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDOMoveFeatureDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDORemoveFeatureDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDORevisionDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDOSetFeatureDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDOUnsetFeatureDelta;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevision;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.util.CommitIntegrityException;

import org.eclipse.net4j.util.CheckUtil;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject.EStore;
import org.eclipse.emf.spi.cdo.InternalCDOObject;
import org.eclipse.emf.spi.cdo.InternalCDOTransaction;
import org.eclipse.emf.spi.cdo.InternalCDOTransaction.InternalCDOCommitContext;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Caspar De Groot
 * @since 4.0
 */
public class CommitIntegrityCheck
{
  private InternalCDOTransaction transaction;

  private Style style;

  private Set<CDOID> newIDs, dirtyIDs, detachedIDs;

  private Set<CDOObject> missingObjects = new HashSet<CDOObject>();

  private StringBuilder exceptionMessage = new StringBuilder();

  public CommitIntegrityCheck(InternalCDOCommitContext commitContext)
  {
    this(commitContext, Style.EXCEPTION_FAST);
  }

  public CommitIntegrityCheck(InternalCDOCommitContext commitContext, Style style)
  {
    transaction = commitContext.getTransaction();

    CheckUtil.checkArg(style, "style");
    this.style = style;

    newIDs = commitContext.getNewObjects().keySet();
    dirtyIDs = commitContext.getDirtyObjects().keySet();
    detachedIDs = commitContext.getDetachedObjects().keySet();
  }

  public void check() throws CommitIntegrityException
  {
    // For new objects: ensure that their container is included,
    // as well as the targets of the new object's bidi references
    for (CDOID newID : newIDs)
    {
      CDOObject newObject = transaction.getObject(newID);
      checkContainerIncluded(newObject, "new");
      checkCurrentRefTargetsIncluded(newObject, "new");
    }

    // For detached objects: ensure that their former container is included,
    // as well as the targets of the detached object's bidi references
    for (CDOID detachedID : detachedIDs)
    {
      CDOObject detachedObject = transaction.getObject(detachedID);
      checkFormerContainerIncluded(detachedObject);
      checkFormerBidiRefTargetsIncluded(detachedObject, "detached");
    }

    // For dirty objects: if any of the deltas for the object, affect containment (i.e. object was moved)
    // or a bi-di reference, ensure that for containment, both the old and new containers are included,
    // (or that the child is included if we are considering the dirty parent),
    // and that for a bi-di reference, the object holding the other end of the bi-di is included,
    // as well as possibly the *former* object holding the other end.
    for (CDOID dirtyID : dirtyIDs)
    {
      CDOObject dirtyObject = transaction.getObject(dirtyID);
      analyzeRevisionDelta((InternalCDOObject)dirtyObject);
    }

    if (!missingObjects.isEmpty() && style == Style.EXCEPTION)
    {
      throw createException();
    }
  }

  public Set<? extends EObject> getMissingObjects()
  {
    return missingObjects;
  }

  private CDOID getContainerOrResourceID(InternalCDORevision revision)
  {
    CDOID containerOrResourceID = null;
    Object idOrObject = revision.getContainerID();
    if (idOrObject != null)
    {
      containerOrResourceID = (CDOID)transaction.convertObjectToID(idOrObject);
    }

    if (CDOIDUtil.isNull(containerOrResourceID))
    {
      idOrObject = revision.getResourceID();
      if (idOrObject != null)
      {
        containerOrResourceID = (CDOID)transaction.convertObjectToID(idOrObject);
      }
    }

    return containerOrResourceID;
  }

  private void analyzeRevisionDelta(InternalCDOObject dirtyObject) throws CommitIntegrityException
  {
    // Getting the deltas from the TX is not a good idea...
    // We better recompute a fresh delta:
    InternalCDORevision cleanRev = transaction.getCleanRevisions().get(dirtyObject);
    CheckUtil.checkNull(cleanRev, "Could not obtain clean revision for dirty object " + dirtyObject);

    InternalCDORevision dirtyRev = dirtyObject.cdoRevision();
    CDORevisionDelta rDelta = dirtyRev.compare(cleanRev);

    for (CDOFeatureDelta featureDelta : rDelta.getFeatureDeltas())
    {
      EStructuralFeature feat = featureDelta.getFeature();
      if (feat == CDOContainerFeatureDelta.CONTAINER_FEATURE)
      {
        // Three possibilities here:
        // 1. Object's container has changed
        // 2. Object's containment feature has changed
        // 3. Object's resource has changed
        // (or several of the above)

        // @1
        CDOID currentContainerID = (CDOID)transaction.convertObjectToID(dirtyRev.getContainerID());
        CDOID cleanContainerID = (CDOID)transaction.convertObjectToID(cleanRev.getContainerID());
        if (!CDOIDUtil.equals(currentContainerID, cleanContainerID))
        {
          if (currentContainerID != CDOID.NULL)
          {
            checkIncluded(currentContainerID, "container of moved", dirtyObject);
          }

          if (cleanContainerID != CDOID.NULL)
          {
            checkIncluded(cleanContainerID, "former container of moved", dirtyObject);
          }
        }

        // @2
        // Nothing to be done. (I think...)

        // @3
        CDOID currentResourceID = (CDOID)transaction.convertObjectToID(dirtyRev.getResourceID());
        CDOID cleanResourceID = (CDOID)transaction.convertObjectToID(cleanRev.getResourceID());
        if (!CDOIDUtil.equals(currentResourceID, cleanResourceID))
        {
          if (currentResourceID != CDOID.NULL)
          {
            checkIncluded(currentResourceID, "resource of moved", dirtyObject);
          }

          if (cleanResourceID != CDOID.NULL)
          {
            checkIncluded(cleanResourceID, "former resource of moved", dirtyObject);
          }
        }
      }
      else if (feat instanceof EReference)
      {
        if (featureDelta instanceof CDOListFeatureDelta)
        {
          for (CDOFeatureDelta innerFeatDelta : ((CDOListFeatureDelta)featureDelta).getListChanges())
          {
            checkFeatureDelta(innerFeatDelta, dirtyObject);
          }
        }
        else
        {
          checkFeatureDelta(featureDelta, dirtyObject);
        }
      }
    }
  }

  private void checkIncluded(Object idOrObject, String msg, CDOObject o) throws CommitIntegrityException
  {
    idOrObject = transaction.convertObjectToID(idOrObject);
    if (idOrObject instanceof CDOID)
    {
      CDOID id = (CDOID)idOrObject;
      if (!id.isNull())
      {
        checkIncluded(id, msg, o);
      }
    }

    // else: Transient object -- ignore
  }

  private void checkFeatureDelta(CDOFeatureDelta featureDelta, CDOObject dirtyObject) throws CommitIntegrityException
  {
    EReference ref = (EReference)featureDelta.getFeature();
    boolean containmentOrWithOpposite = ref.isContainment() || hasPersistentOpposite(ref);

    if (featureDelta instanceof CDOAddFeatureDelta)
    {
      Object idOrObject = ((CDOAddFeatureDelta)featureDelta).getValue();
      if (containmentOrWithOpposite || isNew(idOrObject))
      {
        checkIncluded(idOrObject, "added child / refTarget of", dirtyObject);
      }
    }
    else if (featureDelta instanceof CDOSetFeatureDelta)
    {
      Object oldIDOrObject = ((CDOSetFeatureDelta)featureDelta).getOldValue();
      CDOID oldID = (CDOID)transaction.convertObjectToID(oldIDOrObject);
      if (!CDOIDUtil.isNull(oldID))
      {
        // Old child must be included if it's the container or has an eOpposite
        if (containmentOrWithOpposite)
        {
          checkIncluded(oldID, "removed / former child / refTarget of", dirtyObject);
        }
      }

      Object newIDOrObject = ((CDOSetFeatureDelta)featureDelta).getValue();
      if (newIDOrObject != null)
      {
        // New child must be included
        newIDOrObject = transaction.convertObjectToID(newIDOrObject);
        if (containmentOrWithOpposite || isNew(newIDOrObject))
        {
          checkIncluded(newIDOrObject, "new child / refTarget of", dirtyObject);
        }
      }
    }
    else if (containmentOrWithOpposite)
    {
      if (featureDelta instanceof CDORemoveFeatureDelta)
      {
        Object idOrObject = ((CDORemoveFeatureDelta)featureDelta).getValue();
        CDOID id = (CDOID)transaction.convertObjectToID(idOrObject);
        checkIncluded(id, "removed child / refTarget of", dirtyObject);
      }
      else if (featureDelta instanceof CDOClearFeatureDelta)
      {
        EStructuralFeature feat = ((CDOClearFeatureDelta)featureDelta).getFeature();
        InternalCDORevision cleanRev = transaction.getCleanRevisions().get(dirtyObject);
        int n = cleanRev.size(feat);
        for (int i = 0; i < n; i++)
        {
          Object idOrObject = cleanRev.get(feat, i);
          CDOID id = (CDOID)transaction.convertObjectToID(idOrObject);
          checkIncluded(id, "removed child / refTarget of", dirtyObject);
        }
      }
      else if (featureDelta instanceof CDOUnsetFeatureDelta)
      {
        EStructuralFeature feat = ((CDOUnsetFeatureDelta)featureDelta).getFeature();
        InternalCDORevision cleanRev = transaction.getCleanRevisions().get(dirtyObject);
        Object idOrObject = cleanRev.getValue(feat);
        CDOID id = (CDOID)transaction.convertObjectToID(idOrObject);
        checkIncluded(id, "removed child / refTarget of", dirtyObject);
      }
      else if (featureDelta instanceof CDOMoveFeatureDelta)
      {
        // Nothing to do: a move doesn't affect the child being moved
        // so that child does not need to be included
      }
      else
      {
        throw new IllegalArgumentException("Unexpected delta type: " + featureDelta.getClass().getSimpleName());
      }
    }
  }

  private boolean isNew(Object idOrObject)
  {
    if (idOrObject instanceof CDOIDTemp)
    {
      return true;
    }
    if (idOrObject instanceof EObject)
    {
      CDOObject obj = CDOUtil.getCDOObject((EObject)idOrObject);
      return obj.cdoState() == CDOState.NEW;
    }
    return false;
  }

  private void checkIncluded(CDOID id, String msg, CDOObject o) throws CommitIntegrityException
  {
    if (id.isNull())
    {
      throw new IllegalArgumentException("CDOID must not be NULL");
    }

    if (!dirtyIDs.contains(id) && !detachedIDs.contains(id) && !newIDs.contains(id))
    {
      CDOObject missingObject = transaction.getObject(id);
      if (missingObject == null)
      {
        throw new IllegalStateException("Could not find object for CDOID " + id);
      }

      missingObjects.add(missingObject);

      if (exceptionMessage.length() > 0)
      {
        exceptionMessage.append('\n');
      }

      String m = String.format("The %s object %s needs to be included in the commit but isn't", msg, o);
      exceptionMessage.append(m);

      if (style == Style.EXCEPTION_FAST)
      {
        throw createException();
      }
    }
  }

  private CommitIntegrityException createException()
  {
    return new CommitIntegrityException(exceptionMessage.toString(), missingObjects);
  }

  /**
   * Checks whether the container of a given object is included in the commit
   */
  private void checkContainerIncluded(CDOObject object, String msgFrag) throws CommitIntegrityException
  {
    EObject eContainer = object.eContainer();
    if (eContainer == null)
    {
      // It's a top-level object
      CDOResource resource = object.cdoDirectResource();
      checkIncluded(resource.cdoID(), "resource of " + msgFrag, object);
    }
    else
    {
      CDOObject container = CDOUtil.getCDOObject(eContainer);
      checkIncluded(container.cdoID(), "container of " + msgFrag, object);
    }
  }

  private void checkCurrentRefTargetsIncluded(CDOObject referencer, String msgFrag) throws CommitIntegrityException
  {
    for (EReference eRef : referencer.eClass().getEAllReferences())
    {
      if (EMFUtil.isPersistent(eRef))
      {
        if (eRef.isMany())
        {
          EList<?> list = (EList<?>)referencer.eGet(eRef);
          for (Object refTarget : list)
          {
            checkBidiRefTargetOrNewNonBidiTargetIncluded(referencer, eRef, refTarget, msgFrag);
          }
        }
        else
        {
          Object refTarget = referencer.eGet(eRef);
          if (refTarget != null)
          {
            checkBidiRefTargetOrNewNonBidiTargetIncluded(referencer, eRef, refTarget, msgFrag);
          }
        }
      }
    }
  }

  private void checkBidiRefTargetOrNewNonBidiTargetIncluded(CDOObject referencer, EReference eRef, Object refTarget,
      String msgFrag) throws CommitIntegrityException
  {
    if (hasPersistentOpposite(eRef))
    {
      // It's a bi-di ref; the target must definitely be included
      checkBidiRefTargetIncluded(refTarget, referencer, eRef.getName(), msgFrag);
    }
    else if (isNew(refTarget))
    {
      // It's a non-bidi ref; the target doesn't have to be included unless it's NEW
      checkIncluded(refTarget, "target of reference '" + eRef.getName() + "' of " + msgFrag, referencer);
    }
  }

  private void checkFormerBidiRefTargetsIncluded(CDOObject referencer, String msgFrag) throws CommitIntegrityException
  {
    // The referencer argument should really be a detached object, and so we know
    // that we can find the pre-detach revision in tx.getFormerRevisions(). However,
    // the object may have already been dirty prior to detachment, so we check the
    // clean revisions first.
    InternalCDORevision cleanRev = transaction.getCleanRevisions().get(referencer);
    CheckUtil.checkState(cleanRev, "cleanRev");

    for (EReference eRef : referencer.eClass().getEAllReferences())
    {
      if (EMFUtil.isPersistent(eRef) && hasPersistentOpposite(eRef))
      {
        Object value = cleanRev.get(eRef, EStore.NO_INDEX);
        if (value != null)
        {
          if (eRef.isMany())
          {
            EList<?> list = (EList<?>)value;
            for (Object element : list)
            {
              checkBidiRefTargetIncluded(element, referencer, eRef.getName(), msgFrag);
            }
          }
          else
          {
            checkBidiRefTargetIncluded(value, referencer, eRef.getName(), msgFrag);
          }
        }
      }
    }
  }

  private void checkBidiRefTargetIncluded(Object refTarget, CDOObject referencer, String refName, String msgFrag)
      throws CommitIntegrityException
  {
    CheckUtil.checkArg(refTarget, "refTarget");
    CDOID refTargetID = null;
    if (refTarget instanceof EObject)
    {
      refTargetID = CDOUtil.getCDOObject((EObject)refTarget).cdoID();
      if (refTargetID == null)
      {
        // No ID, means object is TRANSIENT; ignore.
        return;
      }
    }
    else if (refTarget instanceof CDOID)
    {
      refTargetID = (CDOID)refTarget;
    }

    checkIncluded(refTargetID, "target of reference '" + refName + "' of " + msgFrag, referencer);
  }

  private void checkFormerContainerIncluded(CDOObject detachedObject) throws CommitIntegrityException
  {
    InternalCDORevision rev = transaction.getCleanRevisions().get(detachedObject);
    CheckUtil.checkNull(rev, "Could not obtain clean revision for detached object " + detachedObject);

    CDOID id = getContainerOrResourceID(rev);
    checkIncluded(id, "former container (or resource) of detached", detachedObject);
  }

  private static boolean hasPersistentOpposite(EReference ref)
  {
    EReference eOpposite = ref.getEOpposite();
    return eOpposite != null && EMFUtil.isPersistent(eOpposite);
  }

  /**
   * Designates an exception style for a {@link CommitIntegrityCheck}
   * 
   * @author Caspar De Groot
   */
  public static enum Style
  {
    /**
     * Throw an exception as soon as this {@link CommitIntegrityCheck} encounters the first problem
     */
    EXCEPTION_FAST,

    /**
     * Throw an exception when this {@link CommitIntegrityCheck} finishes performing all possible checks, in case any
     * problems were found
     */
    EXCEPTION,

    /**
     * Do not throw an exception. Caller must invoke {@link CommitIntegrityCheck#getMissingObjects()} to find out if the
     * check discovered any problems.
     */
    NO_EXCEPTION
  }
}

Back to the top