use of org.apache.flink.core.memory.MemorySegment in project flink by apache.
the class SkipListKeyComparatorTest method compareSkipListKey.
private <K, N> int compareSkipListKey(@Nonnull SkipListKeySerializer<K, N> keySerializer, K key1, N namespace1, K key2, N namespace2) {
MemorySegment b1 = MemorySegmentFactory.wrap(keySerializer.serialize(key1, namespace1));
MemorySegment b2 = MemorySegmentFactory.wrap(keySerializer.serialize(key2, namespace2));
return SkipListKeyComparator.compareTo(b1, 0, b2, 0);
}
use of org.apache.flink.core.memory.MemorySegment in project flink by apache.
the class SkipListUtilsTest method verifyGetKeySpace.
private void verifyGetKeySpace(KeySpace keySpace, MemorySegment memorySegment, int offset) {
assertEquals(keySpace.level, SkipListUtils.getLevel(memorySegment, offset));
assertEquals(keySpace.status, SkipListUtils.getNodeStatus(memorySegment, offset));
assertEquals(keySpace.keyData.length, SkipListUtils.getKeyLen(memorySegment, offset));
assertEquals(keySpace.valuePointer, SkipListUtils.getValuePointer(memorySegment, offset));
assertEquals(keySpace.nextKeyPointer, SkipListUtils.getNextKeyPointer(memorySegment, offset));
for (int i = 1; i <= keySpace.nextIndexNodes.length; i++) {
assertEquals(keySpace.nextIndexNodes[i - 1], SkipListUtils.getNextIndexNode(memorySegment, offset, i));
}
for (int i = 1; i <= keySpace.prevIndexNodes.length; i++) {
assertEquals(keySpace.prevIndexNodes[i - 1], SkipListUtils.getPrevIndexNode(memorySegment, offset, keySpace.level, i));
}
int keyDataOffset = SkipListUtils.getKeyDataOffset(keySpace.level);
MemorySegment keyDataSegment = MemorySegmentFactory.wrap(keySpace.keyData);
assertEquals(0, memorySegment.compare(keyDataSegment, offset + keyDataOffset, 0, keySpace.keyData.length));
}
use of org.apache.flink.core.memory.MemorySegment in project flink by apache.
the class SkipListUtilsTest method testValueSpacePutAndGet.
@Test
public void testValueSpacePutAndGet() {
for (int i = 0; i < 100; i++) {
int valueLen = ThreadLocalRandom.current().nextInt(100) + 1;
ValueSpace valueSpace = createValueSpace(valueLen);
int valueMetaLen = SkipListUtils.getValueMetaLen();
int totalValueSpaceLen = valueMetaLen + valueLen;
int offset = 100;
MemorySegment segment = MemorySegmentFactory.allocateUnpooledSegment(totalValueSpaceLen + offset);
putValueSpace(valueSpace, segment, offset);
verifyGetValueSpace(valueSpace, segment, offset);
}
}
use of org.apache.flink.core.memory.MemorySegment in project flink by apache.
the class CopyOnWriteSkipListStateMap method removeAndGetOld.
@Override
public S removeAndGetOld(K key, N namespace) {
updateStat();
MemorySegment keySegment = getKeySegment(key, namespace);
int keyLen = keySegment.size();
return removeNode(keySegment, 0, keyLen, true);
}
use of org.apache.flink.core.memory.MemorySegment in project flink by apache.
the class CopyOnWriteSkipListStateMap method updateValueWithReplace.
/**
* Update the value of the node with replace mode. The old value will be unlinked and replaced
* by the new value, and can not be accessed later. Note that the space of the old value is not
* freed here, and the caller of this method should be responsible for the space management.
*
* @param node the node whose value will be replaced.
* @param value the value.
* @return the old value pointer.
*/
private long updateValueWithReplace(long node, byte[] value) {
// a null value indicates this is a removed node
int valueSize = value == null ? 0 : value.length;
int totalValueLen = SkipListUtils.getValueMetaLen() + valueSize;
long valuePointer = allocateSpace(totalValueLen);
Node nodeStorage = getNodeSegmentAndOffset(node);
MemorySegment nodeSegment = nodeStorage.nodeSegment;
int offsetInNodeSegment = nodeStorage.nodeOffset;
long oldValuePointer = SkipListUtils.getValuePointer(nodeSegment, offsetInNodeSegment);
long nextValuePointer = SkipListUtils.helpGetNextValuePointer(oldValuePointer, spaceAllocator);
doWriteValue(valuePointer, value, stateMapVersion, node, nextValuePointer);
// update value pointer in node after the new value has points the older value so that
// old value can be accessed concurrently
SkipListUtils.putValuePointer(nodeSegment, offsetInNodeSegment, valuePointer);
return oldValuePointer;
}
Aggregations