Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8e9f8b6f80fae2141315874746538b9777060683 (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
/***************************************************************************
 * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
 * 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.internal.server;

import org.eclipse.emf.cdo.internal.protocol.CDOIDImpl;
import org.eclipse.emf.cdo.internal.protocol.model.CDOClassRefImpl;
import org.eclipse.emf.cdo.internal.server.bundle.OM;
import org.eclipse.emf.cdo.protocol.CDOID;
import org.eclipse.emf.cdo.protocol.model.CDOClassRef;
import org.eclipse.emf.cdo.server.IStoreReader;
import org.eclipse.emf.cdo.server.ITypeManager;

import org.eclipse.net4j.internal.util.lifecycle.QueueWorker;
import org.eclipse.net4j.util.ImplementationError;
import org.eclipse.net4j.util.io.CachedFileMap;
import org.eclipse.net4j.util.io.ExtendedDataInput;
import org.eclipse.net4j.util.io.ExtendedDataOutput;
import org.eclipse.net4j.util.io.ExtendedIOUtil;
import org.eclipse.net4j.util.io.IOUtil;
import org.eclipse.net4j.util.io.SortedFileMap;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author Eike Stepper
 */
public class TypeManager extends QueueWorker<ObjectEntry> implements ITypeManager
{
  private Repository repository;

  private boolean persistent;

  private ConcurrentMap<CDOID, CDOClassRef> objectTypes = new ConcurrentHashMap();

  private PackageURIMap packageURIMap;

  private PackageIDMap packageIDMap;

  private ObjectTypeMap objectTypeMap;

  /**
   * TODO Needed?
   */
  private ObjectTypeMap metaObjectTypeMap;

  private int nextPackageID;

  public TypeManager(Repository repository)
  {
    this.repository = repository;
  }

  public Repository getRepository()
  {
    return repository;
  }

  public boolean isPersistent()
  {
    return persistent;
  }

  public void setPersistent(boolean persistent)
  {
    this.persistent = persistent;
  }

  public CDOClassRef getObjectType(IStoreReader storeReader, CDOID id)
  {
    CDOClassRef type = objectTypes.get(id);
    if (type == null && persistent)
    {
      type = persistentLoadType(id);
    }

    if (type == null && storeReader != null)
    {
      type = storeReader.readObjectType(id);
    }

    if (type == null)
    {
      throw new ImplementationError("Type not found for id " + id);
    }

    objectTypes.put(id, type);
    return type;
  }

  public void registerObjectType(CDOID id, CDOClassRef type)
  {
    objectTypes.putIfAbsent(id, type);
    if (persistent)
    {
      addWork(new ObjectEntry(id, type));
    }
  }

  protected CDOClassRef persistentLoadType(CDOID id)
  {
    TypeEntry entry = null;
    if (id.isMeta())
    {
      if (objectTypeMap != null)
      {
        entry = objectTypeMap.get(id);
      }
    }
    else
    {
      if (metaObjectTypeMap != null)
      {
        entry = metaObjectTypeMap.get(id);
      }
    }

    if (entry == null)
    {
      return null;
    }

    String packageURI = packageURIMap.get(entry.getPackageID());
    return new CDOClassRefImpl(packageURI, entry.getClassifierID());
  }

  @Override
  protected void work(WorkContext context, ObjectEntry element)
  {
    CDOID id = element.getID();
    CDOClassRef type = element.getType();
    String packageURI = type.getPackageURI();
    int classifierID = type.getClassifierID();

    Integer packageID = packageIDMap.get(packageURI);
    if (packageID == null)
    {
      packageID = nextPackageID++;
      packageIDMap.put(packageURI, packageID);
      packageURIMap.put(packageID, packageURI);
    }

    TypeEntry entry = new TypeEntry(classifierID, packageID);
    if (id.isMeta())
    {
      metaObjectTypeMap.put(id, entry);
    }
    else
    {
      objectTypeMap.put(id, entry);
    }
  }

  @Override
  protected void doActivate() throws Exception
  {
    super.doActivate();
    if (persistent)
    {
      File stateFolder = new File(OM.BUNDLE.getStateLocation());
      File repositoryFolder = new File(stateFolder, repository.getUUID());
      IOUtil.mkdirs(repositoryFolder);

      packageURIMap = new PackageURIMap(new File(repositoryFolder, "package.uris"));
      packageIDMap = new PackageIDMap(new File(repositoryFolder, "package.ids"));
      objectTypeMap = new ObjectTypeMap(new File(repositoryFolder, "object.types"));
      metaObjectTypeMap = new ObjectTypeMap(new File(repositoryFolder, "metaobject.types"));

      Integer max = packageURIMap.getMaxKey();
      nextPackageID = max == null ? 1 : max + 1;
    }
  }

  @Override
  protected void doDeactivate() throws Exception
  {
    IOUtil.close(metaObjectTypeMap);
    IOUtil.close(objectTypeMap);
    IOUtil.close(packageIDMap);
    IOUtil.close(packageURIMap);
    super.doDeactivate();
  }

  /**
   * @author Eike Stepper
   */
  private static final class TypeEntry
  {
    public static final int SIZE = 8;

    private int classifierID;

    private int packageID;

    public TypeEntry(int classifierID, int packageID)
    {
      this.classifierID = classifierID;
      this.packageID = packageID;
    }

    public TypeEntry(ExtendedDataInput in) throws IOException
    {
      classifierID = in.readInt();
      packageID = in.readInt();
    }

    public void write(ExtendedDataOutput out) throws IOException
    {
      out.writeInt(classifierID);
      out.writeInt(packageID);
    }

    public int getClassifierID()
    {
      return classifierID;
    }

    public int getPackageID()
    {
      return packageID;
    }
  }

  /**
   * @author Eike Stepper
   */
  private static final class PackageURIMap extends CachedFileMap<Integer, String>
  {
    public PackageURIMap(File file)
    {
      super(file, "rw");
    }

    @Override
    public int getKeySize()
    {
      return 4;
    }

    @Override
    protected Integer readKey(ExtendedDataInput in) throws IOException
    {
      return in.readInt();
    }

    @Override
    protected void writeKey(ExtendedDataOutput out, Integer key) throws IOException
    {
      out.writeInt(key);
    }

    @Override
    public int getValueSize()
    {
      return 260;
    }

    @Override
    protected String readValue(ExtendedDataInput in) throws IOException
    {
      return in.readString();
    }

    @Override
    protected void writeValue(ExtendedDataOutput out, String value) throws IOException
    {
      byte[] bytes = value.getBytes();
      if (bytes.length + 4 > getValueSize())
      {
        throw new IllegalArgumentException("Value size of " + getValueSize() + " exceeded: " + value);
      }

      ExtendedIOUtil.writeByteArray(out, bytes);
    }
  }

  /**
   * @author Eike Stepper
   */
  private static final class PackageIDMap extends CachedFileMap<String, Integer>
  {
    public PackageIDMap(File file)
    {
      super(file, "rw");
    }

    @Override
    public int getKeySize()
    {
      return 260;
    }

    @Override
    protected String readKey(ExtendedDataInput in) throws IOException
    {
      return in.readString();
    }

    @Override
    protected void writeKey(ExtendedDataOutput out, String key) throws IOException
    {
      byte[] bytes = key.getBytes();
      if (bytes.length + 4 > getKeySize())
      {
        throw new IllegalArgumentException("Key size of " + getKeySize() + " exceeded: " + key);
      }

      ExtendedIOUtil.writeByteArray(out, bytes);
    }

    @Override
    public int getValueSize()
    {
      return 4;
    }

    @Override
    protected Integer readValue(ExtendedDataInput in) throws IOException
    {
      return in.readInt();
    }

    @Override
    protected void writeValue(ExtendedDataOutput out, Integer value) throws IOException
    {
      out.writeInt(value);
    }
  }

  /**
   * @author Eike Stepper
   */
  private static final class ObjectTypeMap extends SortedFileMap<CDOID, TypeEntry>
  {
    public ObjectTypeMap(File file)
    {
      super(file, "rw");
    }

    @Override
    public int getKeySize()
    {
      return 8;
    }

    @Override
    protected CDOID readKey(ExtendedDataInput in) throws IOException
    {
      return CDOIDImpl.read(in);
    }

    @Override
    protected void writeKey(ExtendedDataOutput out, CDOID key) throws IOException
    {
      CDOIDImpl.write(out, key);
    }

    @Override
    public int getValueSize()
    {
      return TypeEntry.SIZE;
    }

    @Override
    protected TypeEntry readValue(ExtendedDataInput in) throws IOException
    {
      return new TypeEntry(in);
    }

    @Override
    protected void writeValue(ExtendedDataOutput out, TypeEntry value) throws IOException
    {
      value.write(out);
    }
  }
}

/**
 * @author Eike Stepper
 */
final class ObjectEntry
{
  private CDOID id;

  private CDOClassRef type;

  public ObjectEntry(CDOID id, CDOClassRef type)
  {
    this.id = id;
    this.type = type;
  }

  public CDOID getID()
  {
    return id;
  }

  public CDOClassRef getType()
  {
    return type;
  }
}

Back to the top