You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vo...@apache.org on 2016/09/14 11:10:25 UTC

[09/50] ignite git commit: IGNITE-3651 IGFS: Implemented "usedSpaceSize()" operation for local secondary file system. This closes #1009.

IGNITE-3651 IGFS: Implemented "usedSpaceSize()" operation for local secondary file system. This closes #1009.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/f8ae6745
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/f8ae6745
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/f8ae6745

Branch: refs/heads/ignite-3661
Commit: f8ae67456703e63e3afc9bb5c21d81d576d59448
Parents: d06eaa23
Author: tledkov-gridgain <tl...@gridgain.com>
Authored: Sun Sep 4 17:09:08 2016 +0300
Committer: thatcoach <pp...@list.ru>
Committed: Sun Sep 4 17:09:08 2016 +0300

----------------------------------------------------------------------
 .../local/LocalIgfsSecondaryFileSystem.java     | 15 ++++-
 .../local/LocalFileSystemSizeVisitor.java       | 60 ++++++++++++++++++++
 ...SecondaryFileSystemDualAbstractSelfTest.java | 43 ++++++++++++++
 3 files changed, 117 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/f8ae6745/modules/core/src/main/java/org/apache/ignite/igfs/secondary/local/LocalIgfsSecondaryFileSystem.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/igfs/secondary/local/LocalIgfsSecondaryFileSystem.java b/modules/core/src/main/java/org/apache/ignite/igfs/secondary/local/LocalIgfsSecondaryFileSystem.java
index 519f472..36080f2 100644
--- a/modules/core/src/main/java/org/apache/ignite/igfs/secondary/local/LocalIgfsSecondaryFileSystem.java
+++ b/modules/core/src/main/java/org/apache/ignite/igfs/secondary/local/LocalIgfsSecondaryFileSystem.java
@@ -27,6 +27,7 @@ import org.apache.ignite.igfs.IgfsPathNotFoundException;
 import org.apache.ignite.igfs.secondary.IgfsSecondaryFileSystem;
 import org.apache.ignite.igfs.secondary.IgfsSecondaryFileSystemPositionedReadable;
 import org.apache.ignite.internal.processors.igfs.secondary.local.LocalFileSystemIgfsFile;
+import org.apache.ignite.internal.processors.igfs.secondary.local.LocalFileSystemSizeVisitor;
 import org.apache.ignite.internal.processors.igfs.secondary.local.LocalIgfsSecondaryFileSystemPositionedReadable;
 import org.apache.ignite.internal.util.typedef.F;
 import org.apache.ignite.internal.util.typedef.internal.U;
@@ -42,6 +43,7 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.nio.file.Files;
 import java.nio.file.LinkOption;
+import java.nio.file.Path;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.util.Collection;
 import java.util.Collections;
@@ -301,7 +303,18 @@ public class LocalIgfsSecondaryFileSystem implements IgfsSecondaryFileSystem, Li
 
     /** {@inheritDoc} */
     @Override public long usedSpaceSize() {
-        throw new UnsupportedOperationException("usedSpaceSize operation is not yet supported.");
+        Path p = fileForPath(new IgfsPath("/")).toPath();
+
+        try {
+            LocalFileSystemSizeVisitor visitor = new LocalFileSystemSizeVisitor();
+
+            Files.walkFileTree(p, visitor);
+
+            return visitor.size();
+        }
+        catch (IOException e) {
+            throw new IgfsException("Failed to calculate used space size.", e);
+        }
     }
 
     /** {@inheritDoc} */

