Skip to content

Commit

Permalink
perf: removed call to ResultInternal.wrap()
Browse files Browse the repository at this point in the history
Issue #1576
  • Loading branch information
lvca committed Apr 29, 2024
1 parent 13cadac commit e8c89bf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.stream.*;

/**
* Implementation of a {@link Result}.
* <p>
* Created by luigidellaquila on 06/07/16.
*/
public class ResultInternal implements Result {
Expand Down Expand Up @@ -120,9 +122,9 @@ public <T> 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;

Expand All @@ -135,9 +137,9 @@ else if (element != null)
public <T> 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;

Expand All @@ -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()`.
* <p>
* 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));
Expand All @@ -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<String, Object> o : ((Map<String, Object>) input).entrySet())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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);
Expand All @@ -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);
}
};
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
Expand All @@ -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<Document> documents, final long cursorId) {
private Document firstBatchCursorResponse(final String ns, final String key, final List<Document> documents,
final long cursorId) {
final Document cursorResponse = new Document();
cursorResponse.put("id", cursorId);
cursorResponse.put("ns", getFullCollectionNamespace(ns));
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e8c89bf

Please sign in to comment.