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

The following examples show how to use org.apache.flink.core.memory.MemorySegment#compare() . 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: NormalizedKeySorter.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public int compare(int segmentNumberI, int segmentOffsetI, int segmentNumberJ, int segmentOffsetJ) {
	final MemorySegment segI = this.sortIndex.get(segmentNumberI);
	final MemorySegment segJ = this.sortIndex.get(segmentNumberJ);

	int val = segI.compare(segJ, segmentOffsetI + OFFSET_LEN, segmentOffsetJ + OFFSET_LEN, this.numKeyBytes);

	if (val != 0 || this.normalizedKeyFullyDetermines) {
		return this.useNormKeyUninverted ? val : -val;
	}

	final long pointerI = segI.getLong(segmentOffsetI) & POINTER_MASK;
	final long pointerJ = segJ.getLong(segmentOffsetJ) & POINTER_MASK;

	return compareRecords(pointerI, pointerJ);
}
 
Example 2
Source File: NormalizedKeySorter.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public int compare(int segmentNumberI, int segmentOffsetI, int segmentNumberJ, int segmentOffsetJ) {
	final MemorySegment segI = this.sortIndex.get(segmentNumberI);
	final MemorySegment segJ = this.sortIndex.get(segmentNumberJ);

	int val = segI.compare(segJ, segmentOffsetI + OFFSET_LEN, segmentOffsetJ + OFFSET_LEN, this.numKeyBytes);

	if (val != 0 || this.normalizedKeyFullyDetermines) {
		return this.useNormKeyUninverted ? val : -val;
	}

	final long pointerI = segI.getLong(segmentOffsetI) & POINTER_MASK;
	final long pointerJ = segJ.getLong(segmentOffsetJ) & POINTER_MASK;

	return compareRecords(pointerI, pointerJ);
}
 
Example 3
Source File: SkipListKeyComparator.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Compares for order. Returns a negative integer, zero, or a positive integer
 * as the first node is less than, equal to, or greater than the second.
 *
 * @param left        left skip list key's ByteBuffer
 * @param leftOffset  left skip list key's ByteBuffer's offset
 * @param right       right skip list key's ByteBuffer
 * @param rightOffset right skip list key's ByteBuffer's offset
 * @return An integer result of the comparison.
 */
static int compareTo(MemorySegment left, int leftOffset, MemorySegment right, int rightOffset) {
	// compare namespace
	int leftNamespaceLen = left.getInt(leftOffset);
	int rightNamespaceLen = right.getInt(rightOffset);

	int c = left.compare(right, leftOffset + Integer.BYTES, rightOffset + Integer.BYTES,
		leftNamespaceLen, rightNamespaceLen);

	if (c != 0) {
		return c;
	}

	// compare key
	int leftKeyOffset = leftOffset + Integer.BYTES + leftNamespaceLen;
	int rightKeyOffset = rightOffset + Integer.BYTES + rightNamespaceLen;
	int leftKeyLen = left.getInt(leftKeyOffset);
	int rightKeyLen = right.getInt(rightKeyOffset);

	return left.compare(right, leftKeyOffset + Integer.BYTES, rightKeyOffset + Integer.BYTES,
		leftKeyLen, rightKeyLen);
}
 
Example 4
Source File: NormalizedKeySorter.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public int compare(int segmentNumberI, int segmentOffsetI, int segmentNumberJ, int segmentOffsetJ) {
	final MemorySegment segI = this.sortIndex.get(segmentNumberI);
	final MemorySegment segJ = this.sortIndex.get(segmentNumberJ);

	int val = segI.compare(segJ, segmentOffsetI + OFFSET_LEN, segmentOffsetJ + OFFSET_LEN, this.numKeyBytes);

	if (val != 0 || this.normalizedKeyFullyDetermines) {
		return this.useNormKeyUninverted ? val : -val;
	}

	final long pointerI = segI.getLong(segmentOffsetI) & POINTER_MASK;
	final long pointerJ = segJ.getLong(segmentOffsetJ) & POINTER_MASK;

	return compareRecords(pointerI, pointerJ);
}
 
Example 5
Source File: ComparatorTestBase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
protected void testNormalizedKeysGreatSmall(boolean greater, TypeComparator<T> comparator, boolean halfLength) {
	try {
		T[] data = getSortedData();
		// Get the normKeyLen on which we are testing
		int normKeyLen = getNormKeyLen(halfLength, data, comparator);

		// Write the data into different 2 memory segments
		MemorySegment memSegLow = setupNormalizedKeysMemSegment(data, normKeyLen, comparator);
		MemorySegment memSegHigh = setupNormalizedKeysMemSegment(data, normKeyLen, comparator);

		boolean fullyDetermines = !comparator.isNormalizedKeyPrefixOnly(normKeyLen);
		
		// Compare every element with every bigger element
		for (int l = 0; l < data.length - 1; l++) {
			for (int h = l + 1; h < data.length; h++) {
				int cmp;
				if (greater) {
					cmp = memSegLow.compare(memSegHigh, l * normKeyLen, h * normKeyLen, normKeyLen);
					if (fullyDetermines) {
						assertTrue(cmp < 0);
					} else {
						assertTrue(cmp <= 0);
					}
				} else {
					cmp = memSegHigh.compare(memSegLow, h * normKeyLen, l * normKeyLen, normKeyLen);
					if (fullyDetermines) {
						assertTrue(cmp > 0);
					} else {
						assertTrue(cmp >= 0);
					}
				}
			}
		}
	} catch (Exception e) {
		System.err.println(e.getMessage());
		e.printStackTrace();
		fail("Exception in test: " + e.getMessage());
	}
}
 
