Search in sources :

Example 1 with Mappings

use of com.b2international.index.mapping.Mappings in project snow-owl by b2ihealthcare.

the class IndexResource method before.

@Override
protected void before() throws Throwable {
    if (INIT.compareAndSet(false, true)) {
        mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
        client = Indexes.createIndexClient(UUID.randomUUID().toString(), mapper, new Mappings(), indexSettings.get());
        index = new DefaultIndex(client);
        revisionIndex = new DefaultRevisionIndex(index, new TimestampProvider.Default(), mapper);
    }
    // apply mapper changes first
    objectMapperConfigurator.accept(mapper);
    // then mapping changes
    revisionIndex.admin().updateMappings(new Mappings(types));
    // then settings changes
    revisionIndex.admin().updateSettings(indexSettings.get());
    // then make sure we have all indexes ready for tests
    revisionIndex.admin().create();
}
Also used : Mappings(com.b2international.index.mapping.Mappings) DefaultRevisionIndex(com.b2international.index.revision.DefaultRevisionIndex) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with Mappings

use of com.b2international.index.mapping.Mappings in project snow-owl by b2ihealthcare.

the class SnomedIdentifierPlugin method registerSnomedIdentifierService.

private void registerSnomedIdentifierService(final SnomedIdentifierConfiguration conf, final Environment env, final ISnomedIdentifierReservationService reservationService) {
    ISnomedIdentifierService identifierService = null;
    switch(conf.getStrategy()) {
        case EMBEDDED:
            final Index index = Indexes.createIndex(SNOMED_IDS_INDEX, env.service(ObjectMapper.class), new Mappings(SctId.class), env.service(IndexSettings.class).forIndex(env.service(RepositoryConfiguration.class).getIndexConfiguration(), SNOMED_IDS_INDEX));
            index.admin().create();
            final ItemIdGenerationStrategy generationStrategy = new SequentialItemIdGenerationStrategy(reservationService);
            identifierService = new DefaultSnomedIdentifierService(index, generationStrategy, reservationService, conf);
            break;
        case CIS:
            final ObjectMapper mapper = new ObjectMapper();
            identifierService = new CisSnomedIdentifierService(conf, reservationService, mapper);
            break;
        default:
            throw new IllegalStateException(String.format("Unknown ID generation source configured: %s. ", conf.getStrategy()));
    }
    env.services().registerService(ISnomedIdentifierService.class, identifierService);
    LOGGER.info("Snow Owl is configured to use {} based identifier service.", conf.getStrategy());
}
Also used : SequentialItemIdGenerationStrategy(com.b2international.snowowl.snomed.cis.gen.SequentialItemIdGenerationStrategy) SequentialItemIdGenerationStrategy(com.b2international.snowowl.snomed.cis.gen.SequentialItemIdGenerationStrategy) ItemIdGenerationStrategy(com.b2international.snowowl.snomed.cis.gen.ItemIdGenerationStrategy) Mappings(com.b2international.index.mapping.Mappings) CisSnomedIdentifierService(com.b2international.snowowl.snomed.cis.client.CisSnomedIdentifierService) DefaultSnomedIdentifierService(com.b2international.snowowl.snomed.cis.memory.DefaultSnomedIdentifierService) Index(com.b2international.index.Index) RepositoryConfiguration(com.b2international.snowowl.core.config.RepositoryConfiguration) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SctId(com.b2international.snowowl.snomed.cis.domain.SctId)

Example 3 with Mappings

use of com.b2international.index.mapping.Mappings in project snow-owl by b2ihealthcare.

the class StagingArea method collectConflicts.

