Skip to main content
summaryrefslogtreecommitdiffstats
blob: 36d04287f155c428288261a4710d1db10ae0f7e9 (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
/**
 * 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.server.internal.mongodb;

import org.eclipse.emf.cdo.common.branch.CDOBranch;
import org.eclipse.emf.cdo.common.branch.CDOBranchHandler;
import org.eclipse.emf.cdo.common.branch.CDOBranchPoint;
import org.eclipse.emf.cdo.common.branch.CDOBranchVersion;
import org.eclipse.emf.cdo.common.commit.CDOCommitData;
import org.eclipse.emf.cdo.common.commit.CDOCommitInfo;
import org.eclipse.emf.cdo.common.commit.CDOCommitInfoHandler;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.id.CDOIDUtil;
import org.eclipse.emf.cdo.common.lob.CDOLobHandler;
import org.eclipse.emf.cdo.common.model.CDOClassifierRef;
import org.eclipse.emf.cdo.common.model.EMFUtil;
import org.eclipse.emf.cdo.common.revision.CDORevision;
import org.eclipse.emf.cdo.common.revision.CDORevisionCacheAdder;
import org.eclipse.emf.cdo.common.revision.CDORevisionHandler;
import org.eclipse.emf.cdo.common.util.CDOQueryInfo;
import org.eclipse.emf.cdo.eresource.EresourcePackage;
import org.eclipse.emf.cdo.server.IQueryHandler;
import org.eclipse.emf.cdo.server.ISession;
import org.eclipse.emf.cdo.server.IStoreChunkReader;
import org.eclipse.emf.cdo.server.ITransaction;
import org.eclipse.emf.cdo.server.internal.mongodb.bundle.OM;
import org.eclipse.emf.cdo.server.mongodb.IMongoDBStoreAccessor;
import org.eclipse.emf.cdo.spi.common.commit.CDOChangeSetSegment;
import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageRegistry;
import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageUnit;
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.cdo.spi.server.Store;
import org.eclipse.emf.cdo.spi.server.StoreAccessorBase;

import org.eclipse.net4j.util.ObjectUtil;
import org.eclipse.net4j.util.collection.Pair;
import org.eclipse.net4j.util.om.monitor.OMMonitor;
import org.eclipse.net4j.util.om.trace.ContextTracer;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * @author Eike Stepper
 */
public class MongoDBStoreAccessor extends StoreAccessorBase implements IMongoDBStoreAccessor
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, MongoDBStoreAccessor.class);

  private static final boolean ZIP_PACKAGE_BYTES = true;

  public MongoDBStoreAccessor(Store store, ISession session)
  {
    super(store, session);
  }

  public MongoDBStoreAccessor(Store store, ITransaction transaction)
  {
    super(store, transaction);
  }

  @Override
  public MongoDBStore getStore()
  {
    return (MongoDBStore)super.getStore();
  }

  public IStoreChunkReader createChunkReader(InternalCDORevision revision, EStructuralFeature feature)
  {
    // Partial collection loading not supported, yet.
    return null;
  }

  public Collection<InternalCDOPackageUnit> readPackageUnits()
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public EPackage[] loadPackageUnit(InternalCDOPackageUnit packageUnit)
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public InternalCDORevision readRevision(CDOID id, CDOBranchPoint branchPoint, int listChunk,
      CDORevisionCacheAdder cache)
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public InternalCDORevision readRevisionByVersion(CDOID id, CDOBranchVersion branchVersion, int listChunk,
      CDORevisionCacheAdder cache)
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public CDOID readResourceID(CDOID folderID, String name, CDOBranchPoint branchPoint)
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public void handleRevisions(EClass eClass, CDOBranch branch, long timeStamp, boolean exactTime,
      CDORevisionHandler handler)
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public Set<CDOID> readChangeSet(OMMonitor monitor, CDOChangeSetSegment... segments)
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public void queryResources(QueryResourcesContext context)
  {
    // Only support timestamp in audit mode
    if (context.getTimeStamp() != CDORevision.UNSPECIFIED_DATE && !getStore().getRepository().isSupportingAudits())
    {
      throw new IllegalArgumentException("Auditing not supported");
    }

    EresourcePackage resourcesPackage = EresourcePackage.eINSTANCE;

    // First query folders
    boolean shallContinue = queryResources(context, resourcesPackage.getCDOResourceFolder());

    // Not enough results? -> query resources
    if (shallContinue)
    {
      queryResources(context, resourcesPackage.getCDOResource());
    }
  }

  private boolean queryResources(QueryResourcesContext context, EClass eClass)
  {
    IDHandler idHandler = getStore().getIDHandler();
    Mapper mapper = getStore().getMapper();

    DBCollection collection = mapper.getCollection(eClass);
    DBCursor cursor = null;

    CDOID folderID = context.getFolderID();
    String name = context.getName();
    boolean exactMatch = context.exactMatch();

    try
    {

      DBObject query = getStore().getMode().createResourcesQuery(folderID, name, exactMatch, context);
      DBObject keys = new BasicDBObject("_cdoid", 1);
      cursor = collection.find(query, keys);
      while (cursor.hasNext())
      {
        DBObject doc = cursor.next();

        CDOID id = idHandler.read(doc, "_cdoid");
        if (TRACER.isEnabled())
        {
          TRACER.trace("Resources query returned ID " + id); //$NON-NLS-1$
        }

        if (!context.addResource(id))
        {
          // No more results allowed
          return false; // Don't continue
        }
      }

      return true; // Continue with other results
    }
    finally
    {
    }
  }

  public void queryXRefs(QueryXRefsContext context)
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public void queryLobs(List<byte[]> ids)
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public void loadLob(byte[] id, OutputStream out) throws IOException
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public void handleLobs(long fromTime, long toTime, CDOLobHandler handler) throws IOException
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public IQueryHandler getQueryHandler(CDOQueryInfo info)
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public Pair<Integer, Long> createBranch(int branchID, BranchInfo branchInfo)
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public BranchInfo loadBranch(int branchID)
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public SubBranchInfo[] loadSubBranches(int branchID)
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public int loadBranches(int startID, int endID, CDOBranchHandler branchHandler)
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public void loadCommitInfos(CDOBranch branch, long startTime, long endTime, CDOCommitInfoHandler handler)
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public CDOCommitData loadCommitData(long timeStamp)
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  public void writePackageUnits(InternalCDOPackageUnit[] packageUnits, OMMonitor monitor)
  {
    MongoDBStore store = getStore();
    DBCollection packageUnitsCollection = store.getPackageUnitsCollection();
    DBObject[] docs = marshallPackageUnits(packageUnits);

    for (DBObject doc : docs)
    {
      packageUnitsCollection.insert(doc);
    }
  }

  public void write(InternalCommitContext context, OMMonitor monitor)
  {
    try
    {
      monitor.begin(104);
      CDOBranchPoint branchPoint = context.getBranchPoint();

      DBObject doc = new BasicDBObject();
      doc.put("_id", branchPoint.getTimeStamp());

      long previous = context.getPreviousTimeStamp();
      if (previous != CDOBranchPoint.UNSPECIFIED_DATE)
      {
        doc.put("previous", previous);
      }

      if (getStore().getRepository().isSupportingBranches())
      {
        doc.put("branch", branchPoint.getBranch().getID());
      }

      String user = context.getUserID();
      if (user != null)
      {
        doc.put("user", user);
      }

      String comment = context.getCommitComment();
      if (comment != null)
      {
        doc.put("comment", comment);
      }

      InternalCDOPackageUnit[] newPackageUnits = context.getNewPackageUnits();
      if (!ObjectUtil.isEmpty(newPackageUnits))
      {
        doc.put("packages", marshallPackageUnits(newPackageUnits));
      }

      monitor.worked();
      addIDMappings(context, monitor.fork());
      context.applyIDMappings(monitor.fork());

      List<DBObject> docs = new ArrayList<DBObject>();
      marshalRevisions(docs, context.getNewObjects());
      marshalRevisions(docs, context.getDirtyObjects());
      if (!docs.isEmpty())
      {
        doc.put("revisions", docs);
      }

      monitor.worked();

      getStore().getCommitInfosCollection().insert(doc);
      monitor.worked(100);

      CDOCommitInfo commitInfo = context.createCommitInfo();
      getStore().getMapper().addWork(commitInfo);
    }
    finally
    {
      monitor.done();
    }
  }

  public void commit(OMMonitor monitor)
  {
    // Do nothing
  }

  public void rollback()
  {
    throw new UnsupportedOperationException("Not yet implemented"); // TODO Implement me
  }

  @Override
  protected CDOID getNextCDOID(CDORevision revision)
  {
    return getStore().getIDHandler().getNextCDOID(revision);
  }

  private DBObject[] marshallPackageUnits(InternalCDOPackageUnit[] packageUnits)
  {
    DBObject[] result = new DBObject[packageUnits.length];
    InternalCDOPackageRegistry packageRegistry = getStore().getRepository().getPackageRegistry();

    for (int i = 0; i < packageUnits.length; i++)
    {
      InternalCDOPackageUnit packageUnit = packageUnits[i];
      EPackage ePackage = packageUnit.getTopLevelPackageInfo().getEPackage();
      byte[] bytes = EMFUtil.getEPackageBytes(ePackage, ZIP_PACKAGE_BYTES, packageRegistry);

      DBObject doc = new BasicDBObject();
      doc.put("id", packageUnit.getID());
      doc.put("type", packageUnit.getOriginalType().toString());
      doc.put("time", packageUnit.getTimeStamp());
      doc.put("data", bytes);

      result[i] = doc;
    }

    return result;
  }

  private void marshalRevisions(List<DBObject> docs, InternalCDORevision[] revisions)
  {
    for (InternalCDORevision revision : revisions)
    {
      DBObject doc = marshallRevision(revision);
      docs.add(doc);
    }
  }

  private DBObject[] marshallRevisions(InternalCDORevision[] revisions)
  {
    DBObject[] result = new DBObject[revisions.length];
    for (int i = 0; i < revisions.length; i++)
    {
      InternalCDORevision revision = revisions[i];
      result[i] = marshallRevision(revision);
    }

    return result;
  }

  private DBObject marshallRevision(InternalCDORevision revision)
  {
    IDHandler idHandler = getStore().getIDHandler();

    DBObject doc = new BasicDBObject();
    idHandler.write(doc, "cdo_id", revision.getID());
    if (getStore().getRepository().isSupportingBranches())
    {
      int branch = revision.getBranch().getID();
      if (branch != 0)
      {
        doc.put("cdo_branch", branch);
      }
    }

    doc.put("cdo_version", revision.getVersion());
    doc.put("cdo_created", revision.getTimeStamp());

    long revised = revision.getRevised();
    if (revised != CDOBranchPoint.UNSPECIFIED_DATE)
    {
      doc.put("cdo_revised", revised);
    }

    doc.put("cdo_class", new CDOClassifierRef(revision.getEClass()).getURI());

    CDOID resourceID = revision.getResourceID();
    if (!CDOIDUtil.isNull(resourceID))
    {
      idHandler.write(doc, "cdo_resource", resourceID);
    }

    CDOID containerID = (CDOID)revision.getContainerID();
    if (!CDOIDUtil.isNull(containerID))
    {
      idHandler.write(doc, "cdo_container", containerID);
      int featureID = revision.getContainingFeatureID();
      if (featureID != 0)
      {
        doc.put("cdo_feature", featureID);
      }
    }

    return doc;
  }

  private DBObject[] marshallRevisionDeltas(InternalCDORevisionDelta[] revisionDeltas)
  {
    DBObject[] result = new DBObject[revisionDeltas.length];
    for (int i = 0; i < revisionDeltas.length; i++)
    {
      InternalCDORevisionDelta revisionDelta = revisionDeltas[i];
      result[i] = marshallRevisionDelta(revisionDelta);
    }

    return result;
  }

  private DBObject marshallRevisionDelta(InternalCDORevisionDelta revisionDelta)
  {
    IDHandler idHandler = getStore().getIDHandler();

    DBObject doc = new BasicDBObject();
    idHandler.write(doc, "cdo_id", revisionDelta.getID());
    if (getStore().getRepository().isSupportingBranches())
    {
      doc.put("cdo_branch", revisionDelta.getBranch().getID());
    }

    // doc.put("cdo_version", revisionDelta.getVersion());

    return doc;
  }

  private DBObject[] marshallObjectTypes(Map<CDOID, EClass> objectTypes)
  {
    IDHandler idHandler = getStore().getIDHandler();
    Iterator<Entry<CDOID, EClass>> it = objectTypes.entrySet().iterator();

    DBObject[] result = new DBObject[objectTypes.size()];
    for (int i = 0; i < result.length; i++)
    {
      Entry<CDOID, EClass> entry = it.next();
      CDOID id = entry.getKey();
      EClass type = entry.getValue();

      DBObject doc = new BasicDBObject();
      idHandler.write(doc, "id", id);
      doc.put("type", new CDOClassifierRef(type).getURI());

      result[i] = doc;
    }

    return result;
  }
}

Back to the top