Skip to content

Commit

Permalink
🐛 Fix field-to-aggregation mapping not working for non-array fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ujibang committed Oct 17, 2023
1 parent 039a2bf commit d96ee5e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 25 deletions.
65 changes: 48 additions & 17 deletions commons/src/main/java/org/restheart/exchange/MongoRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
*/
package org.restheart.exchange;

import io.undertow.server.HttpServerExchange;
import io.undertow.util.AttachmentKey;
import io.undertow.util.Headers;
import io.undertow.util.PathTemplateMatch;

import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -32,6 +27,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.bson.BsonArray;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
Expand All @@ -40,19 +36,54 @@
import org.bson.BsonValue;
import org.bson.json.JsonMode;
import org.bson.json.JsonParseException;
import static org.restheart.exchange.ExchangeKeys.*;
import static org.restheart.utils.BsonUtils.document;
import static org.restheart.exchange.ExchangeKeys.ADMIN;
import static org.restheart.exchange.ExchangeKeys.BINARY_CONTENT;
import static org.restheart.exchange.ExchangeKeys.CACHE_QPARAM_KEY;
import static org.restheart.exchange.ExchangeKeys.COLL_META_DOCID_PREFIX;
import static org.restheart.exchange.ExchangeKeys.CONFIG;
import static org.restheart.exchange.ExchangeKeys.DB_META_DOCID;
import org.restheart.exchange.ExchangeKeys.DOC_ID_TYPE;
import static org.restheart.exchange.ExchangeKeys.ETAG_CHECK_QPARAM_KEY;
import static org.restheart.exchange.ExchangeKeys.FS_CHUNKS_SUFFIX;
import static org.restheart.exchange.ExchangeKeys.FS_FILES_SUFFIX;
import org.restheart.exchange.ExchangeKeys.HAL_MODE;
import static org.restheart.exchange.ExchangeKeys.JSON_MODE_QPARAM_KEY;
import static org.restheart.exchange.ExchangeKeys.LOCAL;
import static org.restheart.exchange.ExchangeKeys.META_COLLNAME;
import static org.restheart.exchange.ExchangeKeys.NO_CACHE_QPARAM_KEY;
import static org.restheart.exchange.ExchangeKeys.NO_PROPS_KEY;
import static org.restheart.exchange.ExchangeKeys.NUL;
import static org.restheart.exchange.ExchangeKeys.READ_CONCERN_QPARAM_KEY;
import static org.restheart.exchange.ExchangeKeys.READ_PREFERENCE_QPARAM_KEY;
import org.restheart.exchange.ExchangeKeys.REPRESENTATION_FORMAT;
import static org.restheart.exchange.ExchangeKeys.RESOURCES_WILDCARD_KEY;
import static org.restheart.exchange.ExchangeKeys.SYSTEM;
import org.restheart.exchange.ExchangeKeys.TYPE;
import static org.restheart.exchange.ExchangeKeys.WRITE_CONCERN_QPARAM_KEY;
import org.restheart.exchange.ExchangeKeys.WRITE_MODE;
import static org.restheart.exchange.ExchangeKeys.WRITE_MODE_QPARAM_KEY;
import static org.restheart.exchange.ExchangeKeys.WRITE_MODE_SHORT_QPARAM_KEY;
import static org.restheart.exchange.ExchangeKeys._AGGREGATIONS;
import static org.restheart.exchange.ExchangeKeys._INDEXES;
import static org.restheart.exchange.ExchangeKeys._META;
import static org.restheart.exchange.ExchangeKeys._METRICS;
import static org.restheart.exchange.ExchangeKeys._SCHEMAS;
import static org.restheart.exchange.ExchangeKeys._SESSIONS;
import static org.restheart.exchange.ExchangeKeys._SIZE;
import static org.restheart.exchange.ExchangeKeys._STREAMS;
import static org.restheart.exchange.ExchangeKeys._TRANSACTIONS;
import org.restheart.mongodb.RSOps;
import org.restheart.mongodb.db.sessions.ClientSessionImpl;
import static org.restheart.utils.BsonUtils.document;
import org.restheart.utils.MongoServiceAttachments;
import org.restheart.utils.URLUtils;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.undertow.server.HttpServerExchange;
import io.undertow.util.AttachmentKey;
import io.undertow.util.Headers;
import io.undertow.util.PathTemplateMatch;

