You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ku...@apache.org on 2019/07/04 06:56:23 UTC

[flink] 01/02: [FLINK-11775][runtime] Introduce MemorySegmentWritable

This is an automated email from the ASF dual-hosted git repository.

kurt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 99a94edf543fb28ccc8e34b886a67763f328b79c
Author: JingsongLi <lz...@aliyun.com>
AuthorDate: Tue Jun 18 15:46:37 2019 +0800

    [FLINK-11775][runtime] Introduce MemorySegmentWritable
---
 .../flink/core/memory/DataOutputSerializer.java    | 14 +++++++-
 .../flink/core/memory/MemorySegmentWritable.java   | 41 ++++++++++++++++++++++
 .../runtime/memory/AbstractPagedOutputView.java    |  4 ++-
 3 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/flink-core/src/main/java/org/apache/flink/core/memory/DataOutputSerializer.java b/flink-core/src/main/java/org/apache/flink/core/memory/DataOutputSerializer.java
index b749c84..85fd767 100644
--- a/flink-core/src/main/java/org/apache/flink/core/memory/DataOutputSerializer.java
+++ b/flink-core/src/main/java/org/apache/flink/core/memory/DataOutputSerializer.java
@@ -33,7 +33,7 @@ import java.util.Arrays;
 /**
  * A simple and efficient serializer for the {@link java.io.DataOutput} interface.
  */
-public class DataOutputSerializer implements DataOutputView {
+public class DataOutputSerializer implements DataOutputView, MemorySegmentWritable {
 
 	private static final Logger LOG = LoggerFactory.getLogger(DataOutputSerializer.class);
 
@@ -156,6 +156,18 @@ public class DataOutputSerializer implements DataOutputView {
 	}
 
 	@Override
+	public void write(MemorySegment segment, int off, int len) throws IOException {
+		if (len < 0 || off > segment.size() - len) {
+			throw new ArrayIndexOutOfBoundsException();
+		}
+		if (this.position > this.buffer.length - len) {
+			resize(len);
+		}
+		segment.get(off, this.buffer, this.position, len);
+		this.position += len;
+	}
+
+	@Override
 	public void writeBoolean(boolean v) throws IOException {
 		write(v ? 1 : 0);
 	}
diff --git a/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegmentWritable.java b/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegmentWritable.java
new file mode 100644
index 0000000..680178f
--- /dev/null
+++ b/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegmentWritable.java
@@ -0,0 +1,41 @@
+/*
+ * 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.flink.core.memory;
+
+import org.apache.flink.annotation.Internal;
+
+import java.io.IOException;
+
+/**
+ * Provides the interface for write(Segment).
+ */
+@Internal
+public interface MemorySegmentWritable {
+
+	/**
+	 * Writes {@code len} bytes from memory segment {@code segment} starting at offset {@code off}, in order,
+	 * to the output.
+	 *
+	 * @param segment memory segment to copy the bytes from.
+	 * @param off the start offset in the memory segment.
+	 * @param len The number of bytes to copy.
+	 * @throws IOException if an I/O error occurs.
+	 */
+	void write(MemorySegment segment, int off, int len) throws IOException;
+}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/memory/AbstractPagedOutputView.java b/flink-runtime/src/main/java/org/apache/flink/runtime/memory/AbstractPagedOutputView.java
index 7451d54..80be658 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/memory/AbstractPagedOutputView.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/memory/AbstractPagedOutputView.java
@@ -21,6 +21,7 @@ package org.apache.flink.runtime.memory;
 import org.apache.flink.core.memory.DataInputView;
 import org.apache.flink.core.memory.DataOutputView;
 import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.core.memory.MemorySegmentWritable;
 
 import java.io.IOException;
 import java.io.UTFDataFormatException;
@@ -33,7 +34,7 @@ import java.io.UTFDataFormatException;
  *
  * <p>The paging assumes that all memory segments are of the same size.
  */
-public abstract class AbstractPagedOutputView implements DataOutputView {
+public abstract class AbstractPagedOutputView implements DataOutputView, MemorySegmentWritable {
 
 	private MemorySegment currentSegment;			// the current memory segment to write to
 
@@ -416,6 +417,7 @@ public abstract class AbstractPagedOutputView implements DataOutputView {
 		}
 	}
 
+	@Override
 	public void write(MemorySegment segment, int off, int len) throws IOException {
 		int remaining = this.segmentSize - this.positionInSegment;
 		if (remaining >= len) {