use of org.apache.ignite.igfs.IgfsInputStream in project ignite by apache.
the class IgfsProcessorSelfTest method testRename.
/** @throws Exception If failed. */
public void testRename() throws Exception {
// Create directories.
igfs.mkdirs(path("/A/B1/C1"));
for (Object key : metaCache.keySet()) info("Entry in cache [key=" + key + ", val=" + metaCache.get(key) + ']');
// Move under itself.
GridTestUtils.assertThrowsInherited(log, new Callable<Object>() {
@Override
public Object call() throws Exception {
igfs.rename(path("/A/B1/C1"), path("/A/B1/C1/C2"));
return null;
}
}, IgfsException.class, null);
// Move under itself.
GridTestUtils.assertThrowsInherited(log, new Callable<Object>() {
@Override
public Object call() throws Exception {
igfs.rename(path("/A/B1/C1"), path("/A/B1/C1/D/C2"));
return null;
}
}, IgfsException.class, null);
// Move under itself.
GridTestUtils.assertThrowsInherited(log, new Callable<Object>() {
@Override
public Object call() throws Exception {
igfs.rename(path("/A/B1/C1"), path("/A/B1/C1/D/E/C2"));
return null;
}
}, IgfsException.class, null);
///
// F6 > Enter > Tab x N times
// "I like to move it, move it..."
//
Collection<IgniteBiTuple<String, String>> chain = Arrays.asList(F.t("/A/B1/C1", "/A/B1/C2"), F.t("/A/B1", "/A/B2"), F.t("/A", "/Q"), //F.t("/Q/B2/C2", "/C3"),
F.t("/Q/B2/C2", "/Q/B2/C1"), F.t("/Q/B2", "/Q/B1"), F.t("/Q", "/A"), //F.t("/C3", "/A/B1/C1")
F.t("/A/B1/C1", "/"), F.t("/C1", "/A/B1"));
final IgfsPath root = path("/");
for (IgniteBiTuple<String, String> e : chain) {
final IgfsPath p1 = path(e.get1());
final IgfsPath p2 = path(e.get2());
assertTrue("Entry: " + e, igfs.exists(p1));
igfs.rename(p1, p2);
assertFalse("Entry: " + e, igfs.exists(p1));
assertTrue("Entry: " + e, igfs.exists(p2));
// Test root rename.
GridTestUtils.assertThrowsInherited(log, new Callable<Object>() {
@Override
public Object call() throws Exception {
igfs.rename(root, p1);
return null;
}
}, IgfsException.class, null);
// Test root rename.
GridTestUtils.assertThrowsInherited(log, new Callable<Object>() {
@Override
public Object call() throws Exception {
igfs.rename(p1, root);
return null;
}
}, IgfsException.class, null);
// Test root rename.
if (!root.equals(p2)) {
GridTestUtils.assertThrowsInherited(log, new Callable<Object>() {
@Override
public Object call() throws Exception {
igfs.rename(root, p2);
return null;
}
}, IgfsException.class, null);
}
// Test same rename.
igfs.rename(p1, p1);
igfs.rename(p2, p2);
}
// List items.
assertEquals(Arrays.asList(path("/A")), sorted(igfs.listPaths(root)));
assertEquals(Arrays.asList(path("/A/B1")), sorted(igfs.listPaths(path("/A"))));
assertEquals(Arrays.asList(path("/A/B1/C1")), sorted(igfs.listPaths(path("/A/B1"))));
String text = "Test long number: " + rnd.nextLong();
// Create file.
assertEquals(text, create("/A/a", false, text));
try (IgfsInputStream in0 = igfs.open(path("/A/a"))) {
// Rename file.
igfs.rename(path("/A/a"), path("/b"));
assertEquals(text, IOUtils.toString(in0, UTF_8));
}
// Validate after renamed.
assertOpenFails("/A/a", "File not found");
assertEquals(text, read("/b"));
// Cleanup.
igfs.clear();
assertTrue(F.isEmpty(igfs.listPaths(root)));
}
use of org.apache.ignite.igfs.IgfsInputStream in project ignite by apache.
the class IgfsMetricsSelfTest method testMultipleClose.
/** @throws Exception If failed. */
public void testMultipleClose() throws Exception {
IgniteFileSystem fs = igfsPrimary[0];
IgfsOutputStream out = fs.create(new IgfsPath("/primary/file"), false);
out.close();
out.close();
IgfsInputStream in = fs.open(new IgfsPath("/primary/file"));
in.close();
in.close();
IgfsMetrics m = fs.metrics();
assertEquals(0, m.filesOpenedForWrite());
assertEquals(0, m.filesOpenedForRead());
}
use of org.apache.ignite.igfs.IgfsInputStream in project ignite by apache.
the class IgfsMetricsSelfTest method testMetrics.
/** @throws Exception If failed. */
public void testMetrics() throws Exception {
IgniteFileSystem fs = igfsPrimary[0];
assertNotNull(fs);
IgfsMetrics m = fs.metrics();
assertNotNull(m);
assertEquals(0, m.directoriesCount());
assertEquals(0, m.filesCount());
assertEquals(0, m.filesOpenedForRead());
assertEquals(0, m.filesOpenedForWrite());
fs.mkdirs(new IgfsPath("/primary/dir1"));
m = fs.metrics();
assertNotNull(m);
assertEquals(2, m.directoriesCount());
assertEquals(0, m.filesCount());
assertEquals(0, m.filesOpenedForRead());
assertEquals(0, m.filesOpenedForWrite());
fs.mkdirs(new IgfsPath("/primary/dir1/dir2/dir3"));
fs.mkdirs(new IgfsPath("/primary/dir4"));
m = fs.metrics();
assertNotNull(m);
assertEquals(5, m.directoriesCount());
assertEquals(0, m.filesCount());
assertEquals(0, m.filesOpenedForRead());
assertEquals(0, m.filesOpenedForWrite());
IgfsOutputStream out1 = fs.create(new IgfsPath("/primary/dir1/file1"), false);
IgfsOutputStream out2 = fs.create(new IgfsPath("/primary/dir1/file2"), false);
IgfsOutputStream out3 = fs.create(new IgfsPath("/primary/dir1/dir2/file"), false);
m = fs.metrics();
assertNotNull(m);
assertEquals(5, m.directoriesCount());
assertEquals(3, m.filesCount());
assertEquals(0, m.filesOpenedForRead());
assertEquals(3, m.filesOpenedForWrite());
out1.write(new byte[10]);
out2.write(new byte[20]);
out3.write(new byte[30]);
out1.close();
m = fs.metrics();
assertNotNull(m);
assertEquals(5, m.directoriesCount());
assertEquals(3, m.filesCount());
assertEquals(0, m.filesOpenedForRead());
assertEquals(2, m.filesOpenedForWrite());
out2.close();
out3.close();
m = fs.metrics();
assertNotNull(m);
assertEquals(5, m.directoriesCount());
assertEquals(3, m.filesCount());
assertEquals(0, m.filesOpenedForRead());
assertEquals(0, m.filesOpenedForWrite());
IgfsOutputStream out = fs.append(new IgfsPath("/primary/dir1/file1"), false);
out.write(new byte[20]);
m = fs.metrics();
assertNotNull(m);
assertEquals(5, m.directoriesCount());
assertEquals(3, m.filesCount());
assertEquals(0, m.filesOpenedForRead());
assertEquals(1, m.filesOpenedForWrite());
out.write(new byte[20]);
out.close();
m = fs.metrics();
assertNotNull(m);
assertEquals(5, m.directoriesCount());
assertEquals(3, m.filesCount());
assertEquals(0, m.filesOpenedForRead());
assertEquals(0, m.filesOpenedForWrite());
IgfsInputStream in1 = fs.open(new IgfsPath("/primary/dir1/file1"));
IgfsInputStream in2 = fs.open(new IgfsPath("/primary/dir1/file2"));
m = fs.metrics();
assertNotNull(m);
assertEquals(5, m.directoriesCount());
assertEquals(3, m.filesCount());
assertEquals(2, m.filesOpenedForRead());
assertEquals(0, m.filesOpenedForWrite());
in1.close();
in2.close();
m = fs.metrics();
assertNotNull(m);
assertEquals(5, m.directoriesCount());
assertEquals(3, m.filesCount());
assertEquals(0, m.filesOpenedForRead());
assertEquals(0, m.filesOpenedForWrite());
fs.delete(new IgfsPath("/primary/dir1/file1"), false);
fs.delete(new IgfsPath("/primary/dir1/dir2"), true);
m = fs.metrics();
assertNotNull(m);
assertEquals(3, m.directoriesCount());
assertEquals(1, m.filesCount());
assertEquals(0, m.filesOpenedForRead());
assertEquals(0, m.filesOpenedForWrite());
fs.clear();
m = fs.metrics();
assertNotNull(m);
assertEquals(0, m.directoriesCount());
assertEquals(0, m.filesCount());
assertEquals(0, m.filesOpenedForRead());
assertEquals(0, m.filesOpenedForWrite());
}
use of org.apache.ignite.igfs.IgfsInputStream in project ignite by apache.
the class IgfsAbstractBaseSelfTest method checkFileContent.
/**
* Ensure that the given file has exactly the same content as provided in the "data" parameter.
*
* @param igfs IGFS.
* @param file File.
* @param chunks Expected data.
* @throws IOException In case of IO exception.
* @throws IgniteCheckedException In case of Grid exception.
*/
protected static void checkFileContent(IgfsImpl igfs, IgfsPath file, @Nullable byte[]... chunks) throws IOException, IgniteCheckedException {
if (chunks != null && chunks.length > 0) {
IgfsInputStream is = null;
try {
is = igfs.open(file);
int chunkIdx = 0;
int pos = 0;
for (byte[] chunk : chunks) {
byte[] buf = new byte[chunk.length];
is.readFully(pos, buf);
assert Arrays.equals(chunk, buf) : "Bad chunk [igfs=" + igfs.name() + ", chunkIdx=" + chunkIdx + ", expected=" + Arrays.toString(chunk) + ", actual=" + Arrays.toString(buf) + ']';
chunkIdx++;
pos += chunk.length;
}
is.close();
} finally {
U.closeQuiet(is);
}
}
}
use of org.apache.ignite.igfs.IgfsInputStream in project ignite by apache.
the class IgfsImpl method open.
/** {@inheritDoc} */
@Override
public IgfsInputStream open(final IgfsPath path, final int bufSize, final int seqReadsBeforePrefetch) {
A.notNull(path, "path");
A.ensure(bufSize >= 0, "bufSize >= 0");
A.ensure(seqReadsBeforePrefetch >= 0, "seqReadsBeforePrefetch >= 0");
return safeOp(new Callable<IgfsInputStream>() {
@Override
public IgfsInputStream call() throws Exception {
if (log.isDebugEnabled())
log.debug("Open file for reading [path=" + path + ", bufSize=" + bufSize + ']');
int bufSize0 = bufSize == 0 ? cfg.getBufferSize() : bufSize;
IgfsMode mode = resolveMode(path);
switch(mode) {
case PRIMARY:
{
IgfsEntryInfo info = meta.infoForPath(path);
if (info == null)
throw new IgfsPathNotFoundException("File not found: " + path);
if (!info.isFile())
throw new IgfsPathIsDirectoryException("Failed to open file (not a file): " + path);
// Input stream to read data from grid cache with separate blocks.
IgfsInputStreamImpl os = new IgfsInputStreamImpl(igfsCtx, path, info, cfg.getPrefetchBlocks(), seqReadsBeforePrefetch, null, info.length(), info.blockSize(), info.blocksCount(), false);
IgfsUtils.sendEvents(igfsCtx.kernalContext(), path, EVT_IGFS_FILE_OPENED_READ);
return os;
}
case DUAL_ASYNC:
case DUAL_SYNC:
{
assert IgfsUtils.isDualMode(mode);
IgfsSecondaryInputStreamDescriptor desc = meta.openDual(secondaryFs, path, bufSize0);
IgfsEntryInfo info = desc.info();
IgfsInputStreamImpl os = new IgfsInputStreamImpl(igfsCtx, path, info, cfg.getPrefetchBlocks(), seqReadsBeforePrefetch, desc.reader(), info.length(), info.blockSize(), info.blocksCount(), false);
IgfsUtils.sendEvents(igfsCtx.kernalContext(), path, EVT_IGFS_FILE_OPENED_READ);
return os;
}
case PROXY:
{
assert secondaryFs != null;
IgfsFile info = info(path);
if (info == null)
throw new IgfsPathNotFoundException("File not found: " + path);
if (!info.isFile())
throw new IgfsPathIsDirectoryException("Failed to open file (not a file): " + path);
IgfsSecondaryFileSystemPositionedReadable secReader = new IgfsLazySecondaryFileSystemPositionedReadable(secondaryFs, path, bufSize);
long len = info.length();
int blockSize = info.blockSize() > 0 ? info.blockSize() : cfg.getBlockSize();
long blockCnt = len / blockSize;
if (len % blockSize != 0)
blockCnt++;
IgfsInputStream os = new IgfsInputStreamImpl(igfsCtx, path, null, cfg.getPrefetchBlocks(), seqReadsBeforePrefetch, secReader, info.length(), blockSize, blockCnt, true);
IgfsUtils.sendEvents(igfsCtx.kernalContext(), path, EVT_IGFS_FILE_OPENED_READ);
return os;
}
default:
assert false : "Unexpected mode " + mode;
return null;
}
}
});
}
Aggregations