[ https://issues.apache.org/jira/browse/FLINK-5023?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15647567#comment-15647567 ]
ASF GitHub Bot commented on FLINK-5023: --------------------------------------- Github user StephanEwen commented on a diff in the pull request: https://github.com/apache/flink/pull/2768#discussion_r86987689 --- Diff: flink-contrib/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBSimpleState.java --- @@ -0,0 +1,99 @@ +/* + * 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.contrib.streaming.state; + +import org.apache.flink.api.common.state.SimpleStateDescriptor; +import org.apache.flink.api.common.state.State; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.core.memory.DataInputViewStreamWrapper; +import org.apache.flink.util.Preconditions; +import org.rocksdb.ColumnFamilyHandle; +import org.rocksdb.RocksDBException; +import org.rocksdb.WriteOptions; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +/** + * The implementation that stores simple states in RocksDB. + * + * @param <K> The type of the key. + * @param <N> The type of the namespace. + * @param <V> The type of value that the state state stores. + */ +public class RocksDBSimpleState<K, N, V> extends AbstractRocksDBState<K, N> implements State<V> { + + /** State descriptor from which to create this state instance */ + protected final SimpleStateDescriptor<V, ? extends State<V>> stateDesc; + + /** Serializer for the values */ + protected final TypeSerializer<V> valueSerializer; + + /** + * We disable writes to the write-ahead-log here. We can't have these in the base class + * because JNI segfaults for some reason if they are. + */ + protected final WriteOptions writeOptions; + + /** + * Creates a new {@code RocksDBSimpleState}. + * + * @param namespaceSerializer The serializer for the namespace. + * @param stateDesc The state identifier for the state. This contains name and can create a default state value. + */ + public RocksDBSimpleState(ColumnFamilyHandle columnFamily, + TypeSerializer<N> namespaceSerializer, + SimpleStateDescriptor<V, ? extends State<V>> stateDesc, + RocksDBKeyedStateBackend<K> backend + ) { + super(columnFamily, namespaceSerializer, backend); + + this.stateDesc = Preconditions.checkNotNull(stateDesc, "State Descriptor"); + this.valueSerializer = stateDesc.getSerializer(); + + writeOptions = new WriteOptions(); + writeOptions.setDisableWAL(true); + } + + @Override + public V get() { + try { + writeCurrentKeyWithGroupAndNamespace(); + byte[] key = keySerializationStream.toByteArray(); + byte[] valueBytes = backend.db.get(columnFamily, key); + if (valueBytes == null) { + return stateDesc.getDefaultValue(); + } + return valueSerializer.deserialize(new DataInputViewStreamWrapper(new ByteArrayInputStream(valueBytes))); --- End diff -- Given that this is used only in single threaded settings, we should try to reuse the `DataInputViewStreamWrapper` and `ByteArrayInputStream`. > Add get() method in State interface > ----------------------------------- > > Key: FLINK-5023 > URL: https://issues.apache.org/jira/browse/FLINK-5023 > Project: Flink > Issue Type: Improvement > Components: State Backends, Checkpointing > Reporter: Xiaogang Shi > Assignee: Xiaogang Shi > > Currently, the only method provided by the State interface is `clear()`. I > think we should provide another method called `get()` to return the > structured value (e.g., value, list, or map) under the current key. > In fact, the functionality of `get()` has already been implemented in all > types of states: e.g., `value()` in ValueState and `get()` in ListState. The > modification to the interface can better abstract these states. -- This message was sent by Atlassian JIRA (v6.3.4#6332)