use of org.apache.hadoop.fs.swift.util.SwiftObjectPath in project hadoop by apache.
the class SwiftNativeFileSystemStore method copy.
public void copy(Path srcKey, Path dstKey) throws IOException {
SwiftObjectPath srcObject = toObjectPath(srcKey);
SwiftObjectPath destObject = toObjectPath(dstKey);
swiftRestClient.copyObject(srcObject, destObject);
}
use of org.apache.hadoop.fs.swift.util.SwiftObjectPath in project hadoop by apache.
the class SwiftNativeFileSystemStore method getObjectMetadata.
/**
* Get the metadata of an object
*
* @param path path
* @param newest flag to say "set the newest header", otherwise take any entry
* @return file metadata. -or null if no headers were received back from the server.
* @throws IOException on a problem
* @throws FileNotFoundException if there is nothing at the end
*/
public SwiftFileStatus getObjectMetadata(Path path, boolean newest) throws IOException, FileNotFoundException {
SwiftObjectPath objectPath = toObjectPath(path);
final Header[] headers = stat(objectPath, newest);
//no headers is treated as a missing file
if (headers.length == 0) {
throw new FileNotFoundException("Not Found " + path.toUri());
}
boolean isDir = false;
long length = 0;
long lastModified = 0;
for (Header header : headers) {
String headerName = header.getName();
if (headerName.equals(SwiftProtocolConstants.X_CONTAINER_OBJECT_COUNT) || headerName.equals(SwiftProtocolConstants.X_CONTAINER_BYTES_USED)) {
length = 0;
isDir = true;
}
if (SwiftProtocolConstants.HEADER_CONTENT_LENGTH.equals(headerName)) {
length = Long.parseLong(header.getValue());
}
if (SwiftProtocolConstants.HEADER_LAST_MODIFIED.equals(headerName)) {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN);
try {
lastModified = simpleDateFormat.parse(header.getValue()).getTime();
} catch (ParseException e) {
throw new SwiftException("Failed to parse " + header.toString(), e);
}
}
}
if (lastModified == 0) {
lastModified = System.currentTimeMillis();
}
Path correctSwiftPath = getCorrectSwiftPath(path);
return new SwiftFileStatus(length, isDir, 1, getBlocksize(), lastModified, correctSwiftPath);
}
use of org.apache.hadoop.fs.swift.util.SwiftObjectPath in project hadoop by apache.
the class SwiftNativeFileSystemStore method listDirectory.
/**
* List a directory.
* This is O(n) for the number of objects in this path.
*
*
*
* @param path working path
* @param listDeep ask for all the data
* @param newest ask for the newest data
* @return Collection of file statuses
* @throws IOException IO problems
* @throws FileNotFoundException if the path does not exist
*/
private List<FileStatus> listDirectory(SwiftObjectPath path, boolean listDeep, boolean newest) throws IOException {
final byte[] bytes;
final ArrayList<FileStatus> files = new ArrayList<FileStatus>();
final Path correctSwiftPath = getCorrectSwiftPath(path);
try {
bytes = swiftRestClient.listDeepObjectsInDirectory(path, listDeep);
} catch (FileNotFoundException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("" + "File/Directory not found " + path);
}
if (SwiftUtils.isRootDir(path)) {
return Collections.emptyList();
} else {
throw e;
}
} catch (SwiftInvalidResponseException e) {
//bad HTTP error code
if (e.getStatusCode() == HttpStatus.SC_NO_CONTENT) {
//this can come back on a root list if the container is empty
if (SwiftUtils.isRootDir(path)) {
return Collections.emptyList();
} else {
//NO_CONTENT returned on something other than the root directory;
//see if it is there, and convert to empty list or not found
//depending on whether the entry exists.
FileStatus stat = getObjectMetadata(correctSwiftPath, newest);
if (stat.isDirectory()) {
//it's an empty directory. state that
return Collections.emptyList();
} else {
//it's a file -return that as the status
files.add(stat);
return files;
}
}
} else {
//a different status code: rethrow immediately
throw e;
}
}
final CollectionType collectionType = JSONUtil.getJsonMapper().getTypeFactory().constructCollectionType(List.class, SwiftObjectFileStatus.class);
final List<SwiftObjectFileStatus> fileStatusList = JSONUtil.toObject(new String(bytes, Charset.forName("UTF-8")), collectionType);
//in this case swift will return empty array
if (fileStatusList.isEmpty()) {
SwiftFileStatus objectMetadata = getObjectMetadata(correctSwiftPath, newest);
if (objectMetadata.isFile()) {
files.add(objectMetadata);
}
return files;
}
for (SwiftObjectFileStatus status : fileStatusList) {
if (status.getName() != null) {
files.add(new SwiftFileStatus(status.getBytes(), status.getBytes() == 0, 1, getBlocksize(), status.getLast_modified().getTime(), getCorrectSwiftPath(new Path(status.getName()))));
}
}
return files;
}
use of org.apache.hadoop.fs.swift.util.SwiftObjectPath in project hadoop by apache.
the class TestSwiftObjectPath method testParsePath.
@Test(timeout = SWIFT_TEST_TIMEOUT)
public void testParsePath() throws Exception {
final String pathString = "/home/user/files/file1";
final Path path = new Path(pathString);
final URI uri = new URI("http://container.localhost");
final SwiftObjectPath expected = SwiftObjectPath.fromPath(uri, path);
final SwiftObjectPath actual = new SwiftObjectPath(RestClientBindings.extractContainerName(uri), pathString);
assertEquals(expected, actual);
}
use of org.apache.hadoop.fs.swift.util.SwiftObjectPath in project hadoop by apache.
the class TestSwiftObjectPath method testParseAuthenticatedUrl.
@Test(timeout = SWIFT_TEST_TIMEOUT)
public void testParseAuthenticatedUrl() throws Exception {
final String pathString = "swift://container.service1/v2/AUTH_00345h34l93459y4/home/tom/documents/finance.docx";
final URI uri = new URI(pathString);
final Path path = new Path(pathString);
final SwiftObjectPath expected = SwiftObjectPath.fromPath(uri, path);
final SwiftObjectPath actual = new SwiftObjectPath(RestClientBindings.extractContainerName(uri), "/home/tom/documents/finance.docx");
assertEquals(expected, actual);
}
Aggregations