/* * Licensed to ElasticSearch and Shay Banon under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. ElasticSearch licenses this * file to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.elasticsearch.common.blobstore.hdfs; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.PathFilter; import org.elasticsearch.common.blobstore.BlobMetaData; import org.elasticsearch.common.blobstore.BlobPath; import org.elasticsearch.common.blobstore.support.AbstractBlobContainer; import org.elasticsearch.common.blobstore.support.PlainBlobMetaData; import org.elasticsearch.common.collect.ImmutableMap; import java.io.IOException; /** * */ public abstract class AbstractHdfsBlobContainer extends AbstractBlobContainer { protected final HdfsBlobStore blobStore; protected final Path path; public AbstractHdfsBlobContainer(HdfsBlobStore blobStore, BlobPath blobPath, Path path) { super(blobPath); this.blobStore = blobStore; this.path = path; } 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 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(); } public boolean deleteBlob(String blobName) throws IOException { return blobStore.fileSystem().delete(new Path(path, blobName), true); } @Override public boolean blobExists(String blobName) { try { return blobStore.fileSystem().exists(new Path(path, blobName)); } catch (IOException e) { return false; } } @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); } } }); } }