-
Notifications
You must be signed in to change notification settings - Fork 43
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
Data store implementation #1327
Closed
Closed
Changes from 35 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
0cbefb9
First data store implementation
quinarygio 4807466
Small refactoring
quinarygio 483741c
XmlDataFormat implementation
quinarygio 7ffaea1
Rename DirectoryDataStore
quinarygio 31e07be
Xiidm import from data store
quinarygio 3e4043c
MemDataStore implementation
quinarygio f868992
Merge branch 'master' into data_store
quinarygio 327e144
XMLExporter export to data store
quinarygio 8aed2a8
Compressed archive data store implementation
quinarygio 5809716
Fix import from directory path
quinarygio b4113e9
Remove unused parameters
quinarygio aad07dc
UcteDtaFormat implementation
quinarygio f59bb6f
Merge branch 'master' into data_store
quinarygio c1f62f9
Fix sonar vulnerabilities
quinarygio 78c6741
Fix review remarks and code smells
quinarygio b13c5df
Fix sonar issues
quinarygio 142d8ab
Handle XIIDM mapping file
quinarygio 83451a0
Increase code coverage
quinarygio 1ced3fb
Fix bug exporting mapping file
quinarygio abf1cd2
Increase code coverage
quinarygio 7ae1555
Fix copyright and author
quinarygio 4bbb06a
Check for unique main entry.
quinarygio 7613a58
Convert DataStore to DataSource for backward compatibility
quinarygio 478fc8a
Subfolders not supported
quinarygio d9535d5
Data stores refactoring
quinarygio 60e30c6
Fix code smells
quinarygio df56cf5
Merge branch 'master' into data_store
quinarygio 64856f8
Export to data store takes a basename as input instead of filename
quinarygio 334e55c
Avoid unnecessary zip file rewriting
quinarygio b91fe8d
Merge branch 'master' into data_store
quinarygio e781808
Merge branch 'master' into data_store
quinarygio 6184a5c
Declare entryFilename as final
quinarygio 884f8e0
Add javadoc
quinarygio 8d1c9de
Remove trailing spaces in javadoc
quinarygio 2463a3b
DataSource wrapper for a DataStore
quinarygio 597de37
Remove toDataSource method from DataStore
quinarygio aa6471e
Check entry name for Gzip and Bzip stores and throw IOException if do…
quinarygio 71f029f
Throw NetworkImportException in case of errors in network import
quinarygio 11388c9
importDataStore methods default implementation
quinarygio 6212993
Fix code duplication issue
quinarygio 2c47bd0
Add importDataPack method
quinarygio 428a52d
Fix code smell
quinarygio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
commons/src/main/java/com/powsybl/commons/datasource/StoreDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Copyright (c) 2020, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package com.powsybl.commons.datasource; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.util.HashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
import com.powsybl.commons.datastore.DataStore; | ||
|
||
/** | ||
* @author Giovanni Ferrari <giovanni.ferrari at techrain.eu> | ||
*/ | ||
public class StoreDataSource implements DataSource { | ||
|
||
private final DataStore store; | ||
private final String baseName; | ||
|
||
public StoreDataSource(DataStore store, String baseName) { | ||
this.store = Objects.requireNonNull(store); | ||
this.baseName = Objects.requireNonNull(baseName); | ||
} | ||
|
||
@Override | ||
public String getBaseName() { | ||
return baseName; | ||
} | ||
|
||
@Override | ||
public boolean exists(String suffix, String ext) throws IOException { | ||
return store.exists(DataSourceUtil.getFileName(baseName, suffix, ext)); | ||
} | ||
|
||
@Override | ||
public boolean exists(String fileName) throws IOException { | ||
return store.exists(fileName); | ||
} | ||
|
||
@Override | ||
public InputStream newInputStream(String suffix, String ext) throws IOException { | ||
return store.newInputStream(DataSourceUtil.getFileName(baseName, suffix, ext)); | ||
} | ||
|
||
@Override | ||
public InputStream newInputStream(String fileName) throws IOException { | ||
return store.newInputStream(fileName); | ||
} | ||
|
||
@Override | ||
public Set<String> listNames(String regex) throws IOException { | ||
return new HashSet<>(store.getEntryNames()); | ||
} | ||
|
||
@Override | ||
public OutputStream newOutputStream(String fileName, boolean append) throws IOException { | ||
return store.newOutputStream(fileName, append); | ||
} | ||
|
||
@Override | ||
public OutputStream newOutputStream(String suffix, String ext, boolean append) throws IOException { | ||
return store.newOutputStream(DataSourceUtil.getFileName(baseName, suffix, ext), append); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
commons/src/main/java/com/powsybl/commons/datastore/AbstractDataResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Copyright (c) 2020, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package com.powsybl.commons.datastore; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Properties; | ||
import java.util.stream.Collectors; | ||
|
||
import com.google.common.io.Files; | ||
|
||
/** | ||
* @author Giovanni Ferrari <giovanni.ferrari at techrain.eu> | ||
*/ | ||
public abstract class AbstractDataResolver implements DataResolver { | ||
|
||
@Override | ||
public Optional<DataPack> resolve(ReadOnlyDataStore store, String mainFileName, Properties parameters) | ||
throws IOException, NonUniqueResultException { | ||
Objects.requireNonNull(store); | ||
|
||
DataPack dp = null; | ||
if (mainFileName != null) { | ||
if (store.exists(mainFileName) && checkFileExtension(mainFileName)) { | ||
dp = buildDataPack(store, mainFileName); | ||
} | ||
} else { | ||
List<String> candidates = store.getEntryNames().stream().filter(this::checkFileExtension).collect(Collectors.toList()); | ||
if (candidates.size() > 1) { | ||
throw new NonUniqueResultException("Non unique data pack found"); | ||
} else if (candidates.size() == 1) { | ||
String entryName = candidates.get(0); | ||
dp = buildDataPack(store, entryName); | ||
} | ||
} | ||
return Optional.ofNullable(dp); | ||
} | ||
|
||
@Override | ||
public boolean validate(DataPack pack, Properties properties) { | ||
Optional<DataEntry> main = pack.getMainEntry(); | ||
return pack.getDataFormatId().equals(getDataFormat().getId()) && main.isPresent() && checkFileExtension(main.get().getName()); | ||
} | ||
|
||
public boolean checkFileExtension(String filename) { | ||
return getDataFormat().getExtensions().contains(Files.getFileExtension(filename)); | ||
} | ||
|
||
private DataPack buildDataPack(ReadOnlyDataStore store, String mainFileName) { | ||
DataPack dp = new DataPack(store, getDataFormat().getId()); | ||
DataEntry entry = new DataEntry(mainFileName, DataPack.MAIN_ENTRY_TAG); | ||
dp.addEntry(entry); | ||
return dp; | ||
} | ||
|
||
public abstract DataFormat getDataFormat(); | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
commons/src/main/java/com/powsybl/commons/datastore/Bzip2FileDataStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Copyright (c) 2020, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package com.powsybl.commons.datastore; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; | ||
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; | ||
|
||
import com.powsybl.commons.datasource.Bzip2FileDataSource; | ||
import com.powsybl.commons.datasource.DataSource; | ||
import com.powsybl.commons.datasource.DataSourceUtil; | ||
|
||
/** | ||
* @author Giovanni Ferrari <giovanni.ferrari at techrain.eu> | ||
*/ | ||
public class Bzip2FileDataStore implements DataStore { | ||
|
||
private final Path path; | ||
|
||
public Bzip2FileDataStore(Path path) { | ||
this.path = Objects.requireNonNull(path); | ||
} | ||
|
||
@Override | ||
public List<String> getEntryNames() throws IOException { | ||
return Collections.singletonList(getEntryName()); | ||
} | ||
|
||
@Override | ||
public boolean exists(String entryName) { | ||
return getEntryName().equals(entryName); | ||
} | ||
|
||
@Override | ||
public InputStream newInputStream(String entryName) throws IOException { | ||
return new BZip2CompressorInputStream(Files.newInputStream(path)); | ||
} | ||
|
||
@Override | ||
public OutputStream newOutputStream(String entryName, boolean append) throws IOException { | ||
return new BZip2CompressorOutputStream(Files.newOutputStream(path, DataSourceUtil.getOpenOptions(append))); | ||
} | ||
|
||
private String getEntryName() { | ||
String fileName = path.getFileName().toString(); | ||
return fileName.substring(0, fileName.lastIndexOf(".bz2")); | ||
} | ||
|
||
@Override | ||
public DataSource toDataSource(String filename) { | ||
return new Bzip2FileDataSource(path.getParent(), filename); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
commons/src/main/java/com/powsybl/commons/datastore/DataEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Copyright (c) 2020, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package com.powsybl.commons.datastore; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* @author Giovanni Ferrari <giovanni.ferrari at techrain.eu> | ||
*/ | ||
public class DataEntry { | ||
|
||
private final String name; | ||
|
||
private final List<String> tags = new ArrayList<>(); | ||
|
||
public DataEntry(String name, String... tags) { | ||
this.name = Objects.requireNonNull(name); | ||
if (tags != null) { | ||
mathbagu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.tags.addAll(Arrays.asList(tags)); | ||
} | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public List<String> getTags() { | ||
return Collections.unmodifiableList(tags); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As in gzip implementation, I think we should throw an IOException if the entryName does not match the registered one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done