protected TypeSerializer<N> getNamespaceSerializer() { return stateTable.getNamespaceSerializer(); }
protected TypeSerializer<S> getStateSerializer() { return stateTable.getStateSerializer(); } }
@SuppressWarnings("unchecked") static <K, N, SV, S extends State, IS extends S> IS create( StateDescriptor<S, SV> stateDesc, StateTable<K, N, SV> stateTable, TypeSerializer<K> keySerializer) { return (IS) new HeapValueState<>( stateTable, keySerializer, stateTable.getStateSerializer(), stateTable.getNamespaceSerializer(), stateDesc.getDefaultValue()); } }
private static <K, N, S> StateSnapshotKeyGroupReader createV2PlusReader(StateTable<K, N, S> stateTable) { final TypeSerializer<K> keySerializer = stateTable.keyContext.getKeySerializer(); final TypeSerializer<N> namespaceSerializer = stateTable.getNamespaceSerializer(); final TypeSerializer<S> stateSerializer = stateTable.getStateSerializer(); final Tuple3<N, K, S> buffer = new Tuple3<>(); return KeyGroupPartitioner.createKeyGroupPartitionReader((in) -> { buffer.f0 = namespaceSerializer.deserialize(in); buffer.f1 = keySerializer.deserialize(in); buffer.f2 = stateSerializer.deserialize(in); return buffer; }, (element, keyGroupId1) -> stateTable.put(element.f1, keyGroupId1, element.f0, element.f2)); }
@Override public UV get(UK userKey) { Map<UK, UV> userMap = stateTable.get(currentNamespace); if (userMap == null) { return null; } return userMap.get(userKey); }
@Override public void mergeNamespaces(N target, Collection<N> sources) throws Exception { if (sources == null || sources.isEmpty()) { return; // nothing to do } final StateTable<K, N, SV> map = stateTable; SV merged = null; // merge the sources for (N source : sources) { // get and remove the next source per namespace/key SV sourceState = map.removeAndGetOld(source); if (merged != null && sourceState != null) { merged = mergeState(merged, sourceState); } else if (merged == null) { merged = sourceState; } } // merge into the target, if needed if (merged != null) { map.transform(target, merged, mergeTransformation); } }
@Override public void add(T value) throws IOException { if (value == null) { clear(); return; } try { stateTable.transform(currentNamespace, value, foldTransformation); } catch (Exception e) { throw new IOException("Could not add value to folding state.", e); } }
RegisteredKeyValueStateBackendMetaInfo<N, V> restoredKvMetaInfo = stateTable.getMetaInfo(); stateTable.setMetaInfo(restoredKvMetaInfo); } else { RegisteredKeyValueStateBackendMetaInfo<N, V> newMetaInfo = new RegisteredKeyValueStateBackendMetaInfo<>(
@Override public void updateInternal(SV valueToStore) { stateTable.put(currentNamespace, valueToStore); } }
/** * Returns whether this {@link StateTable} is empty. * * @return {@code true} if this {@link StateTable} has no elements, {@code false} * otherwise. * @see #size() */ public boolean isEmpty() { return size() == 0; }
/** * Returns the total number of state entries across all keys for the given namespace. */ @VisibleForTesting public int numStateEntries(Object namespace) { int sum = 0; for (StateTable<K, ?, ?> stateTable : stateTables.values()) { sum += stateTable.sizeOfNamespace(namespace); } return sum; }
@Override public final void clear() { stateTable.remove(currentNamespace); }
private <N, V> StateTable<K, N, V> tryRegisterStateTable( TypeSerializer<N> namespaceSerializer, StateDescriptor<?, V> stateDesc) throws StateMigrationException { @SuppressWarnings("unchecked") StateTable<K, N, V> stateTable = (StateTable<K, N, V>) stateTables.get(stateDesc.getName()); RegisteredKeyedBackendStateMetaInfo<N, V> newMetaInfo; if (stateTable != null) { @SuppressWarnings("unchecked") RegisteredKeyedBackendStateMetaInfo.Snapshot<N, V> restoredMetaInfoSnapshot = (RegisteredKeyedBackendStateMetaInfo.Snapshot<N, V>) restoredKvStateMetaInfos.get(stateDesc.getName()); Preconditions.checkState( restoredMetaInfoSnapshot != null, "Requested to check compatibility of a restored RegisteredKeyedBackendStateMetaInfo," + " but its corresponding restored snapshot cannot be found."); newMetaInfo = RegisteredKeyedBackendStateMetaInfo.resolveKvStateCompatibility( restoredMetaInfoSnapshot, namespaceSerializer, stateDesc); stateTable.setMetaInfo(newMetaInfo); } else { newMetaInfo = new RegisteredKeyedBackendStateMetaInfo<>( stateDesc.getType(), stateDesc.getName(), namespaceSerializer, stateDesc.getSerializer()); stateTable = snapshotStrategy.newStateTable(newMetaInfo); stateTables.put(stateDesc.getName(), stateTable); } return stateTable; }
@SuppressWarnings("unchecked") static <K, N, SV, S extends State, IS extends S> IS create( StateDescriptor<S, SV> stateDesc, StateTable<K, N, SV> stateTable, TypeSerializer<K> keySerializer) { return (IS) new HeapValueState<>( stateTable, keySerializer, stateTable.getStateSerializer(), stateTable.getNamespaceSerializer(), stateDesc.getDefaultValue()); } }
private static <K, N, S> StateSnapshotKeyGroupReader createV2PlusReader(StateTable<K, N, S> stateTable) { final TypeSerializer<K> keySerializer = stateTable.keyContext.getKeySerializer(); final TypeSerializer<N> namespaceSerializer = stateTable.getNamespaceSerializer(); final TypeSerializer<S> stateSerializer = stateTable.getStateSerializer(); final Tuple3<N, K, S> buffer = new Tuple3<>(); return KeyGroupPartitioner.createKeyGroupPartitionReader((in) -> { buffer.f0 = namespaceSerializer.deserialize(in); buffer.f1 = keySerializer.deserialize(in); buffer.f2 = stateSerializer.deserialize(in); return buffer; }, (element, keyGroupId1) -> stateTable.put(element.f1, keyGroupId1, element.f0, element.f2)); }
@Override public Iterable<UV> values() { HashMap<UK, UV> userMap = stateTable.get(currentNamespace); return userMap == null ? null : userMap.values(); }
@Override public void mergeNamespaces(N target, Collection<N> sources) throws Exception { if (sources == null || sources.isEmpty()) { return; // nothing to do } final StateTable<K, N, SV> map = stateTable; SV merged = null; // merge the sources for (N source : sources) { // get and remove the next source per namespace/key SV sourceState = map.removeAndGetOld(source); if (merged != null && sourceState != null) { merged = mergeState(merged, sourceState); } else if (merged == null) { merged = sourceState; } } // merge into the target, if needed if (merged != null) { map.transform(target, merged, mergeTransformation); } }
@Override public void add(T value) throws IOException { if (value == null) { clear(); return; } try { stateTable.transform(currentNamespace, value, foldTransformation); } catch (Exception e) { throw new IOException("Could not add value to folding state.", e); } }
RegisteredKeyValueStateBackendMetaInfo<N, V> restoredKvMetaInfo = stateTable.getMetaInfo(); stateTable.setMetaInfo(restoredKvMetaInfo); } else { RegisteredKeyValueStateBackendMetaInfo<N, V> newMetaInfo = new RegisteredKeyValueStateBackendMetaInfo<>(