Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5b20c8293d5c68a0ed41e7c368784bfc6875e11f (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
/***************************************************************************
 * 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.Lifecycle;
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 Lifecycle implements ITypeManager
{
  private Repository repository;

  private boolean persistent;

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

  private PackageURIMap packageURIMap;

  private ObjectTypeMap objectTypeMap;

  private ObjectTypeMap metaObjectTypeMap;

  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)
    {
      type = loadTypeFromMap(id);
      if (type == null && storeReader != null)
      {
        type = storeReader.readObjectType(id);
      }

      if (type == null)
      {
        throw new ImplementationError("type == null");
      }

      objectTypes.put(id, type);
    }

    return type;
  }

  public void registerObjectType(CDOID id, CDOClassRefImpl type)
  {
    objectTypes.putIfAbsent(id, type);
  }

  private CDOClassRef loadTypeFromMap(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 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"));
      objectTypeMap = new ObjectTypeMap(new File(repositoryFolder, "object.types"));
      metaObjectTypeMap = new ObjectTypeMap(new File(repositoryFolder, "metaobject.types"));
    }
  }

  @Override
  protected void doDeactivate() throws Exception
  {
    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 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);
    }
  }
}

Back to the top