add support for findSingleAttributeSet

This commit is contained in:
Roland Praml
2022-05-13 14:45:08 +02:00
parent 61f529d15a
commit ca20c9ef1f
19 changed files with 148 additions and 48 deletions
+46 -19
View File
@@ -1,21 +1,23 @@
package org.tests.cache;
import io.ebean.xtest.BaseTestCase;
import io.ebean.CacheMode;
import io.ebean.DB;
import io.ebean.ExpressionList;
import io.ebean.bean.BeanCollection;
import io.ebean.cache.ServerCache;
import io.ebean.test.LoggedSql;
import io.ebean.xtest.BaseTestCase;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.Customer;
import org.tests.model.basic.ResetBasicData;
import org.tests.model.cache.EColAB;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.*;
public class TestQueryCache extends BaseTestCase {
@@ -50,7 +52,7 @@ public class TestQueryCache extends BaseTestCase {
}
@Test
public void findSingleAttribute() {
public void findSingleAttributeList() {
DB.find(EColAB.class).delete();
new EColAB("03", "SingleAttribute").save();
@@ -76,6 +78,7 @@ public class TestQueryCache extends BaseTestCase {
assertThat(colA_Second).isSameAs(colA_first);
List<String> colA_NotDistinct = DB
.find(EColAB.class)
.setUseQueryCache(true)
@@ -89,15 +92,39 @@ public class TestQueryCache extends BaseTestCase {
// ensure that findCount & findSingleAttribute use different
// slots in cache. If not a "Cannot cast List to int" should happen.
int count = DB
.find(EColAB.class)
.setUseQueryCache(true)
.select("columnA")
.where()
.eq("columnB", "SingleAttribute")
.findCount();
.find(EColAB.class)
.setUseQueryCache(true)
.select("columnA")
.where()
.eq("columnB", "SingleAttribute")
.findCount();
assertThat(count).isEqualTo(2);
}
@Test
public void findSingleAttributeSet() {
DB.find(EColAB.class).delete();
new EColAB("03", "SingleAttribute").save();
new EColAB("03", "SingleAttribute").save();
ExpressionList<EColAB> query = DB
.find(EColAB.class)
.setUseQueryCache(true)
.select("columnA")
.where()
.eq("columnB", "SingleAttribute");
Set<String> colA_first = query.findSingleAttributeSet();
Set<String> colA_Second = query.findSingleAttributeSet();
assertThat(colA_Second).isSameAs(colA_first).hasSize(1);
assertThatThrownBy(colA_first::clear).isInstanceOf(UnsupportedOperationException.class);
// ensure, that we do not have cache collisions on same query
query.findSingleAttributeList();
query.findSingleAttribute();
query.findCount();
}
@Test
public void findCount() {
@@ -279,7 +306,7 @@ public class TestQueryCache extends BaseTestCase {
assertSame(list, list2B);
List<Customer> list3 = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(false).where()
.ilike("name", "Rob").findList();
.ilike("name", "Rob").findList();
assertNotSame(list, list3);
BeanCollection<Customer> bc3 = (BeanCollection<Customer>) list3;
@@ -323,10 +350,10 @@ public class TestQueryCache extends BaseTestCase {
// and now, ensure that we hit the database
LoggedSql.start();
colA_second = DB.find(EColAB.class)
.setUseQueryCache(CacheMode.PUT)
.where()
.eq("columnB", "someId")
.findIds();
.setUseQueryCache(CacheMode.PUT)
.where()
.eq("columnB", "someId")
.findIds();
sql = LoggedSql.stop();
assertThat(sql).hasSize(1);
@@ -335,15 +362,15 @@ public class TestQueryCache extends BaseTestCase {
@Test
public void findCountDifferentQueriesBit() {
DB.getDefault().pluginApi().cacheManager().clearAll();
differentFindCount(q->q.bitwiseAny("id",1), q->q.bitwiseAny("id",0));
differentFindCount(q->q.bitwiseAll("id",1), q->q.bitwiseAll("id",0));
differentFindCount(q -> q.bitwiseAny("id", 1), q -> q.bitwiseAny("id", 0));
differentFindCount(q -> q.bitwiseAll("id", 1), q -> q.bitwiseAll("id", 0));
// differentFindCount(q->q.bitwiseNot("id",1), q->q.bitwiseNot("id",0)); NOT 1 == AND 1 = 0
differentFindCount(q->q.bitwiseAnd("id",1, 0), q->q.bitwiseAnd("id",1, 1));
differentFindCount(q -> q.bitwiseAnd("id", 1, 0), q -> q.bitwiseAnd("id", 1, 1));
differentFindCount(q->q.bitwiseAnd("id",2, 0), q->q.bitwiseAnd("id",4, 0));
differentFindCount(q->q.bitwiseAnd("id",2, 1), q->q.bitwiseAnd("id",4, 1));
differentFindCount(q -> q.bitwiseAnd("id", 2, 0), q -> q.bitwiseAnd("id", 4, 0));
differentFindCount(q -> q.bitwiseAnd("id", 2, 1), q -> q.bitwiseAnd("id", 4, 1));
// Will produce hash collision
differentFindCount(q->q.bitwiseAnd("id",10, 0), q->q.bitwiseAnd("id",0, 928210));
differentFindCount(q -> q.bitwiseAnd("id", 10, 0), q -> q.bitwiseAnd("id", 0, 928210));
}