class PurgeUnusedSnapshotsFacetImpl extends FacetSupport implements PurgeUnusedSnapshotsFacet {

  @TransactionalDeleteBlob
  protected void deleteUnusedSnapshotComponents(LocalDate olderThan) {
    StorageTx tx = UnitOfWork.currentTx();

    // entire component count is used just for reporting progress
    Repository repo = getRepository();
    long totalComponents = tx.countComponents(builder().build(), singletonList(repo));
    log.info("Found {} total components in repository {} to evaluate for unused snapshots", totalComponents,
        repo.getName());

    ORID bucketId = id(tx.findBucket(getRepository()));
    String unusedWhereTemplate = getUnusedWhere(bucketId);

    long skipCount = 0;
    lastComponent = new ORecordId(-1, -1);
    while (skipCount < totalComponents && !isCanceled()) {
      log.info(PROGRESS,
          format("Processing components [%.2f%%] complete", ((double) skipCount / totalComponents) * 100));

      for (Component component : findNextPageOfUnusedSnapshots(tx, olderThan, bucketId, unusedWhereTemplate)) {
        if (isCanceled()) {
          return;
        }

        deleteComponent(component);
      }

      // commit each loop as well
      tx.commit();
      tx.begin();

      skipCount += findUnusedLimit;
    }
  }

  @Inject public  PurgeUnusedSnapshotsFacetImpl(
      final ComponentEntityAdapter componentEntityAdapter,
      @Named(GroupType.NAME) final Type groupType,
      @Named(HostedType.NAME) final Type hostedType,
      @Named("${nexus.tasks.purgeUnusedSnapshots.findUnusedLimit:-50}") int findUnusedLimit);

}