Search in sources :

Example 1 with Iterables.partition

use of com.google.common.collect.Iterables.partition in project snowstorm by IHTSDO.

the class AdminOperationsService method reduceVersionsReplaced.

public Map<Class, AtomicLong> reduceVersionsReplaced(String branch) {
    // For all entries in the versionsReplaced map check if the document is from a child branch. If so remove from the set.
    Branch latest = branchService.findBranchOrThrow(branch);
    Map<Class, AtomicLong> reducedByType = new HashMap<>();
    Map<String, Set<String>> versionsReplaced = latest.getVersionsReplaced();
    final Map<Class<? extends DomainEntity>, ElasticsearchRepository> componentTypeRepoMap = domainEntityConfiguration.getAllTypeRepositoryMap();
    for (Class<? extends DomainEntity> type : componentTypeRepoMap.keySet()) {
        Set<String> toRemove = new HashSet<>();
        Set<String> versionsReplacedForType = versionsReplaced.getOrDefault(type.getSimpleName(), Collections.emptySet());
        for (List<String> versionsReplacedSegment : Iterables.partition(versionsReplacedForType, 1_000)) {
            try (final SearchHitsIterator<? extends DomainEntity> entitiesReplaced = elasticsearchTemplate.searchForStream(new NativeSearchQueryBuilder().withQuery(boolQuery().must(prefixQuery("path", branch + "/")).must(termsQuery("_id", versionsReplacedSegment))).withPageable(ConceptService.LARGE_PAGE).build(), type)) {
                entitiesReplaced.forEachRemaining(entity -> toRemove.add(entity.getId()));
            }
        }
        if (!toRemove.isEmpty()) {
            versionsReplacedForType.removeAll(toRemove);
            reducedByType.computeIfAbsent(type, (t) -> new AtomicLong(0)).addAndGet(toRemove.size());
        }
    }
    latest.setVersionsReplaced(versionsReplaced);
    branchRepository.save(latest);
    return reducedByType;
}
Also used : Iterables.partition(com.google.common.collect.Iterables.partition) Iterables(com.google.common.collect.Iterables) SortBuilders(org.elasticsearch.search.sort.SortBuilders) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Query(org.springframework.data.elasticsearch.core.query.Query) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) DescriptionHelper(org.snomed.snowstorm.core.util.DescriptionHelper) Lists(com.google.common.collect.Lists) IdentifierService(org.snomed.snowstorm.core.data.services.identifier.IdentifierService) Service(org.springframework.stereotype.Service) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Pair(org.springframework.data.util.Pair) ElasticsearchRepository(org.springframework.data.elasticsearch.repository.ElasticsearchRepository) Commit(io.kaicode.elasticvc.domain.Commit) BranchRepository(io.kaicode.elasticvc.repositories.BranchRepository) VersionControlHelper(io.kaicode.elasticvc.api.VersionControlHelper) DomainEntity(io.kaicode.elasticvc.domain.DomainEntity) Logger(org.slf4j.Logger) UpdateQuery(org.springframework.data.elasticsearch.core.query.UpdateQuery) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) LARGE_PAGE(io.kaicode.elasticvc.api.ComponentService.LARGE_PAGE) String.format(java.lang.String.format) AtomicLong(java.util.concurrent.atomic.AtomicLong) LongOpenHashSet(it.unimi.dsi.fastutil.longs.LongOpenHashSet) BranchCriteria(io.kaicode.elasticvc.api.BranchCriteria) ElasticsearchOperations(org.springframework.data.elasticsearch.core.ElasticsearchOperations) SearchHitsIterator(org.springframework.data.elasticsearch.core.SearchHitsIterator) RF2Constants(org.snomed.snowstorm.core.rf2.RF2Constants) Branch(io.kaicode.elasticvc.domain.Branch) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) BranchService(io.kaicode.elasticvc.api.BranchService) org.snomed.snowstorm.core.data.domain(org.snomed.snowstorm.core.data.domain) BufferedReader(java.io.BufferedReader) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) Long.parseLong(java.lang.Long.parseLong) PathUtil(io.kaicode.elasticvc.api.PathUtil) SearchLanguagesConfiguration(org.snomed.snowstorm.config.SearchLanguagesConfiguration) Document(org.springframework.data.elasticsearch.core.document.Document) InputStream(java.io.InputStream) LongOpenHashSet(it.unimi.dsi.fastutil.longs.LongOpenHashSet) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) AtomicLong(java.util.concurrent.atomic.AtomicLong) Branch(io.kaicode.elasticvc.domain.Branch) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) ElasticsearchRepository(org.springframework.data.elasticsearch.repository.ElasticsearchRepository) DomainEntity(io.kaicode.elasticvc.domain.DomainEntity) LongOpenHashSet(it.unimi.dsi.fastutil.longs.LongOpenHashSet)

Aggregations

Iterables (com.google.common.collect.Iterables)1 Iterables.partition (com.google.common.collect.Iterables.partition)1 Lists (com.google.common.collect.Lists)1 BranchCriteria (io.kaicode.elasticvc.api.BranchCriteria)1 BranchService (io.kaicode.elasticvc.api.BranchService)1 LARGE_PAGE (io.kaicode.elasticvc.api.ComponentService.LARGE_PAGE)1 PathUtil (io.kaicode.elasticvc.api.PathUtil)1 VersionControlHelper (io.kaicode.elasticvc.api.VersionControlHelper)1 Branch (io.kaicode.elasticvc.domain.Branch)1 Commit (io.kaicode.elasticvc.domain.Commit)1 DomainEntity (io.kaicode.elasticvc.domain.DomainEntity)1 BranchRepository (io.kaicode.elasticvc.repositories.BranchRepository)1 Long2ObjectOpenHashMap (it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap)1 LongOpenHashSet (it.unimi.dsi.fastutil.longs.LongOpenHashSet)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Long.parseLong (java.lang.Long.parseLong)1 String.format (java.lang.String.format)1