samperson1997 commented on a change in pull request #1384:
URL: https://github.com/apache/incubator-iotdb/pull/1384#discussion_r443369289



##########
File path: server/src/main/java/org/apache/iotdb/db/metadata/MTree.java
##########
@@ -899,6 +918,69 @@ private void findNodes(MNode node, String path, 
List<String> res, int targetLeve
     }
   }
 
+  public int getSnapshotLineNumber() {
+    return snapshotLineNumber;
+  }
+
+  public void serializeTo(String snapshotPath, int lineNumber) throws 
IOException {
+    try (BufferedWriter bw = new BufferedWriter(
+        new FileWriter(SystemFileFactory.INSTANCE.getFile(snapshotPath)))) {
+      bw.write(String.valueOf(lineNumber));
+      bw.newLine();
+      root.serializeTo(bw);
+    }
+  }
+
+  public static MTree deserializeFrom(File mtreeSnapshot) {
+    try (BufferedReader br = new BufferedReader(new 
FileReader(mtreeSnapshot))) {
+      int snapshotLineNumber = Integer.parseInt(br.readLine());
+      String s;
+      Deque<MNode> nodeStack = new ArrayDeque<>();
+      MNode node = null;
+
+      while ((s = br.readLine()) != null) {
+        String[] nodeInfo = s.split(",");
+        short nodeType = Short.parseShort(nodeInfo[0]);
+        if (nodeType == MetadataConstant.STORAGE_GROUP_MNODE_TYPE) {
+          node = StorageGroupMNode.deserializeFrom(nodeInfo);
+        } else if (nodeType == MetadataConstant.MEASUREMENT_MNODE_TYPE) {
+          node = MeasurementMNode.deserializeFrom(nodeInfo);
+        } else {
+          node = new MNode(null, nodeInfo[1]);
+        }
+
+        int childrenSize = Integer.parseInt(nodeInfo[nodeInfo.length - 1]);
+        if (childrenSize == 0) {
+          nodeStack.push(node);
+        } else {
+          Map<String, MNode> childrenMap = new LinkedHashMap<>();
+          for (int i = 0; i < childrenSize; i++) {
+            MNode child = nodeStack.removeFirst();
+            child.setParent(node);
+            childrenMap.put(child.getName(), child);
+            if (child instanceof MeasurementMNode) {
+              String alias = ((MeasurementMNode) child).getAlias();
+              if (alias != null) {
+                node.addAlias(alias, child);
+              }
+            }
+          }
+          node.setChildren(childrenMap);
+          nodeStack.push(node);
+        }
+      }
+      return new MTree(node, snapshotLineNumber);
+    } catch (IOException e) {
+      logger.warn("Failed to deserialize from {}. Use a new MTree.", 
mtreeSnapshot.getPath());
+      return new MTree();

Review comment:
       Sure, the logic is in `MManager`: 
   ```
      if (!mtreeSnapshot.exists()) {
         mtree = new MTree();
       } else {
         mtree = MTree.deserializeFrom(mtreeSnapshot);
       }
   ```
   We could always ensure the correctness of `.snapshot` file since we have 
`.snapshot.tmp` file as a "buffer" file 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to