org.sonatype.nexus.repository.storage.Bucket Java Examples

The following examples show how to use org.sonatype.nexus.repository.storage.Bucket. 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: OrientPyPiHostedFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Asset createIndexAsset(final String name,
                               final StorageTx tx,
                               final String indexPath,
                               final Bucket bucket) throws IOException
{
  String html = buildIndex(name, tx);

  Asset savedIndex = tx.createAsset(bucket, getRepository().getFormat());
  savedIndex.name(indexPath);
  savedIndex.formatAttributes().set(P_ASSET_KIND, AssetKind.INDEX.name());

  StorageFacet storageFacet = getRepository().facet(StorageFacet.class);
  TempBlob tempBlob = storageFacet.createTempBlob(new ByteArrayInputStream(html.getBytes(UTF_8)), HASH_ALGORITHMS);

  saveAsset(tx, savedIndex, tempBlob, TEXT_HTML, new AttributesMap());

  return savedIndex;
}
 
Example #2
Source File: DefaultIntegrityCheckStrategyTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private void runTest(final String assetName,
                     final HashCode assetHash,
                     final String blobName,
                     final HashCode blobHash,
                     final Supplier<Boolean> cancel,
                     final String blobId,
                     final Blob mockBlob)
{
  Asset asset = getMockAsset(assetName, assetHash);
  BlobAttributes blobAttributes = getMockBlobAttribues(blobName, blobHash.toString());
  when(storageTx.browseAssets(any(Bucket.class))).thenReturn(newHashSet(asset));
  when(blobStore.getBlobAttributes(any())).thenReturn(blobAttributes);
  when(blobStore.get(new BlobId(blobId))).thenReturn(mockBlob);

  defaultIntegrityCheckStrategy.check(repository, blobStore, cancel, CHECK_FAILED_HANDLER);

  verify(logger).info(startsWith("Checking integrity of assets"), anyString(), anyString());

  // if cancel is invoked, we'll never see the debug line
  if (!cancel.get()) {
    verify(logger).debug(startsWith("checking asset {}"), any(Asset.class));
  }
}
 
Example #3
Source File: OrientPyPiIndexFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalDeleteBlob
public void deleteIndex(final String packageName)
{
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  String indexPath = PyPiPathUtils.indexPath(normalizeName(packageName));
  Asset cachedIndex = findAsset(tx, bucket, indexPath);
  
  /*
    There is a chance that the index wasn't found because of package name normalization. For example '.' 
    characters are normalized to '-' so jts.python would have an index at /simple/jts-python/. It is possible that 
    we could just check for the normalized name but we check for both just in case. Searching for an index with a
    normalized name first means that most, if not all, index deletions will only perform a single search.
    
    See https://issues.sonatype.org/browse/NEXUS-19303 for additional context. 
   */
  if (cachedIndex == null) {
    indexPath = PyPiPathUtils.indexPath(packageName);
    cachedIndex = findAsset(tx, bucket, indexPath);
  }
  
  if (cachedIndex != null) {
    tx.deleteAsset(cachedIndex);
  }
}
 
Example #4
Source File: HelmFacetImpl.java    From nexus-repository-helm with Eclipse Public License 1.0 6 votes vote down vote up
private Asset createAsset(
    final StorageTx tx,
    final String assetPath,
    final HelmAttributes helmAttributes,
    final AssetKind assetKind)
{
  checkNotNull(assetKind);

  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = CacheControllerHolder.METADATA.equals(assetKind.getCacheType())
      ? tx.createAsset(bucket, getRepository().getFormat())
      : tx.createAsset(bucket, findOrCreateComponent(tx, bucket, helmAttributes.getName(), helmAttributes.getVersion()));
  asset.formatAttributes().set(P_ASSET_KIND, assetKind.name());
  helmAttributes.populate(asset.formatAttributes());
  asset.name(assetPath);
  tx.saveAsset(asset);
  return asset;
}
 
Example #5
Source File: NpmFacetUtils.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the tarball content.
 */
