Package org.sonatype.nexus.blobstore.api

Examples of org.sonatype.nexus.blobstore.api.Blob


          {
            @Override
            public void run() throws Exception {
              final byte[] data = new byte[random.nextInt(BLOB_MAX_SIZE_BYTES) + 1];
              random.nextBytes(data);
              final Blob blob = underTest.create(new ByteArrayInputStream(data), TEST_HEADERS);

              blobIdsInTheStore.add(blob.getId());
            }
          }
      );
      testWorkers.add(r);
    }

    for (int i = 0; i < numberOfReaders; i++) {
      TestWorker r = new TestWorker(testWorkers.size(), startingGun, failureHasOccurred, numberOfIterations,
          new TestTask()
          {
            @Override
            public void run() throws Exception {
              final BlobId blobId = blobIdsInTheStore.peek();

              log("Attempting to read " + blobId);

              if (blobId == null) {
                return;
              }

              final Blob blob = underTest.get(blobId);
              if (blob == null) {
                log("Attempted to obtain blob, but it was deleted:" + blobId);
                return;
              }

              try (InputStream inputStream = blob.getInputStream()) {
                readContentAndValidateMetrics(blobId, inputStream, blob.getMetrics());
              }
              catch (BlobStoreException e) {
                // This is normal operation if another thread deletes your blob after you obtain a Blob reference
                log("Concurrent deletion suspected while calling blob.getInputStream().", e);
              }
View Full Code Here


  /**
   * Creates a new blob, requesting that it be deleted in the event of a rollback.
   */
  public Blob create(InputStream content, Map<String, String> headers) {
    final Blob blob = blobStore.create(content, headers);
    newlyCreatedBlobs.add(blob);
    return blob;
  }
View Full Code Here

    // generate asset id
    EntityId assetId = entityIdFactory.newId();

    // create blob from source
    Blob blob = createBlob(blobTx, sourceAsset, assetId);
    checkArgument(blob != null, "Cannot store new asset without a content stream");

    // create and save a new asset document using source and system-provided data
    ODocument assetDocument = createAssetDocument(db, assetClass, sourceAsset, assetAdapter, assetId,
        blob, componentDocument.getIdentity());
View Full Code Here

      // generate asset id
      EntityId assetId = entityIdFactory.newId();

      // create blob from source
      Blob blob = createBlob(blobTx, sourceAsset, assetId);
      checkArgument(blob != null, "Cannot store new asset without a content stream");

      // create and save a new asset document using source and system-provided data
      ODocument assetDocument = createAssetDocument(db, assetClass, sourceAsset, assetAdapter, assetId,
          blob, componentDocument.getIdentity());
View Full Code Here

    // update asset document using source data
    assetAdapter.populateDocument(sourceAsset, assetDocument);

    // grab the local blob
    Blob currentBlob = getLocalBlob(assetDocument);

    // create blob from source if it specifies a content change
    Blob newBlob = createBlob(blobTx, sourceAsset, assetId);
    if (newBlob != null) {
      blobTx.delete(currentBlob);
      currentBlob = newBlob;

      // update blob ref with new one
View Full Code Here

  private <A extends Asset> A assetFrom(AssetAdapter<A> assetAdapter,
      ODocument assetDocument) {
    ODocument componentDocument = assetDocument.field(P_COMPONENT);
    // TODO: For performance, consider storing componentId as a STRING directly in the asset document.
    EntityId componentId = new EntityId((String) componentDocument.field(EntityAdapter.P_ID));
    Blob blob = getLocalBlob(assetDocument);
    return assetFrom(assetAdapter, assetDocument, componentId, blob);
  }
View Full Code Here

  private Blob getLocalBlob(ODocument assetDocument) {
    Map<String, String> blobRefs = assetDocument.field(P_BLOB_REFS);
    checkState(blobRefs.containsKey(localBlobStoreId), "Local blobRef does not exist for that asset");
    BlobId blobId = new BlobId(blobRefs.get(localBlobStoreId));
    Blob blob = blobStore.get(new BlobId(blobRefs.get(localBlobStoreId)));
    checkState(blob != null, "Blob not found in local store: %", blobId.asUniqueString());
    return blob;
  }
View Full Code Here

  }

  protected ORID addTestAsset(ODatabaseDocumentTx db, ORID componentDocumentRid, EntityId componentId, int n) {
    // create the blob
    String assetId = "asset" + n;
    Blob blob = blobStore.create(toStream(testContent(n)), ImmutableMap.of(
        BlobStore.BLOB_NAME_HEADER, assetId,
        BlobStore.CREATED_BY_HEADER, "Test"));

    // create the asset document
    ODocument assetDocument = db.newInstance("testasset");
    assetDocument.field(P_ID, assetId);
    assetDocument.field(P_COMPONENT, componentDocumentRid);
    assetDocument.field(P_DOWNLOAD_COUNT, n);
    assetDocument.field(P_FIRST_CREATED, blob.getMetrics().getCreationTime().toDate());
    assetDocument.field(P_CONTENT_TYPE, "text/plain");
    if (n % 2 == 1) {
      assetDocument.field(P_PATH, "" + n); // for variance, only set path if n is odd
    }
    Map<String, String> blobRefs = ImmutableMap.of("someBlobStoreId", blob.getId().asUniqueString());
    assetDocument.field(P_BLOB_REFS, blobRefs);
    assetDocument.save();

    // return the id of the saved asset document
    return assetDocument.getIdentity();
View Full Code Here

  @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);

    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();
    assertThat(storeMetrics.getBlobCount(), is(equalTo(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());
    assertThat(deleted, is(equalTo(true)));

    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();
View Full Code Here

  @Test
  public void hardDeletePreventsGetDespiteOpenStreams() throws Exception {
    final byte[] content = new byte[TEST_DATA_LENGTH];
    new Random().nextBytes(content);

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

    final InputStream inputStream = blob.getInputStream();

    // Read half the data
    inputStream.read(new byte[content.length / 2]);

    // force delete
    underTest.deleteHard(blob.getId());

    final Blob newBlob = underTest.get(blob.getId());
    assertThat(newBlob, is(nullValue()));
  }
View Full Code Here

TOP

Related Classes of org.sonatype.nexus.blobstore.api.Blob

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.