#304 - ElasticSearch integration part 1 / doc store integration

This commit is contained in:
Robin Bygrave committed 2016-03-01 15:36:44 +13:00
1 parent cb1fe297d6
commit 8d3b4a0407
209 files changed
+6736 -573

No files matched your search

@@ -8,6 +8,7 @@ import com.avaje.ebeaninternal.api.SpiExpressionValidation;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.el.ElPropertyDeploy;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
@@ -26,6 +27,22 @@ class AllEqualsExpression extends NonPrepareExpression {
return propName;
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
context.writeBoolMustStart();
for (Map.Entry<String, Object> entry : propMap.entrySet()) {
Object value = entry.getValue();
String propName = entry.getKey();
if (value == null) {
context.writeExists(false, propName);
} else {
context.writeTerm(propName, value);
}
}
context.writeBoolEnd();
}
@Override
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
if (propMap != null) {
@@ -4,6 +4,8 @@ import com.avaje.ebeaninternal.api.HashQueryPlanBuilder;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import java.io.IOException;
class BetweenExpression extends AbstractExpression {
private static final long serialVersionUID = 2078918165221454910L;
@@ -20,6 +22,11 @@ class BetweenExpression extends AbstractExpression {
this.valueHigh = valHigh;
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
context.writeRange(propName, Op.GT_EQ, valueLow, Op.LT_EQ, valueHigh);
}
@Override
public void addBindValues(SpiExpressionRequest request) {
request.addBindValue(valueLow);
@@ -8,6 +8,8 @@ import com.avaje.ebeaninternal.api.SpiExpressionValidation;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.el.ElPropertyDeploy;
import java.io.IOException;
/**
* Between expression where a value is between two properties.
*/
@@ -31,6 +33,15 @@ class BetweenPropertyExpression extends NonPrepareExpression {
return propName;
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
context.writeBoolMustStart();
context.writeSimple(Op.LT_EQ, lowProperty, value);
context.writeSimple(Op.GT_EQ, highProperty, value);
context.writeBoolEnd();
}
@Override
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
@@ -5,6 +5,8 @@ import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
import java.io.IOException;
class CaseInsensitiveEqualExpression extends AbstractExpression {
private static final long serialVersionUID = -6406036750998971064L;
@@ -16,6 +18,21 @@ class CaseInsensitiveEqualExpression extends AbstractExpression {
this.value = value.toLowerCase();
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
String[] values = value.split(" ");
if (values.length == 1) {
context.writeMatch(propName, value);
} else {
context.writeBoolStart(true);
for (String val : values) {
context.writeMatch(propName, val);
}
context.writeBoolEnd();
}
}
@Override
public void addBindValues(SpiExpressionRequest request) {
@@ -1,5 +1,6 @@
package com.avaje.ebeaninternal.server.expression;
import java.io.IOException;
import java.util.ArrayList;
import com.avaje.ebean.ExampleExpression;
@@ -94,6 +95,17 @@ public class DefaultExampleExpression implements SpiExpression, ExampleExpressio
}
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
if (!list.isEmpty()) {
context.writeBoolMustStart();
for (SpiExpression expr : list) {
expr.writeElastic(context);
}
context.writeBoolEnd();
}
}
@Override
public SpiExpression copyForPlanKey() {
return new DefaultExampleExpression(list);
@@ -306,7 +306,7 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
*/
@Override
public Expression exists(Query<?> subQuery) {
return new ExistsExpression((SpiQuery<?>) subQuery, false);
return new ExistsQueryExpression((SpiQuery<?>) subQuery, false);
}
/**
@@ -314,7 +314,7 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
*/
@Override
public Expression notExists(Query<?> subQuery) {
return new ExistsExpression((SpiQuery<?>) subQuery, true);
return new ExistsQueryExpression((SpiQuery<?>) subQuery, true);
}
@@ -10,6 +10,7 @@ import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.api.SpiExpressionValidation;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
@@ -59,6 +60,33 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
this(null, null, null, new ArrayList<SpiExpression>());
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
writeElastic(context, null);
}
public void writeElastic(ElasticExpressionContext context, SpiExpression idEquals) throws IOException {
int size = list.size();
if (size == 1 && idEquals == null) {
// only 1 expression - skip bool must
list.get(0).writeElastic(context);
} else if (size == 0 && idEquals != null) {
// only idEquals - skip bool must
idEquals.writeElastic(context);
} else {
// bool must wrap all the children
context.writeBoolMustStart();
if (idEquals != null) {
idEquals.writeElastic(context);
}
for (int i = 0; i < size; i++) {
list.get(i).writeElastic(context);
}
context.writeBoolEnd();
}
}
@Override
public SpiExpressionList<?> trimPath(int prefixTrim) {
throw new RuntimeException("Only allowed on FilterExpressionList");
@@ -382,7 +410,7 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
return false;
}
DefaultExpressionList<?> that = (DefaultExpressionList<?>)other;
DefaultExpressionList<?> that = (DefaultExpressionList<?>) other;
if (list.size() != that.list.size()) {
return false;
}
@@ -396,7 +424,7 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
@Override
public boolean isSameByBind(SpiExpression other) {
DefaultExpressionList<?> that = (DefaultExpressionList<?>)other;
DefaultExpressionList<?> that = (DefaultExpressionList<?>) other;
if (list.size() != that.list.size()) {
return false;
}
@@ -421,16 +449,16 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
* Path does not exist - for the given path in a JSON document.
*/
@Override
public ExpressionList<T> jsonNotExists(String propertyName, String path){
public ExpressionList<T> jsonNotExists(String propertyName, String path) {
add(expr.jsonNotExists(propertyName, path));
return this;
}
/**
* Equal to expression for the value at the given path in the JSON document.
*/
@Override
public ExpressionList<T> jsonEqualTo(String propertyName, String path, Object value){
public ExpressionList<T> jsonEqualTo(String propertyName, String path, Object value) {
add(expr.jsonEqualTo(propertyName, path, value));
return this;
}
@@ -439,7 +467,7 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
* Not Equal to - for the given path in a JSON document.
*/
@Override
public ExpressionList<T> jsonNotEqualTo(String propertyName, String path, Object val){
public ExpressionList<T> jsonNotEqualTo(String propertyName, String path, Object val) {
add(expr.jsonNotEqualTo(propertyName, path, val));
return this;
}
@@ -448,7 +476,7 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
* Greater than - for the given path in a JSON document.
*/
@Override
public ExpressionList<T> jsonGreaterThan(String propertyName, String path, Object val){
public ExpressionList<T> jsonGreaterThan(String propertyName, String path, Object val) {
add(expr.jsonGreaterThan(propertyName, path, val));
return this;
}
@@ -457,7 +485,7 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
* Greater than or equal to - for the given path in a JSON document.
*/
@Override
public ExpressionList<T> jsonGreaterOrEqual(String propertyName, String path, Object val){
public ExpressionList<T> jsonGreaterOrEqual(String propertyName, String path, Object val) {
add(expr.jsonGreaterOrEqual(propertyName, path, val));
return this;
}
@@ -466,7 +494,7 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
* Less than - for the given path in a JSON document.
*/
@Override
public ExpressionList<T> jsonLessThan(String propertyName, String path, Object val){
public ExpressionList<T> jsonLessThan(String propertyName, String path, Object val) {
add(expr.jsonLessThan(propertyName, path, val));
return this;
}
@@ -475,7 +503,7 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
* Less than or equal to - for the given path in a JSON document.
*/
@Override
public ExpressionList<T> jsonLessOrEqualTo(String propertyName, String path, Object val){
public ExpressionList<T> jsonLessOrEqualTo(String propertyName, String path, Object val) {
add(expr.jsonLessOrEqualTo(propertyName, path, val));
return this;
}
@@ -484,7 +512,7 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
* Between - for the given path in a JSON document.
*/
@Override
public ExpressionList<T> jsonBetween(String propertyName, String path, Object lowerValue, Object upperValue){
public ExpressionList<T> jsonBetween(String propertyName, String path, Object lowerValue, Object upperValue) {
add(expr.jsonBetween(propertyName, path, lowerValue, upperValue));
return this;
}
@@ -0,0 +1,303 @@
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebean.OrderBy;
import com.avaje.ebean.plugin.BeanType;
import com.avaje.ebean.plugin.ExpressionPath;
import com.avaje.ebeaninternal.server.query.SplitName;
import com.fasterxml.jackson.core.JsonGenerator;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Context for writing elastic search expressions.
*/
public class ElasticExpressionContext {
public static final String MUST = "must";
public static final String SHOULD = "should";
public static final String MUST_NOT = "must_not";
public static final String BOOL = "bool";
public static final String TERM = "term";
public static final String RANGE = "range";
public static final String TERMS = "terms";
public static final String IDS = "ids";
public static final String VALUES = "values";
public static final String PREFIX = "prefix";
public static final String MATCH = "match";
public static final String WILDCARD = "wildcard";
public static final String EXISTS = "exists";
public static final String FIELD = "field";
private final JsonGenerator json;
private final BeanType<?> desc;
private String currentNestedPath;
public ElasticExpressionContext(JsonGenerator json, BeanType<?> desc) {
this.json = json;
this.desc = desc;
}
/**
* Return the JsonGenerator.
*/
public JsonGenerator json() {
return json;
}
/**
* Flush the JsonGenerator buffer.
*/
public void flush() throws IOException {
endNested();
json.flush();
}
/**
* Return true if the path contains a many.
*/
public boolean containsMany(String path) {
ExpressionPath elPath = desc.getExpressionPath(path);
return elPath == null || elPath.containsMany();
}
/**
* Return an associated 'raw' property given the property name.
*/
private String rawProperty(String propertyName) {
return desc.docStore().rawProperty(propertyName);
}
public void writeBoolStart(boolean conjunction) throws IOException {
writeBoolStart((conjunction) ? MUST : SHOULD);
}
public void writeBoolMustStart() throws IOException {
writeBoolStart(MUST);
}
public void writeBoolMustNotStart() throws IOException {
writeBoolStart(MUST_NOT);
}
private void writeBoolStart(String type) throws IOException {
endNested();
json.writeStartObject();
json.writeObjectFieldStart(BOOL);
json.writeArrayFieldStart(type);
}
public void writeBoolEnd() throws IOException {
json.writeEndArray();
json.writeEndObject();
json.writeEndObject();
}
public void writeTerm(String propertyName, Object value) throws IOException {
writeRawType(TERM, rawProperty(propertyName), value);
}
public void writeRange(String propertyName, String rangeType, Object value) throws IOException {
prepareNestedPath(propertyName);
json.writeStartObject();
json.writeObjectFieldStart(RANGE);
json.writeObjectFieldStart(rawProperty(propertyName));
json.writeFieldName(rangeType);
json.writeObject(value);
json.writeEndObject();
json.writeEndObject();
json.writeEndObject();
}
public void writeRange(String propertyName, Op lowOp, Object valueLow, Op highOp, Object valueHigh) throws IOException {
prepareNestedPath(propertyName);
json.writeStartObject();
json.writeObjectFieldStart(RANGE);
json.writeObjectFieldStart(rawProperty(propertyName));
json.writeFieldName(lowOp.docExp());
json.writeObject(valueLow);
json.writeFieldName(highOp.docExp());
json.writeObject(valueHigh);
json.writeEndObject();
json.writeEndObject();
json.writeEndObject();
}
public void writeTerms(String propertyName, Object[] values) throws IOException {
prepareNestedPath(propertyName);
json.writeStartObject();
json.writeObjectFieldStart(TERMS);
json.writeArrayFieldStart(rawProperty(propertyName));
for (Object value : values) {
json.writeObject(value);
}
json.writeEndArray();
json.writeEndObject();
json.writeEndObject();
}
public void writeIds(List<?> idList) throws IOException {
endNested();
json.writeStartObject();
json.writeObjectFieldStart(IDS);
json.writeArrayFieldStart(VALUES);
for (Object id : idList) {
json.writeObject(id);
}
json.writeEndArray();
json.writeEndObject();
json.writeEndObject();
}
public void writeId(Object value) throws IOException {
List<Object> ids = new ArrayList<Object>(1);
ids.add(value);
writeIds(ids);
}
public void writeSuffix(String propertyName, String value) {
throw new IllegalArgumentException("Not implemented yet. Could search for a mapped 'reversed' property and do prefix query");
}
public void writePrefix(String propertyName, String value) throws IOException {
// use analysed field
prepareNestedPath(propertyName);
writeRawType(PREFIX, propertyName, value);
}
public void writeMatch(String propertyName, String value) throws IOException {
// use analysed field
prepareNestedPath(propertyName);
writeRawType(MATCH, propertyName, value);
}
public void writeWildcard(String propertyName, String value) throws IOException {
prepareNestedPath(propertyName);
writeRawType(WILDCARD, propertyName, value);
}
public void writeRaw(String jsonExpression) throws IOException {
json.writeRaw(jsonExpression);
}
public void writeExists(boolean notNull, String propertyName) throws IOException {
prepareNestedPath(propertyName);
if (!notNull) {
writeBoolMustNotStart();
}
writeExists(propertyName);
if (!notNull) {
writeBoolEnd();
}
}
private void writeExists(String propertyName) throws IOException {
writeRawType(EXISTS, FIELD, propertyName);
}
private void writeRawType(String type, String propertyName, Object value) throws IOException {
prepareNestedPath(propertyName);
json.writeStartObject();
json.writeObjectFieldStart(type);
json.writeFieldName(propertyName);
json.writeObject(value);
json.writeEndObject();
json.writeEndObject();
}
public void writeSimple(Op type, String propertyName, Object value) throws IOException {
prepareNestedPath(propertyName);
switch (type) {
case EQ:
writeTerm(propertyName, value);
break;
case NOT_EQ:
writeBoolMustNotStart();
writeTerm(propertyName, value);
writeBoolEnd();
break;
case EXISTS:
writeExists(true, propertyName);
break;
case NOT_EXISTS:
writeExists(false, propertyName);
break;
case BETWEEN:
throw new IllegalStateException("BETWEEN Not expected in SimpleExpression?");
default:
writeRange(propertyName, type.docExp(), value);
}
}
/**
* Write the query sort.
*/
public <T> void writeOrderBy(OrderBy<T> orderBy) throws IOException {
if (orderBy != null && !orderBy.isEmpty()) {
json.writeArrayFieldStart("sort");
for (OrderBy.Property property : orderBy.getProperties()) {
json.writeStartObject();
json.writeObjectFieldStart(rawProperty(property.getProperty()));
json.writeStringField("order", property.isAscending() ? "asc" : "desc");
json.writeEndObject();
json.writeEndObject();
}
json.writeEndArray();
}
}
private void prepareNestedPath(String propName) throws IOException {
ExpressionPath exprPath = desc.getExpressionPath(propName);
if (exprPath != null && exprPath.containsMany()) {
String[] manyPath = SplitName.splitBegin(propName);
startNested(manyPath[0]);
} else {
endNested();
}
}
private void startNested(String nestedPath) throws IOException {
if (currentNestedPath != null) {
if (currentNestedPath.equals(nestedPath)) {
// just add to currentNestedPath
return;
} else {
endNested();
}
}
currentNestedPath = nestedPath;
json.writeStartObject();
json.writeObjectFieldStart("nested");
json.writeStringField("path", nestedPath);
json.writeFieldName("filter");
}
private void endNested() throws IOException {
if (currentNestedPath != null) {
currentNestedPath = null;
//json.writeEndObject();
json.writeEndObject();
json.writeEndObject();
}
}
}
@@ -1,5 +1,6 @@
package com.avaje.ebeaninternal.server.expression;
import java.io.IOException;
import java.util.List;
import com.avaje.ebean.event.BeanQueryRequest;
@@ -13,7 +14,7 @@ import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.query.CQuery;
public class ExistsExpression implements SpiExpression {
class ExistsQueryExpression implements SpiExpression, UnsupportedDocStoreExpression {
private static final long serialVersionUID = 666990277309851644L;
@@ -25,18 +26,23 @@ public class ExistsExpression implements SpiExpression {
protected String sql;
public ExistsExpression(SpiQuery<?> subQuery, boolean not) {
public ExistsQueryExpression(SpiQuery<?> subQuery, boolean not) {
this.subQuery = subQuery;
this.not = not;
}
ExistsExpression(boolean not, String sql , List<Object> bindParams) {
ExistsQueryExpression(boolean not, String sql , List<Object> bindParams) {
this.not = not;
this.sql = sql;
this.bindParams = bindParams;
this.subQuery = null;
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
throw new IllegalStateException("Not supported");
}
@Override
public void prepareExpression(BeanQueryRequest<?> request) {
@@ -60,7 +66,7 @@ public class ExistsExpression implements SpiExpression {
@Override
public void queryPlanHash(HashQueryPlanBuilder builder) {
builder.add(ExistsExpression.class).add(not);
builder.add(ExistsQueryExpression.class).add(not);
builder.add(sql).add(bindParams.size());
}
@@ -90,11 +96,11 @@ public class ExistsExpression implements SpiExpression {
@Override
public boolean isSameByPlan(SpiExpression other) {
if (!(other instanceof ExistsExpression)) {
if (!(other instanceof ExistsQueryExpression)) {
return false;
}
ExistsExpression that = (ExistsExpression) other;
ExistsQueryExpression that = (ExistsQueryExpression) other;
return this.sql.equals(that.sql)
&& this.not == that.not
&& this.bindParams.size() == that.bindParams.size();
@@ -102,7 +108,7 @@ public class ExistsExpression implements SpiExpression {
@Override
public boolean isSameByBind(SpiExpression other) {
ExistsExpression that = (ExistsExpression) other;
ExistsQueryExpression that = (ExistsQueryExpression) other;
if (this.bindParams.size() != that.bindParams.size()) {
return false;
}
@@ -7,6 +7,8 @@ import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.api.SpiExpressionValidation;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import java.io.IOException;
/**
* Slightly redundant as Query.setId() ultimately also does the same job.
*/
@@ -20,6 +22,11 @@ class IdExpression extends NonPrepareExpression implements SpiExpression {
this.value = value;
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
context.writeId(value);
}
/**
* Always returns false.
*/
@@ -8,6 +8,7 @@ import com.avaje.ebeaninternal.api.SpiExpressionValidation;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.id.IdBinder;
import java.io.IOException;
import java.util.List;
/**
@@ -27,6 +28,11 @@ public class IdInExpression extends NonPrepareExpression {
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
context.writeIds(idList);
}
@Override
public void validate(SpiExpressionValidation validation) {
// always valid
@@ -6,6 +6,7 @@ import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
import java.io.IOException;
import java.util.Collection;
class InExpression extends AbstractExpression {
@@ -28,6 +29,11 @@ class InExpression extends AbstractExpression {
this.not = not;
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
context.writeTerms(propName, values);
}
@Override
public void addBindValues(SpiExpressionRequest request) {
@@ -8,12 +8,13 @@ import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.query.CQuery;
import java.io.IOException;
import java.util.List;
/**
* In expression using a sub query.
*/
class InQueryExpression extends AbstractExpression {
class InQueryExpression extends AbstractExpression implements UnsupportedDocStoreExpression {
private static final long serialVersionUID = 666990277309851644L;
@@ -39,6 +40,11 @@ class InQueryExpression extends AbstractExpression {
this.bindParams = bindParams;
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
throw new IllegalStateException("Not supported");
}
@Override
public void prepareExpression(BeanQueryRequest<?> request) {
@@ -4,6 +4,8 @@ import com.avaje.ebeaninternal.api.HashQueryPlanBuilder;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import java.io.IOException;
/**
* Generally speaking tests the value at a given path in the JSON document.
* <p>
@@ -57,6 +59,17 @@ class JsonPathExpression extends AbstractExpression {
this.upperValue = upperValue;
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
String fullName = propName + "." + path;
if (operator == Op.BETWEEN) {
context.writeRange(fullName, Op.GT_EQ, value, Op.LT_EQ, upperValue);
} else {
context.writeSimple(operator, fullName, value);
}
}
@Override
public void queryPlanHash(HashQueryPlanBuilder builder) {
builder.add(JsonPathExpression.class).add(propName).add(path).add(operator);
@@ -22,6 +22,7 @@ import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.api.SpiExpressionValidation;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.List;
@@ -98,6 +99,17 @@ abstract class JunctionExpression<T> implements Junction<T>, SpiExpression, Expr
this.exprList = exprList;
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
context.writeBoolStart(!disjunction);
List<SpiExpression> list = exprList.internalList();
for (int i = 0; i < list.size(); i++) {
list.get(i).writeElastic(context);
}
context.writeBoolEnd();
}
@Override
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
@@ -126,8 +138,7 @@ abstract class JunctionExpression<T> implements Junction<T>, SpiExpression, Expr
@Override
public Junction<T> add(Expression item) {
SpiExpression i = (SpiExpression) item;
exprList.add(i);
exprList.add(item);
return this;
}
@@ -143,8 +154,7 @@ abstract class JunctionExpression<T> implements Junction<T>, SpiExpression, Expr
List<SpiExpression> list = exprList.internalList();
for (int i = 0; i < list.size(); i++) {
SpiExpression item = list.get(i);
item.addBindValues(request);
list.get(i).addBindValues(request);
}
}
@@ -6,6 +6,8 @@ import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
import java.io.IOException;
class LikeExpression extends AbstractExpression {
private static final long serialVersionUID = -5398151809111172380L;
@@ -23,6 +25,36 @@ class LikeExpression extends AbstractExpression {
this.val = value;
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
String paramVal = (caseInsensitive) ? val.toLowerCase() : val;
switch (type) {
case RAW:
context.writeWildcard(propName, paramVal);
break;
case STARTS_WITH:
context.writePrefix(propName, paramVal);
break;
case ENDS_WITH:
context.writeSuffix(propName, paramVal);
break;
case CONTAINS:
context.writeMatch(propName, paramVal);
break;
case EQUAL_TO:
context.writeTerm(propName, paramVal);
break;
default:
throw new RuntimeException("LikeType " + type + " missed?");
}
}
@Override
public void addBindValues(SpiExpressionRequest request) {
@@ -9,6 +9,8 @@ import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.api.SpiExpressionValidation;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import java.io.IOException;
/**
* A logical And or Or for joining two expressions.
*/
@@ -60,6 +62,16 @@ abstract class LogicExpression implements SpiExpression {
this.expTwo = (SpiExpression) expTwo;
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
boolean conjunction = joinType.equals(AND);
context.writeBoolStart(conjunction);
expOne.writeElastic(context);
expTwo.writeElastic(context);
context.writeBoolEnd();
}
@Override
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
expOne.containsMany(desc, manyWhereJoin);
@@ -8,6 +8,8 @@ import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.api.SpiExpressionValidation;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import java.io.IOException;
/**
* Effectively an expression that has no effect.
*/
@@ -20,6 +22,10 @@ class NoopExpression implements SpiExpression {
return this;
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
}
@Override
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins whereManyJoins) {
// nothing to do
@@ -9,6 +9,8 @@ import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.api.SpiExpressionValidation;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import java.io.IOException;
final class NotExpression implements SpiExpression {
private static final long serialVersionUID = 5648926732402355781L;
@@ -22,6 +24,13 @@ final class NotExpression implements SpiExpression {
this.exp = (SpiExpression) exp;
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
context.writeBoolMustNotStart();
exp.writeElastic(context);
context.writeBoolEnd();
}
@Override
public SpiExpression copyForPlanKey() {
return new NotExpression(exp.copyForPlanKey());
@@ -5,6 +5,8 @@ import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
import java.io.IOException;
/**
* Slightly redundant as Query.setId() ultimately also does the same job.
@@ -20,6 +22,11 @@ class NullExpression extends AbstractExpression {
this.notNull = notNull;
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
context.writeExists(notNull, propName);
}
@Override
public void addBindValues(SpiExpressionRequest request) {
@@ -8,54 +8,56 @@ public enum Op {
/**
* Exists (JSON).
*/
EXISTS(" is not null "),
EXISTS(" is not null ", ""),
/**
* Not Exists (JSON).
*/
NOT_EXISTS(" is null "),
NOT_EXISTS(" is null ", ""),
/**
* Between (JSON).
*/
BETWEEN(" between ? and ? "),
BETWEEN(" between ? and ? ", ""),
/**
* Equal to
*/
EQ(" = ? "),
EQ(" = ? ", ""),
/**
* Not equal to.
*/
NOT_EQ(" <> ? "),
NOT_EQ(" <> ? ", ""),
/**
*
* Less than.
*/
LT(" < ? "),
LT(" < ? ", "lt"),
/**
* Less than or equal to.
*/
LT_EQ(" <= ? "),
LT_EQ(" <= ? ", "lte"),
/**
* Greater than.
*/
GT(" > ? "),
GT(" > ? ", "gt"),
/**
* Greater than or equal to.
*/
GT_EQ(" >= ? ");
GT_EQ(" >= ? ", "gte");
final String exp;
Op(String exp) {
final String docExp;
Op(String exp, String docExp) {
this.exp = exp;
this.docExp = docExp;
}
/**
@@ -64,4 +66,11 @@ public enum Op {
public String bind() {
return exp;
}
/**
* Return the doc store expression.
*/
public String docExp() {
return docExp;
}
}
@@ -7,6 +7,8 @@ import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.api.SpiExpressionValidation;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import java.io.IOException;
class RawExpression extends NonPrepareExpression {
private static final long serialVersionUID = 7973903141340334606L;
@@ -20,6 +22,11 @@ class RawExpression extends NonPrepareExpression {
this.values = values;
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
context.writeRaw(sql);
}
@Override
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
@@ -6,6 +6,8 @@ import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
import java.io.IOException;
public class SimpleExpression extends AbstractExpression {
private static final long serialVersionUID = -382881395755603790L;
@@ -20,6 +22,16 @@ public class SimpleExpression extends AbstractExpression {
this.value = value;
}
@Override
public void writeElastic(ElasticExpressionContext context) throws IOException {
if (type == Op.BETWEEN) {
throw new IllegalStateException("BETWEEN Not expected in SimpleExpression?");
}
context.writeSimple(type, propName, value);
}
public final String getPropName() {
return propName;
}
@@ -0,0 +1,7 @@
package com.avaje.ebeaninternal.server.expression;
/**
* Marked interface for expressions unsupported in doc store.
*/
public interface UnsupportedDocStoreExpression {
}