http://git-wip-us.apache.org/repos/asf/ignite/blob/f8ae6745/modules/core/src/main/java/org/apache/ignite/internal/processors/igfs/secondary/local/LocalFileSystemSizeVisitor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/igfs/secondary/local/LocalFileSystemSizeVisitor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/igfs/secondary/local/LocalFileSystemSizeVisitor.java
new file mode 100644
index 0000000..222b749
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/igfs/secondary/local/LocalFileSystemSizeVisitor.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.igfs.secondary.local;
+
+import java.io.IOException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.FileVisitor;
+import java.nio.file.Path;
+import java.nio.file.attribute.BasicFileAttributes;
+
+/**
+ * File visitor to get occupied file system size.
+ */
+public class LocalFileSystemSizeVisitor implements FileVisitor<Path> {
+    /** File size accumulator. */
+    private long size;
+
+    /** {@inheritDoc} */
+    @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
+        return FileVisitResult.CONTINUE;
+    }
+
+    /** {@inheritDoc} */
+    @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+        size += attrs.size();
+        return FileVisitResult.CONTINUE;
+    }
+
+    /** {@inheritDoc} */
+    @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
+        return FileVisitResult.CONTINUE;
+    }
+
+    /** {@inheritDoc} */
+    @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
+        return FileVisitResult.CONTINUE;
+    }
+
+    /**
+     * @return Total size of visited files.
+     */
+    public long size() {
+        return size;
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/f8ae6745/modules/core/src/test/java/org/apache/ignite/internal/processors/igfs/IgfsLocalSecondaryFileSystemDualAbstractSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/igfs/IgfsLocalSecondaryFileSystemDualAbstractSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/igfs/IgfsLocalSecondaryFileSystemDualAbstractSelfTest.java
index 1d1ce8d..df7d782 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/igfs/IgfsLocalSecondaryFileSystemDualAbstractSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/igfs/IgfsLocalSecondaryFileSystemDualAbstractSelfTest.java
@@ -24,6 +24,7 @@ import org.apache.ignite.igfs.secondary.IgfsSecondaryFileSystem;
 import org.apache.ignite.igfs.secondary.local.LocalIgfsSecondaryFileSystem;
 import org.apache.ignite.internal.util.typedef.F;
 import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.lang.IgniteBiInClosure;
 import org.jetbrains.annotations.Nullable;
 
 import java.io.File;
@@ -32,6 +33,7 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.nio.file.Files;
 import java.util.Collection;
+import java.util.concurrent.atomic.AtomicLong;
 
 /**
  * Abstract test for Hadoop 1.0 file system stack.
@@ -170,6 +172,47 @@ public abstract class IgfsLocalSecondaryFileSystemDualAbstractSelfTest extends I
      *
      * @throws Exception If failed.
      */
+    public void testUsedSpaceSize() throws Exception {
+        final int DIRS_COUNT = 5;
+        final int DIRS_MAX_DEEP = 3;
+        final int FILES_COUNT = 10;
+        final AtomicLong totalSize = new AtomicLong();
+
+        IgniteBiInClosure<Integer, IgfsPath> createHierarchy = new IgniteBiInClosure<Integer, IgfsPath>() {
+            @Override public void apply(Integer level, IgfsPath levelDir) {
+                try {
+                    for (int i = 0; i < FILES_COUNT; ++i) {
+                        IgfsPath filePath = new IgfsPath(levelDir, "file" + Integer.toString(i));
+
+                        createFile(igfs, filePath, true, chunk);
+
+                        totalSize.getAndAdd(chunk.length);
+                    }
+
+                    if (level < DIRS_MAX_DEEP) {
+                        for (int dir = 0; dir < DIRS_COUNT; dir++) {
+                            IgfsPath dirPath = new IgfsPath(levelDir, "dir" + Integer.toString(dir));
+
+                            igfs.mkdirs(dirPath);
+
+                            apply(level + 1, dirPath);
+                        }
+                    }
+                } catch (Exception e) {
+                    fail(e.getMessage());
+                }
+            }
+        };
+
+        createHierarchy.apply(1, new IgfsPath("/dir"));
+
+        assertEquals(totalSize.get(), igfs.metrics().secondarySpaceSize());
+    }
+
+    /**
+     *
+     * @throws Exception If failed.
+     */
     private void createSymlinks() throws Exception {
         assert dirLinkDest.mkdir();