You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2020/03/24 02:37:35 UTC

[GitHub] [incubator-iotdb] fanhualta commented on a change in pull request #935: Integrate data file version recording with time partitioning

fanhualta commented on a change in pull request #935: Integrate data file version recording with time partitioning
URL: https://github.com/apache/incubator-iotdb/pull/935#discussion_r396863018
 
 

 ##########
 File path: server/src/main/java/org/apache/iotdb/db/engine/storagegroup/StorageGroupProcessor.java
 ##########
 @@ -1638,12 +1646,98 @@ public void loadNewTsFile(TsFileResource newTsFileResource)
     }
   }
 
+  /**
+   * Find the position of "newTsFileResource" in the sequence files if it can be inserted into them.
+   * @param newTsFileResource
+   * @return POS_ALREADY_EXIST(-2) if some file has the same name as the one to be inserted
+   *         POS_OVERLAP(-3) if some file overlaps the new file
+   *         an insertion position i >= -1 if the new file can be inserted between [i, i+1]
+   */
+  private int findInsertionPosition(TsFileResource newTsFileResource) {
+    File tsfileToBeInserted = newTsFileResource.getFile();
+    long newFilePartitionId = Long.parseLong(tsfileToBeInserted.getParent());
+    int insertPos = -1;
+
+    List<TsFileResource> sequenceList = new ArrayList<>(sequenceFileTreeSet);
+    // find the position where the new file should be inserted
+    for (int i = 0; i < sequenceList.size(); i++) {
+      TsFileResource localFile = sequenceList.get(i);
+      if (localFile.getFile().getName().equals(tsfileToBeInserted.getName())) {
+        return POS_ALREADY_EXIST;
+      }
+      long localPartitionId = Long.parseLong(localFile.getFile().getParent());
+      if (i == sequenceList.size() - 1 && localFile.getEndTimeMap().isEmpty()
+          || newFilePartitionId != localPartitionId) {
+        // skip files that are not in the partition as the new file and the last empty file
+        continue;
+      }
+
+      int fileComparison = compareTsFileDevices(newTsFileResource, localFile);
+      switch (fileComparison) {
+        case 0:
+          // some devices are newer but some devices are older, the two files overlap in general
+          return POS_OVERLAP;
+        case -1:
+          // all devices in the local file are newer than the new file, the new file can be
+          // inserted before the new file
+          return i - 1;
+        default:
+          // all devices in the local file are older than the new file, proceed to the next file
+          insertPos = i;
+      }
+    }
+    return insertPos;
+  }
+
+  /**
+   * Compare each device in the two files to find the time relation of them.
+   * @param fileA
+   * @param fileB
+   * @return -1 if fileA is totally older than fileB (A < B)
+   *          0 if fileA is partially older than fileB and partially newer than fileB (A X B)
+   *          1 if fileA is totally newer than fileB (B < A)
+   */
+  private int compareTsFileDevices(TsFileResource fileA, TsFileResource fileB) {
+    boolean hasPre = false, hasSubsequence = false;
+    for (String device : fileA.getStartTimeMap().keySet()) {
+      if (!fileB.getStartTimeMap().containsKey(device)) {
+        continue;
+      }
+      long startTimeA = fileA.getStartTimeMap().get(device);
+      long endTimeA = fileA.getEndTimeMap().get(device);
+      long startTimeB = fileB.getStartTimeMap().get(device);
+      long endTimeB = fileB.getEndTimeMap().get(device);
+      if (startTimeA > endTimeB) {
+        // A's data of the device is later than to the B's data
+        hasSubsequence = true;
+      } else if (startTimeB > endTimeA) {
+        // A's data of the device is previous to the B's data
+        hasPre = true;
+      } else {
+        // the two files overlap in the device
+        return 0;
+      }
+    }
+    if (hasPre && hasSubsequence) {
+      // some devices are newer but some devices are older, the two files overlap in general
+      return 0;
+    }
+    if (!hasPre && hasSubsequence) {
+      // all devices in B are newer than those in A
 
 Review comment:
   All devices in A are newer than those in B.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services