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

org.apache.kafka.streams.kstream.internals.SessionKeySerde Maven / Gradle / Ivy

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.kafka.streams.kstream.internals;

import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serializer;
import org.apache.kafka.common.utils.Bytes;
import org.apache.kafka.streams.kstream.Window;
import org.apache.kafka.streams.kstream.Windowed;

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

/**
 * Serde for a {@link Windowed} key when working with {@link org.apache.kafka.streams.kstream.SessionWindows}
 *
 * @param  sessionId type
 */
public class SessionKeySerde implements Serde> {
    private static final int TIMESTAMP_SIZE = 8;

    private final Serde keySerde;

    public SessionKeySerde(final Serde keySerde) {
        this.keySerde = keySerde;
    }

    @Override
    public void configure(final Map configs, final boolean isKey) {
    }

    @Override
    public void close() {
    }

    @Override
    public Serializer> serializer() {
        return new SessionKeySerializer(keySerde.serializer());
    }

    @Override
    public Deserializer> deserializer() {
        return new SessionKeyDeserializer(keySerde.deserializer());
    }

    private class SessionKeySerializer implements Serializer> {

        private final Serializer keySerializer;

        SessionKeySerializer(final Serializer keySerializer) {
            this.keySerializer = keySerializer;
        }

        @Override
        public void configure(final Map configs, final boolean isKey) {

        }

        @Override
        public byte[] serialize(final String topic, final Windowed data) {
            if (data == null) {
                return null;
            }
            return toBinary(data, keySerializer, topic).get();
        }

        @Override
        public void close() {

        }
    }

    private class SessionKeyDeserializer implements Deserializer> {
        private final Deserializer deserializer;

        SessionKeyDeserializer(final Deserializer deserializer) {
            this.deserializer = deserializer;
        }

        @Override
        public void configure(final Map configs, final boolean isKey) {
        }

        @Override
        public Windowed deserialize(final String topic, final byte[] data) {
            if (data == null || data.length == 0) {
                return null;
            }
            return from(data, deserializer, topic);
        }


        @Override
        public void close() {

        }
    }

    public static long extractEnd(final byte[] binaryKey) {
        return ByteBuffer.wrap(binaryKey).getLong(binaryKey.length - 2 * TIMESTAMP_SIZE);
    }

    public static long extractStart(final byte[] binaryKey) {
        return ByteBuffer.wrap(binaryKey).getLong(binaryKey.length - TIMESTAMP_SIZE);
    }

    static Window extractWindow(final byte[] binaryKey) {
        final ByteBuffer buffer = ByteBuffer.wrap(binaryKey);
        final long start = buffer.getLong(binaryKey.length - TIMESTAMP_SIZE);
        final long end = buffer.getLong(binaryKey.length - 2 * TIMESTAMP_SIZE);
        return new SessionWindow(start, end);
    }

    public static byte[] extractKeyBytes(final byte[] binaryKey) {
        final byte[] bytes = new byte[binaryKey.length - 2 * TIMESTAMP_SIZE];
        System.arraycopy(binaryKey, 0, bytes, 0, bytes.length);
        return bytes;
    }

    public static  Windowed from(final byte[] binaryKey, final Deserializer keyDeserializer, final String topic) {
        final K key = extractKey(binaryKey, keyDeserializer, topic);
        final Window window = extractWindow(binaryKey);
        return new Windowed<>(key, window);
    }

    public static Windowed fromBytes(Bytes bytesKey) {
        final byte[] binaryKey = bytesKey.get();
        final ByteBuffer buffer = ByteBuffer.wrap(binaryKey);
        final long start = buffer.getLong(binaryKey.length - TIMESTAMP_SIZE);
        final long end = buffer.getLong(binaryKey.length - 2 * TIMESTAMP_SIZE);
        return new Windowed<>(Bytes.wrap(extractKeyBytes(binaryKey)), new SessionWindow(start, end));
    }

    private static  K extractKey(final byte[] binaryKey, final Deserializer deserializer, final String topic) {
        return deserializer.deserialize(topic, extractKeyBytes(binaryKey));
    }

    public static  Bytes toBinary(final Windowed sessionKey, final Serializer serializer, final String topic) {
        final byte[] bytes = serializer.serialize(topic, sessionKey.key());
        ByteBuffer buf = ByteBuffer.allocate(bytes.length + 2 * TIMESTAMP_SIZE);
        buf.put(bytes);
        buf.putLong(sessionKey.window().end());
        buf.putLong(sessionKey.window().start());
        return new Bytes(buf.array());
    }

    public static Bytes bytesToBinary(final Windowed sessionKey) {
        final byte[] bytes = sessionKey.key().get();
        ByteBuffer buf = ByteBuffer.allocate(bytes.length + 2 * TIMESTAMP_SIZE);
        buf.put(bytes);
        buf.putLong(sessionKey.window().end());
        buf.putLong(sessionKey.window().start());
        return new Bytes(buf.array());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy