nemo-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-nemo] taegeonum commented on a change in pull request #213: [NEMO-383] Implement DirectByteBufferOutputStream for Off-heap SerializedMemoryStore
Date Sun, 14 Apr 2019 05:25:22 GMT
taegeonum commented on a change in pull request #213: [NEMO-383] Implement DirectByteBufferOutputStream
for Off-heap SerializedMemoryStore
URL: https://github.com/apache/incubator-nemo/pull/213#discussion_r275140975
 
 

 ##########
 File path: common/src/main/java/org/apache/nemo/common/DirectByteBufferOutputStream.java
 ##########
 @@ -0,0 +1,163 @@
+/*
+ * 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.nemo.common;
+
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.util.LinkedList;
+
+/**
+ * This class is a customized output stream implementation backed by
+ * {@link ByteBuffer}, which utilizes off heap memory when writing the data.
+ * Memory is allocated when needed by the specified {@code pageSize}.
+ */
+public class DirectByteBufferOutputStream extends OutputStream {
+
+  private final LinkedList<ByteBuffer> dataList = new LinkedList<>();
+  private final int pageSize;
+
+  /**
+   * Default constructor.
+   * Sets the {@code pageSize} as default size of 4KB.
+   */
+  public DirectByteBufferOutputStream() {
+    pageSize = 4096;
+  }
+
+  /**
+   * Constructor specifying the {@code pageSize}.
+   *
+   * @param size should be a power of 2 and greater than 4KB.
+   */
+  public DirectByteBufferOutputStream(final int size) {
+    if (size < 4096 || (size & (size - 1)) != 0) {
+      throw new IllegalArgumentException("Invalid pageSize");
+    }
+    pageSize = size;
+  }
+
+  /**
+   * Allocates new {@link ByteBuffer} with the capacity equal to
+   * {@code pageSize}.
+   */
+  private void newLastBuffer() {
+    dataList.addLast(ByteBuffer.allocateDirect(pageSize));
+  }
+
+  /**
+   * Writes the specified byte to this output stream.
+   *
+   * @param   b   the byte to be written.
+   */
+  @Override
+  public void write(final int b) {
+    ByteBuffer currentBuf = (dataList.isEmpty() ? null : dataList.getLast());
 
 Review comment:
   Also, it would be good to define the currentBuf as an instance variable

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

Mime
View raw message