Skip to content

[DE-959] Improve serde perfs #588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Dec 10, 2024
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -535,8 +535,8 @@ workflows:
matrix:
parameters:
args:
- '-Dadb.jackson.version=2.18.0'
- '-Dadb.jackson.version=2.17.2'
- '-Dadb.jackson.version=2.18.2'
- '-Dadb.jackson.version=2.17.3'
- '-Dadb.jackson.version=2.16.2'
- '-Dadb.jackson.version=2.15.4'
- '-Dadb.jackson.version=2.14.3'
@@ -551,7 +551,7 @@ workflows:
only:
- main
- test:
name: test-native-ssl=<<matrix.ssl>>
name: test-native-ssl=<<matrix.ssl>>-<<matrix.graalvm-version>>
matrix:
parameters:
native:
@@ -571,7 +571,7 @@ workflows:
only:
- main
- test-shaded:
name: test-native-shaded-ssl=<<matrix.ssl>>
name: test-native-shaded-ssl=<<matrix.ssl>>-<<matrix.graalvm-version>>
matrix:
parameters:
native:
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
<relativePath>../release-parent</relativePath>
<groupId>com.arangodb</groupId>
<artifactId>release-parent</artifactId>
<version>7.14.0</version>
<version>7.15.0-SNAPSHOT</version>
</parent>

<name>core</name>
Original file line number Diff line number Diff line change
@@ -20,16 +20,17 @@

package com.arangodb.entity;

import java.util.ArrayList;
import java.util.List;

