Skip to content

Commit 89aa1fe

Browse files
author
mpv1989
committed
optimized ArangoDB.Builder for better multi thread support
1 parent e7b0bd6 commit 89aa1fe

File tree

6 files changed

+124
-78
lines changed

6 files changed

+124
-78
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
v4.1.12 (2017-04-xx)
22
---------------------------
33
* added ArangoDatabase.cursor() (issue #116)
4+
* optimized ArangoDB.Builder for better multi thread support
45

56
v4.1.11 (2017-03-24)
67
---------------------------

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<logback-classic.version>1.1.3</logback-classic.version>
2828
<hamcrest-all.version>1.3</hamcrest-all.version>
2929
<junit.version>4.12</junit.version>
30-
<arangodb.velocypack.version>1.0.3</arangodb.velocypack.version>
30+
<arangodb.velocypack.version>1.0.4</arangodb.velocypack.version>
3131
</properties>
3232

3333
<developers>

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.arangodb.internal.util.ArangoDeserializerImpl;
4545
import com.arangodb.internal.util.ArangoSerializerImpl;
4646
import com.arangodb.internal.util.ArangoUtilImpl;
47+
import com.arangodb.internal.velocypack.VPackDocumentModule;
4748
import com.arangodb.internal.velocypack.VPackDriverModule;
4849
import com.arangodb.internal.velocystream.Communication;
4950
import com.arangodb.internal.velocystream.CommunicationSync;
@@ -90,18 +91,16 @@ public static class Builder {
9091
private Integer chunksize;
9192
private Integer maxConnections;
9293
private final VPack.Builder vpackBuilder;
93-
private final CollectionCache collectionCache;
9494
private final VPackParser.Builder vpackParserBuilder;
9595
private ArangoSerializer serializer;
9696
private ArangoDeserializer deserializer;
9797

9898
public Builder() {
9999
super();
100100
vpackBuilder = new VPack.Builder();
101-
collectionCache = new CollectionCache();
102101
vpackParserBuilder = new VPackParser.Builder();
103-
vpackBuilder.registerModule(new VPackDriverModule(collectionCache));
104-
vpackParserBuilder.registerModule(new VPackDriverModule(collectionCache));
102+
vpackBuilder.registerModule(new VPackDriverModule());
103+
vpackParserBuilder.registerModule(new VPackDriverModule());
105104
host = new Host(ArangoDBConstants.DEFAULT_HOST, ArangoDBConstants.DEFAULT_PORT);
106105
hosts = new ArrayList<Host>();
107106
loadProperties(ArangoDB.class.getResourceAsStream(DEFAULT_PROPERTY_FILE));
@@ -321,24 +320,26 @@ public Builder setDeserializer(final ArangoDeserializer deserializer) {
321320
return this;
322321
}
323322

324-
public ArangoDB build() {
323+
public synchronized ArangoDB build() {
325324
if (hosts.isEmpty()) {
326325
hosts.add(host);
327326
}
328-
final VPack vpacker = vpackBuilder.build();
327+
final CollectionCache collectionCache = new CollectionCache();
328+
vpackBuilder.registerModule(new VPackDocumentModule(collectionCache));
329+
vpackParserBuilder.registerModule(new VPackDocumentModule(collectionCache));
330+
331+
final VPack vpacker = vpackBuilder.serializeNullValues(false).build();
329332
final VPack vpackerNull = vpackBuilder.serializeNullValues(true).build();
330333
final VPackParser vpackParser = vpackParserBuilder.build();
331-
if (serializer == null) {
332-
serializer = new ArangoSerializerImpl(vpacker, vpackerNull, vpackParser);
333-
}
334-
if (deserializer == null) {
335-
deserializer = new ArangoDeserializerImpl(vpackerNull, vpackParser);
336-
}
334+
final ArangoSerializer serializerTemp = serializer != null ? serializer
335+
: new ArangoSerializerImpl(vpacker, vpackerNull, vpackParser);
336+
final ArangoDeserializer deserializerTemp = deserializer != null ? deserializer
337+
: new ArangoDeserializerImpl(vpackerNull, vpackParser);
337338
return new ArangoDB(
338-
new CommunicationSync.Builder(new DefaultHostHandler(hosts)).timeout(timeout).user(user)
339-
.password(password).useSsl(useSsl).sslContext(sslContext).chunksize(chunksize)
339+
new CommunicationSync.Builder(new DefaultHostHandler(new ArrayList<Host>(hosts))).timeout(timeout)
340+
.user(user).password(password).useSsl(useSsl).sslContext(sslContext).chunksize(chunksize)
340341
.maxConnections(maxConnections),
341-
new ArangoUtilImpl(serializer, deserializer), collectionCache);
342+
new ArangoUtilImpl(serializerTemp, deserializerTemp), collectionCache);
342343
}
343344

344345
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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.velocypack;
22+
23+
import org.json.simple.JSONValue;
24+
25+
import com.arangodb.internal.CollectionCache;
26+
import com.arangodb.velocypack.VPackDeserializationContext;
27+
import com.arangodb.velocypack.VPackDeserializer;
28+
import com.arangodb.velocypack.VPackJsonDeserializer;
29+
import com.arangodb.velocypack.VPackModule;
30+
import com.arangodb.velocypack.VPackParserModule;
31+
import com.arangodb.velocypack.VPackParserSetupContext;
32+
import com.arangodb.velocypack.VPackSetupContext;
33+
import com.arangodb.velocypack.VPackSlice;
34+
import com.arangodb.velocypack.ValueType;
35+
import com.arangodb.velocypack.exception.VPackException;
36+
import com.arangodb.velocypack.internal.util.NumberUtil;
37+
38+
/**
39+
* @author Mark - mark at arangodb.com
40+
*
41+
*/
42+
public class VPackDocumentModule implements VPackModule, VPackParserModule {
43+
44+
private static final String ID = "_id";
45+
private final CollectionCache collectionCache;
46+
47+
public VPackDocumentModule(final CollectionCache collectionCache) {
48+
super();
49+
this.collectionCache = collectionCache;
50+
}
51+
52+
@Override
53+
public <C extends VPackSetupContext<C>> void setup(final C context) {
54+
context.registerDeserializer(ID, String.class, new VPackDeserializer<String>() {
55+
@Override
56+
public String deserialize(
57+
final VPackSlice parent,
58+
final VPackSlice vpack,
59+
final VPackDeserializationContext context) throws VPackException {
60+
final String id;
61+
if (vpack.isCustom()) {
62+
final long idLong = NumberUtil.toLong(vpack.getBuffer(), vpack.getStart() + 1,
63+
vpack.getByteSize() - 1);
64+
final String collectionName = collectionCache.getCollectionName(idLong);
65+
if (collectionName != null) {
66+
final VPackSlice key = parent.get("_key");
67+
id = String.format("%s/%s", collectionName, key.getAsString());
68+
} else {
69+
id = null;
70+
}
71+
} else {
72+
id = vpack.getAsString();
73+
}
74+
return id;
75+
}
76+
});
77+
78+
}
79+
80+
@Override
81+
public <C extends VPackParserSetupContext<C>> void setup(final C context) {
82+
context.registerDeserializer(ID, ValueType.CUSTOM, new VPackJsonDeserializer() {
83+
@Override
84+
public void deserialize(
85+
final VPackSlice parent,
86+
final String attribute,
87+
final VPackSlice vpack,
88+
final StringBuilder json) throws VPackException {
89+
final String id;
90+
final long idLong = NumberUtil.toLong(vpack.getBuffer(), vpack.getStart() + 1, vpack.getByteSize() - 1);
91+
final String collectionName = collectionCache.getCollectionName(idLong);
92+
if (collectionName != null) {
93+
final VPackSlice key = parent.get("_key");
94+
id = String.format("%s/%s", collectionName, key.getAsString());
95+
} else {
96+
id = null;
97+
}
98+
json.append(JSONValue.toJSONString(id));
99+
}
100+
});
101+
}
102+
103+
}

src/main/java/com/arangodb/internal/velocypack/VPackDriverModule.java

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,20 @@
2323
import java.lang.reflect.Field;
2424
import java.util.Date;
2525

26-
import org.json.simple.JSONValue;
27-
2826
import com.arangodb.entity.BaseDocument;
2927
import com.arangodb.entity.BaseEdgeDocument;
3028
import com.arangodb.entity.CollectionStatus;
3129
import com.arangodb.entity.CollectionType;
3230
import com.arangodb.entity.DocumentField;
3331
import com.arangodb.entity.LogLevel;
3432
import com.arangodb.entity.QueryEntity;
35-
import com.arangodb.internal.CollectionCache;
3633
import com.arangodb.internal.velocystream.AuthenticationRequest;
3734
import com.arangodb.model.TraversalOptions;
38-
import com.arangodb.velocypack.VPackDeserializationContext;
39-
import com.arangodb.velocypack.VPackDeserializer;
4035
import com.arangodb.velocypack.VPackFieldNamingStrategy;
41-
import com.arangodb.velocypack.VPackJsonDeserializer;
4236
import com.arangodb.velocypack.VPackModule;
4337
import com.arangodb.velocypack.VPackParserModule;
4438
import com.arangodb.velocypack.VPackParserSetupContext;
4539
import com.arangodb.velocypack.VPackSetupContext;
46-
import com.arangodb.velocypack.VPackSlice;
47-
import com.arangodb.velocypack.ValueType;
48-
import com.arangodb.velocypack.exception.VPackException;
49-
import com.arangodb.velocypack.internal.util.NumberUtil;
5040
import com.arangodb.velocystream.Request;
5141
import com.arangodb.velocystream.Response;
5242

@@ -56,14 +46,6 @@
5646
*/
5747
public class VPackDriverModule implements VPackModule, VPackParserModule {
5848

59-
private static final String ID = "_id";
60-
private final CollectionCache collectionCache;
61-
62-
public VPackDriverModule(final CollectionCache collectionCache) {
63-
super();
64-
this.collectionCache = collectionCache;
65-
}
66-
6749
@Override
6850
public <C extends VPackSetupContext<C>> void setup(final C context) {
6951
context.fieldNamingStrategy(new VPackFieldNamingStrategy() {
@@ -76,29 +58,6 @@ public String translateName(final Field field) {
7658
return field.getName();
7759
}
7860
});
79-
context.registerDeserializer(ID, String.class, new VPackDeserializer<String>() {
80-
@Override
81-
public String deserialize(
82-
final VPackSlice parent,
83-
final VPackSlice vpack,
84-
final VPackDeserializationContext context) throws VPackException {
85-
final String id;
86-
if (vpack.isCustom()) {
87-
final long idLong = NumberUtil.toLong(vpack.getBuffer(), vpack.getStart() + 1,
88-
vpack.getByteSize() - 1);
89-
final String collectionName = collectionCache.getCollectionName(idLong);
90-
if (collectionName != null) {
91-
final VPackSlice key = parent.get("_key");
92-
id = String.format("%s/%s", collectionName, key.getAsString());
93-
} else {
94-
id = null;
95-
}
96-
} else {
97-
id = vpack.getAsString();
98-
}
99-
return id;
100-
}
101-
});
10261
context.registerSerializer(Request.class, VPackSerializers.REQUEST);
10362
context.registerSerializer(AuthenticationRequest.class, VPackSerializers.AUTH_REQUEST);
10463
context.registerSerializer(CollectionType.class, VPackSerializers.COLLECTION_TYPE);
@@ -118,25 +77,7 @@ public String deserialize(
11877

11978
@Override
12079
public <C extends VPackParserSetupContext<C>> void setup(final C context) {
121-
context.registerDeserializer(ID, ValueType.CUSTOM, new VPackJsonDeserializer() {
122-
@Override
123-
public void deserialize(
124-
final VPackSlice parent,
125-
final String attribute,
126-
final VPackSlice vpack,
127-
final StringBuilder json) throws VPackException {
128-
final String id;
129-
final long idLong = NumberUtil.toLong(vpack.getBuffer(), vpack.getStart() + 1, vpack.getByteSize() - 1);
130-
final String collectionName = collectionCache.getCollectionName(idLong);
131-
if (collectionName != null) {
132-
final VPackSlice key = parent.get("_key");
133-
id = String.format("%s/%s", collectionName, key.getAsString());
134-
} else {
135-
id = null;
136-
}
137-
json.append(JSONValue.toJSONString(id));
138-
}
139-
});
80+
14081
}
14182

14283
}

src/test/java/com/arangodb/entity/BaseDocumentTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void serialize() throws VPackException {
4848
entity.addAttribute("a", "a");
4949

5050
final Builder builder = new VPack.Builder();
51-
builder.registerModule(new VPackDriverModule(null));
51+
builder.registerModule(new VPackDriverModule());
5252
final VPack vpacker = builder.build();
5353

5454
final VPackSlice vpack = vpacker.serialize(entity);
@@ -80,7 +80,7 @@ public void deserialize() throws VPackException {
8080
builder.close();
8181

8282
final VPack.Builder vbuilder = new VPack.Builder();
83-
vbuilder.registerModule(new VPackDriverModule(null));
83+
vbuilder.registerModule(new VPackDriverModule());
8484
final VPack vpacker = vbuilder.build();
8585

8686
final BaseDocument entity = vpacker.deserialize(builder.slice(), BaseDocument.class);

0 commit comments

Comments
 (0)