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

org.apache.flink.cep.nfa.NFAStateSerializerSnapshot Maven / Gradle / Ivy

There is a newer version: 2.0-preview1
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.cep.nfa;

import org.apache.flink.api.common.typeutils.CompositeTypeSerializerSnapshot;
import org.apache.flink.api.common.typeutils.TypeSerializer;
import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
import org.apache.flink.cep.nfa.sharedbuffer.EventId;
import org.apache.flink.cep.nfa.sharedbuffer.NodeId;
import org.apache.flink.core.memory.DataInputView;
import org.apache.flink.core.memory.DataOutputView;

import java.io.IOException;

/** Snapshot class for {@link NFAStateSerializer}. */
public class NFAStateSerializerSnapshot
        extends CompositeTypeSerializerSnapshot {

    private static final int CURRENT_VERSION = 2;

    private static final int FIRST_VERSION_WITH_PREVIOUS_TIMESTAMP = 2;

    private boolean supportsPreviousTimestamp = true;

    /** Constructor for read instantiation. */
    public NFAStateSerializerSnapshot() {}

    /** Constructor to create the snapshot for writing. */
    public NFAStateSerializerSnapshot(NFAStateSerializer serializerInstance) {
        super(serializerInstance);
        supportsPreviousTimestamp = serializerInstance.isSupportsPreviousTimestamp();
    }

    @Override
    protected int getCurrentOuterSnapshotVersion() {
        return CURRENT_VERSION;
    }

    @Override
    protected void readOuterSnapshot(
            int readOuterSnapshotVersion, DataInputView in, ClassLoader userCodeClassLoader)
            throws IOException {
        if (readOuterSnapshotVersion < FIRST_VERSION_WITH_PREVIOUS_TIMESTAMP) {
            supportsPreviousTimestamp = false;
        } else {
            supportsPreviousTimestamp = in.readBoolean();
        }
    }

    @Override
    protected void writeOuterSnapshot(DataOutputView out) throws IOException {
        out.writeBoolean(supportsPreviousTimestamp);
    }

    @Override
    protected OuterSchemaCompatibility resolveOuterSchemaCompatibility(
            TypeSerializerSnapshot oldSerializerSnapshot) {
        if (!(oldSerializerSnapshot instanceof NFAStateSerializerSnapshot)) {
            return OuterSchemaCompatibility.INCOMPATIBLE;
        }
        if (supportsPreviousTimestamp
                != ((NFAStateSerializerSnapshot) oldSerializerSnapshot).supportsPreviousTimestamp) {
            return OuterSchemaCompatibility.COMPATIBLE_AFTER_MIGRATION;
        }

        return OuterSchemaCompatibility.COMPATIBLE_AS_IS;
    }

    @Override
    protected TypeSerializer[] getNestedSerializers(NFAStateSerializer outerSerializer) {
        TypeSerializer versionSerializer = outerSerializer.getVersionSerializer();
        TypeSerializer nodeIdSerializer = outerSerializer.getNodeIdSerializer();
        TypeSerializer eventIdSerializer = outerSerializer.getEventIdSerializer();

        return new TypeSerializer[] {versionSerializer, nodeIdSerializer, eventIdSerializer};
    }

    @Override
    protected NFAStateSerializer createOuterSerializerWithNestedSerializers(
            TypeSerializer[] nestedSerializers) {

        @SuppressWarnings("unchecked")
        TypeSerializer versionSerializer =
                (TypeSerializer) nestedSerializers[0];

        @SuppressWarnings("unchecked")
        TypeSerializer nodeIdSerializer = (TypeSerializer) nestedSerializers[1];

        @SuppressWarnings("unchecked")
        TypeSerializer eventIdSerializer = (TypeSerializer) nestedSerializers[2];

        return new NFAStateSerializer(
                versionSerializer, nodeIdSerializer, eventIdSerializer, supportsPreviousTimestamp);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy