Skip to main content
summaryrefslogtreecommitdiffstats
blob: 69b604c6bbc5e93d4f2c7e18c162c6dbd8d870e4 (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
/*
 * Copyright (c) 2004 - 2012 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:
 *    Ibrahim Sallam - initial API and implementation
 */
package org.eclipse.emf.cdo.server.internal.objectivity.clustering;

import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.id.CDOIDTemp;
import org.eclipse.emf.cdo.eresource.EresourcePackage;
import org.eclipse.emf.cdo.server.internal.objectivity.ObjectivityStore;
import org.eclipse.emf.cdo.server.internal.objectivity.bundle.OM;
import org.eclipse.emf.cdo.server.internal.objectivity.db.ObjyObject;
import org.eclipse.emf.cdo.server.internal.objectivity.db.ObjyScope;
import org.eclipse.emf.cdo.server.internal.objectivity.db.ObjySession;
import org.eclipse.emf.cdo.server.internal.objectivity.utils.OBJYCDOIDUtil;
import org.eclipse.emf.cdo.spi.common.id.AbstractCDOIDLong;
import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevision;
import org.eclipse.emf.cdo.spi.server.InternalCommitContext;

import org.eclipse.net4j.util.om.trace.ContextTracer;

import org.eclipse.emf.ecore.EClass;

import com.objy.db.app.ooId;

import java.util.HashMap;
import java.util.Map;

/***
 * This class will attempt to cluster objects with their container, or with a resource. It should be able to use the
 * global clustering which will use model elements to locate where an object will end up.
 * 
 * @author Ibrahim Sallam
 */
public class ObjyPlacementManagerLocal
{

  private static final ContextTracer TRACER_DEBUG = new ContextTracer(OM.DEBUG, ObjyPlacementManagerLocal.class);

  // private static final ContextTracer TRACER_ERROR = new ContextTracer(OM.ERROR, ObjyPlacementManagerLocal.class);

  // private static final ContextTracer TRACER_INFO = new ContextTracer(OM.INFO, ObjyPlacementManagerLocal.class);

  private String repositoryName = null;

  ObjySession objySession = null;

  InternalCommitContext commitContext = null;

  Map<CDOID, InternalCDORevision> newObjectsMap;

  Map<CDOID, CDOID> idMapper;

  Map<CDOID, ObjyObject> newObjyObjectsMap;

  public ObjyPlacementManagerLocal(ObjectivityStore objyStore, ObjySession objySession,
      InternalCommitContext commitContext, Map<CDOID, ObjyObject> newObjyObjectsMap)
  {
    repositoryName = objyStore.getRepository().getName();
    this.objySession = objySession;
    this.commitContext = commitContext;
    // first put them in a map for easy lookup and processing....
    newObjectsMap = new HashMap<CDOID, InternalCDORevision>();
    for (InternalCDORevision revision : commitContext.getNewObjects())
    {
      newObjectsMap.put(revision.getID(), revision);
    }

    idMapper = new HashMap<CDOID, CDOID>();

    this.newObjyObjectsMap = newObjyObjectsMap;
  }

  public void processRevision(InternalCDORevision revision)
  {
    // the revision could've been processed in case if it's a container
    // object and we reached it while processing another revision.
    if (isIdProcessed(revision.getID()))
    {
      return;
    }

    // create the object and add it to mapping, this will recursively call
    // other object creation as needed, based on the default clustering of
    // having each object is stored with its container.
    try
    {
      createObjectAndAddToMapping(revision);
    }
    catch (com.objy.db.ObjyRuntimeException ex)
    {
      ex.printStackTrace();
    }
  }

  private ObjyObject createObjectAndAddToMapping(InternalCDORevision revision)
  {
    ObjyObject objyObject = createObject(revision);

    CDOID newID = OBJYCDOIDUtil.getCDOID(objyObject.ooId());

    // nearObject = objyObject.ooId();
    if (TRACER_DEBUG.isEnabled())
    {
      TRACER_DEBUG.trace("Adding object to mapping from " + revision.getID() + " to " + newID);
    }
    commitContext.addIDMapping(revision.getID(), newID);
    // keep a track of this mapping.
    idMapper.put(revision.getID(), newID);
    newObjyObjectsMap.put(newID, objyObject);

    return objyObject;
  }

  protected boolean isIdProcessed(CDOID id)
  {
    // if the ID in the idMapper, then we did process the revision alreay
    return idMapper.get(id) != null;
  }

  protected ObjyObject createObject(InternalCDORevision revision)
  {
    long startTime = System.nanoTime();
    ooId nearObject = null;
    EClass eClass = revision.getEClass();
    if (TRACER_DEBUG.isEnabled())
    {
      TRACER_DEBUG.trace("Creating new object for revision: " + revision + " - eClass: " + eClass);
    }
    if (revision.isResourceNode())
    {
      String resourceName = (String)revision.data().get(EresourcePackage.eINSTANCE.getCDOResourceNode_Name(), 0);
      // The resourcelist is in the ConfigDB, but each resource is in a resource
      // container in the repo database, except the first root (ID == resourceID).
      // if (revision.getID() == revision.getResourceID()) // Check with Eike!
      if (resourceName == null) // root.
      {
        nearObject = objySession.getResourceList(repositoryName).ooId();
      }
      else
      {
        ObjyScope objyScope = new ObjyScope(repositoryName, resourceName /* ObjyDb.RESOURCELIST_CONT_NAME */);
        nearObject = objyScope.getScopeContOid();
      }
    }
    else
    {
      nearObject = getNearObject(revision);
    }

    if (nearObject == null)
    {
      // we have to put it somewhere.
      // call the global placement manager.
      nearObject = objySession.getObjectManager().getGlobalPlacementManager()
          .getNearObject(null, null, revision.getEClass());
    }

    ObjyObject objyObject = objySession.getObjectManager().newObject(eClass, nearObject);

    // // if it's a resource, collect it.
    // if (revision.isResourceNode())
    // {
    // // Add resource to the list
    // ObjyResourceList resourceList = objySession.getResourceList();
    //
    // // before we update the data into the object we need to check
    // // if it's a resource and we're trying to add a duplicate.
    // // TODO - do we need to check for Folder and resouce, or is the isResourceNode()
    // // check is enough?!!!
    // if (revision.isResourceFolder() || revision.isResource())
    // {
    // // this call will throw exception if we have a duplicate resource we trying to add.
    // resourceList.checkDuplicateResources(revision);
    // }
    // SmartLock.lock(objyObject);
    // resourceList.add(objyObject);
    // }
    ObjyObject.createObjectTime += System.nanoTime() - startTime;
    ObjyObject.createObjectCount++;

    return objyObject;
  }

  /***
   * This function might be called recursively throw the call to createAndAddToMapping() to create all the container
   * objects and or resources needed to cluster the rest of the new objects...
   */
  protected ooId getNearObject(InternalCDORevision revision)
  {
    ooId nearObject = null;
    // find the new object which is either a container or a resource.
    Object id = revision.getContainerID();

    if (id instanceof CDOID && (CDOID)id != CDOID.NULL)
    {
      nearObject = getOidFromCDOID((CDOID)id);
    }
    else
    {
      // use the resource...
      CDOID resourceId = revision.getResourceID();
      nearObject = getOidFromCDOID(resourceId);
    }
    return nearObject;
  }

  protected ooId getOidFromCDOID(CDOID id)
  {
    ooId oid = null;

    // if (OBJYCDOIDUtil.isValidObjyId(id))
    // oid = OBJYCDOIDUtil.getooId(id);

    if (id instanceof AbstractCDOIDLong)
    {
      oid = OBJYCDOIDUtil.getooId(id);
    }
    else if (id instanceof CDOIDTemp)
    {
      // see if we've seen it before
      CDOID nearId = idMapper.get(id);
      if (nearId != null)
      {
        oid = OBJYCDOIDUtil.getooId(nearId);
      }
      else
      {
        // create that object since it wasn't created and mapped yet.
        InternalCDORevision containerRevision = newObjectsMap.get(id);
        if (containerRevision != null)
        {
          oid = createObjectAndAddToMapping(containerRevision).ooId();
        }
      }
    }

    return oid;
  }

}

Back to the top