summaryrefslogtreecommitdiffstats
path: root/searchlib/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'searchlib/src/main')
-rw-r--r--searchlib/src/main/java/com/yahoo/searchlib/ranking/features/fieldmatch/Field.java13
-rwxr-xr-xsearchlib/src/main/java/com/yahoo/searchlib/rankingexpression/ExpressionFunction.java12
-rw-r--r--searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/AbstractArrayContext.java17
-rw-r--r--searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Arguments.java15
-rw-r--r--searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/LambdaFunctionNode.java34
-rw-r--r--searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/SetMembershipNode.java3
6 files changed, 56 insertions, 38 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 3f711df4fdd..9492cebc608 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,7 +1,8 @@
// 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 java.util.Arrays;
+import com.google.common.collect.ImmutableList;
+
import java.util.List;
/**
@@ -11,17 +12,19 @@ import java.util.List;
*/
public class Field {
- private final List<Term> terms;
+ private final ImmutableList<Term> terms;
/** Creates a field from a space-separated string */
public Field(String fieldString) {
- terms = Arrays.stream(fieldString.split(" ")).map(Term::new).toList();
-
+ ImmutableList.Builder<Term> list = new ImmutableList.Builder<>();
+ for (String term : fieldString.split(" "))
+ list.add(new Term(term));
+ this.terms = list.build();
}
/** Creates a field from a list of terms */
public Field(List<Term> terms) {
- this.terms = List.copyOf(terms);
+ this.terms = ImmutableList.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 241a53fb458..eac87ff2f12 100755
--- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/ExpressionFunction.java
+++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/ExpressionFunction.java
@@ -1,6 +1,8 @@
// 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;
@@ -34,10 +36,10 @@ import java.util.Optional;
public class ExpressionFunction {
private final String name;
- private final List<String> arguments;
+ private final ImmutableList<String> arguments;
/** Types of the inputs, if known. The keys here is any subset (including empty and identity) of the argument list */
- private final Map<String, TensorType> argumentTypes;
+ private final ImmutableMap<String, TensorType> argumentTypes;
private final RankingExpression body;
private final Optional<TensorType> returnType;
@@ -60,17 +62,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, Map.of(), Optional.empty());
+ this(name, arguments, body, ImmutableMap.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 ? List.of() : List.copyOf(arguments);
+ this.arguments = arguments==null ? ImmutableList.of() : ImmutableList.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 = Map.copyOf(argumentTypes);
+ this.argumentTypes = ImmutableMap.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 4d6ac0104c7..340556b7e2d 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,12 +1,11 @@
// 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.yahoo.lang.MutableInteger;
+import com.google.common.collect.ImmutableMap;
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;
@@ -117,7 +116,7 @@ public abstract class AbstractArrayContext extends Context implements Cloneable,
private static class IndexedBindings implements Cloneable {
/** The mapping from variable name to index */
- private final Map<String, Integer> nameToIndex;
+ private final ImmutableMap<String, Integer> nameToIndex;
/** The current values set, pre-converted to doubles */
private double[] doubleValues;
@@ -126,7 +125,7 @@ public abstract class AbstractArrayContext extends Context implements Cloneable,
private BitSet setValues;
/** Value to return if value is missing. */
- private final double missingValue;
+ private double missingValue;
public IndexedBindings(RankingExpression expression, Value missingValue) {
Set<String> bindTargets = new LinkedHashSet<>();
@@ -139,8 +138,11 @@ public abstract class AbstractArrayContext extends Context implements Cloneable,
doubleValues[i] = this.missingValue;
}
- MutableInteger index = new MutableInteger(0);
- nameToIndex = bindTargets.stream().collect(CustomCollectors.toLinkedMap(name -> name, name -> index.next()));
+ int i = 0;
+ ImmutableMap.Builder<String, Integer> nameToIndexBuilder = new ImmutableMap.Builder<>();
+ for (String variable : bindTargets)
+ nameToIndexBuilder.put(variable,i++);
+ nameToIndex = nameToIndexBuilder.build();
}
private void extractBindTargets(ExpressionNode node, Set<String> bindTargets) {
@@ -150,7 +152,8 @@ 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 cNode) {
+ else if (node instanceof CompositeNode) {
+ CompositeNode cNode = (CompositeNode)node;
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 b716b693011..e770e6ac038 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,6 +1,7 @@
// 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;
@@ -17,23 +18,27 @@ public final class Arguments implements Serializable {
public static final Arguments EMPTY = new Arguments();
- private final List<ExpressionNode> expressions;
+ private final ImmutableList<ExpressionNode> expressions;
public Arguments() {
- this(List.of());
+ this(ImmutableList.of());
}
public Arguments(ExpressionNode singleArgument) {
- this(List.of(singleArgument));
+ this(ImmutableList.of(singleArgument));
}
public Arguments(List<? extends ExpressionNode> expressions) {
if (expressions == null) {
- this.expressions = List.of();
+ this.expressions = ImmutableList.of();
return;
}
- this.expressions = List.copyOf(expressions);
+ // 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();
}
/** 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 9c0c0e46804..97e9a74f9c8 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,6 +1,7 @@
// 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;
@@ -26,7 +27,7 @@ import java.util.stream.Collectors;
*/
public class LambdaFunctionNode extends CompositeNode {
- private final List<String> arguments;
+ private final ImmutableList<String> arguments;
private final ExpressionNode functionExpression;
public LambdaFunctionNode(List<String> arguments, ExpressionNode functionExpression) {
@@ -36,7 +37,7 @@ public class LambdaFunctionNode extends CompositeNode {
.filter(f -> ! arguments.contains(f))
.collect(Collectors.joining(", ")));
}
- this.arguments = List.copyOf(arguments);
+ this.arguments = ImmutableList.copyOf(arguments);
this.functionExpression = functionExpression;
}
@@ -105,12 +106,15 @@ public class LambdaFunctionNode extends CompositeNode {
}
private Optional<DoubleBinaryOperator> getDirectEvaluator() {
- if ( ! (functionExpression instanceof OperationNode node)) {
+ if ( ! (functionExpression instanceof OperationNode)) {
return Optional.empty();
}
- if ( ! (node.children().get(0) instanceof ReferenceNode lhs) || ! (node.children().get(1) instanceof ReferenceNode rhs)) {
+ OperationNode node = (OperationNode) functionExpression;
+ if ( ! (node.children().get(0) instanceof ReferenceNode) || ! (node.children().get(1) instanceof ReferenceNode)) {
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();
}
@@ -118,17 +122,17 @@ public class LambdaFunctionNode extends CompositeNode {
return Optional.empty();
}
Operator operator = node.operators().get(0);
- 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();
- };
+ 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();
}
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 6b87f75d884..5da2fbfe624 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,6 +1,7 @@
// 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;
@@ -29,7 +30,7 @@ public class SetMembershipNode extends BooleanNode {
public SetMembershipNode(ExpressionNode testValue, List<ExpressionNode> setValues) {
this.testValue = testValue;
- this.setValues = List.copyOf(setValues);
+ this.setValues = ImmutableList.copyOf(setValues);
}
/** The value to check for membership in the set */