From 0b47126e5c10cb6a092c4f49e4b1835bd15aa878 Mon Sep 17 00:00:00 2001 From: Joel Bernstein Date: Thu, 25 May 2017 11:20:15 -0400 Subject: [PATCH] SOLR-10743: Add sequence StreamEvaluator --- .../apache/solr/handler/StreamHandler.java | 1 + .../solrj/io/eval/SequenceEvaluator.java | 72 +++++++++++++++++++ .../solrj/io/stream/StreamExpressionTest.java | 53 ++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SequenceEvaluator.java diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java index 98805127497..b219a2c1e2b 100644 --- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java @@ -186,6 +186,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware, .withFunctionName("empiricalDistribution", EmpiricalDistributionEvaluator.class) .withFunctionName("describe", DescribeEvaluator.class) .withFunctionName("finddelay", FindDelayEvaluator.class) + .withFunctionName("sequence", SequenceEvaluator.class) // metrics .withFunctionName("min", MinMetric.class) diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SequenceEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SequenceEvaluator.java new file mode 100644 index 00000000000..a88d695afaf --- /dev/null +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SequenceEvaluator.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.solr.client.solrj.io.eval; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.math3.util.MathArrays; +import org.apache.solr.client.solrj.io.Tuple; +import org.apache.solr.client.solrj.io.stream.expr.Explanation; +import org.apache.solr.client.solrj.io.stream.expr.Explanation.ExpressionType; +import org.apache.solr.client.solrj.io.stream.expr.Expressible; +import org.apache.solr.client.solrj.io.stream.expr.StreamExpression; +import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParameter; +import org.apache.solr.client.solrj.io.stream.expr.StreamFactory; + +public class SequenceEvaluator extends ComplexEvaluator implements Expressible { + + private static final long serialVersionUID = 1; + + public SequenceEvaluator(StreamExpression expression, StreamFactory factory) throws IOException { + super(expression, factory); + } + + public List evaluate(Tuple tuple) throws IOException { + StreamEvaluator sizeEval = subEvaluators.get(0); + StreamEvaluator startEval = subEvaluators.get(1); + StreamEvaluator strideEval = subEvaluators.get(2); + + Number sizeNum = (Number)sizeEval.evaluate(tuple); + Number startNum = (Number)startEval.evaluate(tuple); + Number strideNum = (Number)strideEval.evaluate(tuple); + + int[] sequence = MathArrays.sequence(sizeNum.intValue(), startNum.intValue(), strideNum.intValue()); + List numbers = new ArrayList(sequence.length); + for(int i : sequence) { + numbers.add(i); + } + + return numbers; + } + + @Override + public StreamExpressionParameter toExpression(StreamFactory factory) throws IOException { + StreamExpression expression = new StreamExpression(factory.getFunctionName(getClass())); + return expression; + } + + @Override + public Explanation toExplanation(StreamFactory factory) throws IOException { + return new Explanation(nodeId.toString()) + .withExpressionType(ExpressionType.EVALUATOR) + .withFunctionName(factory.getFunctionName(getClass())) + .withImplementingClass(getClass().getName()) + .withExpression(toExpression(factory).toString()); + } +} \ No newline at end of file diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java index ab792f9feac..0dff6fcf8cc 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java @@ -5206,6 +5206,59 @@ public class StreamExpressionTest extends SolrCloudTestCase { } + @Test + public void testSequence() throws Exception { + String expr = "tuple(seq=sequence(20, 0, 1))"; + ModifiableSolrParams paramsLoc = new ModifiableSolrParams(); + paramsLoc.set("expr", expr); + paramsLoc.set("qt", "/stream"); + + String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString()+"/"+COLLECTIONORALIAS; + TupleStream solrStream = new SolrStream(url, paramsLoc); + + StreamContext context = new StreamContext(); + solrStream.setStreamContext(context); + List tuples = getTuples(solrStream); + assertTrue(tuples.size() == 1); + List sequence = (List)tuples.get(0).get("seq"); + assertTrue(sequence.size() == 20); + for(int i=0; i)tuples.get(0).get("seq"); + assertTrue(sequence.size() == 100); + for(int i=0; i)tuples.get(0).get("seq"); + assertTrue(sequence.size() == 100); + for(int i=0; i