Merge pull request #2926 from FOCONIS/prepare-query

FIX: prepare FindMap query for proper bean cache / DB access
This commit is contained in:
Rob Bygrave authored and GitHub committed 2023-01-09 10:28:09 +13:00
commit 28bd018d1f
2 files changed
+35 -4

No files matched your search

@@ -1107,12 +1107,13 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
@Override @Override
@SuppressWarnings({"unchecked", "rawtypes"}) @SuppressWarnings({"unchecked", "rawtypes"})
public <K, T> Map<K, T> findMap(Query<T> query, @Nullable Transaction transaction) { public <K, T> Map<K, T> findMap(Query<T> query, @Nullable Transaction transaction) {
SpiOrmQueryRequest request = createQueryRequest(Type.MAP, query, transaction); SpiOrmQueryRequest request = buildQueryRequest(Type.MAP, query, transaction);
request.resetBeanCacheAutoMode(false); request.resetBeanCacheAutoMode(false);
if ((transaction == null || !transaction.isSkipCache()) && request.getFromBeanCache()) { if ((transaction == null || !transaction.isSkipCache()) && request.getFromBeanCache()) {
// hit bean cache and got all results from cache // hit bean cache and got all results from cache
return request.beanCacheHitsAsMap(); return request.beanCacheHitsAsMap();
} }
request.prepareQuery();
Object result = request.getFromQueryCache(); Object result = request.getFromQueryCache();
if (result != null) { if (result != null) {
return (Map<K, T>) result; return (Map<K, T>) result;
+33 -3
View File
@@ -1,11 +1,12 @@
package org.tests.cache; package org.tests.cache;
import io.ebean.xtest.BaseTestCase;
import io.ebean.DB; import io.ebean.DB;
import io.ebean.Query;
import io.ebean.Transaction; import io.ebean.Transaction;
import io.ebean.cache.ServerCache; import io.ebean.cache.ServerCache;
import io.ebean.cache.ServerCacheStatistics; import io.ebean.cache.ServerCacheStatistics;
import io.ebean.test.LoggedSql; import io.ebean.test.LoggedSql;
import io.ebean.xtest.BaseTestCase;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -54,10 +55,39 @@ public class TestBeanCache extends BaseTestCase {
assertThat(sql).isEmpty(); assertThat(sql).isEmpty();
} }
@Test
public void idsInFindMap() {
List<OCachedBean> beans = createBeans(Arrays.asList("m0", "m1", "m2", "m3", "m4", "m5", "m6"));
List<Long> ids = beans.stream().map(OCachedBean::getId).collect(Collectors.toList());
beanCache.clear();
beanCache.statistics(true);
Query<OCachedBean> query = DB.find(OCachedBean.class).setUseCache(true);
// Test findIds
LoggedSql.start();
query.copy()
.where().idIn(ids.subList(0, 1))
.findMap(); // cache key is: 3/d[{/c1000}]/w[List[IdIn[?1],]]
assertThat(LoggedSql.stop().get(0)).contains("in (?)");
LoggedSql.start();
query.copy()
.where().idIn(ids.subList(0, 4))
.findMap(); // cache key is: 3/d[{/c1000}]/w[List[IdIn[?5],]]
assertThat(LoggedSql.stop().get(0)).contains("in (?,?,?,?,?)");
LoggedSql.start();
query.copy()
.where().idIn(ids.subList(2, 6))
.findMap(); // same cache key as above and same SQL above
assertThat(LoggedSql.stop().get(0)).contains("in (?,?,?,?,?)");
}
@Test @Test
public void idsIn_explicitCache_expect_cachePut() { public void idsIn_explicitCache_expect_cachePut() {
List<OCachedBean> beans = createBeans(Arrays.asList("k0","k1")); List<OCachedBean> beans = createBeans(Arrays.asList("k0", "k1"));
List<Long> ids = beans.stream().map(OCachedBean::getId).collect(Collectors.toList()); List<Long> ids = beans.stream().map(OCachedBean::getId).collect(Collectors.toList());
beanCache.clear(); beanCache.clear();
@@ -183,7 +213,7 @@ public class TestBeanCache extends BaseTestCase {
Country country = DB.find(Country.class) Country country = DB.find(Country.class)
.where() .where()
.eq("name","NotValid") .eq("name", "NotValid")
.findOne(); .findOne();
assertThat(country).isNull(); assertThat(country).isNull();