mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#2200 - ENH: Add DtoQuery findEach with batch consumer - findEach(int batchSize, Consumer<List<T>> consumer)
This commit is contained in:
@@ -10,11 +10,13 @@ import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.EBasicLog;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -22,6 +24,9 @@ public class DtoQueryTest extends BaseTestCase {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(DtoQueryTest.class);
|
||||
|
||||
private final AtomicInteger batchCount = new AtomicInteger();
|
||||
private final AtomicInteger rowCount = new AtomicInteger();
|
||||
|
||||
@Test
|
||||
public void dto_findList_constructorMatch() {
|
||||
|
||||
@@ -78,6 +83,63 @@ public class DtoQueryTest extends BaseTestCase {
|
||||
assertSql(sql.get(0)).contains("select id, name from o_customer where id > ?");
|
||||
}
|
||||
|
||||
private void resetFindEachCounts() {
|
||||
batchCount.set(0);
|
||||
rowCount.set(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dto_findEachBatch() {
|
||||
seedData(); // 15 rows inserted to fetch
|
||||
|
||||
resetFindEachCounts();
|
||||
findEachWithBatch(5);
|
||||
assertThat(batchCount.get()).isEqualTo(3);
|
||||
assertThat(rowCount.get()).isEqualTo(15);
|
||||
|
||||
resetFindEachCounts();
|
||||
findEachWithBatch(10);
|
||||
assertThat(batchCount.get()).isEqualTo(2);
|
||||
assertThat(rowCount.get()).isEqualTo(15);
|
||||
|
||||
resetFindEachCounts();
|
||||
findEachWithBatch(14);
|
||||
assertThat(batchCount.get()).isEqualTo(2);
|
||||
assertThat(rowCount.get()).isEqualTo(15);
|
||||
|
||||
resetFindEachCounts();
|
||||
findEachWithBatch(15);
|
||||
assertThat(batchCount.get()).isEqualTo(1);
|
||||
assertThat(rowCount.get()).isEqualTo(15);
|
||||
|
||||
resetFindEachCounts();
|
||||
findEachWithBatch(16);
|
||||
assertThat(batchCount.get()).isEqualTo(1);
|
||||
assertThat(rowCount.get()).isEqualTo(15);
|
||||
|
||||
resetFindEachCounts();
|
||||
findEachWithBatch(20);
|
||||
assertThat(batchCount.get()).isEqualTo(1);
|
||||
assertThat(rowCount.get()).isEqualTo(15);
|
||||
}
|
||||
|
||||
private void findEachWithBatch(int batchSize) {
|
||||
server().findDto(DCust.class, "select id, name from e_basic_log where name like ?")
|
||||
.setParameter("dtoFindEachBatch%")
|
||||
.findEach(batchSize, batch -> {
|
||||
int batchId = batchCount.incrementAndGet();
|
||||
int rows = rowCount.addAndGet(batch.size());
|
||||
log.info("batch {} rows {}", batchId, rows);
|
||||
});
|
||||
}
|
||||
|
||||
private void seedData() {
|
||||
for (int i = 0; i < 15; i++) {
|
||||
EBasicLog log = new EBasicLog("dtoFindEachBatch "+i);
|
||||
DB.save(log);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dto_findOneEmpty() {
|
||||
|
||||
|
||||
@@ -445,6 +445,10 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
public <T> void findDtoEach(SpiDtoQuery<T> query, Consumer<T> consumer) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void findDtoEach(SpiDtoQuery<T> query, int batch, Consumer<List<T>> consumer) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void findDtoEachWhile(SpiDtoQuery<T> query, Predicate<T> consumer) {
|
||||
}
|
||||
|
||||
@@ -13,6 +13,10 @@ public class EBasicLog {
|
||||
|
||||
String name;
|
||||
|
||||
public EBasicLog(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ public class EBasicWithLog {
|
||||
}
|
||||
|
||||
private void writeLog(String title) {
|
||||
EBasicLog log = new EBasicLog();
|
||||
EBasicLog log = new EBasicLog(name);
|
||||
log.setName(title);
|
||||
DB.save(log);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user