Skip to content

Commit f493908

Browse files
Merge pull request #95 from cleverage/94
#94 - flysystem_list_content: read the listing item by item
2 parents d0734a4 + 7962b88 commit f493908

4 files changed

Lines changed: 62 additions & 14 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Latest
22
------
33

44
### Changes
5+
* [#94](https://github.com/cleverage/flysystem-process-bundle/issues/94) `ListContentTask` (`flysystem_list_content`): the listing is read item by item (the Flysystem generator is iterated lazily) instead of being loaded in an array, which used about 26 MB more memory for 100,000 files. The output and its order are unchanged; `getFilteredFilesystemContents()` and `$fsContent` of this (final since 3.3) class now are a `\Generator`. Update documentation, add tests.
56
* [#92](https://github.com/cleverage/flysystem-process-bundle/issues/92) Documentation: the storages are configured with the format of league/flysystem-bundle 3.5 and later (the adapter being the key of its options), the `adapter` / `options` format being deprecated since flysystem-bundle 3.5; the former format is mentioned for an earlier version.
67

78
v3.4

‎docs/reference/tasks/list_content_task.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ with `property: path` to get the path, as expected by [FileFetchTask](file_fetch
3030

3131
When the storage is empty, or when no item matches `file_pattern`, no output is produced and the task is skipped.
3232

33+
The listing is read item by item while the task iterates, it is not loaded in memory. Each execution of the task (for each
34+
input) lists the storage again. Be careful if a downstream task changes the listed directory (e.g. removing the files)
35+
while the iteration is in progress: with an adapter listing lazily, the iteration may then skip some items.
36+
3337
Options
3438
-------
3539

‎src/Task/ListContentTask.php‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
class ListContentTask extends AbstractConfigurableTask implements IterableTaskInterface
3030
{
3131
/**
32-
* @var list<\League\Flysystem\StorageAttributes>|null
32+
* The listing being iterated, read lazily (a huge storage is not loaded in memory).
33+
*
34+
* @var \Generator<int, \League\Flysystem\StorageAttributes>|null
3335
*/
34-
protected ?array $fsContent = null;
36+
protected ?\Generator $fsContent = null;
3537

3638
/**
3739
* @param ServiceLocator<FilesystemOperator> $storages
@@ -54,7 +56,7 @@ protected function configureOptions(OptionsResolver $resolver): void
5456
*/
5557
public function execute(ProcessState $state): void
5658
{
57-
if (null === $this->fsContent || null === key($this->fsContent)) {
59+
if (!$this->fsContent instanceof \Generator || !$this->fsContent->valid()) {
5860
/** @var string $filesystemOption */
5961
$filesystemOption = $this->getOption($state, 'filesystem');
6062
$filesystem = $this->storages->get($filesystemOption);
@@ -64,39 +66,36 @@ public function execute(ProcessState $state): void
6466
$this->fsContent = $this->getFilteredFilesystemContents($filesystem, $patternOption);
6567
}
6668

67-
if (null === key($this->fsContent)) {
69+
if (!$this->fsContent->valid()) {
6870
$state->setSkipped(true);
6971
$this->fsContent = null;
7072
} else {
71-
$state->setOutput(current($this->fsContent));
73+
$state->setOutput($this->fsContent->current());
7274
}
7375
}
7476

7577
public function next(ProcessState $state): bool
7678
{
77-
if (!\is_array($this->fsContent)) {
79+
if (!$this->fsContent instanceof \Generator) {
7880
return false;
7981
}
8082

81-
next($this->fsContent);
83+
$this->fsContent->next();
8284

83-
return null !== key($this->fsContent);
85+
return $this->fsContent->valid();
8486
}
8587

8688
/**
87-
* @return list<\League\Flysystem\StorageAttributes>
89+
* @return \Generator<int, \League\Flysystem\StorageAttributes>
8890
*
8991
* @throws FilesystemException
9092
*/
91-
protected function getFilteredFilesystemContents(FilesystemOperator $filesystem, ?string $pattern = null): array
93+
protected function getFilteredFilesystemContents(FilesystemOperator $filesystem, ?string $pattern = null): \Generator
9294
{
93-
$results = [];
9495
foreach ($filesystem->listContents('') as $item) {
9596
if (null === $pattern || preg_match($pattern, $item->path())) {
96-
$results[] = $item;
97+
yield $item;
9798
}
9899
}
99-
100-
return $results;
101100
}
102101
}

‎tests/Task/ListContentTaskTest.php‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515

1616
use CleverAge\FlysystemProcessBundle\Task\ListContentTask;
1717
use CleverAge\ProcessBundle\Model\ProcessState;
18+
use League\Flysystem\DirectoryListing;
19+
use League\Flysystem\FileAttributes;
20+
use League\Flysystem\FilesystemOperator;
1821
use League\Flysystem\StorageAttributes;
1922
use PHPUnit\Framework\Attributes\CoversClass;
23+
use Symfony\Component\DependencyInjection\ServiceLocator;
2024
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
2125

2226
#[CoversClass(ListContentTask::class)]
@@ -57,6 +61,46 @@ public function testEachExecutionListsTheStorageAgain(): void
5761
self::assertSame(2, $this->listings['source']);
5862
}
5963

64+
/**
65+
* The listing is read item by item, not loaded in an array: only the items already output (and the one being
66+
* output) are read from the listing.
67+
*/
68+
public function testListingIsReadLazily(): void
69+
{
70+
$read = 0;
71+
$listing = new DirectoryListing((static function () use (&$read): \Generator {
72+
foreach (['a.txt', 'b.txt', 'c.txt'] as $path) {
73+
++$read;
74+
yield new FileAttributes($path);
75+
}
76+
})());
77+
$filesystem = $this->createStub(FilesystemOperator::class);
78+
$filesystem->method('listContents')->willReturn($listing);
79+
$task = new ListContentTask(new ServiceLocator(['source' => static fn (): FilesystemOperator => $filesystem]));
80+
$state = $this->createState(ListContentTask::class, ['filesystem' => 'source']);
81+
$task->initialize($state);
82+
83+
$state->reset(true);
84+
$task->execute($state);
85+
self::assertSame(1, $read);
86+
self::assertTrue($task->next($state));
87+
$task->execute($state);
88+
self::assertSame(2, $read);
89+
self::assertTrue($task->next($state));
90+
$task->execute($state);
91+
self::assertFalse($task->next($state));
92+
self::assertSame(3, $read);
93+
}
94+
95+
public function testNoItemMatchingThePattern(): void
96+
{
97+
$this->createFiles('source', ['a.txt' => 'a']);
98+
[$task, $state] = $this->createTask(['file_pattern' => '/\.csv$/']);
99+
100+
self::assertSame([], $this->iterate($task, $state, null));
101+
self::assertFalse($task->next($state));
102+
}
103+
60104
public function testNextBeforeExecute(): void
61105
{
62106
[$task, $state] = $this->createTask([]);

0 commit comments

Comments
 (0)