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/06/01 02:11:46 UTC

[GitHub] [incubator-iotdb] qiaojialin commented on a change in pull request #1293: [IOTDB-721]First steps for isolating serialization.

qiaojialin commented on a change in pull request #1293:
URL: https://github.com/apache/incubator-iotdb/pull/1293#discussion_r433014970



##########
File path: tsfile/src/main/java/org/apache/iotdb/tsfile/common/serialization/PageHeaderSerializer.java
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.common.serialization;
+import org.apache.iotdb.tsfile.file.header.PageHeader;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.file.metadata.statistics.Statistics;
+import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+
+public class PageHeaderSerializer implements IDataSerializer<PageHeader, TSDataType> {
+
+    @Override
+    public int serializeTo(PageHeader data, OutputStream outputStream) throws IOException {
+        int length = ReadWriteIOUtils.write(data.getUncompressedSize(), outputStream);
+        length+=ReadWriteIOUtils.write(data.getCompressedSize(), outputStream);
+        length+=data.getStatistics().serialize(outputStream);
+        return length;
+    }
+    @Override
+    public int serializeTo(PageHeader data, ByteBuffer buffer) {
+        int length = ReadWriteIOUtils.write(data.getUncompressedSize(), buffer);
+        length+=ReadWriteIOUtils.write(data.getCompressedSize(), buffer);
+        ByteArrayOutputStream stream = new ByteArrayOutputStream();
+        try {
+            length+=data.getStatistics().serialize(stream);
+        } catch (IOException ex) {
+            // i know but in this case never happens an exception.
+            return 0;
+        }
+        buffer.put(stream.toByteArray());
+        return length;
+    }
+
+    @Override
+    public PageHeader deserializeFrom(ByteBuffer buffer, TSDataType options) {

Review comment:
       If the options have some meaning, we'd better add some Javadoc for it

##########
File path: tsfile/src/main/java/org/apache/iotdb/tsfile/common/serialization/ChunkHeaderSerializer.java
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.common.serialization;
+import org.apache.iotdb.tsfile.file.MetaMarker;
+import org.apache.iotdb.tsfile.file.header.ChunkHeader;
+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.apache.iotdb.tsfile.utils.ReadWriteIOUtils;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+
+public class ChunkHeaderSerializer implements IDataSerializer<ChunkHeader, Boolean> {
+
+    @Override
+    public int serializeTo(ChunkHeader data, OutputStream outputStream) throws IOException {
+        int length = 0;
+        length += ReadWriteIOUtils.write(MetaMarker.CHUNK_HEADER, outputStream);
+        length += ReadWriteIOUtils.write(data.getMeasurementID(), outputStream);
+        length += ReadWriteIOUtils.write(data.getDataSize(), outputStream);
+        length += ReadWriteIOUtils.write(data.getDataType(), outputStream);
+        length += ReadWriteIOUtils.write(data.getNumOfPages(), outputStream);
+        length += ReadWriteIOUtils.write(data.getCompressionType(), outputStream);
+        length += ReadWriteIOUtils.write(data.getEncodingType(), outputStream);
+        return length;
+    }
+
+    @Override
+    public int serializeTo(ChunkHeader data, ByteBuffer buffer) {
+        int length = 0;
+        length += ReadWriteIOUtils.write(MetaMarker.CHUNK_HEADER, buffer);
+        length += ReadWriteIOUtils.write(data.getMeasurementID(), buffer);
+        length += ReadWriteIOUtils.write(data.getDataSize(), buffer);
+        length += ReadWriteIOUtils.write(data.getDataType(), buffer);
+        length += ReadWriteIOUtils.write(data.getNumOfPages(), buffer);
+        length += ReadWriteIOUtils.write(data.getCompressionType(), buffer);
+        length += ReadWriteIOUtils.write(data.getEncodingType(), buffer);
+        return length;
+    }
+
+    @Override
+    public ChunkHeader deserializeFrom(ByteBuffer buffer, Boolean options) {

Review comment:
       How about renaming the parameter option to markerRead

##########
File path: tsfile/src/main/java/org/apache/iotdb/tsfile/file/header/ChunkHeader.java
##########
@@ -76,20 +75,8 @@ public static int getSerializedSize(String measurementID) {
    * @param markerRead Whether the marker of the CHUNK_HEADER has been read
    */
   public static ChunkHeader deserializeFrom(InputStream inputStream, boolean markerRead) throws IOException {
-    if (!markerRead) {
-      byte marker = (byte) inputStream.read();
-      if (marker != MetaMarker.CHUNK_HEADER) {
-        MetaMarker.handleUnexpectedMarker(marker);
-      }
-    }
-
-    String measurementID = ReadWriteIOUtils.readString(inputStream);
-    int dataSize = ReadWriteIOUtils.readInt(inputStream);
-    TSDataType dataType = TSDataType.deserialize(ReadWriteIOUtils.readShort(inputStream));
-    int numOfPages = ReadWriteIOUtils.readInt(inputStream);
-    CompressionType type = ReadWriteIOUtils.readCompressionType(inputStream);
-    TSEncoding encoding = ReadWriteIOUtils.readEncoding(inputStream);
-    return new ChunkHeader(measurementID, dataSize, dataType, type, encoding, numOfPages);
+    ChunkHeaderSerializer serializer = new ChunkHeaderSerializer(); 
+    return serializer.deserializeFrom(inputStream, markerRead);

Review comment:
       Why not make the deserializeFrom a static method?




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