@Nullable
static Content getTarballContent(final StorageTx tx,
                                 final Bucket bucket,
                                 final NpmPackageId packageId,
                                 final String tarballName)
{
  Asset asset = findTarballAsset(tx, bucket, packageId, tarballName);
  if (asset == null) {
    return null;
  }

  Blob blob = tx.requireBlob(asset.requireBlobRef());
  Content content = new Content(new BlobPayload(blob, asset.requireContentType()));
  Content.extractFromAsset(asset, HASH_ALGORITHMS, content.getAttributes());
  return content;
}
 
Example #6
Source File: NpmFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Nullable
@Override
public Asset putRepositoryRoot(final AssetBlob assetBlob, @Nullable final AttributesMap contentAttributes)
    throws IOException
{
  final Repository repository = getRepository();
  final StorageTx tx = UnitOfWork.currentTx();
  final Bucket bucket = tx.findBucket(repository);
  Asset asset = NpmFacetUtils.findRepositoryRootAsset(tx, bucket);

  if (asset == null) {
    asset = tx.createAsset(bucket, repository.getFormat()).name(REPOSITORY_ROOT_ASSET);
    getEventManager().post(new NpmSearchIndexInvalidatedEvent(repository));
    saveAsset(tx, asset, assetBlob, REPOSITORY_ROOT, contentAttributes);
  }

  return asset;
}
 
Example #7
Source File: P2ProxyFacetImpl.java    From nexus-repository-p2 with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalStoreBlob
protected Content saveMetadataAsAsset(final String assetPath,
                                      final TempBlob metadataContent,
                                      final Payload payload,
                                      final AssetKind assetKind) throws IOException
{
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  Asset asset = facet(P2Facet.class).findAsset(tx, bucket, assetPath);
  if (asset == null) {
    asset = tx.createAsset(bucket, getRepository().getFormat());
    asset.name(assetPath);
    asset.formatAttributes().set(P_ASSET_KIND, assetKind.name());
  }

  return facet(P2Facet.class).saveAsset(tx, asset, metadataContent, payload);
}
 
Example #8
Source File: NpmFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Nullable
@Override
public Asset putPackageRoot(final String packageId,
                            final AssetBlob assetBlob,
                            @Nullable final AttributesMap contentAttributes)
    throws IOException
{
  final Repository repository = getRepository();
  final StorageTx tx = UnitOfWork.currentTx();
  final Bucket bucket = tx.findBucket(repository);
  Asset asset = NpmFacetUtils.findPackageRootAsset(tx, bucket, NpmPackageId.parse(packageId));

  if (asset == null) {
    asset = tx.createAsset(bucket, repository.getFormat()).name(packageId);
    saveAsset(tx, asset, assetBlob, PACKAGE_ROOT, contentAttributes);
  }

  return asset;
}
 
Example #9
Source File: P2FacetImpl.java    From nexus-repository-p2 with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Asset findOrCreateAsset(final StorageTx tx,
                               final Component component,
                               final String path,
                               final P2Attributes attributes)
{
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = findAsset(tx, bucket, path);
  if (asset == null) {
    asset = tx.createAsset(bucket, component);
    asset.name(path);

    asset.formatAttributes().set(PLUGIN_NAME, attributes.getPluginName());
    asset.formatAttributes().set(P_ASSET_KIND, attributes.getAssetKind());
    tx.saveAsset(asset);
  }

  return asset;
}
 
Example #10
Source File: StorageFacetManagerImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
@Guarded(by = STARTED)
public long performDeletions() {
  List<Bucket> buckets = findBucketsForDeletion();
  return buckets.stream().filter((bucket) -> {
    try {
      log.info("Deleting bucket for repository {}", bucket.getRepositoryName());
      deleteBucket(bucket);
      return true;
    }
    catch (Exception e) {
      log.warn("Unable to delete bucket with repository name {}, will require manual cleanup",
          bucket.getRepositoryName(), e);
      return false;
    }
  }).count();
}
 
