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

dev.responsive.kafka.internal.utils.DuplicateKeyListValueIterator Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2024 Responsive Computing, Inc.
 *
 * This source code is licensed under the Responsive Business Source License Agreement v1.0
 * available at:
 *
 * https://www.responsive.dev/legal/responsive-bsl-10
 *
 * This software requires a valid Commercial License Key for production use. Trial and commercial
 * licenses can be obtained at https://www.responsive.dev
 */

package dev.responsive.kafka.internal.utils;

import dev.responsive.kafka.internal.db.mongo.WindowDoc;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;
import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.state.KeyValueIterator;

public class DuplicateKeyListValueIterator implements KeyValueIterator {

  private final Iterator remoteResults;
  private final Function keyExtractor;
  private WindowResult currentWindow;

  public DuplicateKeyListValueIterator(
      final Iterator remoteResults,
      final Function keyExtractor
  ) {
    this.remoteResults = remoteResults;
    this.keyExtractor = keyExtractor;

    if (remoteResults.hasNext()) {
      final WindowDoc firstDoc = remoteResults.next();
      currentWindow = new WindowResult(firstDoc, keyExtractor);
    }
  }

  @Override
  public void close() {

  }

  @Override
  public WindowedKey peekNextKey() {
    if (currentWindow == null) {
      return null;
    } else {
      return currentWindow.key;
    }
  }

  @Override
  public boolean hasNext() {
    return currentWindow != null;
  }

  @Override
  public KeyValue next() {
    final KeyValue next = currentWindow.next();

    if (!currentWindow.hasNext()) {
      if (remoteResults.hasNext()) {
        final WindowDoc nextDoc = remoteResults.next();
        currentWindow = new WindowResult(nextDoc, keyExtractor);
      } else {
        currentWindow = null;
      }
    }

    return next;
  }

  private static class WindowResult {
    private final WindowedKey key;
    private final List values;
    private int valueIndex = 0;

    public WindowResult(
        final WindowDoc windowDoc,
        final Function keyExtractor
    ) {
      this.key = keyExtractor.apply(windowDoc);
      this.values = windowDoc.getValues();
    }

    boolean hasNext() {
      return valueIndex < values.size();
    }

    KeyValue next() {
      return new KeyValue<>(key, values.get(valueIndex++));
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy