mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Refactor internals of batch lazy loading and query fetches
This commit is contained in:
@@ -1,80 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.loadcontext;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
|
||||
public class TestDLoadWeakList extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
String s0 = new String("zero");
|
||||
String s1 = new String("one");
|
||||
String s2 = new String("two");
|
||||
String s3 = new String("three");
|
||||
String s4 = new String("four");
|
||||
String s5 = new String("five");
|
||||
String s6 = new String("six");
|
||||
String s7 = new String("seven");
|
||||
String s8 = new String("eight");
|
||||
String s9 = new String("nine");
|
||||
String s10 = new String("ten");
|
||||
|
||||
DLoadWeakList<Object> list = new DLoadWeakList<Object>();
|
||||
|
||||
list.add(s0);
|
||||
list.add(s1);
|
||||
list.add(s2);
|
||||
list.add(s3);
|
||||
list.add(s4);
|
||||
list.add(s5);
|
||||
list.add(s6);
|
||||
list.add(s7);
|
||||
list.add(s8);
|
||||
list.add(s9);
|
||||
list.add(s10);
|
||||
|
||||
Assert.assertEquals(11, list.list.size());
|
||||
|
||||
System.gc();
|
||||
|
||||
try {
|
||||
Thread.sleep(300);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// this is really only a HINT, so no guarantee
|
||||
// .. but the SUN JVM does do the business
|
||||
System.gc();
|
||||
|
||||
Assert.assertEquals(11, list.list.size());
|
||||
|
||||
List<Object> b0 = list.getLoadBatch(0, 2);
|
||||
Assert.assertEquals(2, b0.size());
|
||||
Assert.assertEquals("zero", b0.get(0));
|
||||
Assert.assertEquals("one", b0.get(1));
|
||||
|
||||
try {
|
||||
b0 = list.getLoadBatch(0, 2);
|
||||
Assert.assertTrue(false);
|
||||
} catch (IllegalStateException e) {
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
b0 = list.getNextBatch(2);
|
||||
Assert.assertEquals(2, b0.size());
|
||||
Assert.assertEquals("two", b0.get(0));
|
||||
Assert.assertEquals("three", b0.get(1));
|
||||
|
||||
list.removeEntry(1);
|
||||
|
||||
b0 = list.getLoadBatch(7, 2);
|
||||
Assert.assertEquals(2, b0.size());
|
||||
Assert.assertEquals("seven", b0.get(0));
|
||||
Assert.assertEquals("eight", b0.get(1));
|
||||
|
||||
}
|
||||
}
|
||||
-108
@@ -1,108 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.loadcontext;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
|
||||
public class TestDLoadWeakListWithGC extends BaseTestCase {
|
||||
|
||||
private void doStuffInMethodScope(DLoadWeakList<Object> list) {
|
||||
String s5 = new String("five");
|
||||
String s6 = new String("six");
|
||||
String s7 = new String("seven");
|
||||
String s8 = new String("eight");
|
||||
String s9 = new String("nine");
|
||||
String s10 = new String("ten");
|
||||
|
||||
list.add(s5);
|
||||
list.add(s6);
|
||||
list.add(s7);
|
||||
list.add(s8);
|
||||
list.add(s9);
|
||||
list.add(s10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
String s0 = new String("zero");
|
||||
String s1 = new String("one");
|
||||
String s2 = new String("two");
|
||||
String s3 = new String("three");
|
||||
String s4 = new String("four");
|
||||
|
||||
DLoadWeakList<Object> list = new DLoadWeakList<Object>();
|
||||
|
||||
list.add(s0);
|
||||
list.add(s1);
|
||||
list.add(s2);
|
||||
list.add(s3);
|
||||
list.add(s4);
|
||||
|
||||
int initialSize = list.list.size();
|
||||
|
||||
doStuffInMethodScope(list);
|
||||
Assert.assertEquals(11, list.list.size());
|
||||
|
||||
System.gc();
|
||||
|
||||
try {
|
||||
Thread.sleep(300);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// this is really only a HINT, so no guarantee
|
||||
// .. but the SUN JVM does do the business
|
||||
System.gc();
|
||||
|
||||
Assert.assertEquals(11, list.list.size());
|
||||
|
||||
// these weak refs are still good as we still
|
||||
// have a hard reference to the objects in scope
|
||||
for (int i = 0; i < initialSize; i++) {
|
||||
WeakReference<Object> weakReference = list.list.get(i);
|
||||
Assert.assertNotNull(weakReference);
|
||||
Assert.assertNotNull(weakReference.get());
|
||||
}
|
||||
|
||||
// these weak refs are all null as we don't have
|
||||
// hard refs to the objects in scope ... and it just
|
||||
// so happens the System.gc() in sun jvm is aggressive here.
|
||||
for (int i = initialSize; i < list.list.size(); i++) {
|
||||
WeakReference<Object> weakReference = list.list.get(i);
|
||||
Assert.assertNotNull(weakReference);
|
||||
Assert.assertNull(weakReference.get());
|
||||
}
|
||||
|
||||
List<Object> b0 = list.getLoadBatch(0, 2);
|
||||
Assert.assertEquals(2, b0.size());
|
||||
Assert.assertEquals("zero", b0.get(0));
|
||||
Assert.assertEquals("one", b0.get(1));
|
||||
|
||||
try {
|
||||
b0 = list.getLoadBatch(0, 2);
|
||||
Assert.assertTrue(false);
|
||||
} catch (IllegalStateException e) {
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
b0 = list.getNextBatch(2);
|
||||
Assert.assertEquals(2, b0.size());
|
||||
Assert.assertEquals("two", b0.get(0));
|
||||
Assert.assertEquals("three", b0.get(1));
|
||||
|
||||
list.removeEntry(1);
|
||||
|
||||
try {
|
||||
// past the gc blown area
|
||||
b0 = list.getLoadBatch(7, 2);
|
||||
Assert.assertTrue(false);
|
||||
} catch (IllegalStateException e) {
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -19,19 +19,27 @@ public class TestQueryFetchJoinWithOrder extends BaseTestCase {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Order> list = Ebean.find(Order.class).fetch("details", new FetchConfig().query()).order()
|
||||
.asc("id").order().desc("details.id").findList();
|
||||
List<Order> list = Ebean.find(Order.class)
|
||||
.fetch("details", new FetchConfig().query())
|
||||
.order().asc("id")
|
||||
.order().desc("details.id").findList();
|
||||
|
||||
Assert.assertNotNull(list);
|
||||
|
||||
List<Order> list2 = Ebean.find(Order.class).fetch("customer", new FetchConfig().query())
|
||||
.fetch("customer.contacts").order().asc("id").order().asc("customer.contacts.lastName")
|
||||
List<Order> list2 = Ebean.find(Order.class)
|
||||
.fetch("customer", new FetchConfig().query(5))
|
||||
.fetch("customer.contacts")
|
||||
.order().asc("id")
|
||||
.order().asc("customer.contacts.lastName")
|
||||
.findList();
|
||||
|
||||
Assert.assertNotNull(list2);
|
||||
|
||||
List<Customer> list3 = Ebean.find(Customer.class).fetch("orders").filterMany("orders")
|
||||
.eq("status", Order.Status.NEW).order().desc("orders.id").findList();
|
||||
List<Customer> list3 = Ebean.find(Customer.class)
|
||||
.fetch("orders")
|
||||
.filterMany("orders").eq("status", Order.Status.NEW)
|
||||
.order().desc("orders.id")
|
||||
.findList();
|
||||
|
||||
Assert.assertNotNull(list3);
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.FetchConfig;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryJoinBatchSize extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Order> list = Ebean.find(Order.class)
|
||||
.fetch("customer", new FetchConfig().queryFirst(3).lazy(2))
|
||||
//.fetch("orders.details", new FetchConfig().query())
|
||||
//.fetch("orders.shipments", new FetchConfig().query())
|
||||
.findList();
|
||||
|
||||
for (Order order : list) {
|
||||
Customer customer = order.getCustomer();
|
||||
customer.getName();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void test() {
|
||||
//
|
||||
// ResetBasicData.reset();
|
||||
//
|
||||
// Ebean.find(Customer.class)
|
||||
// .fetch("orders", new FetchConfig().query(3).lazy(2))
|
||||
// //.fetch("orders.details", new FetchConfig().query())
|
||||
// //.fetch("orders.shipments", new FetchConfig().query())
|
||||
// .findList();
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -30,7 +30,8 @@ public class TestQueryJoinQueryNonRoot extends BaseTestCase {
|
||||
|
||||
List<Order> list = Ebean.find(Order.class).fetch("customer")
|
||||
.fetch("customer.contacts", "firstName", new FetchConfig().query().lazy(10))
|
||||
.fetch("customer.contacts.group").where().lt("id", 3).findList();
|
||||
.fetch("customer.contacts.group")
|
||||
.where().lt("id", 3).findList();
|
||||
|
||||
Assert.assertNotNull(list);
|
||||
Assert.assertTrue(list.size() > 0);
|
||||
|
||||
@@ -36,8 +36,11 @@ public class TestRawSqlOrmWrapper3 extends BaseTestCase {
|
||||
|
||||
List<OrderAggregate> list2 = Ebean.find(OrderAggregate.class).setRawSql(rawSql)
|
||||
.fetch("order", new FetchConfig().query())
|
||||
.fetch("order.details", new FetchConfig().query()).where().gt("order.id", 2).having()
|
||||
.gt("totalAmount", 10).filterMany("order.details").gt("unitPrice", 2d).findList();
|
||||
.fetch("order.details", new FetchConfig().query())
|
||||
.where().gt("order.id", 2)
|
||||
.having().gt("totalAmount", 10)
|
||||
.filterMany("order.details").gt("unitPrice", 2d)
|
||||
.findList();
|
||||
|
||||
output(list2);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user