Search in sources :

Example 86 with MemorySegment

use of org.apache.flink.core.memory.MemorySegment in project flink by apache.

the class CopyOnWriteSkipListStateMap method doWriteValue.

/**
 * Write the meta and data for the value to the space where the value pointer points.
 *
 * @param valuePointer pointer to the space where the meta and data is written.
 * @param value data of the value.
 * @param version version of this value.
 * @param keyPointer pointer to the key.
 * @param nextValuePointer pointer to the next value.
 */
private void doWriteValue(long valuePointer, byte[] value, int version, long keyPointer, long nextValuePointer) {
    Node node = getNodeSegmentAndOffset(valuePointer);
    MemorySegment segment = node.nodeSegment;
    int offsetInSegment = node.nodeOffset;
    SkipListUtils.putValueVersion(segment, offsetInSegment, version);
    SkipListUtils.putKeyPointer(segment, offsetInSegment, keyPointer);
    SkipListUtils.putNextValuePointer(segment, offsetInSegment, nextValuePointer);
    SkipListUtils.putValueLen(segment, offsetInSegment, value == null ? 0 : value.length);
    if (value != null) {
        SkipListUtils.putValueData(segment, offsetInSegment, value);
    }
}
Also used : MemorySegment(org.apache.flink.core.memory.MemorySegment)

Example 87 with MemorySegment

use of org.apache.flink.core.memory.MemorySegment in project flink by apache.

the class SkipListUtils method compareSegmentAndNode.

/**
 * Compare the first skip list key in the given memory segment with the second skip list key in
 * the given node.
 *
 * @param keySegment memory segment storing the first key.
 * @param keyOffset offset of the first key in memory segment.
 * @param targetNode the node storing the second key.
 * @param spaceAllocator the space allocator.
 * @return Returns a negative integer, zero, or a positive integer as the first key is less
 *     than, equal to, or greater than the second.
 */
static int compareSegmentAndNode(MemorySegment keySegment, int keyOffset, long targetNode, @Nonnull Allocator spaceAllocator) {
    Chunk chunk = spaceAllocator.getChunkById(SpaceUtils.getChunkIdByAddress(targetNode));
    int offsetInChunk = SpaceUtils.getChunkOffsetByAddress(targetNode);
    MemorySegment targetKeySegment = chunk.getMemorySegment(offsetInChunk);
    int offsetInByteBuffer = chunk.getOffsetInSegment(offsetInChunk);
    int level = getLevel(targetKeySegment, offsetInByteBuffer);
    int targetKeyOffset = offsetInByteBuffer + getKeyDataOffset(level);
    return SkipListKeyComparator.compareTo(keySegment, keyOffset, targetKeySegment, targetKeyOffset);
}
Also used : Chunk(org.apache.flink.runtime.state.heap.space.Chunk) MemorySegment(org.apache.flink.core.memory.MemorySegment)

Example 88 with MemorySegment

use of org.apache.flink.core.memory.MemorySegment in project flink by apache.

the class SkipListUtils method removeLevelIndex.

/**
 * Remove the level index for the node from the skip list.
 *
 * @param node the node.
 * @param spaceAllocator the space allocator.
 * @param levelIndexHeader the head level index.
 */
static void removeLevelIndex(long node, Allocator spaceAllocator, LevelIndexHeader levelIndexHeader) {
    Chunk chunk = spaceAllocator.getChunkById(SpaceUtils.getChunkIdByAddress(node));
    int offsetInChunk = SpaceUtils.getChunkOffsetByAddress(node);
    MemorySegment segment = chunk.getMemorySegment(offsetInChunk);
    int offsetInByteBuffer = chunk.getOffsetInSegment(offsetInChunk);
    int level = getLevel(segment, offsetInByteBuffer);
    for (int i = 1; i <= level; i++) {
        long prevNode = getPrevIndexNode(segment, offsetInByteBuffer, level, i);
        long nextNode = getNextIndexNode(segment, offsetInByteBuffer, i);
        helpSetNextNode(prevNode, nextNode, i, levelIndexHeader, spaceAllocator);
        helpSetPrevNode(nextNode, prevNode, i, spaceAllocator);
    }
}
Also used : Chunk(org.apache.flink.runtime.state.heap.space.Chunk) MemorySegment(org.apache.flink.core.memory.MemorySegment)

