Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 901ee7a0eddf76239e90b1fe793653676596c5e4 (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
/**
 * 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:
 *    Victor Roldan Betancort - initial API and implementation
 */
package org.eclipse.emf.cdo.server.internal.db4o;

import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.id.CDOIDUtil;
import org.eclipse.emf.cdo.server.ISession;
import org.eclipse.emf.cdo.server.IStore;
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.db4o.IDB4OIdentifiableObject;
import org.eclipse.emf.cdo.server.db4o.IDB4OStore;
import org.eclipse.emf.cdo.spi.server.LongIDStore;
import org.eclipse.emf.cdo.spi.server.StoreAccessorPool;

import org.eclipse.net4j.util.ReflectUtil.ExcludeFromDump;

import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.ObjectServer;
import com.db4o.ObjectSet;
import com.db4o.config.Configuration;
import com.db4o.query.Query;
import com.db4o.reflect.jdk.JdkReflector;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Victor Roldan Betancort
 */
public class DB4OStore extends LongIDStore implements IDB4OStore
{
  private static final String ID_ATTRIBUTE = "id";

  private transient String storeLocation;

  private transient int port;

  private transient ObjectServer server;

  private transient Configuration serverConfiguration;

  private ServerInfo serverInfo;

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

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

  public DB4OStore(String storeLocation, int port)
  {
    super(IDB4OStore.TYPE, set(ChangeFormat.REVISION), set(RevisionTemporality.NONE, RevisionTemporality.AUDITING),
        set(RevisionParallelism.NONE, RevisionParallelism.BRANCHING));

    // setRevisionTemporality(RevisionTemporality.AUDITING);
    // setRevisionParallelism(RevisionParallelism.BRANCHING);

    this.storeLocation = storeLocation;
    this.port = port;
  }

  public DB4OStore(String storeLocation, int port, Configuration serverConfiguration)
  {
    this(storeLocation, port);
    this.serverConfiguration = serverConfiguration;
  }

  public String getStoreLocation()
  {
    return storeLocation;
  }

  public int getPort()
  {
    return port;
  }

  public long getCreationTime()
  {
    return getServerInfo().getCreationTime();
  }

  public void setCreationTime(long creationTime)
  {
    getServerInfo().setCreationTime(creationTime);
  }

  public boolean isFirstStart()
  {
    return getServerInfo().isFirstTime();
  }

  public Map<String, String> getPersistentProperties(Set<String> names)
  {
    if (names == null || names.isEmpty())
    {
      return new HashMap<String, String>(getServerInfo().getProperties());
    }

    Map<String, String> result = new HashMap<String, String>();
    for (String key : names)
    {
      String value = getServerInfo().getProperties().get(key);
      if (value != null)
      {
        result.put(key, value);
      }
    }

    return result;
  }

  public void setPersistentProperties(Map<String, String> properties)
  {
    ServerInfo serverInfo = getServerInfo();
    serverInfo.getProperties().putAll(properties);
    commitServerInfo(null);
  }

  public void removePersistentProperties(Set<String> names)
  {
    ServerInfo serverInfo = getServerInfo();
    Map<String, String> properties = serverInfo.getProperties();
    for (String key : names)
    {
      properties.remove(key);
    }

    commitServerInfo(null);
  }

  // @Override
  // public CDOID getNextCDOID(LongIDStoreAccessor accessor, CDORevision revision)
  // {
  // ObjectContainer objectContainer = ((DB4OStoreAccessor)accessor).getObjectContainer();
  // ExtObjectContainer ext = objectContainer.ext();
  // ext.store(revision);
  //
  // long id = ext.getID(revision);
  // return CDOIDUtil.createLong(id);
  // }

  public ObjectContainer openClient()
  {
    return server.openClient();
  }

  @Override
  protected void doActivate() throws Exception
  {
    super.doActivate();
    initObjectServer();
    initServerInfo();
    setLastCommitTime(getServerInfo().getLastCommitTime());
  }

  protected void initObjectServer()
  {
    Configuration configuration = serverConfiguration;
    if (configuration == null)
    {
      configuration = createServerConfiguration();
    }
    {
      File file = new File(getStoreLocation());
      if (!file.exists())
      {
        try
        {
          file.createNewFile();
        }
        catch (IOException ex)
        {
          throw new RuntimeException(ex);
        }
      }
    }

    server = Db4o.openServer(configuration, getStoreLocation(), getPort());
  }

  protected void tearDownObjectServer()
  {
    server.close();
    server = null;
  }

  private ServerInfo getServerInfo()
  {
    if (serverInfo == null)
    {
      initServerInfo();
    }

    return serverInfo;
  }

  private void initServerInfo()
  {
    ObjectContainer container = openClient();

    try
    {
      ObjectSet<ServerInfo> infos = container.query(ServerInfo.class);
      if (infos.size() > 1)
      {
        throw new IllegalStateException("ServeInfo is stored in container more than once");
      }

      if (infos.isEmpty())
      {
        serverInfo = new ServerInfo();
        serverInfo.setFirstTime(true);
        serverInfo.setCreationTime(System.currentTimeMillis());
        commitServerInfo(container);
      }
      else
      {
        serverInfo = infos.get(0);
        if (serverInfo.isFirstTime())
        {
          serverInfo.setFirstTime(false);
          commitServerInfo(container);
        }
      }
    }
    finally
    {
      closeClient(container);
    }
  }

  protected void closeClient(ObjectContainer container)
  {
    container.close();
  }

  private void commitServerInfo(ObjectContainer container)
  {
    ObjectContainer usedContainer = container != null ? container : openClient();

    try
    {
      usedContainer.store(serverInfo);
      usedContainer.commit();
    }
    finally
    {
      if (usedContainer != container)
      {
        closeClient(usedContainer);
      }
    }
  }

  @Override
  protected void doDeactivate() throws Exception
  {
    tearDownObjectServer();
    ObjectContainer container = openClient();
    try
    {
      getServerInfo().setLastCommitTime(getLastCommitTime());
      commitServerInfo(container);
    }
    finally
    {
      closeClient(container);
    }
    super.doDeactivate();
  }

  protected Configuration createServerConfiguration()
  {
    Configuration config = Db4o.newConfiguration();
    config.reflectWith(new JdkReflector(getClass().getClassLoader()));
    config.objectClass(DB4ORevision.class).objectField("id").indexed(true);
    config.objectClass(DB4OPackageUnit.class).objectField("id").indexed(true);
    config.objectClass(DB4OIdentifiableObject.class).objectField("id").indexed(true);
    config.objectClass(DB4OCommitInfo.class).objectField("timeStamp").indexed(true);
    return config;
  }

  @Override
  protected IStoreAccessor createReader(ISession session)
  {
    return new DB4OStoreAccessor(this, session);
  }

  @Override
  protected IStoreAccessor createWriter(ITransaction transaction)
  {
    return new DB4OStoreAccessor(this, transaction);
  }

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

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

  public static DB4ORevision getRevision(ObjectContainer container, CDOID id)
  {
    Query query = container.query();
    query.constrain(DB4ORevision.class);
    query.descend(ID_ATTRIBUTE).constrain(CDOIDUtil.getLong(id));

    ObjectSet<?> revisions = query.execute();
    if (revisions.isEmpty())
    {
      return null;
    }

    return (DB4ORevision)revisions.get(0);
  }

  public static <T> List<T> getElementsOfType(ObjectContainer container, Class<T> clazz)
  {
    ObjectSet<T> elements = container.query(clazz);
    return elements;
  }

  public static IDB4OIdentifiableObject getIdentifiableObject(ObjectContainer container, String id)
  {
    Query query = container.query();
    query.constrain(IDB4OIdentifiableObject.class);
    query.descend(ID_ATTRIBUTE).constrain(id);

    ObjectSet<?> revisions = query.execute();
    if (revisions.isEmpty())
    {
      return null;
    }

    return (IDB4OIdentifiableObject)revisions.get(0);
  }

  public static void removeRevision(ObjectContainer container, CDOID id)
  {
    DB4ORevision revision = getRevision(container, id);
    if (revision != null)
    {
      container.delete(revision);
    }
  }

  /**
   * Carries {@link IStore}-related information.
   * 
   * @author Victor Roldan Betancort
   */
  private static final class ServerInfo
  {
    private boolean isFirstTime;

    private long creationTime;

    private long lastCommitTime;

    private Map<String, String> properties = new HashMap<String, String>();

    public boolean isFirstTime()
    {
      return isFirstTime;
    }

    public void setFirstTime(boolean isFirstTime)
    {
      this.isFirstTime = isFirstTime;
    }

    public void setCreationTime(long creationTime)
    {
      this.creationTime = creationTime;
    }

    public long getCreationTime()
    {
      return creationTime;
    }

    @SuppressWarnings("unused")
    public void setProperties(Map<String, String> properties)
    {
      this.properties = properties;
    }

    public Map<String, String> getProperties()
    {
      return properties;
    }

    public long getLastCommitTime()
    {
      return lastCommitTime;
    }

    public void setLastCommitTime(long lastCommitTime)
    {
      this.lastCommitTime = lastCommitTime;
    }
  }
}

Back to the top