Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: move streaming output helps to command base class
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed May 15, 2025
commit 32ace67e7de3c0859f6a8f3608afaab08fbb6b96
3 changes: 2 additions & 1 deletion apps/files/lib/Command/Object/ListObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public function execute(InputInterface $input, OutputInterface $output): int {
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;
}
Expand Down
62 changes: 5 additions & 57 deletions apps/files/lib/Command/Object/ObjectUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@

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,
Expand Down Expand Up @@ -95,36 +93,13 @@ public function objectExistsInDb(string $object): int|false {
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'] ?? []));
Expand All @@ -137,31 +112,4 @@ private function formatObject(array $object, bool $humanOutput): array {
}
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;
}
}
}
4 changes: 2 additions & 2 deletions apps/files/lib/Command/Object/Orphans.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function execute(InputInterface $input, OutputInterface $output): int {
$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;
}
Expand Down
52 changes: 52 additions & 0 deletions core/Command/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,58 @@ protected function writeTableInOutputFormat(InputInterface $input, OutputInterfa
}
}

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
Expand Down
Loading