Example 89 with MemorySegment

use of org.apache.flink.core.memory.MemorySegment in project flink by apache.

the class SkipListUtils method helpSetPrevAndNextNode.

/**
 * Set the previous node and the next node of the given node at the given level. The level must
 * be positive.
 *
 * @param node the node.
 * @param prevNode the previous node to set.
 * @param nextNode the next node to set.
 * @param level the level to find the next node.
 * @param spaceAllocator the space allocator.
 */
static void helpSetPrevAndNextNode(long node, long prevNode, long nextNode, int level, Allocator spaceAllocator) {
    Preconditions.checkArgument(node != HEAD_NODE, "head node does not have previous node");
    Preconditions.checkArgument(level > 0, "only index level have previous node");
    Chunk chunk = spaceAllocator.getChunkById(SpaceUtils.getChunkIdByAddress(node));
    int offsetInChunk = SpaceUtils.getChunkOffsetByAddress(node);
    MemorySegment segment = chunk.getMemorySegment(offsetInChunk);
    int offsetInByteBuffer = chunk.getOffsetInSegment(offsetInChunk);
    int topLevel = getLevel(segment, offsetInByteBuffer);
    putNextIndexNode(segment, offsetInByteBuffer, level, nextNode);
    putPrevIndexNode(segment, offsetInByteBuffer, topLevel, level, prevNode);
}
Also used : Chunk(org.apache.flink.runtime.state.heap.space.Chunk) MemorySegment(org.apache.flink.core.memory.MemorySegment)

Example 90 with MemorySegment

use of org.apache.flink.core.memory.MemorySegment in project flink by apache.

the class SkipListUtils method helpGetNextNode.

/**
 * Return the next of the given node at the given level.
 *
 * @param node the node to find the next node for.
 * @param level the level to find the next node.
 * @param levelIndexHeader the header of the level index.
 * @param spaceAllocator the space allocator.
 * @return the pointer to the next node of the given node at the given level.
 */
static long helpGetNextNode(long node, int level, LevelIndexHeader levelIndexHeader, Allocator spaceAllocator) {
    if (node == HEAD_NODE) {
        return levelIndexHeader.getNextNode(level);
    }
    Chunk chunk = spaceAllocator.getChunkById(SpaceUtils.getChunkIdByAddress(node));
    int offsetInChunk = SpaceUtils.getChunkOffsetByAddress(node);
    MemorySegment segment = chunk.getMemorySegment(offsetInChunk);
    int offsetInByteBuffer = chunk.getOffsetInSegment(offsetInChunk);
    return level == 0 ? getNextKeyPointer(segment, offsetInByteBuffer) : getNextIndexNode(segment, offsetInByteBuffer, level);
}
Also used : Chunk(org.apache.flink.runtime.state.heap.space.Chunk) MemorySegment(org.apache.flink.core.memory.MemorySegment)

Aggregations

MemorySegment (org.apache.flink.core.memory.MemorySegment)375 Test (org.junit.Test)136 ArrayList (java.util.ArrayList)52 DummyInvokable (org.apache.flink.runtime.operators.testutils.DummyInvokable)44 IOException (java.io.IOException)37 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)29 Buffer (org.apache.flink.runtime.io.network.buffer.Buffer)26 NetworkBuffer (org.apache.flink.runtime.io.network.buffer.NetworkBuffer)25 MemoryAllocationException (org.apache.flink.runtime.memory.MemoryAllocationException)24 IntPair (org.apache.flink.runtime.operators.testutils.types.IntPair)24 FileIOChannel (org.apache.flink.runtime.io.disk.iomanager.FileIOChannel)20 EOFException (java.io.EOFException)18 ByteBuffer (java.nio.ByteBuffer)18 AbstractInvokable (org.apache.flink.runtime.jobgraph.tasks.AbstractInvokable)18 TestData (org.apache.flink.runtime.operators.testutils.TestData)18 Random (java.util.Random)16 UniformIntPairGenerator (org.apache.flink.runtime.operators.testutils.UniformIntPairGenerator)16 Chunk (org.apache.flink.runtime.state.heap.space.Chunk)15 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)15 IOManagerAsync (org.apache.flink.runtime.io.disk.iomanager.IOManagerAsync)14