/**
* @author Mark Vollmary
*/
public final class MultiDocumentEntity<E> {

private List<E> documents;
private List<ErrorEntity> errors;
private List<Object> documentsAndErrors;
private List<E> documents = new ArrayList<>();
private List<ErrorEntity> errors = new ArrayList<>();
private List<Object> documentsAndErrors = new ArrayList<>();
private boolean isPotentialDirtyRead = false;

public MultiDocumentEntity() {
126 changes: 15 additions & 111 deletions core/src/main/java/com/arangodb/internal/InternalArangoCollection.java
Original file line number Diff line number Diff line change
@@ -32,7 +32,6 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.arangodb.internal.serde.SerdeUtils.constructParametricType;

@@ -111,28 +110,9 @@ private InternalRequest createInsertDocumentRequest(final DocumentCreateOptions

protected <T> ResponseDeserializer<MultiDocumentEntity<DocumentCreateEntity<T>>> insertDocumentsResponseDeserializer(Class<T> userDataClass) {
return (response) -> {
final MultiDocumentEntity<DocumentCreateEntity<T>> multiDocument = new MultiDocumentEntity<>();
final List<DocumentCreateEntity<T>> docs = new ArrayList<>();
final List<ErrorEntity> errors = new ArrayList<>();
final List<Object> documentsAndErrors = new ArrayList<>();
final JsonNode body = getSerde().parse(response.getBody());
for (final JsonNode next : body) {
JsonNode isError = next.get(ArangoResponseField.ERROR_FIELD_NAME);
if (isError != null && isError.booleanValue()) {
final ErrorEntity error = getSerde().deserialize(next, ErrorEntity.class);
errors.add(error);
documentsAndErrors.add(error);
} else {
Type type = constructParametricType(DocumentCreateEntity.class, userDataClass);
final DocumentCreateEntity<T> doc = getSerde().deserialize(next, type);
docs.add(doc);
documentsAndErrors.add(doc);
}
}
multiDocument.setDocuments(docs);
multiDocument.setErrors(errors);
multiDocument.setDocumentsAndErrors(documentsAndErrors);
return multiDocument;
Type type = constructParametricType(MultiDocumentEntity.class,
constructParametricType(DocumentCreateEntity.class, userDataClass));
return getSerde().deserialize(response.getBody(), type);
};
}

@@ -184,31 +164,12 @@ protected InternalRequest getDocumentsRequest(final Iterable<String> keys, final
return request;
}

protected <T> ResponseDeserializer<MultiDocumentEntity<T>> getDocumentsResponseDeserializer(
final Class<T> type) {
protected <T> ResponseDeserializer<MultiDocumentEntity<T>> getDocumentsResponseDeserializer(final Class<T> type) {
return (response) -> {
final MultiDocumentEntity<T> multiDocument = new MultiDocumentEntity<>();
MultiDocumentEntity<T> multiDocument = getSerde().deserialize(response.getBody(),
constructParametricType(MultiDocumentEntity.class, type));
boolean potentialDirtyRead = Boolean.parseBoolean(response.getMeta("X-Arango-Potential-Dirty-Read"));
multiDocument.setPotentialDirtyRead(potentialDirtyRead);
final List<T> docs = new ArrayList<>();
final List<ErrorEntity> errors = new ArrayList<>();
final List<Object> documentsAndErrors = new ArrayList<>();
final JsonNode body = getSerde().parse(response.getBody());
for (final JsonNode next : body) {
JsonNode isError = next.get(ArangoResponseField.ERROR_FIELD_NAME);
if (isError != null && isError.booleanValue()) {
final ErrorEntity error = getSerde().deserialize(next, ErrorEntity.class);
errors.add(error);
documentsAndErrors.add(error);
} else {
final T doc = getSerde().deserializeUserData(getSerde().serialize(next), type);
docs.add(doc);
documentsAndErrors.add(doc);
}
}
multiDocument.setDocuments(docs);
multiDocument.setErrors(errors);
multiDocument.setDocumentsAndErrors(documentsAndErrors);
return multiDocument;
};
}
@@ -250,28 +211,9 @@ private InternalRequest createReplaceDocumentRequest(final DocumentReplaceOption
protected <T> ResponseDeserializer<MultiDocumentEntity<DocumentUpdateEntity<T>>> replaceDocumentsResponseDeserializer(
final Class<T> returnType) {
return (response) -> {
final MultiDocumentEntity<DocumentUpdateEntity<T>> multiDocument = new MultiDocumentEntity<>();
final List<DocumentUpdateEntity<T>> docs = new ArrayList<>();
final List<ErrorEntity> errors = new ArrayList<>();
final List<Object> documentsAndErrors = new ArrayList<>();
final JsonNode body = getSerde().parse(response.getBody());
for (final JsonNode next : body) {
JsonNode isError = next.get(ArangoResponseField.ERROR_FIELD_NAME);
if (isError != null && isError.booleanValue()) {
final ErrorEntity error = getSerde().deserialize(next, ErrorEntity.class);
errors.add(error);
documentsAndErrors.add(error);
} else {
Type type = constructParametricType(DocumentUpdateEntity.class, returnType);
final DocumentUpdateEntity<T> doc = getSerde().deserialize(next, type);
docs.add(doc);
documentsAndErrors.add(doc);
}
}
multiDocument.setDocuments(docs);
multiDocument.setErrors(errors);
multiDocument.setDocumentsAndErrors(documentsAndErrors);
return multiDocument;
Type type = constructParametricType(MultiDocumentEntity.class,
constructParametricType(DocumentUpdateEntity.class, returnType));
return getSerde().deserialize(response.getBody(), type);
};
}

@@ -313,28 +255,9 @@ private InternalRequest createUpdateDocumentRequest(final DocumentUpdateOptions
protected <T> ResponseDeserializer<MultiDocumentEntity<DocumentUpdateEntity<T>>> updateDocumentsResponseDeserializer(
final Class<T> returnType) {
return (response) -> {
final MultiDocumentEntity<DocumentUpdateEntity<T>> multiDocument = new MultiDocumentEntity<>();
final List<DocumentUpdateEntity<T>> docs = new ArrayList<>();
final List<ErrorEntity> errors = new ArrayList<>();
final List<Object> documentsAndErrors = new ArrayList<>();
final JsonNode body = getSerde().parse(response.getBody());
for (final JsonNode next : body) {
JsonNode isError = next.get(ArangoResponseField.ERROR_FIELD_NAME);
if (isError != null && isError.booleanValue()) {
final ErrorEntity error = getSerde().deserialize(next, ErrorEntity.class);
errors.add(error);
documentsAndErrors.add(error);
} else {
Type type = constructParametricType(DocumentUpdateEntity.class, returnType);
final DocumentUpdateEntity<T> doc = getSerde().deserialize(next, type);
docs.add(doc);
documentsAndErrors.add(doc);
}
}
multiDocument.setDocuments(docs);
multiDocument.setErrors(errors);
multiDocument.setDocumentsAndErrors(documentsAndErrors);
return multiDocument;
Type type = constructParametricType(MultiDocumentEntity.class,
constructParametricType(DocumentUpdateEntity.class, returnType));
return getSerde().deserialize(response.getBody(), type);
};
}

@@ -370,28 +293,9 @@ private InternalRequest createDeleteDocumentRequest(final DocumentDeleteOptions
protected <T> ResponseDeserializer<MultiDocumentEntity<DocumentDeleteEntity<T>>> deleteDocumentsResponseDeserializer(
final Class<T> userDataClass) {
return (response) -> {
final MultiDocumentEntity<DocumentDeleteEntity<T>> multiDocument = new MultiDocumentEntity<>();
final List<DocumentDeleteEntity<T>> docs = new ArrayList<>();
final List<ErrorEntity> errors = new ArrayList<>();
final List<Object> documentsAndErrors = new ArrayList<>();
final JsonNode body = getSerde().parse(response.getBody());
for (final JsonNode next : body) {
JsonNode isError = next.get(ArangoResponseField.ERROR_FIELD_NAME);
if (isError != null && isError.booleanValue()) {
final ErrorEntity error = getSerde().deserialize(next, ErrorEntity.class);
errors.add(error);
documentsAndErrors.add(error);
} else {
Type type = constructParametricType(DocumentDeleteEntity.class, userDataClass);
final DocumentDeleteEntity<T> doc = getSerde().deserialize(next, type);
docs.add(doc);
documentsAndErrors.add(doc);
}
}
multiDocument.setDocuments(docs);
multiDocument.setErrors(errors);
multiDocument.setDocumentsAndErrors(documentsAndErrors);
return multiDocument;
Type type = constructParametricType(MultiDocumentEntity.class,
constructParametricType(DocumentDeleteEntity.class, userDataClass));
return getSerde().deserialize(response.getBody(), type);
};
}

Original file line number Diff line number Diff line change
@@ -23,8 +23,6 @@
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicLong;

import static com.arangodb.internal.util.SerdeUtils.toJsonString;

@UsedInApi
public abstract class Communication implements Closeable {
private static final Logger LOGGER = LoggerFactory.getLogger(Communication.class);
@@ -59,7 +57,7 @@ private CompletableFuture<InternalResponse> doExecuteAsync(
final InternalRequest request, final HostHandle hostHandle, final Host host, final int attemptCount, Connection connection, long reqId
) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Send Request [id={}]: {} {}", reqId, request, toJsonString(serde, request.getBody()));
LOGGER.debug("Send Request [id={}]: {} {}", reqId, request, serde.toJsonString(request.getBody()));
}
final CompletableFuture<InternalResponse> rfuture = new CompletableFuture<>();
try {
@@ -85,7 +83,7 @@ private CompletableFuture<InternalResponse> doExecuteAsync(
handleException(isSafe(request), e, hostHandle, request, host, reqId, attemptCount, rfuture);
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Received Response [id={}]: {} {}", reqId, response, toJsonString(serde, response.getBody()));
LOGGER.debug("Received Response [id={}]: {} {}", reqId, response, serde.toJsonString(response.getBody()));
}
ArangoDBException errorEntityEx = ResponseUtils.translateError(serde, response);
if (errorEntityEx instanceof ArangoDBRedirectException) {
Original file line number Diff line number Diff line change
@@ -6,8 +6,11 @@
import com.arangodb.entity.ReplicationFactor;
import com.arangodb.entity.arangosearch.CollectionLink;
import com.arangodb.entity.arangosearch.FieldLink;
import com.arangodb.util.RawBytes;
import com.arangodb.util.RawJson;
import com.arangodb.internal.InternalResponse;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
@@ -16,6 +19,8 @@
import com.fasterxml.jackson.databind.node.*;

import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
@@ -26,7 +31,23 @@ public final class InternalDeserializers {
static final JsonDeserializer<RawJson> RAW_JSON_DESERIALIZER = new JsonDeserializer<RawJson>() {
@Override
public RawJson deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return RawJson.of(SerdeUtils.INSTANCE.writeJson(p.readValueAsTree()));
if (JsonFactory.FORMAT_NAME_JSON.equals(p.getCodec().getFactory().getFormatName())) {
return RawJson.of(new String(SerdeUtils.extractBytes(p), StandardCharsets.UTF_8));
} else {
StringWriter w = new StringWriter();
try (JsonGenerator gen = SerdeUtils.INSTANCE.getJsonMapper().getFactory().createGenerator(w)) {
gen.copyCurrentStructure(p);
gen.flush();
}
return RawJson.of(w.toString());
}
}
};

static final JsonDeserializer<RawBytes> RAW_BYTES_DESERIALIZER = new JsonDeserializer<RawBytes>() {
@Override
public RawBytes deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return RawBytes.of(SerdeUtils.extractBytes(p));
}
};

@@ -134,5 +155,4 @@ public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx
}
}


}
18 changes: 7 additions & 11 deletions core/src/main/java/com/arangodb/internal/serde/InternalModule.java
Original file line number Diff line number Diff line change
@@ -3,38 +3,34 @@
import com.arangodb.entity.CollectionStatus;
import com.arangodb.entity.CollectionType;
import com.arangodb.entity.InvertedIndexPrimarySort;
import com.arangodb.entity.MultiDocumentEntity;
import com.arangodb.entity.ReplicationFactor;
import com.arangodb.util.RawBytes;
import com.arangodb.util.RawJson;
import com.arangodb.internal.InternalRequest;
import com.arangodb.internal.InternalResponse;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.module.SimpleModule;

import java.util.function.Supplier;
class InternalModule {

enum InternalModule implements Supplier<Module> {
INSTANCE;
static Module get(InternalSerde serde) {
SimpleModule module = new SimpleModule();

private final SimpleModule module;

InternalModule() {
module = new SimpleModule();
module.addDeserializer(MultiDocumentEntity.class, new MultiDocumentEntityDeserializer(serde));

module.addSerializer(RawJson.class, InternalSerializers.RAW_JSON_SERIALIZER);
module.addSerializer(InternalRequest.class, InternalSerializers.REQUEST);
module.addSerializer(CollectionType.class, InternalSerializers.COLLECTION_TYPE);

module.addDeserializer(RawJson.class, InternalDeserializers.RAW_JSON_DESERIALIZER);
module.addDeserializer(RawBytes.class, InternalDeserializers.RAW_BYTES_DESERIALIZER);
module.addDeserializer(CollectionStatus.class, InternalDeserializers.COLLECTION_STATUS);
module.addDeserializer(CollectionType.class, InternalDeserializers.COLLECTION_TYPE);
module.addDeserializer(ReplicationFactor.class, InternalDeserializers.REPLICATION_FACTOR);
module.addDeserializer(InternalResponse.class, InternalDeserializers.RESPONSE);
module.addDeserializer(InvertedIndexPrimarySort.Field.class, InternalDeserializers.INVERTED_INDEX_PRIMARY_SORT_FIELD);
}

@Override
public Module get() {
return module;
}

}
Loading