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

org.apache.kafka.streams.state.internals.LeftOrRightValueSerializer Maven / Gradle / Ivy

/*
 * 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.kafka.streams.state.internals;

import org.apache.kafka.common.serialization.Serializer;
import org.apache.kafka.streams.kstream.internals.WrappingNullableSerializer;
import org.apache.kafka.streams.processor.internals.SerdeGetter;

import java.nio.ByteBuffer;
import java.util.Map;

import static org.apache.kafka.streams.kstream.internals.WrappingNullableUtils.initNullableSerializer;

/**
 * Serializes a {@link LeftOrRightValue}. The serialized bytes starts with a byte that references
 * to whether the value is V1 or V2.
 */
public class LeftOrRightValueSerializer implements WrappingNullableSerializer, Void, Object> {
    private Serializer leftSerializer;
    private Serializer rightSerializer;

    public LeftOrRightValueSerializer(final Serializer leftSerializer, final Serializer rightSerializer) {
        this.leftSerializer = leftSerializer;
        this.rightSerializer = rightSerializer;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void setIfUnset(final SerdeGetter getter) {
        if (leftSerializer == null) {
            leftSerializer = (Serializer) getter.valueSerde().serializer();
        }

        if (rightSerializer == null) {
            rightSerializer = (Serializer) getter.valueSerde().serializer();
        }

        initNullableSerializer(leftSerializer, getter);
        initNullableSerializer(rightSerializer, getter);
    }

    @Override
    public void configure(final Map configs, final boolean isKey) {
        leftSerializer.configure(configs, isKey);
        rightSerializer.configure(configs, isKey);
    }

    @Override
    public byte[] serialize(final String topic, final LeftOrRightValue data) {
        if (data == null) {
            return null;
        }

        final byte[] rawValue = (data.getLeftValue() != null)
            ? leftSerializer.serialize(topic, data.getLeftValue())
            : rightSerializer.serialize(topic, data.getRightValue());

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

        return ByteBuffer
            .allocate(1 + rawValue.length)
            .put((byte) (data.getLeftValue() != null ? 1 : 0))
            .put(rawValue)
            .array();
    }

    @Override
    public void close() {
        leftSerializer.close();
        rightSerializer.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy