mumrah commented on code in PR #15007:
URL: https://github.com/apache/kafka/pull/15007#discussion_r1427972960


##########
metadata/src/main/java/org/apache/kafka/metadata/migration/BufferingBatchConsumer.java:
##########
@@ -0,0 +1,59 @@
+/*
+ * 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.metadata.migration;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Consumer;
+
+/**
+ * A record batch consumer that merges incoming batches into batches of a 
minimum a given size. It does so
+ * by buffering the records into an array that is later flushed to a 
downstream consumer. Batches consumed
+ * by this class will not be broken apart, only combined with other batches to 
reach the minimum batch size.
+ * </p>
+ * Note that {@link #close()} must be called after the last batch has been 
accepted in order to flush any
+ * buffered records.
+ */
+public class BufferingBatchConsumer<T> implements Consumer<List<T>> {
+
+    private final Consumer<List<T>> delegateConsumer;
+    private final List<T> bufferedBatch;
+    private final int minBatchSize;
+
+    BufferingBatchConsumer(Consumer<List<T>> delegateConsumer, int 
minBatchSize) {
+        this.delegateConsumer = delegateConsumer;
+        this.bufferedBatch = new ArrayList<>(minBatchSize);
+        this.minBatchSize = minBatchSize;
+    }
+
+    @Override
+    public void accept(List<T> batch) {
+        bufferedBatch.addAll(batch);
+        if (bufferedBatch.size() >= minBatchSize) {
+            delegateConsumer.accept(new ArrayList<>(bufferedBatch));
+            bufferedBatch.clear();
+        }
+    }
+
+    public void close() {

Review Comment:
   Yea, good point. I'll rename this to flush



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to