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

[incubator-iotdb] 01/01: update

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

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

commit 2a989ae80b511f895eb83ed27c468e72a6b14106
Author: mdf369 <95...@qq.com>
AuthorDate: Mon Apr 1 19:24:33 2019 +0800

    update
---
 .../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   | 39 ++++++++++++++++++++++
 .../iotdb/db/metadata/MManagerBasicTest.java       | 25 ++++++++++++++
 .../org/apache/iotdb/db/metadata/MTreeTest.java    | 29 ++++++++++++++++
 5 files changed, 117 insertions(+)

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 14c94fb..469ef8a 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
@@ -294,6 +294,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 c3f07c6..2aa9138 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
@@ -683,6 +683,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 2668619..5358ea8 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";
@@ -546,6 +547,44 @@ 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());
+      }
+      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 1316bd3..a560816 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
@@ -287,4 +287,29 @@ public class MManagerBasicTest {
       fail(e.getMessage());
     }
   }
+
+  @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());
+    }
+  }
 }
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 e4bed25..b06d993 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;
@@ -213,4 +217,29 @@ public class MTreeTest {
       fail(e.getMessage());
     }
   }
+
+  @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());
+    }
+  }
 }