Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1313dace861c1353d000a1a9b08929fb1595d8d0 (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
/*
 * Copyright (c) 2012, 2016, 2019 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.server.internal.lissome.optimizer;

import org.eclipse.emf.cdo.common.branch.CDOBranch;
import org.eclipse.emf.cdo.common.branch.CDOBranchPoint;
import org.eclipse.emf.cdo.common.branch.CDOBranchVersion;
import org.eclipse.emf.cdo.common.commit.CDOCommitInfo;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.revision.CDORevision;
import org.eclipse.emf.cdo.common.revision.CDORevisionCacheAdder;
import org.eclipse.emf.cdo.server.internal.lissome.LissomeStore;
import org.eclipse.emf.cdo.server.internal.lissome.db.IndexWriter;
import org.eclipse.emf.cdo.server.internal.lissome.file.Vob;
import org.eclipse.emf.cdo.spi.common.commit.InternalCDOCommitInfoManager;
import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageUnit;
import org.eclipse.emf.cdo.spi.common.revision.DetachedCDORevision;
import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevision;
import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevisionDelta;
import org.eclipse.emf.cdo.spi.server.InternalCommitContext;

import org.eclipse.emf.ecore.EClass;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Eike Stepper
 */
public class CommitTransactionTask implements OptimizerTask
{
  protected final CDOBranchPoint branchPoint;

  protected final long previousTimeStamp;

  protected final String userID;

  protected final String commitComment;

  protected final InternalCDOPackageUnit[] newPackageUnits;

  protected final InternalCDORevision[] newObjects;

  protected final InternalCDORevisionDelta[] dirtyObjectDeltas;

  protected final InternalCDORevision[] dirtyObjects;

  protected final CDOID[] detachedObjects;

  protected final Map<CDOID, EClass> detachedObjectTypes;

  protected final CDOBranchVersion[] detachedObjectVersions;

  protected final List<byte[]> lobs = new ArrayList<byte[]>();

  private long newCommitPointer;

  private long newPackageUnitPointer;

  private Map<CDORevision, Long> newObjectPointers = new HashMap<CDORevision, Long>();

  private InternalCDORevision[] detachedRevisions;

  public CommitTransactionTask(InternalCommitContext context)
  {
    branchPoint = context.getBranchPoint();
    previousTimeStamp = context.getPreviousTimeStamp();
    userID = context.getUserID();
    commitComment = context.getCommitComment();
    newPackageUnits = context.getNewPackageUnits();
    newObjects = context.getNewObjects();
    dirtyObjectDeltas = context.getDirtyObjectDeltas();
    dirtyObjects = context.getDirtyObjects();
    detachedObjects = context.getDetachedObjects();
    detachedObjectTypes = context.getDetachedObjectTypes();
    detachedObjectVersions = context.getDetachedObjectVersions();
  }

  public CDOBranchPoint getBranchPoint()
  {
    return branchPoint;
  }

  public boolean isValid(CDOBranchPoint branchPoint)
  {
    long timeStamp = branchPoint.getTimeStamp();
    if (timeStamp != CDOBranchPoint.UNSPECIFIED_DATE && timeStamp < this.branchPoint.getTimeStamp())
    {
      return false;
    }

    return branchPoint.getBranch() == this.branchPoint.getBranch();
  }

  public void setDetachedRevisions(InternalCDORevision[] detachedRevisions)
  {
    this.detachedRevisions = detachedRevisions;
  }

  public long getNewCommitPointer()
  {
    return newCommitPointer;
  }

  public void setNewCommitPointer(long newCommitPointer)
  {
    this.newCommitPointer = newCommitPointer;
  }

  public long getNewPackageUnitPointer()
  {
    return newPackageUnitPointer;
  }

  public Map<CDORevision, Long> getNewObjectPointers()
  {
    return newObjectPointers;
  }

  public List<byte[]> getLobs()
  {
    return lobs;
  }

  public CDOCommitInfo createCommitInfo(InternalCDOCommitInfoManager commitInfoManager)
  {
    CDOBranch branch = branchPoint.getBranch();
    long timeStamp = branchPoint.getTimeStamp();
    return commitInfoManager.createCommitInfo(branch, timeStamp, previousTimeStamp, userID, commitComment, null, null);
  }

  @Override
  public void execute(Optimizer optimizer) throws IOException
  {
    LissomeStore store = optimizer.getStore();
    IndexWriter indexWriter = store.getIndex().getWriter();
    Vob vob = store.getVob();

    indexWriter.addCommitInfo(branchPoint, newCommitPointer);

    addObjects(indexWriter);
    updateObjects(indexWriter, vob);
    detachObjects(indexWriter, vob);

    indexWriter.commit();
  }

  protected void addObjects(IndexWriter indexWriter)
  {
    if (newObjects.length != 0)
    {
      long[] pointers = new long[newObjects.length];
      for (int i = 0; i < newObjects.length; i++)
      {
        InternalCDORevision revision = newObjects[i];
        pointers[i] = -newObjectPointers.get(revision);
      }

      indexWriter.addObjects(newObjects, pointers);
    }
  }

  protected void updateObjects(IndexWriter indexWriter, Vob vob)
  {
    if (dirtyObjects.length != 0)
    {
      long[] pointers = new long[dirtyObjects.length];
      for (int i = 0; i < dirtyObjects.length; i++)
      {
        InternalCDORevision revision = dirtyObjects[i];
        pointers[i] = vob.addRevision(revision);
      }

      indexWriter.updateObjects(dirtyObjects, pointers);
    }
  }

  protected void detachObjects(IndexWriter indexWriter, Vob vob)
  {
    if (detachedObjects.length != 0)
    {
      long[] pointers = new long[detachedRevisions.length];
      for (int i = 0; i < detachedRevisions.length; i++)
      {
        InternalCDORevision revision = detachedRevisions[i];
        pointers[i] = vob.addRevision(revision);
      }

      indexWriter.detachObjects(branchPoint, detachedObjects, detachedRevisions, pointers);
    }
  }

  public void cacheRevisions(CDORevisionCacheAdder cache)
  {
    for (int i = 0; i < newObjects.length; i++)
    {
      InternalCDORevision revision = newObjects[i];
      cache.addRevision(revision);
    }

    for (int i = 0; i < dirtyObjects.length; i++)
    {
      InternalCDORevision revision = dirtyObjects[i];
      cache.addRevision(revision);
    }

    for (int i = 0; i < detachedObjects.length; i++)
    {
      CDOID id = detachedObjects[i];
      EClass eClass = detachedObjectTypes.get(id);
      CDOBranchVersion branchVersion = detachedObjectVersions[i];
      CDOBranch branch = branchVersion.getBranch();
      int version = branchVersion.getVersion();
      long timeStamp = branchPoint.getTimeStamp();

      DetachedCDORevision revision = new DetachedCDORevision(eClass, id, branch, version, timeStamp);
      cache.addRevision(revision);
    }
  }
}

Back to the top