use of org.apache.solr.client.solrj.io.stream.StreamContext in project lucene-solr by apache.
the class TestJavabinTupleStreamParser method testSimple.
public void testSimple() throws IOException {
List<Map<String, Object>> l = new ArrayList();
l.add(Utils.makeMap("id", 1, "f", 1.0f, "s", "Some str 1"));
l.add(Utils.makeMap("id", 2, "f", 2.0f, "s", "Some str 2"));
l.add(Utils.makeMap("id", 3, "f", 1.0f, "s", "Some str 3"));
l.add(Utils.makeMap("EOF", true, "RESPONSE_TIME", 206, "sleepMillis", 1000));
Iterator<Map<String, Object>> iterator = l.iterator();
TupleStream tupleStream = new TupleStream() {
@Override
public void setStreamContext(StreamContext context) {
}
@Override
public List<TupleStream> children() {
return null;
}
@Override
public void open() throws IOException {
}
@Override
public void close() throws IOException {
}
@Override
public Tuple read() throws IOException {
if (iterator.hasNext())
return new Tuple(iterator.next());
else
return null;
}
@Override
public StreamComparator getStreamSort() {
return null;
}
@Override
public Explanation toExplanation(StreamFactory factory) throws IOException {
return new StreamExplanation(getStreamNodeId().toString()).withFunctionName("Dummy").withImplementingClass(this.getClass().getName()).withExpressionType(Explanation.ExpressionType.STREAM_SOURCE).withExpression("--non-expressible--");
}
};
byte[] bytes = serialize(tupleStream);
JavabinTupleStreamParser parser = new JavabinTupleStreamParser(new ByteArrayInputStream(bytes), true);
Map m = parser.next();
assertEquals(1L, m.get("id"));
assertEquals(1.0, (Double) m.get("f"), 0.01);
m = parser.next();
assertEquals(2L, m.get("id"));
assertEquals(2.0, (Double) m.get("f"), 0.01);
m = parser.next();
assertEquals(3L, m.get("id"));
assertEquals(1.0, (Double) m.get("f"), 0.01);
m = parser.next();
assertEquals(Boolean.TRUE, m.get("EOF"));
parser = new JavabinTupleStreamParser(new ByteArrayInputStream(bytes), false);
m = parser.next();
assertEquals(1, m.get("id"));
assertEquals(1.0, (Float) m.get("f"), 0.01);
m = parser.next();
assertEquals(2, m.get("id"));
assertEquals(2.0, (Float) m.get("f"), 0.01);
m = parser.next();
assertEquals(3, m.get("id"));
assertEquals(1.0, (Float) m.get("f"), 0.01);
m = parser.next();
assertEquals(Boolean.TRUE, m.get("EOF"));
}
use of org.apache.solr.client.solrj.io.stream.StreamContext in project lucene-solr by apache.
the class ModelCache method getModel.
public Tuple getModel(String zkHost, String collection, String modelID, long checkMillis) throws IOException {
Model model = null;
long currentTime = new Date().getTime();
synchronized (this) {
model = models.get(modelID);
if (model != null && ((currentTime - model.getLastChecked()) <= checkMillis)) {
return model.getTuple();
}
if (model != null) {
//model is expired
models.remove(modelID);
}
}
//Model is not in cache or has expired so fetch the model
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("q", "name_s:" + modelID);
params.set("fl", "terms_ss, idfs_ds, weights_ds, iteration_i, _version_");
params.set(SORT, "iteration_i desc");
StreamContext streamContext = new StreamContext();
streamContext.setSolrClientCache(solrClientCache);
CloudSolrStream stream = new CloudSolrStream(zkHost, collection, params);
stream.setStreamContext(streamContext);
Tuple tuple = null;
try {
stream.open();
tuple = stream.read();
if (tuple.EOF) {
return null;
}
} finally {
stream.close();
}
synchronized (this) {
//check again to see if another thread has updated the same model
Model m = models.get(modelID);
if (m != null) {
Tuple t = m.getTuple();
long v = t.getLong(VERSION_FIELD);
if (v >= tuple.getLong(VERSION_FIELD)) {
return t;
} else {
models.put(modelID, new Model(tuple, currentTime));
return tuple;
}
} else {
models.put(modelID, new Model(tuple, currentTime));
return tuple;
}
}
}
use of org.apache.solr.client.solrj.io.stream.StreamContext in project lucene-solr by apache.
the class GraphExpressionTest method testGatherNodesFriendsStream.
@Test
public void testGatherNodesFriendsStream() throws Exception {
new UpdateRequest().add(id, "0", "from_s", "bill", "to_s", "jim", "message_t", "Hello jim").add(id, "1", "from_s", "bill", "to_s", "sam", "message_t", "Hello sam").add(id, "2", "from_s", "bill", "to_s", "max", "message_t", "Hello max").add(id, "3", "from_s", "max", "to_s", "kip", "message_t", "Hello kip").add(id, "4", "from_s", "sam", "to_s", "steve", "message_t", "Hello steve").add(id, "5", "from_s", "jim", "to_s", "ann", "message_t", "Hello steve").commit(cluster.getSolrClient(), COLLECTION);
List<Tuple> tuples = null;
GatherNodesStream stream = null;
StreamContext context = new StreamContext();
SolrClientCache cache = new SolrClientCache();
context.setSolrClientCache(cache);
StreamFactory factory = new StreamFactory().withCollectionZkHost("collection1", cluster.getZkServer().getZkAddress()).withFunctionName("gatherNodes", GatherNodesStream.class).withFunctionName("search", CloudSolrStream.class).withFunctionName("count", CountMetric.class).withFunctionName("hashJoin", HashJoinStream.class).withFunctionName("avg", MeanMetric.class).withFunctionName("sum", SumMetric.class).withFunctionName("min", MinMetric.class).withFunctionName("max", MaxMetric.class);
String expr = "gatherNodes(collection1, " + "walk=\"bill->from_s\"," + "gather=\"to_s\")";
stream = (GatherNodesStream) factory.constructStream(expr);
stream.setStreamContext(context);
tuples = getTuples(stream);
Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
assertTrue(tuples.size() == 3);
assertTrue(tuples.get(0).getString("node").equals("jim"));
assertTrue(tuples.get(1).getString("node").equals("max"));
assertTrue(tuples.get(2).getString("node").equals("sam"));
//Test scatter branches, leaves and trackTraversal
expr = "gatherNodes(collection1, " + "walk=\"bill->from_s\"," + "gather=\"to_s\"," + "scatter=\"branches, leaves\", trackTraversal=\"true\")";
stream = (GatherNodesStream) factory.constructStream(expr);
context = new StreamContext();
context.setSolrClientCache(cache);
stream.setStreamContext(context);
tuples = getTuples(stream);
Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
assertTrue(tuples.size() == 4);
assertTrue(tuples.get(0).getString("node").equals("bill"));
assertTrue(tuples.get(0).getLong("level").equals(new Long(0)));
assertTrue(tuples.get(0).getStrings("ancestors").size() == 0);
assertTrue(tuples.get(1).getString("node").equals("jim"));
assertTrue(tuples.get(1).getLong("level").equals(new Long(1)));
List<String> ancestors = tuples.get(1).getStrings("ancestors");
System.out.println("##################### Ancestors:" + ancestors);
assert (ancestors.size() == 1);
assert (ancestors.get(0).equals("bill"));
assertTrue(tuples.get(2).getString("node").equals("max"));
assertTrue(tuples.get(2).getLong("level").equals(new Long(1)));
ancestors = tuples.get(2).getStrings("ancestors");
assert (ancestors.size() == 1);
assert (ancestors.get(0).equals("bill"));
assertTrue(tuples.get(3).getString("node").equals("sam"));
assertTrue(tuples.get(3).getLong("level").equals(new Long(1)));
ancestors = tuples.get(3).getStrings("ancestors");
assert (ancestors.size() == 1);
assert (ancestors.get(0).equals("bill"));
// Test query root
expr = "gatherNodes(collection1, " + "search(collection1, q=\"message_t:jim\", fl=\"from_s\", sort=\"from_s asc\")," + "walk=\"from_s->from_s\"," + "gather=\"to_s\")";
stream = (GatherNodesStream) factory.constructStream(expr);
context = new StreamContext();
context.setSolrClientCache(cache);
stream.setStreamContext(context);
tuples = getTuples(stream);
Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
assertTrue(tuples.size() == 3);
assertTrue(tuples.get(0).getString("node").equals("jim"));
assertTrue(tuples.get(1).getString("node").equals("max"));
assertTrue(tuples.get(2).getString("node").equals("sam"));
// Test query root scatter branches
expr = "gatherNodes(collection1, " + "search(collection1, q=\"message_t:jim\", fl=\"from_s\", sort=\"from_s asc\")," + "walk=\"from_s->from_s\"," + "gather=\"to_s\", scatter=\"branches, leaves\")";
stream = (GatherNodesStream) factory.constructStream(expr);
context = new StreamContext();
context.setSolrClientCache(cache);
stream.setStreamContext(context);
tuples = getTuples(stream);
Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
assertTrue(tuples.size() == 4);
assertTrue(tuples.get(0).getString("node").equals("bill"));
assertTrue(tuples.get(0).getLong("level").equals(new Long(0)));
assertTrue(tuples.get(1).getString("node").equals("jim"));
assertTrue(tuples.get(1).getLong("level").equals(new Long(1)));
assertTrue(tuples.get(2).getString("node").equals("max"));
assertTrue(tuples.get(2).getLong("level").equals(new Long(1)));
assertTrue(tuples.get(3).getString("node").equals("sam"));
assertTrue(tuples.get(3).getLong("level").equals(new Long(1)));
expr = "gatherNodes(collection1, " + "search(collection1, q=\"message_t:jim\", fl=\"from_s\", sort=\"from_s asc\")," + "walk=\"from_s->from_s\"," + "gather=\"to_s\")";
String expr2 = "gatherNodes(collection1, " + expr + "," + "walk=\"node->from_s\"," + "gather=\"to_s\")";
stream = (GatherNodesStream) factory.constructStream(expr2);
context = new StreamContext();
context.setSolrClientCache(cache);
stream.setStreamContext(context);
tuples = getTuples(stream);
Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
assertTrue(tuples.size() == 3);
assertTrue(tuples.get(0).getString("node").equals("ann"));
assertTrue(tuples.get(1).getString("node").equals("kip"));
assertTrue(tuples.get(2).getString("node").equals("steve"));
//Test two traversals in the same expression
String expr3 = "hashJoin(" + expr2 + ", hashed=" + expr2 + ", on=\"node\")";
HashJoinStream hstream = (HashJoinStream) factory.constructStream(expr3);
context = new StreamContext();
context.setSolrClientCache(cache);
hstream.setStreamContext(context);
tuples = getTuples(hstream);
Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
assertTrue(tuples.size() == 3);
assertTrue(tuples.get(0).getString("node").equals("ann"));
assertTrue(tuples.get(1).getString("node").equals("kip"));
assertTrue(tuples.get(2).getString("node").equals("steve"));
//=================================
expr = "gatherNodes(collection1, " + "search(collection1, q=\"message_t:jim\", fl=\"from_s\", sort=\"from_s asc\")," + "walk=\"from_s->from_s\"," + "gather=\"to_s\")";
expr2 = "gatherNodes(collection1, " + expr + "," + "walk=\"node->from_s\"," + "gather=\"to_s\", scatter=\"branches, leaves\")";
stream = (GatherNodesStream) factory.constructStream(expr2);
context = new StreamContext();
context.setSolrClientCache(cache);
stream.setStreamContext(context);
tuples = getTuples(stream);
Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
assertTrue(tuples.size() == 7);
assertTrue(tuples.get(0).getString("node").equals("ann"));
assertTrue(tuples.get(0).getLong("level").equals(new Long(2)));
assertTrue(tuples.get(1).getString("node").equals("bill"));
assertTrue(tuples.get(1).getLong("level").equals(new Long(0)));
assertTrue(tuples.get(2).getString("node").equals("jim"));
assertTrue(tuples.get(2).getLong("level").equals(new Long(1)));
assertTrue(tuples.get(3).getString("node").equals("kip"));
assertTrue(tuples.get(3).getLong("level").equals(new Long(2)));
assertTrue(tuples.get(4).getString("node").equals("max"));
assertTrue(tuples.get(4).getLong("level").equals(new Long(1)));
assertTrue(tuples.get(5).getString("node").equals("sam"));
assertTrue(tuples.get(5).getLong("level").equals(new Long(1)));
assertTrue(tuples.get(6).getString("node").equals("steve"));
assertTrue(tuples.get(6).getLong("level").equals(new Long(2)));
//Add a cycle from jim to bill
new UpdateRequest().add(id, "6", "from_s", "jim", "to_s", "bill", "message_t", "Hello steve").add(id, "7", "from_s", "sam", "to_s", "bill", "message_t", "Hello steve").commit(cluster.getSolrClient(), COLLECTION);
expr = "gatherNodes(collection1, " + "search(collection1, q=\"message_t:jim\", fl=\"from_s\", sort=\"from_s asc\")," + "walk=\"from_s->from_s\"," + "gather=\"to_s\", trackTraversal=\"true\")";
expr2 = "gatherNodes(collection1, " + expr + "," + "walk=\"node->from_s\"," + "gather=\"to_s\", scatter=\"branches, leaves\", trackTraversal=\"true\")";
stream = (GatherNodesStream) factory.constructStream(expr2);
context = new StreamContext();
context.setSolrClientCache(cache);
stream.setStreamContext(context);
tuples = getTuples(stream);
Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
assertTrue(tuples.size() == 7);
assertTrue(tuples.get(0).getString("node").equals("ann"));
assertTrue(tuples.get(0).getLong("level").equals(new Long(2)));
//Bill should now have one ancestor
assertTrue(tuples.get(1).getString("node").equals("bill"));
assertTrue(tuples.get(1).getLong("level").equals(new Long(0)));
assertTrue(tuples.get(1).getStrings("ancestors").size() == 2);
List<String> anc = tuples.get(1).getStrings("ancestors");
Collections.sort(anc);
assertTrue(anc.get(0).equals("jim"));
assertTrue(anc.get(1).equals("sam"));
assertTrue(tuples.get(2).getString("node").equals("jim"));
assertTrue(tuples.get(2).getLong("level").equals(new Long(1)));
assertTrue(tuples.get(3).getString("node").equals("kip"));
assertTrue(tuples.get(3).getLong("level").equals(new Long(2)));
assertTrue(tuples.get(4).getString("node").equals("max"));
assertTrue(tuples.get(4).getLong("level").equals(new Long(1)));
assertTrue(tuples.get(5).getString("node").equals("sam"));
assertTrue(tuples.get(5).getLong("level").equals(new Long(1)));
assertTrue(tuples.get(6).getString("node").equals("steve"));
assertTrue(tuples.get(6).getLong("level").equals(new Long(2)));
cache.close();
}
use of org.apache.solr.client.solrj.io.stream.StreamContext in project lucene-solr by apache.
the class GraphExpressionTest method testGatherNodesStream.
@Test
public void testGatherNodesStream() throws Exception {
new UpdateRequest().add(id, "0", "basket_s", "basket1", "product_s", "product1", "price_f", "20").add(id, "1", "basket_s", "basket1", "product_s", "product3", "price_f", "30").add(id, "2", "basket_s", "basket1", "product_s", "product5", "price_f", "1").add(id, "3", "basket_s", "basket2", "product_s", "product1", "price_f", "2").add(id, "4", "basket_s", "basket2", "product_s", "product6", "price_f", "5").add(id, "5", "basket_s", "basket2", "product_s", "product7", "price_f", "10").add(id, "6", "basket_s", "basket3", "product_s", "product4", "price_f", "20").add(id, "7", "basket_s", "basket3", "product_s", "product3", "price_f", "10").add(id, "8", "basket_s", "basket3", "product_s", "product1", "price_f", "10").add(id, "9", "basket_s", "basket4", "product_s", "product4", "price_f", "40").add(id, "10", "basket_s", "basket4", "product_s", "product3", "price_f", "10").add(id, "11", "basket_s", "basket4", "product_s", "product1", "price_f", "10").commit(cluster.getSolrClient(), COLLECTION);
List<Tuple> tuples = null;
Set<String> paths = null;
GatherNodesStream stream = null;
StreamContext context = new StreamContext();
SolrClientCache cache = new SolrClientCache();
context.setSolrClientCache(cache);
StreamFactory factory = new StreamFactory().withCollectionZkHost("collection1", cluster.getZkServer().getZkAddress()).withFunctionName("gatherNodes", GatherNodesStream.class).withFunctionName("nodes", GatherNodesStream.class).withFunctionName("search", CloudSolrStream.class).withFunctionName("count", CountMetric.class).withFunctionName("avg", MeanMetric.class).withFunctionName("sum", SumMetric.class).withFunctionName("min", MinMetric.class).withFunctionName("max", MaxMetric.class);
String expr = "nodes(collection1, " + "walk=\"product1->product_s\"," + "gather=\"basket_s\")";
stream = (GatherNodesStream) factory.constructStream(expr);
stream.setStreamContext(context);
tuples = getTuples(stream);
Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
assertTrue(tuples.size() == 4);
assertTrue(tuples.get(0).getString("node").equals("basket1"));
assertTrue(tuples.get(1).getString("node").equals("basket2"));
assertTrue(tuples.get(2).getString("node").equals("basket3"));
assertTrue(tuples.get(3).getString("node").equals("basket4"));
//Test maxDocFreq param
String docFreqExpr = "gatherNodes(collection1, " + "walk=\"product1, product7->product_s\"," + "maxDocFreq=\"2\"," + "gather=\"basket_s\")";
stream = (GatherNodesStream) factory.constructStream(docFreqExpr);
stream.setStreamContext(context);
tuples = getTuples(stream);
Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
assertTrue(tuples.size() == 1);
assertTrue(tuples.get(0).getString("node").equals("basket2"));
String expr2 = "gatherNodes(collection1, " + expr + "," + "walk=\"node->basket_s\"," + "gather=\"product_s\", count(*), avg(price_f), sum(price_f), min(price_f), max(price_f))";
stream = (GatherNodesStream) factory.constructStream(expr2);
context = new StreamContext();
context.setSolrClientCache(cache);
stream.setStreamContext(context);
tuples = getTuples(stream);
Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
assertTrue(tuples.size() == 5);
assertTrue(tuples.get(0).getString("node").equals("product3"));
assertTrue(tuples.get(0).getDouble("count(*)").equals(3.0D));
assertTrue(tuples.get(1).getString("node").equals("product4"));
assertTrue(tuples.get(1).getDouble("count(*)").equals(2.0D));
assertTrue(tuples.get(1).getDouble("avg(price_f)").equals(30.0D));
assertTrue(tuples.get(1).getDouble("sum(price_f)").equals(60.0D));
assertTrue(tuples.get(1).getDouble("min(price_f)").equals(20.0D));
assertTrue(tuples.get(1).getDouble("max(price_f)").equals(40.0D));
assertTrue(tuples.get(2).getString("node").equals("product5"));
assertTrue(tuples.get(2).getDouble("count(*)").equals(1.0D));
assertTrue(tuples.get(3).getString("node").equals("product6"));
assertTrue(tuples.get(3).getDouble("count(*)").equals(1.0D));
assertTrue(tuples.get(4).getString("node").equals("product7"));
assertTrue(tuples.get(4).getDouble("count(*)").equals(1.0D));
//Test list of root nodes
expr = "gatherNodes(collection1, " + "walk=\"product4, product7->product_s\"," + "gather=\"basket_s\")";
stream = (GatherNodesStream) factory.constructStream(expr);
context = new StreamContext();
context.setSolrClientCache(cache);
stream.setStreamContext(context);
tuples = getTuples(stream);
Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
assertTrue(tuples.size() == 3);
assertTrue(tuples.get(0).getString("node").equals("basket2"));
assertTrue(tuples.get(1).getString("node").equals("basket3"));
assertTrue(tuples.get(2).getString("node").equals("basket4"));
//Test with negative filter query
expr = "gatherNodes(collection1, " + "walk=\"product4, product7->product_s\"," + "gather=\"basket_s\", fq=\"-basket_s:basket4\")";
stream = (GatherNodesStream) factory.constructStream(expr);
context = new StreamContext();
context.setSolrClientCache(cache);
stream.setStreamContext(context);
tuples = getTuples(stream);
Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
assertTrue(tuples.size() == 2);
assertTrue(tuples.get(0).getString("node").equals("basket2"));
assertTrue(tuples.get(1).getString("node").equals("basket3"));
cache.close();
}
use of org.apache.solr.client.solrj.io.stream.StreamContext in project lucene-solr by apache.
the class ConversionEvaluatorsTest method testFunction.
public void testFunction(String expression, Number expected) throws Exception {
StreamEvaluator evaluator = factory.constructEvaluator(expression);
StreamContext streamContext = new StreamContext();
evaluator.setStreamContext(streamContext);
Object result = evaluator.evaluate(new Tuple(values));
assertTrue(result instanceof Number);
assertEquals(expected, result);
}
Aggregations