feat: move streaming output helps to command base class

Signed-off-by: Robin Appelman <robin@icewind.nl>
pull/52866/head
Robin Appelman 2025-03-28 15:23:13 +07:00
parent 18997b0199
commit 4ff14790af
4 changed files with 61 additions and 60 deletions

@ -42,7 +42,8 @@ class ListObject extends Base {
return self::FAILURE; return self::FAILURE;
} }
$objects = $objectStore->listObjects(); $objects = $objectStore->listObjects();
$this->objectUtils->writeIteratorToOutput($input, $output, $objects, self::CHUNK_SIZE); $objects = $this->objectUtils->formatObjects($objects, $input->getOption('output') === self::OUTPUT_FORMAT_PLAIN);
$this->writeStreamingTableInOutputFormat($input, $output, $objects, self::CHUNK_SIZE);
return self::SUCCESS; return self::SUCCESS;
} }

@ -8,16 +8,14 @@ declare(strict_types=1);
namespace OCA\Files\Command\Object; namespace OCA\Files\Command\Object;
use OC\Core\Command\Base;
use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\ObjectStore\IObjectStore; use OCP\Files\ObjectStore\IObjectStore;
use OCP\IConfig; use OCP\IConfig;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\Util; use OCP\Util;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
class ObjectUtil extends Base { class ObjectUtil {
public function __construct( public function __construct(
private IConfig $config, private IConfig $config,
private IDBConnection $connection, private IDBConnection $connection,
@ -95,36 +93,13 @@ class ObjectUtil extends Base {
return $fileId; return $fileId;
} }
public function writeIteratorToOutput(InputInterface $input, OutputInterface $output, \Iterator $objects, int $chunkSize): void { public function formatObjects(\Iterator $objects, bool $humanOutput): \Iterator {
$outputType = $input->getOption('output'); foreach ($objects as $object) {
$humanOutput = $outputType === Base::OUTPUT_FORMAT_PLAIN; yield $this->formatObject($object, $humanOutput);
if ($humanOutput) {
// we can't write tables in a streaming way, so we print them in chunks instead
foreach ($this->chunkIterator($objects, $chunkSize) as $chunk) {
$this->outputChunkHuman($input, $output, $chunk);
}
} else {
$first = true;
$output->writeln('[');
foreach ($objects as $object) {
if (!$first) {
$output->writeln(',');
}
$row = $this->formatObject($object, false);
if ($outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
$output->write(json_encode($row, JSON_PRETTY_PRINT));
} else {
$output->write(json_encode($row));
}
$first = false;
}
$output->writeln("\n]");
} }
} }
private function formatObject(array $object, bool $humanOutput): array { public function formatObject(array $object, bool $humanOutput): array {
$row = array_merge([ $row = array_merge([
'urn' => $object['urn'], 'urn' => $object['urn'],
], ($object['metadata'] ?? [])); ], ($object['metadata'] ?? []));
@ -137,31 +112,4 @@ class ObjectUtil extends Base {
} }
return $row; return $row;
} }
private function outputChunkHuman(InputInterface $input, OutputInterface $output, iterable $chunk): void {
$result = [];
foreach ($chunk as $object) {
$result[] = $this->formatObject($object, true);
}
$this->writeTableInOutputFormat($input, $output, $result);
}
public function chunkIterator(\Iterator $iterator, int $count): \Iterator {
$chunk = [];
for ($i = 0; $iterator->valid(); $i++) {
$chunk[] = $iterator->current();
$iterator->next();
if (count($chunk) == $count) {
// Got a full chunk, yield and start a new one
yield $chunk;
$chunk = [];
}
}
if (count($chunk)) {
// Yield the last chunk even if incomplete
yield $chunk;
}
}
} }

@ -63,9 +63,9 @@ class Orphans extends Base {
$fileId = (int)substr($object['urn'], $prefixLength); $fileId = (int)substr($object['urn'], $prefixLength);
return !$this->fileIdInDb($fileId); return !$this->fileIdInDb($fileId);
}); });
$orphans->rewind();
$this->objectUtils->writeIteratorToOutput($input, $output, $orphans, self::CHUNK_SIZE); $orphans = $this->objectUtils->formatObjects($orphans, $input->getOption('output') === self::OUTPUT_FORMAT_PLAIN);
$this->writeStreamingTableInOutputFormat($input, $output, $orphans, self::CHUNK_SIZE);
return self::SUCCESS; return self::SUCCESS;
} }

@ -88,6 +88,58 @@ class Base extends Command implements CompletionAwareInterface {
} }
} }
protected function writeStreamingTableInOutputFormat(InputInterface $input, OutputInterface $output, \Iterator $items, int $tableGroupSize): void {
switch ($input->getOption('output')) {
case self::OUTPUT_FORMAT_JSON:
case self::OUTPUT_FORMAT_JSON_PRETTY:
$this->writeStreamingJsonArray($input, $output, $items);
break;
default:
foreach ($this->chunkIterator($items, $tableGroupSize) as $chunk) {
$this->writeTableInOutputFormat($input, $output, $chunk);
}
break;
}
}
protected function writeStreamingJsonArray(InputInterface $input, OutputInterface $output, \Iterator $items): void {
$first = true;
$outputType = $input->getOption('output');
$output->writeln('[');
foreach ($items as $item) {
if (!$first) {
$output->writeln(',');
}
if ($outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
$output->write(json_encode($item, JSON_PRETTY_PRINT));
} else {
$output->write(json_encode($item));
}
$first = false;
}
$output->writeln("\n]");
}
public function chunkIterator(\Iterator $iterator, int $count): \Iterator {
$chunk = [];
for ($i = 0; $iterator->valid(); $i++) {
$chunk[] = $iterator->current();
$iterator->next();
if (count($chunk) == $count) {
// Got a full chunk, yield and start a new one
yield $chunk;
$chunk = [];
}
}
if (count($chunk)) {
// Yield the last chunk even if incomplete
yield $chunk;
}
}
/** /**
* @param mixed $item * @param mixed $item