Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d25c415a7ed4ec3f682382cfea7b4cee4d9ab1f4 (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
/*
 * Copyright (c) 2012, 2013, 2015-2017, 2019-2022 Eike Stepper (Loehne, 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.compare;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.CDOState;
import org.eclipse.emf.cdo.common.branch.CDOBranchPoint;
import org.eclipse.emf.cdo.common.commit.CDOChangeSetData;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.id.CDOIDUtil;
import org.eclipse.emf.cdo.common.revision.CDOIDAndVersion;
import org.eclipse.emf.cdo.common.revision.CDORevisionData;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.eresource.CDOResourceNode;
import org.eclipse.emf.cdo.spi.common.branch.CDOBranchUtil;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.util.ObjectNotFoundException;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.cdo.view.CDOViewOpener;

import org.eclipse.net4j.util.om.OMPlatform;

import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.util.AbstractTreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.compare.Match;
import org.eclipse.emf.compare.scope.AbstractComparisonScope;
import org.eclipse.emf.compare.scope.IComparisonScope;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.EcoreUtil.ProperContentIterator;
import org.eclipse.emf.spi.cdo.InternalCDOSession;
import org.eclipse.emf.spi.cdo.InternalCDOSession.MergeData;
import org.eclipse.emf.spi.cdo.InternalCDOView;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterators;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * A CDO-specific base implementation of a {@link IComparisonScope comparison scope}.
 *
 * @author Eike Stepper
 */
public abstract class CDOComparisonScope extends AbstractComparisonScope
{
  private static final CDOViewOpener DEFAULT_VIEW_OPENER = CDOCompareUtil.DEFAULT_VIEW_OPENER;

  private static final boolean COLLECT_RESOURCE_URIS = OMPlatform.INSTANCE.isProperty("org.eclipse.emf.cdo.compare.CDOComparisonScope.COLLECT_RESOURCE_URIS");

  private boolean resolveProxies = true;

  public CDOComparisonScope(Notifier left, Notifier right, Notifier origin)
  {
    super(left, right, origin);
  }

  @Override
  public Iterator<? extends Resource> getCoveredResources(ResourceSet resourceSet)
  {
    return Collections.<Resource> emptyList().iterator();
  }

  @Override
  public Iterator<? extends EObject> getCoveredEObjects(Resource resource)
  {
    return Collections.<EObject> emptyList().iterator();
  }

  public final boolean isResolveProxies()
  {
    return resolveProxies;
  }

  public final void setResolveProxies(boolean resolveProxies)
  {
    this.resolveProxies = resolveProxies;
  }

  private static CDOView openOriginView(CDOView leftView, CDOView rightView, CDOView[] originView, CDOViewOpener viewOpener)
  {
    if (leftView.getSession() != rightView.getSession())
    {
      throw new IllegalArgumentException("Sessions are different");
    }

    if (originView != null)
    {
      if (originView[0] != null)
      {
        throw new IllegalArgumentException("originView[0] != null");
      }

      CDOBranchPoint ancestor = CDOBranchUtil.getAncestor(leftView, rightView);
      if (!ancestor.equals(leftView) && !ancestor.equals(rightView))
      {
        if (viewOpener == DEFAULT_VIEW_OPENER)
        {
          viewOpener = leftView.getSession();
        }

        originView[0] = viewOpener.openView(ancestor, new ResourceSetImpl());
        return originView[0];
      }
    }

    return null;
  }

  /**
   * Takes an arbitrary {@link CDOObject object} (including {@link CDOResourceNode resource nodes})
   * and returns {@link Match matches} for <b>all</b> elements of its {@link EObject#eAllContents() content tree}. This scope has the advantage that the comparison can
   * be rooted at specific objects that are different from (below of) the root resource. The disadvantage is that all the transitive children of this specific object are
   * matched, whether they differ or not. Major parts of huge repositories can be loaded to the client side easily, if no attention is paid.
   * The following method returns comparisons that are based on this scope algorithm:
   * <ul>
   * <li>{@link CDOCompareUtil#compare(CDOObject, CDOView, CDOView[])}
   * </ul>
   *
   * @author Eike Stepper
   */
  public static class AllContents extends CDOComparisonScope
  {
    public AllContents(Notifier left, Notifier right, Notifier origin)
    {
      super(left, right, origin);
    }

    @Override
    public Iterator<? extends EObject> getChildren(EObject eObject)
    {
      return EcoreUtil.getAllProperContents(eObject, isResolveProxies());
    }

    /**
     * Takes an arbitrary {@link CDOObject object} (including {@link CDOResourceNode resource nodes}) and returns {@link Match matches} for <b>all</b> elements of its {@link EObject#eAllContents() content tree}. This scope has the advantage that the comparison can
     * be rooted at specific objects that are different from (below of) the root resource. The disadvantage is that all the transitive children of this specific object are
     * matched, whether they differ or not. Major parts of huge repositories can be loaded to the client side easily, if no attention is paid.
     */
    public static AllContents create(CDOObject left, CDOView rightView, CDOView[] originView)
    {
      return create(left, rightView, originView, DEFAULT_VIEW_OPENER);
    }

    /**
     * Takes an arbitrary {@link CDOObject object} (including {@link CDOResourceNode resource nodes}) and returns {@link Match matches} for <b>all</b> elements of its {@link EObject#eAllContents() content tree}. This scope has the advantage that the comparison can
     * be rooted at specific objects that are different from (below of) the root resource. The disadvantage is that all the transitive children of this specific object are
     * matched, whether they differ or not. Major parts of huge repositories can be loaded to the client side easily, if no attention is paid.
     *
     * @since 4.3
     */
    public static AllContents create(CDOObject left, CDOView rightView, CDOView[] originView, CDOViewOpener viewOpener)
    {
      CDOView leftView = left.cdoView();
      CDOView view = openOriginView(leftView, rightView, originView, viewOpener);

      CDOObject right = CDOUtil.getCDOObject(rightView.getObject(left));
      CDOObject origin = view == null ? null : CDOUtil.getCDOObject(view.getObject(left));

      return new CDOComparisonScope.AllContents(left, right, origin);
    }
  }

  /**
   * Takes a {@link CDOView view}/{@link CDOTransaction transaction}
   * and returns {@link Match matches} only for the <b>changed</b> elements of the entire content tree of its {@link CDOView#getRootResource() root resource}.
   * The advantage of this scope is that CDO-specific mechanisms are used to efficiently (remotely) determine the set of changed objects. Only those and their container
   * objects are considered as matches, making this scope scale seamlessly with the overall size of a repository.
   * The following method returns comparisons that are based on this scope algorithm:
   * <ul>
   * <li>{@link CDOCompareUtil#compare(CDOView, CDOView, CDOView[])}
   * </ul>
   *
   * @author Eike Stepper
   */
  public static class Minimal extends CDOComparisonScope implements Predicate<EObject>
  {
    private Set<CDOID> ids;

    public Minimal(CDOView leftView, CDOView rightView, CDOView originView, Set<CDOID> ids)
    {
      super(getRoot(leftView), getRoot(rightView), getRoot(originView));
      this.ids = ids;

      Set<CDOID> requiredParentIDs = new HashSet<>();
      for (CDOID id : ids)
      {
        collectRequiredParentID(leftView, id, requiredParentIDs);
        collectRequiredParentID(rightView, id, requiredParentIDs);
        if (originView != null)
        {
          collectRequiredParentID(originView, id, requiredParentIDs);
        }
      }

      ids.addAll(requiredParentIDs);

      Set<String> nsURIs = getNsURIs();
      for (CDOID id : ids)
      {
        String nsURI = getNsURI(leftView, id);
        if (nsURI == null)
        {
          nsURI = getNsURI(rightView, id);
          if (nsURI == null)
          {
            nsURI = getNsURI(originView, id);
          }
        }

        if (nsURI != null)
        {
          nsURIs.add(nsURI);
        }
      }

      CDOResource rootResource = (CDOResource)getLeft();
      ids.remove(rootResource.cdoID());
    }

    @Override
    public Iterator<? extends EObject> getChildren(EObject eObject)
    {
      return new AbstractTreeIterator<EObject>(eObject, false)
      {
        private static final long serialVersionUID = 1L;

        @Override
        public Iterator<EObject> getChildren(Object object)
        {
          if (object instanceof Resource)
          {
            Iterator<EObject> iterator = ((Resource)object).getContents().iterator();
            return Iterators.filter(iterator, Minimal.this);
          }

          Iterator<EObject> iterator = new ProperContentIterator<>((EObject)object, isResolveProxies());
          return Iterators.filter(iterator, Minimal.this);
        }
      };
    }

    @Override
    public boolean apply(EObject input)
    {
      CDOObject object = CDOUtil.getCDOObject(input);
      CDOID id = object.cdoID();
      return ids.contains(id);
    }

    private void collectRequiredParentIDs(CDOObject object, Set<CDOID> requiredParentIDs)
    {
      CDOState state = object.cdoState();
      if (state == CDOState.TRANSIENT)
      {
        return;
      }

      CDOView view = object.cdoView();
      if (state == CDOState.PROXY)
      {
        CDOUtil.load(object, view);
      }

      CDORevisionData revisionData = object.cdoRevision().data();

      CDOID resourceID = revisionData.getResourceID();
      if (!CDOIDUtil.isNull(resourceID))
      {
        collectRequiredParentIDs(view, resourceID, requiredParentIDs);
      }
      else
      {
        CDOID containerID;

        Object containerOrID = revisionData.getContainerID();
        if (containerOrID instanceof EObject)
        {
          containerID = (CDOID)((InternalCDOView)object.cdoView()).convertObjectToID(containerOrID);
        }
        else
        {
          containerID = (CDOID)containerOrID;
        }

        collectRequiredParentIDs(view, containerID, requiredParentIDs);
      }
    }

    private void collectRequiredParentIDs(CDOView view, CDOID id, Set<CDOID> requiredParentIDs)
    {
      if (!CDOIDUtil.isNull(id))
      {
        if (!ids.contains(id) && requiredParentIDs.add(id))
        {
          collectRequiredParentID(view, id, requiredParentIDs);
        }
      }
    }

    protected void collectRequiredParentID(CDOView view, CDOID id, Set<CDOID> requiredParentIDs)
    {
      try
      {
        CDOObject object = view.getObject(id);
        if (object != null)
        {
          if (collectResourceURIs() && object instanceof Resource)
          {
            URI resourceURI = ((Resource)object).getURI();
            if (resourceURI != null)
            {
              Set<String> resourceURIs = getResourceURIs();

              URIConverter uriConverter = view.getResourceSet().getURIConverter();
              resourceURIs.add(uriConverter.normalize(resourceURI).toString());
            }
          }

          collectRequiredParentIDs(object, requiredParentIDs);
        }
      }
      catch (ObjectNotFoundException ex)
      {
        //$FALL-THROUGH$
      }
    }

    /**
     * @since 4.6
     */
    protected boolean collectResourceURIs()
    {
      return COLLECT_RESOURCE_URIS;
    }

    public static IComparisonScope create(CDOView leftView, CDOView rightView, CDOView[] originView)
    {
      return create(leftView, rightView, originView, DEFAULT_VIEW_OPENER);
    }

    /**
     * @since 4.3
     */
    public static IComparisonScope create(CDOView leftView, CDOView rightView, CDOView[] originView, CDOViewOpener viewOpener)
    {
      CDOView view = openOriginView(leftView, rightView, originView, viewOpener);

      Set<CDOID> ids = getAffectedIDs(leftView, rightView, view);
      addDirtyIDs(ids, leftView);
      addDirtyIDs(ids, rightView);

      return new CDOComparisonScope.Minimal(leftView, rightView, view, ids);
    }

    public static IComparisonScope create(CDOView leftView, CDOView rightView, CDOView[] originView, Set<CDOID> ids)
    {
      return create(leftView, rightView, originView, ids, DEFAULT_VIEW_OPENER);
    }

    /**
     * @since 4.3
     */
    public static IComparisonScope create(CDOView leftView, CDOView rightView, CDOView[] originView, Set<CDOID> ids, CDOViewOpener viewOpener)
    {
      CDOView view = openOriginView(leftView, rightView, originView, viewOpener);
      return new CDOComparisonScope.Minimal(leftView, rightView, view, ids);
    }

    public static IComparisonScope create(CDOTransaction transaction)
    {
      return create(transaction, DEFAULT_VIEW_OPENER);
    }

    /**
     * @since 4.3
     */
    public static IComparisonScope create(CDOTransaction transaction, CDOViewOpener viewOpener)
    {
      if (viewOpener == null)
      {
        viewOpener = transaction.getSession();
      }

      CDOBranchPoint lastUpdate = transaction.getBranch().getPoint(transaction.getLastUpdateTime());
      CDOView lastView = viewOpener.openView(lastUpdate, new ResourceSetImpl());

      Set<CDOID> ids = new HashSet<>();
      ids.addAll(transaction.getNewObjects().keySet());
      ids.addAll(transaction.getDirtyObjects().keySet());
      ids.addAll(transaction.getDetachedObjects().keySet());

      return new CDOComparisonScope.Minimal(transaction, lastView, null, ids);
    }

    private static Set<CDOID> getAffectedIDs(CDOView leftView, CDOView rightView, CDOView originView)
    {
      if (originView != null)
      {
        InternalCDOSession session = (InternalCDOSession)leftView.getSession();
        MergeData mergeData = session.getMergeData(leftView, rightView, originView, false);
        return mergeData.getIDs();
      }

      CDOChangeSetData changeSetData = leftView.compareRevisions(rightView);
      return new HashSet<>(changeSetData.getChangeKinds().keySet());
    }

    private static void addDirtyIDs(Set<CDOID> ids, CDOView view)
    {
      if (view instanceof CDOTransaction)
      {
        CDOChangeSetData changeSetData = ((CDOTransaction)view).getChangeSetData();
        addDirtyIDs(ids, changeSetData.getNewObjects());
        addDirtyIDs(ids, changeSetData.getChangedObjects());
        addDirtyIDs(ids, changeSetData.getDetachedObjects());
      }
    }

    private static void addDirtyIDs(Set<CDOID> ids, List<? extends CDOIDAndVersion> keys)
    {
      for (CDOIDAndVersion key : keys)
      {
        ids.add(key.getID());
      }
    }

    private static CDOResource getRoot(CDOView view)
    {
      if (view == null)
      {
        return null;
      }

      return view.getRootResource();
    }

    private static String getNsURI(CDOView view, CDOID id)
    {
      if (view != null)
      {
        try
        {
          CDOObject object = view.getObject(id);
          if (object != null)
          {
            return object.eClass().getEPackage().getNsURI();
          }
        }
        catch (ObjectNotFoundException ex)
        {
          //$FALL-THROUGH$
        }
      }

      return null;
    }
  }
}

Back to the top