@Override public boolean test(final Repository input) { return input.optionalFacet(type).isPresent(); }
@Override protected boolean appliesTo(final Repository repository) { return repository.optionalFacet(SearchFacet.class).isPresent(); }
@Override protected boolean appliesTo(final Repository repository) { return repository.optionalFacet(PurgeUnusedFacet.class).isPresent(); }
@Override protected boolean appliesTo(final Repository repository) { return repository.optionalFacet(NpmSearchFacet.class).isPresent(); }
/** * Lookup an {@link Optional} facet on attached repository. * * Reduce some verbosity for commonly used repository operation. * * @see Repository#optionalFacet(Class) */ @Nonnull protected <T extends Facet> Optional<T> optionalFacet(final Class<T> type) { return getRepository().optionalFacet(type); }
private static List<Repository> allMembers(final List<Repository> members, final Repository root) { members.add(root); List<Repository> groupMembers = root.optionalFacet(GroupFacet.class).map(GroupFacet::members) .orElseGet(Collections::emptyList); for (Repository child : groupMembers) { allMembers(members, child); } return members; }
@Override public void afterMove(final List<Map<String, String>> components, final Repository destination) { destination.optionalFacet(MavenHostedFacet.class).ifPresent(f -> components.stream() .map(component -> component.get("group")) .distinct() .forEach(group -> f.rebuildMetadata(group, null, null, false)) ); } }
private void removeRepositoryFromAllGroups(final Repository repositoryToRemove) throws Exception { for (Repository group : repositories.values()) { Optional<GroupFacet> groupFacet = group.optionalFacet(GroupFacet.class); if (groupFacet.isPresent() && groupFacet.get().member(repositoryToRemove)) { removeRepositoryFromGroup(repositoryToRemove, group); } } }
private static boolean repoOnlineAndHasSearchFacet(final Repository repo) { return repo.optionalFacet(SearchFacet.class).isPresent() && repo.getConfiguration().isOnline(); }
@VisibleForTesting Map<String, List<String>> getRepoToContainedGroupMap(List<Repository> repositories) { Map<String, List<String>> repoToContainedGroupMap = new HashMap<>(); for (Repository repository : repositories) { List<String> groupNames = new ArrayList<>(); groupNames.add(repository.getName()); groupNames.addAll(repositories.stream().filter(groupRepository -> { Optional<GroupFacet> groupFacet = groupRepository.optionalFacet(GroupFacet.class); return groupFacet.isPresent() && groupFacet.get().leafMembers().stream() .anyMatch(leafMember -> repository.getName().equals(leafMember.getName())); }).map(groupRepository -> groupRepository.getName()).collect(Collectors.toSet())); repoToContainedGroupMap.put(repository.getName(), groupNames); } return repoToContainedGroupMap; }
private void findContainingGroups(final String name, final SortedMap<Integer, List<String>> groupNamesByLevel, final int level) { final List<String> newContainingGroups = new ArrayList<>(); //find any groups that directly contain the desired repository name (and make sure to only include each name //once to save processing time) repositories.values().stream().filter(repository -> repository.optionalFacet(GroupFacet.class).filter(groupFacet -> groupFacet.member(name)).isPresent() && groupNamesByLevel.values().stream().noneMatch(repoNames -> repoNames.contains(repository.getName()))) .forEach(repository -> newContainingGroups.add(repository.getName())); List<String> groupNames = groupNamesByLevel.computeIfAbsent(level, newLevel -> new ArrayList<>()); groupNames.addAll(newContainingGroups); //now process each group we found and check if any groups contain it newContainingGroups.forEach( newName -> findContainingGroups(newName, groupNamesByLevel, level + 1)); }
@Override protected boolean canAttemptRestore(final PyPiRestoreBlobData data) { Repository repository = data.getBlobData().getRepository(); if (!repository.optionalFacet(PyPiFacet.class).isPresent()) { log.warn("Skipping as PyPI Facet not found on repository: {}", repository.getName()); return false; } return true; }
@Override protected boolean canAttemptRestore(final RawRestoreBlobData data) { Repository repository = data.getBlobData().getRepository(); if (!repository.optionalFacet(RawContentFacet.class).isPresent()) { log.warn("Skipping as Raw Facet not found on repository: {}", repository.getName()); return false; } return true; }
@Override public void contribute(final BoolQueryBuilder query, final String type, final String value) { if (value == null) { return; } Repository repository = repositoryManager.get(value); if (repository != null) { Optional<GroupFacet> groupFacet = repository.optionalFacet(GroupFacet.class); if (groupFacet.isPresent()) { List<Repository> members = groupFacet.get().leafMembers(); query.must(QueryBuilders.termsQuery(type, members.stream().map(Repository::getName).toArray(String[]::new))); return; } } super.contribute(query, type, value); } }
@Override protected boolean appliesTo(final Repository repository) { return maven2Format.equals(repository.getFormat()) && repository.optionalFacet(RemoveSnapshotsFacet.class).isPresent() && repository.facet(MavenFacet.class).getVersionPolicy() != VersionPolicy.RELEASE; }
private Asset getAssetById(final Repository repository, final EntityId assetId) { Optional<GroupFacet> optionalGroupFacet = repository.optionalFacet(GroupFacet.class); List<Repository> members = optionalGroupFacet.isPresent() ? optionalGroupFacet.get().allMembers() : Collections.singletonList(repository); return Transactional.operation.withDb(repository.facet(StorageFacet.class).txSupplier()).call(() -> { StorageTx tx = UnitOfWork.currentTx(); Asset candidate = tx.findAsset(assetId); if (candidate != null) { final String asssetBucketRepositoryName = bucketStore.getById(candidate.bucketId()).getRepositoryName(); if (members.stream().anyMatch(repo -> repo.getName().equals(asssetBucketRepositoryName))) { return candidate; } } return null; }); }
@Override public void restore(final Properties properties, final Blob blob, final String blobStoreName, final boolean isDryRun) { RestoreBlobData blobData = new RestoreBlobData(blob, properties, blobStoreName, repositoryManager); Optional<StorageFacet> storageFacet = blobData.getRepository().optionalFacet(StorageFacet.class); T restoreData = createRestoreData(blobData); if (storageFacet.isPresent() && canAttemptRestore(restoreData)) { doRestore(storageFacet.get(), blobData, restoreData, isDryRun); } else { log.debug("Skipping asset, blob store: {}, repository: {}, blob name: {}, blob id: {}", blobStoreName, blobData.getRepository().getName(), blobData.getBlobName(), blob.getId()); } }
private void doUpdateSearchIndex(final Repository repository, final IndexRequest indexRequest) { repository.optionalFacet(SearchFacet.class).ifPresent(searchFacet -> { UnitOfWork.begin(repository.facet(StorageFacet.class).txSupplier()); try { if (bulkProcessing) { indexRequest.bulkApply(searchFacet); } else { indexRequest.apply(searchFacet); } } finally { UnitOfWork.end(); } }); } }
if (!repository.optionalFacet(ProxyFacet.class).isPresent()) { log.info("Updating metadata on repository '{}'", repositoryName); ProgressLogIntervalHelper intervalLogger = new ProgressLogIntervalHelper(log, 60); Optional<MavenHostedFacet> mavenHostedFacet = repository.optionalFacet(MavenHostedFacet.class); if (mavenHostedFacet.isPresent()) { mavenHostedFacet.get().deleteMetadata(gav.group, gav.name, gav.baseVersion);
@Override @Transactional public Component afterMove(final Component component, final Repository destination) { destination.optionalFacet(NpmHostedFacet.class).ifPresent(f -> { final StorageTx tx = UnitOfWork.currentTx(); tx.browseAssets(component).forEach(asset -> { Blob blob = checkNotNull(tx.getBlob(asset.blobRef())); final Map<String, Object> packageJson = npmPackageParser.parsePackageJson(blob::getInputStream); final NpmPackageId packageId = NpmPackageId.parse((String) packageJson.get(P_NAME)); try { final NestedAttributesMap updatedMetadata = createFullPackageMetadata( new NestedAttributesMap("metadata", packageJson), destination.getName(), blob.getMetrics().getSha1Hash(), destination, extractNewestVersion); f.putPackageRoot(packageId, null, updatedMetadata); } catch (IOException e) { log.error("Failed to update package root, packageId: {}", packageId, e); } }); }); return component; } }