#745 - ElasticSearch - internally translate where().id.eq(42) ... to where().setId(42)

This commit is contained in:
Robin Bygrave committed 2016-06-20 22:29:38 +12:00
1 parent d12e55c20b
commit c793d6652f
18 files changed
+153 -31

No files matched your search

@@ -94,4 +94,8 @@ public interface SpiExpression extends Expression {
*/
SpiExpression copyForPlanKey();
/**
* Return the bind Id value if this is a "equal to" expression for the id property.
*/
Object getIdEqualTo(String idName);
}
@@ -133,6 +133,11 @@ public interface SpiQuery<T> extends Query<T> {
}
}
/**
* Check for a single "equal to" expression for the Id.
*/
void checkIdEqualTo();
/**
* Return true if AutoTune should be attempted on this query.
*/
@@ -1043,21 +1043,20 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
public <T> T findUnique(Query<T> query, Transaction transaction) {
Object id = query.getId();
SpiQuery<T> spiQuery = (SpiQuery<T>) query;
spiQuery.checkIdEqualTo();
Object id = spiQuery.getId();
if (id != null) {
// actually a find by Id query
return findId(query, transaction);
}
SpiQuery<T> spiQuery = (SpiQuery<T>) query;
BeanDescriptor<T> desc = spiQuery.getBeanDescriptor();
SpiTransaction t = (SpiTransaction) transaction;
if (t == null) {
t = getCurrentServerTransaction();
}
if (t == null || !t.isSkipCache()) {
id = desc.cacheNaturalKeyIdLookup(spiQuery);
id = spiQuery.getBeanDescriptor().cacheNaturalKeyIdLookup(spiQuery);
if (id != null) {
T bean = findIdCheckPersistenceContextAndCache(t, spiQuery, id);
if (bean != null) {
@@ -1768,6 +1768,13 @@ public class BeanDescriptor<T> implements MetaBeanInfo, BeanType<T> {
pc.deleted(rootBeanType, idValue);
}
/**
* Return the Id property name or null if no Id property exists.
*/
public String getIdName() {
return (idProperty == null) ? null : idProperty.getName();
}
/**
* Helper method to return the unique property. If only one property makes up
* the unique id then it's value is returned. If there is a concatenated
@@ -21,6 +21,12 @@ public abstract class AbstractExpression implements SpiExpression {
this.propName = propName;
}
@Override
public Object getIdEqualTo(String idName) {
// override on SimpleExpression
return null;
}
@Override
public SpiExpression copyForPlanKey() {
return this;
@@ -16,6 +16,12 @@ public abstract class AbstractTextExpression extends AbstractExpression {
super(propName);
}
@Override
public Object getIdEqualTo(String idName) {
// always null for this expression
return null;
}
@Override
public void addSql(SpiExpressionRequest request) {
// do nothing, only execute against document store
@@ -1,8 +1,5 @@
package com.avaje.ebeaninternal.server.expression;
import java.io.IOException;
import java.util.ArrayList;
import com.avaje.ebean.ExampleExpression;
import com.avaje.ebean.LikeType;
import com.avaje.ebean.bean.EntityBean;
@@ -12,33 +9,34 @@ import com.avaje.ebeaninternal.api.ManyWhereJoins;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.api.SpiExpressionValidation;
import com.avaje.ebeaninternal.server.core.OrmQueryRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import com.avaje.ebeaninternal.server.query.SplitName;
import java.io.IOException;
import java.util.ArrayList;
/**
* A "Query By Example" type of expression.
* <p>
* Pass in an example entity and for each non-null scalar properties an
* expression is added.
* </p>
*
* <pre class="code">
* <p>
* <pre>{@code
* // create an example bean and set the properties
* // with the query parameters you want
* Customer example = new Customer();
* example.setName(&quot;Rob%&quot;);
* example.setNotes(&quot;%something%&quot;);
*
* List&lt;Customer&gt; list = Ebean.find(Customer.class).where()
* // pass the bean into the where() clause
* .exampleLike(example)
* // you can add other expressions to the same query
* .gt(&quot;id&quot;, 2).findList();
*
* </pre>
* example.setName("Rob%");
* example.setNotes("%something%");
*
* List<Customer> list = Ebean.find(Customer.class)
* .where()
* .exampleLike(example)
* .findList();
*
* }</pre>
*/
public class DefaultExampleExpression implements SpiExpression, ExampleExpression {
@@ -71,13 +69,10 @@ public class DefaultExampleExpression implements SpiExpression, ExampleExpressio
/**
* Construct the query by example expression.
*
* @param entity
* the example entity with non null property values
* @param caseInsensitive
* if true use case insensitive expressions
* @param likeType
* the type of Like wild card used
*
* @param entity the example entity with non null property values
* @param caseInsensitive if true use case insensitive expressions
* @param likeType the type of Like wild card used
*/
public DefaultExampleExpression(EntityBean entity, boolean caseInsensitive, LikeType likeType) {
this.entity = entity;
@@ -104,6 +99,12 @@ public class DefaultExampleExpression implements SpiExpression, ExampleExpressio
}
}
@Override
public Object getIdEqualTo(String idName) {
// always return null for this expression
return null;
}
@Override
public SpiExpression copyForPlanKey() {
return new DefaultExampleExpression(list);
@@ -295,9 +296,9 @@ public class DefaultExampleExpression implements SpiExpression, ExampleExpressio
}
} else if ((beanProperty instanceof BeanPropertyAssocOne) && (value instanceof EntityBean)) {
BeanPropertyAssocOne assocOne = (BeanPropertyAssocOne)beanProperty;
BeanPropertyAssocOne assocOne = (BeanPropertyAssocOne) beanProperty;
BeanDescriptor targetDescriptor = assocOne.getTargetDescriptor();
addExpressions(list, targetDescriptor, (EntityBean)value, propName);
addExpressions(list, targetDescriptor, (EntityBean) value, propName);
}
}
}
@@ -204,6 +204,12 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
return copy;
}
@Override
public Object getIdEqualTo(String idName) {
// always return null for this expression
return null;
}
/**
* Return true if one of the expressions is related to a Many property.
*/
@@ -1027,4 +1033,11 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
public void prepareDocNested(BeanDescriptor<T> beanDescriptor) {
PrepareDocNested.prepare(this, beanDescriptor);
}
public Object idEqualTo(String idName) {
if (list.size() == 1) {
return list.get(0).getIdEqualTo(idName);
}
return null;
}
}
@@ -41,6 +41,12 @@ class ExistsQueryExpression implements SpiExpression, UnsupportedDocStoreExpress
throw new IllegalStateException("Not supported");
}
@Override
public Object getIdEqualTo(String idName) {
// always return null for this expression
return null;
}
@Override
public void prepareExpression(BeanQueryRequest<?> request) {
@@ -80,6 +80,12 @@ class JunctionExpression<T> implements SpiJunction<T>, SpiExpression, Expression
context.endBoolGroupList();
}
@Override
public Object getIdEqualTo(String idName) {
// always null for this expression
return null;
}
@Override
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
@@ -88,6 +88,12 @@ abstract class LogicExpression implements SpiExpression {
return null;
}
@Override
public Object getIdEqualTo(String idName) {
// always return null for this expression
return null;
}
@Override
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
expOne.containsMany(desc, manyWhereJoin);
@@ -31,6 +31,12 @@ class NestedPathWrapperExpression implements SpiExpression {
context.endNested();
}
@Override
public Object getIdEqualTo(String idName) {
// always return null for this expression
return null;
}
@Override
public String nestedPath(BeanDescriptor<?> desc) {
return nestedPath;
@@ -13,6 +13,12 @@ abstract class NonPrepareExpression implements SpiExpression {
// do nothing
}
@Override
public Object getIdEqualTo(String idName) {
// always null in this expression
return null;
}
@Override
public SpiExpression copyForPlanKey() {
return this;
@@ -26,6 +26,12 @@ class NoopExpression implements SpiExpression {
public void writeDocQuery(DocQueryContext context) throws IOException {
}
@Override
public Object getIdEqualTo(String idName) {
// always return null for this expression
return null;
}
@Override
public String nestedPath(BeanDescriptor<?> desc) {
return null;
@@ -29,6 +29,12 @@ final class NotExpression implements SpiExpression {
context.endBool();
}
@Override
public Object getIdEqualTo(String idName) {
// always return null for this expression
return null;
}
@Override
public SpiExpression copyForPlanKey() {
return new NotExpression(exp.copyForPlanKey());
@@ -21,6 +21,14 @@ public class SimpleExpression extends AbstractExpression {
this.value = value;
}
@Override
public Object getIdEqualTo(String idName) {
if (type == Op.EQ && idName.equals(propName)) {
return value;
}
return null;
}
@Override
public void writeDocQuery(DocQueryContext context) throws IOException {
if (type == Op.BETWEEN) {
@@ -236,6 +236,16 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
return beanDescriptor;
}
@Override
public void checkIdEqualTo() {
if (id == null && whereExpressions != null) {
id = whereExpressions.idEqualTo(beanDescriptor.getIdName());
if (id != null) {
whereExpressions = null;
}
}
}
@Override
public boolean isAutoTunable() {
return beanDescriptor.isAutoTunable();