org.apache.flink.runtime.state.StateTransformationFunction Java Examples

The following examples show how to use org.apache.flink.runtime.state.StateTransformationFunction. 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: NestedMapsStateTable.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public <T> void transform(N namespace, T value, StateTransformationFunction<S, T> transformation) throws Exception {
	final K key = keyContext.getCurrentKey();
	checkKeyNamespacePreconditions(key, namespace);
	final int keyGroupIndex = keyContext.getCurrentKeyGroupIndex();

	Map<N, Map<K, S>> namespaceMap = getMapForKeyGroup(keyGroupIndex);

	if (namespaceMap == null) {
		namespaceMap = new HashMap<>();
		setMapForKeyGroup(keyGroupIndex, namespaceMap);
	}

	Map<K, S> keyedMap = namespaceMap.computeIfAbsent(namespace, k -> new HashMap<>());
	keyedMap.put(key, transformation.apply(keyedMap.get(key), value));
}
 
Example #2
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 #3
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 #4
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 #5
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 #6
Source File: StateTable.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Applies the given {@link StateTransformationFunction} to the state (1st input argument), using the given value as
 * second input argument. The result of {@link StateTransformationFunction#apply(Object, Object)} is then stored as
 * the new state. This function is basically an optimization for get-update-put pattern.
 *
 * @param namespace      the namespace. Not null.
 * @param value          the value to use in transforming the state. Can be null.
 * @param transformation the transformation function.
 * @throws Exception if some exception happens in the transformation function.
 */
public <T> void transform(
		N namespace,
		T value,
		StateTransformationFunction<S, T> transformation) throws Exception {
	K key = keyContext.getCurrentKey();
	checkKeyNamespacePreconditions(key, namespace);

	int keyGroup = keyContext.getCurrentKeyGroupIndex();
	StateMap<K, N, S> stateMap = getMapForKeyGroup(keyGroup);
	stateMap.transform(key, namespace, value, transformation);
}
 
Example #7
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 #8
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));
}
 
Example #9
Source File: StateTable.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Applies the given {@link StateTransformationFunction} to the state (1st input argument), using the given value as
 * second input argument. The result of {@link StateTransformationFunction#apply(Object, Object)} is then stored as
 * the new state. This function is basically an optimization for get-update-put pattern.
 *
 * @param namespace      the namespace. Not null.
 * @param value          the value to use in transforming the state. Can be null.
 * @param transformation the transformation function.
 * @throws Exception if some exception happens in the transformation function.
 */
public <T> void transform(
		N namespace,
		T value,
		StateTransformationFunction<S, T> transformation) throws Exception {
	K key = keyContext.getCurrentKey();
	checkKeyNamespacePreconditions(key, namespace);

	int keyGroup = keyContext.getCurrentKeyGroupIndex();
	StateMap<K, N, S> stateMap = getMapForKeyGroup(keyGroup);
	stateMap.transform(key, namespace, value, transformation);
}
 
Example #10
Source File: CopyOnWriteStateTable.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public <T> void transform(N namespace, T value, StateTransformationFunction<S, T> transformation) throws Exception {
	transform(keyContext.getCurrentKey(), namespace, value, transformation);
}
 
Example #11
Source File: NestedStateMap.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public <T> void transform(
	K key, N namespace, T value, StateTransformationFunction<S, T> transformation) throws Exception {
	Map<K, S> keyedMap = namespaceMap.computeIfAbsent(namespace, k -> new HashMap<>());
	keyedMap.put(key, transformation.apply(keyedMap.get(key), value));
}
 
Example #12
Source File: NestedStateMap.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public <T> void transform(
	K key, N namespace, T value, StateTransformationFunction<S, T> transformation) throws Exception {
	Map<K, S> keyedMap = namespaceMap.computeIfAbsent(namespace, k -> new HashMap<>());
	keyedMap.put(key, transformation.apply(keyedMap.get(key), value));
}
 
Example #13
Source File: StateTable.java    From Flink-CEPplus with Apache License 2.0 2 votes vote down vote up
/**
 * Applies the given {@link StateTransformationFunction} to the state (1st input argument), using the given value as
 * second input argument. The result of {@link StateTransformationFunction#apply(Object, Object)} is then stored as
 * the new state. This function is basically an optimization for get-update-put pattern.
 *
 * @param namespace      the namespace. Not null.
 * @param value          the value to use in transforming the state. Can be null.
 * @param transformation the transformation function.
 * @throws Exception if some exception happens in the transformation function.
 */
public abstract <T> void transform(
		N namespace,
		T value,
		StateTransformationFunction<S, T> transformation) throws Exception;
 
Example #14
Source File: StateMap.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Applies the given {@link StateTransformationFunction} to the state (1st input argument), using the given value as
 * second input argument. The result of {@link StateTransformationFunction#apply(Object, Object)} is then stored as
 * the new state. This function is basically an optimization for get-update-put pattern.
 *
 * @param key            the key. Not null.
 * @param namespace      the namespace. Not null.
 * @param value          the value to use in transforming the state. Can be null.
 * @param transformation the transformation function.
 * @throws Exception if some exception happens in the transformation function.
 */
public abstract <T> void transform(
	K key,
	N namespace,
	T value,
	StateTransformationFunction<S, T> transformation) throws Exception;
 
Example #15
Source File: StateMap.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Applies the given {@link StateTransformationFunction} to the state (1st input argument), using the given value as
 * second input argument. The result of {@link StateTransformationFunction#apply(Object, Object)} is then stored as
 * the new state. This function is basically an optimization for get-update-put pattern.
 *
 * @param key            the key. Not null.
 * @param namespace      the namespace. Not null.
 * @param value          the value to use in transforming the state. Can be null.
 * @param transformation the transformation function.
 * @throws Exception if some exception happens in the transformation function.
 */
public abstract <T> void transform(
	K key,
	N namespace,
	T value,
	StateTransformationFunction<S, T> transformation) throws Exception;