Search in sources :

Example 21 with FileInStream

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

the class TailCommand method runCommand.

@Override
protected void runCommand(AlluxioURI path, CommandLine cl) throws AlluxioException, IOException {
    URIStatus status = mFileSystem.getStatus(path);
    int numOfBytes = Constants.KB;
    if (cl.hasOption('c')) {
        numOfBytes = Integer.parseInt(cl.getOptionValue('c'));
        Preconditions.checkArgument(numOfBytes > 0, "specified bytes must be > 0");
    }
    if (status.isFolder()) {
        throw new IOException(ExceptionMessage.PATH_MUST_BE_FILE.getMessage(path));
    }
    OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
    try (FileInStream is = mFileSystem.openFile(path, options)) {
        byte[] buf = new byte[numOfBytes];
        long bytesToRead;
        if (status.getLength() > numOfBytes) {
            bytesToRead = numOfBytes;
        } else {
            bytesToRead = status.getLength();
        }
        is.skip(status.getLength() - bytesToRead);
        int read = is.read(buf);
        if (read != -1) {
            System.out.write(buf, 0, read);
        }
    }
}
Also used : FileInStream(alluxio.client.file.FileInStream) OpenFileOptions(alluxio.client.file.options.OpenFileOptions) IOException(java.io.IOException) URIStatus(alluxio.client.file.URIStatus)

Example 22 with FileInStream

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

the class FileInStreamIntegrationTest method seekExceptionTest1.

/**
   * Tests {@link FileInStream#seek(long)}. Validate the expected exception for seeking a negative
   * position.
   */
@Test
public void seekExceptionTest1() throws Exception {
    mThrown.expect(IllegalArgumentException.class);
    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);
            try (FileInStream is = sFileSystem.openFile(uri, FileSystemTestUtils.toOpenFileOptions(op))) {
                is.seek(-1);
            }
        }
    }
}
Also used : CreateFileOptions(alluxio.client.file.options.CreateFileOptions) FileInStream(alluxio.client.file.FileInStream) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 23 with FileInStream

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

the class FileInStreamIntegrationTest method seekExceptionTest2.

/**
   * Tests {@link FileInStream#seek(long)}. Validate the expected exception for seeking a position
   * that is past EOF.
   */
@Test
public void seekExceptionTest2() throws Exception {
    mThrown.expect(IllegalArgumentException.class);
    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);
            try (FileInStream is = sFileSystem.openFile(uri, FileSystemTestUtils.toOpenFileOptions(op))) {
                is.seek(k + 1);
            }
        }
    }
}
Also used : CreateFileOptions(alluxio.client.file.options.CreateFileOptions) FileInStream(alluxio.client.file.FileInStream) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 24 with FileInStream

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

the class IsolatedFileSystemIntegrationTest method unlockBlockTest2.

@Test
public void unlockBlockTest2() throws Exception {
    String uniqPath = PathUtils.uniqPath();
    FileInStream is;
    ByteBuffer buf;
    int numOfFiles = 5;
    int fileSize = WORKER_CAPACITY_BYTES / numOfFiles;
    List<AlluxioURI> files = new ArrayList<>();
    for (int k = 0; k < numOfFiles; k++) {
        FileSystemTestUtils.createByteFile(mFileSystem, uniqPath + k, fileSize, mWriteBoth);
        files.add(new AlluxioURI(uniqPath + k));
    }
    for (int k = 0; k < numOfFiles; k++) {
        URIStatus info = mFileSystem.getStatus(files.get(k));
        Assert.assertTrue(info.getInMemoryPercentage() == 100);
        is = mFileSystem.openFile(files.get(k), FileSystemTestUtils.toOpenFileOptions(mWriteBoth));
        buf = ByteBuffer.allocate((int) info.getBlockSizeBytes());
        Assert.assertTrue(is.read(buf.array()) != -1);
        is.seek(0);
        buf.clear();
        Assert.assertTrue(is.read(buf.array()) != -1);
        is.close();
    }
    FileSystemTestUtils.createByteFile(mFileSystem, uniqPath + numOfFiles, fileSize, mWriteBoth);
    files.add(new AlluxioURI(uniqPath + numOfFiles));
    for (int k = 1; k < numOfFiles; k++) {
        URIStatus info = mFileSystem.getStatus(files.get(k));
        Assert.assertTrue(info.getInMemoryPercentage() == 100);
    }
    HeartbeatScheduler.execute(HeartbeatContext.WORKER_BLOCK_SYNC);
    URIStatus info = mFileSystem.getStatus(files.get(numOfFiles));
    Assert.assertTrue(info.getInMemoryPercentage() == 100);
}
Also used : FileInStream(alluxio.client.file.FileInStream) ArrayList(java.util.ArrayList) URIStatus(alluxio.client.file.URIStatus) ByteBuffer(java.nio.ByteBuffer) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 25 with FileInStream

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

the class LocalBlockInStreamIntegrationTest method readTest1.

/**
   * Tests {@link alluxio.client.block.LocalBlockInStream#read()}.
   */
@Test
public void readTest1() throws Exception {
    for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) {
        for (CreateFileOptions op : getOptionSet()) {
            AlluxioURI uri = new AlluxioURI(sTestPath + "/file_" + k + "_" + op.hashCode());
            FileInStream is = sFileSystem.openFile(uri, sReadNoCache);
            byte[] ret = new byte[k];
            int value = is.read();
            int cnt = 0;
            while (value != -1) {
                Assert.assertTrue(value >= 0);
                Assert.assertTrue(value < 256);
                ret[cnt++] = (byte) value;
                value = is.read();
            }
            Assert.assertEquals(cnt, k);
            Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k, ret));
            is.close();
            is = sFileSystem.openFile(uri, sReadCachePromote);
            ret = new byte[k];
            value = is.read();
            cnt = 0;
            while (value != -1) {
                Assert.assertTrue(value >= 0);
                Assert.assertTrue(value < 256);
                ret[cnt++] = (byte) value;
                value = is.read();
            }
            Assert.assertEquals(cnt, k);
            Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k, ret));
            is.close();
            Assert.assertTrue(sFileSystem.getStatus(uri).getInMemoryPercentage() == 100);
        }
    }
}
Also used : CreateFileOptions(alluxio.client.file.options.CreateFileOptions) FileInStream(alluxio.client.file.FileInStream) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

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