Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 210d33518f162e9613c74d24631c1b3bc0b0b528 (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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
 * Copyright (c) 2008-2015 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:
 *    Simon McDuff  - initial API and implementation
 *    Eike Stepper  - maintenance
 *    Cyril Jaquier - Bug 310574 (with the help of Pascal Lehmann)
 */
package org.eclipse.emf.cdo.internal.common.revision.delta;

import org.eclipse.emf.cdo.common.protocol.CDODataInput;
import org.eclipse.emf.cdo.common.protocol.CDODataOutput;
import org.eclipse.emf.cdo.common.revision.CDORevision;
import org.eclipse.emf.cdo.common.revision.delta.CDOAddFeatureDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDOFeatureDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDOFeatureDeltaVisitor;
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.CDOSetFeatureDelta;
import org.eclipse.emf.cdo.spi.common.revision.CDOReferenceAdjuster;
import org.eclipse.emf.cdo.spi.common.revision.InternalCDOFeatureDelta;

import org.eclipse.net4j.util.ObjectUtil;
import org.eclipse.net4j.util.collection.Pair;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.FeatureMapUtil;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

/**
 * @author Simon McDuff
 */
public class CDOListFeatureDeltaImpl extends CDOFeatureDeltaImpl implements CDOListFeatureDelta
{
  private final int originSize;

  private final List<CDOFeatureDelta> listChanges = new ArrayList<CDOFeatureDelta>();

  private transient int[] cachedIndices;

  private transient ListTargetAdding[] cachedSources;

  private transient List<CDOFeatureDelta> unprocessedFeatureDeltas;

  public CDOListFeatureDeltaImpl(EStructuralFeature feature, int originSize)
  {
    super(feature);
    this.originSize = originSize;
  }

  public CDOListFeatureDeltaImpl(CDODataInput in, EClass eClass) throws IOException
  {
    super(in, eClass);
    originSize = in.readInt();
    int size = in.readInt();
    for (int i = 0; i < size; i++)
    {
      listChanges.add(in.readCDOFeatureDelta(eClass));
    }
  }

  public CDOListFeatureDelta copy()
  {
    CDOListFeatureDeltaImpl result = new CDOListFeatureDeltaImpl(getFeature(), getOriginSize());

    Map<CDOFeatureDelta, CDOFeatureDelta> map = null;
    if (cachedSources != null || unprocessedFeatureDeltas != null)
    {
      map = new HashMap<CDOFeatureDelta, CDOFeatureDelta>();
    }

    for (CDOFeatureDelta delta : listChanges)
    {
      CDOFeatureDelta newDelta = delta.copy();
      result.listChanges.add(newDelta);
      if (map != null)
      {
        map.put(delta, newDelta);
      }
    }

    if (cachedIndices != null)
    {
      result.cachedIndices = copyOf(cachedIndices, cachedIndices.length);
    }

    if (cachedSources != null)
    {
      int length = cachedSources.length;
      result.cachedSources = new ListTargetAdding[length];
      for (int i = 0; i < length; i++)
      {
        ListTargetAdding oldElement = cachedSources[i];
        CDOFeatureDelta newElement = map.get(oldElement);
        if (newElement instanceof ListTargetAdding)
        {
          result.cachedSources[i] = (ListTargetAdding)newElement;
        }
      }
    }

    if (unprocessedFeatureDeltas != null)
    {
      int size = unprocessedFeatureDeltas.size();
      result.unprocessedFeatureDeltas = new ArrayList<CDOFeatureDelta>(size);
      for (CDOFeatureDelta oldDelta : unprocessedFeatureDeltas)
      {
        CDOFeatureDelta newDelta = map.get(oldDelta);
        if (newDelta != null)
        {
          result.unprocessedFeatureDeltas.add(newDelta);
        }
      }
    }

    return result;
  }

  @Override
  public void write(CDODataOutput out, EClass eClass) throws IOException
  {
    super.write(out, eClass);
    out.writeInt(originSize);
    out.writeInt(listChanges.size());
    for (CDOFeatureDelta featureDelta : listChanges)
    {
      out.writeCDOFeatureDelta(eClass, featureDelta);
    }
  }

  public Type getType()
  {
    return Type.LIST;
  }

  public int getOriginSize()
  {
    return originSize;
  }

  public List<CDOFeatureDelta> getListChanges()
  {
    return listChanges;
  }

  /**
   * Returns the number of indices as the first element of the array.
   *
   * @return never <code>null</code>.
   */
  public Pair<ListTargetAdding[], int[]> reconstructAddedIndices()
  {
    reconstructAddedIndicesWithNoCopy();
    return Pair.create(copyOf(cachedSources, cachedSources.length, cachedSources.getClass()),
        copyOf(cachedIndices, cachedIndices.length));
  }

  private void reconstructAddedIndicesWithNoCopy()
  {
    // Note that cachedIndices and cachedSources are always either both null or
    // both non-null, and in the latter case, are always of the same length.
    // Furthermore, there can only be unprocessedFeatureDeltas if cachesIndices
    // and cachedSources are non-null.

    if (cachedIndices == null || unprocessedFeatureDeltas != null)
    {
      if (cachedIndices == null)
      {
        int initialCapacity = listChanges.size() + 1;
        cachedIndices = new int[initialCapacity];
        cachedSources = new ListTargetAdding[initialCapacity];
      }
      else
        // i.e. unprocessedFeatureDeltas != null
      {
        int requiredCapacity = 1 + cachedIndices[0] + unprocessedFeatureDeltas.size();
        if (cachedIndices.length < requiredCapacity)
        {
          int newCapacity = Math.max(requiredCapacity, cachedIndices.length * 2);

          int[] newIndices = new int[newCapacity];
          System.arraycopy(cachedIndices, 0, newIndices, 0, cachedIndices.length);
          cachedIndices = newIndices;

          ListTargetAdding[] newSources = new ListTargetAdding[newCapacity];
          System.arraycopy(cachedSources, 0, newSources, 0, cachedSources.length);
          cachedSources = newSources;
        }
      }

      List<CDOFeatureDelta> featureDeltasToBeProcessed = unprocessedFeatureDeltas == null ? listChanges
          : unprocessedFeatureDeltas;
      for (CDOFeatureDelta featureDelta : featureDeltasToBeProcessed)
      {
        if (featureDelta instanceof ListIndexAffecting)
        {
          ListIndexAffecting affecting = (ListIndexAffecting)featureDelta;
          affecting.affectIndices(cachedSources, cachedIndices);
        }

        if (featureDelta instanceof ListTargetAdding)
        {
          cachedIndices[++cachedIndices[0]] = ((ListTargetAdding)featureDelta).getIndex();
          cachedSources[cachedIndices[0]] = (ListTargetAdding)featureDelta;
        }
      }

      unprocessedFeatureDeltas = null;
    }
  }

  private boolean cleanupWithNewDelta(CDOFeatureDelta featureDelta)
  {
    EStructuralFeature feature = getFeature();
    switch (featureDelta.getType())
    {
    case REMOVE:
    {
      if (feature instanceof EReference || FeatureMapUtil.isFeatureMap(feature))
      {
        Boolean result = cleanupWithNewRemoveDelta((CDORemoveFeatureDelta)featureDelta);
        if (result != null)
        {
          return result;
        }
      }

      break;
    }
    case SET:
    {
      Boolean result = cleanupWithNewSetDelta((CDOSetFeatureDelta)featureDelta);
      if (result != null)
      {
        return result;
      }

      break;
    }
    }

    if (cachedIndices != null)
    {
      if (unprocessedFeatureDeltas == null)
      {
        unprocessedFeatureDeltas = new ArrayList<CDOFeatureDelta>();
      }

      unprocessedFeatureDeltas.add(featureDelta);
    }

    return true;
  }

  private Boolean cleanupWithNewRemoveDelta(CDORemoveFeatureDelta removeFeatureDelta)
  {
    int indexToRemove = removeFeatureDelta.getIndex();
    reconstructAddedIndicesWithNoCopy();

    for (int i = 1; i <= cachedIndices[0]; i++)
    {
      int index = cachedIndices[i];
      if (indexToRemove == index)
      {
        // The previous implementation set the value of the feature delta to CDOID.NULL. Databinding and probably
        // others don't really like it. We now remove the ADD (or SET which seems to appear in CDOListFeatureDelta
        // during opposite adjustment!? Why???) and patch the other feature deltas.
        // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=310574

        ListTargetAdding delta = cachedSources[i];

        // We use a "floating" index which is the index (in the list) of the item to remove at the time when the
        // object was still in the list. This index evolves with the feature deltas.
        int floatingIndex = delta.getIndex();

        // First updates cachedSources and cachedIndices using CDORemoveFeatureDelta.
        ListIndexAffecting affecting = (ListIndexAffecting)removeFeatureDelta;
        affecting.affectIndices(cachedSources, cachedIndices);

        // Then adjusts the remaining feature deltas.
        boolean skip = true;
        ListIterator<CDOFeatureDelta> iterator = listChanges.listIterator();

        while (iterator.hasNext())
        {
          CDOFeatureDelta fd = iterator.next();

          // We only need to process feature deltas that come after the ADD (or SET) to be removed.
          if (skip)
          {
            if (fd == delta)
            {
              // Found the ADD (or SET) feature delta that we need to remove. So remove it from the list and start
              // processing the next feature deltas.
              skip = false;
              iterator.remove();

              // SET
              if (fd instanceof CDOSetFeatureDelta)
              {
                // If the removed delta is SET we add the REMOVE to the feature deltas. We do not need to adjust the
                // other feature deltas because SET do not modify the list.
                return true;
              }
            }

            continue;
          }

          // ADD
          if (fd instanceof CDOAddFeatureDelta)
          {
            // Increases the floating index if the ADD came in front of the item.
            if (((CDOAddFeatureDelta)fd).getIndex() <= floatingIndex)
            {
              ++floatingIndex;
            }

            // Adjusts the feature delta too.
            ((WithIndex)fd).adjustAfterRemoval(floatingIndex);
          }

          // REMOVE
          else if (fd instanceof CDORemoveFeatureDelta)
          {
            int idx = floatingIndex;
            // Decreases the floating index if the REMOVE came in front of the item.
            if (((CDORemoveFeatureDelta)fd).getIndex() <= floatingIndex)
            {
              --floatingIndex;
            }

            // Adjusts the feature delta too.
            ((WithIndex)fd).adjustAfterRemoval(idx);
          }

          // MOVE
          else if (fd instanceof CDOMoveFeatureDelta)
          {
            // Remembers the positions before we patch them.
            int from = ((CDOMoveFeatureDelta)fd).getOldPosition();
            int to = ((CDOMoveFeatureDelta)fd).getNewPosition();

            if (floatingIndex == from)
            {
              // We are moving the "to be deleted" item. So we update our floating index and remove the MOVE. It has
              // no effect on the list.
              floatingIndex = to;
              iterator.remove();
            }
            else
            {
              // In the other cases, we need to patch the positions.

              // If the old position is greater or equal to the current position of the item to be removed (remember,
              // that's our floating index), decrease the position.
              int patchedFrom = floatingIndex <= from ? from - 1 : from;

              // The new position requires more care. We need to know the direction of the move (left-to-right or
              // right-to-left).
              int patchedTo;
              if (from > to)
              {
                // left-to-right. Only decreases the position if it is strictly greater than the current item
                // position.
                patchedTo = floatingIndex < to ? to - 1 : to;
              }
              else
              {
                // right-to-left. Decreases the position if it is greater or equal than the current item position.
                patchedTo = floatingIndex <= to ? to - 1 : to;
              }

              // We can now update our floating index. We use the original positions because the floating index
              // represents the item "to be deleted" before it was actually removed.
              if (from < floatingIndex && floatingIndex <= to)
              {
                --floatingIndex;
              }
              else if (to <= floatingIndex && floatingIndex < from)
              {
                ++floatingIndex;
              }

              // And finally adjust the feature delta.
              if (patchedFrom == patchedTo)
              {
                // Source and destination are the same so just remove the feature delta.
                iterator.remove();
              }
              else
              {
                ((CDOMoveFeatureDeltaImpl)fd).setOldPosition(patchedFrom);
                ((CDOMoveFeatureDeltaImpl)fd).setNewPosition(patchedTo);
              }
            }
          }

          // SET
          else if (fd instanceof CDOSetFeatureDelta)
          {
            // Adjusts the feature delta too.
            ((WithIndex)fd).adjustAfterRemoval(floatingIndex);
          }
        }

        // If the removed delta was ADD so we do not add the REMOVE to the feature deltas.
        return false;
      }
    }

    return null;
  }

  /**
   * A new SET feature delta can interfere with former ADD or SET deltas.
   */
  private Boolean cleanupWithNewSetDelta(CDOSetFeatureDelta featureDelta)
  {
    final class DeltaProxy implements InternalCDOFeatureDelta.WithIndex
    {
      private final int indexIntoListChanges;

      private int index;

      public DeltaProxy(int indexIntoListChanges, int index)
      {
        this.indexIntoListChanges = indexIntoListChanges;
        this.index = index;
      }

      public int getIndexIntoListChanges()
      {
        return indexIntoListChanges;
      }

      public int getIndex()
      {
        return index;
      }

      public void adjustAfterAddition(int index)
      {
        if (index <= this.index)
        {
          ++this.index;
        }
      }

      public void adjustAfterRemoval(int index)
      {
        if (index < this.index && this.index > 0)
        {
          --this.index;
        }
      }
    }

    int size = listChanges.size();
    List<DeltaProxy> proxies = new ArrayList<DeltaProxy>();

    for (int i = 0; i < size; i++)
    {
      CDOFeatureDelta fd = listChanges.get(i);
      switch (fd.getType())
      {
      case MOVE:
      {
        int oldPosition = ((CDOMoveFeatureDelta)fd).getOldPosition();
        int newPosition = ((CDOMoveFeatureDelta)fd).getNewPosition();
        for (DeltaProxy proxy : proxies)
        {
          proxy.adjustAfterRemoval(oldPosition);
          proxy.adjustAfterAddition(newPosition);
        }

        break;
      }

      case REMOVE:
      {
        int index = ((CDORemoveFeatureDelta)fd).getIndex();
        for (DeltaProxy proxy : proxies)
        {
          proxy.adjustAfterRemoval(index);
        }

        break;
      }

      case ADD:
      {
        int index = ((CDOAddFeatureDelta)fd).getIndex();
        for (DeltaProxy proxy : proxies)
        {
          proxy.adjustAfterAddition(index);
        }

        proxies.add(new DeltaProxy(i, index));
        break;
      }

      case SET:
      {
        int index = ((CDOSetFeatureDelta)fd).getIndex();
        proxies.add(new DeltaProxy(i, index));
        break;
      }
      }
    }

    int index = featureDelta.getIndex();
    for (DeltaProxy proxy : proxies)
    {
      if (proxy.getIndex() == index)
      {
        int indexIntoListChanges = proxy.getIndexIntoListChanges();
        CDOFeatureDelta featureDeltaToModify = listChanges.get(indexIntoListChanges);
        if (featureDeltaToModify.getType() == CDOFeatureDelta.Type.ADD)
        {
          ((CDOAddFeatureDeltaImpl)featureDeltaToModify).setValue(featureDelta.getValue());
          return false;
        }

        // Here featureDeltaToModify is a SET delta
        listChanges.remove(indexIntoListChanges); // Replace the SET delta that existed for this index
        break;
      }
    }

    return null;
  }

  public void add(CDOFeatureDelta featureDelta)
  {
    // Only adds the feature delta to the list if required.
    if (cleanupWithNewDelta(featureDelta))
    {
      listChanges.add(featureDelta);
    }
  }

  public Object applyTo(CDORevision revision)
  {
    for (CDOFeatureDelta featureDelta : listChanges)
    {
      ((CDOFeatureDeltaImpl)featureDelta).applyTo(revision);
    }

    return null;
  }

  @Override
  public boolean adjustReferences(CDOReferenceAdjuster adjuster)
  {
    boolean changed = false;
    for (CDOFeatureDelta featureDelta : listChanges)
    {
      changed |= ((CDOFeatureDeltaImpl)featureDelta).adjustReferences(adjuster);
    }

    return changed;
  }

  public void accept(CDOFeatureDeltaVisitor visitor)
  {
    visitor.visit(this);
  }

  @Override
  public boolean isStructurallyEqual(Object obj)
  {
    if (!super.isStructurallyEqual(obj))
    {
      return false;
    }

    CDOListFeatureDelta that = (CDOListFeatureDelta)obj;
    return ObjectUtil.equals(listChanges, that.getListChanges());
  }

  @Override
  protected String toStringAdditional()
  {
    return "originSize=" + originSize + ", list=" + listChanges; //$NON-NLS-1$
  }

  /**
   * Copied from JAVA 1.6 {@link Arrays Arrays.copyOf}.
   */
  private static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType)
  {
    @SuppressWarnings("unchecked")
    T[] copy = (Object)newType == (Object)Object[].class ? (T[])new Object[newLength]
        : (T[])Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;
  }

  /**
   * Copied from JAVA 1.6 {@link Arrays Arrays.copyOf}.
   */
  private static int[] copyOf(int[] original, int newLength)
  {
    int[] copy = new int[newLength];
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;
  }
}

Back to the top