diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2024-01-15 10:23:18 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-15 10:23:18 +0100 |
commit | 29a807d35ac5d9e76ea1b8d653bb25b0e4e2dc73 (patch) | |
tree | d55fddad443566300bd4a7fdd3ef1118a8460700 /vespajlib | |
parent | 48b1bae2a6cdf58a237aa7be59632a06aba86861 (diff) | |
parent | 252fbeed13b8622fbc813620dc3b4e45abc6bbe2 (diff) |
Merge branch 'master' into balder/sliced-parallell-or
Diffstat (limited to 'vespajlib')
9 files changed, 30 insertions, 155 deletions
diff --git a/vespajlib/abi-spec.json b/vespajlib/abi-spec.json index a97950415e4..10b0478b5b0 100644 --- a/vespajlib/abi-spec.json +++ b/vespajlib/abi-spec.json @@ -3420,7 +3420,8 @@ "public static org.w3c.dom.Element getChild(org.w3c.dom.Element, java.lang.String)", "public static java.util.Optional getChildValue(org.w3c.dom.Element, java.lang.String)", "public static java.lang.String getNodePath(org.w3c.dom.Node, java.lang.String)", - "public static boolean isName(java.lang.CharSequence)" + "public static boolean isName(java.lang.CharSequence)", + "public static javax.xml.transform.TransformerFactory createTransformerFactory()" ], "fields" : [ ] }, diff --git a/vespajlib/pom.xml b/vespajlib/pom.xml index 16ae251aa8b..9d785faeae4 100644 --- a/vespajlib/pom.xml +++ b/vespajlib/pom.xml @@ -24,10 +24,6 @@ <artifactId>lz4-java</artifactId> </dependency> <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-exec</artifactId> - </dependency> - <dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna</artifactId> </dependency> diff --git a/vespajlib/src/main/java/com/yahoo/system/execution/ProcessExecutor.java b/vespajlib/src/main/java/com/yahoo/system/execution/ProcessExecutor.java deleted file mode 100644 index 691a5844596..00000000000 --- a/vespajlib/src/main/java/com/yahoo/system/execution/ProcessExecutor.java +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.system.execution; - -import org.apache.commons.exec.CommandLine; -import org.apache.commons.exec.DefaultExecutor; -import org.apache.commons.exec.ExecuteException; -import org.apache.commons.exec.ExecuteWatchdog; -import org.apache.commons.exec.PumpStreamHandler; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.util.Optional; -import java.util.concurrent.TimeUnit; - -/** - * Configurable system command executor that captures stdout and stderr. - * - * @author gjoranv - * @author bjorncs - */ -public class ProcessExecutor { - - public static class Builder { - private final int timeoutSeconds; - private int[] successExitCodes; - - public Builder(int timeoutSeconds) { - this.timeoutSeconds = timeoutSeconds; - } - - public Builder successExitCodes(int... successExitCodes) { - this.successExitCodes = successExitCodes; - return this; - } - - public ProcessExecutor build() { - return new ProcessExecutor(timeoutSeconds, successExitCodes); - } - } - - private ProcessExecutor(int timeoutSeconds, int[] successExitCodes) { - this.timeoutSeconds = timeoutSeconds; - this.successExitCodes = successExitCodes; - } - - public final int timeoutSeconds; - private final int[] successExitCodes; - - /** - * Convenience method to execute a process with no input data. See {@link #execute(String, String)} for details. - */ - public Optional<ProcessResult> execute(String command) throws IOException { - return execute(command, null); - } - - /** - * Executes the given command synchronously. - * - * @param command The command to execute. - * @param processInput Input provided to the process. - * @return The result of the execution, or empty if the process does not terminate within the timeout set for this executor. - * @throws IOException if the process execution failed. - */ - public Optional<ProcessResult> execute(String command, String processInput) throws IOException { - ByteArrayOutputStream processErr = new ByteArrayOutputStream(); - ByteArrayOutputStream processOut = new ByteArrayOutputStream(); - - DefaultExecutor executor = new DefaultExecutor(); - executor.setStreamHandler(createStreamHandler(processOut, processErr, processInput)); - ExecuteWatchdog watchDog = new ExecuteWatchdog(TimeUnit.SECONDS.toMillis(timeoutSeconds)); - executor.setWatchdog(watchDog); - executor.setExitValues(successExitCodes); - - int exitCode; - try { - exitCode = executor.execute(CommandLine.parse(command)); - } catch (ExecuteException e) { - exitCode = e.getExitValue(); - } - return (watchDog.killedProcess()) ? - Optional.empty() : Optional.of(new ProcessResult(exitCode, processOut.toString(), processErr.toString())); - } - - private static PumpStreamHandler createStreamHandler(ByteArrayOutputStream processOut, - ByteArrayOutputStream processErr, - String input) { - if (input != null) { - InputStream processInput = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)); - return new PumpStreamHandler(processOut, processErr, processInput); - } else { - return new PumpStreamHandler(processOut, processErr); - } - } - -} diff --git a/vespajlib/src/main/java/com/yahoo/system/execution/ProcessResult.java b/vespajlib/src/main/java/com/yahoo/system/execution/ProcessResult.java deleted file mode 100644 index 03ded02848f..00000000000 --- a/vespajlib/src/main/java/com/yahoo/system/execution/ProcessResult.java +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.system.execution; - -/** - * @author bjorncs - * @author gjoranv - */ -public class ProcessResult { - public final String stdOut; - public final String stdErr; - public final int exitCode; - - public ProcessResult(int exitCode, String stdOut, String stdErr) { - this.exitCode = exitCode; - this.stdOut = stdOut; - this.stdErr = stdErr; - } - -} diff --git a/vespajlib/src/main/java/com/yahoo/system/execution/package-info.java b/vespajlib/src/main/java/com/yahoo/system/execution/package-info.java deleted file mode 100644 index a2fce8038fe..00000000000 --- a/vespajlib/src/main/java/com/yahoo/system/execution/package-info.java +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -@ExportPackage -package com.yahoo.system.execution; - -import com.yahoo.osgi.annotation.ExportPackage; diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Slice.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Slice.java index 97414d8859a..807f56b1a49 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Slice.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Slice.java @@ -278,10 +278,11 @@ public class Slice<NAMETYPE extends Name> extends PrimitiveTensorFunction<NAMETY } private String valueToString(ToStringContext<NAMETYPE> context) { - if (label != null) - return label; - else + if (label != null) { + return TensorAddress.labelToString(label); + } else { return index.toString(context); + } } @Override diff --git a/vespajlib/src/main/java/com/yahoo/text/XML.java b/vespajlib/src/main/java/com/yahoo/text/XML.java index a6e36a0c3e1..72a2dba54e1 100644 --- a/vespajlib/src/main/java/com/yahoo/text/XML.java +++ b/vespajlib/src/main/java/com/yahoo/text/XML.java @@ -9,9 +9,11 @@ import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; +import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerFactory; import java.io.File; import java.io.IOException; import java.io.Reader; @@ -446,6 +448,19 @@ public class XML { } /** + * Creates a new XML TransformerFactory. + * + * @return a TransformerFactory + */ + public static TransformerFactory createTransformerFactory() { + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); + transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); + return transformerFactory; + } + + + /** * The point of this weird class and the jumble of abstract methods is * linking the scan for characters that must be quoted into the quoting * table, and making it actual work to make them go out of sync again. diff --git a/vespajlib/src/test/java/com/yahoo/system/execution/ProcessExecutorTest.java b/vespajlib/src/test/java/com/yahoo/system/execution/ProcessExecutorTest.java deleted file mode 100644 index 88ad9c18134..00000000000 --- a/vespajlib/src/test/java/com/yahoo/system/execution/ProcessExecutorTest.java +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.system.execution; - -import org.junit.Test; - -import java.util.Optional; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * @author gjoranv - */ -public class ProcessExecutorTest { - - @Test - public void echo_can_be_executed() throws Exception { - final String message = "Hello from executor!"; - ProcessExecutor executor = new ProcessExecutor.Builder(10).build(); - Optional<ProcessResult> result = executor.execute("echo " + message); - assertTrue(result.isPresent()); - assertEquals(message, result.get().stdOut.trim()); - assertEquals("", result.get().stdErr); - } -} diff --git a/vespajlib/src/test/java/com/yahoo/tensor/functions/SliceTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/functions/SliceTestCase.java index 94a06a416a5..3a21017ded4 100644 --- a/vespajlib/src/test/java/com/yahoo/tensor/functions/SliceTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/tensor/functions/SliceTestCase.java @@ -135,4 +135,13 @@ public class SliceTestCase { .toString()); } + @Test + public void testLabelsWithSpaceToString() { + Tensor input = Tensor.from("tensor(key{}):{a:1, 'b c':2}"); + assertEquals("tensor(key{}):{a:1.0, 'b c':2.0}{key:'b c'}", + new Slice<>(new ConstantTensor<>(input), + List.of(new Slice.DimensionValue<>("key", "b c"))) + .toString()); + } + } |