FIX: Cache exist query results

This commit is contained in:
Roland Praml
2021-08-27 13:56:51 +02:00
committed by Jonas Pöhler
parent 56fed3ad87
commit 83f8435000
4 changed files with 50 additions and 1 deletions
@@ -81,6 +81,11 @@ public interface SpiQuery<T> extends Query<T>, SpiQueryFetch, TxnProfileEventCod
*/
ID_LIST(FIND_ID_LIST, "findIds"),
/**
* Find exists.
*/
EXISTS(FIND_EXISTS, "exists"),
/**
* Find single attribute.
*/
@@ -26,6 +26,7 @@ public interface TxnProfileEventCodes {
String FIND_MANY = "fm";
String FIND_ITERATE = "fe";
String FIND_ID_LIST = "fi";
String FIND_EXISTS = "ex";
String FIND_ATTRIBUTE = "fa";
String FIND_COUNT = "fc";
String FIND_SUBQUERY = "fs";
@@ -1291,7 +1291,12 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
@Override
public <T> boolean exists(Query<T> ormQuery, Transaction transaction) {
Query<T> ormQueryCopy = ormQuery.copy().setMaxRows(1);
SpiOrmQueryRequest<?> request = createQueryRequest(Type.ID_LIST, ormQueryCopy, transaction);
SpiOrmQueryRequest<?> request = createQueryRequest(Type.EXISTS, ormQueryCopy, transaction);
List<Object> ids = request.getFromQueryCache();
if (ids != null) {
return !ids.isEmpty();
}
try {
request.initTransIfRequired();
return !request.findIds().isEmpty();
@@ -3,6 +3,7 @@ package org.tests.cache;
import io.ebean.BaseTestCase;
import io.ebean.CacheMode;
import io.ebean.DB;
import io.ebean.Ebean;
import io.ebean.ExpressionList;
import io.ebean.bean.BeanCollection;
import io.ebean.cache.ServerCache;
@@ -135,6 +136,43 @@ public class TestQueryCache extends BaseTestCase {
assertThat(sql).hasSize(1);
}
@Test
public void exists() {
new EColAB("06", "exists").save();
new EColAB("07", "exists").save();
LoggedSqlCollector.start();
boolean exists0 = Ebean.find(EColAB.class)
.setUseQueryCache(CacheMode.ON)
.where()
.eq("columnB", "exists")
.exists();
boolean exists1 = Ebean.find(EColAB.class)
.setUseQueryCache(CacheMode.ON)
.where()
.eq("columnB", "exists")
.exists();
List<String> sql = LoggedSqlCollector.stop();
assertThat(exists0).isEqualTo(exists1);
assertThat(sql).hasSize(1);
// and now, ensure that we hit the database
LoggedSqlCollector.start();
boolean exists2 = Ebean.find(EColAB.class)
.setUseQueryCache(CacheMode.OFF)
.where()
.eq("columnB", "exists")
.exists();
assertThat(exists2).isEqualTo(exists1);
sql = LoggedSqlCollector.stop();
assertThat(sql).hasSize(1);
}
@Test
public void findCountDifferentQueries() {