From a085083380e86f7632d522c0c8d3ec4bdd1eeb13 Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Tue, 17 Oct 2023 21:21:45 +0200 Subject: [PATCH] Rework `FlysystemFS::scan()` to improve performance --- .../src/Flow/ETL/Filesystem/FlysystemFS.php | 28 ++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/core/etl/src/Flow/ETL/Filesystem/FlysystemFS.php b/src/core/etl/src/Flow/ETL/Filesystem/FlysystemFS.php index 5e8f88f2b..d4aabef64 100644 --- a/src/core/etl/src/Flow/ETL/Filesystem/FlysystemFS.php +++ b/src/core/etl/src/Flow/ETL/Filesystem/FlysystemFS.php @@ -120,12 +120,10 @@ public function open(Path $path, Mode $mode) : FileStream $fs->createDirectory($path->parentDirectory()->path()); } - /** @phpstan-ignore-next-line */ - return new FileStream($path, \fopen($path->path(), $mode->value, false, $path->context()->resource())); + return new FileStream($path, \fopen($path->path(), $mode->value, false, $path->context()->resource()) ?: null); } - /** @phpstan-ignore-next-line */ - return new FileStream($path, \fopen($path->uri(), $mode->value, false, $path->context()->resource())); + return new FileStream($path, \fopen($path->uri(), $mode->value, false, $path->context()->resource()) ?: null); } public function rm(Path $path) : void @@ -180,6 +178,12 @@ public function scan(Path $path, PartitionFilter $partitionFilter) : \Generator default => throw new InvalidArgumentException('Unexpected scheme: ' . $path->scheme()) }; + if ($fs->fileExists($path->path())) { + yield $path; + + return; + } + $filter = function (FileAttributes|DirectoryAttributes $file) use ($path, $partitionFilter) : bool { if ($file instanceof DirectoryAttributes) { return false; @@ -191,23 +195,9 @@ public function scan(Path $path, PartitionFilter $partitionFilter) : \Generator } } - $filePath = new Path(DIRECTORY_SEPARATOR . $file->path()); - - if (\count($filePath->partitions())) { - if (!$partitionFilter->keep(...$filePath->partitions())) { - return false; - } - } - - return true; + return $partitionFilter->keep(...(new Path(DIRECTORY_SEPARATOR . $file->path()))->partitions()); }; - if ($fs->fileExists($path->path())) { - yield $path; - - return; - } - /** * @psalm-suppress ArgumentTypeCoercion *