aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/ArrayContext.java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/ArrayContext.java
Publish
Diffstat (limited to 'searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/ArrayContext.java')
-rw-r--r--searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/ArrayContext.java120
1 files changed, 120 insertions, 0 deletions
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/ArrayContext.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/ArrayContext.java
new file mode 100644
index 00000000000..b9ff630198e
--- /dev/null
+++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/ArrayContext.java
@@ -0,0 +1,120 @@
+// Copyright 2016 Yahoo Inc. 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.searchlib.rankingexpression.RankingExpression;
+
+import java.util.Arrays;
+
+/**
+ * Creates a context which supports array index based lookup.
+ * This instance may be reused indefinitely for evaluations of a single
+ * ranking expression, in a single thread at the time.
+ *
+ * @author bratseth
+ */
+public class ArrayContext extends AbstractArrayContext implements Cloneable {
+
+ /** The current values set */
+ private Value[] values;
+
+ private static DoubleValue constantZero = DoubleValue.frozen(0);
+
+ /**
+ * Create a fast lookup context for an expression.
+ * This instance should be reused indefinitely by a single thread.
+ * This will fail if unknown values are attempted added.
+ */
+ public ArrayContext(RankingExpression expression) {
+ this(expression, false);
+ }
+
+ /**
+ * Create a fast lookup context for an expression.
+ * This instance should be reused indefinitely by a single thread.
+ *
+ * @param expression the expression to create a context for
+ * @param ignoreUnknownValues whether attempts to put values not present in this expression
+ * should fail (false - the default), or be ignored (true)
+ */
+ public ArrayContext(RankingExpression expression, boolean ignoreUnknownValues) {
+ super(expression, ignoreUnknownValues);
+ values = new Value[doubleValues().length];
+ Arrays.fill(values, DoubleValue.zero);
+ }
+
+ /**
+ * Puts a value by name.
+ * The value will be frozen if it isn't already.
+ *
+ * @throws IllegalArgumentException if the name is not present in the ranking expression this was created with, and
+ * ignoredUnknownValues is false
+ * @since 5.1.5
+ */
+ @Override
+ public final void put(String name, Value value) {
+ Integer index = nameToIndex().get(name);
+ if (index==null) {
+ if (ignoreUnknownValues())
+ return;
+ else
+ throw new IllegalArgumentException("Value '" + name + "' is not known to " + this);
+ }
+ put(index, value);
+ }
+
+ /** Same as put(index,DoubleValue.frozen(value)) */
+ public final void put(int index, double value) {
+ put(index, DoubleValue.frozen(value));
+ }
+
+ /**
+ * Puts a value by index.
+ * The value will be frozen if it isn't already.
+ *
+ * @since 5.1.5
+ */
+ public final void put(int index, Value value) {
+ values[index]=value.freeze();
+ try {
+ doubleValues()[index]=value.asDouble();
+ }
+ catch (UnsupportedOperationException e) {
+ doubleValues()[index]=Double.NaN; // see getDouble below
+ }
+ }
+
+ /** Perform a slow lookup by name */
+ @Override
+ public Value get(String name) {
+ Integer index=nameToIndex().get(name);
+ if (index==null) return DoubleValue.zero;
+ return values[index];
+ }
+
+ /** Perform a fast lookup by index */
+ @Override
+ public final Value get(int index) {
+ return values[index];
+ }
+
+ /** Perform a fast lookup directly of the value as a double. This is faster than get(index).asDouble() */
+ @Override
+ public final double getDouble(int index) {
+ double value=doubleValues()[index];
+ if (value==Double.NaN)
+ throw new UnsupportedOperationException("Value at " + index + " has no double representation");
+ return value;
+ }
+
+ /**
+ * Creates a clone of this context suitable for evaluating against the same ranking expression
+ * in a different thread (i.e, name name to index map, different value set.
+ */
+ public ArrayContext clone() {
+ ArrayContext clone=(ArrayContext)super.clone();
+ clone.values = new Value[nameToIndex().size()];
+ Arrays.fill(values,constantZero);
+ return clone;
+ }
+
+}