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



##########
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:
       `return new ChunkHeaderSerializer().deserializeFrom(inputStream, 
markerRead);`. (avoid to generate a temporary object reference)

##########
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);

Review comment:
       code  style: `length += ReadW....`

##########
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:
       Better to add javadoc here to explain what the `options` (option) is.

##########
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:
       Besides, to keep consistent with `deserializeFrom(InputStream ...`, 
better to move the logic for handling the `offset` of the ByteBuffer in this 
implementation, rather than the `ChunkerHeader` class




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