Example #11
Source File: MavenProxyFacet.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
@TransactionalTouchMetadata
protected void indicateVerified(final Context context, final Content content, final CacheInfo cacheInfo)
    throws IOException
{
  final StorageTx tx = UnitOfWork.currentTx();
  final Bucket bucket = tx.findBucket(getRepository());
  final MavenPath path = mavenPath(context);

  // by EntityId
  Asset asset = Content.findAsset(tx, bucket, content);
  if (asset == null) {
    // by format coordinates
    asset = findAsset(tx, bucket, path);
  }
  if (asset == null) {
    log.debug("Attempting to set cache info for non-existent maven asset {}", path.getPath());
    return;
  }

  log.debug("Updating cacheInfo of {} to {}", path.getPath(), cacheInfo);
  CacheInfo.applyToAsset(asset, cacheInfo);
  tx.saveAsset(asset);
}
 
Example #12
Source File: GolangDataAccess.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Save an asset and create blob.
 *
 * @return blob content
 */
@TransactionalStoreBlob
public Content maybeCreateAndSaveAsset(final Repository repository,
                                       final String assetPath,
                                       final AssetKind assetKind,
                                       final TempBlob tempBlob,
                                       final Payload payload) throws IOException
{
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(repository);

  Asset asset = findAsset(tx, bucket, assetPath);
  if (asset == null) {
    asset = tx.createAsset(bucket, repository.getFormat());
    asset.name(assetPath);
    asset.formatAttributes().set(P_ASSET_KIND, assetKind.name());
  }
  return saveAsset(tx, asset, tempBlob, payload);
}
 
Example #13
Source File: ComponentDirectorTest.java    From nexus-repository-helm with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void allowMoveTest() {
  HelmComponentDirector director = new HelmComponentDirector(bucketStore, repositoryManager);
  assertTrue(director.allowMoveTo(destination));
  assertTrue(director.allowMoveFrom(source));

  EntityId bucketId = mock(EntityId.class);
  when(component.bucketId()).thenReturn(bucketId);
  Bucket bucket = mock(Bucket.class);
  when(bucketStore.getById(bucketId)).thenReturn(bucket);
  when(bucket.getRepositoryName()).thenReturn("repo");
  when(repositoryManager.get("repo")).thenReturn(source);

  assertTrue(director.allowMoveTo(component, destination));

  assertTrue(director.allowMoveTo(destination));
}
 
Example #14
Source File: RFacetImpl.java    From nexus-repository-r with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Asset findOrCreateAsset(final StorageTx tx,
                               final Component component,
                               final String path,
                               final Map<String, String> attributes)
{
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = findAsset(tx, bucket, path);
  if (asset == null) {
    asset = tx.createAsset(bucket, component);
    asset.name(path);

    // TODO: Make this a bit more robust (could be problematic if keys are removed in later versions, or if keys clash)
    for (Entry<String, String> attribute : attributes.entrySet()) {
      asset.formatAttributes().set(attribute.getKey(), attribute.getValue());
    }
    asset.formatAttributes().set(P_ASSET_KIND, getAssetKind(path).name());
    tx.saveAsset(asset);
  }

  return asset;
}
 
Example #15
Source File: StorageFacetManagerImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
@Guarded(by = STARTED)
public void enqueueDeletion(final Repository repository, final BlobStore blobStore, final Bucket bucket)
{
  checkNotNull(repository);
  checkNotNull(blobStore);
  checkNotNull(bucket);

  // The bucket associated with repository needs a new "repository name" created for it in order to avoid clashes.
  // Consider what happens if the bucket is still being deleted and someone tries to reuse that repository name.
  String generatedRepositoryName = repository.getName() + '$' + UUID.randomUUID();
  bucket.setRepositoryName(generatedRepositoryName);
  bucket.attributes().set(P_PENDING_DELETION, true);

  updateBucket(bucket);
}
 
