Skip to content

Commit

Permalink
IGNITE-3611: IGFS: Slight refactoring to listPaths() and listFiles() …
Browse files Browse the repository at this point in the history
…methods.
  • Loading branch information
vozerov-gridgain committed Sep 5, 2016
1 parent 008cf64 commit bf9371a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 40 deletions.
Expand Up @@ -17,7 +17,6 @@


package org.apache.ignite.internal.processors.igfs; package org.apache.ignite.internal.processors.igfs;


import java.util.Set;
import org.apache.ignite.Ignite; import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.IgniteException; import org.apache.ignite.IgniteException;
Expand Down Expand Up @@ -68,7 +67,6 @@
import org.apache.ignite.internal.processors.task.GridInternal; import org.apache.ignite.internal.processors.task.GridInternal;
import org.apache.ignite.internal.util.GridSpinBusyLock; import org.apache.ignite.internal.util.GridSpinBusyLock;
import org.apache.ignite.internal.util.future.GridFutureAdapter; import org.apache.ignite.internal.util.future.GridFutureAdapter;
import org.apache.ignite.internal.util.typedef.C1;
import org.apache.ignite.internal.util.typedef.F; import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.internal.util.typedef.T2; import org.apache.ignite.internal.util.typedef.T2;
import org.apache.ignite.internal.util.typedef.internal.A; import org.apache.ignite.internal.util.typedef.internal.A;
Expand Down Expand Up @@ -791,16 +789,18 @@ else if (val)


IgfsMode mode = resolveMode(path); IgfsMode mode = resolveMode(path);


Collection<String> files = new HashSet<>(); Collection<IgfsPath> files = new HashSet<>();


if (IgfsUtils.isDualMode(mode)) { if (IgfsUtils.isDualMode(mode)) {
assert secondaryFs != null; assert secondaryFs != null;


try { try {
Collection<IgfsPath> children = secondaryFs.listPaths(path); Collection<IgfsPath> children = secondaryFs.listPaths(path);


for (IgfsPath child : children) files.addAll(children);
files.add(child.name());
if (!modeRslvr.hasPrimaryChild(path))
return files;
} }
catch (Exception e) { catch (Exception e) {
U.error(log, "List paths in DUAL mode failed [path=" + path + ']', e); U.error(log, "List paths in DUAL mode failed [path=" + path + ']', e);
Expand All @@ -809,20 +809,17 @@ else if (val)
} }
} }


if (!IgfsUtils.isDualMode(mode) || modeRslvr.hasPrimaryChild(path)) { IgfsEntryInfo info = primaryInfoForListing(path);
IgniteUuid fileId = meta.fileId(path);


if (fileId != null) if (info != null) {
files.addAll(meta.directoryListing(fileId).keySet()); // Perform the listing.
else if (mode == PRIMARY) for (String child : info.listing().keySet())
throw new IgfsPathNotFoundException("Failed to list files (path not found): " + path); files.add(new IgfsPath(path, child));
} }
else if (mode == PRIMARY)
throw new IgfsPathNotFoundException("Failed to list paths (path not found): " + path);


return F.viewReadOnly(files, new C1<String, IgfsPath>() { return files;
@Override public IgfsPath apply(String e) {
return new IgfsPath(path, e);
}
});
} }
}); });
} }
Expand All @@ -841,7 +838,7 @@ else if (mode == PRIMARY)


IgfsMode mode = resolveMode(path); IgfsMode mode = resolveMode(path);


Set<IgfsFile> files = new HashSet<>(); Collection<IgfsFile> files = new HashSet<>();


if (IgfsUtils.isDualMode(mode)) { if (IgfsUtils.isDualMode(mode)) {
assert secondaryFs != null; assert secondaryFs != null;
Expand All @@ -865,27 +862,22 @@ else if (mode == PRIMARY)
} }
} }


IgniteUuid fileId = meta.fileId(path); IgfsEntryInfo info = primaryInfoForListing(path);

if (fileId != null) {
IgfsEntryInfo info = meta.info(fileId);


// Handle concurrent deletion. if (info != null) {
if (info != null) { if (info.isFile())
if (info.isFile()) // If this is a file, return its description.
// If this is a file, return its description. return Collections.<IgfsFile>singleton(new IgfsFileImpl(path, info,
return Collections.<IgfsFile>singleton(new IgfsFileImpl(path, info, data.groupBlockSize()));
data.groupBlockSize()));


