Skip to content

Commit 77b8d32

Browse files
author
Mark
committed
added ArangoUtil for de-/serialization
1 parent 3fd2290 commit 77b8d32

File tree

7 files changed

+217
-98
lines changed

7 files changed

+217
-98
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.arangodb.internal;
2222

2323
import com.arangodb.internal.velocystream.Connection;
24+
import com.arangodb.util.ArangoUtil;
2425

2526
/**
2627
* @author Mark - mark at arangodb.com
@@ -35,4 +36,7 @@ public ArangoExecuteable(final E executor) {
3536
this.executor = executor;
3637
}
3738

39+
public ArangoUtil util() {
40+
return executor.util();
41+
}
3842
}

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

Lines changed: 17 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@
2222

2323
import java.lang.reflect.Type;
2424
import java.util.Map;
25-
import java.util.concurrent.Future;
2625
import java.util.regex.Pattern;
2726

2827
import com.arangodb.ArangoDBException;
2928
import com.arangodb.internal.velocystream.Communication;
3029
import com.arangodb.internal.velocystream.Connection;
30+
import com.arangodb.util.ArangoUtil;
3131
import com.arangodb.velocypack.VPack;
3232
import com.arangodb.velocypack.VPackParser;
3333
import com.arangodb.velocypack.VPackSlice;
3434
import com.arangodb.velocypack.exception.VPackException;
35-
import com.arangodb.velocypack.exception.VPackParserException;
3635
import com.arangodb.velocystream.Response;
3736

3837
/**
@@ -48,40 +47,24 @@ public static interface ResponseDeserializer<T> {
4847
private static final String REGEX_DOCUMENT_KEY = "[^/]+";
4948
private static final String REGEX_DOCUMENT_ID = "[^/]+/[^/]+";
5049

51-
protected final Communication<R, C> communication;
52-
protected final VPack vpacker;
53-
protected final VPack vpackerNull;
54-
protected final VPackParser vpackParser;
55-
protected final DocumentCache documentCache;
56-
protected final CollectionCache collectionCache;
50+
private final Communication<R, C> communication;
51+
private final DocumentCache documentCache;
52+
private final CollectionCache collectionCache;
53+
private final ArangoUtil util;
5754

5855
protected ArangoExecutor(final Communication<R, C> communication, final VPack vpacker, final VPack vpackerNull,
5956
final VPackParser vpackParser, final DocumentCache documentCache, final CollectionCache collectionCache) {
6057
super();
6158
this.communication = communication;
62-
this.vpacker = vpacker;
63-
this.vpackerNull = vpackerNull;
64-
this.vpackParser = vpackParser;
6559
this.documentCache = documentCache;
6660
this.collectionCache = collectionCache;
61+
util = new ArangoUtil(vpacker, vpackerNull, vpackParser);
6762
}
6863

6964
public Communication<R, C> communication() {
7065
return communication;
7166
}
7267

73-
protected VPack vpack() {
74-
return vpacker;
75-
}
76-
77-
protected VPack vpackNull() {
78-
return vpackerNull;
79-
}
80-
81-
protected VPackParser vpackParser() {
82-
return vpackParser;
83-
}
84-
8568
public DocumentCache documentCache() {
8669
return documentCache;
8770
}
@@ -90,6 +73,10 @@ protected CollectionCache collectionCache() {
9073
return collectionCache;
9174
}
9275

76+
protected ArangoUtil util() {
77+
return util;
78+
}
79+
9380
protected String createPath(final String... params) {
9481
final StringBuilder sb = new StringBuilder();
9582
for (int i = 0; i < params.length; i++) {
@@ -117,94 +104,34 @@ protected void validateName(final String type, final String regex, final CharSeq
117104
}
118105

119106
@SuppressWarnings("unchecked")
120-
protected <T> T createResult(
121-
final VPack vpack,
122-
final VPackParser vpackParser,
123-
final Type type,
124-
final Response response) {
107+
protected <T> T createResult(final Type type, final Response response) {
125108
return (T) ((type != Void.class && response.getBody() != null) ? deserialize(response.getBody(), type) : null);
126109
}
127110

128-
@SuppressWarnings("unchecked")
129111
protected <T> T deserialize(final VPackSlice vpack, final Type type) throws ArangoDBException {
130-
try {
131-
final T doc;
132-
if (type == String.class && !vpack.isString()) {
133-
doc = (T) vpackParser.toJson(vpack);
134-
} else {
135-
doc = vpacker.deserialize(vpack, type);
136-
}
137-
return doc;
138-
} catch (final VPackException e) {
139-
throw new ArangoDBException(e);
140-
}
112+
return util.deserialize(vpack, type);
141113
}
142114

143115
protected VPackSlice serialize(final Object entity) throws ArangoDBException {
144-
try {
145-
final VPackSlice vpack;
146-
if (String.class.isAssignableFrom(entity.getClass())) {
147-
vpack = vpackParser.fromJson((String) entity);
148-
} else {
149-
vpack = vpacker.serialize(entity);
150-
}
151-
return vpack;
152-
} catch (final VPackException e) {
153-
throw new ArangoDBException(e);
154-
}
116+
return util.serialize(entity);
155117
}
156118

157119
protected VPackSlice serialize(final Object entity, final boolean serializeNullValues) throws ArangoDBException {
158-
try {
159-
final VPackSlice vpack;
160-
if (String.class.isAssignableFrom(entity.getClass())) {
161-
vpack = vpackParser.fromJson((String) entity, serializeNullValues);
162-
} else {
163-
final VPack vp = serializeNullValues ? vpackerNull : vpacker;
164-
vpack = vp.serialize(entity);
165-
}
166-
return vpack;
167-
} catch (final VPackException e) {
168-
throw new ArangoDBException(e);
169-
}
120+
return util.serialize(entity, serializeNullValues);
170121
}
171122

172123
protected VPackSlice serialize(final Object entity, final Type type) throws ArangoDBException {
173-
try {
174-
return vpacker.serialize(entity, type);
175-
} catch (final VPackException e) {
176-
throw new ArangoDBException(e);
177-
}
124+
return util.serialize(entity, type);
178125
}
179126

180127
protected VPackSlice serialize(final Object entity, final Type type, final boolean serializeNullValues)
181128
throws ArangoDBException {
182-
try {
183-
final VPack vp = serializeNullValues ? vpackerNull : vpacker;
184-
return vp.serialize(entity, type);
185-
} catch (final VPackException e) {
186-
throw new ArangoDBException(e);
187-
}
129+
return util.serialize(entity, type, serializeNullValues);
188130
}
189131

190132
protected VPackSlice serialize(final Object entity, final Type type, final Map<String, Object> additionalFields)
191133
throws ArangoDBException {
192-
try {
193-
return vpacker.serialize(entity, type, additionalFields);
194-
} catch (final VPackParserException e) {
195-
throw new ArangoDBException(e);
196-
}
134+
return util.serialize(entity, type, additionalFields);
197135
}
198136

199-
protected <T> T unwrap(final Future<T> future) throws ArangoDBException {
200-
try {
201-
return future.get();
202-
} catch (final Exception e) {
203-
final Throwable cause = e.getCause();
204-
if (cause != null && ArangoDBException.class.isAssignableFrom(cause.getClass())) {
205-
throw ArangoDBException.class.cast(cause);
206-
}
207-
throw new ArangoDBException(e);
208-
}
209-
}
210137
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ public <T> T execute(final Request request, final Type type) throws ArangoDBExce
4747
return execute(request, new ResponseDeserializer<T>() {
4848
@Override
4949
public T deserialize(final Response response) throws VPackException {
50-
return createResult(vpacker, vpackParser, type, response);
50+
return createResult(type, response);
5151
}
5252
});
5353
}
5454

5555
public <T> T execute(final Request request, final ResponseDeserializer<T> responseDeserializer)
5656
throws ArangoDBException {
5757
try {
58-
final Response response = communication.execute(request);
58+
final Response response = communication().execute(request);
5959
return responseDeserializer.deserialize(response);
6060
} catch (final VPackException e) {
6161
throw new ArangoDBException(e);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public DocumentCreateEntity<T> deserialize(final Response response) throws VPack
106106
values.put(DocumentField.Type.ID, doc.getId());
107107
values.put(DocumentField.Type.KEY, doc.getKey());
108108
values.put(DocumentField.Type.REV, doc.getRev());
109-
executor.documentCache.setValues(value, values);
109+
executor.documentCache().setValues(value, values);
110110
return doc;
111111
}
112112
};
@@ -198,7 +198,7 @@ public DocumentUpdateEntity<T> deserialize(final Response response) throws VPack
198198
}
199199
final Map<DocumentField.Type, String> values = new HashMap<DocumentField.Type, String>();
200200
values.put(DocumentField.Type.REV, doc.getRev());
201-
executor.documentCache.setValues(value, values);
201+
executor.documentCache().setValues(value, values);
202202
return doc;
203203
}
204204
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public EdgeEntity deserialize(final Response response) throws VPackException {
8585
values.put(DocumentField.Type.ID, doc.getId());
8686
values.put(DocumentField.Type.KEY, doc.getKey());
8787
values.put(DocumentField.Type.REV, doc.getRev());
88-
executor.documentCache.setValues(value, values);
88+
executor.documentCache().setValues(value, values);
8989
return doc;
9090
}
9191
};
@@ -127,7 +127,7 @@ public EdgeUpdateEntity deserialize(final Response response) throws VPackExcepti
127127
final EdgeUpdateEntity doc = executor.deserialize(body, EdgeUpdateEntity.class);
128128
final Map<DocumentField.Type, String> values = new HashMap<DocumentField.Type, String>();
129129
values.put(DocumentField.Type.REV, doc.getRev());
130-
executor.documentCache.setValues(value, values);
130+
executor.documentCache().setValues(value, values);
131131
return doc;
132132
}
133133
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public VertexEntity deserialize(final Response response) throws VPackException {
9090
values.put(DocumentField.Type.ID, doc.getId());
9191
values.put(DocumentField.Type.KEY, doc.getKey());
9292
values.put(DocumentField.Type.REV, doc.getRev());
93-
executor.documentCache.setValues(value, values);
93+
executor.documentCache().setValues(value, values);
9494
return doc;
9595
}
9696
};
@@ -132,7 +132,7 @@ public VertexUpdateEntity deserialize(final Response response) throws VPackExcep
132132
final VertexUpdateEntity doc = executor.deserialize(body, VertexUpdateEntity.class);
133133
final Map<DocumentField.Type, String> values = new HashMap<DocumentField.Type, String>();
134134
values.put(DocumentField.Type.REV, doc.getRev());
135-
executor.documentCache.setValues(value, values);
135+
executor.documentCache().setValues(value, values);
136136
return doc;
137137
}
138138
};

0 commit comments

Comments
 (0)