SOLR-10743: Add sequence StreamEvaluator

This commit is contained in:
Joel Bernstein 2017-05-25 11:20:15 -04:00
parent 7fef7e3374
commit 0b47126e5c
3 changed files with 126 additions and 0 deletions

View File

@ -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)

View File

@ -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<Number> 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<Number> 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());
}
}

View File

@ -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<Tuple> tuples = getTuples(solrStream);
assertTrue(tuples.size() == 1);
List<Number> sequence = (List<Number>)tuples.get(0).get("seq");
assertTrue(sequence.size() == 20);
for(int i=0; i<sequence.size(); i++) {
assertTrue(sequence.get(i).intValue() == i);
}
//Change the size, stride
expr = "tuple(seq=sequence(100, 0, 4))";
paramsLoc = new ModifiableSolrParams();
paramsLoc.set("expr", expr);
paramsLoc.set("qt", "/stream");
solrStream = new SolrStream(url, paramsLoc);
tuples = getTuples(solrStream);
assertTrue(tuples.size() == 1);
sequence = (List<Number>)tuples.get(0).get("seq");
assertTrue(sequence.size() == 100);
for(int i=0; i<sequence.size(); i++) {
assertTrue(sequence.get(i).intValue() == (i*4));
}
//Change the start
expr = "tuple(seq=sequence(100, 10, 1))";
paramsLoc = new ModifiableSolrParams();
paramsLoc.set("expr", expr);
paramsLoc.set("qt", "/stream");
solrStream = new SolrStream(url, paramsLoc);
tuples = getTuples(solrStream);
assertTrue(tuples.size() == 1);
sequence = (List<Number>)tuples.get(0).get("seq");
assertTrue(sequence.size() == 100);
for(int i=0; i<sequence.size(); i++) {
assertTrue(sequence.get(i).intValue() == (i+10));
}
}
@Test
public void testEvalStream() throws Exception {
UpdateRequest updateRequest = new UpdateRequest();