Java Code Examples for org.sonatype.nexus.blobstore.api.Blob#getMetrics()

The following examples show how to use org.sonatype.nexus.blobstore.api.Blob#getMetrics() . 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: FileBlobStoreIT.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void temporaryBlobMoveFallback() throws Exception {
  final byte[] content = new byte[TEST_DATA_LENGTH];
  new Random().nextBytes(content);

  doThrow(new AtomicMoveNotSupportedException("", "", "")).when(fileOperations).moveAtomic(any(), any());

  final Blob blob = underTest.create(new ByteArrayInputStream(content), TEST_HEADERS);
  verifyMoveOperations(blob);

  final byte[] output = extractContent(blob);
  assertThat("data must survive", content, is(equalTo(output)));

  final BlobMetrics metrics = blob.getMetrics();
  assertThat("size must be calculated correctly", metrics.getContentSize(), is(equalTo((long) TEST_DATA_LENGTH)));
}
 
Example 2
Source File: FluentAssetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private AssetBlobData createAssetBlob(final BlobRef blobRef,
                                      final Blob blob,
                                      final Map<HashAlgorithm, HashCode> checksums)
{
  BlobMetrics metrics = blob.getMetrics();
  Map<String, String> headers = blob.getHeaders();

  AssetBlobData assetBlob = new AssetBlobData();
  assetBlob.setBlobRef(blobRef);
  assetBlob.setBlobSize(metrics.getContentSize());
  assetBlob.setContentType(headers.get(CONTENT_TYPE_HEADER));

  assetBlob.setChecksums(checksums.entrySet().stream().collect(
      toImmutableMap(
          e -> e.getKey().name(),
          e -> e.getValue().toString())));

  assetBlob.setBlobCreated(toOffsetDateTime(metrics.getCreationTime()));
  assetBlob.setCreatedBy(headers.get(CREATED_BY_HEADER));
  assetBlob.setCreatedByIp(headers.get(CREATED_BY_IP_HEADER));

  facet.stores().assetBlobStore.createAssetBlob(assetBlob);

  return assetBlob;
}
 
Example 3
Source File: DatastoreDeadBlobFinder.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Verify that the Blob exists and is in agreement with the stored Asset metadata.;
 */
private void verifyBlob(final Blob blob, final Asset asset) throws MismatchedSHA1Exception, BlobUnavilableException, IOException {
  BlobMetrics metrics = blob.getMetrics();

  String assetChecksum =
      asset.blob().map(AssetBlob::checksums).map(checksums -> checksums.get(HashAlgorithm.SHA1.name())).orElse(null);
  if (!metrics.getSha1Hash().equals(assetChecksum)) {
    throw new MismatchedSHA1Exception();
  }

  try (InputStream blobstream = blob.getInputStream()) {
    if (metrics.getContentSize() > 0 && blobstream.available() == 0) {
      throw new BlobUnavilableException();
    }
  }
}
 
Example 4
Source File: FileBlobStoreIT.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void createAndDeleteBlobWithDirectPathSuccessful() throws IOException {
  final byte[] content = new byte[TEST_DATA_LENGTH];
  new Random().nextBytes(content);

  final Blob blob = underTest.create(new ByteArrayInputStream(content), ImmutableMap.of(
      CREATED_BY_HEADER, "test",
      BLOB_NAME_HEADER, "health-check/repositoryName/bundle.gz",
      DIRECT_PATH_BLOB_HEADER, "true"
  ));
  verifyMoveOperationsAtomic(blob);

  final byte[] output = extractContent(blob);
  assertThat("data must survive", content, is(equalTo(output)));

  final BlobMetrics metrics = blob.getMetrics();
  assertThat("size must be calculated correctly", metrics.getContentSize(), is(equalTo((long) TEST_DATA_LENGTH)));

  final BlobStoreMetrics storeMetrics = underTest.getMetrics();
  await().atMost(METRICS_FLUSH_TIMEOUT, SECONDS).until(() -> underTest.getMetrics().getBlobCount(), is(1L));

  assertThat(storeMetrics.getAvailableSpace(), is(greaterThan(0L)));

  final boolean deleted = underTest.delete(blob.getId(), "createAndDeleteBlobWithDirectPathSuccessful");
  assertThat(deleted, is(equalTo(true)));
  underTest.compact();
  await().atMost(METRICS_FLUSH_TIMEOUT, SECONDS).until(() -> underTest.getMetrics().getBlobCount(), is(0L));

  final Blob deletedBlob = underTest.get(blob.getId());
  assertThat(deletedBlob, is(nullValue()));
}
 
Example 5
Source File: FileBlobStoreIT.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void basicSmokeTest() throws Exception {
  final byte[] content = new byte[TEST_DATA_LENGTH];
  new Random().nextBytes(content);

  final Blob blob = underTest.create(new ByteArrayInputStream(content), TEST_HEADERS);
  verifyMoveOperationsAtomic(blob);

  final byte[] output = extractContent(blob);
  assertThat("data must survive", content, is(equalTo(output)));

  final BlobMetrics metrics = blob.getMetrics();
  assertThat("size must be calculated correctly", metrics.getContentSize(), is(equalTo((long) TEST_DATA_LENGTH)));

  final BlobStoreMetrics storeMetrics = underTest.getMetrics();
  await().atMost(METRICS_FLUSH_TIMEOUT, SECONDS).until(() -> underTest.getMetrics().getBlobCount(), is(1L));

  // FIXME: This is no longer valid
  //assertThat(storeMetrics.getTotalSize(), is(equalTo((long) TEST_DATA_LENGTH)));

  assertThat(storeMetrics.getAvailableSpace(), is(greaterThan(0L)));

  final boolean deleted = underTest.delete(blob.getId(), "basicSmokeTest");
  assertThat(deleted, is(equalTo(true)));
  underTest.compact();
  await().atMost(METRICS_FLUSH_TIMEOUT, SECONDS).until(() -> underTest.getMetrics().getBlobCount(), is(0L));

  final Blob deletedBlob = underTest.get(blob.getId());
  assertThat(deletedBlob, is(nullValue()));

  // Now that we've deleted the blob, there shouldn't be anything left
  final BlobStoreMetrics storeMetrics2 = underTest.getMetrics();

  // FIXME: This is no longer valid
  //assertThat(storeMetrics2.getBlobCount(), is(equalTo(0L)));
  //assertThat(storeMetrics2.getTotalSize(), is(equalTo((long) TEST_DATA_LENGTH)));

  underTest.compact();

  final BlobStoreMetrics storeMetrics3 = underTest.getMetrics();

  // FIXME: This is no longer valid
  //assertThat("compacting should reclaim deleted blobs' space", storeMetrics3.getTotalSize(), is(equalTo(0L)));
}
 
Example 6
Source File: FileBlobStoreIT.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void overwriteDirectPathBlobSuccessful() throws IOException {
  byte[] content = "hello".getBytes();
  Blob blob = underTest.create(new ByteArrayInputStream(content), ImmutableMap.of(
      CREATED_BY_HEADER, "test",
      BLOB_NAME_HEADER, "health-check/repositoryName/file.txt",
      DIRECT_PATH_BLOB_HEADER, "true"
  ));
  verifyMoveOperationsAtomic(blob);

  byte[] output = extractContent(blob);
  assertThat("data must survive", content, is(equalTo(output)));

  BlobMetrics metrics = blob.getMetrics();
  assertThat("size must be calculated correctly", metrics.getContentSize(), is(equalTo((long) content.length)));

  final BlobStoreMetrics storeMetrics = underTest.getMetrics();
  await().atMost(METRICS_FLUSH_TIMEOUT, SECONDS).until(() -> underTest.getMetrics().getBlobCount(), is(1L));

  assertThat(storeMetrics.getAvailableSpace(), is(greaterThan(0L)));

  // now overwrite the blob
  content = "goodbye".getBytes();
  blob = underTest.create(new ByteArrayInputStream(content), ImmutableMap.of(
      CREATED_BY_HEADER, "test",
      BLOB_NAME_HEADER, "health-check/repositoryName/file.txt",
      DIRECT_PATH_BLOB_HEADER, "true"
  ));
  verifyOverwriteOperationsAtomic(blob);

  output = extractContent(blob);
  assertThat("data must have been overwritten", content, is(equalTo(output)));

  metrics = blob.getMetrics();
  assertThat("size must be updated correctly", metrics.getContentSize(), is(equalTo((long) content.length)));

  final boolean deleted = underTest.delete(blob.getId(), " overwriteDirectPathBlobSuccessful");
  assertThat(deleted, is(equalTo(true)));
  underTest.compact();
  await().atMost(METRICS_FLUSH_TIMEOUT, SECONDS).until(() -> underTest.getMetrics().getBlobCount(), is(0L));

  final Blob deletedBlob = underTest.get(blob.getId());
  assertThat(deletedBlob, is(nullValue()));
}