/**
*
Expand Down Expand Up @@ -208,12 +239,12 @@ protected MongoRequest(HttpServerExchange exchange, String requestUri, String re
this.writeMode = mode;

var anyRsSet = false;
var rsOps = new RSOps();
var _rsOps = new RSOps();

// readConcern
if (exchange.getQueryParameters().containsKey(READ_CONCERN_QPARAM_KEY)) {
try {
rsOps = rsOps.withReadConcern(exchange.getQueryParameters().get(READ_CONCERN_QPARAM_KEY).getFirst());
_rsOps = _rsOps.withReadConcern(exchange.getQueryParameters().get(READ_CONCERN_QPARAM_KEY).getFirst());
anyRsSet = true;
} catch (IllegalArgumentException iae) {
// nothing to do
Expand All @@ -223,7 +254,7 @@ protected MongoRequest(HttpServerExchange exchange, String requestUri, String re
// readPreference
if (exchange.getQueryParameters().containsKey(READ_PREFERENCE_QPARAM_KEY)) {
try {
rsOps = rsOps.withReadPreference(exchange.getQueryParameters().get(READ_PREFERENCE_QPARAM_KEY).getFirst());
_rsOps = _rsOps.withReadPreference(exchange.getQueryParameters().get(READ_PREFERENCE_QPARAM_KEY).getFirst());
anyRsSet = true;
} catch (IllegalArgumentException iae) {
// nothing to do
Expand All @@ -232,12 +263,12 @@ protected MongoRequest(HttpServerExchange exchange, String requestUri, String re

// writeConcern
if (exchange.getQueryParameters().containsKey(WRITE_CONCERN_QPARAM_KEY)) {
rsOps = rsOps.withWriteConcern(exchange.getQueryParameters().get(WRITE_CONCERN_QPARAM_KEY).getFirst());
_rsOps = _rsOps.withWriteConcern(exchange.getQueryParameters().get(WRITE_CONCERN_QPARAM_KEY).getFirst());
anyRsSet = true;
}

this.rsOps = anyRsSet ? Optional.of(rsOps) : Optional.empty();
LOGGER.debug("ReplicaSet connection options: {}", rsOps);
this.rsOps = anyRsSet ? Optional.of(_rsOps) : Optional.empty();
LOGGER.debug("ReplicaSet connection options: {}", _rsOps);
}

private String defaultWriteMode() {
Expand Down Expand Up @@ -655,7 +686,7 @@ public String getSid() {
* @return the txn id or null if request type is not TRANSACTION
*/
public Long getTxnId() {
return isTxn() ? Long.parseLong(getPathTokenAt(4)) : null;
return isTxn() ? Long.valueOf(getPathTokenAt(4)) : null;
}

/**
Expand Down Expand Up @@ -1093,7 +1124,7 @@ public boolean isContentInjected() {
return this.wrapped.getAttachment(CONTENT_INJECTED);
}

public void setContentInjected(boolean value) {
public final void setContentInjected(boolean value) {
this.wrapped.putAttachment(CONTENT_INJECTED, value);
}

Expand Down Expand Up @@ -1599,7 +1630,7 @@ public boolean isNoCache() {
/**
* ReplicaSet connection otpions
*
* @return the rsOps
* @return the _rsOps
*/
public Optional<RSOps> rsOps() {
return rsOps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.mongodb.client.AggregateIterable;

import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLList;


public class GQLAggregationDataFetcher extends GraphQLDataFetcher {
Expand Down Expand Up @@ -99,12 +100,14 @@ public Object get(DataFetchingEnvironment env) throws Exception {
.allowDiskUse(aggregation.getAllowDiskUse().getValue())
.maxTime(this.aggregationTimeLimit, TimeUnit.MILLISECONDS);

var stageOutput = new ArrayList<BsonDocument>();
boolean isMultiple = env.getFieldDefinition().getType() instanceof GraphQLList;

if (res != null) {
res.into(stageOutput);
if (isMultiple) {
var aggregationResult = new BsonArray();
res.into(aggregationResult.asArray());
return aggregationResult;
} else {
return res.first();
}

return stageOutput;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@

package org.restheart.metrics;

import static org.restheart.metrics.MetricsService.METRICS_REGISTRIES_PREFIX;
import org.restheart.plugins.Initializer;
import org.restheart.plugins.RegisterPlugin;
import static org.restheart.metrics.MetricsService.METRICS_REGISTRIES_PREFIX;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.codahale.metrics.SharedMetricRegistries;
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
import com.codahale.metrics.jvm.MemoryUsageGaugeSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RegisterPlugin(name="jvmMetricsCollector", description = "registers the JVM metrics", enabledByDefault = false)
public class JvmMetricsCollector implements Initializer {
private static final Logger LOGGER = LoggerFactory.getLogger(JvmMetricsCollector.class);

@Override
public void init() {
var registry = SharedMetricRegistries.getOrCreate(METRICS_REGISTRIES_PREFIX + "/jvm");
registry.registerAll("jvm mem", new MemoryUsageGaugeSet());
Expand Down

0 comments on commit d96ee5e

Please sign in to comment.