@Override public void readBlob(final String blobName, final ReadBlobListener listener) { blobStore .executor() .execute( new Runnable() { @Override public void run() { byte[] buffer = new byte[blobStore.bufferSizeInBytes()]; FSDataInputStream fileStream; try { fileStream = blobStore.fileSystem().open(new Path(path, blobName)); } catch (IOException e) { listener.onFailure(e); return; } try { int bytesRead; while ((bytesRead = fileStream.read(buffer)) != -1) { listener.onPartial(buffer, 0, bytesRead); } listener.onCompleted(); } catch (Exception e) { try { fileStream.close(); } catch (IOException e1) { // ignore } listener.onFailure(e); } } }); }
@Override public boolean blobExists(String blobName) { try { return blobStore.fileSystem().exists(new Path(path, blobName)); } catch (Exception e) { return false; } }
public ImmutableMap<String, BlobMetaData> listBlobs() throws IOException { FileStatus[] files = blobStore.fileSystem().listStatus(path); if (files == null || files.length == 0) { return ImmutableMap.of(); } ImmutableMap.Builder<String, BlobMetaData> builder = ImmutableMap.builder(); for (FileStatus file : files) { builder.put( file.getPath().getName(), new PlainBlobMetaData(file.getPath().getName(), file.getLen())); } return builder.build(); }
@Override public ImmutableMap<String, BlobMetaData> listBlobsByPrefix(final @Nullable String blobNamePrefix) throws IOException { FileStatus[] files = blobStore .fileSystem() .listStatus( path, new PathFilter() { @Override public boolean accept(Path path) { return path.getName().startsWith(blobNamePrefix); } }); if (files == null || files.length == 0) { return ImmutableMap.of(); } ImmutableMap.Builder<String, BlobMetaData> builder = ImmutableMap.builder(); for (FileStatus file : files) { builder.put( file.getPath().getName(), new PlainBlobMetaData(file.getPath().getName(), file.getLen())); } return builder.build(); }
@Override public boolean deleteBlob(String blobName) throws IOException { return blobStore.fileSystem().delete(new Path(path, blobName), true); }