org.apache.flink.table.runtime.dataview.StateListView Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of flink-table-runtime-blink_2.12 Show documentation
Show all versions of flink-table-runtime-blink_2.12 Show documentation
This module contains classes that are required by a task manager for
execution of table programs. The content of this module is work-in-progress.
It will replace the runtime classes contained in flink-table-planner once
it is stable. See FLINK-11439 and FLIP-32 for more details.
The 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.table.runtime.dataview;
import org.apache.flink.annotation.Internal;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.runtime.state.internal.InternalListState;
import org.apache.flink.table.api.dataview.ListView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* {@link StateListView} is a {@link ListView} which is implemented using state backends.
*
* @param the external type of element in the {@link ListView}
*/
@Internal
public abstract class StateListView extends ListView implements StateDataView {
private final Iterable emptyList = Collections.emptyList();
@Override
public List getList() {
final List list = new ArrayList<>();
try {
get().forEach(list::add);
} catch (Exception e) {
throw new RuntimeException(e);
}
return list;
}
@Override
public void setList(List list) {
clear();
try {
addAll(list);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public Iterable get() throws Exception {
Iterable original = getListState().get();
return original != null ? original : emptyList;
}
@Override
public void add(EE value) throws Exception {
getListState().add(value);
}
@Override
public void addAll(List list) throws Exception {
getListState().addAll(list);
}
@Override
public boolean remove(EE value) throws Exception {
Iterable iterable = getListState().get();
if (iterable == null) {
// ListState.get() may return null according to the Javadoc.
return false;
}
// the getListState().get() not always returns List object
// copy values to ArrayList for removing
Iterator iterator = iterable.iterator();
List list = new ArrayList<>();
while (iterator.hasNext()) {
EE it = iterator.next();
list.add(it);
}
boolean success = list.remove(value);
if (success) {
getListState().update(list);
}
return success;
}
@Override
public void clear() {
getListState().clear();
}
protected abstract ListState getListState();
/**
* {@link KeyedStateListView} is an default implementation of {@link StateListView} whose
* underlying representation is a keyed state.
*/
public static final class KeyedStateListView extends StateListView {
private final ListState listState;
public KeyedStateListView(ListState listState) {
this.listState = listState;
}
@Override
public void setCurrentNamespace(N namespace) {
throw new UnsupportedOperationException();
}
@Override
protected ListState getListState() {
return listState;
}
}
/**
* {@link NamespacedStateListView} is an {@link StateListView} whose underlying representation
* is a keyed and namespaced state. It also support to change current namespace.
*/
public static final class NamespacedStateListView extends StateListView {
private final InternalListState listState;
private N namespace;
public NamespacedStateListView(InternalListState listState) {
this.listState = listState;
}
@Override
public void setCurrentNamespace(N namespace) {
this.namespace = namespace;
}
@Override
protected ListState getListState() {
listState.setCurrentNamespace(namespace);
return listState;
}
}
}