Example 6
Source File: FixedLengthRecordSorter.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public int compare(int segmentNumberI, int segmentOffsetI, int segmentNumberJ, int segmentOffsetJ) {
	final MemorySegment segI = this.sortBuffer.get(segmentNumberI);
	final MemorySegment segJ = this.sortBuffer.get(segmentNumberJ);

	int val = segI.compare(segJ, segmentOffsetI, segmentOffsetJ, this.numKeyBytes);
	return this.useNormKeyUninverted ? val : -val;
}
 
Example 7
Source File: ComparatorTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
protected void testNormalizedKeysGreatSmall(boolean greater, TypeComparator<T> comparator, boolean halfLength) {
	try {
		T[] data = getSortedData();
		// Get the normKeyLen on which we are testing
		int normKeyLen = getNormKeyLen(halfLength, data, comparator);

		// Write the data into different 2 memory segments
		MemorySegment memSegLow = setupNormalizedKeysMemSegment(data, normKeyLen, comparator);
		MemorySegment memSegHigh = setupNormalizedKeysMemSegment(data, normKeyLen, comparator);

		boolean fullyDetermines = !comparator.isNormalizedKeyPrefixOnly(normKeyLen);
		
		// Compare every element with every bigger element
		for (int l = 0; l < data.length - 1; l++) {
			for (int h = l + 1; h < data.length; h++) {
				int cmp;
				if (greater) {
					cmp = memSegLow.compare(memSegHigh, l * normKeyLen, h * normKeyLen, normKeyLen);
					if (fullyDetermines) {
						assertTrue(cmp < 0);
					} else {
						assertTrue(cmp <= 0);
					}
				} else {
					cmp = memSegHigh.compare(memSegLow, h * normKeyLen, l * normKeyLen, normKeyLen);
					if (fullyDetermines) {
						assertTrue(cmp > 0);
					} else {
						assertTrue(cmp >= 0);
					}
				}
			}
		}
	} catch (Exception e) {
		System.err.println(e.getMessage());
		e.printStackTrace();
		fail("Exception in test: " + e.getMessage());
	}
}
 
Example 8
Source File: FixedLengthRecordSorter.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public int compare(int segmentNumberI, int segmentOffsetI, int segmentNumberJ, int segmentOffsetJ) {
	final MemorySegment segI = this.sortBuffer.get(segmentNumberI);
	final MemorySegment segJ = this.sortBuffer.get(segmentNumberJ);

	int val = segI.compare(segJ, segmentOffsetI, segmentOffsetJ, this.numKeyBytes);
	return this.useNormKeyUninverted ? val : -val;
}
 
Example 9
Source File: ComparatorTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
protected void testNormalizedKeysGreatSmall(boolean greater, TypeComparator<T> comparator, boolean halfLength) {
	try {
		T[] data = getSortedData();
		// Get the normKeyLen on which we are testing
		int normKeyLen = getNormKeyLen(halfLength, data, comparator);

		// Write the data into different 2 memory segments
		MemorySegment memSegLow = setupNormalizedKeysMemSegment(data, normKeyLen, comparator);
		MemorySegment memSegHigh = setupNormalizedKeysMemSegment(data, normKeyLen, comparator);

		boolean fullyDetermines = !comparator.isNormalizedKeyPrefixOnly(normKeyLen);
		
		// Compare every element with every bigger element
		for (int l = 0; l < data.length - 1; l++) {
			for (int h = l + 1; h < data.length; h++) {
				int cmp;
				if (greater) {
					cmp = memSegLow.compare(memSegHigh, l * normKeyLen, h * normKeyLen, normKeyLen);
					if (fullyDetermines) {
						assertTrue(cmp < 0);
					} else {
						assertTrue(cmp <= 0);
					}
				} else {
					cmp = memSegHigh.compare(memSegLow, h * normKeyLen, l * normKeyLen, normKeyLen);
					if (fullyDetermines) {
						assertTrue(cmp > 0);
					} else {
						assertTrue(cmp >= 0);
					}
				}
			}
		}
	} catch (Exception e) {
		System.err.println(e.getMessage());
		e.printStackTrace();
		fail("Exception in test: " + e.getMessage());
	}
}
 
Example 10
Source File: FixedLengthRecordSorter.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public int compare(int segmentNumberI, int segmentOffsetI, int segmentNumberJ, int segmentOffsetJ) {
	final MemorySegment segI = this.sortBuffer.get(segmentNumberI);
	final MemorySegment segJ = this.sortBuffer.get(segmentNumberJ);

	int val = segI.compare(segJ, segmentOffsetI, segmentOffsetJ, this.numKeyBytes);
	return this.useNormKeyUninverted ? val : -val;
}
 
Example 11
Source File: SkipListKeyComparator.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Compares the namespace in the memory segment with the namespace in the node .
 * Returns a negative integer, zero, or a positive integer as the first node is
 * less than, equal to, or greater than the second.
 *
 * @param namespaceSegment    memory segment to store the namespace.
 * @param namespaceOffset     offset of namespace in the memory segment.
 * @param namespaceLen        length of namespace.
 * @param nodeSegment         memory segment to store the node key.
 * @param nodeKeyOffset       offset of node key in the memory segment.
 * @return An integer result of the comparison.
 */
static int compareNamespaceAndNode(
	MemorySegment namespaceSegment, int namespaceOffset, int namespaceLen,
	MemorySegment nodeSegment, int nodeKeyOffset) {

	int nodeNamespaceLen = nodeSegment.getInt(nodeKeyOffset);
	return namespaceSegment.compare(nodeSegment, namespaceOffset, nodeKeyOffset + Integer.BYTES,
		namespaceLen, nodeNamespaceLen);
}