* Test file system structure in meta-cache.
*
* @throws Exception If failed.
*/
public void testStructure() throws Exception {
GridGgfsFileInfo rootInfo = new GridGgfsFileInfo();
// Test empty structure.
assertEmpty(mgr.directoryListing(ROOT_ID));
assertEquals(rootInfo, mgr.info(ROOT_ID));
assertEquals(F.asMap(ROOT_ID, rootInfo), mgr.infos(Arrays.asList(ROOT_ID)));
GridGgfsFileInfo a = new GridGgfsFileInfo(true, null);
GridGgfsFileInfo b = new GridGgfsFileInfo(true, null);
GridGgfsFileInfo f1 = new GridGgfsFileInfo(400, null, false, null);
GridGgfsFileInfo f2 = new GridGgfsFileInfo(new GridGgfsFileInfo(400, null, false, null), 0);
GridGgfsFileInfo f3 = new GridGgfsFileInfo(new GridGgfsFileInfo(400, null, false, null), 200000L);
// Validate 'add file' operation.
assertNull(mgr.putIfAbsent(ROOT_ID, "a", a));
assertNull(mgr.putIfAbsent(ROOT_ID, "f1", f1));
assertNull(mgr.putIfAbsent(a.id(), "b", b));
assertNull(mgr.putIfAbsent(a.id(), "f2", f2));
assertNull(mgr.putIfAbsent(b.id(), "f3", f3));
assertEquals(b.id(), mgr.putIfAbsent(a.id(), "b", f3));
expectsPutIfAbsentFail(a.id(), "c", f3, "Failed to add file details into cache");
assertEquals(F.asMap("a", new GridGgfsListingEntry(a), "f1", new GridGgfsListingEntry(f1)),
mgr.directoryListing(ROOT_ID));
assertEquals(F.asMap("b", new GridGgfsListingEntry(b), "f2", new GridGgfsListingEntry(f2)),
mgr.directoryListing(a.id()));
assertEquals(F.asMap("f3", new GridGgfsListingEntry(f3)), mgr.directoryListing(b.id()));
// Validate empty files listings.
for (GridGgfsFileInfo info : Arrays.asList(f1, f2, f3)) {
assertEmpty(mgr.directoryListing(info.id()));
}
// Validate 'file info' operations.
for (GridGgfsFileInfo info : Arrays.asList(rootInfo, a, b, f1, f2, f3)) {
assertEquals(info, mgr.info(info.id()));
assertEquals(F.asMap(info.id(), info), mgr.infos(Arrays.asList(info.id())));
}
// Validate 'file ID' operations.
assertEquals(ROOT_ID, mgr.fileId(new GridGgfsPath("/")));
assertEquals(a.id(), mgr.fileId(new GridGgfsPath("/a")));
assertEquals(b.id(), mgr.fileId(new GridGgfsPath("/a/b")));
assertEquals(f1.id(), mgr.fileId(new GridGgfsPath("/f1")));
assertEquals(f2.id(), mgr.fileId(new GridGgfsPath("/a/f2")));
assertEquals(f3.id(), mgr.fileId(new GridGgfsPath("/a/b/f3")));
assertNull(mgr.fileId(new GridGgfsPath("/f4")));
assertNull(mgr.fileId(new GridGgfsPath("/a/f5")));
assertNull(mgr.fileId(new GridGgfsPath("/a/b/f6")));
assertEquals(a.id(), mgr.fileId(ROOT_ID, "a"));
assertEquals(b.id(), mgr.fileId(a.id(), "b"));
assertEquals(f1.id(), mgr.fileId(ROOT_ID, "f1"));
assertEquals(f2.id(), mgr.fileId(a.id(), "f2"));
assertEquals(f3.id(), mgr.fileId(b.id(), "f3"));
assertNull(mgr.fileId(ROOT_ID, "f4"));
assertNull(mgr.fileId(a.id(), "f5"));
assertNull(mgr.fileId(b.id(), "f6"));
assertEquals(Arrays.asList(ROOT_ID), mgr.fileIds(new GridGgfsPath("/")));
assertEquals(Arrays.asList(ROOT_ID, a.id()), mgr.fileIds(new GridGgfsPath("/a")));
assertEquals(Arrays.asList(ROOT_ID, a.id(), b.id()), mgr.fileIds(new GridGgfsPath("/a/b")));
assertEquals(Arrays.asList(ROOT_ID, f1.id()), mgr.fileIds(new GridGgfsPath("/f1")));
assertEquals(Arrays.asList(ROOT_ID, a.id(), f2.id()), mgr.fileIds(new GridGgfsPath("/a/f2")));
assertEquals(Arrays.asList(ROOT_ID, a.id(), b.id(), f3.id()), mgr.fileIds(new GridGgfsPath("/a/b/f3")));
assertEquals(Arrays.asList(ROOT_ID, null), mgr.fileIds(new GridGgfsPath("/f4")));
assertEquals(Arrays.asList(ROOT_ID, a.id(), null), mgr.fileIds(new GridGgfsPath("/a/f5")));
assertEquals(Arrays.asList(ROOT_ID, a.id(), b.id(), null), mgr.fileIds(new GridGgfsPath("/a/b/f6")));
assertEquals(Arrays.asList(ROOT_ID, null, null, null, null), mgr.fileIds(new GridGgfsPath("/f7/a/b/f6")));
// Validate 'rename' operation.
final GridUuid rndId = GridUuid.randomUuid();
// One of participated files does not exist in cache.
expectsRenameFail(ROOT_ID, "b", rndId, "b2", rndId, "Failed to lock source directory (not found?)");
expectsRenameFail(b.id(), "b", rndId, "b2", rndId, "Failed to lock source directory (not found?)");
expectsRenameFail(ROOT_ID, "b", ROOT_ID, "b2", rndId, "Failed to lock destination directory (not found?)");
expectsRenameFail(b.id(), "b", ROOT_ID, "b2", rndId, "Failed to lock destination directory (not found?)");
expectsRenameFail(rndId, "b", ROOT_ID, "b2", ROOT_ID, "Failed to lock target file (not found?)");
expectsRenameFail(rndId, "b", b.id(), "b2", b.id(), "Failed to lock target file (not found?)");
// Target file ID differ from the file ID resolved from the source directory for source file name.
expectsRenameFail(b.id(), "a", ROOT_ID, "q", ROOT_ID, "Failed to remove file name from the source directory");
expectsRenameFail(f1.id(), "a", ROOT_ID, "q", ROOT_ID, "Failed to remove file name from the source directory");
expectsRenameFail(f2.id(), "a", ROOT_ID, "q", ROOT_ID, "Failed to remove file name from the source directory");
expectsRenameFail(f3.id(), "a", ROOT_ID, "q", ROOT_ID, "Failed to remove file name from the source directory");
// Invalid source file name (not found).
expectsRenameFail(a.id(), "u1", ROOT_ID, "q", ROOT_ID, "Failed to remove file name from the source");
expectsRenameFail(a.id(), "u2", ROOT_ID, "q", ROOT_ID, "Failed to remove file name from the source");
expectsRenameFail(a.id(), "u3", ROOT_ID, "q", ROOT_ID, "Failed to remove file name from the source");
// Invalid destination file - already exists.
expectsRenameFail(a.id(), "a", ROOT_ID, "f1", ROOT_ID, "Failed to add file name into the destination");
expectsRenameFail(f2.id(), "f2", a.id(), "f1", ROOT_ID, "Failed to add file name into the destination");
expectsRenameFail(f3.id(), "f3", b.id(), "f1", ROOT_ID, "Failed to add file name into the destination");
expectsRenameFail(b.id(), "b", a.id(), "f2", a.id(), "Failed to add file name into the destination");
System.out.println("/: " + mgr.directoryListing(ROOT_ID));
System.out.println("a: " + mgr.directoryListing(a.id()));
System.out.println("b: " + mgr.directoryListing(b.id()));
System.out.println("f3: " + mgr.directoryListing(f3.id()));
mgr.move(a.id(), "a", ROOT_ID, "a2", ROOT_ID);
mgr.move(b.id(), "b", a.id(), "b2", a.id());
assertNotNull(mgr.info(b.id()));
mgr.move(f3.id(), "f3", b.id(), "f3-2", a.id());
assertNotNull(mgr.info(b.id()));
mgr.move(f3.id(), "f3-2", a.id(), "f3", b.id());
mgr.move(b.id(), "b2", a.id(), "b", a.id());
mgr.move(a.id(), "a2", ROOT_ID, "a", ROOT_ID);
// Validate 'remove' operation.
for (int i = 0; i < 100; i++) {
// One of participants doesn't exist.
assertNull(mgr.removeIfEmpty(ROOT_ID, "a", GridUuid.randomUuid(), new GridGgfsPath("/a"), true));
assertNull(mgr.removeIfEmpty(GridUuid.randomUuid(), "a", GridUuid.randomUuid(),
new GridGgfsPath("/" + GridUuid.randomUuid() + "/a"), true));
}
expectsRemoveFail(ROOT_ID, "a", a.id(), new GridGgfsPath("/a"),
"Failed to remove file (directory is not empty)");
expectsRemoveFail(a.id(), "b", b.id(), new GridGgfsPath("/a/b"),
"Failed to remove file (directory is not empty)");
assertNull(mgr.removeIfEmpty(ROOT_ID, "a", f1.id(), new GridGgfsPath("/a"), true));
assertNull(mgr.removeIfEmpty(a.id(), "b", f1.id(), new GridGgfsPath("/a/b"), true));
assertEquals(f3, mgr.removeIfEmpty(b.id(), "f3", f3.id(), new GridGgfsPath("/a/b/f3"), true));
assertEquals(F.asMap("a", new GridGgfsListingEntry(a), "f1", new GridGgfsListingEntry(f1)),
mgr.directoryListing(ROOT_ID));
assertEquals(F.asMap("b", new GridGgfsListingEntry(b), "f2", new GridGgfsListingEntry(f2)),
mgr.directoryListing(a.id()));
assertEmpty(mgr.directoryListing(b.id()));
assertEquals(b, mgr.removeIfEmpty(a.id(), "b", b.id(), new GridGgfsPath("/a/b"), true));
assertEquals(F.asMap("a", new GridGgfsListingEntry(a), "f1", new GridGgfsListingEntry(f1)),
mgr.directoryListing(ROOT_ID));
assertEquals(F.asMap("f2", new GridGgfsListingEntry(f2)), mgr.directoryListing(a.id()));
assertEmpty(mgr.directoryListing(b.id()));
// Validate last actual data received from 'remove' operation.
GridGgfsFileInfo newF2 = mgr.updateInfo(f2.id(), new C1<GridGgfsFileInfo, GridGgfsFileInfo>() {
@Override public GridGgfsFileInfo apply(GridGgfsFileInfo e) {
return new GridGgfsFileInfo(e, e.length() + 20);
}
});
assertNotNull(newF2);
assertEquals(f2.id(), newF2.id());
assertNotSame(f2, newF2);
assertEquals(newF2, mgr.removeIfEmpty(a.id(), "f2", f2.id(), new GridGgfsPath("/a/f2"), true));
assertEquals(F.asMap("a", new GridGgfsListingEntry(a), "f1", new GridGgfsListingEntry(f1)),