8000 vpack · anderick/arangodb-java-driver@a377ab1 · GitHub
[go: up one dir, main page]

Skip to content

Commit a377ab1

Browse files
author
Mark
committed
vpack
1 parent 536f406 commit a377ab1

File tree

9 files changed

+274
-157
lines changed

9 files changed

+274
-157
lines changed

src/main/java/com/arangodb/entity/EntityFactory.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.arangodb.entity;
1818

1919
import java.util.Collection;
20+
import java.util.HashMap;
2021
import java.util.Map;
2122

2223
import com.arangodb.ArangoException;
@@ -183,10 +184,22 @@ public static <T extends Map<K, C>, K, C> T createEntity(
183184
}
184185

185186
public static <T> VPackSlice toVPack(final T obj) throws ArangoException {
187+
return toVPack(obj, new HashMap<String, Object>());
188+
}
189+
190+
public static <T> VPackSlice toVPack(final T obj, final Map<String, Object> additionalFields)
191+
throws ArangoException {
186192
return toVPack(obj, INCLUDE_NULL_VALUE_DEFAULT);
187193
}
188194

189195
public static <T> VPackSlice toVPack(final T obj, final boolean includeNullValue) throws ArangoException {
196+
return toVPack(obj, includeNullValue, new HashMap<String, Object>());
197+
}
198+
199+
public static <T> VPackSlice toVPack(
200+
final T obj,
201+
final boolean includeNullValue,
202+
final Map<String, Object> additionalFields) throws ArangoException {
190203
try {
191204
return includeNullValue ? EntityFactory.vpackNull.serialize(obj) : EntityFactory.vpack.serialize(obj);
192205
} catch (final VPackParserException e) {
@@ -219,6 +232,40 @@ public static <T> VPackSlice toVPack(final T obj, final boolean includeNullValue
219232
}
220233
}
221234

235+
public static <T> VPackSlice toEdgeVPack(
236+
final String key,
237+
final String fromHandle,
238+
final String toHandle,
239+
final T value) throws ArangoException {
240+
final Map<String, Object> additionalFields = new HashMap<String, Object>();
241+
if (key != null) {
242+
additionalFields.put(BaseDocument.KEY, key);
243+
}
244+
if (fromHandle != null) {
245+
additionalFields.put(BaseDocument.FROM, fromHandle);
246+
}
247+
if (toHandle != null) {
248+
additionalFields.put(BaseDocument.TO, toHandle);
249+
}
250+
final VPackSlice vpack = EntityFactory.toVPack(value, additionalFields);
251+
if (!vpack.isObject()) {
252+
throw new ArangoException("edge need object type(not support array, primitive, etc..).");
253+
}
254+
return vpack;
255+
}
256+
257+
public static <T> VPackSlice toDocumentVPack(final String key, final T value) throws ArangoException {
258+
final Map<String, Object> additionalFields = new HashMap<String, Object>();
259+
if (key != null) {
260+
additionalFields.put(BaseDocument.KEY, key);
261+
}
262+
final VPackSlice vpack = EntityFactory.toVPack(value, additionalFields);
263+
if (!vpack.isObject()) {
264+
throw new ArangoException("vertex need object type(not support array, primitive, etc..).");
265+
}
266+
return vpack;
267+
}
268+
222269
public static String toJson(final VPackSlice vpack) {
223270
return VPackParser.toJson(vpack, INCLUDE_NULL_VALUE_DEFAULT);
224271
}

src/main/java/com/arangodb/http/HttpManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
public class HttpManager {
8888

8989
private static final String USERAGENT = "Mozilla/5.0 (compatible; ArangoDB-JavaDriver/1.1; +http://mt.orz.at/)";
90-
private static final ContentType CONTENT_TYPE = ContentType.create("application/x-velocypack");
90+
public static final ContentType CONTENT_TYPE = ContentType.create("application/x-velocypack");
9191

9292
private static Logger logger = LoggerFactory.getLogger(HttpManager.class);
9393

src/main/java/com/arangodb/impl/InternalBatchDriverImpl.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
import com.arangodb.entity.BatchResponseEntity;
2929
import com.arangodb.entity.BatchResponseListEntity;
3030
import com.arangodb.entity.DefaultEntity;
31+
import com.arangodb.entity.EntityFactory;
3132
import com.arangodb.http.BatchPart;
3233
import com.arangodb.http.HttpManager;
3334
import com.arangodb.http.HttpResponseEntity;
3435
import com.arangodb.http.InvocationObject;
36+
import com.arangodb.velocypack.VPackSlice;
3537

3638
/**
3739
* @author Florian Bartels
@@ -69,11 +71,12 @@ public DefaultEntity executeBatch(final List<BatchPart> callStack, final String
6971
headers.put("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
7072

7173
final HttpResponseEntity res = httpManager.doPostWithHeaders(createEndpointUrl(defaultDataBase, "/_api/batch"),
72-
null, null, headers, sb.toString());
74+
null, null, headers, EntityFactory.toVPack(sb.toString()));
7375

74-
final String data = res.getText();
75-
res.setContentType("application/json");
76-
res.setText("");
76+
final VPackSlice data = res.getContent();
77+
78+
res.setContentType(HttpManager.CONTENT_TYPE.getMimeType());
79+
res.setContent(null);
7780
final List<BatchResponseEntity> batchResponseEntityList = handleResponse(resolver, data);
7881
batchResponseListEntity = new BatchResponseListEntity();
7982
batchResponseListEntity.setBatchResponseEntities(batchResponseEntityList);
@@ -84,13 +87,16 @@ public BatchResponseListEntity getBatchResponseListEntity() {
8487
return batchResponseListEntity;
8588
}
8689

87-
private List<BatchResponseEntity> handleResponse(final Map<String, InvocationObject> resolver, final String data) {
90+
private List<BatchResponseEntity> handleResponse(
91+
final Map<String, InvocationObject> resolver,
92+
final VPackSlice data) throws ArangoException {
8893
String currentId = null;
8994
Boolean fetchText = false;
9095
final List<BatchResponseEntity> batchResponseEntityList = new ArrayList<BatchResponseEntity>();
9196
BatchResponseEntity batchResponseEntity = new BatchResponseEntity(null);
9297
final StringBuilder sb = new StringBuilder();
93-
for (final String line : data.split(newline)) {
98+
final String stringData = data.getAsString();
99+
for (final String line : stringData.split(newline)) {
94100
line.trim();
95101
line.replaceAll("\r", "");
96102
if (line.indexOf("Content-Id") != -1) {
@@ -122,9 +128,10 @@ private List<BatchResponseEntity> handleResponse(final Map<String, InvocationObj
122128
return batchResponseEntityList;
123129
}
124130

125-
private void copyResponseToEntity(final BatchResponseEntity batchResponseEntity, final StringBuilder sb) {
131+
private void copyResponseToEntity(final BatchResponseEntity batchResponseEntity, final StringBuilder sb)
132+
throws ArangoException {
126133
if (!batchResponseEntity.getHttpResponseEntity().isDumpResponse()) {
127-
batchResponseEntity.getHttpResponseEntity().setText(sb.toString());
134+
batchResponseEntity.getHttpResponseEntity().setContent(EntityFactory.toVPack(sb.toString()));
128135
} else {
129136
final InputStream is = new ByteArrayInputStream(sb.toString().getBytes());
130137
batchResponseEntity.getHttpResponseEntity().setStream(is);

src/main/java/com/arangodb/impl/InternalDocumentDriverImpl.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import com.arangodb.entity.EntityFactory;
3030
import com.arangodb.http.HttpManager;
3131
import com.arangodb.http.HttpResponseEntity;
32-
import com.arangodb.util.EdgeUtils;
3332
import com.arangodb.util.MapBuilder;
3433
import com.arangodb.velocypack.VPackSlice;
3534

@@ -57,15 +56,9 @@ private <T> DocumentEntity<T> internalCreateDocument(
5756

5857
VPackSlice body;
5958
if (raw) {
60-
body = value.toString();
61-
} else if (documentKey != null) {
62-
final JsonElement elem = EntityFactory.toJsonElement(value, false);
63-
if (elem.isJsonObject()) {
64-
elem.getAsJsonObject().addProperty(BaseDocument.KEY, documentKey);
65-
}
66-
body = EntityFactory.toVPack(elem);
59+
body = EntityFactory.toVPack(value.toString());
6760
} else {
68-
body = EntityFactory.toVPack(value);
61+
body = EntityFactory.toDocumentVPack(documentKey, value);
6962
}
7063

7164
final HttpResponseEntity res = httpManager.doPost(createDocumentEndpointUrl(database),
@@ -129,9 +122,10 @@ public DocumentEntity<String> replaceDocumentRaw(
129122
final String rev,
130123
final Boolean waitForSync) throws ArangoException {
131124

125+
final VPackSlice obj = EntityFactory.toVPack(rawJsonString);
132126
validateDocumentHandle(documentHandle);
133127
final HttpResponseEntity res = httpManager.doPut(createDocumentEndpointUrl(database, documentHandle),
134-
createRevisionCheckHeader(rev), new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).get(), rawJsonString);
128+
createRevisionCheckHeader(rev), new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).get(), obj);
135129

136130
@SuppressWarnings("unchecked")
137131
final DocumentEntity<String> result = createEntity(res, DocumentEntity.class);
@@ -171,10 +165,11 @@ public DocumentEntity<String> updateDocumentRaw(
171165
final Boolean waitForSync,
172166
final Boolean keepNull) throws ArangoException {
173167

168+
final VPackSlice obj = EntityFactory.toVPack(rawJsonString);
174169
validateDocumentHandle(documentHandle);
175170
final HttpResponseEntity res = httpManager.doPatch(createDocumentEndpointUrl(database, documentHandle),
176171
createRevisionCheckHeader(rev),
177-
new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).put("keepNull", keepNull).get(), rawJsonString);
172+
new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).put("keepNull", keepNull).get(), obj);
178173

179174
@SuppressWarnings("unchecked")
180175
final DocumentEntity<String> result = createEntity(res, DocumentEntity.class);
@@ -277,11 +272,10 @@ public <T> EdgeEntity<T> createEdge(
277272

278273
validateCollectionName(collectionName);
279274

280-
final JsonObject obj = EdgeUtils.valueToEdgeJsonObject(documentKey, fromHandle, toHandle, value);
275+
final VPackSlice obj = EntityFactory.toEdgeVPack(documentKey, fromHandle, toHandle, value);
281276

282277
final HttpResponseEntity res = httpManager.doPost(createDocumentEndpointUrl(database),
283-
new MapBuilder().put(COLLECTION, collectionName).put(WAIT_FOR_SYNC, waitForSync).get(),
284-
EntityFactory.toVPack(obj));
278+
new MapBuilder().put(COLLECTION, collectionName).put(WAIT_FOR_SYNC, waitForSync).get(), obj);
285279

286280
@SuppressWarnings("unchecked")
287281
final EdgeEntity<T> entity = createEntity(res, EdgeEntity.class);

src/main/java/com/arangodb/impl/InternalGraphDriverImpl.java

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
import com.arangodb.http.HttpManager;
3535
import com.arangodb.http.HttpResponseEntity;
3636
import com.arangodb.util.CollectionUtils;
37-
import com.arangodb.util.EdgeUtils;
3837
import com.arangodb.util.MapBuilder;
3938
import com.arangodb.util.StringUtils;
39+
import com.arangodb.velocypack.VPackSlice;
4040

4141
/**
4242
* @author tamtam180 - kirscheless at gmail.com
@@ -349,26 +349,13 @@ public <T> VertexEntity<T> createVertex(
349349
final T vertex,
350350
final Boolean waitForSync) throws ArangoException {
351351

352-
JsonObject obj;
353-
if (vertex == null) {
354-
obj = new JsonObject();
355-
} else {
356-
final JsonElement elem = EntityFactory.toJsonElement(vertex, false);
357-
if (elem.isJsonObject()) {
358-
obj = elem.getAsJsonObject();
359-
} else {
360-
throw new IllegalArgumentException("vertex need object type(not support array, primitive, etc..).");
361-
}
362-
}
363-
if (key != null) {
364-
obj.addProperty("_key", key);
365-
}
366-
352+
final VPackSlice obj = EntityFactory.toDocumentVPack(key, vertex);
367353
validateCollectionName(graphName);
368-
final HttpResponseEntity res = httpManager.doPost(
369-
createGharialEndpointUrl(database, StringUtils.encodeUrl(graphName), VERTEX,
370-
StringUtils.encodeUrl(collectionName)),
371-
new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).get(), EntityFactory.toJsonString(obj));
354+
final HttpResponseEntity res = httpManager
355+
.doPost(
356+
createGharialEndpointUrl(database, StringUtils.encodeUrl(graphName), VERTEX,
357+
StringUtils.encodeUrl(collectionName)),
358+
new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).get(), obj);
372359

373360
if (wrongResult(res)) {
374361
throw new ArangoException(UNKNOWN_ERROR);
@@ -524,13 +511,13 @@ public <T> EdgeEntity<T> createEdge(
524511
final T value,
525512
final Boolean waitForSync) throws ArangoException {
526513

527-
final JsonObject obj = EdgeUtils.valueToEdgeJsonObject(key, fromHandle, toHandle, value);
514+
final VPackSlice obj = EntityFactory.toEdgeVPack(key, fromHandle, toHandle, value);
528515

529516
validateCollectionName(graphName);
530517
final HttpResponseEntity res = httpManager.doPost(
531518
createGharialEndpointUrl(database, StringUtils.encodeUrl(graphName), EDGE,
532519
StringUtils.encodeUrl(edgeCollectionName)),
533-
new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).get(), EntityFactory.toJsonString(obj));
520+
new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).get(), obj);
534521

535522
final EdgeEntity<T> entity = createEntity(res, EdgeEntity.class, value == null ? null : value.getClass());
536523
if (value != null) {
@@ -600,14 +587,14 @@ public <T> EdgeEntity<T> replaceEdge(
600587
final String ifMatchRevision,
601588
final String ifNoneMatchRevision) throws ArangoException {
602589

603-
final JsonObject obj = EdgeUtils.valueToEdgeJsonObject(key, fromHandle, toHandle, value);
590+
final VPackSlice obj = EntityFactory.toEdgeVPack(key, fromHandle, toHandle, value);
604591

605592
validateCollectionName(graphName);
606593
final HttpResponseEntity res = httpManager.doPut(
607594
createGharialEndpointUrl(database, StringUtils.encodeUrl(graphName), EDGE,
608595
StringUtils.encodeUrl(edgeCollectionName), StringUtils.encodeUrl(key)),
609596
new MapBuilder().put(IF_NONE_MATCH, ifNoneMatchRevision, true).put(IF_MATCH, ifMatchRevision, true).get(),
610-
new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).get(), EntityFactory.toVPack(obj));
597+
new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).get(), obj);
611598

612599
final EdgeEntity<T> entity = createEntity(res, EdgeEntity.class, value == null ? null : value.getClass());
613600
if (value != null) {
@@ -640,15 +627,14 @@ public <T> EdgeEntity<T> updateEdge(
640627
final String ifMatchRevision,
641628
final String ifNoneMatchRevision) throws ArangoException {
642629

643-
final JsonObject obj = EdgeUtils.valueToEdgeJsonObject(key, fromHandle, toHandle, value);
630+
final VPackSlice obj = EntityFactory.toEdgeVPack(key, fromHandle, toHandle, value);
644631

645632
validateCollectionName(graphName);
646633
final HttpResponseEntity res = httpManager.doPatch(
647634
createGharialEndpointUrl(database, StringUtils.encodeUrl(graphName), EDGE,
648635
StringUtils.encodeUrl(edgeCollectionName), StringUtils.encodeUrl(key)),
649636
new MapBuilder().put(IF_NONE_MATCH, ifNoneMatchRevision, true).put(IF_MATCH, ifMatchRevision, true).get(),
650-
new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).put("keepNull", keepNull).get(),
651-
EntityFactory.toVPack(obj));
637+
new MapBuilder().put(WAIT_FOR_SYNC, waitForSync).put("keepNull", keepNull).get(), obj);
652638

653639
final EdgeEntity<T> entity = createEntity(res, EdgeEntity.class, value == null ? null : value.getClass());
654640
if (value != null) {

0 commit comments

Comments
 (0)
0