rkhachatryan commented on code in PR #27071: URL: https://github.com/apache/flink/pull/27071#discussion_r2411771881
########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/orderedmultisetstate/ValueStateMultiSetState.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.table.runtime.orderedmultisetstate; + +import org.apache.flink.api.common.functions.RuntimeContext; +import org.apache.flink.api.common.state.ValueState; +import org.apache.flink.api.common.state.ValueStateDescriptor; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.common.typeutils.base.ListSerializer; +import org.apache.flink.api.common.typeutils.base.LongSerializer; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.api.java.tuple.Tuple3; +import org.apache.flink.api.java.typeutils.runtime.TupleSerializer; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.runtime.generated.RecordEqualiser; +import org.apache.flink.types.RowKind; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Optional; +import java.util.function.Function; + +/** + * Simple implementation of {@link OrderedMultiSetState} based on plain {@code ValueState<List>}. + */ +class ValueStateMultiSetState implements OrderedMultiSetState<RowData> { + + private final ValueState<List<Tuple2<RowData, Long>>> valuesState; + private final RecordEqualiser keyEqualiser; + private final Function<RowData, RowData> keyExtractor; + private final TimeSelector timeSelector; + private List<Tuple2<RowData, Long>> cache; + + ValueStateMultiSetState( + ValueState<List<Tuple2<RowData, Long>>> valuesState, + RecordEqualiser keyEqualiser, + Function<RowData, RowData> keyExtractor, + TimeSelector timeSelector) { + this.valuesState = valuesState; + this.keyEqualiser = keyEqualiser; + this.keyExtractor = keyExtractor; + this.timeSelector = timeSelector; + } + + public static OrderedMultiSetState<RowData> create( + OrderedMultiSetStateContext p, RuntimeContext ctx) { + //noinspection rawtypes,unchecked + return new ValueStateMultiSetState( + ctx.getState( + new ValueStateDescriptor<>( + "list", + new ListSerializer<>( + new TupleSerializer( + Tuple2.class, + new TypeSerializer[] { + p.recordSerializer, LongSerializer.INSTANCE + })))), + p.generatedKeyEqualiser.newInstance(ctx.getUserCodeClassLoader()), + p.keyExtractor, + p.config.getTimeSelector()); + } + + @Override + public SizeChangeInfo add(RowData row, long ts) throws Exception { + normalizeRowKind(row); + final Tuple2<RowData, Long> toAdd = Tuple2.of(row, timeSelector.getTimestamp(ts)); + final RowData key = asKey(row); + final List<Tuple2<RowData, Long>> list = maybeReadState(); + final int oldSize = list.size(); + + int idx = Integer.MIN_VALUE; + int i = 0; + for (Tuple2<RowData, Long> t : list) { + if (keyEqualiser.equals(asKey(t.f0), key)) { + idx = i; + break; + } + } + if (idx < 0) { + list.add(toAdd); + } else { + list.set(idx, toAdd); + } + valuesState.update(list); Review Comment: Could be another optimization 🤔 But it's not trivial because it has to be aware of snapshots and the context key. We can probably just flush on context key change, but that's less efficient. Conceptually though, I think it is the be responsibility of state backend - and it is (implemented by memtable in case of RocksDB). I'd leave it as a potential follow-up in case this approach is not enough, WDYT? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
