use of com.b2international.snowowl.core.locks.Locks in project snow-owl by b2ihealthcare.
the class ClassificationJobRequest method executeClassification.
private void executeClassification(final BranchContext context, final String classificationId, final ClassificationTracker tracker) {
final RevisionSearcher revisionSearcher = context.service(RevisionSearcher.class);
TerminologyResource resource = context.service(TerminologyResource.class);
@SuppressWarnings("unchecked") final Set<String> reasonerExcludedModuleIds = Collections3.toImmutableSet((Iterable) resource.getSettings().getOrDefault(REASONER_EXCLUDE_MODULE_IDS, Collections.emptySet()));
final SnomedCoreConfiguration configuration = context.service(SnomedCoreConfiguration.class);
final boolean concreteDomainSupported = configuration.isConcreteDomainSupported();
final ReasonerTaxonomy taxonomy;
try (Locks locks = Locks.on(context).lock(DatastoreLockContextDescriptions.CLASSIFY, parentLockContext)) {
taxonomy = buildTaxonomy(revisionSearcher, reasonerExcludedModuleIds, concreteDomainSupported);
} catch (final LockedException e) {
throw new ReasonerApiException("Couldn't acquire exclusive access to terminology store for classification; %s", e.getMessage(), e);
}
final OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
ontologyManager.getOntologyFactories().add(new DelegateOntologyFactory(taxonomy));
// TODO: custom moduleId in ontology IRI?
final IRI ontologyIRI = IRI.create(DelegateOntology.NAMESPACE_SCTM + Concepts.MODULE_SCT_CORE);
try {
final DelegateOntology ontology = (DelegateOntology) ontologyManager.createOntology(ontologyIRI);
final ReasonerTaxonomyInferrer inferrer = new ReasonerTaxonomyInferrer(reasonerId, ontology, context);
final ReasonerTaxonomy inferredTaxonomy = inferrer.addInferences(taxonomy);
final NormalFormGenerator normalFormGenerator = new NormalFormGenerator(inferredTaxonomy);
tracker.classificationCompleted(classificationId, inferredTaxonomy, normalFormGenerator);
} catch (final OWLOntologyCreationException e) {
throw new ReasonerApiException("Exception caught while creating ontology instance.", e);
} catch (final ReasonerInterruptedException | OWLReasonerRuntimeException e) {
throw new ReasonerApiException("Exception caught while classifying the ontology.", e);
}
}
use of com.b2international.snowowl.core.locks.Locks in project snow-owl by b2ihealthcare.
the class SaveJobRequest method execute.
@Override
public Boolean execute(final BranchContext context) {
final IProgressMonitor monitor = context.service(IProgressMonitor.class);
final ClassificationTracker tracker = context.service(ClassificationTracker.class);
final String user = !Strings.isNullOrEmpty(userId) ? userId : context.service(User.class).getUsername();
try (Locks locks = Locks.on(context).user(user).lock(DatastoreLockContextDescriptions.SAVE_CLASSIFICATION_RESULTS, parentLockContext)) {
return persistChanges(context, monitor);
} catch (final LockedException e) {
tracker.classificationFailed(classificationId);
throw new ReasonerApiException("Couldn't acquire exclusive access to terminology store for persisting classification changes; %s", e.getMessage(), e);
} catch (final Exception e) {
LOG.error("Unexpected error while persisting classification changes.", e);
tracker.classificationSaveFailed(classificationId);
throw new ReasonerApiException("Error while persisting classification changes on '%s'.", context.path(), e);
} finally {
monitor.done();
}
}
use of com.b2international.snowowl.core.locks.Locks in project snow-owl by b2ihealthcare.
the class SnomedRf2ImportRequest method execute.
@Override
public ImportResponse execute(BranchContext context) {
log = LoggerFactory.getLogger("import");
context = context.inject().bind(Logger.class, log).build();
Rf2ImportConfiguration importConfig = new Rf2ImportConfiguration(releaseType, createVersions);
validate(context, importConfig);
final InternalAttachmentRegistry fileReg = (InternalAttachmentRegistry) context.service(AttachmentRegistry.class);
final File rf2Archive = fileReg.getAttachment(this.rf2Archive.getAttachmentId());
try (Locks locks = Locks.on(context).lock(DatastoreLockContextDescriptions.IMPORT)) {
return doImport(context, rf2Archive, importConfig);
} catch (Exception e) {
if (e instanceof ApiException) {
throw (ApiException) e;
}
throw SnowowlRuntimeException.wrap(e);
}
}
use of com.b2international.snowowl.core.locks.Locks in project snow-owl by b2ihealthcare.
the class RepositoryTransactionContext method commit.
@Override
public Optional<Commit> commit(String author, String commitComment, String parentLockContext) {
if (!isDirty()) {
return Optional.empty();
}
// fall back to the current lock context or ROOT if none is present
if (Strings.isNullOrEmpty(parentLockContext)) {
parentLockContext = optionalService(Locks.class).map(Locks::lockContext).orElse(DatastoreLockContextDescriptions.ROOT);
}
final DatastoreLockContext lockContext = createLockContext(service(User.class).getUsername(), parentLockContext);
final DatastoreLockTarget lockTarget = createLockTarget(info().id(), path());
IOperationLockManager locks = service(IOperationLockManager.class);
Commit commit = null;
try {
locks.lock(lockContext, 1000L, lockTarget);
final long timestamp = service(TimestampProvider.class).getTimestamp();
log().info("Persisting changes to {}@{}", path(), timestamp);
commit = staging.commit(null, timestamp, author, commitComment);
log().info("Changes have been successfully persisted to {}@{}.", path(), timestamp);
return Optional.ofNullable(commit);
} catch (final IndexException e) {
Throwable rootCause = Throwables.getRootCause(e);
if (rootCause instanceof CycleDetectedException) {
throw (CycleDetectedException) rootCause;
}
throw new SnowowlRuntimeException(e.getMessage(), e);
} finally {
locks.unlock(lockContext, lockTarget);
if (commit != null && isNotificationEnabled()) {
service(RepositoryCommitNotificationSender.class).publish(this, commit);
}
clear();
}
}
Aggregations