Skip to content

Commit

Permalink
Aggregation - use aggregation pipeline to replace group #33
Browse files Browse the repository at this point in the history
  • Loading branch information
greenlaw110 committed Jun 16, 2019
1 parent b2548aa commit 02e2bfe
Show file tree
Hide file tree
Showing 9 changed files with 583 additions and 528 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
1.7.0

* Improve aggregation API #32
* Aggregation - use aggregation pipeline to replace group #33

1.6.7 20/Apr/2019
* update to act 1.8.20
Expand Down
103 changes: 26 additions & 77 deletions src/main/java/act/db/morphia/MorphiaDaoBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import act.app.App;
import act.db.*;
import act.db.morphia.util.AggregationResult;
import act.db.morphia.util.DoubleResult;
import act.db.morphia.util.LongResult;
import act.db.util.AuditHelper;
import act.event.EventBus;
import act.util.General;
Expand Down Expand Up @@ -398,109 +396,60 @@ public UpdateOperations<MODEL_TYPE> createUpdateOperations() {
return updates();
}

public MorphiaQuery.GroupBy groupBy(String... groupKeys) {
return defQuery.groupBy(groupKeys);
public SimpleAggregation aggregation() {
return defQuery.aggregation();
}

public DoubleResult groupMax(String field, String... groupKeys) {
return defQuery.groupMax(field, groupKeys);
}

public AggregationResult<Integer> intGroupMax(String field, String... groupKeys) {
return defQuery.intGroupMax(field, groupKeys);
}

public AggregationResult<Long> longGroupMax(String field, String... groupKeys) {
return defQuery.longGroupMax(field, groupKeys);
public MorphiaQuery.GroupByStage groupBy(String groupKey, String... otherGroupKeys) {
return defQuery.groupBy(groupKey, otherGroupKeys);
}

public Double max(String maxField) {
return groupMax(maxField).getDefault();
}

public Integer intMax(String field) {
return intGroupMax(field).getDefault();
return defQuery.max(maxField);
}

public Long longMax(String field) {
return longGroupMax(field).getDefault();
public Integer intMax(String maxField) {
return defQuery.intMax(maxField);
}

public DoubleResult groupMin(String field, String... groupKeys) {
return defQuery.groupMin(field, groupKeys);
}

public AggregationResult<Integer> intGroupMin(String field, String... groupKeys) {
return defQuery.intGroupMin(field, groupKeys);
}

public AggregationResult<Long> longGroupMin(String field, String... groupKeys) {
return defQuery.longGroupMin(field, groupKeys);
public Long longMax(String maxField) {
return defQuery.longMax(maxField);
}

public Double min(String minField) {
return groupMin(minField).getDefault();
}

public Integer intMin(String field) {
return intGroupMin(field).getDefault();
return defQuery.min(minField);
}

public Long longMin(String field) {
return longGroupMin(field).getDefault();
public Integer intMin(String minField) {
return defQuery.intMin(minField);
}

public DoubleResult groupAverage(String field, String... groupKeys) {
return defQuery.groupAverage(field, groupKeys);
public Long longMin(String minField) {
return defQuery.longMin(minField);
}

public AggregationResult<Integer> intGroupAverage(String field, String... groupKeys) {
return defQuery.intGroupAverage(field, groupKeys);
public Double average(String averageField) {
return defQuery.average(averageField);
}

public AggregationResult<Long> longGroupAverage(String field, String... groupKeys) {
return defQuery.longGroupAverage(field, groupKeys);
public Integer intAverage(String averageField) {
return defQuery.intAverage(averageField);
}

public Double average(String field) {
return groupAverage(field).getDefault();
public Long longAverage(String averageField) {
return defQuery.longAverage(averageField);
}

public Integer intAverage(String field) {
return intGroupAverage(field).getDefault();
}

public Long longAverage(String field) {
return longGroupAverage(field).getDefault();
}


public DoubleResult groupSum(String field, String... groupKeys) {
return defQuery.groupSum(field, groupKeys);
}

public AggregationResult<Integer> intGroupSum(String field, String... groupKeys) {
return defQuery.intGroupSum(field, groupKeys);
}

public AggregationResult<Long> longGroupSum(String field, String... groupKeys) {
return defQuery.longGroupSum(field, groupKeys);
}

public Double sum(String field) {
return groupSum(field).getDefault();
}

public Integer intSum(String field) {
return intGroupSum(field).getDefault();
public Double sum(String sumField) {
return defQuery.sum(sumField);
}

public Long longSum(String field) {
return longGroupSum(field).getDefault();
public Integer intSum(String sumField) {
return defQuery.intSum(sumField);
}

public LongResult groupCount(String... groupKeys) {
return defQuery.groupCount(groupKeys);
public Long longSum(String sumField) {
return defQuery.longSum(sumField);
}

private Map<String, Object> kvList(String keys, Object... values) {
Expand Down
32 changes: 31 additions & 1 deletion src/main/java/act/db/morphia/MorphiaPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import act.db.DbService;
import act.db.morphia.annotation.PersistAsList;
import act.db.morphia.annotation.PersistAsMap;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.logging.MorphiaLoggerFactory;
Expand Down Expand Up @@ -95,6 +97,34 @@ public DbService initDbService(String id, App app, Map<String, String> conf) {
return new MorphiaService(id, app, conf);
}


/**
* Helper function to construct {@link DBObject}. Usage:
*
* ```
* DBObject dbo = dbo("k1", 1, "k2", dbo("k3", "foo"));
* ```
*
* @param key the first key
* @param val the first val
* @param others other key value pairs.
* @return An instance of {@link DBObject}
*/
public static DBObject dbo(String key, Object val, Object ... others) {
DBObject retVal = new BasicDBObject();
retVal.put(key, val);
int len = others.length;
if (len == 0) {
return retVal;
}
for (int i = 0; i < len; i += 2) {
String k = (String) others[i];
Object v = null;
if (i + 1 < len) {
v = (Object) others[i + 1];
}
retVal.put(k, v);
}
return retVal;
}

}
Loading

0 comments on commit 02e2bfe

Please sign in to comment.