
alluxio.client.file.BaseFileSystem Maven / Gradle / Ivy
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package alluxio.client.file;
import alluxio.AlluxioURI;
import alluxio.Constants;
import alluxio.annotation.PublicApi;
import alluxio.client.file.options.CreateDirectoryOptions;
import alluxio.client.file.options.CreateFileOptions;
import alluxio.client.file.options.DeleteOptions;
import alluxio.client.file.options.ExistsOptions;
import alluxio.client.file.options.FreeOptions;
import alluxio.client.file.options.GetStatusOptions;
import alluxio.client.file.options.ListStatusOptions;
import alluxio.client.file.options.LoadMetadataOptions;
import alluxio.client.file.options.MountOptions;
import alluxio.client.file.options.OpenFileOptions;
import alluxio.client.file.options.RenameOptions;
import alluxio.client.file.options.SetAttributeOptions;
import alluxio.client.file.options.UnmountOptions;
import alluxio.exception.AlluxioException;
import alluxio.exception.DirectoryNotEmptyException;
import alluxio.exception.ExceptionMessage;
import alluxio.exception.FileAlreadyExistsException;
import alluxio.exception.FileDoesNotExistException;
import alluxio.exception.InvalidPathException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import javax.annotation.concurrent.ThreadSafe;
/**
* Default implementation of the {@link FileSystem} interface. Developers can extend this class
* instead of implementing the interface. This implementation reads and writes data through
* {@link FileInStream} and {@link FileOutStream}. This class is thread safe.
*/
@PublicApi
@ThreadSafe
public class BaseFileSystem implements FileSystem {
private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);
private final FileSystemContext mContext;
/**
* @return the {@link BaseFileSystem}
*/
public static BaseFileSystem get() {
return new BaseFileSystem();
}
protected BaseFileSystem() {
mContext = FileSystemContext.INSTANCE;
}
@Override
public void createDirectory(AlluxioURI path)
throws FileAlreadyExistsException, InvalidPathException, IOException, AlluxioException {
createDirectory(path, CreateDirectoryOptions.defaults());
}
@Override
public void createDirectory(AlluxioURI path, CreateDirectoryOptions options)
throws FileAlreadyExistsException, InvalidPathException, IOException, AlluxioException {
FileSystemMasterClient masterClient = mContext.acquireMasterClient();
try {
masterClient.createDirectory(path, options);
LOG.debug("Created directory " + path.getPath());
} finally {
mContext.releaseMasterClient(masterClient);
}
}
@Override
public FileOutStream createFile(AlluxioURI path)
throws FileAlreadyExistsException, InvalidPathException, IOException, AlluxioException {
return createFile(path, CreateFileOptions.defaults());
}
@Override
public FileOutStream createFile(AlluxioURI path, CreateFileOptions options)
throws FileAlreadyExistsException, InvalidPathException, IOException, AlluxioException {
FileSystemMasterClient masterClient = mContext.acquireMasterClient();
try {
masterClient.createFile(path, options);
LOG.debug("Created file " + path.getPath());
} finally {
mContext.releaseMasterClient(masterClient);
}
return new FileOutStream(path, options.toOutStreamOptions());
}
@Override
public void delete(AlluxioURI path)
throws DirectoryNotEmptyException, FileDoesNotExistException, IOException, AlluxioException {
delete(path, DeleteOptions.defaults());
}
@Override
public void delete(AlluxioURI path, DeleteOptions options)
throws DirectoryNotEmptyException, FileDoesNotExistException, IOException, AlluxioException {
FileSystemMasterClient masterClient = mContext.acquireMasterClient();
try {
masterClient.delete(path, options);
LOG.debug("Deleted file " + path.getName());
} finally {
mContext.releaseMasterClient(masterClient);
}
}
@Override
public boolean exists(AlluxioURI path)
throws InvalidPathException, IOException, AlluxioException {
return exists(path, ExistsOptions.defaults());
}
@Override
public boolean exists(AlluxioURI path, ExistsOptions options)
throws InvalidPathException, IOException, AlluxioException {
FileSystemMasterClient masterClient = mContext.acquireMasterClient();
try {
// TODO(calvin): Make this more efficient
masterClient.getStatus(path);
return true;
} catch (FileDoesNotExistException e) {
return false;
} catch (InvalidPathException e) {
return false;
} finally {
mContext.releaseMasterClient(masterClient);
}
}
@Override
public void free(AlluxioURI path)
throws FileDoesNotExistException, IOException, AlluxioException {
free(path, FreeOptions.defaults());
}
@Override
public void free(AlluxioURI path, FreeOptions options)
throws FileDoesNotExistException, IOException, AlluxioException {
FileSystemMasterClient masterClient = mContext.acquireMasterClient();
try {
masterClient.free(path, options);
LOG.debug("Freed file " + path.getPath());
} finally {
mContext.releaseMasterClient(masterClient);
}
}
@Override
public URIStatus getStatus(AlluxioURI path)
throws FileDoesNotExistException, IOException, AlluxioException {
return getStatus(path, GetStatusOptions.defaults());
}
@Override
public URIStatus getStatus(AlluxioURI path, GetStatusOptions options)
throws FileDoesNotExistException, IOException, AlluxioException {
FileSystemMasterClient masterClient = mContext.acquireMasterClient();
try {
return masterClient.getStatus(path);
} catch (FileDoesNotExistException e) {
throw new FileDoesNotExistException(ExceptionMessage.PATH_DOES_NOT_EXIST.getMessage(path));
} catch (InvalidPathException e) {
throw new FileDoesNotExistException(ExceptionMessage.PATH_DOES_NOT_EXIST.getMessage(path));
} finally {
mContext.releaseMasterClient(masterClient);
}
}
@Override
public List listStatus(AlluxioURI path)
throws FileDoesNotExistException, IOException, AlluxioException {
return listStatus(path, ListStatusOptions.defaults());
}
@Override
public List listStatus(AlluxioURI path, ListStatusOptions options)
throws FileDoesNotExistException, IOException, AlluxioException {
FileSystemMasterClient masterClient = mContext.acquireMasterClient();
// TODO(calvin): Fix the exception handling in the master
try {
return masterClient.listStatus(path, options);
} catch (FileDoesNotExistException e) {
throw new FileDoesNotExistException(ExceptionMessage.PATH_DOES_NOT_EXIST.getMessage(path));
} finally {
mContext.releaseMasterClient(masterClient);
}
}
/**
* {@inheritDoc}
*
* @deprecated since version 1.1 and will be removed in version 2.0
*/
@Deprecated
@Override
public void loadMetadata(AlluxioURI path)
throws FileDoesNotExistException, IOException, AlluxioException {
loadMetadata(path, LoadMetadataOptions.defaults());
}
/**
* {@inheritDoc}
*
* @deprecated since version 1.1 and will be removed in version 2.0
*/
@Deprecated
@Override
public void loadMetadata(AlluxioURI path, LoadMetadataOptions options)
throws FileDoesNotExistException, IOException, AlluxioException {
FileSystemMasterClient masterClient = mContext.acquireMasterClient();
try {
masterClient.loadMetadata(path, options);
LOG.debug("loaded metadata {} with options {}", path.getParent(), options);
} finally {
mContext.releaseMasterClient(masterClient);
}
}
@Override
public void mount(AlluxioURI src, AlluxioURI dst) throws IOException, AlluxioException {
mount(src, dst, MountOptions.defaults());
}
@Override
public void mount(AlluxioURI src, AlluxioURI dst, MountOptions options)
throws IOException, AlluxioException {
FileSystemMasterClient masterClient = mContext.acquireMasterClient();
try {
// TODO(calvin): Make this fail on the master side
masterClient.mount(src, dst, options);
LOG.info("Mount " + src.getPath() + " to " + dst.getPath());
} finally {
mContext.releaseMasterClient(masterClient);
}
}
@Override
public FileInStream openFile(AlluxioURI path)
throws FileDoesNotExistException, IOException, AlluxioException {
return openFile(path, OpenFileOptions.defaults());
}
@Override
public FileInStream openFile(AlluxioURI path, OpenFileOptions options)
throws FileDoesNotExistException, IOException, AlluxioException {
URIStatus status = getStatus(path);
if (status.isFolder()) {
throw new FileNotFoundException(
ExceptionMessage.CANNOT_READ_DIRECTORY.getMessage(status.getName()));
}
return FileInStream.create(status, options.toInStreamOptions());
}
@Override
public void rename(AlluxioURI src, AlluxioURI dst)
throws FileDoesNotExistException, IOException, AlluxioException {
rename(src, dst, RenameOptions.defaults());
}
@Override
public void rename(AlluxioURI src, AlluxioURI dst, RenameOptions options)
throws FileDoesNotExistException, IOException, AlluxioException {
FileSystemMasterClient masterClient = mContext.acquireMasterClient();
try {
// TODO(calvin): Update this code on the master side.
masterClient.rename(src, dst);
LOG.debug("Renamed file " + src.getPath() + " to " + dst.getPath());
} finally {
mContext.releaseMasterClient(masterClient);
}
}
@Override
public void setAttribute(AlluxioURI path)
throws FileDoesNotExistException, IOException, AlluxioException {
setAttribute(path, SetAttributeOptions.defaults());
}
@Override
public void setAttribute(AlluxioURI path, SetAttributeOptions options)
throws FileDoesNotExistException, IOException, AlluxioException {
FileSystemMasterClient masterClient = mContext.acquireMasterClient();
try {
masterClient.setAttribute(path, options);
LOG.debug("Set attributes for path {} with options {}", path.getPath(), options);
} finally {
mContext.releaseMasterClient(masterClient);
}
}
@Override
public void unmount(AlluxioURI path) throws IOException, AlluxioException {
unmount(path, UnmountOptions.defaults());
}
@Override
public void unmount(AlluxioURI path, UnmountOptions options)
throws IOException, AlluxioException {
FileSystemMasterClient masterClient = mContext.acquireMasterClient();
try {
masterClient.unmount(path);
LOG.info("Unmount " + path);
} finally {
mContext.releaseMasterClient(masterClient);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy