You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by do...@apache.org on 2019/04/01 14:18:51 UTC

[incubator-iotdb] branch master updated: Add method getAllSGByFile() for MManager (#125)

This is an automated email from the ASF dual-hosted git repository.

dope pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new f83b601  Add method getAllSGByFile() for MManager (#125)
f83b601 is described below

commit f83b6014975de94c30434784d83787e00ddbb7f8
Author: DongFang Mao <95...@qq.com>
AuthorDate: Mon Apr 1 22:18:47 2019 +0800

    Add method getAllSGByFile() for MManager (#125)
---
 .../java/org/apache/iotdb/db/metadata/MGraph.java  |  7 ++++
 .../org/apache/iotdb/db/metadata/MManager.java     | 17 +++++++++
 .../java/org/apache/iotdb/db/metadata/MTree.java   | 40 ++++++++++++++++++++++
 .../iotdb/db/metadata/MManagerBasicTest.java       | 26 ++++++++++++++
 .../org/apache/iotdb/db/metadata/MTreeTest.java    | 29 +++++++++++++++-
 5 files changed, 118 insertions(+), 1 deletion(-)

diff --git a/iotdb/src/main/java/org/apache/iotdb/db/metadata/MGraph.java b/iotdb/src/main/java/org/apache/iotdb/db/metadata/MGraph.java
index 8b2bfdc..5cf2847 100644
--- a/iotdb/src/main/java/org/apache/iotdb/db/metadata/MGraph.java
+++ b/iotdb/src/main/java/org/apache/iotdb/db/metadata/MGraph.java
@@ -305,6 +305,13 @@ public class MGraph implements Serializable {
   }
 
   /**
+   * Get all file names for given seriesPath
+   */
+  public List<String> getAllFileNamesByPath(String path) throws PathErrorException {
+    return mtree.getAllFileNamesByPath(path);
+  }
+
+  /**
    * Check whether the seriesPath given exists.
    */
   public boolean pathExist(String path) {
diff --git a/iotdb/src/main/java/org/apache/iotdb/db/metadata/MManager.java b/iotdb/src/main/java/org/apache/iotdb/db/metadata/MManager.java
index 7384695..eef21ca 100644
--- a/iotdb/src/main/java/org/apache/iotdb/db/metadata/MManager.java
+++ b/iotdb/src/main/java/org/apache/iotdb/db/metadata/MManager.java
@@ -697,6 +697,23 @@ public class MManager {
   }
 
   /**
+   * Get all file names for given seriesPath
+   *
+   * @return List of String represented all file names
+   */
+  public List<String> getAllFileNamesByPath(String path) throws PathErrorException {
+
+    lock.readLock().lock();
+    try {
+      return mgraph.getAllFileNamesByPath(path);
+    } catch (PathErrorException e) {
+      throw new PathErrorException(e);
+    } finally {
+      lock.readLock().unlock();
+    }
+  }
+
+  /**
    * return a HashMap contains all the paths separated by File Name.
    */
   public Map<String, ArrayList<String>> getAllPathGroupByFileName(String path)
diff --git a/iotdb/src/main/java/org/apache/iotdb/db/metadata/MTree.java b/iotdb/src/main/java/org/apache/iotdb/db/metadata/MTree.java
index 5f65507..6444b4a 100644
--- a/iotdb/src/main/java/org/apache/iotdb/db/metadata/MTree.java
+++ b/iotdb/src/main/java/org/apache/iotdb/db/metadata/MTree.java
@@ -39,6 +39,7 @@ public class MTree implements Serializable {
 
   private static final long serialVersionUID = -4200394435237291964L;
   private static final String QUAD_SPACE = "    ";
+  private static final String SEPARATOR = ".";
   private static final String DOUB_SEPARATOR = "\\.";
   private static final String NO_CHILD_ERROR = "Timeseries is not correct. Node[%s] "
       + "doesn't have child named:%s";
@@ -579,6 +580,45 @@ public class MTree implements Serializable {
   }
 
   /**
+   * Get all the storage group seriesPaths for one seriesPath.
+   *
+   * @return List storage group seriesPath list
+   */
+  public List<String> getAllFileNamesByPath(String path) throws PathErrorException {
+
+    List<String> sgList = new ArrayList<>();
+    String[] nodes = path.split(DOUB_SEPARATOR);
+    MNode cur = getRoot();
+    for (int i = 1; i < nodes.length; i++) {
+      if (cur == null) {
+        throw new PathErrorException(
+            String.format(NOT_SERIES_PATH,
+                path));
+      }
+      if (cur.isStorageLevel()) {
+        sgList.add(cur.getDataFileName());
+        return sgList;
+      }
+      cur = cur.getChild(nodes[i]);
+    }
+    if (sgList.isEmpty()) {
+      getAllStorageGroupsOfNode(cur, path, sgList);
+    }
+    return sgList;
+  }
+
+  private void getAllStorageGroupsOfNode(MNode node, String path, List<String> sgList) {
+    if (node.isStorageLevel()) {
+      sgList.add(path);
+      return;
+    }
+
+    for (MNode child : node.getChildren().values()) {
+      getAllStorageGroupsOfNode(child, path + SEPARATOR + child.getName(), sgList);
+    }
+  }
+
+  /**
    * function for getting file name by path.
    */
   public String getFileNameByPath(MNode node, String path) throws PathErrorException {
diff --git a/iotdb/src/test/java/org/apache/iotdb/db/metadata/MManagerBasicTest.java b/iotdb/src/test/java/org/apache/iotdb/db/metadata/MManagerBasicTest.java
index bbc41d2..2133cc0 100644
--- a/iotdb/src/test/java/org/apache/iotdb/db/metadata/MManagerBasicTest.java
+++ b/iotdb/src/test/java/org/apache/iotdb/db/metadata/MManagerBasicTest.java
@@ -289,6 +289,32 @@ public class MManagerBasicTest {
   }
 
   @Test
+  public void testGetAllFileNamesByPath() {
+
+    MManager manager = MManager.getInstance();
+    try {
+      manager.setStorageLevelToMTree("root.laptop.d1");
+      manager.setStorageLevelToMTree("root.laptop.d2");
+      manager.addPathToMTree("root.laptop.d1.s1", TSDataType.INT32, TSEncoding.PLAIN,
+          CompressionType.GZIP, null);
+      manager.addPathToMTree("root.laptop.d1.s1", TSDataType.INT32, TSEncoding.PLAIN,
+          CompressionType.GZIP, null);
+
+      List<String> list = new ArrayList<>();
+
+      list.add("root.laptop.d1");
+      assertEquals(list, manager.getAllFileNamesByPath("root.laptop.d1.s1"));
+      assertEquals(list, manager.getAllFileNamesByPath("root.laptop.d1"));
+
+      list.add("root.laptop.d2");
+      assertEquals(list, manager.getAllFileNamesByPath("root.laptop"));
+      assertEquals(list, manager.getAllFileNamesByPath("root"));
+    } catch (PathErrorException | IOException | MetadataArgsErrorException e) {
+      e.printStackTrace();
+      fail(e.getMessage());
+    }
+  }
+
   public void testCheckStorageExistOfPath() {
     MManager manager = MManager.getInstance();
 
diff --git a/iotdb/src/test/java/org/apache/iotdb/db/metadata/MTreeTest.java b/iotdb/src/test/java/org/apache/iotdb/db/metadata/MTreeTest.java
index 6f8ac94..3e45d4b 100644
--- a/iotdb/src/test/java/org/apache/iotdb/db/metadata/MTreeTest.java
+++ b/iotdb/src/test/java/org/apache/iotdb/db/metadata/MTreeTest.java
@@ -24,8 +24,12 @@ import static org.junit.Assert.fail;
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import org.apache.iotdb.db.exception.PathErrorException;
 import org.apache.iotdb.db.utils.EnvironmentUtils;
+import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
@@ -215,6 +219,30 @@ public class MTreeTest {
   }
 
   @Test
+  public void testGetAllFileNamesByPath() {
+    // set storage group first
+    MTree root = new MTree("root");
+    try {
+      root.setStorageGroup("root.laptop.d1");
+      root.setStorageGroup("root.laptop.d2");
+      root.addTimeseriesPath("root.laptop.d1.s1", TSDataType.INT32, TSEncoding.PLAIN, CompressionType.GZIP, null);
+      root.addTimeseriesPath("root.laptop.d1.s1", TSDataType.INT32, TSEncoding.PLAIN, CompressionType.GZIP, null);
+
+      List<String> list = new ArrayList<>();
+
+      list.add("root.laptop.d1");
+      assertEquals(list, root.getAllFileNamesByPath("root.laptop.d1.s1"));
+      assertEquals(list, root.getAllFileNamesByPath("root.laptop.d1"));
+
+      list.add("root.laptop.d2");
+      assertEquals(list, root.getAllFileNamesByPath("root.laptop"));
+      assertEquals(list, root.getAllFileNamesByPath("root"));
+    } catch (PathErrorException e) {
+      e.printStackTrace();
+      fail(e.getMessage());
+    }
+  }
+
   public void testCheckStorageExistOfPath() {
     // set storage group first
     MTree root = new MTree("root");
@@ -241,5 +269,4 @@ public class MTreeTest {
       fail(e.getMessage());
     }
   }
-
 }