Search in sources :

Example 6 with SwiftObjectPath

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);
}
Also used : SwiftObjectPath(org.apache.hadoop.fs.swift.util.SwiftObjectPath)

Example 7 with SwiftObjectPath

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);
}
Also used : Path(org.apache.hadoop.fs.Path) SwiftObjectPath(org.apache.hadoop.fs.swift.util.SwiftObjectPath) Header(org.apache.commons.httpclient.Header) FileNotFoundException(java.io.FileNotFoundException) SwiftException(org.apache.hadoop.fs.swift.exceptions.SwiftException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) SwiftObjectPath(org.apache.hadoop.fs.swift.util.SwiftObjectPath)

Example 8 with SwiftObjectPath

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;
}
Also used : Path(org.apache.hadoop.fs.Path) SwiftObjectPath(org.apache.hadoop.fs.swift.util.SwiftObjectPath) FileStatus(org.apache.hadoop.fs.FileStatus) CollectionType(com.fasterxml.jackson.databind.type.CollectionType) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) SwiftInvalidResponseException(org.apache.hadoop.fs.swift.exceptions.SwiftInvalidResponseException)

Example 9 with SwiftObjectPath

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);
}
Also used : Path(org.apache.hadoop.fs.Path) SwiftObjectPath(org.apache.hadoop.fs.swift.util.SwiftObjectPath) URI(java.net.URI) SwiftObjectPath(org.apache.hadoop.fs.swift.util.SwiftObjectPath) Test(org.junit.Test)

Example 10 with SwiftObjectPath

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);
}
Also used : Path(org.apache.hadoop.fs.Path) SwiftObjectPath(org.apache.hadoop.fs.swift.util.SwiftObjectPath) URI(java.net.URI) SwiftObjectPath(org.apache.hadoop.fs.swift.util.SwiftObjectPath) Test(org.junit.Test)

Aggregations

SwiftObjectPath (org.apache.hadoop.fs.swift.util.SwiftObjectPath)15 Test (org.junit.Test)10 Path (org.apache.hadoop.fs.Path)8 FileNotFoundException (java.io.FileNotFoundException)4 URI (java.net.URI)4 Header (org.apache.commons.httpclient.Header)2 FileStatus (org.apache.hadoop.fs.FileStatus)2 CollectionType (com.fasterxml.jackson.databind.type.CollectionType)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 FileAlreadyExistsException (org.apache.hadoop.fs.FileAlreadyExistsException)1 SwiftException (org.apache.hadoop.fs.swift.exceptions.SwiftException)1 SwiftInvalidResponseException (org.apache.hadoop.fs.swift.exceptions.SwiftInvalidResponseException)1 SwiftOperationFailedException (org.apache.hadoop.fs.swift.exceptions.SwiftOperationFailedException)1 Duration (org.apache.hadoop.fs.swift.util.Duration)1 DurationStats (org.apache.hadoop.fs.swift.util.DurationStats)1