Search in sources :

Example 6 with BlobMetadata

use of org.elasticsearch.common.blobstore.BlobMetadata in project elasticsearch by elastic.

the class ESBlobStoreContainerTestCase method testMoveAndList.

public void testMoveAndList() throws IOException {
    try (BlobStore store = newBlobStore()) {
        final BlobContainer container = store.blobContainer(new BlobPath());
        assertThat(container.listBlobs().size(), equalTo(0));
        int numberOfFooBlobs = randomIntBetween(0, 10);
        int numberOfBarBlobs = randomIntBetween(3, 20);
        Map<String, Long> generatedBlobs = new HashMap<>();
        for (int i = 0; i < numberOfFooBlobs; i++) {
            int length = randomIntBetween(10, 100);
            String name = "foo-" + i + "-";
            generatedBlobs.put(name, (long) length);
            writeRandomBlob(container, name, length);
        }
        for (int i = 1; i < numberOfBarBlobs; i++) {
            int length = randomIntBetween(10, 100);
            String name = "bar-" + i + "-";
            generatedBlobs.put(name, (long) length);
            writeRandomBlob(container, name, length);
        }
        int length = randomIntBetween(10, 100);
        String name = "bar-0-";
        generatedBlobs.put(name, (long) length);
        byte[] data = writeRandomBlob(container, name, length);
        Map<String, BlobMetaData> blobs = container.listBlobs();
        assertThat(blobs.size(), equalTo(numberOfFooBlobs + numberOfBarBlobs));
        for (Map.Entry<String, Long> generated : generatedBlobs.entrySet()) {
            BlobMetaData blobMetaData = blobs.get(generated.getKey());
            assertThat(generated.getKey(), blobMetaData, notNullValue());
            assertThat(blobMetaData.name(), equalTo(generated.getKey()));
            assertThat(blobMetaData.length(), equalTo(generated.getValue()));
        }
        assertThat(container.listBlobsByPrefix("foo-").size(), equalTo(numberOfFooBlobs));
        assertThat(container.listBlobsByPrefix("bar-").size(), equalTo(numberOfBarBlobs));
        assertThat(container.listBlobsByPrefix("baz-").size(), equalTo(0));
        String newName = "bar-new";
        // Move to a new location
        container.move(name, newName);
        assertThat(container.listBlobsByPrefix(name).size(), equalTo(0));
        blobs = container.listBlobsByPrefix(newName);
        assertThat(blobs.size(), equalTo(1));
        assertThat(blobs.get(newName).length(), equalTo(generatedBlobs.get(name)));
        assertThat(data, equalTo(readBlobFully(container, newName, length)));
    }
}
Also used : BlobPath(org.elasticsearch.common.blobstore.BlobPath) HashMap(java.util.HashMap) BlobMetaData(org.elasticsearch.common.blobstore.BlobMetaData) BlobContainer(org.elasticsearch.common.blobstore.BlobContainer) HashMap(java.util.HashMap) Map(java.util.Map) BlobStore(org.elasticsearch.common.blobstore.BlobStore)

Example 7 with BlobMetadata

use of org.elasticsearch.common.blobstore.BlobMetadata in project elasticsearch by elastic.

the class FsBlobContainer method listBlobsByPrefix.

@Override
public Map<String, BlobMetaData> listBlobsByPrefix(String blobNamePrefix) throws IOException {
    // If we get duplicate files we should just take the last entry
    Map<String, BlobMetaData> builder = new HashMap<>();
    blobNamePrefix = blobNamePrefix == null ? "" : blobNamePrefix;
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, blobNamePrefix + "*")) {
        for (Path file : stream) {
            final BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
            if (attrs.isRegularFile()) {
                builder.put(file.getFileName().toString(), new PlainBlobMetaData(file.getFileName().toString(), attrs.size()));
            }
        }
    }
    return unmodifiableMap(builder);
}
Also used : BlobPath(org.elasticsearch.common.blobstore.BlobPath) Path(java.nio.file.Path) PlainBlobMetaData(org.elasticsearch.common.blobstore.support.PlainBlobMetaData) HashMap(java.util.HashMap) PlainBlobMetaData(org.elasticsearch.common.blobstore.support.PlainBlobMetaData) BlobMetaData(org.elasticsearch.common.blobstore.BlobMetaData) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

BlobMetaData (org.elasticsearch.common.blobstore.BlobMetaData)7 PlainBlobMetaData (org.elasticsearch.common.blobstore.support.PlainBlobMetaData)4 HashMap (java.util.HashMap)2 BlobContainer (org.elasticsearch.common.blobstore.BlobContainer)2 BlobPath (org.elasticsearch.common.blobstore.BlobPath)2 BlobStore (org.elasticsearch.common.blobstore.BlobStore)2 BlobProperties (com.microsoft.azure.storage.blob.BlobProperties)1 CloudBlobClient (com.microsoft.azure.storage.blob.CloudBlobClient)1 CloudBlobContainer (com.microsoft.azure.storage.blob.CloudBlobContainer)1 CloudBlockBlob (com.microsoft.azure.storage.blob.CloudBlockBlob)1 ListBlobItem (com.microsoft.azure.storage.blob.ListBlobItem)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 FileStatus (org.apache.hadoop.fs.FileStatus)1 FsBlobStore (org.elasticsearch.common.blobstore.fs.FsBlobStore)1 ChecksumBlobStoreFormat (org.elasticsearch.repositories.blobstore.ChecksumBlobStoreFormat)1