From e8c89bf51544337c1e7ace4fb527e779498a7db8 Mon Sep 17 00:00:00 2001 From: lvca Date: Mon, 29 Apr 2024 09:42:11 -0400 Subject: [PATCH] perf: removed call to ResultInternal.wrap() Issue #1576 --- .../query/sql/executor/ResultInternal.java | 24 +++++++++++++------ .../mongo/MongoDBDatabaseWrapper.java | 24 ++++++++++++------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/engine/src/main/java/com/arcadedb/query/sql/executor/ResultInternal.java b/engine/src/main/java/com/arcadedb/query/sql/executor/ResultInternal.java index 7079eb29b..17c232fe6 100644 --- a/engine/src/main/java/com/arcadedb/query/sql/executor/ResultInternal.java +++ b/engine/src/main/java/com/arcadedb/query/sql/executor/ResultInternal.java @@ -29,6 +29,8 @@ import java.util.stream.*; /** + * Implementation of a {@link Result}. + *

* Created by luigidellaquila on 06/07/16. */ public class ResultInternal implements Result { @@ -120,9 +122,9 @@ public T getProperty(final String name) { T result; if (content != null && !content.isEmpty()) // IF CONTENT IS PRESENT SKIP CHECKING FOR ELEMENT (PROJECTIONS USED) - result = (T) wrap(content.get(name)); + result = (T) content.get(name); else if (element != null) - result = (T) wrap(element.get(name)); + result = (T) element.get(name); else result = null; @@ -135,9 +137,9 @@ else if (element != null) public T getProperty(final String name, final Object defaultValue) { T result; if (content != null && content.containsKey(name)) - result = (T) wrap(content.get(name)); + result = (T) content.get(name); else if (element != null && element.has(name)) - result = (T) wrap(element.get(name)); + result = (T) element.get(name); else result = (T) defaultValue; @@ -163,7 +165,15 @@ else if (element != null) return result instanceof Record ? (Record) result : null; } - private Object wrap(final Object input) { + /** + * Returns the input object transformed into a map when the value is a record. it works recursively. + * This method was originally used in OrientDB because all the record were immutable. With ArcadeDB all the + * records returned by a query are always immutable, so this method is not called automatically from the `getProperty()`. + *

+ * From v24.5.1 this method is public to allow to be called for retro compatibility with existent code base or porting + * from OrientDB. + */ + public static Object wrap(final Object input) { if (input instanceof Document && ((Document) input).getIdentity() == null && !(input instanceof EmbeddedDocument)) { final Document elem = ((Document) input); final ResultInternal result = new ResultInternal(elem.toMap(false)); @@ -172,9 +182,9 @@ private Object wrap(final Object input) { return result; } else if (input instanceof List) { - return ((List) input).stream().map(this::wrap).collect(Collectors.toList()); + return ((List) input).stream().map(ResultInternal::wrap).collect(Collectors.toList()); } else if (input instanceof Set) { - return ((Set) input).stream().map(this::wrap).collect(Collectors.toSet()); + return ((Set) input).stream().map(ResultInternal::wrap).collect(Collectors.toSet()); } else if (input instanceof Map) { final Map result = new HashMap(); for (final Map.Entry o : ((Map) input).entrySet()) diff --git a/mongodbw/src/main/java/com/arcadedb/mongo/MongoDBDatabaseWrapper.java b/mongodbw/src/main/java/com/arcadedb/mongo/MongoDBDatabaseWrapper.java index 2c3e54606..c5e5fc7c3 100644 --- a/mongodbw/src/main/java/com/arcadedb/mongo/MongoDBDatabaseWrapper.java +++ b/mongodbw/src/main/java/com/arcadedb/mongo/MongoDBDatabaseWrapper.java @@ -81,7 +81,8 @@ public void handleClose(final Channel channel) { } @Override - public Document handleCommand(final Channel channel, final String command, final Document document, final DatabaseResolver databaseResolver, + public Document handleCommand(final Channel channel, final String command, final Document document, + final DatabaseResolver databaseResolver, final Oplog opLog) { try { if (command.equalsIgnoreCase("find")) @@ -95,8 +96,11 @@ else if (command.equalsIgnoreCase("insert")) else if (command.equalsIgnoreCase("aggregate")) return aggregateCollection(command, document, opLog); else { - LogManager.instance().log(this, Level.SEVERE, "Received unsupported command from MongoDB client '%s', (document=%s)", null, command, document); - throw new UnsupportedOperationException(String.format("Received unsupported command from MongoDB client '%s', (document=%s)", command, document)); + LogManager.instance() + .log(this, Level.SEVERE, "Received unsupported command from MongoDB client '%s', (document=%s)", null, command, + document); + throw new UnsupportedOperationException( + String.format("Received unsupported command from MongoDB client '%s', (document=%s)", command, document)); } } catch (final Exception e) { throw new MongoServerException("Error on executing MongoDB '" + command + "' command", e); @@ -121,7 +125,7 @@ public ResultSet query(final String query) throws MongoServerException { final IteratorResultSet resultset = new IteratorResultSet(result.iterator()) { @Override public Result next() { - final Map doc = super.next().getProperty("value"); + final Map doc = (Map) ResultInternal.wrap(super.next().getProperty("value")); return new ResultInternal(doc); } }; @@ -173,7 +177,8 @@ public QueryResult handleQuery(final MongoQuery query) throws MongoServerExcepti } } - private Document aggregateCollection(final String command, final Document document, final Oplog oplog) throws MongoServerException { + private Document aggregateCollection(final String command, final Document document, final Oplog oplog) + throws MongoServerException { final String collectionName = document.get("aggregate").toString(); database.countType(collectionName, false); @@ -184,7 +189,8 @@ private Document aggregateCollection(final String command, final Document docume if (!pipeline.isEmpty()) { final Document changeStream = (Document) pipeline.get(0).get("$changeStream"); if (changeStream != null) { - final Aggregation aggregation = Aggregation.fromPipeline(pipeline.subList(1, pipeline.size()), plugin, this, collection, oplog); + final Aggregation aggregation = Aggregation.fromPipeline(pipeline.subList(1, pipeline.size()), plugin, this, collection, + oplog); aggregation.validate(document); return commandChangeStreamPipeline(document, oplog, collectionName, changeStream, aggregation); } @@ -195,7 +201,8 @@ private Document aggregateCollection(final String command, final Document docume return firstBatchCursorResponse(collectionName, "firstBatch", aggregation.computeResult(), 0); } - private Document firstBatchCursorResponse(final String ns, final String key, final List documents, final long cursorId) { + private Document firstBatchCursorResponse(final String ns, final String key, final List documents, + final long cursorId) { final Document cursorResponse = new Document(); cursorResponse.put("id", cursorId); cursorResponse.put("ns", getFullCollectionNamespace(ns)); @@ -246,7 +253,8 @@ public void unregisterCollection(final String collectionName) { database.getSchema().dropBucket(collectionName); } - private Document commandChangeStreamPipeline(final Document query, final Oplog oplog, final String collectionName, final Document changeStreamDocument, + private Document commandChangeStreamPipeline(final Document query, final Oplog oplog, final String collectionName, + final Document changeStreamDocument, final Aggregation aggregation) { final Document cursorDocument = (Document) query.get("cursor"); final int batchSize = (int) cursorDocument.getOrDefault("batchSize", 0);