Skip to content

Commit

Permalink
Rework FlysystemFS::scan() to improve performance (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd authored Oct 18, 2023
1 parent 031cc33 commit 2a8ad9f
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions src/core/etl/src/Flow/ETL/Filesystem/FlysystemFS.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
*
Expand Down

0 comments on commit 2a8ad9f

Please sign in to comment.