Java Code Examples for org.sonatype.nexus.repository.storage.StorageTx#deleteComponent()

The following examples show how to use org.sonatype.nexus.repository.storage.StorageTx#deleteComponent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HelmComponentMaintenanceFacet.java    From nexus-repository-helm with Eclipse Public License 1.0 6 votes vote down vote up
@Transactional(retryOn = ONeedRetryException.class)
@Override
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlobs) {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = tx.findAsset(assetId, bucket);

  if (asset == null) {
    return Collections.emptySet();
  }

  tx.deleteAsset(asset, deleteBlobs);

  if (asset.componentId() != null) {
    Component component = tx.findComponentInBucket(asset.componentId(), bucket);

    if (!tx.browseAssets(component).iterator().hasNext()) {
      log.debug("Deleting component: {}", component);
      tx.deleteComponent(component, deleteBlobs);
    }
  }
  return Collections.singleton(asset.name());
}
 
Example 2
Source File: CondaComponentMaintenanceFacet.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Transactional(retryOn = ONeedRetryException.class)
@Override
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlobs) {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = tx.findAsset(assetId, bucket);

  if (asset == null) {
    return emptySet();
  }

  tx.deleteAsset(asset, deleteBlobs);

  if (asset.componentId() != null) {
    Component component = tx.findComponentInBucket(asset.componentId(), bucket);

    if (!tx.browseAssets(component).iterator().hasNext()) {
      log.debug("Deleting component: {}", component);
      tx.deleteComponent(component, deleteBlobs);
    }
  }
  return Collections.singleton(asset.name());
}
 
Example 3
Source File: OrientNpmHostedFacet.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
@TransactionalDeleteBlob
public Set<String> deleteTarball(final NpmPackageId packageId, final String tarballName, final boolean deleteBlob) {
  checkNotNull(packageId);
  checkNotNull(tarballName);
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  Asset tarballAsset = NpmFacetUtils.findTarballAsset(tx, bucket, packageId, tarballName);
  if (tarballAsset == null) {
    return Collections.emptySet();
  }
  Component tarballComponent = tx.findComponentInBucket(tarballAsset.componentId(), bucket);
  if (tarballComponent == null) {
    return Collections.emptySet();
  }
  return tx.deleteComponent(tarballComponent, deleteBlob);
}
 
Example 4
Source File: OrientPyPiComponentMaintenance.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Deletes the root AND package index if a component has been deleted
 */
@TransactionalDeleteBlob
@Override
protected DeletionResult deleteComponentTx(final EntityId componentId, final boolean deleteBlobs) {
  StorageTx tx = UnitOfWork.currentTx();
  Component component = tx.findComponentInBucket(componentId, tx.findBucket(getRepository()));
  if (component == null) {
    return new DeletionResult(null, Collections.emptySet());
  }

  deleteRootIndex();

  deleteCachedIndex(component.name());

  log.debug("Deleting component: {}", component.toStringExternal());
  return new DeletionResult(component, tx.deleteComponent(component, deleteBlobs));
}
 
Example 5
Source File: AptSnapshotFacetSupport.java    From nexus-repository-apt with Eclipse Public License 1.0 5 votes vote down vote up
@Transactional(retryOn = { ONeedRetryException.class })
@Override
public void deleteSnapshot(String id) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Component component = tx.findComponentWithProperty(P_NAME, id, bucket);
  if (component == null) {
    return;
  }
  tx.deleteComponent(component);
}
 
Example 6
Source File: RawContentFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@TransactionalDeleteBlob
public boolean delete(final String path) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();

  final Component component = findComponent(tx, tx.findBucket(getRepository()), path);
  if (component == null) {
    return false;
  }

  tx.deleteComponent(component);
  return true;
}
 
Example 7
Source File: PurgeUnusedSnapshotsFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void deleteComponent(final Component component) {
  log.debug("Deleting unused snapshot component {}", component);
  final StorageTx tx = UnitOfWork.currentTx();
  tx.deleteComponent(component);

  NestedAttributesMap attributes = component.formatAttributes();
  String groupId = attributes.get(P_GROUP_ID, String.class);
  String artifactId = attributes.get(P_ARTIFACT_ID, String.class);
  String baseVersion = attributes.get(P_BASE_VERSION, String.class);

  try {
    Bucket bucket = tx.findBucket(getRepository());

    getRepository().facet(MavenFacet.class).maybeDeleteOrFlagToRebuildMetadata(bucket, metadataPath(groupId, artifactId, baseVersion));

    //for GA metadata, if there are no other GAVs matching the GA, delete it, otherwise mark as invalid so it will
    //be rebuilt on request
    getRepository().facet(MavenFacet.class).maybeDeleteOrFlagToRebuildMetadata(bucket, metadataPath(groupId, artifactId, null));

    //for G metadata, if there are no other GAs matching the G, delete it, otherwise mark as invalid so it will
    //be rebuilt on request
    getRepository().facet(MavenFacet.class).maybeDeleteOrFlagToRebuildMetadata(bucket, metadataPath(groupId, null, null));
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 8
Source File: MavenFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private boolean deleteArtifact(final MavenPath path, final StorageTx tx) {
  final Component component = findComponent(tx, getRepository(), path);
  if (component == null) {
    return false;
  }
  final Asset asset = findAsset(tx, tx.findBucket(getRepository()), path);
  if (asset == null) {
    return false;
  }
  tx.deleteAsset(asset);
  if (!tx.browseAssets(component).iterator().hasNext()) {
    tx.deleteComponent(component);
  }
  return true;
}
 
Example 9
Source File: AptHostedComponentMaintenanceFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Transactional(retryOn = ONeedRetryException.class)
@Override
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlobs) {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = tx.findAsset(assetId, bucket);

  if (asset == null) {
    return Collections.emptySet();
  }

  String assetKind = asset.formatAttributes().get(P_ASSET_KIND, String.class);
  Set<String> result = super.deleteAssetTx(assetId, deleteBlobs);
  if ("DEB".equals(assetKind)) {
    try {
      getRepository().facet(AptHostedFacet.class)
          .rebuildIndexes(Collections.singletonList(new AptHostedFacet.AssetChange(AssetAction.REMOVED, asset)));
    }
    catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  if (asset.componentId() != null) {
    Component component = tx.findComponentInBucket(asset.componentId(), bucket);

    if (!tx.browseAssets(component).iterator().hasNext()) {
      log.debug("Deleting component: {}", component);
      tx.deleteComponent(component, deleteBlobs);
    }
  }

  return result;
}
 
Example 10
Source File: AptHostedComponentMaintenanceFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalDeleteBlob
@Override
protected DeletionResult deleteComponentTx(final EntityId componentId, final boolean deleteBlobs) {
  StorageTx tx = UnitOfWork.currentTx();
  Component component = tx.findComponentInBucket(componentId, tx.findBucket(getRepository()));

  if (component == null) {
    return new DeletionResult(null, Collections.emptySet());
  }

  Iterable<Asset> assets = tx.browseAssets(component);
  List<AssetChange> changes = new ArrayList<>();
  for (Asset asset : assets) {
    changes.add(new AssetChange(AssetAction.REMOVED, asset));
  }

  log.debug("Deleting component: {}", component.toStringExternal());
  DeletionResult result = new DeletionResult(component, tx.deleteComponent(component, deleteBlobs));
  try {
    getRepository().facet(AptHostedFacet.class).rebuildIndexes(changes);
  }
  catch (IOException e) {
    throw new UncheckedIOException(e);
  }

  return result;
}
 
Example 11
Source File: PurgeUnusedFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Delete all unused components.
 */
@TransactionalDeleteBlob
protected void deleteUnusedComponents(final Date olderThan) {
  StorageTx tx = UnitOfWork.currentTx();

  for (Component component : findUnusedComponents(tx, olderThan)) {
    checkCancellation();
    log.debug("Deleting unused component {}", component);
    tx.deleteComponent(component); // TODO: commit in batches
  }
}
 
Example 12
Source File: RemoveSnapshotsFacetImpl.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Examine all snapshots in the given repo, delete those that match our configuration criteria and flag which GAVs
 * require a metadata update.
 */
@TransactionalDeleteBlob
protected Collection<GAV> processRepository(final Repository repository, final RemoveSnapshotsConfig config) {
  StorageTx tx = UnitOfWork.currentTx();

  log.info("Begin processing snapshots in repository '{}'", repository.getName());

  Set<GAV> snapshotCandidates = Sets.newHashSet(findSnapshotCandidates(tx, repository));
  log.info("Found {} snapshot GAVs to analyze", snapshotCandidates.size());

  // only interested in the ones where we actually delete something, otherwise we would needlessly regenerate metadata
  Set<GAV> gavsWithDeletions = new HashSet<>();

  ProgressLogIntervalHelper intervalLogger = new ProgressLogIntervalHelper(log, 60);
  long deleted = 0;
  long processed = 0;
  for (GAV snapshotCandidate : snapshotCandidates) {
    log.debug("Processing GAV = {}", snapshotCandidate);
    Set<Component> components = Sets.newHashSet(findComponentsForGav(tx, repository, snapshotCandidate));

    if (components.isEmpty()) {
      continue;
    }

    Set<Component> toDelete = getSnapshotsToDelete(config, components);

    if (!toDelete.isEmpty()) {
      log.debug("Found {} components to remove for GAV = {}", toDelete.size(), snapshotCandidate);

      gavsWithDeletions.add(snapshotCandidate);
      for (Component component : toDelete) {
        log.debug("Deleting component: {}", component);
        tx.deleteComponent(component);

        if (maybeCommit(tx, ++deleted)) {
          intervalLogger.info("Elapsed time: {}, GAVs processed: {}, snapshots deleted: {}",
              intervalLogger.getElapsed(), processed, deleted);
        }
      }
    }

    processed++;
  }
  log.debug("Committing final batch delete");
  tx.commit();
  tx.begin();
  intervalLogger.flush();

  DateTime olderThan = DateTime.now().minusDays(Math.max(config.getSnapshotRetentionDays(), 0));
  log.info("Elapsed time: {}, deleted {} components from {} distinct GAVs", intervalLogger.getElapsed(), deleted,
      gavsWithDeletions.size());
  log.info("Finished processing snapshots with more than {} versions created before {}", config.getMinimumRetained(),
      olderThan);
  return gavsWithDeletions;
}