private void collectConflicts(RevisionBranchChangeSet fromChangeSet, List<RevisionCompareDetail> fromChangeDetails, RevisionBranchChangeSet toChangeSet, List<RevisionCompareDetail> toChangeDetails, List<Conflict> conflictsToReport, Map<Class<? extends Revision>, Multimap<String, RevisionPropertyDiff>> propertyUpdatesToApply, RevisionConflictProcessor conflictProcessor) {
    List<Conflict> conflicts = newArrayList();
    for (Class<? extends Revision> type : ImmutableSet.copyOf(Iterables.concat(fromChangeSet.getAddedTypes(), toChangeSet.getAddedTypes()))) {
        final Set<String> newRevisionIdsOnSource = fromChangeSet.getAddedIds(type);
        final Set<String> newRevisionIdsOnTarget = toChangeSet.getAddedIds(type);
        final Set<String> addedInSourceAndTarget = Sets.intersection(newRevisionIdsOnSource, newRevisionIdsOnTarget);
        // check for added in both source and target conflicts
        if (!addedInSourceAndTarget.isEmpty()) {
            addedInSourceAndTarget.forEach(revisionId -> {
                conflicts.add(new AddedInSourceAndTargetConflict(ObjectId.of(type, revisionId)));
            });
        }
        // check deleted containers on target and report them as conflicts
        newRevisionIdsOnSource.forEach(newRevisionOnSource -> {
            ObjectId newRevisionOnSourceId = ObjectId.of(type, newRevisionOnSource);
            ObjectId requiredContainer = fromChangeSet.getContainerId(newRevisionOnSourceId);
            if (requiredContainer != null && toChangeSet.isRemoved(requiredContainer)) {
                conflicts.add(new AddedInSourceAndDetachedInTargetConflict(newRevisionOnSourceId, requiredContainer));
            }
        });
        // check deleted containers on source and report them as conflicts
        newRevisionIdsOnTarget.forEach(newRevisionOnTarget -> {
            ObjectId newRevisionOnTargetId = ObjectId.of(type, newRevisionOnTarget);
            ObjectId requiredContainer = toChangeSet.getContainerId(newRevisionOnTargetId);
            if (requiredContainer != null && fromChangeSet.isRemoved(requiredContainer)) {
                conflicts.add(new AddedInTargetAndDetachedInSourceConflict(requiredContainer, newRevisionOnTargetId));
            }
        });
    }
    // check property conflicts
    Set<String> changedRevisionIdsToCheck = newHashSet(toChangeSet.getChangedIds());
    Set<String> removedRevisionIdsToCheck = newHashSet(toChangeSet.getRemovedIds());
    for (Class<? extends Revision> type : fromChangeSet.getChangedTypes()) {
        final DocumentMapping mapping = index.admin().mappings().getMapping(type);
        final String docType = mapping.typeAsString();
        Set<String> changedRevisionIdsToMerge = newHashSet(fromChangeSet.getChangedIds(type));
        // first handle changed vs. removed
        Set<String> changedInSourceDetachedInTargetIds = Sets.newHashSet(Sets.intersection(changedRevisionIdsToMerge, removedRevisionIdsToCheck));
        if (!changedInSourceDetachedInTargetIds.isEmpty()) {
            // report any conflicts
            changedInSourceDetachedInTargetIds.forEach(changedInSourceDetachedInTargetId -> {
                List<RevisionPropertyDiff> sourceChanges = fromChangeDetails.stream().filter(detail -> detail.getObject().id().equals(changedInSourceDetachedInTargetId)).filter(detail -> !detail.isComponentChange()).map(change -> new RevisionPropertyDiff(change.getProperty(), change.getFromValue(), change.getValue())).collect(Collectors.toList());
                Conflict conflict = conflictProcessor.handleChangedInSourceDetachedInTarget(ObjectId.of(docType, changedInSourceDetachedInTargetId), sourceChanges);
                if (conflict != null) {
                    conflicts.add(conflict);
                }
            });
            // register them as revised on source from the target branch point of view
            revisionsToReviseOnMergeSource.putAll(type, changedInSourceDetachedInTargetIds);
            changedInSourceDetachedInTargetIds.forEach(id -> fromChangeSet.removeChanged(type, id));
            changedRevisionIdsToMerge.removeAll(changedInSourceDetachedInTargetIds);
        }
        // then handle changed vs. changed with the conflict processor
        Set<String> changedInSourceAndTargetIds = Sets.intersection(changedRevisionIdsToMerge, changedRevisionIdsToCheck);
        if (!changedInSourceAndTargetIds.isEmpty()) {
            final Map<String, Map<String, RevisionCompareDetail>> sourcePropertyChangesByObject = indexPropertyChangesByObject(fromChangeDetails);
            final Map<String, Map<String, RevisionCompareDetail>> targetPropertyChangesByObject = indexPropertyChangesByObject(toChangeDetails);
            for (String changedInSourceAndTargetId : changedInSourceAndTargetIds) {
                // take the prop changes from both paths
                final Map<String, RevisionCompareDetail> sourcePropertyChanges = sourcePropertyChangesByObject.remove(changedInSourceAndTargetId);
                final Map<String, RevisionCompareDetail> targetPropertyChanges = targetPropertyChangesByObject.remove(changedInSourceAndTargetId);
                if (sourcePropertyChanges != null) {
                    for (Entry<String, RevisionCompareDetail> sourceChange : sourcePropertyChanges.entrySet()) {
                        final String changedProperty = sourceChange.getKey();
                        final RevisionCompareDetail sourcePropertyChange = sourceChange.getValue();
                        final RevisionPropertyDiff sourceChangeDiff = new RevisionPropertyDiff(changedProperty, sourcePropertyChange.getFromValue(), sourcePropertyChange.getValue());
                        final RevisionCompareDetail targetPropertyChange = targetPropertyChanges == null ? null : targetPropertyChanges.get(changedProperty);
                        if (targetPropertyChange == null) {
                            // this property did not change in target, just apply directly on the target object via
                            if (!propertyUpdatesToApply.containsKey(type)) {
                                propertyUpdatesToApply.put(type, HashMultimap.create());
                            }
                            propertyUpdatesToApply.get(type).put(changedInSourceAndTargetId, sourceChangeDiff);
                            fromChangeSet.removeChanged(type, changedInSourceAndTargetId);
                        } else {
                            RevisionPropertyDiff targetChangeDiff = new RevisionPropertyDiff(targetPropertyChange.getProperty(), targetPropertyChange.getFromValue(), targetPropertyChange.getValue());
                            // changed on both sides, ask conflict processor to resolve the issue or raise conflict error
                            RevisionPropertyDiff resolution = conflictProcessor.handleChangedInSourceAndTarget(changedInSourceAndTargetId, mapping, sourceChangeDiff, targetChangeDiff, mapper);
                            if (resolution == null) {
                                conflicts.add(new ChangedInSourceAndTargetConflict(sourcePropertyChange.getObject(), sourceChangeDiff.convert(conflictProcessor), targetChangeDiff.convert(conflictProcessor)));
                            } else {
                                if (!propertyUpdatesToApply.containsKey(type)) {
                                    propertyUpdatesToApply.put(type, HashMultimap.create());
                                }
                                propertyUpdatesToApply.get(type).put(changedInSourceAndTargetId, resolution);
                            }
                            fromChangeSet.removeChanged(type, changedInSourceAndTargetId);
                        }
                    }
                } else {
                    fromChangeSet.removeChanged(type, changedInSourceAndTargetId);
                }
                // this object has changed on both sides either by tracked field changes or due to some cascading derived field change
                // revise the revision on source, since we already have one on this branch already
                revisionsToReviseOnMergeSource.put(type, changedInSourceAndTargetId);
            }
        }
    }
    // after generic conflict processing execute domain specific merge rules via conflict processor
    conflictProcessor.checkConflicts(this, fromChangeSet, toChangeSet).forEach(conflicts::add);
    // handle domain-specific conflict filtering, like donated content, etc.
    // and add all reported conflicts to conflictsToReport
    conflictsToReport.addAll(conflictProcessor.filterConflicts(this, conflicts));
}
Also used : java.util(java.util) Query(com.b2international.index.query.Query) PostCommitHook(com.b2international.index.revision.Hooks.PostCommitHook) BiFunction(java.util.function.BiFunction) JsonDiff(com.b2international.index.util.JsonDiff) Mappings(com.b2international.index.mapping.Mappings) Pair(com.b2international.commons.Pair) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) JsonNode(com.fasterxml.jackson.databind.JsonNode) PreCommitHook(com.b2international.index.revision.Hooks.PreCommitHook) CompareUtils(com.b2international.commons.CompareUtils) com.google.common.collect(com.google.common.collect) IndexClientFactory(com.b2international.index.IndexClientFactory) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) BulkUpdate(com.b2international.index.BulkUpdate) Hook(com.b2international.index.revision.Hooks.Hook) JsonPatch(com.flipkart.zjsonpatch.JsonPatch) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) UUIDs(org.elasticsearch.common.UUIDs) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) DocumentMapping(com.b2international.index.mapping.DocumentMapping) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Stream(java.util.stream.Stream) Expressions(com.b2international.index.query.Expressions) IndexException(com.b2international.index.IndexException) JsonChange(com.b2international.index.util.JsonDiff.JsonChange) Entry(java.util.Map.Entry) Maps.newHashMapWithExpectedSize(com.google.common.collect.Maps.newHashMapWithExpectedSize) Preconditions(com.google.common.base.Preconditions) DocumentMapping(com.b2international.index.mapping.DocumentMapping) Maps.newHashMap(com.google.common.collect.Maps.newHashMap)

