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

Datasources - Part 2: main extension + existStrict + builder #3102

Merged
merged 33 commits into from
Aug 1, 2024

Conversation

rolnico
Copy link
Member

@rolnico rolnico commented Jul 12, 2024

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

Does this PR already have an issue describing the problem?

What kind of change does this PR introduce?
Feature + bug fix

What is the current behavior?

What is the new behavior (if this is a feature change)?
Features:

  • The parameter dataExtension corresponds to the extension of the files the users want to consider in their datasources. For example, if dataExtension == "xiidm", only files ending with ".xiidm" will be considered. The value can be anything. If the parameter is empty, it won't be considered in the file listing.
  • "Double" extensions (.iidm.xml, .iidm.json, .iidm.bin) are not anymore considered by the importers.
  • A DataSourceBuilder is now provided in addition to usual datasources creation methods.

Bug fixes:

  • When multiple files with the same base name are located next to each other, the correct importer will now be used.
  • When providing a filename to a DirectoryDataSource method (exists, newOutputStream, etc.), the datasource compression format extension is added to the filename only if the filename doesn't have one already.

Does this PR introduce a breaking change or deprecate an API?

  • Yes
  • No

If yes, please check if the following requirements are fulfilled

  • The Breaking Change or Deprecated label has been added
  • The migration steps are described in the following section

What changes might users need to make in their application due to this PR? (migration steps)

Some datasource constructors have changed or have been deleted. Change accordingly:

Old constructor New constructor
Bzip2DirectoryDataSource(Path directory, String baseName, DataSourceObserver observer) or Bzip2DirectoryDataSource(Path directory, String baseName) Bzip2DirectoryDataSource(Path directory, String baseName, String dataExtension, DataSourceObserver observer)
GzDirectoryDataSource(Path directory, String baseName, DataSourceObserver observer) or GzDirectoryDataSource(Path directory, String baseName) GzDirectoryDataSource(Path directory, String baseName, String dataExtension, DataSourceObserver observer)
XZDirectoryDataSource(Path directory, String baseName, DataSourceObserver observer) or XZDirectoryDataSource(Path directory, String baseName) XZDirectoryDataSource(Path directory, String baseName, String dataExtension, DataSourceObserver observer)
ZstdDirectoryDataSource(Path directory, String baseName, DataSourceObserver observer) or ZstdDirectoryDataSource(Path directory, String baseName) ZstdDirectoryDataSource(Path directory, String baseName, String dataExtension, DataSourceObserver observer)
XZDirectoryDataSource(Path directory, String baseName, DataSourceObserver observer) or XzDirectoryDataSource(Path directory, String baseName) XzDirectoryDataSource(Path directory, String baseName, String dataExtension, DataSourceObserver observer)
ZipArchiveDataSource(Path directory, String zipFileName, String baseName, DataSourceObserver observer) ZipArchiveDataSource(Path directory, String zipFileName, String baseName, String dataExtension, DataSourceObserver observer)
ZipArchiveDataSource(Path directory, String zipFileName, String baseName) ZipArchiveDataSource(Path directory, String zipFileName, String baseName, String dataExtension)

"Double" extensions (.iidm.xml, .iidm.json, .iidm.bin) are not anymore considered by the importers. If you used them, please use those instead:

Old double extension Extension to use
.iidm.xml .xiidm
.iidm.json .jiidm
.iidm.bin .biidm

Previously, when providing a filename to a datasource method (exists, newOutputStream, etc.), the datasource compression format extension was always added to the filename. Now it is added to the filename only if the filename doesn't have one already.

Other information:

Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
@rolnico rolnico self-assigned this Jul 12, 2024
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
@rolnico rolnico added Breaking Change API is broken bug labels Jul 15, 2024
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
…ataSource

Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
…asources_2_main_extension

# Conflicts:
#	commons/src/main/java/com/powsybl/commons/datasource/ReadOnlyDataSource.java
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
@rolnico rolnico force-pushed the nro/datasources_2_main_extension branch from c76c4eb to 4fd2dba Compare July 15, 2024 14:45
@@ -59,7 +62,8 @@ protected OutputStream getCompressedOutputStream(OutputStream os) throws IOExcep