Example #16
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 #17
Source File: RepositoryFacetTestSupport.java    From nexus-repository-r with Eclipse Public License 1.0 6 votes vote down vote up
@Before
public void initialiseFacetMocksAndSetupTransaction() throws Exception {
  assets = new ArrayList<>();
  UnitOfWork.beginBatch(storageTx);
  when(storageTx.browseAssets(any(Bucket.class))).thenReturn(assets);
  when(storageTx.browseAssets(any(), any(Bucket.class))).thenReturn(assets);
  when(storageTx.findAssetWithProperty(anyString(), anyString(), any(Bucket.class))).thenReturn(asset);
  when(storageTx.findBucket(repository)).thenReturn(bucket);
  when(storageTx.requireBlob(any())).thenReturn(blob);
  when(repository.facet(StorageFacet.class)).thenReturn(storageFacet);
  when(asset.formatAttributes()).thenReturn(formatAttributes);
  when(asset.attributes()).thenReturn(attributes);
  when(attributes.get("last_modified", Date.class)).thenReturn(new Date());
  when(attributes.get("last_verified", Date.class)).thenReturn(new Date());
  when(attributes.get("cache_token", String.class)).thenReturn("test");
  when(attributes.child("content")).thenReturn(attributes);
  when(attributes.child("cache")).thenReturn(attributes);
  underTest = initialiseSystemUnderTest();
  underTest.attach(repository);
}
 
Example #18
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 #19
Source File: RawContentFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
@TransactionalTouchMetadata
public void setCacheInfo(final String path, final Content content, final CacheInfo cacheInfo) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  // by EntityId
  Asset asset = Content.findAsset(tx, bucket, content);
  if (asset == null) {
    // by format coordinates
    Component component = tx.findComponentWithProperty(P_NAME, path, bucket);
    if (component != null) {
      asset = tx.firstAsset(component);
    }
  }
  if (asset == null) {
    log.debug("Attempting to set cache info for non-existent raw component {}", path);
    return;
  }

  log.debug("Updating cacheInfo of {} to {}", path, cacheInfo);
  CacheInfo.applyToAsset(asset, cacheInfo);
  tx.saveAsset(asset);
}
 
Example #20
Source File: RemoveSnapshotsFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Find all GAVs that qualify for deletion.
 */
@VisibleForTesting
Set<GAV> findSnapshotCandidates(final StorageTx tx, final Repository repository)
{
  log.info(PROGRESS, "Searching for GAVS with snapshots that qualify for deletion on repository '{}'",
      repository.getName());

  final Bucket bucket = tx.findBucket(repository);
  final OResultSet<ODocument> result = tx.getDb().command(new OSQLSynchQuery<>(GAVS_WITH_SNAPSHOTS))
      .execute(AttachedEntityHelper.id(bucket));
  return result.stream().map((doc) -> {
    String group = doc.field(P_GROUP, String.class);
    String name = doc.field(P_NAME, String.class);
    String baseVersion = doc.field("baseVersion", String.class);
    Integer count = doc.field("cnt", Integer.class);
    return new GAV(group, name, baseVersion, count);
  }).collect(Collectors.toSet());
}
 
Example #21
Source File: CocoapodsFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Component findOrCreateComponent(final StorageTx tx, final Bucket bucket, final String path) {
  PodInfo podInfo = podPathParser.parse(path);

  Iterable<Component> components = tx.findComponents(
      builder()
          .where(P_NAME).eq(podInfo.getName())
          .and(P_VERSION).eq(podInfo.getVersion())
          .build(),
      singletonList(getRepository())
  );

  Component component = Iterables.getFirst(components, null); // NOSONAR
  if (component == null) {
    component = tx.createComponent(bucket, getRepository().getFormat())
        .name(podInfo.getName())
        .version(podInfo.getVersion());
    tx.saveComponent(component);
  }
  return component;
}
 
Example #22
Source File: AptSnapshotFacetSupport.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Transactional(retryOn = {ONeedRetryException.class})
protected void createSnapshot(final String id, final Iterable<SnapshotItem> snapshots) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();
  StorageFacet storageFacet = facet(StorageFacet.class);
  Bucket bucket = tx.findBucket(getRepository());
  for (SnapshotItem item : snapshots) {
    String assetName = createAssetPath(id, item.specifier.path);
    Asset asset = tx.createAsset(bucket, getRepository().getFormat()).name(assetName);
    try (final TempBlob streamSupplier = storageFacet
        .createTempBlob(item.content.openInputStream(), FacetHelper.hashAlgorithms)) {
      AssetBlob blob = tx.createBlob(item.specifier.path, streamSupplier, FacetHelper.hashAlgorithms, null,
          item.specifier.role.getMimeType(), true);
      tx.attachBlob(asset, blob);
    }
    finally {
      item.content.close();
    }
    tx.saveAsset(asset);
  }
}
 
Example #23
Source File: MavenFacetImplTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testGet_expiredMetadata() throws Exception {
  String path = "/org/sonatype/nexus/nexus-base/3.19.0-SNAPSHOT/maven-metadata.xml";
  Asset asset = createMetadataAsset(path, INVALIDATED);
  Blob blob = mock(Blob.class);
  when(blob.getInputStream())
      .thenReturn(getClass().getResourceAsStream("/org/sonatype/nexus/repository/maven/gavMetadata.xml"));
  when(storageTx.findAssetWithProperty(any(), any(), any(Bucket.class))).thenReturn(asset);
  when(storageTx.requireBlob(any())).thenReturn(blob);
  MavenPath mavenPath = maven2MavenPathParser.parsePath(path);
  Content content = underTest.get(mavenPath);

  assertThat(content, not(nullValue()));
  assertThat(content.getContentType(), is(TEXT_XML));
  verify(metadataRebuilder)
      .rebuildInTransaction(any(), eq(false), eq(false), eq("org.sonatype.nexus"), eq("nexus-base"),
          eq("3.19.0-SNAPSHOT"));
}
 
Example #24
Source File: CondaProxyFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalStoreBlob
protected Content findOrCreateAsset(final TempBlob tempBlob,
                                    final Content content,
                                    final AssetKind assetKind,
                                    final String assetPath,
                                    final Component component) throws IOException
{
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  Asset asset = getCondaFacet().findAsset(tx, bucket, assetPath);

  if (asset == null) {
    if (ARCH_TAR_PACKAGE.equals(assetKind) || ARCH_CONDA_PACKAGE.equals(assetKind)) {
      asset = tx.createAsset(bucket, component);
    }
    else {
      asset = tx.createAsset(bucket, getRepository().getFormat());
    }
    asset.name(assetPath);
    asset.formatAttributes().set(P_ASSET_KIND, assetKind.name());
  }

  return getCondaFacet().saveAsset(tx, asset, tempBlob, content);
}
 
Example #25
Source File: ComponentComponentTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testReadAsset_notAllowed() {
  Asset asset = mock(Asset.class);
  Bucket bucket = mock(Bucket.class);
  EntityId bucketId = mock(EntityId.class);
  when(asset.bucketId()).thenReturn(bucketId);
  when(bucketStore.getById(bucketId)).thenReturn(bucket);
  when(bucket.getRepositoryName()).thenReturn("testRepositoryName");
  when(browseService.getAssetById(new DetachedEntityId("someid"), repository)).thenReturn(asset);
  when(contentPermissionChecker.isPermitted(anyString(),any(), any(), any())).thenReturn(false);

  try{
    underTest.readAsset("someid", "testRepositoryName");
    fail("AuthorizationException should have been thrown");
  } catch (AuthorizationException ae) {
    //expected
  }
}
 
Example #26
Source File: OrientNpmHostedFacetTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void getDistTagWhenPackageRootFound() throws Exception {
  Bucket bucket = mock(Bucket.class);
  Asset asset = mock(Asset.class);
  Blob blob = mock(Blob.class);
  ByteArrayInputStream bis = new ByteArrayInputStream("{\"dist-tags\":{\"latest\":\"1.0.0\"}}".getBytes());
  when(storageTx.findBucket(repository)).thenReturn(bucket);
  when(storageTx.findAssetWithProperty("name", "package", bucket)).thenReturn(asset);
  when(storageTx.requireBlob(asset.requireBlobRef())).thenReturn(blob);
  when(blob.getInputStream()).thenReturn(bis);

  final Content content = underTest.getDistTags(NpmPackageId.parse("package"));

  final String actual = IOUtils.toString(content.openInputStream());
  assertThat(actual, is("{\"latest\":\"1.0.0\"}"));
}
 
Example #27
Source File: MavenFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Asset putFile(final StorageTx tx,
                      final MavenPath path,
                      final AssetBlob assetBlob,
                      @Nullable final AttributesMap contentAttributes)
    throws IOException
{
  final Bucket bucket = tx.findBucket(getRepository());
  Asset asset = findAsset(tx, bucket, path);
  if (asset == null) {
    asset = tx.createAsset(bucket, getRepository().getFormat());
    asset.name(path.getPath());
    asset.formatAttributes().set(P_ASSET_KIND, fileAssetKindFor(path));
  }

  putAssetPayload(tx, asset, assetBlob, contentAttributes);

  tx.saveAsset(asset);

  return asset;
}
 
Example #28
Source File: PurgeUnusedFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Find all components that were last accessed before specified date. Date when a component was last accessed is the
 * last time an asset of that component was last accessed.
 */
private Iterable<Component> findUnusedComponents(final StorageTx tx, final Date olderThan) {
  final Bucket bucket = tx.findBucket(getRepository());

  String sql = String.format(
      "SELECT FROM (SELECT %s, MAX(%s) AS lastDownloaded FROM asset WHERE %s=:bucket AND %s IS NOT NULL GROUP BY %s) WHERE lastDownloaded < :olderThan",
      P_COMPONENT, P_LAST_DOWNLOADED, P_BUCKET, P_COMPONENT, P_COMPONENT
  );

  Map<String, Object> sqlParams = ImmutableMap.of(
      "bucket", AttachedEntityHelper.id(bucket),
      "olderThan", olderThan
  );

  checkCancellation();
  return Iterables.transform(tx.browse(sql, sqlParams),
      (doc) -> componentEntityAdapter.readEntity(doc.field(P_COMPONENT)));
}
 
Example #29
Source File: DefaultComponentMetadataProducerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void defaultLastBlobUpdatedUsesNewestUpload() throws Exception {
  DateTime expected = new DateTime();

  Bucket bucket = createBucket(REPO_NAME);
  Component component = createDetachedComponent(bucket, GROUP, NAME, VERSION);
  Iterable<Asset> assets = newArrayList(
      createDetachedAsset(bucket, "asset1", component).blobUpdated(expected.minusMillis(1)),
      createDetachedAsset(bucket, NAME, component).blobUpdated(expected),
      createDetachedAsset(bucket, "asset2", component).blobUpdated(expected.minusMillis(2))
  );

  DateTime actual = underTest.lastBlobUpdated(assets).get();

  assertThat(actual, is(expected));
}
 
Example #30
Source File: OrientBlobstoreRestoreTestHelper.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void assertAssetMatchesBlob(final Repository repository, final String name) {
  try (StorageTx tx = getStorageTx(repository)) {
    tx.begin();
    Asset asset = tx.findAssetWithProperty(AssetEntityAdapter.P_NAME, name, tx.findBucket(repository));
    Blob blob = tx.requireBlob(asset.blobRef());

    assertThat(repository.getName(), equalTo(blob.getHeaders().get(Bucket.REPO_NAME_HEADER)));
    assertThat(asset.name(), equalTo(blob.getHeaders().get(BlobStore.BLOB_NAME_HEADER)));
    assertThat(asset.createdBy(), equalTo(blob.getHeaders().get(BlobStore.CREATED_BY_HEADER)));
    assertThat(asset.createdByIp(), equalTo(blob.getHeaders().get(BlobStore.CREATED_BY_IP_HEADER)));
    assertThat(asset.contentType(), equalTo(blob.getHeaders().get(BlobStore.CONTENT_TYPE_HEADER)));
    assertThat(asset.attributes().child("checksum").get("sha1"), equalTo(blob.getMetrics().getSha1Hash()));
    assertThat(asset.size(), equalTo(blob.getMetrics().getContentSize()));
  }
}