Example 4 with Mappings

use of com.b2international.index.mapping.Mappings in project snow-owl by b2ihealthcare.

the class ReservationImplTest method whenReservingRangeOfIDs_ThenItShouldConflictWithAllIDsInThatRangeIncludingBoundaries.

@Test
public void whenReservingRangeOfIDs_ThenItShouldConflictWithAllIDsInThatRangeIncludingBoundaries() throws Exception {
    final Index store = Indexes.createIndex(UUID.randomUUID().toString(), new ObjectMapper(), new Mappings(SctId.class));
    store.admin().create();
    final ISnomedIdentifierService identifierService = new DefaultSnomedIdentifierService(store, new ItemIdGenerationStrategy() {

        int counter = 200;

        @Override
        public Set<String> generateItemIds(String namespace, ComponentCategory category, int quantity, int attempt) {
            return IntStream.range(counter, counter + quantity).mapToObj(String::valueOf).collect(Collectors.toSet());
        }
    }, new SnomedIdentifierReservationServiceImpl(), new SnomedIdentifierConfiguration());
    final Set<ComponentCategory> components = Collections.singleton(ComponentCategory.CONCEPT);
    final Reservation range = Reservations.range(200, 300, "", components);
    final Set<String> componentIds = identifierService.generate(null, ComponentCategory.CONCEPT, 300 - 200 + 1);
    for (String id : componentIds) {
        final SnomedIdentifier identifier = SnomedIdentifiers.create(id);
        assertTrue(range.includes(identifier));
    }
    store.admin().delete();
}
Also used : ItemIdGenerationStrategy(com.b2international.snowowl.snomed.cis.gen.ItemIdGenerationStrategy) Set(java.util.Set) Index(com.b2international.index.Index) SnomedIdentifier(com.b2international.snowowl.snomed.cis.SnomedIdentifier) ComponentCategory(com.b2international.snowowl.core.terminology.ComponentCategory) SnomedIdentifierReservationServiceImpl(com.b2international.snowowl.snomed.cis.internal.reservations.SnomedIdentifierReservationServiceImpl) Reservation(com.b2international.snowowl.snomed.cis.reservations.Reservation) Mappings(com.b2international.index.mapping.Mappings) DefaultSnomedIdentifierService(com.b2international.snowowl.snomed.cis.memory.DefaultSnomedIdentifierService) ISnomedIdentifierService(com.b2international.snowowl.snomed.cis.ISnomedIdentifierService) SnomedIdentifierConfiguration(com.b2international.snowowl.snomed.cis.SnomedIdentifierConfiguration) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SctId(com.b2international.snowowl.snomed.cis.domain.SctId) Test(org.junit.Test)

