Java Code Examples for org.apache.flink.runtime.state.StateTransformationFunction#apply()

The following examples show how to use org.apache.flink.runtime.state.StateTransformationFunction#apply() . 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: CopyOnWriteStateTable.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * @param key            the key of the mapping to remove. Not null.
 * @param namespace      the namespace of the mapping to remove. Not null.
 * @param value          the value that is the second input for the transformation.
 * @param transformation the transformation function to apply on the old state and the given value.
 * @param <T>            type of the value that is the second input to the {@link StateTransformationFunction}.
 * @throws Exception exception that happen on applying the function.
 * @see #transform(Object, Object, StateTransformationFunction).
 */
<T> void transform(
		K key,
		N namespace,
		T value,
		StateTransformationFunction<S, T> transformation) throws Exception {

	final StateTableEntry<K, N, S> entry = putEntry(key, namespace);

	// copy-on-write check for state
	entry.state = transformation.apply(
			(entry.stateVersion < highestRequiredSnapshotVersion) ?
					getStateSerializer().copy(entry.state) :
					entry.state,
			value);
	entry.stateVersion = stateTableVersion;
}
 
Example 2
Source File: CopyOnWriteStateMap.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public <T> void transform(
	K key,
	N namespace,
	T value,
	StateTransformationFunction<S, T> transformation) throws Exception {

	final StateMapEntry<K, N, S> entry = putEntry(key, namespace);

	// copy-on-write check for state
	entry.state = transformation.apply(
		(entry.stateVersion < highestRequiredSnapshotVersion) ?
			getStateSerializer().copy(entry.state) :
			entry.state,
		value);
	entry.stateVersion = stateMapVersion;
}
 
Example 3
Source File: CopyOnWriteSkipListStateMapBasicOpTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Test state transform with existing key.
 */
@Test
public void testTransformExistingState() throws Exception {
	final int key = 1;
	final String oldState = "1";
	final int delta = 1;
	StateTransformationFunction<String, Integer> function =
		(String prevState, Integer value) -> prevState == null ? String.valueOf(value) : prevState + value;
	stateMap.put(key, namespace, oldState);
	String expectedState = function.apply(oldState, delta);
	stateMap.transform(key, namespace, delta, function);
	assertThat(stateMap.get(key, namespace), is(expectedState));
	assertThat(stateMap.size(), is(1));
	assertThat(stateMap.totalSize(), is(1));
	assertThat(allocator.getTotalSpaceNumber(), is(2));
}
 
Example 4
Source File: CopyOnWriteStateMap.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public <T> void transform(
	K key,
	N namespace,
	T value,
	StateTransformationFunction<S, T> transformation) throws Exception {

	final StateMapEntry<K, N, S> entry = putEntry(key, namespace);

	// copy-on-write check for state
	entry.state = transformation.apply(
		(entry.stateVersion < highestRequiredSnapshotVersion) ?
			getStateSerializer().copy(entry.state) :
			entry.state,
		value);
	entry.stateVersion = stateMapVersion;
}
 
Example 5
Source File: CopyOnWriteSkipListStateMap.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public <T> void transform(
	K key,
	N namespace,
	T value,
	StateTransformationFunction<S, T> transformation) throws Exception {
	updateStat();
	MemorySegment keySegment = getKeySegment(key, namespace);
	int keyLen = keySegment.size();

	S oldState = getNode(keySegment, 0, keyLen);
	S newState = transformation.apply(oldState, value);
	byte[] stateBytes = skipListValueSerializer.serialize(newState);
	putValue(keySegment, 0, keyLen, stateBytes, false);
}
 
Example 6
Source File: CopyOnWriteSkipListStateMapBasicOpTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Test state transform with new key.
 */
@Test
public void testTransformAbsentState() throws Exception {
	final int key = 1;
	final int delta = 1;
	StateTransformationFunction<String, Integer> function =
		(String prevState, Integer value) -> prevState == null ? String.valueOf(value) : prevState + value;
	String expectedState = function.apply(null, delta);
	stateMap.transform(key, namespace, delta, function);
	assertThat(stateMap.get(key, namespace), is(expectedState));
	assertThat(stateMap.size(), is(1));
	assertThat(stateMap.totalSize(), is(1));
	assertThat(allocator.getTotalSpaceNumber(), is(2));
}