// Perform the listing. // Perform the listing.
for (Map.Entry<String, IgfsListingEntry> e : info.listing().entrySet()) { for (Map.Entry<String, IgfsListingEntry> e : info.listing().entrySet()) {
IgfsEntryInfo childInfo = meta.info(e.getValue().fileId()); IgfsEntryInfo childInfo = meta.info(e.getValue().fileId());


if (childInfo != null) { if (childInfo != null) {
IgfsPath childPath = new IgfsPath(path, e.getKey()); IgfsPath childPath = new IgfsPath(path, e.getKey());


files.add(new IgfsFileImpl(childPath, childInfo, data.groupBlockSize())); files.add(new IgfsFileImpl(childPath, childInfo, data.groupBlockSize()));
}
} }
} }
} }
Expand All @@ -897,6 +889,19 @@ else if (mode == PRIMARY)
}); });
} }


/**
* Get primary file system info for listing operation.
*
* @param path Path.
* @return Info or {@code null} if not found.
* @throws IgniteCheckedException If failed.
*/
private IgfsEntryInfo primaryInfoForListing(IgfsPath path) throws IgniteCheckedException {
IgniteUuid fileId = meta.fileId(path);

return fileId != null ? meta.info(fileId) : null;
}

/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public long usedSpaceSize() { @Override public long usedSpaceSize() {
return metrics().localSpaceSize(); return metrics().localSpaceSize();
Expand Down
Expand Up @@ -400,12 +400,12 @@ public void testBasicOps() throws Exception {
igfs.delete(path("/A1/B1/C3"), false); igfs.delete(path("/A1/B1/C3"), false);
assertNull(igfs.info(path("/A1/B1/C3"))); assertNull(igfs.info(path("/A1/B1/C3")));


assertEquals(Collections.<IgfsPath>emptyList(), igfs.listPaths(path("/A1/B1"))); assertTrue(F.isEmpty(igfs.listPaths(path("/A1/B1"))));


igfs.delete(path("/A2/B2"), true); igfs.delete(path("/A2/B2"), true);
assertNull(igfs.info(path("/A2/B2"))); assertNull(igfs.info(path("/A2/B2")));


assertEquals(Collections.<IgfsPath>emptyList(), igfs.listPaths(path("/A2"))); assertTrue(F.isEmpty(igfs.listPaths(path("/A2"))));


assertEquals(Arrays.asList(path("/A"), path("/A1"), path("/A2")), sorted(igfs.listPaths(path("/")))); assertEquals(Arrays.asList(path("/A"), path("/A1"), path("/A2")), sorted(igfs.listPaths(path("/"))));


Expand All @@ -416,13 +416,14 @@ public void testBasicOps() throws Exception {
igfs.delete(path("/A"), true); igfs.delete(path("/A"), true);
igfs.delete(path("/A1"), true); igfs.delete(path("/A1"), true);
igfs.delete(path("/A2"), true); igfs.delete(path("/A2"), true);
assertEquals(Collections.<IgfsPath>emptyList(), igfs.listPaths(path("/")));
assertTrue(F.isEmpty(igfs.listPaths(path("/"))));


// Delete root when it is empty: // Delete root when it is empty:
igfs.delete(path("/"), false); igfs.delete(path("/"), false);
igfs.delete(path("/"), true); igfs.delete(path("/"), true);


assertEquals(Collections.<IgfsPath>emptyList(), igfs.listPaths(path("/"))); assertTrue(F.isEmpty(igfs.listPaths(path("/"))));


for (Cache.Entry<Object, Object> e : metaCache) for (Cache.Entry<Object, Object> e : metaCache)
info("Entry in cache [key=" + e.getKey() + ", val=" + e.getValue() + ']'); info("Entry in cache [key=" + e.getKey() + ", val=" + e.getValue() + ']');
Expand Down Expand Up @@ -603,7 +604,7 @@ public Object call() throws Exception {
// Cleanup. // Cleanup.
igfs.format(); igfs.format();


assertEquals(Collections.<IgfsPath>emptyList(), igfs.listPaths(root)); assertTrue(F.isEmpty(igfs.listPaths(root)));
} }


/** /**
Expand Down
Expand Up @@ -198,7 +198,7 @@ public void testCreateFile() throws Exception {
long max = 100L * CFG_BLOCK_SIZE / WRITING_THREADS_CNT; long max = 100L * CFG_BLOCK_SIZE / WRITING_THREADS_CNT;


for (long size = 0; size <= max; size = size * 15 / 10 + 1) { for (long size = 0; size <= max; size = size * 15 / 10 + 1) {
assertEquals(Collections.<IgfsPath>emptyList(), fs.listPaths(root)); assertTrue(F.isEmpty(fs.listPaths(root)));


testCreateFile(path, size, new Random().nextInt()); testCreateFile(path, size, new Random().nextInt());
} }
Expand Down

0 comments on commit bf9371a

Please sign in to comment.