Example 5 with Mappings

use of com.b2international.index.mapping.Mappings in project snow-owl by b2ihealthcare.

the class DefaultSnomedIdentifierServiceTest method init.

@Before
public void init() {
    store = Indexes.createIndex(UUID.randomUUID().toString(), new ObjectMapper(), new Mappings(SctId.class));
    store.admin().create();
    final ISnomedIdentifierReservationService reservationService = new SnomedIdentifierReservationServiceImpl();
    final ItemIdGenerationStrategy idGenerationStrategy = new SequentialItemIdGenerationStrategy(reservationService);
    service = new DefaultSnomedIdentifierService(store, idGenerationStrategy, reservationService, new SnomedIdentifierConfiguration());
}
Also used : SequentialItemIdGenerationStrategy(com.b2international.snowowl.snomed.cis.gen.SequentialItemIdGenerationStrategy) SnomedIdentifierReservationServiceImpl(com.b2international.snowowl.snomed.cis.internal.reservations.SnomedIdentifierReservationServiceImpl) SequentialItemIdGenerationStrategy(com.b2international.snowowl.snomed.cis.gen.SequentialItemIdGenerationStrategy) ItemIdGenerationStrategy(com.b2international.snowowl.snomed.cis.gen.ItemIdGenerationStrategy) Mappings(com.b2international.index.mapping.Mappings) ISnomedIdentifierReservationService(com.b2international.snowowl.snomed.cis.reservations.ISnomedIdentifierReservationService) DefaultSnomedIdentifierService(com.b2international.snowowl.snomed.cis.memory.DefaultSnomedIdentifierService) SnomedIdentifierConfiguration(com.b2international.snowowl.snomed.cis.SnomedIdentifierConfiguration) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Before(org.junit.Before)

