Skip to main content
summaryrefslogtreecommitdiffstats
blob: fba6ed700ea8ee0d262517f262c95b40b1e1eb6f (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
/**
 * 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:
 *    Simon McDuff - initial API and implementation
 *    Ibrahim Sallam - code refactoring for CDO 3.0
 */
package org.eclipse.emf.cdo.server.internal.objectivity;

import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.id.CDOIDUtil;
import org.eclipse.emf.cdo.common.revision.CDORevision;
import org.eclipse.emf.cdo.server.ISession;
import org.eclipse.emf.cdo.server.IStoreAccessor;
import org.eclipse.emf.cdo.server.ITransaction;
import org.eclipse.emf.cdo.server.IView;
import org.eclipse.emf.cdo.server.internal.objectivity.bundle.OM;
import org.eclipse.emf.cdo.server.internal.objectivity.clustering.ObjyPlacementManager;
import org.eclipse.emf.cdo.server.internal.objectivity.db.ObjyCommitInfoHandler;
import org.eclipse.emf.cdo.server.internal.objectivity.db.ObjyConnection;
import org.eclipse.emf.cdo.server.internal.objectivity.db.ObjyPackageHandler;
import org.eclipse.emf.cdo.server.internal.objectivity.db.ObjyPropertyMapHandler;
import org.eclipse.emf.cdo.server.internal.objectivity.db.ObjySchema;
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.schema.ObjyStoreInfo;
import org.eclipse.emf.cdo.server.internal.objectivity.utils.ObjyDb;
import org.eclipse.emf.cdo.server.objectivity.IObjectivityStore;
import org.eclipse.emf.cdo.server.objectivity.IObjectivityStoreConfig;
import org.eclipse.emf.cdo.spi.server.LongIDStore;
import org.eclipse.emf.cdo.spi.server.Store;
import org.eclipse.emf.cdo.spi.server.StoreAccessorPool;

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

import com.objy.db.app.Connection;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;

public class ObjectivityStore extends Store implements IObjectivityStore
{

  public static final String TYPE = "objectivity"; //$NON-NLS-1$

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

  protected ConcurrentLinkedQueue<ObjectivityStoreAccessor> writers = new ConcurrentLinkedQueue<ObjectivityStoreAccessor>();

  @ExcludeFromDump
  private transient StoreAccessorPool readerPool = new StoreAccessorPool(this, null);

  @ExcludeFromDump
  private transient StoreAccessorPool writerPool = new StoreAccessorPool(this, null);

  private ObjyConnection objyConnection = null;

  private IObjectivityStoreConfig storeConfig = null;

  private boolean firstTime = false;

  private int nActivate = 0;

  private boolean requiredToSupportAudits;

  private boolean requiredToSupportBranches;

  private ObjyCommitInfoHandler objyCommitInfoHandler = null;

  private ObjyPropertyMapHandler objyPropertyMapHandler = null;

  private ObjyPackageHandler objyPackageHandler = null;

  private boolean storeInitialized = false;

  private long creationTime = CDORevision.UNSPECIFIED_DATE;

  // private boolean resetData = false;

  public ObjectivityStore(IObjectivityStoreConfig config)
  {
    // super(TYPE, set(ChangeFormat.REVISION, ChangeFormat.DELTA), set(
    // RevisionTemporality.NONE, RevisionTemporality.AUDITING),
    // set(RevisionParallelism.NONE));
    // setRevisionTemporality(RevisionTemporality.AUDITING);
    super(TYPE, LongIDStore.OBJECT_ID_TYPES, set(ChangeFormat.REVISION, ChangeFormat.DELTA), set(
        RevisionTemporality.NONE, RevisionTemporality.AUDITING), set(RevisionParallelism.NONE,
        RevisionParallelism.BRANCHING));
    storeConfig = config;
  }

  private void initStore()
  {
    // the caller already used the StoreConfig to open the connection
    // to the FD so, get the current here.
    objyConnection = ObjyConnection.INSTANCE;
    objyConnection.setSessionMinCacheSize(storeConfig.getSessionMinCacheSize());
    objyConnection.setSessionMaxCacheSize(storeConfig.getSessionMaxCacheSize());

    // -----------------------------------------------------------------------
    // Initialize schema as needed, and also any other config information

    // connection to the FD.
    objyConnection.connect(storeConfig.getFdName());
    Connection.current().setUserClassLoader(this.getClass().getClassLoader());

    objyConnection.registerClass("org.eclipse.emf.cdo.server.internal.objectivity.schema.ObjyStoreInfo"); //$NON-NLS-1$
    objyConnection.registerClass("org.eclipse.emf.cdo.server.internal.objectivity.schema.ObjyPackageInfo"); //$NON-NLS-1$
    objyConnection.registerClass("org.eclipse.emf.cdo.server.internal.objectivity.schema.ObjyPackageUnit"); //$NON-NLS-1$
    objyConnection.registerClass("org.eclipse.emf.cdo.server.internal.objectivity.schema.ObjyCommitInfo"); //$NON-NLS-1$
    objyConnection.registerClass("org.eclipse.emf.cdo.server.internal.objectivity.schema.ObjyProperty"); //$NON-NLS-1$

    ObjySession objySession = objyConnection.getWriteSessionFromPool("Main"); //$NON-NLS-1$
    objySession.setRecoveryAutomatic(true);
    objySession.begin();

    ObjySchema.createBaseSchema();

    try
    {
      String repositoryName = getRepository().getName();
      // check if we initialized the store for the first time.
      {
        // we have one-to-one mapping between a store and a repository. In Objectivity, we
        // can still use one federation to store multiple repository, each will have its
        // own ObjyStoreInfo object in the default container.
        ObjyScope objyScope = new ObjyScope(repositoryName, ObjyDb.DEFAULT_CONT_NAME);
        ObjyStoreInfo objyStoreInfo = null;
        try
        {
          objyStoreInfo = (ObjyStoreInfo)objyScope.lookupObject(ObjyDb.OBJYSTOREINFO_NAME);
          creationTime = objyStoreInfo.getCreationTime();
        }
        catch (Exception ex)
        {
          // create the ObjyStoreInfo.
          objyStoreInfo = new ObjyStoreInfo(System.currentTimeMillis(), "...");
          objyScope.getContainerObj().cluster(objyStoreInfo);
          objyScope.nameObj(ObjyDb.OBJYSTOREINFO_NAME, objyStoreInfo);

          // flag as first time.
          firstTime = true;
        }

        creationTime = objyStoreInfo.getCreationTime();
      }

      // This is used for the package storage, it could be lazily done though!!! (verify)
      // ObjyScope.insureScopeExist(objySession, ObjyDb.CONFIGDB_NAME, ObjyDb.PACKAGESTORE_CONT_NAME);

      // make sure we have the root resource created.
      // ObjyDb.getOrCreateResourceList();
      objyCommitInfoHandler = new ObjyCommitInfoHandler(repositoryName);
      objyPropertyMapHandler = new ObjyPropertyMapHandler(repositoryName);
      objyPackageHandler = new ObjyPackageHandler(repositoryName);

      objySession.commit();

      storeInitialized = true;
    }
    catch (RuntimeException ex)
    {
      ex.printStackTrace();
      objySession.abort();
    }
    finally
    {
      objyConnection.returnSessionToPool(objySession);
    }
  }

  @Override
  protected IStoreAccessor createReader(ISession session)
  {
    // System.out
    // .println(">>>>IS:<<<< ObjectivityStore.createRead() - " + (session == null ? "null" : session.toString()));
    return new ObjectivityStoreAccessor(this, session);
  }

  @Override
  protected IStoreAccessor createWriter(ITransaction transaction)
  {
    // if (transaction == null)
    // {
    // System.out.println(">>>>IS:<<<< ObjectivityStore.createWriter() - transaction: null");
    // }
    // else
    // {
    // System.out.println(">>>>IS:<<<< ObjectivityStore.createWriter() - "
    // + (transaction.getSession() == null ? "null" : transaction.getSession().toString()));
    // }
    return new ObjectivityStoreAccessor(this, transaction);
  }

  @Override
  protected StoreAccessorPool getReaderPool(ISession session, boolean forReleasing)
  {
    return readerPool;
  }

  @Override
  protected StoreAccessorPool getWriterPool(IView view, boolean forReleasing)
  {
    return writerPool;
  }

  public boolean isLocal(CDOID id)
  {
    return false;
  }

  /**
   * @since 4.0
   */
  public CDOID createObjectID(String val)
  {
    Long id = Long.valueOf(val);
    return CDOIDUtil.createLong(id);
  }

  public long getCreationTime()
  {
    return creationTime;
  }

  public void setCreationTime(long creationTime)
  {
    // TODO: implement ObjectivityStore.setCreationTime(creationTime)
    throw new UnsupportedOperationException();
  }

  public boolean isFirstStart()
  {
    return firstTime;
  }

  public ObjyConnection getConnection()
  {
    return objyConnection;

  }

  public boolean isRequiredToSupportAudits()
  {
    return requiredToSupportAudits;
  }

  public boolean isRequiredToSupportBranches()
  {
    return requiredToSupportBranches;
  }

  @Override
  protected void doBeforeActivate()
  {
    requiredToSupportAudits = getRepository().isSupportingAudits();
    requiredToSupportBranches = getRepository().isSupportingBranches();
  }

  @Override
  protected void doActivate() throws Exception
  {
    super.doActivate();

    // lazy initialization of the store.
    if (!storeInitialized)
    {
      initStore();
    }

    nActivate++;

    if (TRACER_DEBUG.isEnabled())
    {
      TRACER_DEBUG.trace("doActivate - count: " + nActivate);
    }

    ObjySession objySession = objyConnection.getWriteSessionFromPool("Main");
    objySession.setRecoveryAutomatic(true);
    objySession.begin();

    try
    {
      if (!objySession.getFD().hasDB(getRepository().getName()))
      {
        // Create the repo DB.
        ObjyScope.insureScopeExist(objySession, getRepository().getName(), ObjyDb.DEFAULT_CONT_NAME);
        // ...do other initialisation of the repository here.
        // Note that in the current implementation we don't delete DBs by default, only delete
        // the containers (see ObjectivityStoreConfig.resetFD()) so any initialization done here
        // might not be repeated.
      }

      objySession.commit();
    }
    catch (RuntimeException ex)
    {
      objySession.abort();
    }
    finally
    {
      objyConnection.returnSessionToPool(objySession);
    }
  }

  @Override
  protected void doDeactivate() throws Exception
  {
    try
    {
      ObjySchema.resetCache();
      // System.out.println(" -------- doDeactivate() ObjectivityStore ----------");
    }
    finally
    {
      ObjyConnection.INSTANCE.disconnect();
    }

    // readerPool.dispose();
    // writerPool.dispose();
    super.doDeactivate();

  }

  public Map<String, String> getPersistentProperties(Set<String> names)
  {
    if (names == null || names.isEmpty())
    {
      // Return *all* property values

      // TODO: implement ObjectivityStore.getPropertyValues(names)
      throw new UnsupportedOperationException();
    }

    ObjySession objySession = objyConnection.getReadSessionFromPool("Main");
    objySession.begin();
    Map<String, String> properties = objyPropertyMapHandler.getPropertyValues(names);
    objySession.commit();
    return properties;
  }

  public void setPersistentProperties(Map<String, String> properties)
  {
    ObjySession objySession = objyConnection.getWriteSessionFromPool("Main");
    objySession.begin();
    objyPropertyMapHandler.setPropertyValues(properties);
    objySession.commit();
  }

  public void removePersistentProperties(Set<String> names)
  {
    ObjySession objySession = objyConnection.getWriteSessionFromPool("Main");
    objySession.begin();
    objyPropertyMapHandler.removePropertyValues(names);
    objySession.commit();
  }

  public ObjyCommitInfoHandler getCommitInfoHandler()
  {
    return objyCommitInfoHandler;
  }

  public ObjyPackageHandler getPackageHandler()
  {
    return objyPackageHandler;
  }

  public ObjyPlacementManager getGlobalPlacementManager()
  {
    return ObjyConnection.INSTANCE.getDefaultPlacementManager();
  }

}

Back to the top