Java Code Examples for org.apache.flink.core.memory.MemorySegment#copyTo()

The following examples show how to use org.apache.flink.core.memory.MemorySegment#copyTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractBinaryWriter.java    From flink with Apache License 2.0 6 votes vote down vote up
private void writeMultiSegmentsToVarLenPart(MemorySegment[] segments, int offset, int size) {
	// Write the bytes to the variable length portion.
	int needCopy = size;
	int fromOffset = offset;
	int toOffset = cursor;
	for (MemorySegment sourceSegment : segments) {
		int remain = sourceSegment.size() - fromOffset;
		if (remain > 0) {
			int copySize = remain > needCopy ? needCopy : remain;
			sourceSegment.copyTo(fromOffset, segment, toOffset, copySize);
			needCopy -= copySize;
			toOffset += copySize;
			fromOffset = 0;
		} else {
			fromOffset -= sourceSegment.size();
		}
	}
}
 
Example 2
Source File: AbstractBinaryWriter.java    From flink with Apache License 2.0 6 votes vote down vote up
private void writeMultiSegmentsToVarLenPart(MemorySegment[] segments, int offset, int size) {
	// Write the bytes to the variable length portion.
	int needCopy = size;
	int fromOffset = offset;
	int toOffset = cursor;
	for (MemorySegment sourceSegment : segments) {
		int remain = sourceSegment.size() - fromOffset;
		if (remain > 0) {
			int copySize = remain > needCopy ? needCopy : remain;
			sourceSegment.copyTo(fromOffset, segment, toOffset, copySize);
			needCopy -= copySize;
			toOffset += copySize;
			fromOffset = 0;
		} else {
			fromOffset -= sourceSegment.size();
		}
	}
}
 
Example 3
Source File: AbstractPagedOutputView.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void write(MemorySegment segment, int off, int len) throws IOException {
	int remaining = this.segmentSize - this.positionInSegment;
	if (remaining >= len) {
		segment.copyTo(off, currentSegment, positionInSegment, len);
		this.positionInSegment += len;
	} else {

		if (remaining == 0) {
			advance();
			remaining = this.segmentSize - this.positionInSegment;
		}

		while (true) {
			int toPut = Math.min(remaining, len);
			segment.copyTo(off, currentSegment, positionInSegment, toPut);
			off += toPut;
			len -= toPut;

			if (len > 0) {
				this.positionInSegment = this.segmentSize;
				advance();
				remaining = this.segmentSize - this.positionInSegment;
			}
			else {
				this.positionInSegment += toPut;
				break;
			}
		}
	}
}
 
Example 4
Source File: SkipListKeySerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Gets serialized key and namespace from the byte buffer.
 *
 * @param memorySegment the memory segment which stores the skip list key.
 * @param offset     the start position of the skip list key in the byte buffer.
 * @return tuple of serialized key and namespace.
 */
Tuple2<byte[], byte[]> getSerializedKeyAndNamespace(MemorySegment memorySegment, int offset) {
	// read namespace
	int namespaceLen = memorySegment.getInt(offset);
	MemorySegment namespaceSegment = MemorySegmentFactory.allocateUnpooledSegment(namespaceLen);
	memorySegment.copyTo(offset + Integer.BYTES, namespaceSegment, 0, namespaceLen);

	// read key
	int keyOffset = offset + Integer.BYTES + namespaceLen;
	int keyLen = memorySegment.getInt(keyOffset);
	MemorySegment keySegment = MemorySegmentFactory.allocateUnpooledSegment(keyLen);
	memorySegment.copyTo(keyOffset + Integer.BYTES, keySegment, 0, keyLen);

	return Tuple2.of(keySegment.getArray(), namespaceSegment.getArray());
}
 
Example 5
Source File: CopyOnWriteSkipListStateMap.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the byte array of serialized state.
 *
 * @param valuePointer pointer to value.
 * @return byte array of serialized value.
 */
byte[] helpGetBytesForState(long valuePointer) {
	Node node = getNodeSegmentAndOffset(valuePointer);
	MemorySegment segment = node.nodeSegment;
	int offsetInSegment = node.nodeOffset;

	int valueLen = SkipListUtils.getValueLen(segment, offsetInSegment);
	MemorySegment valueSegment = MemorySegmentFactory.allocateUnpooledSegment(valueLen);
	segment.copyTo(offsetInSegment + SkipListUtils.getValueMetaLen(), valueSegment, 0 , valueLen);

	return valueSegment.getArray();
}
 
Example 6
Source File: CopyOnWriteSkipListStateMap.java    From flink with Apache License 2.0 5 votes vote down vote up
private void setKeySegment(long node) {
	Node nodeStorage = getNodeSegmentAndOffset(node);
	MemorySegment segment = nodeStorage.nodeSegment;
	int offsetInSegment = nodeStorage.nodeOffset;

	int level = SkipListUtils.getLevel(segment, offsetInSegment);
	int keyLen = SkipListUtils.getKeyLen(segment, offsetInSegment);
	int keyDataOffset = offsetInSegment + SkipListUtils.getKeyDataOffset(level);

	MemorySegment nextKeySegment = MemorySegmentFactory.allocateUnpooledSegment(keyLen);
	segment.copyTo(keyDataOffset, nextKeySegment, 0, keyLen);
	this.nextKeySegment = nextKeySegment;
	nextKeyOffset = 0;
}
 
Example 7
Source File: AbstractPagedOutputView.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void write(MemorySegment segment, int off, int len) throws IOException {
	int remaining = this.segmentSize - this.positionInSegment;
	if (remaining >= len) {
		segment.copyTo(off, currentSegment, positionInSegment, len);
		this.positionInSegment += len;
	} else {

		if (remaining == 0) {
			advance();
			remaining = this.segmentSize - this.positionInSegment;
		}

		while (true) {
			int toPut = Math.min(remaining, len);
			segment.copyTo(off, currentSegment, positionInSegment, toPut);
			off += toPut;
			len -= toPut;

			if (len > 0) {
				this.positionInSegment = this.segmentSize;
				advance();
				remaining = this.segmentSize - this.positionInSegment;
			}
			else {
				this.positionInSegment += toPut;
				break;
			}
		}
	}
}
 
Example 8
Source File: AbstractBinaryWriter.java    From flink with Apache License 2.0 4 votes vote down vote up
public void write(MemorySegment seg, int off, int len) throws IOException {
	ensureCapacity(len);
	seg.copyTo(off, segment, cursor, len);
	cursor += len;
}
 
Example 9
Source File: AbstractBinaryWriter.java    From flink with Apache License 2.0 4 votes vote down vote up
public void write(MemorySegment seg, int off, int len) throws IOException {
	ensureCapacity(len);
	seg.copyTo(off, segment, cursor, len);
	cursor += len;
}
 
Example 10
Source File: SkipListUtils.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Puts the key data into key space.
 *
 * @param segment memory segment for key space.
 * @param offset offset of key space in memory segment.
 * @param keySegment memory segment for key data.
 * @param keyOffset offset of key data in memory segment.
 * @param keyLen length of key data.
 * @param level level of the key.
 */
public static void putKeyData(
	MemorySegment segment, int offset, MemorySegment keySegment, int keyOffset, int keyLen, int level) {
	keySegment.copyTo(keyOffset, segment, offset + getKeyDataOffset(level), keyLen);
}
 
Example 11
Source File: SkipListUtils.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Puts the value data into value space.
 *
 * @param memorySegment memory segment for value space.
 * @param offset offset of value space in memory segment.
 * @param value value data.
 */
public static void putValueData(MemorySegment memorySegment, int offset, byte[] value) {
	MemorySegment valueSegment = MemorySegmentFactory.wrap(value);
	valueSegment.copyTo(0, memorySegment, offset + getValueMetaLen(), value.length);
}