Aggregations

Mappings (com.b2international.index.mapping.Mappings)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 Index (com.b2international.index.Index)13 Before (org.junit.Before)7 RepositoryConfiguration (com.b2international.snowowl.core.config.RepositoryConfiguration)5 ValidationRepository (com.b2international.snowowl.core.internal.validation.ValidationRepository)4 ValidationRule (com.b2international.snowowl.core.validation.rule.ValidationRule)4 ValidationIssue (com.b2international.snowowl.core.validation.issue.ValidationIssue)3 ValidationWhiteList (com.b2international.snowowl.core.validation.whitelist.ValidationWhiteList)3 ItemIdGenerationStrategy (com.b2international.snowowl.snomed.cis.gen.ItemIdGenerationStrategy)3 DefaultSnomedIdentifierService (com.b2international.snowowl.snomed.cis.memory.DefaultSnomedIdentifierService)3 DefaultRevisionIndex (com.b2international.index.revision.DefaultRevisionIndex)2 RevisionIndex (com.b2international.index.revision.RevisionIndex)2 ClassPathScanner (com.b2international.snowowl.core.plugin.ClassPathScanner)2 ValidationIssueDetailExtensionProvider (com.b2international.snowowl.core.validation.issue.ValidationIssueDetailExtensionProvider)2 SnomedIdentifierConfiguration (com.b2international.snowowl.snomed.cis.SnomedIdentifierConfiguration)2 SctId (com.b2international.snowowl.snomed.cis.domain.SctId)2 SequentialItemIdGenerationStrategy (com.b2international.snowowl.snomed.cis.gen.SequentialItemIdGenerationStrategy)2 SnomedIdentifierReservationServiceImpl (com.b2international.snowowl.snomed.cis.internal.reservations.SnomedIdentifierReservationServiceImpl)2 CompareUtils (com.b2international.commons.CompareUtils)1