diff options
author | Jon Bratseth <bratseth@oath.com> | 2018-08-17 12:34:47 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@oath.com> | 2018-08-17 12:34:47 +0200 |
commit | 2b98869db4f2c97a36094553d012068654958e5b (patch) | |
tree | 5ca968be7467f3cc667b1719ec9834c71b76ee7e | |
parent | b79f66322dd188deafa835ee86ba6313d8643b6f (diff) |
Avoid ApplicationPackage.getFiles
5 files changed, 34 insertions, 25 deletions
diff --git a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/FilesApplicationPackage.java b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/FilesApplicationPackage.java index 36009682022..7ca9bcf48f3 100644 --- a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/FilesApplicationPackage.java +++ b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/FilesApplicationPackage.java @@ -45,6 +45,7 @@ import java.security.MessageDigest; import java.util.*; import java.util.jar.JarFile; import java.util.logging.Logger; +import java.util.stream.Collectors; import static com.yahoo.text.Lowercase.toLowerCase; @@ -164,7 +165,7 @@ public class FilesApplicationPackage implements ApplicationPackage { return metaData; } - private List<NamedReader> getFiles(Path relativePath,String namePrefix,String suffix,boolean recurse) { + private List<NamedReader> getFiles(Path relativePath, String namePrefix, String suffix, boolean recurse) { try { List<NamedReader> readers=new ArrayList<>(); File dir = new File(appDir, relativePath.getRelative()); diff --git a/config-model-api/src/main/java/com/yahoo/config/application/api/ApplicationPackage.java b/config-model-api/src/main/java/com/yahoo/config/application/api/ApplicationPackage.java index a703a524e1d..a71a0878d3d 100644 --- a/config-model-api/src/main/java/com/yahoo/config/application/api/ApplicationPackage.java +++ b/config-model-api/src/main/java/com/yahoo/config/application/api/ApplicationPackage.java @@ -162,8 +162,8 @@ public interface ApplicationPackage { * Gets a file from the root of the application package * * - * @param relativePath The relative path of the file within this application package. - * @return reader for file + * @param relativePath the relative path of the file within this application package. + * @return information abut the file * @throws IllegalArgumentException if the given path does not exist */ ApplicationFile getFile(Path relativePath); diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/ConvertedModel.java b/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/ConvertedModel.java index e073be71a0c..3bd96c9db26 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/ConvertedModel.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/ConvertedModel.java @@ -39,6 +39,7 @@ import com.yahoo.tensor.serialization.TypedBinaryFormat; import java.io.BufferedReader; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.Reader; import java.io.StringReader; @@ -516,19 +517,19 @@ public class ConvertedModel { Map<String, RankingExpression> readExpressions() { Map<String, RankingExpression> expressions = new HashMap<>(); - List<NamedReader> expressionReaders = null; - try { - expressionReaders = application.getFiles(modelFiles.expressionsPath(), "expression"); - for (NamedReader expressionReader : expressionReaders) { - try { - expressions.put(expressionReader.getName(), new RankingExpression(expressionReader.getReader())); - } catch (ParseException e) { - throw new IllegalStateException("Could not parse " + expressionReader.getName(), e); - } + ApplicationFile expressionPath = application.getFile(modelFiles.expressionsPath()); + if ( ! expressionPath.exists() || ! expressionPath.isDirectory()) return Collections.emptyMap(); + for (ApplicationFile expressionFile : expressionPath.listFiles()) { + try { + String name = expressionFile.getPath().getName(); + expressions.put(name, new RankingExpression(name, expressionFile.createReader())); + } + catch (FileNotFoundException e) { + throw new IllegalStateException("Expression file removed while reading: " + expressionFile, e); + } + catch (ParseException e) { + throw new IllegalStateException("Invalid stored expression in " + expressionFile, e); } - } - finally { - expressionReaders.forEach(r -> close(r)); } return expressions; } @@ -688,7 +689,7 @@ public class ConvertedModel { } public Path expressionPath(String name) { - return storedModelPath().append("expressions").append(name + ".expression"); + return storedModelPath().append("expressions").append(name); } public Path expressionsPath() { diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/ZKApplicationPackage.java b/configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/ZKApplicationPackage.java index 2c46f591037..e9b4d6ac1aa 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/ZKApplicationPackage.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/ZKApplicationPackage.java @@ -24,12 +24,14 @@ import java.io.File; import java.io.Reader; import java.io.StringReader; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; /** * Represents an application residing in zookeeper. @@ -200,16 +202,17 @@ public class ZKApplicationPackage implements ApplicationPackage { return ret; } - //Returns readers for all the children of a node. - //The node is looked up relative to the location of the active application package - //in zookeeper. + /** + * Returns readers for all the children of a node. + * The node is looked up relative to the location of the active application package in zookeeper. + */ @Override - public List<NamedReader> getFiles(Path relativePath,String suffix,boolean recurse) { + public List<NamedReader> getFiles(Path relativePath, String suffix, boolean recurse) { return liveApp.getAllDataFromDirectory(ConfigCurator.USERAPP_ZK_SUBPATH + '/' + relativePath.getRelative(), suffix, recurse); } @Override - public ApplicationFile getFile(Path file) { // foo/bar/baz.json + public ApplicationFile getFile(Path file) { return new ZKApplicationFile(file, liveApp); } diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/ZKLiveApp.java b/configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/ZKLiveApp.java index d7d43dea022..956af02e36f 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/ZKLiveApp.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/ZKLiveApp.java @@ -69,7 +69,8 @@ public class ZKLiveApp { log.finer("ZKApplicationPackage: Skipped '" + child + "' (did not match suffix " + fileNameSuffix + ")"); } if (recursive) - result.addAll(getAllDataFromDirectory(path + "/" + child, namePrefix + child + "/", fileNameSuffix, recursive)); + result.addAll(getAllDataFromDirectory(path + "/" + child, + namePrefix + child + "/", fileNameSuffix, recursive)); } if (log.isLoggable(Level.FINE)) log.fine("ZKApplicationPackage: Found '" + result.size() + "' files in " + fullPath); @@ -80,14 +81,15 @@ public class ZKLiveApp { } /** - * Retrieves a node relative to the node of the live application, e.g. /vespa/config/apps/$lt;app_id>/<path>/<node> + * Retrieves a node relative to the node of the live application, + * e.g. /vespa/config/apps/$lt;app_id>/<path>/<node> * * @param path a path relative to the currently active application * @param node a path relative to the path above * @return a Reader that can be used to get the data */ public Reader getDataReader(String path, String node) { - final String data = getData(path, node); + String data = getData(path, node); if (data == null) { throw new IllegalArgumentException("No node for " + getFullPath(path) + "/" + node + " exists"); } @@ -98,7 +100,8 @@ public class ZKLiveApp { try { return zk.getData(getFullPath(path), node); } catch (Exception e) { - throw new IllegalArgumentException("Could not retrieve node '" + getFullPath(path) + "/" + node + "' in zookeeper", e); + throw new IllegalArgumentException("Could not retrieve node '" + + getFullPath(path) + "/" + node + "' in zookeeper", e); } } @@ -205,5 +208,6 @@ public class ZKLiveApp { } return reader(data); } + } |