All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.flink.runtime.state.heap.HeapListState Maven / Gradle / Ivy

There is a newer version: 1.13.6
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.runtime.state.heap;

import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.State;
import org.apache.flink.api.common.state.StateDescriptor;
import org.apache.flink.api.common.typeutils.TypeSerializer;
import org.apache.flink.api.common.typeutils.base.ListSerializer;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
import org.apache.flink.queryablestate.client.state.serialization.KvStateSerializer;
import org.apache.flink.runtime.state.internal.InternalListState;
import org.apache.flink.util.Preconditions;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Heap-backed partitioned {@link ListState} that is snapshotted into files.
 *
 * @param  The type of the key.
 * @param  The type of the namespace.
 * @param  The type of the value.
 */
class HeapListState extends AbstractHeapMergingState, Iterable>
        implements InternalListState {
    /**
     * Creates a new key/value state for the given hash map of key/value pairs.
     *
     * @param stateTable The state table for which this state is associated to.
     * @param keySerializer The serializer for the keys.
     * @param valueSerializer The serializer for the state.
     * @param namespaceSerializer The serializer for the namespace.
     * @param defaultValue The default value for the state.
     */
    private HeapListState(
            StateTable> stateTable,
            TypeSerializer keySerializer,
            TypeSerializer> valueSerializer,
            TypeSerializer namespaceSerializer,
            List defaultValue) {
        super(stateTable, keySerializer, valueSerializer, namespaceSerializer, defaultValue);
    }

    @Override
    public TypeSerializer getKeySerializer() {
        return keySerializer;
    }

    @Override
    public TypeSerializer getNamespaceSerializer() {
        return namespaceSerializer;
    }

    @Override
    public TypeSerializer> getValueSerializer() {
        return valueSerializer;
    }

    // ------------------------------------------------------------------------
    //  state access
    // ------------------------------------------------------------------------

    @Override
    public Iterable get() {
        return getInternal();
    }

    @Override
    public void add(V value) {
        Preconditions.checkNotNull(value, "You cannot add null to a ListState.");

        final N namespace = currentNamespace;

        final StateTable> map = stateTable;
        List list = map.get(namespace);

        if (list == null) {
            list = new ArrayList<>();
            map.put(namespace, list);
        }
        list.add(value);
    }

    @Override
    public byte[] getSerializedValue(
            final byte[] serializedKeyAndNamespace,
            final TypeSerializer safeKeySerializer,
            final TypeSerializer safeNamespaceSerializer,
            final TypeSerializer> safeValueSerializer)
            throws Exception {

        Preconditions.checkNotNull(serializedKeyAndNamespace);
        Preconditions.checkNotNull(safeKeySerializer);
        Preconditions.checkNotNull(safeNamespaceSerializer);
        Preconditions.checkNotNull(safeValueSerializer);

        Tuple2 keyAndNamespace =
                KvStateSerializer.deserializeKeyAndNamespace(
                        serializedKeyAndNamespace, safeKeySerializer, safeNamespaceSerializer);

        List result = stateTable.get(keyAndNamespace.f0, keyAndNamespace.f1);

        if (result == null) {
            return null;
        }

        final TypeSerializer dupSerializer =
                ((ListSerializer) safeValueSerializer).getElementSerializer();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputViewStreamWrapper view = new DataOutputViewStreamWrapper(baos);

        // write the same as RocksDB writes lists, with one ',' separator
        for (int i = 0; i < result.size(); i++) {
            dupSerializer.serialize(result.get(i), view);
            if (i < result.size() - 1) {
                view.writeByte(',');
            }
        }
        view.flush();

        return baos.toByteArray();
    }

    // ------------------------------------------------------------------------
    //  state merging
    // ------------------------------------------------------------------------

    @Override
    protected List mergeState(List a, List b) {
        a.addAll(b);
        return a;
    }

    @Override
    public void update(List values) throws Exception {
        Preconditions.checkNotNull(values, "List of values to add cannot be null.");

        if (values.isEmpty()) {
            clear();
            return;
        }

        List newStateList = new ArrayList<>();
        for (V v : values) {
            Preconditions.checkNotNull(v, "You cannot add null to a ListState.");
            newStateList.add(v);
        }

        stateTable.put(currentNamespace, newStateList);
    }

    @Override
    public void addAll(List values) throws Exception {
        Preconditions.checkNotNull(values, "List of values to add cannot be null.");

        if (!values.isEmpty()) {
            stateTable.transform(
                    currentNamespace,
                    values,
                    (previousState, value) -> {
                        if (previousState == null) {
                            previousState = new ArrayList<>();
                        }
                        for (V v : value) {
                            Preconditions.checkNotNull(v, "You cannot add null to a ListState.");
                            previousState.add(v);
                        }
                        return previousState;
                    });
        }
    }

    @SuppressWarnings("unchecked")
    static  IS create(
            StateDescriptor stateDesc,
            StateTable stateTable,
            TypeSerializer keySerializer) {
        return (IS)
                new HeapListState<>(
                        (StateTable>) stateTable,
                        keySerializer,
                        (TypeSerializer>) stateTable.getStateSerializer(),
                        stateTable.getNamespaceSerializer(),
                        (List) stateDesc.getDefaultValue());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy