This repository has been archived by the owner on Dec 29, 2024. It is now read-only.
forked from helun/Ektorp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix test, add first naive draft for mangoquery (helun/helun#286)
- Loading branch information
Showing
10 changed files
with
393 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.ektorp.mongoquery; | ||
|
||
import com.fasterxml.jackson.annotation.JsonValue; | ||
|
||
import java.io.Serializable; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Expression implements Serializable { | ||
|
||
private String left; | ||
private Object right; | ||
|
||
public String getLeft() { | ||
return left; | ||
} | ||
|
||
public void setLeft(String left) { | ||
this.left = left; | ||
} | ||
|
||
public Object getRight() { | ||
return right; | ||
} | ||
|
||
public void setRight(Object right) { | ||
this.right = right; | ||
} | ||
|
||
@JsonValue | ||
public Map<String, Object> toJson() { | ||
Map<String, Object> result = new HashMap<>(); | ||
result.put(getLeft(), getRight()); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.ektorp.mongoquery; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonInclude.Include; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.util.List; | ||
|
||
@JsonInclude(Include.NON_NULL) | ||
public class MangoQuery { | ||
|
||
public enum Stale { | ||
OK("ok"), | ||
FALSE("false"); | ||
|
||
private String jsonName; | ||
|
||
Stale(String jsonName) { | ||
this.jsonName = jsonName; | ||
} | ||
|
||
public String getJsonName() { | ||
return jsonName; | ||
} | ||
} | ||
|
||
// object describing criteria used to select documents. | ||
private Expression selector; | ||
|
||
// Maximum number of results returned. Default is 25. Optional | ||
private Integer limit; | ||
|
||
// Skip the first ‘n’ results, where ‘n’ is the value specified. Optional | ||
private Integer skip; | ||
|
||
// contains a list of field name and direction pairs | ||
private List<Sort> sort; | ||
|
||
// specifying which fields of each object should be returned. If it is omitted, the entire | ||
// object is returned. | ||
private List<String> fields; | ||
|
||
// Instruct a query to use a specific index. | ||
@JsonProperty("use_index") | ||
private String useIndex; | ||
|
||
// Read quorum needed for the result. This defaults to 1, in which case the document found in | ||
// the index is returned. If set to a higher value, each document is read from at least that | ||
// many replicas before it is returned in the results. This is likely to take more time than | ||
// using only the document stored locally with the index. | ||
@JsonProperty("r") | ||
private Integer readQuorum; | ||
|
||
// A string that enables you to specify which page of results you require. Used for paging | ||
// through result sets. Every query returns an opaque string under the bookmark key that can | ||
// then be passed back in a query to get the next page of results. If any part of the selector | ||
// query changes between requests, the results are undefined. | ||
private String bookmark; | ||
|
||
// Whether to update the index prior to returning the result. Default is true. | ||
private Boolean update; | ||
|
||
// Whether or not the view results should be returned from a “stable” set of shards | ||
private Boolean stable; | ||
|
||
// Combination of update=false and stable=true options. (Default: false) | ||
private Stale stale; | ||
|
||
// Include execution statistics in the query response. (Default: false) | ||
@JsonProperty("execution_stats") | ||
private Boolean executionStats; | ||
|
||
|
||
|
||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/org/ektorp/mongoquery/MangoQueryBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.ektorp.mongoquery; | ||
|
||
import com.fasterxml.jackson.annotation.JsonValue; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class MangoQueryBuilder { | ||
|
||
private Expression expression; | ||
|
||
public MangoQueryBuilder or(Operator... operators) { | ||
return combinationOperators("$or", operators); | ||
} | ||
|
||
public MangoQueryBuilder and(Operator... operators) { | ||
return combinationOperators("$and", operators); | ||
} | ||
|
||
private MangoQueryBuilder combinationOperators(String oper, Operator... operators) { | ||
expression = new Expression(); | ||
expression.setLeft(oper); | ||
|
||
if (operators.length > 1) { | ||
List<Expression> expressions = new LinkedList<>(); | ||
for (Operator operator : operators) { | ||
Expression e = new Expression(); | ||
e.setLeft(operator.getFieldName()); | ||
e.setRight(operator.getExpression()); | ||
|
||
expressions.add(e); | ||
} | ||
expression.setRight(expressions); | ||
} else if (operators.length == 1){ | ||
Expression e = new Expression(); | ||
e.setLeft(operators[0].getFieldName()); | ||
e.setRight(operators[0].getExpression()); | ||
|
||
expression = e; | ||
} | ||
|
||
return this; | ||
} | ||
|
||
@JsonValue | ||
public Map<String, Object> toJson() { | ||
return this.expression.toJson(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.ektorp.mongoquery; | ||
|
||
import com.fasterxml.jackson.annotation.JsonValue; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Operator { | ||
|
||
private String fieldName; | ||
private Expression expression; | ||
|
||
public void setFieldName(String fieldName) { | ||
this.fieldName = fieldName; | ||
} | ||
|
||
public void setExpression(Expression expression) { | ||
this.expression = expression; | ||
} | ||
|
||
@JsonValue | ||
public Map<String, Object> toJson() { | ||
Map<String, Object> outerMap = new HashMap<>(); | ||
outerMap.put(this.fieldName, this.expression); | ||
return outerMap; | ||
} | ||
|
||
public static class SimpleCompareOperator extends Operator { | ||
|
||
private String comparator; | ||
private Object value; | ||
|
||
public SimpleCompareOperator(String fieldName, String comparator, Object value) { | ||
this.setFieldName(fieldName); | ||
Expression exp = new Expression(); | ||
exp.setLeft(comparator); | ||
exp.setRight(value); | ||
this.setExpression(exp); | ||
} | ||
} | ||
|
||
public static class EqualOperator extends SimpleCompareOperator { | ||
|
||
private static final String COMPARATOR = "$eq"; | ||
|
||
public EqualOperator(String fieldName, Object value) { | ||
super(fieldName, COMPARATOR, value); | ||
} | ||
} | ||
|
||
public static class ExistsOperator extends SimpleCompareOperator { | ||
|
||
private static final String COMPARATOR = "$in"; | ||
|
||
public ExistsOperator(String fieldName, Boolean value) { | ||
super(fieldName, COMPARATOR, value); | ||
} | ||
} | ||
|
||
|
||
public String getFieldName() { | ||
return fieldName; | ||
} | ||
|
||
public Expression getExpression() { | ||
return expression; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.ektorp.mongoquery; | ||
|
||
import java.io.Serializable; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Sort implements Serializable { | ||
|
||
public enum SortOrder { | ||
DESC("desc"), | ||
ASC("asc"); | ||
|
||
private String jsonName; | ||
|
||
SortOrder(String jsonName) { | ||
this.jsonName = jsonName; | ||
} | ||
|
||
public String getJsonName() { | ||
return this.jsonName; | ||
} | ||
} | ||
|
||
private String fieldName; | ||
|
||
private SortOrder sortOrder; | ||
|
||
public Sort(String fieldName, SortOrder sortOrder) { | ||
this.fieldName = fieldName; | ||
this.sortOrder = sortOrder; | ||
} | ||
|
||
public Sort(String fieldName) { | ||
this(fieldName, SortOrder.ASC); | ||
} | ||
|
||
public Map<String, String> toJson() { | ||
Map<String, String> result = new HashMap<>(); | ||
result.put(fieldName, sortOrder.getJsonName()); | ||
return result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.