Skip to content

Commit 969cc4b

Browse files
author
mpv1989
committed
added support for replacing build-in VelocyPack serializer/deserializer
1 parent 56ad189 commit 969cc4b

File tree

14 files changed

+501
-227
lines changed

14 files changed

+501
-227
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ v4.1.11 (2017-03-xx)
66
* added connection pooling (issue #103)
77
* extracted VelocyPack implementation to https://github.com/arangodb/java-velocypack
88
* fixed NPE in ArangoCursor (issue #112)
9+
* added support for replacing build-in VelocyPack serializer/deserializer
910

1011
v4.1.10 (2017-02-22)
1112
---------------------------

src/main/java/com/arangodb/ArangoDB.java

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
import com.arangodb.internal.CollectionCache.DBAccess;
4242
import com.arangodb.internal.DocumentCache;
4343
import com.arangodb.internal.InternalArangoDB;
44+
import com.arangodb.internal.util.ArangoDeserializerImpl;
45+
import com.arangodb.internal.util.ArangoSerializerImpl;
46+
import com.arangodb.internal.util.ArangoUtilImpl;
4447
import com.arangodb.internal.velocypack.VPackDriverModule;
4548
import com.arangodb.internal.velocystream.Communication;
4649
import com.arangodb.internal.velocystream.CommunicationSync;
@@ -50,6 +53,9 @@
5053
import com.arangodb.model.LogOptions;
5154
import com.arangodb.model.UserCreateOptions;
5255
import com.arangodb.model.UserUpdateOptions;
56+
import com.arangodb.util.ArangoDeserializer;
57+
import com.arangodb.util.ArangoSerializer;
58+
import com.arangodb.util.ArangoUtil;
5359
import com.arangodb.velocypack.VPack;
5460
import com.arangodb.velocypack.VPackAnnotationFieldFilter;
5561
import com.arangodb.velocypack.VPackAnnotationFieldNaming;
@@ -84,15 +90,17 @@ public static class Builder {
8490
private Integer maxConnections;
8591
private final VPack.Builder vpackBuilder;
8692
private final CollectionCache collectionCache;
87-
private final VPackParser.Builder vpackParser;
93+
private final VPackParser.Builder vpackParserBuilder;
94+
private ArangoSerializer serializer;
95+
private ArangoDeserializer deserializer;
8896

8997
public Builder() {
9098
super();
9199
vpackBuilder = new VPack.Builder();
92100
collectionCache = new CollectionCache();
93-
vpackParser = new VPackParser.Builder();
101+
vpackParserBuilder = new VPackParser.Builder();
94102
vpackBuilder.registerModule(new VPackDriverModule(collectionCache));
95-
vpackParser.registerModule(new VPackDriverModule(collectionCache));
103+
vpackParserBuilder.registerModule(new VPackDriverModule(collectionCache));
96104
host = new Host(ArangoDBConstants.DEFAULT_HOST, ArangoDBConstants.DEFAULT_PORT);
97105
hosts = new ArrayList<Host>();
98106
loadProperties(ArangoDB.class.getResourceAsStream(DEFAULT_PROPERTY_FILE));
@@ -223,28 +231,28 @@ public <T> Builder registerInstanceCreator(final Class<T> clazz, final VPackInst
223231
}
224232

225233
public Builder registerJsonDeserializer(final ValueType type, final VPackJsonDeserializer deserializer) {
226-
vpackParser.registerDeserializer(type, deserializer);
234+
vpackParserBuilder.registerDeserializer(type, deserializer);
227235
return this;
228236
}
229237

230238
public Builder registerJsonDeserializer(
231239
final String attribute,
232240
final ValueType type,
233241
final VPackJsonDeserializer deserializer) {
234-
vpackParser.registerDeserializer(attribute, type, deserializer);
242+
vpackParserBuilder.registerDeserializer(attribute, type, deserializer);
235243
return this;
236244
}
237245

238246
public <T> Builder registerJsonSerializer(final Class<T> clazz, final VPackJsonSerializer<T> serializer) {
239-
vpackParser.registerSerializer(clazz, serializer);
247+
vpackParserBuilder.registerSerializer(clazz, serializer);
240248
return this;
241249
}
242250

243251
public <T> Builder registerJsonSerializer(
244252
final String attribute,
245253
final Class<T> clazz,
246254
final VPackJsonSerializer<T> serializer) {
247-
vpackParser.registerSerializer(attribute, clazz, serializer);
255+
vpackParserBuilder.registerSerializer(attribute, clazz, serializer);
248256
return this;
249257
}
250258

@@ -272,30 +280,47 @@ public Builder registerModules(final VPackModule... modules) {
272280
return this;
273281
}
274282

283+
public Builder setSerializer(final ArangoSerializer serializer) {
284+
this.serializer = serializer;
285+
return this;
286+
}
287+
288+
public Builder setDeserializer(final ArangoDeserializer deserializer) {
289+
this.deserializer = deserializer;
290+
return this;
291+
}
292+
275293
public ArangoDB build() {
276294
if (hosts.isEmpty()) {
277295
hosts.add(host);
278296
}
297+
final VPack vpacker = vpackBuilder.build();
298+
final VPack vpackerNull = vpackBuilder.serializeNullValues(true).build();
299+
final VPackParser vpackParser = vpackParserBuilder.build();
300+
if (serializer == null) {
301+
serializer = new ArangoSerializerImpl(vpacker, vpackerNull, vpackParser);
302+
}
303+
if (deserializer == null) {
304+
deserializer = new ArangoDeserializerImpl(vpackerNull, vpackParser);
305+
}
279306
return new ArangoDB(
280307
new CommunicationSync.Builder(new DefaultHostHandler(hosts)).timeout(timeout).user(user)
281308
.password(password).useSsl(useSsl).sslContext(sslContext).chunksize(chunksize)
282309
.maxConnections(maxConnections),
283-
vpackBuilder.build(), vpackBuilder.serializeNullValues(true).build(), vpackParser.build(),
284-
collectionCache);
310+
new ArangoUtilImpl(serializer, deserializer), collectionCache);
285311
}
286312

287313
}
288314

289-
public ArangoDB(final CommunicationSync.Builder commBuilder, final VPack vpack, final VPack vpackNull,
290-
final VPackParser vpackParser, final CollectionCache collectionCache) {
291-
super(new ArangoExecutorSync(commBuilder.build(vpack, collectionCache), vpack, vpackNull, vpackParser,
292-
new DocumentCache(), collectionCache));
293-
final Communication<Response, ConnectionSync> cacheCom = commBuilder.build(vpack, collectionCache);
315+
public ArangoDB(final CommunicationSync.Builder commBuilder, final ArangoUtil util,
316+
final CollectionCache collectionCache) {
317+
super(new ArangoExecutorSync(commBuilder.build(util, collectionCache), util, new DocumentCache(),
318+
collectionCache));
319+
final Communication<Response, ConnectionSync> cacheCom = commBuilder.build(util, collectionCache);
294320
collectionCache.init(new DBAccess() {
295321
@Override
296322
public ArangoDatabase db(final String name) {
297-
return new ArangoDatabase(cacheCom, vpackNull, vpack, vpackParser, executor.documentCache(), null,
298-
name);
323+
return new ArangoDatabase(cacheCom, util, executor.documentCache(), null, name);
299324
}
300325
});
301326
}

src/main/java/com/arangodb/ArangoDatabase.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@
5454
import com.arangodb.model.GraphCreateOptions;
5555
import com.arangodb.model.TransactionOptions;
5656
import com.arangodb.model.TraversalOptions;
57+
import com.arangodb.util.ArangoUtil;
5758
import com.arangodb.velocypack.Type;
58-
import com.arangodb.velocypack.VPack;
59-
import com.arangodb.velocypack.VPackParser;
6059
import com.arangodb.velocystream.Request;
6160
import com.arangodb.velocystream.Response;
6261

@@ -70,11 +69,9 @@ protected ArangoDatabase(final ArangoDB arangoDB, final String name) {
7069
super(arangoDB, arangoDB.executor(), name);
7170
}
7271

73-
protected ArangoDatabase(final Communication<Response, ConnectionSync> communication, final VPack vpacker,
74-
final VPack vpackerNull, final VPackParser vpackParser, final DocumentCache documentCache,
75-
final CollectionCache collectionCache, final String name) {
76-
super(null, new ArangoExecutorSync(communication, vpacker, vpackerNull, vpackParser, documentCache,
77-
collectionCache), name);
72+
protected ArangoDatabase(final Communication<Response, ConnectionSync> communication, final ArangoUtil util,
73+
final DocumentCache documentCache, final CollectionCache collectionCache, final String name) {
74+
super(null, new ArangoExecutorSync(communication, util, documentCache, collectionCache), name);
7875
}
7976

8077
/**

src/main/java/com/arangodb/internal/ArangoExecutor.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import com.arangodb.internal.velocystream.Communication;
3131
import com.arangodb.internal.velocystream.Connection;
3232
import com.arangodb.util.ArangoUtil;
33-
import com.arangodb.velocypack.VPack;
34-
import com.arangodb.velocypack.VPackParser;
3533
import com.arangodb.velocypack.VPackSlice;
3634
import com.arangodb.velocypack.exception.VPackException;
3735
import com.arangodb.velocystream.Response;
@@ -56,13 +54,13 @@ public static interface ResponseDeserializer<T> {
5654
private final CollectionCache collectionCache;
5755
private final ArangoUtil util;
5856

59-
protected ArangoExecutor(final Communication<R, C> communication, final VPack vpacker, final VPack vpackerNull,
60-
final VPackParser vpackParser, final DocumentCache documentCache, final CollectionCache collectionCache) {
57+
protected ArangoExecutor(final Communication<R, C> communication, final ArangoUtil util,
58+
final DocumentCache documentCache, final CollectionCache collectionCache) {
6159
super();
6260
this.communication = communication;
6361
this.documentCache = documentCache;
6462
this.collectionCache = collectionCache;
65-
util = new ArangoUtil(vpacker, vpackerNull, vpackParser);
63+
this.util = util;
6664
}
6765

6866
public Communication<R, C> communication() {

src/main/java/com/arangodb/internal/ArangoExecutorSync.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
import com.arangodb.ArangoDBException;
2626
import com.arangodb.internal.velocystream.Communication;
2727
import com.arangodb.internal.velocystream.ConnectionSync;
28-
import com.arangodb.velocypack.VPack;
29-
import com.arangodb.velocypack.VPackParser;
28+
import com.arangodb.util.ArangoUtil;
3029
import com.arangodb.velocypack.exception.VPackException;
3130
import com.arangodb.velocystream.Request;
3231
import com.arangodb.velocystream.Response;
@@ -37,10 +36,9 @@
3736
*/
3837
public class ArangoExecutorSync extends ArangoExecutor<Response, ConnectionSync> {
3938

40-
public ArangoExecutorSync(final Communication<Response, ConnectionSync> communication, final VPack vpacker,
41-
final VPack vpackerNull, final VPackParser vpackParser, final DocumentCache documentCache,
42-
final CollectionCache collectionCache) {
43-
super(communication, vpacker, vpackerNull, vpackParser, documentCache, collectionCache);
39+
public ArangoExecutorSync(final Communication<Response, ConnectionSync> communication, final ArangoUtil util,
40+
final DocumentCache documentCache, final CollectionCache collectionCache) {
41+
super(communication, util, documentCache, collectionCache);
4442
}
4543

4644
public <T> T execute(final Request request, final Type type) throws ArangoDBException {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.internal.util;
22+
23+
import java.lang.reflect.Type;
24+
25+
import com.arangodb.ArangoDBException;
26+
import com.arangodb.util.ArangoDeserializer;
27+
import com.arangodb.velocypack.VPack;
28+
import com.arangodb.velocypack.VPackParser;
29+
import com.arangodb.velocypack.VPackSlice;
30+
import com.arangodb.velocypack.exception.VPackException;
31+
32+
/**
33+
* @author Mark - mark at arangodb.com
34+
*
35+
*/
36+
public class ArangoDeserializerImpl implements ArangoDeserializer {
37+
38+
private final VPack vpacker;
39+
private final VPackParser vpackParser;
40+
41+
public ArangoDeserializerImpl(final VPack vpacker, final VPackParser vpackParser) {
42+
super();
43+
this.vpacker = vpacker;
44+
this.vpackParser = vpackParser;
45+
}
46+
47+
@Override
48+
@SuppressWarnings("unchecked")
49+
public <T> T deserialize(final VPackSlice vpack, final Type type) throws ArangoDBException {
50+
try {
51+
final T doc;
52+
if (type == String.class && !vpack.isString()) {
53+
doc = (T) vpackParser.toJson(vpack);
54+
} else {
55+
doc = vpacker.deserialize(vpack, type);
56+
}
57+
return doc;
58+
} catch (final VPackException e) {
59+
throw new ArangoDBException(e);
60+
}
61+
}
62+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.internal.util;
22+
23+
import java.lang.reflect.Type;
24+
import java.util.Iterator;
25+
import java.util.Map;
26+
27+
import com.arangodb.ArangoDBException;
28+
import com.arangodb.util.ArangoSerializer;
29+
import com.arangodb.velocypack.VPack;
30+
import com.arangodb.velocypack.VPackParser;
31+
import com.arangodb.velocypack.VPackSlice;
32+
import com.arangodb.velocypack.exception.VPackException;
33+
import com.arangodb.velocypack.exception.VPackParserException;
34+
35+
/**
36+
* @author Mark - mark at arangodb.com
37+
*
38+
*/
39+
public class ArangoSerializerImpl implements ArangoSerializer {
40+
41+
private final VPack vpacker;
42+
private final VPack vpackerNull;
43+
private final VPackParser vpackParser;
44+
45+
public ArangoSerializerImpl(final VPack vpacker, final VPack vpackerNull, final VPackParser vpackParser) {
46+
super();
47+
this.vpacker = vpacker;
48+
this.vpackerNull = vpackerNull;
49+
this.vpackParser = vpackParser;
50+
}
51+
52+
@Override
53+
public VPackSlice serialize(final Object entity) throws ArangoDBException {
54+
return serialize(entity, false);
55+
}
56+
57+
@Override
58+
public VPackSlice serialize(final Object entity, final boolean serializeNullValues) throws ArangoDBException {
59+
return serialize(entity, serializeNullValues, false);
60+
}
61+
62+
@Override
63+
@SuppressWarnings("unchecked")
64+
public VPackSlice serialize(final Object entity, final boolean serializeNullValues, final boolean stringAsJson)
65+
throws ArangoDBException {
66+
try {
67+
final VPackSlice vpack;
68+
final Class<? extends Object> type = entity.getClass();
69+
if (String.class.isAssignableFrom(type)) {
70+
vpack = vpackParser.fromJson((String) entity, serializeNullValues);
71+
} else if (stringAsJson && Iterable.class.isAssignableFrom(type)) {
72+
final Iterator<?> iterator = Iterable.class.cast(entity).iterator();
73+
if (iterator.hasNext() && String.class.isAssignableFrom(iterator.next().getClass())) {
74+
vpack = vpackParser.fromJson((Iterable<String>) entity, serializeNullValues);
75+
} else {
76+
final VPack vp = serializeNullValues ? vpackerNull : vpacker;
77+
vpack = vp.serialize(entity);
78+
}
79+
} else {
80+
final VPack vp = serializeNullValues ? vpackerNull : vpacker;
81+
vpack = vp.serialize(entity);
82+
}
83+
return vpack;
84+
} catch (final VPackException e) {
85+
throw new ArangoDBException(e);
86+
}
87+
}
88+
89+
@Override
90+
public VPackSlice serialize(final Object entity, final Type type) throws ArangoDBException {
91+
try {
92+
return vpacker.serialize(entity, type);
93+
} catch (final VPackException e) {
94+
throw new ArangoDBException(e);
95+
}
96+
}
97+
98+
@Override
99+
public VPackSlice serialize(final Object entity, final Type type, final boolean serializeNullValues)
100+
throws ArangoDBException {
101+
try {
102+
final VPack vp = serializeNullValues ? vpackerNull : vpacker;
103+
return vp.serialize(entity, type);
104+
} catch (final VPackException e) {
105+
throw new ArangoDBException(e);
106+
}
107+
}
108+
109+
@Override
110+
public VPackSlice serialize(final Object entity, final Type type, final Map<String, Object> additionalFields)
111+
throws ArangoDBException {
112+
try {
113+
return vpacker.serialize(entity, type, additionalFields);
114+
} catch (final VPackParserException e) {
115+
throw new ArangoDBException(e);
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)