Search in sources :

Example 1 with CommitResult

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);
}
Also used : CommitResult(com.b2international.snowowl.core.request.CommitResult) File(java.io.File) Pair(com.b2international.commons.Pair) AbstractSnomedApiTest(com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest) Test(org.junit.Test)

Example 2 with CommitResult

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);
}
Also used : CommitResult(com.b2international.snowowl.core.request.CommitResult) TransactionContext(com.b2international.snowowl.core.domain.TransactionContext) Stopwatch(com.google.common.base.Stopwatch) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) IBranchPath(com.b2international.snowowl.core.api.IBranchPath) Test(org.junit.Test) AbstractSnomedApiTest(com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest)

Example 3 with CommitResult

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();
}
Also used : BaseRevisionBranching(com.b2international.index.revision.BaseRevisionBranching) Branching(com.b2international.snowowl.core.branch.Branching) Merge(com.b2international.snowowl.core.merge.Merge) Merging(com.b2international.snowowl.core.branch.Merging) CommitResult(com.b2international.snowowl.core.request.CommitResult) RevisionBranch(com.b2international.index.revision.RevisionBranch) Branch(com.b2international.snowowl.core.branch.Branch) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) SnomedDescriptionCreateRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedDescriptionCreateRequestBuilder) RemoteJobEntry(com.b2international.snowowl.core.jobs.RemoteJobEntry) Test(org.junit.Test)

Example 4 with CommitResult

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;
    }
}
Also used : CommitResult(com.b2international.snowowl.core.request.CommitResult) SubMonitor(org.eclipse.core.runtime.SubMonitor) Request(com.b2international.snowowl.core.events.Request) BulkRequest(com.b2international.snowowl.core.events.bulk.BulkRequest) Branch(com.b2international.snowowl.core.branch.Branch) TransactionContext(com.b2international.snowowl.core.domain.TransactionContext) BranchContext(com.b2international.snowowl.core.domain.BranchContext) BadRequestException(com.b2international.commons.exceptions.BadRequestException) ClassificationTracker(com.b2international.snowowl.snomed.reasoner.classification.ClassificationTracker)

Example 5 with CommitResult

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());
}
Also used : CommitResult(com.b2international.snowowl.core.request.CommitResult) CodeSystem(com.b2international.snowowl.core.codesystem.CodeSystem) Test(org.junit.Test)

Aggregations

CommitResult (com.b2international.snowowl.core.request.CommitResult)6 Test (org.junit.Test)5 AbstractSnomedApiTest (com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest)3 Branch (com.b2international.snowowl.core.branch.Branch)2 TransactionContext (com.b2international.snowowl.core.domain.TransactionContext)2 Pair (com.b2international.commons.Pair)1 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 BaseRevisionBranching (com.b2international.index.revision.BaseRevisionBranching)1 RevisionBranch (com.b2international.index.revision.RevisionBranch)1 ServiceProvider (com.b2international.snowowl.core.ServiceProvider)1 IBranchPath (com.b2international.snowowl.core.api.IBranchPath)1 Branching (com.b2international.snowowl.core.branch.Branching)1 Merging (com.b2international.snowowl.core.branch.Merging)1 CodeSystem (com.b2international.snowowl.core.codesystem.CodeSystem)1 BranchContext (com.b2international.snowowl.core.domain.BranchContext)1 Request (com.b2international.snowowl.core.events.Request)1 BulkRequest (com.b2international.snowowl.core.events.bulk.BulkRequest)1 RemoteJobEntry (com.b2international.snowowl.core.jobs.RemoteJobEntry)1 Merge (com.b2international.snowowl.core.merge.Merge)1 SnomedConcepts (com.b2international.snowowl.snomed.core.domain.SnomedConcepts)1