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

marklau99 pushed a commit to branch IOTDB-4251
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/IOTDB-4251 by this push:
     new c99d54ac80 refactor some codes
c99d54ac80 is described below

commit c99d54ac80da0bfce55e7d5829f1e6aefc941112
Author: Liu Xuxin <[email protected]>
AuthorDate: Tue Sep 6 17:22:23 2022 +0800

    refactor some codes
---
 .../file/metadata/MetadataIndexConstructor.java    |  5 +++
 .../write/writer/MemoryControlTsFileIOWriter.java  | 49 +++++-----------------
 2 files changed, 15 insertions(+), 39 deletions(-)

diff --git 
a/tsfile/src/main/java/org/apache/iotdb/tsfile/file/metadata/MetadataIndexConstructor.java
 
b/tsfile/src/main/java/org/apache/iotdb/tsfile/file/metadata/MetadataIndexConstructor.java
index beb9ddb3e5..44cdc8b0bf 100644
--- 
a/tsfile/src/main/java/org/apache/iotdb/tsfile/file/metadata/MetadataIndexConstructor.java
+++ 
b/tsfile/src/main/java/org/apache/iotdb/tsfile/file/metadata/MetadataIndexConstructor.java
@@ -81,6 +81,11 @@ public class MetadataIndexConstructor {
               measurementMetadataIndexQueue, out, 
MetadataIndexNodeType.INTERNAL_MEASUREMENT));
     }
 
+    return checkAndBuildLevelIndex(deviceMetadataIndexMap, out);
+  }
+
+  public static MetadataIndexNode checkAndBuildLevelIndex(
+      Map<String, MetadataIndexNode> deviceMetadataIndexMap, TsFileOutput out) 
throws IOException {
     // if not exceed the max child nodes num, ignore the device index and 
directly point to the
     // measurement
     if (deviceMetadataIndexMap.size() <= config.getMaxDegreeOfIndexNode()) {
diff --git 
a/tsfile/src/main/java/org/apache/iotdb/tsfile/write/writer/MemoryControlTsFileIOWriter.java
 
b/tsfile/src/main/java/org/apache/iotdb/tsfile/write/writer/MemoryControlTsFileIOWriter.java
index acd0ba1aa4..b8d8c7b768 100644
--- 
a/tsfile/src/main/java/org/apache/iotdb/tsfile/write/writer/MemoryControlTsFileIOWriter.java
+++ 
b/tsfile/src/main/java/org/apache/iotdb/tsfile/write/writer/MemoryControlTsFileIOWriter.java
@@ -53,6 +53,7 @@ import java.util.Queue;
 import java.util.TreeMap;
 
 import static 
org.apache.iotdb.tsfile.file.metadata.MetadataIndexConstructor.addCurrentIndexNodeToQueue;
+import static 
org.apache.iotdb.tsfile.file.metadata.MetadataIndexConstructor.checkAndBuildLevelIndex;
 import static 
org.apache.iotdb.tsfile.file.metadata.MetadataIndexConstructor.generateRootNode;
 
 public class MemoryControlTsFileIOWriter extends TsFileIOWriter {
@@ -198,6 +199,13 @@ public class MemoryControlTsFileIOWriter extends 
TsFileIOWriter {
 
     // read in the chunk metadata, and construct the index tree
     readChunkMetadataAndConstructIndexTree();
+
+    // write magic string
+    out.write(MAGIC_STRING_BYTES);
+
+    // close file
+    out.close();
+    canWrite = false;
   }
 
   private void readChunkMetadataAndConstructIndexTree() throws IOException {
@@ -223,6 +231,7 @@ public class MemoryControlTsFileIOWriter extends 
TsFileIOWriter {
     BloomFilter filter =
         BloomFilter.getEmptyBloomFilter(
             
TSFileDescriptor.getInstance().getConfig().getBloomFilterErrorRate(), 
pathCount);
+
     while (iterator.hasNextChunkMetadata()) {
       // read in all chunk metadata of one series
       // construct the timeseries metadata for this series
@@ -259,38 +268,7 @@ public class MemoryControlTsFileIOWriter extends 
TsFileIOWriter {
       timeseriesMetadata.serializeTo(out.wrapAsStream());
     }
 
-    MetadataIndexNode metadataIndex = null;
-    // if not exceed the max child nodes num, ignore the device index and 
directly point to the
-    // measurement
-    if (deviceMetadataIndexMap.size() <= config.getMaxDegreeOfIndexNode()) {
-      MetadataIndexNode metadataIndexNode =
-          new MetadataIndexNode(MetadataIndexNodeType.LEAF_DEVICE);
-      for (Map.Entry<String, MetadataIndexNode> entry : 
deviceMetadataIndexMap.entrySet()) {
-        metadataIndexNode.addEntry(new MetadataIndexEntry(entry.getKey(), 
out.getPosition()));
-        entry.getValue().serializeTo(out.wrapAsStream());
-      }
-      metadataIndexNode.setEndOffset(out.getPosition());
-      metadataIndex = metadataIndexNode;
-    } else {
-      // else, build level index for devices
-      Queue<MetadataIndexNode> deviceMetadataIndexQueue = new ArrayDeque<>();
-      currentIndexNode = new 
MetadataIndexNode(MetadataIndexNodeType.LEAF_DEVICE);
-
-      for (Map.Entry<String, MetadataIndexNode> entry : 
deviceMetadataIndexMap.entrySet()) {
-        // when constructing from internal node, each node is related to an 
entry
-        if (currentIndexNode.isFull()) {
-          addCurrentIndexNodeToQueue(currentIndexNode, 
deviceMetadataIndexQueue, out);
-          currentIndexNode = new 
MetadataIndexNode(MetadataIndexNodeType.LEAF_DEVICE);
-        }
-        currentIndexNode.addEntry(new MetadataIndexEntry(entry.getKey(), 
out.getPosition()));
-        entry.getValue().serializeTo(out.wrapAsStream());
-      }
-      addCurrentIndexNodeToQueue(currentIndexNode, deviceMetadataIndexQueue, 
out);
-      MetadataIndexNode deviceMetadataIndexNode =
-          generateRootNode(deviceMetadataIndexQueue, out, 
MetadataIndexNodeType.INTERNAL_DEVICE);
-      deviceMetadataIndexNode.setEndOffset(out.getPosition());
-      metadataIndex = deviceMetadataIndexNode;
-    }
+    MetadataIndexNode metadataIndex = 
checkAndBuildLevelIndex(deviceMetadataIndexMap, out);
 
     TsFileMetadata tsFileMetadata = new TsFileMetadata();
     tsFileMetadata.setMetadataIndex(metadataIndex);
@@ -301,13 +279,6 @@ public class MemoryControlTsFileIOWriter extends 
TsFileIOWriter {
 
     // write TsFileMetaData size
     ReadWriteIOUtils.write(size, out.wrapAsStream());
-
-    // write magic string
-    out.write(MAGIC_STRING_BYTES);
-
-    // close file
-    out.close();
-    canWrite = false;
   }
 
   /**

Reply via email to