Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework FlysystemFS::scan() to improve performance #608

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Changes from all commits
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
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
Loading