use of org.molgenis.data.Fetch in project molgenis by molgenis.
the class InMemoryRepositoryTest method streamFetch.
@Test
public void streamFetch() throws IOException {
String idAttrName = "id";
EntityType entityType = mock(EntityType.class);
Attribute idAttr = when(mock(Attribute.class).getName()).thenReturn(idAttrName).getMock();
when(entityType.getIdAttribute()).thenReturn(idAttr);
try (InMemoryRepository inMemoryRepository = new InMemoryRepository(entityType)) {
Object id0 = 0;
Entity entity0 = when(mock(Entity.class).get(idAttrName)).thenReturn(id0).getMock();
Object id1 = 1;
Entity entity1 = when(mock(Entity.class).get(idAttrName)).thenReturn(id1).getMock();
inMemoryRepository.add(entity0);
inMemoryRepository.add(entity1);
Fetch fetch = new Fetch();
@SuppressWarnings("unchecked") Consumer<List<Entity>> consumer = mock(Consumer.class);
inMemoryRepository.forEachBatched(fetch, consumer, 1000);
verify(consumer).accept(Arrays.asList(entity0, entity1));
}
}
use of org.molgenis.data.Fetch in project molgenis by molgenis.
the class PostgreSqlRepository method forEachBatched.
@Override
public void forEachBatched(Fetch fetch, Consumer<List<Entity>> consumer, int batchSize) {
final Stopwatch stopwatch = createStarted();
final JdbcTemplate template = new JdbcTemplate(dataSource);
template.setFetchSize(batchSize);
final Query<Entity> query = new QueryImpl<>();
if (fetch != null) {
query.fetch(fetch);
}
final String allRowsSelect = getSqlSelect(entityType, query, emptyList(), false);
LOG.debug("Fetching [{}] data...", getName());
LOG.trace("SQL: {}", allRowsSelect);
RowMapper<Entity> rowMapper = postgreSqlEntityFactory.createRowMapper(entityType, fetch);
template.query(allRowsSelect, (ResultSetExtractor<Object>) resultSet -> processResultSet(consumer, batchSize, entityType, rowMapper, resultSet));
LOG.debug("Streamed entire repository in batches of size {} in {}.", batchSize, stopwatch);
}
use of org.molgenis.data.Fetch in project molgenis by molgenis.
the class PostgreSqlRepository method verifyUpdate.
private void verifyUpdate(List<? extends Entity> entitiesBatch, int[] counts, Attribute idAttr) {
int nrUpdatedEntities = Arrays.stream(counts).sum();
if (nrUpdatedEntities < entitiesBatch.size()) {
Set<Object> existingEntityIds = findAll(entitiesBatch.stream().map(Entity::getIdValue), new Fetch().field(idAttr.getName())).map(Entity::getIdValue).collect(toSet());
Object nonExistingEntityId = entitiesBatch.stream().map(Entity::getIdValue).filter(entityId -> !existingEntityIds.contains(entityId)).findFirst().orElseThrow(() -> new IllegalStateException("Not all entities in batch were updated but all are present in the repository."));
throw new MolgenisValidationException(new ConstraintViolation(format("Cannot update [%s] with id [%s] because it does not exist", entityType.getId(), nonExistingEntityId.toString())));
}
}
use of org.molgenis.data.Fetch in project molgenis by molgenis.
the class EntityListenerRepositoryDecoratorTest method findAllStreamFetch.
@Test
public void findAllStreamFetch() {
Fetch fetch = new Fetch();
Object id0 = "id0";
Object id1 = "id1";
Entity entity0 = Mockito.mock(Entity.class);
Entity entity1 = Mockito.mock(Entity.class);
Stream<Object> entityIds = Stream.of(id0, id1);
Mockito.when(delegateRepository.findAll(entityIds, fetch)).thenReturn(Stream.of(entity0, entity1));
Stream<Entity> expectedEntities = entityListenerRepositoryDecorator.findAll(entityIds, fetch);
Assert.assertEquals(expectedEntities.collect(Collectors.toList()), Arrays.asList(entity0, entity1));
}
use of org.molgenis.data.Fetch in project molgenis by molgenis.
the class InMemoryRepositoryTest method findOneObjectFetch.
@Test
public void findOneObjectFetch() throws IOException {
String idAttrName = "id";
EntityType entityType = mock(EntityType.class);
Attribute idAttr = when(mock(Attribute.class).getName()).thenReturn(idAttrName).getMock();
when(entityType.getIdAttribute()).thenReturn(idAttr);
try (InMemoryRepository inMemoryRepository = new InMemoryRepository(entityType)) {
Object id = 0;
Entity entity = when(mock(Entity.class).get(idAttrName)).thenReturn(id).getMock();
inMemoryRepository.add(entity);
Fetch fetch = new Fetch();
assertEquals(inMemoryRepository.findOneById(id, fetch), entity);
}
}
Aggregations