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 2019/03/07 07:41:03 UTC

[GitHub] [incubator-iotdb] JulianFeinauer commented on a change in pull request #87: [IOTDB-36]Enable recover data from a incomplete TsFile and continue to write

JulianFeinauer commented on a change in pull request #87: [IOTDB-36]Enable recover data from a incomplete TsFile and continue to write
URL: https://github.com/apache/incubator-iotdb/pull/87#discussion_r263265417
 
 

 ##########
 File path: tsfile/src/main/java/org/apache/iotdb/tsfile/write/writer/NativeRestorableIOWriter.java
 ##########
 @@ -0,0 +1,201 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.tsfile.write.writer;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.iotdb.tsfile.file.MetaMarker;
+import org.apache.iotdb.tsfile.file.footer.ChunkGroupFooter;
+import org.apache.iotdb.tsfile.file.header.ChunkHeader;
+import org.apache.iotdb.tsfile.file.header.PageHeader;
+import org.apache.iotdb.tsfile.file.metadata.ChunkGroupMetaData;
+import org.apache.iotdb.tsfile.file.metadata.ChunkMetaData;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TsFileSequenceReader;
+import org.apache.iotdb.tsfile.write.schema.MeasurementSchema;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * a restorable tsfile which do not depend on a restore file.
+ */
+public class NativeRestorableIOWriter extends TsFileIOWriter {
+
+  private static final Logger LOGGER = LoggerFactory
+      .getLogger(NativeRestorableIOWriter.class);
+
+  private long truncatedPosition = -1;
+  private Map<String, MeasurementSchema> knownSchemas = new HashMap<>();
+
+  public long getTruncatedPosition() {
+    return truncatedPosition;
+  }
+
+  public NativeRestorableIOWriter(File file) throws IOException {
+    super();
+    long fileSize;
+    if (!file.exists()) {
+      this.out = new DefaultTsFileOutput(file, true);
+      startFile();
+      return;
+    } else {
+      fileSize = file.length();
+      this.out = new DefaultTsFileOutput(file, true);
+    }
+
+    //we need to read data to recover TsFileIOWriter.chunkGroupMetaDataList
+    //and remove broken data if exists.
+
+    ChunkMetaData currentChunk;
+    String measurementID;
+    TSDataType dataType;
+    long fileOffsetOfChunk;
+    long startTimeOfChunk = 0;
+    long endTimeOfChunk = 0;
+    long numOfPoints = 0;
+
+    ChunkGroupMetaData currentChunkGroup;
+    List<ChunkMetaData> chunks = null;
+    String deviceID;
+    long startOffsetOfChunkGroup = 0;
+    long endOffsetOfChunkGroup;
+    long versionOfChunkGroup = 0;
+    boolean haveReadAnUnverifiedGroupFooter = false;
+    boolean newGroup = true;
+
+    TsFileSequenceReader reader = new TsFileSequenceReader(file.getAbsolutePath(), false);
+    if (fileSize <= magicStringBytes.length) {
+      LOGGER.debug("{} only has magic header, does not worth to recover.", file.getAbsolutePath());
+      reader.close();
+      this.out.truncate(0);
+      startFile();
+      truncatedPosition = magicStringBytes.length;
+      return;
+    }
+    if (reader.readTailMagic().equals(reader.readHeadMagic(true))) {
 
 Review comment:
   Perhaps we should check that there is at least the magic bytes in the header, so that this really is a tsfile?
   Otherwise othwer file types would lead to obscure Marker Exceptions, I guess.

----------------------------------------------------------------
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