adoroszlai commented on PR #8050:
URL: https://github.com/apache/ozone/pull/8050#issuecomment-2721582730

   I know duplication is temporary, but it still bothers me.
   
   ```diff
   diff --git 
hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/container/ListSubcommand.java
 
hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/container/ListSubcommand.java
   index 888a01f211..c651750ce3 100644
   --- 
hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/container/ListSubcommand.java
   +++ 
hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/container/ListSubcommand.java
   @@ -26,6 +26,7 @@
    import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
    import com.google.common.base.Strings;
    import java.io.IOException;
   +import java.util.List;
    import org.apache.hadoop.hdds.cli.HddsVersionProvider;
    import org.apache.hadoop.hdds.client.ReplicationConfig;
    import org.apache.hadoop.hdds.client.ReplicationType;
   @@ -36,6 +37,8 @@
    import org.apache.hadoop.hdds.scm.client.ScmClient;
    import org.apache.hadoop.hdds.scm.container.ContainerInfo;
    import org.apache.hadoop.hdds.scm.container.ContainerListResult;
   +import org.apache.hadoop.hdds.utils.IOUtils;
   +import org.apache.ratis.util.function.CheckedConsumer;
    import picocli.CommandLine.Command;
    import picocli.CommandLine.Help.Visibility;
    import picocli.CommandLine.Option;
   @@ -96,7 +99,7 @@ public class ListSubcommand extends ScmSubcommand {
      }
    
    
   -  private void outputContainerInfo(ContainerInfo containerInfo) throws 
IOException {
   +  private static void outputContainerInfo(ContainerInfo containerInfo) 
throws IOException {
        // Original behavior - just print the container JSON
        System.out.println(WRITER.writeValueAsString(containerInfo));
      }
   @@ -120,6 +123,7 @@ public void execute(ScmClient scmClient) throws 
IOException {
                ScmConfigKeys.OZONE_SCM_CONTAINER_LIST_MAX_COUNT_DEFAULT);
    
        ContainerListResult containerListAndTotalCount;
   +    ContainerListProcesor processor = jsonFormat ? new JsonListWriter() : 
new JsonItemsWriter();
    
        if (!all) {
          if (count > maxCountAllowed) {
   @@ -129,19 +133,11 @@ public void execute(ScmClient scmClient) throws 
IOException {
            count = maxCountAllowed;
          }
          containerListAndTotalCount = scmClient.listContainer(startId, count, 
state, type, repConfig);
   -      
   -      if (jsonFormat) {
   -        // JSON array format using SequenceWriter directly
   -        try (SequenceWriter sequenceWriter = 
WRITER.writeValues(System.out)) {
   -          sequenceWriter.init(true); // Initialize as a JSON array
   -          
sequenceWriter.writeAll(containerListAndTotalCount.getContainerInfoList());
   -        }
   -        System.out.println(); // Add final newline
   -      } else {
   -        // Original format - one JSON object per line
   -        for (ContainerInfo container : 
containerListAndTotalCount.getContainerInfoList()) {
   -          outputContainerInfo(container);
   -        }
   +
   +      try {
   +        processor.accept(containerListAndTotalCount.getContainerInfoList());
   +      } finally {
   +        IOUtils.closeQuietly(processor);
          }
    
          if (containerListAndTotalCount.getTotalCount() > count) {
   @@ -154,46 +150,61 @@ public void execute(ScmClient scmClient) throws 
IOException {
          int batchSize = (count > 0) ? count : maxCountAllowed;
          long currentStartId = startId;
          int fetchedCount;
   -      
   -      if (jsonFormat) {
   -        // For JSON array format, use SequenceWriter to stream containers 
as we fetch them
   -        try (SequenceWriter sequenceWriter = 
WRITER.writeValues(System.out)) {
   -          sequenceWriter.init(true); // Initialize as a JSON array
   -          
   -          do {
   -            // Fetch containers in batches of 'batchSize'
   -            containerListAndTotalCount = 
scmClient.listContainer(currentStartId, batchSize, state, type, repConfig);
   -            fetchedCount = 
containerListAndTotalCount.getContainerInfoList().size();
   -            
   -            // Write containers directly to the SequenceWriter
   -            for (ContainerInfo container : 
containerListAndTotalCount.getContainerInfoList()) {
   -              sequenceWriter.write(container);
   -            }
   -            
   -            if (fetchedCount > 0) {
   -              currentStartId =
   -                  
containerListAndTotalCount.getContainerInfoList().get(fetchedCount - 
1).getContainerID() + 1;
   -            }
   -          } while (fetchedCount > 0);
   -        }
   -        System.out.println(); // Add final newline
   -      } else {
   -        // Original format - one JSON object per line
   +
   +      try {
            do {
              // Fetch containers in batches of 'batchSize'
              containerListAndTotalCount = 
scmClient.listContainer(currentStartId, batchSize, state, type, repConfig);
              fetchedCount = 
containerListAndTotalCount.getContainerInfoList().size();
    
   -          for (ContainerInfo container : 
containerListAndTotalCount.getContainerInfoList()) {
   -            outputContainerInfo(container);
   -          }
   +          // Write containers directly to the SequenceWriter
   +          
processor.accept(containerListAndTotalCount.getContainerInfoList());
    
              if (fetchedCount > 0) {
                currentStartId =
                    
containerListAndTotalCount.getContainerInfoList().get(fetchedCount - 
1).getContainerID() + 1;
              }
            } while (fetchedCount > 0);
   +      } finally {
   +        IOUtils.closeQuietly(processor);
          }
        }
      }
   +
   +  private interface ContainerListProcesor extends 
CheckedConsumer<List<ContainerInfo>, IOException>, AutoCloseable {
   +    //
   +  }
   +
   +  private static final class JsonListWriter implements 
ContainerListProcesor {
   +    private final SequenceWriter sequenceWriter;
   +
   +    private JsonListWriter() throws IOException {
   +      // For JSON array format, use SequenceWriter to stream containers as 
we fetch them
   +      sequenceWriter = WRITER.writeValues(System.out).init(true);
   +    }
   +
   +    @Override
   +    public void accept(List<ContainerInfo> list) throws IOException {
   +      sequenceWriter.writeAll(list);
   +    }
   +
   +    @Override
   +    public void close() throws Exception {
   +      sequenceWriter.close();
   +    }
   +  }
   +
   +  private static final class JsonItemsWriter implements 
ContainerListProcesor {
   +    @Override
   +    public void accept(List<ContainerInfo> list) throws IOException {
   +      for (ContainerInfo container : list) {
   +        outputContainerInfo(container);
   +      }
   +    }
   +
   +    @Override
   +    public void close() throws Exception {
   +      System.out.println(); // Add final newline
   +    }
   +  }
    }
   ```


-- 
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