use of com.b2international.snowowl.core.request.CommitResult in project snow-owl by b2ihealthcare.
the class SnomedExportApiTest method exportSnapshotWithBranchPoint.
@Test
public void exportSnapshotWithBranchPoint() throws Exception {
final String codeSystemId = "SNOMEDCT-snapshot-with-branch-at";
createCodeSystem(branchPath, codeSystemId).statusCode(201);
final CommitResult commitResult = SnomedRequests.prepareCommit().setBody(SnomedRequests.prepareNewRelationship().setId(new NamespaceIdStrategy("")).setActive(true).setCharacteristicTypeId(Concepts.INFERRED_RELATIONSHIP).setDestinationId(Concepts.ROOT_CONCEPT).setModifierId(Concepts.EXISTENTIAL_RESTRICTION_MODIFIER).setModuleId(Concepts.MODULE_SCT_CORE).setRelationshipGroup(0).setSourceId(Concepts.ACCEPTABILITY).setTypeId(Concepts.IS_A).setUnionGroup(0)).setCommitComment("Created new relationship").setAuthor(RestExtensions.USER).build(ResourceURI.of(CodeSystem.RESOURCE_TYPE, codeSystemId)).execute(getBus()).getSync(1L, TimeUnit.MINUTES);
final long timestamp = commitResult.getCommitTimestamp() - 1L;
final String relationshipId = commitResult.getResultAs(String.class);
// id, effectiveTime, active, moduleId, sourceId, destinationId, relationshipGroup, typeId, characteristicTypeId, modifierId
final String relationshipLine = getComponentLine(List.of(relationshipId, "", "1", Concepts.MODULE_SCT_CORE, Concepts.ACCEPTABILITY, Concepts.ROOT_CONCEPT, "0", Concepts.IS_A, Concepts.INFERRED_RELATIONSHIP, Concepts.EXISTENTIAL_RESTRICTION_MODIFIER));
final Map<String, Object> config = Map.of("type", Rf2ReleaseType.SNAPSHOT.name(), "includeUnpublished", true);
final File exportArchive = doExport(codeSystemId, config);
final String relationshipFileName = "sct2_Relationship_Snapshot";
final Multimap<String, Pair<Boolean, String>> fileToLinesMap = ArrayListMultimap.<String, Pair<Boolean, String>>create();
fileToLinesMap.put(relationshipFileName, Pair.of(true, relationshipLine));
assertArchiveContainsLines(exportArchive, fileToLinesMap);
/*
* A snapshot export with a timestamp set before creating the relationship should result in the relationship
* not appearing in the export file.
*/
final File exportArchiveWithTimestamp = doExport(codeSystemId + "@" + timestamp, config);
fileToLinesMap.clear();
fileToLinesMap.put(relationshipFileName, Pair.of(false, relationshipLine));
assertArchiveContainsLines(exportArchiveWithTimestamp, fileToLinesMap);
}
use of com.b2international.snowowl.core.request.CommitResult in project snow-owl by b2ihealthcare.
the class SnomedMergePerformanceTest method testPerf.
@Test
public void testPerf() throws Exception {
IBranchPath branch = BranchPathUtils.createPath(branchPath, "merge-test");
branching.createBranch(branch).statusCode(201);
BulkRequestBuilder<TransactionContext> bulk = BulkRequest.create();
final int numberOfConceptsToWorkWith = 10_000;
for (int i = 0; i < numberOfConceptsToWorkWith; i++) {
bulk.add(SnomedRequests.prepareNewConcept().setIdFromNamespace(null).setActive(true).setModuleId(Concepts.MODULE_SCT_CORE).addDescription(SnomedRequests.prepareNewDescription().setIdFromNamespace(null).setTerm("MergeTest FSN " + i).setTypeId(Concepts.FULLY_SPECIFIED_NAME).setLanguageCode("en").preferredIn(Concepts.REFSET_LANGUAGE_TYPE_UK)).addDescription(SnomedRequests.prepareNewDescription().setIdFromNamespace(null).setTerm("MergeTest PT " + i).setTypeId(Concepts.SYNONYM).setLanguageCode("en").preferredIn(Concepts.REFSET_LANGUAGE_TYPE_UK)).addRelationship(SnomedRequests.prepareNewRelationship().setIdFromNamespace(null).setCharacteristicTypeId(Concepts.STATED_RELATIONSHIP).setTypeId(Concepts.IS_A).setDestinationId(Concepts.ROOT_CONCEPT)));
}
Stopwatch w = Stopwatch.createStarted();
// commit large changeset
CommitResult createCommitResult = SnomedRequests.prepareCommit().setBody(bulk).setCommitComment("Commit large bulk request").build(branch.getPath()).execute(getBus()).getSync();
System.err.println("Bulk create 10_000 concepts commit took: " + w);
w.reset().start();
// extract the new IDs assigned to the concepts
Set<String> idsOfCreatedConcepts = createCommitResult.getResultAs(BulkResponse.class).stream().filter(String.class::isInstance).map(String.class::cast).collect(Collectors.toSet());
// retrieve the first 100 created concepts back, check the module
SnomedConcepts first100 = SnomedRequests.prepareSearchConcept().setLimit(100).filterByIds(idsOfCreatedConcepts).build(branch.getPath()).execute(getBus()).getSync();
assertThat(first100.getTotal()).isEqualTo(numberOfConceptsToWorkWith);
first100.forEach(concept -> {
assertThat(concept.getModuleId()).isEqualTo(Concepts.MODULE_SCT_CORE);
});
// check that SNOMED CT ROOT has 10_000
int totalDescendants = SnomedRequests.prepareSearchConcept().setLimit(0).filterByStatedParent(Concepts.ROOT_CONCEPT).build(branch.getPath()).execute(getBus()).getSync().getTotal();
assertThat(totalDescendants).isGreaterThanOrEqualTo(numberOfConceptsToWorkWith);
// find the relationship IDs of the concepts
final Set<String> conceptRelationshipIds = SnomedRequests.prepareSearchRelationship().all().filterByActive(true).filterBySources(idsOfCreatedConcepts).filterByDestination(Concepts.ROOT_CONCEPT).setFields(SnomedRelationshipIndexEntry.Fields.ID).build(branch.getPath()).execute(getBus()).getSync().stream().map(SnomedRelationship::getId).collect(Collectors.toSet());
System.err.println("Verify creation of 10_000 concepts took: " + w);
w.reset().start();
// prepare module bulk update
bulk = BulkRequest.create();
for (String conceptIdToUpdate : idsOfCreatedConcepts) {
bulk.add(SnomedRequests.prepareUpdateConcept(conceptIdToUpdate).setModuleId(Concepts.MODULE_SCT_MODEL_COMPONENT));
}
// commit module bulk update
SnomedRequests.prepareCommit().setBody(bulk).setCommitComment("Commit update bulk request").build(branch.getPath()).execute(getBus()).getSync();
System.err.println("Bulk update 10_000 concepts commit took: " + w);
w.reset().start();
// prepare move concepts in hierarchy to clinical finding update
bulk = BulkRequest.create();
// inactivate all current relationships
for (String relationshipIdToInactive : conceptRelationshipIds) {
bulk.add(SnomedRequests.prepareUpdateRelationship(relationshipIdToInactive).setActive(false));
}
// add new relationships to concepts
for (String conceptIdToUpdate : idsOfCreatedConcepts) {
bulk.add(SnomedRequests.prepareNewRelationship().setIdFromNamespace(null).setSourceId(conceptIdToUpdate).setTypeId(Concepts.IS_A).setDestinationId("404684003").setModuleId(Concepts.MODULE_SCT_MODEL_COMPONENT));
}
// commit move concepts in hierarchy to clinical finding update
SnomedRequests.prepareCommit().setBody(bulk).setCommitComment("Commit update bulk request").build(branch.getPath()).execute(getBus()).getSync();
System.err.println("Bulk move 10_000 concepts to Clinical Finding took: " + w);
w.reset().start();
SnomedRestFixtures.merge(branch, branchPath, "Promote changes from task...").body("status", CoreMatchers.equalTo(Merge.Status.COMPLETED.name()));
System.err.println("Merge took: " + w);
}
use of com.b2international.snowowl.core.request.CommitResult in project snow-owl by b2ihealthcare.
the class SnomedBranchRequestTest method createBranchAndCommitToParent.
@Test
public void createBranchAndCommitToParent() throws Exception {
final Branching branches = RepositoryRequests.branching();
final Merging merges = RepositoryRequests.merging();
final String branchA = UUID.randomUUID().toString();
final String branchB = UUID.randomUUID().toString();
final String first = branches.prepareCreate().setParent(branchPath).setName(branchA).build(REPOSITORY_ID).execute(bus).getSync();
final SnomedDescriptionCreateRequestBuilder fsnBuilder = SnomedRequests.prepareNewDescription().setIdFromNamespace(SnomedIdentifiers.INT_NAMESPACE).setModuleId(Concepts.MODULE_ROOT).setTerm("FSN " + branchA).setTypeId(Concepts.FULLY_SPECIFIED_NAME).setAcceptability(ImmutableMap.of(Concepts.REFSET_LANGUAGE_TYPE_UK, Acceptability.PREFERRED));
final SnomedDescriptionCreateRequestBuilder ptBuilder = SnomedRequests.prepareNewDescription().setIdFromNamespace(SnomedIdentifiers.INT_NAMESPACE).setModuleId(Concepts.MODULE_ROOT).setTerm("PT " + branchA).setTypeId(Concepts.SYNONYM).setAcceptability(ImmutableMap.of(Concepts.REFSET_LANGUAGE_TYPE_UK, Acceptability.PREFERRED));
final AsyncRequest<CommitResult> conceptRequest = SnomedRequests.prepareNewConcept().setModuleId(Concepts.MODULE_ROOT).setIdFromNamespace(SnomedIdentifiers.INT_NAMESPACE).addParent(Concepts.ROOT_CONCEPT).addDescription(fsnBuilder).addDescription(ptBuilder).build(first, RestExtensions.USER, "Created new concept");
final CommitResult info = conceptRequest.execute(bus).getSync();
final String conceptId = info.getResultAs(String.class);
final String firstParentPath = BranchPathUtils.createPath(first).getParentPath();
final Request<ServiceProvider, Merge> mergeRequest = merges.prepareCreate().setSource(first).setTarget(firstParentPath).setUserId(User.SYSTEM.getUsername()).setCommitComment("Merging changes").build(REPOSITORY_ID).getRequest();
final String mergeJobId = JobRequests.prepareSchedule().setDescription("Merging changes").setRequest(mergeRequest).setUser(User.SYSTEM.getUsername()).buildAsync().execute(bus).getSync();
final RemoteJobEntry mergeJobResult = JobRequests.waitForJob(bus, mergeJobId);
final Merge merge = mergeJobResult.getResultAs(JsonSupport.getDefaultObjectMapper(), Merge.class);
assertEquals(true, merge.getConflicts().isEmpty());
String second = branches.prepareCreate().setParent(firstParentPath).setName(branchB).build(REPOSITORY_ID).execute(bus).getSync();
final Branch sourceBranch = branches.prepareGet(merge.getSource()).build(REPOSITORY_ID).execute(bus).getSync();
final Branch secondBranch = branches.prepareGet(second).build(REPOSITORY_ID).execute(bus).getSync();
assertBranchesCreated(branchA, branchB, sourceBranch, secondBranch);
assertBranchSegmentsValid(merge.getTarget(), sourceBranch.path(), secondBranch.path());
// Check that the concept is visible on parent
SnomedRequests.prepareGetConcept(conceptId).build(firstParentPath).execute(bus).getSync();
}
use of com.b2international.snowowl.core.request.CommitResult in project snow-owl by b2ihealthcare.
the class SaveJobRequest method persistChanges.
private Boolean persistChanges(final BranchContext context, final IProgressMonitor monitor) {
// Repeat the same checks as in ClassificationSaveRequest, now within the lock
final ClassificationTask classification = ClassificationRequests.prepareGetClassification(classificationId).build().execute(context);
final String branchPath = classification.getBranch();
final Branch branch = RepositoryRequests.branching().prepareGet(branchPath).build().execute(context);
if (!ClassificationSaveRequest.SAVEABLE_STATUSES.contains(classification.getStatus())) {
throw new BadRequestException("Classification '%s' is not in the expected state to start saving changes.", classificationId);
}
if (classification.getTimestamp() < branch.headTimestamp()) {
throw new BadRequestException("Classification '%s' on branch '%s' is stale (classification timestamp: %s, head timestamp: %s).", classificationId, branchPath, classification.getTimestamp(), branch.headTimestamp());
}
final ClassificationTracker classificationTracker = context.service(ClassificationTracker.class);
// Signal the state change
classificationTracker.classificationSaving(classificationId);
final SubMonitor subMonitor = SubMonitor.convert(monitor, "Persisting changes", 6);
final BulkRequestBuilder<TransactionContext> bulkRequestBuilder = BulkRequest.create();
applyChanges(subMonitor, context, bulkRequestBuilder);
long resultTimeStamp = Commit.NO_COMMIT_TIMESTAMP;
for (List<Request<TransactionContext, ?>> partition : Iterables.partition(bulkRequestBuilder.build().getRequests(), getCommitLimit(context))) {
final BulkRequestBuilder<TransactionContext> batchRequest = BulkRequest.create();
partition.forEach(request -> batchRequest.add(request));
final Request<BranchContext, CommitResult> commitRequest = SnomedRequests.prepareCommit().setBody(batchRequest.build()).setCommitComment(commitComment).setParentContextDescription(DatastoreLockContextDescriptions.SAVE_CLASSIFICATION_RESULTS).setAuthor(userId).build();
final CommitResult commitResult = new IdRequest<>(commitRequest).execute(context);
resultTimeStamp = commitResult.getCommitTimestamp();
}
if (Commit.NO_COMMIT_TIMESTAMP == resultTimeStamp) {
classificationTracker.classificationSaveFailed(classificationId);
return Boolean.FALSE;
} else {
classificationTracker.classificationSaved(classificationId, resultTimeStamp);
return Boolean.TRUE;
}
}
use of com.b2international.snowowl.core.request.CommitResult in project snow-owl by b2ihealthcare.
the class ResourceApiTest method createdAtAndUpdatedAt.
@Test
public void createdAtAndUpdatedAt() throws Exception {
createDefaultCodeSystem(DEFAULT_CODE_SYSTEM_SHORT_NAME, DEFAULT_CODE_SYSTEM_OID);
// assert that createdAt and updatedAt values are the same after create
CodeSystem createdCodeSystem = assertResourceGet(DEFAULT_CODE_SYSTEM_SHORT_NAME).statusCode(200).extract().as(CodeSystem.class);
assertEquals(createdCodeSystem.getCreatedAt(), createdCodeSystem.getUpdatedAt());
CommitResult updateResult = CodeSystemRequests.prepareUpdateCodeSystem(DEFAULT_CODE_SYSTEM_SHORT_NAME).setCopyright("Updated copyright").build(RestExtensions.USER, String.format("Updated copyright %s", DEFAULT_CODE_SYSTEM_SHORT_NAME)).execute(bus).getSync();
// assert that createdAt and updatedAt values are the same after create
CodeSystem updatedCodeSystem = assertResourceGet(DEFAULT_CODE_SYSTEM_SHORT_NAME).statusCode(200).extract().as(CodeSystem.class);
// createdAt stays as is, but updatedAt got updated to a time after the marked value
assertEquals(createdCodeSystem.getCreatedAt(), updatedCodeSystem.getCreatedAt());
assertEquals((Long) updateResult.getCommitTimestamp(), updatedCodeSystem.getUpdatedAt());
}
Aggregations