mirror of
https://github.com/nextcloud/server.git
synced 2026-05-28 04:32:30 -04:00
feat: move streaming output helps to command base class
Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
18997b0199
commit
4ff14790af
4 changed files with 61 additions and 60 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue