rohangarg commented on code in PR #13368:
URL: https://github.com/apache/druid/pull/13368#discussion_r1081123338


##########
processing/src/main/java/org/apache/druid/frame/channel/ComposingReadableFrameChannel.java:
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.druid.frame.channel;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import org.apache.druid.frame.Frame;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Supplier;
+
+/**
+ * A composed readable channel to read frames. The channel can encapsulate 
multiple readable channels in it and
+ * automatically switches to next channels once the currently read channel is 
finished.
+ */
+public class ComposingReadableFrameChannel implements ReadableFrameChannel
+{
+  private final List<Supplier<ReadableFrameChannel>> channels;
+  private ReadableFrameChannel currentChannel;
+  private int currentIndex;
+
+  public ComposingReadableFrameChannel(
+      int partition,
+      List<Supplier<ReadableFrameChannel>> channels,
+      Map<Integer, HashSet<Integer>> partitionToChannelMap
+  )
+  {
+    Preconditions.checkNotNull(channels, "channels is null");
+    Preconditions.checkNotNull(partitionToChannelMap, "partitionToChannelMap 
is null");
+    if (partitionToChannelMap.get(partition) == null) {
+      // no writes for the partition, send an empty readable channel
+      this.channels = ImmutableList.of(() -> ReadableNilFrameChannel.INSTANCE);
+    } else {
+      HashSet<Integer> validChannels = partitionToChannelMap.get(partition);
+      Preconditions.checkState(validChannels.size() > 0, "No channels found 
for partition " + partition);
+      ImmutableList.Builder<Supplier<ReadableFrameChannel>> 
validChannelsBuilder = ImmutableList.builder();
+      ArrayList<Integer> sortedChannelIds = new ArrayList<>(validChannels);
+      Collections.sort(sortedChannelIds); // the data was written from lowest 
to highest channel
+      for (Integer channelId : sortedChannelIds) {
+        validChannelsBuilder.add(channels.get(channelId));
+      }
+      this.channels = validChannelsBuilder.build();
+    }
+    this.currentIndex = 0;
+    this.currentChannel = null;
+  }
+
+  @Override
+  public boolean isFinished()
+  {
+    initCurrentChannel();

Review Comment:
   I don't think there can be multiple readers for the same composing readable 
channel - am I missing something?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to