diff options
author | Robin Appelman <robin@icewind.nl> | 2025-03-28 15:23:13 +0100 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2025-03-28 15:23:13 +0100 |
commit | a91f313a1c5796429cfb39b0e2e507e221532282 (patch) | |
tree | 42160752c77f18a908517aa10076e0eff01f61b7 | |
parent | a157ba729ef804f50f9746998deba07137b0190b (diff) | |
download | nextcloud-server-object-store-orphan.tar.gz nextcloud-server-object-store-orphan.zip |
feat: move streaming output helps to command base classobject-store-orphan
Signed-off-by: Robin Appelman <robin@icewind.nl>
-rw-r--r-- | apps/files/lib/Command/Object/ListObject.php | 3 | ||||
-rw-r--r-- | apps/files/lib/Command/Object/ObjectUtil.php | 62 | ||||
-rw-r--r-- | apps/files/lib/Command/Object/Orphans.php | 4 | ||||
-rw-r--r-- | core/Command/Base.php | 52 |
4 files changed, 61 insertions, 60 deletions
diff --git a/apps/files/lib/Command/Object/ListObject.php b/apps/files/lib/Command/Object/ListObject.php index f63eb9c2260..5d30232e09f 100644 --- a/apps/files/lib/Command/Object/ListObject.php +++ b/apps/files/lib/Command/Object/ListObject.php @@ -42,7 +42,8 @@ class ListObject extends Base { return self::FAILURE; } $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; } diff --git a/apps/files/lib/Command/Object/ObjectUtil.php b/apps/files/lib/Command/Object/ObjectUtil.php index 273ad9d6ec9..5f053c2c42f 100644 --- a/apps/files/lib/Command/Object/ObjectUtil.php +++ b/apps/files/lib/Command/Object/ObjectUtil.php @@ -8,16 +8,14 @@ declare(strict_types=1); namespace OCA\Files\Command\Object; -use OC\Core\Command\Base; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Files\ObjectStore\IObjectStore; use OCP\IConfig; use OCP\IDBConnection; use OCP\Util; -use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -class ObjectUtil extends Base { +class ObjectUtil { public function __construct( private IConfig $config, private IDBConnection $connection, @@ -95,36 +93,13 @@ class ObjectUtil extends Base { return $fileId; } - public function writeIteratorToOutput(InputInterface $input, OutputInterface $output, \Iterator $objects, int $chunkSize): void { - $outputType = $input->getOption('output'); - $humanOutput = $outputType === Base::OUTPUT_FORMAT_PLAIN; - - 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]"); + public function formatObjects(\Iterator $objects, bool $humanOutput): \Iterator { + foreach ($objects as $object) { + yield $this->formatObject($object, $humanOutput); } } - private function formatObject(array $object, bool $humanOutput): array { + public function formatObject(array $object, bool $humanOutput): array { $row = array_merge([ 'urn' => $object['urn'], ], ($object['metadata'] ?? [])); @@ -137,31 +112,4 @@ class ObjectUtil extends Base { } 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; - } - } } diff --git a/apps/files/lib/Command/Object/Orphans.php b/apps/files/lib/Command/Object/Orphans.php index aac05f2d4c0..f7132540fc8 100644 --- a/apps/files/lib/Command/Object/Orphans.php +++ b/apps/files/lib/Command/Object/Orphans.php @@ -63,9 +63,9 @@ class Orphans extends Base { $fileId = (int)substr($object['urn'], $prefixLength); 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; } diff --git a/core/Command/Base.php b/core/Command/Base.php index b915ae2ae4a..c9b6337b64a 100644 --- a/core/Command/Base.php +++ b/core/Command/Base.php @@ -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 |