diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-12-01 07:36:44 +0100 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2022-12-01 07:36:44 +0100 |
commit | 00e7d63e41842231528343a6e80ede595d997ff5 (patch) | |
tree | d611749f67d8ac3201b1a39b516339755715f236 /searchlib | |
parent | c42b104ac2a231cb120719dd904d5ad2ac31fbeb (diff) |
- Reduce usage of guava.
- Ensure that tests relying on order are determinsitic.
Diffstat (limited to 'searchlib')
7 files changed, 42 insertions, 60 deletions
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/ranking/features/fieldmatch/Field.java b/searchlib/src/main/java/com/yahoo/searchlib/ranking/features/fieldmatch/Field.java index 9492cebc608..3f711df4fdd 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/ranking/features/fieldmatch/Field.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/ranking/features/fieldmatch/Field.java @@ -1,8 +1,7 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.ranking.features.fieldmatch; -import com.google.common.collect.ImmutableList; - +import java.util.Arrays; import java.util.List; /** @@ -12,19 +11,17 @@ import java.util.List; */ public class Field { - private final ImmutableList<Term> terms; + private final List<Term> terms; /** Creates a field from a space-separated string */ public Field(String fieldString) { - ImmutableList.Builder<Term> list = new ImmutableList.Builder<>(); - for (String term : fieldString.split(" ")) - list.add(new Term(term)); - this.terms = list.build(); + terms = Arrays.stream(fieldString.split(" ")).map(Term::new).toList(); + } /** Creates a field from a list of terms */ public Field(List<Term> terms) { - this.terms = ImmutableList.copyOf(terms); + this.terms = List.copyOf(terms); } /** Returns an immutable list of the terms in this */ diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/ExpressionFunction.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/ExpressionFunction.java index eac87ff2f12..241a53fb458 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/ExpressionFunction.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/ExpressionFunction.java @@ -1,8 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.rankingexpression; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.yahoo.searchlib.rankingexpression.rule.ConstantNode; import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode; import com.yahoo.searchlib.rankingexpression.rule.FunctionNode; @@ -36,10 +34,10 @@ import java.util.Optional; public class ExpressionFunction { private final String name; - private final ImmutableList<String> arguments; + private final List<String> arguments; /** Types of the inputs, if known. The keys here is any subset (including empty and identity) of the argument list */ - private final ImmutableMap<String, TensorType> argumentTypes; + private final Map<String, TensorType> argumentTypes; private final RankingExpression body; private final Optional<TensorType> returnType; @@ -62,17 +60,17 @@ public class ExpressionFunction { * @param body the ranking expression that defines this function */ public ExpressionFunction(String name, List<String> arguments, RankingExpression body) { - this(name, arguments, body, ImmutableMap.of(), Optional.empty()); + this(name, arguments, body, Map.of(), Optional.empty()); } public ExpressionFunction(String name, List<String> arguments, RankingExpression body, Map<String, TensorType> argumentTypes, Optional<TensorType> returnType) { this.name = Objects.requireNonNull(name, "name cannot be null"); - this.arguments = arguments==null ? ImmutableList.of() : ImmutableList.copyOf(arguments); + this.arguments = arguments==null ? List.of() : List.copyOf(arguments); this.body = Objects.requireNonNull(body, "body cannot be null"); if ( ! this.arguments.containsAll(argumentTypes.keySet())) throw new IllegalArgumentException("Argument type keys must be a subset of the argument keys"); - this.argumentTypes = ImmutableMap.copyOf(argumentTypes); + this.argumentTypes = Map.copyOf(argumentTypes); this.returnType = Objects.requireNonNull(returnType, "returnType cannot be null"); } diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/AbstractArrayContext.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/AbstractArrayContext.java index 340556b7e2d..4d6ac0104c7 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/AbstractArrayContext.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/AbstractArrayContext.java @@ -1,11 +1,12 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.rankingexpression.evaluation; -import com.google.common.collect.ImmutableMap; +import com.yahoo.lang.MutableInteger; import com.yahoo.searchlib.rankingexpression.RankingExpression; import com.yahoo.searchlib.rankingexpression.rule.CompositeNode; import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode; import com.yahoo.searchlib.rankingexpression.rule.ReferenceNode; +import com.yahoo.stream.CustomCollectors; import java.util.BitSet; import java.util.LinkedHashSet; @@ -116,7 +117,7 @@ public abstract class AbstractArrayContext extends Context implements Cloneable, private static class IndexedBindings implements Cloneable { /** The mapping from variable name to index */ - private final ImmutableMap<String, Integer> nameToIndex; + private final Map<String, Integer> nameToIndex; /** The current values set, pre-converted to doubles */ private double[] doubleValues; @@ -125,7 +126,7 @@ public abstract class AbstractArrayContext extends Context implements Cloneable, private BitSet setValues; /** Value to return if value is missing. */ - private double missingValue; + private final double missingValue; public IndexedBindings(RankingExpression expression, Value missingValue) { Set<String> bindTargets = new LinkedHashSet<>(); @@ -138,11 +139,8 @@ public abstract class AbstractArrayContext extends Context implements Cloneable, doubleValues[i] = this.missingValue; } - int i = 0; - ImmutableMap.Builder<String, Integer> nameToIndexBuilder = new ImmutableMap.Builder<>(); - for (String variable : bindTargets) - nameToIndexBuilder.put(variable,i++); - nameToIndex = nameToIndexBuilder.build(); + MutableInteger index = new MutableInteger(0); + nameToIndex = bindTargets.stream().collect(CustomCollectors.toLinkedMap(name -> name, name -> index.next())); } private void extractBindTargets(ExpressionNode node, Set<String> bindTargets) { @@ -152,8 +150,7 @@ public abstract class AbstractArrayContext extends Context implements Cloneable, ": Array lookup is not supported with features having arguments)"); bindTargets.add(node.toString()); } - else if (node instanceof CompositeNode) { - CompositeNode cNode = (CompositeNode)node; + else if (node instanceof CompositeNode cNode) { for (ExpressionNode child : cNode.children()) extractBindTargets(child, bindTargets); } diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Arguments.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Arguments.java index e770e6ac038..b716b693011 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Arguments.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Arguments.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.rankingexpression.rule; -import com.google.common.collect.ImmutableList; import com.yahoo.searchlib.rankingexpression.evaluation.Context; import com.yahoo.searchlib.rankingexpression.evaluation.Value; @@ -18,27 +17,23 @@ public final class Arguments implements Serializable { public static final Arguments EMPTY = new Arguments(); - private final ImmutableList<ExpressionNode> expressions; + private final List<ExpressionNode> expressions; public Arguments() { - this(ImmutableList.of()); + this(List.of()); } public Arguments(ExpressionNode singleArgument) { - this(ImmutableList.of(singleArgument)); + this(List.of(singleArgument)); } public Arguments(List<? extends ExpressionNode> expressions) { if (expressions == null) { - this.expressions = ImmutableList.of(); + this.expressions = List.of(); return; } - // Build in a roundabout way because java generics and lists - ImmutableList.Builder<ExpressionNode> b = ImmutableList.builder(); - for (ExpressionNode node : expressions) - b.add(node); - this.expressions = b.build(); + this.expressions = List.copyOf(expressions); } /** Returns an unmodifiable list of the expressions in this, never null */ diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/LambdaFunctionNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/LambdaFunctionNode.java index 97e9a74f9c8..9c0c0e46804 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/LambdaFunctionNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/LambdaFunctionNode.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.rankingexpression.rule; -import com.google.common.collect.ImmutableList; import com.yahoo.searchlib.rankingexpression.Reference; import com.yahoo.searchlib.rankingexpression.evaluation.Context; import com.yahoo.searchlib.rankingexpression.evaluation.MapContext; @@ -27,7 +26,7 @@ import java.util.stream.Collectors; */ public class LambdaFunctionNode extends CompositeNode { - private final ImmutableList<String> arguments; + private final List<String> arguments; private final ExpressionNode functionExpression; public LambdaFunctionNode(List<String> arguments, ExpressionNode functionExpression) { @@ -37,7 +36,7 @@ public class LambdaFunctionNode extends CompositeNode { .filter(f -> ! arguments.contains(f)) .collect(Collectors.joining(", "))); } - this.arguments = ImmutableList.copyOf(arguments); + this.arguments = List.copyOf(arguments); this.functionExpression = functionExpression; } @@ -106,15 +105,12 @@ public class LambdaFunctionNode extends CompositeNode { } private Optional<DoubleBinaryOperator> getDirectEvaluator() { - if ( ! (functionExpression instanceof OperationNode)) { + if ( ! (functionExpression instanceof OperationNode node)) { return Optional.empty(); } - OperationNode node = (OperationNode) functionExpression; - if ( ! (node.children().get(0) instanceof ReferenceNode) || ! (node.children().get(1) instanceof ReferenceNode)) { + if ( ! (node.children().get(0) instanceof ReferenceNode lhs) || ! (node.children().get(1) instanceof ReferenceNode rhs)) { return Optional.empty(); } - var lhs = (ReferenceNode) node.children().get(0); - var rhs = (ReferenceNode) node.children().get(1); if (! lhs.getName().equals(arguments.get(0)) || ! rhs.getName().equals(arguments.get(1))) { return Optional.empty(); } @@ -122,17 +118,17 @@ public class LambdaFunctionNode extends CompositeNode { return Optional.empty(); } Operator operator = node.operators().get(0); - switch (operator) { - case or: return asFunctionExpression((left, right) -> ((left != 0.0) || (right != 0.0)) ? 1.0 : 0.0); - case and: return asFunctionExpression((left, right) -> ((left != 0.0) && (right != 0.0)) ? 1.0 : 0.0); - case plus: return asFunctionExpression((left, right) -> left + right); - case minus: return asFunctionExpression((left, right) -> left - right); - case multiply: return asFunctionExpression((left, right) -> left * right); - case divide: return asFunctionExpression((left, right) -> left / right); - case modulo: return asFunctionExpression((left, right) -> left % right); - case power: return asFunctionExpression(Math::pow); - } - return Optional.empty(); + return switch (operator) { + case or -> asFunctionExpression((left, right) -> ((left != 0.0) || (right != 0.0)) ? 1.0 : 0.0); + case and -> asFunctionExpression((left, right) -> ((left != 0.0) && (right != 0.0)) ? 1.0 : 0.0); + case plus -> asFunctionExpression((left, right) -> left + right); + case minus -> asFunctionExpression((left, right) -> left - right); + case multiply -> asFunctionExpression((left, right) -> left * right); + case divide -> asFunctionExpression((left, right) -> left / right); + case modulo -> asFunctionExpression((left, right) -> left % right); + case power -> asFunctionExpression(Math::pow); + default -> Optional.empty(); + }; } private Optional<DoubleBinaryOperator> asFunctionExpression(DoubleBinaryOperator operator) { diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/SetMembershipNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/SetMembershipNode.java index 5da2fbfe624..6b87f75d884 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/SetMembershipNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/SetMembershipNode.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.rankingexpression.rule; -import com.google.common.collect.ImmutableList; import com.yahoo.searchlib.rankingexpression.Reference; import com.yahoo.searchlib.rankingexpression.evaluation.BooleanValue; import com.yahoo.searchlib.rankingexpression.evaluation.Context; @@ -30,7 +29,7 @@ public class SetMembershipNode extends BooleanNode { public SetMembershipNode(ExpressionNode testValue, List<ExpressionNode> setValues) { this.testValue = testValue; - this.setValues = ImmutableList.copyOf(setValues); + this.setValues = List.copyOf(setValues); } /** The value to check for membership in the set */ diff --git a/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/test/FieldMatchMetricsTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/test/FieldMatchMetricsTestCase.java index f4a003868f8..41a6cd69878 100644 --- a/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/test/FieldMatchMetricsTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/test/FieldMatchMetricsTestCase.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.ranking.features.fieldmatch.test; -import com.google.common.collect.ImmutableList; import com.yahoo.searchlib.ranking.features.fieldmatch.Field; import com.yahoo.searchlib.ranking.features.fieldmatch.FieldMatchMetrics; import com.yahoo.searchlib.ranking.features.fieldmatch.FieldMatchMetricsComputer; @@ -10,6 +9,7 @@ import com.yahoo.searchlib.ranking.features.fieldmatch.QueryTerm; import com.yahoo.searchlib.ranking.features.fieldmatch.Query; import org.junit.Test; +import java.util.ArrayList; import java.util.List; import static org.junit.Assert.assertEquals; @@ -776,9 +776,9 @@ public class FieldMatchMetricsTestCase { } private Field toField(String fieldString) { - if (fieldString.length() == 0) return new Field(ImmutableList.of()); + if (fieldString.length() == 0) return new Field(List.of()); - ImmutableList.Builder<Field.Term> terms = new ImmutableList.Builder<>(); + List<Field.Term> terms = new ArrayList<>(); for (String termString : fieldString.split(" ")) { String[] colonSplit = termString.split(":"); if (colonSplit.length > 1) @@ -786,7 +786,7 @@ public class FieldMatchMetricsTestCase { else terms.add(new Field.Term(colonSplit[0])); } - return new Field(terms.build()); + return new Field(List.copyOf(terms)); } } |