Skip to main content
summaryrefslogtreecommitdiffstats
blob: ad5d5a89ea2e4ee3b350a18df08d04566d0c0300 (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
/***************************************************************************
 * Copyright (c) 2004 - 2008 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.common;

import org.eclipse.emf.cdo.common.CDODataOutput;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.id.CDOIDAndVersion;
import org.eclipse.emf.cdo.common.id.CDOIDMetaRange;
import org.eclipse.emf.cdo.common.id.CDOID.Type;
import org.eclipse.emf.cdo.common.model.CDOClass;
import org.eclipse.emf.cdo.common.model.CDOClassRef;
import org.eclipse.emf.cdo.common.model.CDOFeature;
import org.eclipse.emf.cdo.common.model.CDOPackage;
import org.eclipse.emf.cdo.common.model.CDOPackageURICompressor;
import org.eclipse.emf.cdo.common.model.CDOType;
import org.eclipse.emf.cdo.common.revision.CDOList;
import org.eclipse.emf.cdo.common.revision.CDORevision;
import org.eclipse.emf.cdo.common.revision.CDORevisionUtil;
import org.eclipse.emf.cdo.common.revision.delta.CDOFeatureDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDORevisionDelta;
import org.eclipse.emf.cdo.internal.common.bundle.OM;
import org.eclipse.emf.cdo.internal.common.id.CDOIDAndVersionImpl;
import org.eclipse.emf.cdo.internal.common.model.CDOClassRefImpl;
import org.eclipse.emf.cdo.internal.common.model.CDOTypeImpl;
import org.eclipse.emf.cdo.internal.common.revision.delta.CDOFeatureDeltaImpl;
import org.eclipse.emf.cdo.internal.common.revision.delta.CDORevisionDeltaImpl;
import org.eclipse.emf.cdo.spi.common.AbstractCDOID;
import org.eclipse.emf.cdo.spi.common.InternalCDOClass;
import org.eclipse.emf.cdo.spi.common.InternalCDOFeature;
import org.eclipse.emf.cdo.spi.common.InternalCDOPackage;
import org.eclipse.emf.cdo.spi.common.InternalCDORevision;

import org.eclipse.net4j.util.concurrent.RWLockManager;
import org.eclipse.net4j.util.io.ExtendedDataOutput;
import org.eclipse.net4j.util.om.trace.ContextTracer;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Eike Stepper
 */
public abstract class CDODataOutputImpl implements CDODataOutput
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG_PROTOCOL, CDODataOutputImpl.class);

  private static final Map<Class<?>, CDOType> idTypes = new HashMap<Class<?>, CDOType>();

  private ExtendedDataOutput out;

  static
  {
    idTypes.put(String.class, CDOType.STRING);
    idTypes.put(Boolean.class, CDOType.BOOLEAN);
    idTypes.put(Date.class, CDOType.DATE);
    idTypes.put(Long.class, CDOType.LONG);
    idTypes.put(Integer.class, CDOType.INT);
    idTypes.put(Double.class, CDOType.DOUBLE);
    idTypes.put(Byte.class, CDOType.BYTE);
    idTypes.put(Character.class, CDOType.CHAR);
    idTypes.put(Float.class, CDOType.FLOAT);
  }

  public CDODataOutputImpl(ExtendedDataOutput out)
  {
    this.out = out;
  }

  public ExtendedDataOutput getDelegate()
  {
    return out;
  }

  public void write(byte[] b, int off, int len) throws IOException
  {
    out.write(b, off, len);
  }

  public void write(byte[] b) throws IOException
  {
    out.write(b);
  }

  public void write(int b) throws IOException
  {
    out.write(b);
  }

  public void writeBoolean(boolean v) throws IOException
  {
    out.writeBoolean(v);
  }

  public void writeByte(int v) throws IOException
  {
    out.writeByte(v);
  }

  public void writeByteArray(byte[] b) throws IOException
  {
    out.writeByteArray(b);
  }

  public void writeBytes(String s) throws IOException
  {
    out.writeBytes(s);
  }

  public void writeChar(int v) throws IOException
  {
    out.writeChar(v);
  }

  public void writeChars(String s) throws IOException
  {
    out.writeChars(s);
  }

  public void writeDouble(double v) throws IOException
  {
    out.writeDouble(v);
  }

  public void writeFloat(float v) throws IOException
  {
    out.writeFloat(v);
  }

  public void writeInt(int v) throws IOException
  {
    out.writeInt(v);
  }

  public void writeLong(long v) throws IOException
  {
    out.writeLong(v);
  }

  public void writeObject(Object object) throws IOException
  {
    out.writeObject(object);
  }

  public void writeShort(int v) throws IOException
  {
    out.writeShort(v);
  }

  public void writeString(String str) throws IOException
  {
    out.writeString(str);
  }

  public void writeUTF(String str) throws IOException
  {
    out.writeUTF(str);
  }

  public void writeCDOType(CDOType cdoType) throws IOException
  {
    ((CDOTypeImpl)cdoType).write(this);
  }

  public void writeCDOPackageURI(String uri) throws IOException
  {
    getPackageURICompressor().writePackageURI(this, uri);
  }

  public void writeCDOClassRef(CDOClassRef cdoClassRef) throws IOException
  {
    ((CDOClassRefImpl)cdoClassRef).write(this);
  }

  public void writeCDOClassRef(CDOClass cdoClass) throws IOException
  {
    writeCDOClassRef(cdoClass.createClassRef());
  }

  public void writeCDOPackage(CDOPackage cdoPackage) throws IOException
  {
    ((InternalCDOPackage)cdoPackage).write(this);
  }

  public void writeCDOClass(CDOClass cdoClass) throws IOException
  {
    ((InternalCDOClass)cdoClass).write(this);
  }

  public void writeCDOFeature(CDOFeature cdoFeature) throws IOException
  {
    ((InternalCDOFeature)cdoFeature).write(this);
  }

  public void writeCDOID(CDOID id) throws IOException
  {
    if (id == null)
    {
      id = CDOID.NULL;
    }

    Type type = id.getType();
    int ordinal = type.ordinal();
    if (TRACER.isEnabled())
    {
      TRACER.format("Writing CDOID of type {0} ({1})", ordinal, type);
    }

    writeByte(ordinal);
    ((AbstractCDOID)id).write(this);
  }

  public void writeCDOIDAndVersion(CDOIDAndVersion idAndVersion) throws IOException
  {
    ((CDOIDAndVersionImpl)idAndVersion).write(this);
  }

  public void writeCDOIDMetaRange(CDOIDMetaRange metaRange) throws IOException
  {
    if (metaRange == null)
    {
      writeBoolean(false);
    }
    else
    {
      writeBoolean(true);
      writeCDOID(metaRange.getLowerBound());
      writeInt(metaRange.size());
    }
  }

  public void writeCDORevision(CDORevision revision, int referenceChunk) throws IOException
  {
    if (revision != null)
    {
      writeBoolean(true);
      ((InternalCDORevision)revision).write(this, referenceChunk);
    }
    else
    {
      writeBoolean(false);
    }
  }

  public void writeCDOList(CDOList list, CDOFeature feature, int referenceChunk) throws IOException
  {
    // TODO Simon: Could most of this stuff be moved into the list?
    // (only if protected methods of this class don't need to become public)
    int size = list == null ? 0 : list.size();
    if (size > 0)
    {
      // Need to adjust the referenceChunk in case where we do not have enough value in the list.
      // Even if the referenceChunk is specified, a provider of data could have override that value.
      int sizeToLook = referenceChunk == CDORevision.UNCHUNKED ? size : Math.min(referenceChunk, size);
      for (int i = 0; i < sizeToLook; i++)
      {
        Object element = list.get(i, false);
        if (element == CDORevisionUtil.UNINITIALIZED)
        {
          referenceChunk = i;
          break;
        }
      }
    }

    if (referenceChunk != CDORevision.UNCHUNKED && referenceChunk < size)
    {
      // This happens only on server-side
      if (TRACER.isEnabled())
      {
        TRACER.format("Writing feature {0}: size={1}, referenceChunk={2}", feature, size, referenceChunk);
      }

      out.writeInt(-size);
      out.writeInt(referenceChunk);
      size = referenceChunk;
    }
    else
    {
      if (TRACER.isEnabled())
      {
        TRACER.format("Writing feature {0}: size={1}", feature, size);
      }

      out.writeInt(size);
    }

    for (int j = 0; j < size; j++)
    {
      Object value = list.get(j, false);
      if (value != null && feature.isReference())
      {
        value = getIDProvider().provideCDOID(value);
      }

      if (TRACER.isEnabled())
      {
        TRACER.trace("    " + value);
      }

      feature.getType().writeValue(this, value);
    }
  }

  public void writeCDORevisionDelta(CDORevisionDelta revisionDelta) throws IOException
  {
    ((CDORevisionDeltaImpl)revisionDelta).write(this);
  }

  public void writeCDOFeatureDelta(CDOFeatureDelta featureDelta, CDOClass cdoClass) throws IOException
  {
    ((CDOFeatureDeltaImpl)featureDelta).write(this, cdoClass);
  }

  public void writeCDORevisionOrPrimitive(Object value) throws IOException
  {
    if (value == null)
    {
      value = CDOID.NULL;
    }
    else if (value instanceof CDORevision)
    {
      value = ((CDORevision)value).getID();
    }

    CDOType type = null;
    if (value instanceof CDOID)
    {
      if (((CDOID)value).isTemporary())
      {
        throw new IllegalArgumentException("Temporary ID not supported: " + value);
      }

      type = CDOType.OBJECT;
    }
    else
    {
      type = idTypes.get(value.getClass());
      if (type == null)
      {
        throw new IllegalArgumentException("No type for object " + value.getClass());
      }
    }

    writeCDOType(type);
    type.writeValue(this, value);
  }

  public void writeCDORevisionOrPrimitiveOrClass(Object value) throws IOException
  {
    if (value instanceof CDOClass)
    {
      writeBoolean(true);
      writeCDOClassRef(((CDOClass)value).createClassRef());
    }
    else
    {
      writeBoolean(false);
      writeCDORevisionOrPrimitive(value);
    }
  }

  public void writeCDOLockType(RWLockManager.LockType lockType) throws IOException
  {
    writeBoolean(lockType == RWLockManager.LockType.WRITE ? true : false);
  }

  protected abstract CDOPackageURICompressor getPackageURICompressor();
}

Back to the top