Commit 2bd49d09 authored by Matija Obreza's avatar Matija Obreza
Browse files

Subset API

parent 8f28e9c9
......@@ -950,14 +950,14 @@ public class DatasetServiceImpl implements DatasetService {
*
* @return accessionRefs with matching accessions from Genesys
*/
private Collection<DatasetAccessionRef> lookupMatchingAccessions(final Collection<DatasetAccessionRef> accessionRefs) throws InterruptedException {
private Collection<DatasetAccessionRef> lookupMatchingAccessions(final Collection<DatasetAccessionRef> accessionRefs) {
StopWatch stopWatch = StopWatch.createStarted();
accessionRefs.forEach(ref -> ref.trimStringsToNull());
final AtomicInteger batchCounter = new AtomicInteger(0);
Iterables.partition(accessionRefs, 1000).forEach(batch -> {
Iterables.partition(accessionRefs, 500).forEach(batch -> {
LOG.info("Batch {} with size {}", batchCounter.incrementAndGet(), batch.size());
List<Accession> foundAccessions = accessionRepository.find(batch);
......@@ -1024,12 +1024,18 @@ public class DatasetServiceImpl implements DatasetService {
public Dataset rematchDatasetAccessions(Dataset dataset) {
dataset = datasetRepository.findByUuid(dataset.getUuid());
LOG.warn("Linking {} accessions with dataset {}", dataset.getAccessionCount(), dataset.getId());
try {
accessionRefRepository.update(lookupMatchingAccessions(accessionRefRepository.findAll(dataset)));
} catch (InterruptedException e) {
LOG.warn("AccessionRef matching interrupted.");
}
return dataset;
final AtomicInteger batchCounter = new AtomicInteger(0);
// Use parallel streams for lookups
List<DatasetAccessionRef> accessionRefs = accessionRefRepository.findAll(dataset);
Lists.partition(accessionRefs, 1000).parallelStream().forEach(batch -> {
LOG.info("Batch {} with size {}", batchCounter.incrementAndGet(), batch.size());
lookupMatchingAccessions(batch);
});
// Save in this transaction
accessionRefRepository.update(accessionRefs);
return lazyLoad(dataset);
}
@Override
......
......@@ -108,7 +108,7 @@ public class SubsetController {
*/
@GetMapping(value = "/{uuid}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Subset get(@PathVariable("uuid") final UUID uuid) {
return subsetService.get(uuid);
return subsetService.loadSubset(uuid);
}
/**
......@@ -120,7 +120,7 @@ public class SubsetController {
*/
@DeleteMapping(value = "/{uuid},{version}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Subset remove(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version) {
return subsetService.delete(subsetService.get(uuid, version));
return subsetService.delete(subsetService.getSubset(uuid, version));
}
/**
......@@ -176,7 +176,7 @@ public class SubsetController {
@JsonView({ JsonViews.Public.class })
@GetMapping(value = "/accessions/{uuid}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Page<? extends AccessionRef> listAccessions(@PathVariable("uuid") final UUID uuid, final Pagination page) throws NotFoundElement {
return subsetService.listAccessions(subsetService.get(uuid), page.toPageRequest(100));
return subsetService.listAccessions(subsetService.getSubset(uuid), page.toPageRequest(100));
}
/**
......@@ -189,7 +189,7 @@ public class SubsetController {
*/
@PostMapping(value = "/remove-accessions/{uuid},{version}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Subset removeAccessions(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version, @RequestBody final Set<UUID> accessionsUuids) {
final Subset subset = subsetService.get(uuid, version);
final Subset subset = subsetService.getSubset(uuid, version);
return subsetService.removeAccessions(subset, accessionsUuids);
}
......@@ -203,7 +203,7 @@ public class SubsetController {
*/
@PostMapping(value = "/add-accessions/{uuid},{version}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Subset addAccessions(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version, @RequestBody final Set<AccessionRef> accessionRefs) {
final Subset subset = subsetService.get(uuid, version);
final Subset subset = subsetService.getSubset(uuid, version);
return subsetService.addAccessionRefs(subset, accessionRefs);
}
......@@ -217,7 +217,7 @@ public class SubsetController {
*/
@PostMapping(value = "/set-accessions/{uuid},{version}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Subset setAccessions(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version, @RequestBody final Set<AccessionRef> accessionRefs) {
final Subset subset = subsetService.get(uuid, version);
final Subset subset = subsetService.getSubset(uuid, version);
return subsetService.setAccessionRefs(subset, accessionRefs);
}
......@@ -230,7 +230,7 @@ public class SubsetController {
*/
@PostMapping(value = "/rematch-accessions/{uuid},{version}")
public Subset rematchAccessions(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version) {
final Subset subset = subsetService.get(uuid, version);
final Subset subset = subsetService.getSubset(uuid, version);
return subsetService.rematchSubsetAccessions(subset);
}
......@@ -243,7 +243,7 @@ public class SubsetController {
*/
@PostMapping(value = "/approve", produces = { MediaType.APPLICATION_JSON_VALUE })
public Subset approveSubset(@RequestParam(value = "uuid", required = true) final UUID uuid, @RequestParam(value = "version", required = true) final int version) {
final Subset subset = subsetService.get(uuid, version);
final Subset subset = subsetService.getSubset(uuid, version);
return subsetService.approveSubset(subset);
}
......@@ -256,7 +256,7 @@ public class SubsetController {
*/
@PostMapping(value = "/for-review", produces = { MediaType.APPLICATION_JSON_VALUE })
public Subset reviewSubset(@RequestParam(value = "uuid", required = true) final UUID uuid, @RequestParam(value = "version", required = true) final int version) {
final Subset subset = subsetService.get(uuid, version);
final Subset subset = subsetService.getSubset(uuid, version);
return subsetService.reviewSubset(subset);
}
......@@ -269,7 +269,7 @@ public class SubsetController {
*/
@PostMapping(value = "/reject", produces = { MediaType.APPLICATION_JSON_VALUE })
public Subset rejectSubset(@RequestParam(value = "uuid", required = true) final UUID uuid, @RequestParam(value = "version", required = true) final int version) {
final Subset subset = subsetService.get(uuid, version);
final Subset subset = subsetService.getSubset(uuid, version);
return subsetService.rejectSubset(subset);
}
......
......@@ -88,7 +88,7 @@ public class SubsetCreatorController {
*/
@PostMapping(value = "/create")
public SubsetCreator createSubsetCreator(@PathVariable("uuid") final UUID uuid, @RequestBody final SubsetCreator subsetCreator) throws NotFoundElement {
final Subset subset = subsetService.get(uuid);
final Subset subset = subsetService.getSubset(uuid);
return subsetService.createSubsetCreator(subset, subsetCreator);
}
......@@ -103,7 +103,7 @@ public class SubsetCreatorController {
// uses request body
@RequestMapping(value = "/delete", method = { RequestMethod.POST, RequestMethod.DELETE })
public SubsetCreator deleteSubsetCreator(@PathVariable("uuid") final UUID subsetUuid, @RequestBody final SubsetCreator subsetCreator) throws NotFoundElement {
final Subset subset = subsetService.get(subsetUuid);
final Subset subset = subsetService.getSubset(subsetUuid);
return subsetService.removeSubsetCreator(subset, subsetCreator);
}
......@@ -117,7 +117,7 @@ public class SubsetCreatorController {
*/
@PostMapping(value = "/update")
public SubsetCreator updateSubsetCreator(@PathVariable("uuid") final UUID subsetUuid, @RequestBody final SubsetCreator subsetCreator) throws NotFoundElement {
final Subset subset = subsetService.get(subsetUuid);
final Subset subset = subsetService.getSubset(subsetUuid);
return subsetService.updateSubsetCreator(subset, subsetCreator);
}
......
......@@ -85,7 +85,7 @@ public class SubsetController extends BaseController {
Subset subset;
try {
subset = subsetService.get(uuid);
subset = subsetService.getSubset(uuid);
} catch (final NotFoundElement e) {
throw new ResourceNotFoundException(e.getMessage());
}
......
......@@ -89,6 +89,14 @@ public interface SubsetService {
* @return subset loaded from the database
*/
Subset loadSubset(Subset input);
/**
* Load subset.
*
* @param uuid the uuid
* @return the subset
*/
Subset loadSubset(UUID uuid);
/**
* Load Subset.
......@@ -96,7 +104,7 @@ public interface SubsetService {
* @param uuid the uuid
* @return the subset
*/
Subset get(UUID uuid);
Subset getSubset(UUID uuid);
/**
* Load Subset.
......@@ -105,7 +113,7 @@ public interface SubsetService {
* @param version the version
* @return the subset
*/
Subset get(UUID uuid, int version);
Subset getSubset(UUID uuid, int version);
/**
* Method updating Subset.
......
......@@ -169,7 +169,7 @@ public class SubsetServiceImpl implements SubsetService {
final Subset subset = new Subset();
copyValues(subset, source);
subset.setState(PublishState.DRAFT);
return deepLoad(subsetRepository.save(subset));
return lazyLoad(subsetRepository.save(subset));
}
/**
......@@ -204,7 +204,7 @@ public class SubsetServiceImpl implements SubsetService {
* @param subset subset to deepload
* @return fully loaded subset
*/
private Subset deepLoad(final Subset subset) {
private Subset lazyLoad(final Subset subset) {
if (subset == null)
throw new NotFoundElement("No such subset");
......@@ -223,7 +223,15 @@ public class SubsetServiceImpl implements SubsetService {
@Override
public Subset loadSubset(final Subset input) {
LOG.debug("Load Subset.");
return deepLoad(getSubset(input));
return lazyLoad(getSubset(input));
}
/* (non-Javadoc)
* @see org.genesys2.server.service.SubsetService#loadSubset(java.util.UUID)
*/
@Override
public Subset loadSubset(UUID uuid) {
return lazyLoad(getSubset(uuid));
}
private Subset getSubset(final Subset input) {
......@@ -245,7 +253,7 @@ public class SubsetServiceImpl implements SubsetService {
* {@inheritDoc}
*/
@Override
public Subset get(final UUID uuid) {
public Subset getSubset(final UUID uuid) {
return subsetRepository.getByUuid(uuid);
}
......@@ -254,7 +262,7 @@ public class SubsetServiceImpl implements SubsetService {
*/
@Override
@PostAuthorize("hasRole('ADMINISTRATOR') || returnObject==null || returnObject.isPublished() || hasPermission(returnObject, 'read')")
public Subset get(final UUID uuid, final int version) {
public Subset getSubset(final UUID uuid, final int version) {
final Subset subset = subsetRepository.getByUuidAndVersion(uuid, version);
if (subset == null)
throw new ConcurrencyFailureException("Record with that version doesn't exist");
......@@ -272,7 +280,7 @@ public class SubsetServiceImpl implements SubsetService {
final Subset subset = getSubset(source);
copyValues(subset, source);
return deepLoad(subsetRepository.save(subset));
return lazyLoad(subsetRepository.save(subset));
}
/**
......@@ -299,7 +307,7 @@ public class SubsetServiceImpl implements SubsetService {
accessionRefRepository.delete(subset, accessionRepository.find(accessionsUuids));
subset.setAccessionCount((int) accessionRefRepository.countBySubset(subset));
return deepLoad(subsetRepository.save(subset));
return lazyLoad(subsetRepository.save(subset));
}
/**
......@@ -367,7 +375,7 @@ public class SubsetServiceImpl implements SubsetService {
LOG.warn("Interrupted rematcher");
}
});
return deepLoad(subsetRepository.save(subset));
return lazyLoad(subsetRepository.save(subset));
}
/**
......@@ -389,7 +397,7 @@ public class SubsetServiceImpl implements SubsetService {
}
loaded.setState(PublishState.PUBLISHED);
return deepLoad(subsetRepository.save(loaded));
return lazyLoad(subsetRepository.save(loaded));
}
/**
......@@ -411,7 +419,7 @@ public class SubsetServiceImpl implements SubsetService {
}
loaded.setState(PublishState.REVIEWING);
return deepLoad(subsetRepository.save(loaded));
return lazyLoad(subsetRepository.save(loaded));
}
/**
......@@ -434,7 +442,7 @@ public class SubsetServiceImpl implements SubsetService {
}
loaded.setState(PublishState.DRAFT);
return deepLoad(subsetRepository.save(loaded));
return lazyLoad(subsetRepository.save(loaded));
}
@Override
......@@ -566,8 +574,18 @@ public class SubsetServiceImpl implements SubsetService {
public Subset rematchSubsetAccessions(Subset subset) {
subset = subsetRepository.getByUuid(subset.getUuid());
LOG.warn("Linking {} accessions with subset {}", subset.getAccessionCount(), subset.getId());
accessionRefRepository.update(lookupMatchingAccessions(accessionRefRepository.findAll(subset)));
return subset;
final AtomicInteger batchCounter = new AtomicInteger(0);
// Use parallel streams for lookups
List<SubsetAccessionRef> accessionRefs = accessionRefRepository.findAll(subset);
Lists.partition(accessionRefs, 1000).parallelStream().forEach(batch -> {
LOG.info("Batch {} with size {}", batchCounter.incrementAndGet(), batch.size());
lookupMatchingAccessions(batch);
});
// Save in this transaction
accessionRefRepository.update(accessionRefs);
return lazyLoad(subset);
}
/**
......@@ -584,7 +602,7 @@ public class SubsetServiceImpl implements SubsetService {
final AtomicInteger batchCounter = new AtomicInteger(0);
Iterables.partition(accessionRefs, 1000).forEach(batch -> {
Iterables.partition(accessionRefs, 500).forEach(batch -> {
LOG.info("Batch {} with size {}", batchCounter.incrementAndGet(), batch.size());
List<Accession> foundAccessions = accessionRepository.find(batch);
......
......@@ -290,7 +290,7 @@ public class SubsetRestControllerTest extends AbstractApiTest {
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
/*@formatter:on*/
final Subset updated = subsetService.get(subset.getUuid());
final Subset updated = subsetService.getSubset(subset.getUuid());
assertThat(updated.getAccessionCount(), is(0));
}
......@@ -312,7 +312,7 @@ public class SubsetRestControllerTest extends AbstractApiTest {
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
/*@formatter:on*/
final Subset updated = subsetService.get(subset.getUuid());
final Subset updated = subsetService.getSubset(subset.getUuid());
assertThat(updated.getAccessionCount(), is(2));
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment