forked from hazelcast/hazelcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PortableContextImpl.java
292 lines (251 loc) · 11.2 KB
/
PortableContextImpl.java
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
/*
* Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.internal.serialization.impl;
import com.hazelcast.core.ManagedContext;
import com.hazelcast.internal.serialization.InternalSerializationService;
import com.hazelcast.internal.serialization.PortableContext;
import com.hazelcast.nio.Bits;
import com.hazelcast.nio.BufferObjectDataInput;
import com.hazelcast.nio.serialization.ClassDefinition;
import com.hazelcast.nio.serialization.ClassDefinitionBuilder;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.nio.serialization.FieldDefinition;
import com.hazelcast.nio.serialization.FieldType;
import com.hazelcast.nio.serialization.HazelcastSerializationException;
import com.hazelcast.nio.serialization.Portable;
import com.hazelcast.util.ConcurrencyUtil;
import com.hazelcast.util.ConstructorFunction;
import java.io.IOException;
import java.nio.ByteOrder;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Pattern;
import static com.hazelcast.nio.Bits.combineToLong;
import static com.hazelcast.query.impl.getters.ExtractorHelper.extractAttributeNameNameWithoutArguments;
final class PortableContextImpl implements PortableContext {
private static final Pattern NESTED_FIELD_PATTERN = Pattern.compile("\\.");
private final int version;
private final ConcurrentHashMap<Integer, ClassDefinitionContext> classDefContextMap =
new ConcurrentHashMap<Integer, ClassDefinitionContext>();
private final InternalSerializationService serializationService;
private final ConstructorFunction<Integer, ClassDefinitionContext> constructorFunction =
new ConstructorFunction<Integer, ClassDefinitionContext>() {
public ClassDefinitionContext createNew(Integer arg) {
return new ClassDefinitionContext(arg);
}
};
PortableContextImpl(InternalSerializationService serializationService, int version) {
this.serializationService = serializationService;
this.version = version;
}
@Override
public int getClassVersion(int factoryId, int classId) {
return getClassDefContext(factoryId).getClassVersion(classId);
}
@Override
public void setClassVersion(int factoryId, int classId, int version) {
getClassDefContext(factoryId).setClassVersion(classId, version);
}
@Override
public ClassDefinition lookupClassDefinition(int factoryId, int classId, int version) {
return getClassDefContext(factoryId).lookup(classId, version);
}
@Override
public ClassDefinition lookupClassDefinition(Data data) throws IOException {
if (!data.isPortable()) {
throw new IllegalArgumentException("Data is not Portable!");
}
BufferObjectDataInput in = serializationService.createObjectDataInput(data);
int factoryId = in.readInt();
int classId = in.readInt();
int version = in.readInt();
ClassDefinition classDefinition = lookupClassDefinition(factoryId, classId, version);
if (classDefinition == null) {
classDefinition = readClassDefinition(in, factoryId, classId, version);
}
return classDefinition;
}
ClassDefinition readClassDefinition(BufferObjectDataInput in, int factoryId, int classId, int version)
throws IOException {
boolean register = true;
ClassDefinitionBuilder builder = new ClassDefinitionBuilder(factoryId, classId, version);
// final position after portable is read
in.readInt();
// field count
int fieldCount = in.readInt();
int offset = in.position();
for (int i = 0; i < fieldCount; i++) {
int pos = in.readInt(offset + i * Bits.INT_SIZE_IN_BYTES);
in.position(pos);
short len = in.readShort();
char[] chars = new char[len];
for (int k = 0; k < len; k++) {
chars[k] = (char) in.readUnsignedByte();
}
FieldType type = FieldType.get(in.readByte());
String name = new String(chars);
int fieldFactoryId = 0;
int fieldClassId = 0;
int fieldVersion = version;
if (type == FieldType.PORTABLE) {
// is null
if (in.readBoolean()) {
register = false;
}
fieldFactoryId = in.readInt();
fieldClassId = in.readInt();
// TODO: what there's a null inner Portable field
if (register) {
fieldVersion = in.readInt();
readClassDefinition(in, fieldFactoryId, fieldClassId, fieldVersion);
}
} else if (type == FieldType.PORTABLE_ARRAY) {
int k = in.readInt();
fieldFactoryId = in.readInt();
fieldClassId = in.readInt();
// TODO: what there's a null inner Portable field
if (k > 0) {
int p = in.readInt();
in.position(p);
fieldVersion = in.readInt();
readClassDefinition(in, fieldFactoryId, fieldClassId, fieldVersion);
} else {
register = false;
}
}
builder.addField(new FieldDefinitionImpl(i, name, type, fieldFactoryId, fieldClassId, fieldVersion));
}
ClassDefinition classDefinition = builder.build();
if (register) {
classDefinition = registerClassDefinition(classDefinition);
}
return classDefinition;
}
@Override
public ClassDefinition registerClassDefinition(final ClassDefinition cd) {
return getClassDefContext(cd.getFactoryId()).register(cd);
}
@Override
public ClassDefinition lookupOrRegisterClassDefinition(Portable p) throws IOException {
int portableVersion = SerializationUtil.getPortableVersion(p, version);
ClassDefinition cd = lookupClassDefinition(p.getFactoryId(), p.getClassId(), portableVersion);
if (cd == null) {
ClassDefinitionWriter writer = new ClassDefinitionWriter(this, p.getFactoryId(),
p.getClassId(), portableVersion);
p.writePortable(writer);
cd = writer.registerAndGet();
}
return cd;
}
@Override
// TODO cache the result
public FieldDefinition getFieldDefinition(ClassDefinition classDef, String name) {
FieldDefinition fd = classDef.getField(name);
if (fd == null) {
if (name.contains(".")) {
String[] fieldNames = NESTED_FIELD_PATTERN.split(name);
if (fieldNames.length <= 1) {
return fd;
}
ClassDefinition currentClassDef = classDef;
for (int i = 0; i < fieldNames.length; i++) {
fd = currentClassDef.getField(fieldNames[i]);
if (fd == null) {
fd = currentClassDef.getField(extractAttributeNameNameWithoutArguments(fieldNames[i]));
}
// This is not enough to fully implement issue: https://github.com/hazelcast/hazelcast/issues/3927
if (i == fieldNames.length - 1) {
break;
}
if (fd == null) {
throw new IllegalArgumentException("Unknown field: " + name);
}
currentClassDef = lookupClassDefinition(fd.getFactoryId(), fd.getClassId(),
fd.getVersion());
if (currentClassDef == null) {
throw new IllegalArgumentException("Not a registered Portable field: " + fd);
}
}
} else {
fd = classDef.getField(extractAttributeNameNameWithoutArguments(name));
}
}
return fd;
}
private ClassDefinitionContext getClassDefContext(int factoryId) {
return ConcurrencyUtil.getOrPutIfAbsent(classDefContextMap, factoryId, constructorFunction);
}
@Override
public int getVersion() {
return version;
}
@Override
public ManagedContext getManagedContext() {
return serializationService.getManagedContext();
}
@Override
public ByteOrder getByteOrder() {
return serializationService.getByteOrder();
}
private final class ClassDefinitionContext {
final int factoryId;
final ConcurrentMap<Long, ClassDefinition> versionedDefinitions = new ConcurrentHashMap<Long, ClassDefinition>();
final ConcurrentMap<Integer, Integer> currentClassVersions = new ConcurrentHashMap<Integer, Integer>();
private ClassDefinitionContext(int factoryId) {
this.factoryId = factoryId;
}
int getClassVersion(int classId) {
Integer version = currentClassVersions.get(classId);
return version != null ? version : -1;
}
void setClassVersion(int classId, int version) {
Integer current = currentClassVersions.putIfAbsent(classId, version);
if (current != null && current != version) {
throw new IllegalArgumentException("Class-id: " + classId + " is already registered!");
}
}
ClassDefinition lookup(int classId, int version) {
long versionedClassId = combineToLong(classId, version);
return versionedDefinitions.get(versionedClassId);
}
ClassDefinition register(ClassDefinition cd) {
if (cd == null) {
return null;
}
if (cd.getFactoryId() != factoryId) {
throw new HazelcastSerializationException("Invalid factory-id! " + factoryId + " -> " + cd);
}
if (cd instanceof ClassDefinitionImpl) {
final ClassDefinitionImpl cdImpl = (ClassDefinitionImpl) cd;
cdImpl.setVersionIfNotSet(getVersion());
}
final long versionedClassId = combineToLong(cd.getClassId(), cd.getVersion());
final ClassDefinition currentCd = versionedDefinitions.putIfAbsent(versionedClassId, cd);
if (currentCd == null) {
return cd;
}
if (currentCd instanceof ClassDefinitionImpl) {
if (!currentCd.equals(cd)) {
throw new HazelcastSerializationException(
"Incompatible class-definitions with same class-id: " + cd + " VS " + currentCd);
}
return currentCd;
}
versionedDefinitions.put(versionedClassId, cd);
return cd;
}
}
}