You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2018/04/30 09:43:26 UTC

[GitHub] ivankelly commented on a change in pull request #1669: PIP-17: provide DataBlockHeader and implementation

ivankelly commented on a change in pull request #1669: PIP-17: provide DataBlockHeader and implementation
URL: https://github.com/apache/incubator-pulsar/pull/1669#discussion_r184942331
 
 

 ##########
 File path: pulsar-broker/src/main/java/org/apache/pulsar/broker/s3offload/impl/DataBlockHeaderImpl.java
 ##########
 @@ -0,0 +1,110 @@
+/**
+ * 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.pulsar.broker.s3offload.impl;
+
+import static com.google.common.base.Preconditions.checkState;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufInputStream;
+import io.netty.buffer.PooledByteBufAllocator;
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import org.apache.pulsar.broker.s3offload.DataBlockHeader;
+
+/**
+ *
+ * The data block header in code storage for each data block.
+ *
+ */
+public class DataBlockHeaderImpl implements DataBlockHeader {
+    // Magic Word for data block.
+    // It is a sequence of bytes used to identify the start of a block.
+    private static final int dataBlockMagicWord = 0xDBDBDBDB;
+    // This is bigger than header size. Leaving some place for alignment and future enhancement.
+    // Payload use this as the start offset.
+    private static final int dataBlockHeaderAlign = 128;
+    // The size of this header.
+    private static final int dataBlockHeaderSize = 4 /* magic word */
+        + 4 /* index block length */
+        + 8 /* first entry id */;
+
+    public static DataBlockHeaderImpl of(int blockLength, long firstEntryId) {
+        return new DataBlockHeaderImpl(blockLength, firstEntryId);
+    }
+
+    // Construct DataBlockHeader from InputStream
+    public static DataBlockHeaderImpl fromStream(InputStream stream) throws IOException {
+        DataInputStream dis = new DataInputStream(stream);
+        int magic = dis.readInt();
+        checkState(magic == dataBlockMagicWord);
+        return new DataBlockHeaderImpl(dis.readInt(), dis.readLong());
 
 Review comment:
   When reading the header from a stream, you want to move the input stream read cursor past the padding also. The user of data block header should not be aware that there is padding, and that the data only takes up part of the header. Instead, they should pass the turn a header into an input stream and get a stream that returns _dataBlockHeaderAlign_ bytes. They should pass an input stream into this method, and this method should read _dataBlockHeaderAlign_ bytes and return a DataBlockHeader object, even if only 4 of the bytes read are useful for building the DataBlockHeader object.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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