/** Test <code>void read()</code>. */ @Test public void readTest1() throws IOException { for (int k = 100; k <= 200; k += 33) { for (WriteType op : WriteType.values()) { int fileId = TestUtils.createByteFile(mTfs, "/root/testFile_" + k + "_" + op, op, k); TachyonFile file = mTfs.getFile(fileId); InStream is = (k < 150 ? file.getInStream(ReadType.CACHE) : file.getInStream(ReadType.NO_CACHE)); Assert.assertTrue(is instanceof BlockInStream); byte[] ret = new byte[k]; int value = is.read(); int cnt = 0; while (value != -1) { ret[cnt++] = (byte) value; value = is.read(); } Assert.assertTrue(TestUtils.equalIncreasingByteArray(k, ret)); is.close(); is = (k < 150 ? file.getInStream(ReadType.CACHE) : file.getInStream(ReadType.NO_CACHE)); Assert.assertTrue(is instanceof BlockInStream); ret = new byte[k]; value = is.read(); cnt = 0; while (value != -1) { ret[cnt++] = (byte) value; value = is.read(); } Assert.assertTrue(TestUtils.equalIncreasingByteArray(k, ret)); is.close(); } } }
/** Test <code>long skip(long len)</code>. */ @Test public void skipTest() throws IOException { for (int k = 10; k <= 200; k += 33) { for (WriteType op : WriteType.values()) { int fileId = TestUtils.createByteFile(mTfs, "/root/testFile_" + k + "_" + op, op, k); TachyonFile file = mTfs.getFile(fileId); InStream is = (k < 100 ? file.getInStream(ReadType.CACHE) : file.getInStream(ReadType.NO_CACHE)); Assert.assertTrue(is instanceof BlockInStream); Assert.assertEquals(k / 2, is.skip(k / 2)); Assert.assertEquals(k / 2, is.read()); is.close(); is = (k < 100 ? file.getInStream(ReadType.CACHE) : file.getInStream(ReadType.NO_CACHE)); Assert.assertTrue(is instanceof BlockInStream); int t = k / 3; Assert.assertEquals(t, is.skip(t)); Assert.assertEquals(t, is.read()); Assert.assertEquals(t, is.skip(t)); Assert.assertEquals((byte) (2 * t + 1), is.read()); is.close(); } } }
BlockOutStream(TachyonFile file, WriteType opType, int blockIndex) throws IOException { super(file, opType); if (!opType.isCache()) { throw new IOException("BlockOutStream only support WriteType.CACHE"); } BLOCK_INDEX = blockIndex; BLOCK_CAPACITY_BYTE = FILE.getBlockSizeByte(); BLOCK_ID = FILE.getBlockId(BLOCK_INDEX); BLOCK_OFFSET = BLOCK_CAPACITY_BYTE * blockIndex; PIN = FILE.needPin(); mCanWrite = true; if (!TFS.hasLocalWorker()) { mCanWrite = false; String msg = "The machine does not have any local worker."; throw new IOException(msg); } File localFolder = TFS.createAndGetUserTempFolder(); if (localFolder == null) { mCanWrite = false; String msg = "Failed to create temp user folder for tachyon client."; throw new IOException(msg); } mLocalFilePath = CommonUtils.concat(localFolder.getPath(), BLOCK_ID); mLocalFile = new RandomAccessFile(mLocalFilePath, "rw"); mLocalFileChannel = mLocalFile.getChannel(); // change the permission of the temporary file in order that the worker can move it. CommonUtils.changeLocalFileToFullPermission(mLocalFilePath); // use the sticky bit, only the client and the worker can write to the block CommonUtils.setLocalFileStickyBit(mLocalFilePath); LOG.info(mLocalFilePath + " was created!"); mBuffer = ByteBuffer.allocate(USER_CONF.FILE_BUFFER_BYTES + 4); }
/** Test <code>void read(byte[] b, int off, int len)</code>. */ @Test public void readTest3() throws IOException { for (int k = 100; k <= 300; k += 33) { for (WriteType op : WriteType.values()) { int fileId = TestUtils.createByteFile(mTfs, "/root/testFile_" + k + "_" + op, op, k); TachyonFile file = mTfs.getFile(fileId); InStream is = (k < 200 ? file.getInStream(ReadType.CACHE) : file.getInStream(ReadType.NO_CACHE)); Assert.assertTrue(is instanceof BlockInStream); byte[] ret = new byte[k / 2]; Assert.assertEquals(k / 2, is.read(ret, 0, k / 2)); Assert.assertTrue(TestUtils.equalIncreasingByteArray(k / 2, ret)); is.close(); is = (k < 200 ? file.getInStream(ReadType.CACHE) : file.getInStream(ReadType.NO_CACHE)); Assert.assertTrue(is instanceof BlockInStream); ret = new byte[k]; Assert.assertEquals(k, is.read(ret, 0, k)); Assert.assertTrue(TestUtils.equalIncreasingByteArray(k, ret)); is.close(); } } }