Search in sources :

Example 16 with FileInStream

use of alluxio.client.file.FileInStream in project alluxio by Alluxio.

the class FileInStreamIntegrationTest method readTest2.

/**
   * Tests {@link FileInStream#read(byte[])}.
   */
@Test
public void readTest2() throws Exception {
    for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) {
        for (CreateFileOptions op : getOptionSet()) {
            String filename = sTestPath + "/file_" + k + "_" + op.hashCode();
            AlluxioURI uri = new AlluxioURI(filename);
            FileInStream is = sFileSystem.openFile(uri, FileSystemTestUtils.toOpenFileOptions(op));
            byte[] ret = new byte[k];
            Assert.assertEquals(k, is.read(ret));
            Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k, ret));
            is.close();
            is = sFileSystem.openFile(uri, FileSystemTestUtils.toOpenFileOptions(op));
            ret = new byte[k];
            Assert.assertEquals(k, is.read(ret));
            Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k, ret));
            is.close();
        }
    }
}
Also used : CreateFileOptions(alluxio.client.file.options.CreateFileOptions) FileInStream(alluxio.client.file.FileInStream) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 17 with FileInStream

use of alluxio.client.file.FileInStream in project alluxio by Alluxio.

the class FileInStreamIntegrationTest method seek.

/**
   * Tests {@link FileInStream#seek(long)}.
   */
@Test
public void seek() throws Exception {
    for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) {
        for (CreateFileOptions op : getOptionSet()) {
            String filename = sTestPath + "/file_" + k + "_" + op.hashCode();
            AlluxioURI uri = new AlluxioURI(filename);
            FileInStream is = sFileSystem.openFile(uri, FileSystemTestUtils.toOpenFileOptions(op));
            is.seek(k / 3);
            Assert.assertEquals(k / 3, is.read());
            is.seek(k / 2);
            Assert.assertEquals(k / 2, is.read());
            is.seek(k / 4);
            Assert.assertEquals(k / 4, is.read());
            is.close();
        }
    }
}
Also used : CreateFileOptions(alluxio.client.file.options.CreateFileOptions) FileInStream(alluxio.client.file.FileInStream) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 18 with FileInStream

use of alluxio.client.file.FileInStream in project alluxio by Alluxio.

the class ReadOnlyMountIntegrationTest method openFile.

@Test
public void openFile() throws IOException, AlluxioException {
    AlluxioURI fileUri = new AlluxioURI(FILE_PATH);
    mFileSystem.loadMetadata(fileUri);
    FileInStream inStream = mFileSystem.openFile(fileUri);
    Assert.assertNotNull(inStream);
    inStream.close();
    fileUri = new AlluxioURI(SUB_FILE_PATH);
    mFileSystem.loadMetadata(fileUri, LoadMetadataOptions.defaults().setRecursive(true));
    inStream = mFileSystem.openFile(fileUri);
    Assert.assertNotNull(inStream);
    inStream.close();
}
Also used : FileInStream(alluxio.client.file.FileInStream) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 19 with FileInStream

use of alluxio.client.file.FileInStream in project alluxio by Alluxio.

the class LoadCommand method load.

/**
   * Loads a file or directory in Alluxio space, makes it resident in memory.
   *
   * @param filePath The {@link AlluxioURI} path to load into Alluxio memory
   * @throws AlluxioException when Alluxio exception occurs
   * @throws IOException when non-Alluxio exception occurs
   */
private void load(AlluxioURI filePath) throws AlluxioException, IOException {
    URIStatus status = mFileSystem.getStatus(filePath);
    if (status.isFolder()) {
        List<URIStatus> statuses = mFileSystem.listStatus(filePath);
        for (URIStatus uriStatus : statuses) {
            AlluxioURI newPath = new AlluxioURI(uriStatus.getPath());
            load(newPath);
        }
    } else {
        if (status.getInMemoryPercentage() == 100) {
            // The file has already been fully loaded into Alluxio memory.
            return;
        }
        Closer closer = Closer.create();
        try {
            OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.CACHE_PROMOTE);
            FileInStream in = closer.register(mFileSystem.openFile(filePath, options));
            byte[] buf = new byte[8 * Constants.MB];
            while (in.read(buf) != -1) {
            }
        } catch (Exception e) {
            throw closer.rethrow(e);
        } finally {
            closer.close();
        }
    }
    System.out.println(filePath + " loaded");
}
Also used : Closer(com.google.common.io.Closer) FileInStream(alluxio.client.file.FileInStream) OpenFileOptions(alluxio.client.file.options.OpenFileOptions) URIStatus(alluxio.client.file.URIStatus) AlluxioException(alluxio.exception.AlluxioException) IOException(java.io.IOException) AlluxioURI(alluxio.AlluxioURI)

Example 20 with FileInStream

use of alluxio.client.file.FileInStream in project alluxio by Alluxio.

the class CatCommand method runCommand.

@Override
protected void runCommand(AlluxioURI path, CommandLine cl) throws AlluxioException, IOException {
    URIStatus status = mFileSystem.getStatus(path);
    if (status.isFolder()) {
        throw new FileDoesNotExistException(ExceptionMessage.PATH_MUST_BE_FILE.getMessage(path));
    }
    OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
    byte[] buf = new byte[512];
    try (FileInStream is = mFileSystem.openFile(path, options)) {
        int read = is.read(buf);
        while (read != -1) {
            System.out.write(buf, 0, read);
            read = is.read(buf);
        }
    }
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) FileInStream(alluxio.client.file.FileInStream) OpenFileOptions(alluxio.client.file.options.OpenFileOptions) URIStatus(alluxio.client.file.URIStatus)

Aggregations

FileInStream (alluxio.client.file.FileInStream)179 AlluxioURI (alluxio.AlluxioURI)148 Test (org.junit.Test)132 BaseIntegrationTest (alluxio.testutils.BaseIntegrationTest)67 URIStatus (alluxio.client.file.URIStatus)44 CreateFilePOptions (alluxio.grpc.CreateFilePOptions)31 FileOutStream (alluxio.client.file.FileOutStream)25 CreateFileOptions (alluxio.client.file.options.CreateFileOptions)21 FileSystem (alluxio.client.file.FileSystem)17 ByteBuffer (java.nio.ByteBuffer)16 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)13 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)11 File (java.io.File)11 OpenFileOptions (alluxio.client.file.options.OpenFileOptions)10 FileAlreadyExistsException (alluxio.exception.FileAlreadyExistsException)8 FileInfo (alluxio.wire.FileInfo)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 InstancedConfiguration (alluxio.conf.InstancedConfiguration)5 FileIncompleteException (alluxio.exception.FileIncompleteException)5