private Path getPath(String fileName) {
Objects.requireNonNull(fileName);
return directory.resolve(fileName + getCompressionExt());
FileInformation fileInformation = new FileInformation(fileName, false);
return directory.resolve(fileInformation.getCompressionFormat() == null ? fileName + getCompressionExtension() : fileName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mention this fix/change or put in a separate PR ?

This changes compressed directory data source to allow newinputstream newoutputstream and exists to work with "foo.xiidm" when the file is "foo.xiidm.gz"

so it introduces the concept of "filenameOrUncompressedFilename" ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added info about this in the PR comment + in the breaking change section

}

@Test
void testConversionZip() throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice that you added a test !

@jonenst
Copy link
Contributor

jonenst commented Jul 15, 2024

since everything can be implemented only with getBaseExtension ( see #3085 ) , do we still want to add an API existsStrict ? Maybe yes but i'm not sure

@rolnico rolnico requested a review from flo-dup July 16, 2024 11:56
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
@rolnico
Copy link
Member Author

rolnico commented Jul 16, 2024

since everything can be implemented only with getBaseExtension ( see #3085 ) , do we still want to add an API existsStrict ? Maybe yes but i'm not sure

I find it better to do the check on the datasource side and not on the importer side. However, after discussion with @flo-dup, I changed existsStrict(String suffix, String ext) to isMainExtension(String ext) and fixed the uses. This method is more understandable and still keeps the logic on the datasource side.

Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
@rolnico rolnico marked this pull request as ready for review July 18, 2024 08:46
Base automatically changed from nro/datasources_reorganisation to main July 19, 2024 13:55
# Conflicts:
#	commons/src/main/java/com/powsybl/commons/datasource/AbstractArchiveDataSource.java
#	commons/src/main/java/com/powsybl/commons/datasource/AbstractFileSystemDataSource.java
#	commons/src/main/java/com/powsybl/commons/datasource/Bzip2DirectoryDataSource.java
#	commons/src/main/java/com/powsybl/commons/datasource/DataSourceUtil.java
#	commons/src/main/java/com/powsybl/commons/datasource/DirectoryDataSource.java
#	commons/src/main/java/com/powsybl/commons/datasource/GenericReadOnlyDataSource.java
#	commons/src/main/java/com/powsybl/commons/datasource/GzDirectoryDataSource.java
#	commons/src/main/java/com/powsybl/commons/datasource/ReadOnlyDataSource.java
#	commons/src/main/java/com/powsybl/commons/datasource/XZDirectoryDataSource.java
#	commons/src/main/java/com/powsybl/commons/datasource/ZipArchiveDataSource.java
#	commons/src/main/java/com/powsybl/commons/datasource/ZstdDirectoryDataSource.java
#	commons/src/test/java/com/powsybl/commons/datasource/AbstractArchiveDataSourceTest.java
#	commons/src/test/java/com/powsybl/commons/datasource/AbstractFileSystemDataSourceTest.java
#	commons/src/test/java/com/powsybl/commons/datasource/Bzip2DirectoryDataSourceTest.java
#	commons/src/test/java/com/powsybl/commons/datasource/DirectoryDataSourceTest.java
#	commons/src/test/java/com/powsybl/commons/datasource/GzDirectoryDataSourceTest.java
#	commons/src/test/java/com/powsybl/commons/datasource/MultipleReadOnlyDataSourceTest.java
#	commons/src/test/java/com/powsybl/commons/datasource/XZDirectoryDataSourceTest.java
#	commons/src/test/java/com/powsybl/commons/datasource/ZipArchiveDataSourceTest.java
#	commons/src/test/java/com/powsybl/commons/datasource/ZstdDirectoryDataSourceTest.java
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
Copy link

sonarcloud bot commented Jul 31, 2024

@flo-dup flo-dup merged commit 4b4f148 into main Aug 1, 2024
7 checks passed
@flo-dup flo-dup deleted the nro/datasources_2_main_extension branch August 1, 2024 14:39
if (dataSource.getDataExtension() == null || dataSource.getDataExtension().isEmpty()) {
return false;
} else if (EXTENSIONS.equals(dataSource.getDataExtension())) {
try (InputStream is = dataSource.newInputStream(null, EXTENSIONS)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this do an NPE if the user specified a data extension but the file doesn't actually exist ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would throw an exception but not a NPE. It will be fixed in #3114

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants