mirror of https://github.com/apache/lucene.git
SOLR-1038 -- Adding addBeans(Iterator) method
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@762176 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
3ee3b599e4
commit
55592e8d7c
|
@ -612,4 +612,33 @@ public class CommonsHttpSolrServer extends SolrServer
|
||||||
req.setDocIterator(docIterator);
|
req.setDocIterator(docIterator);
|
||||||
return req.process(this);
|
return req.process(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds the beans supplied by the given iterator.
|
||||||
|
*
|
||||||
|
* @param beanIterator the iterator which returns Beans
|
||||||
|
*
|
||||||
|
* @return the response from the SolrServer
|
||||||
|
*/
|
||||||
|
public UpdateResponse addBeans(final Iterator<?> beanIterator)
|
||||||
|
throws SolrServerException, IOException {
|
||||||
|
UpdateRequest req = new UpdateRequest();
|
||||||
|
req.setDocIterator(new Iterator<SolrInputDocument>() {
|
||||||
|
|
||||||
|
public boolean hasNext() {
|
||||||
|
return beanIterator.hasNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
public SolrInputDocument next() {
|
||||||
|
Object o = beanIterator.next();
|
||||||
|
if (o == null) return null;
|
||||||
|
return getBinder().toSolrInputDocument(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove() {
|
||||||
|
beanIterator.remove();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return req.process(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@ import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
|
||||||
import org.apache.solr.client.solrj.impl.BinaryRequestWriter;
|
import org.apache.solr.client.solrj.impl.BinaryRequestWriter;
|
||||||
import org.apache.solr.client.solrj.response.QueryResponse;
|
import org.apache.solr.client.solrj.response.QueryResponse;
|
||||||
import org.apache.solr.client.solrj.request.RequestWriter;
|
import org.apache.solr.client.solrj.request.RequestWriter;
|
||||||
|
import org.apache.solr.client.solrj.beans.Field;
|
||||||
import org.apache.solr.common.SolrInputDocument;
|
import org.apache.solr.common.SolrInputDocument;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
@ -54,6 +55,43 @@ public class TestBatchUpdate extends SolrExampleTestBase {
|
||||||
commonsHttpSolrServer.deleteByQuery( "*:*" ); // delete everything!
|
commonsHttpSolrServer.deleteByQuery( "*:*" ); // delete everything!
|
||||||
doIt(commonsHttpSolrServer);
|
doIt(commonsHttpSolrServer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testWithBinaryBean()throws Exception{
|
||||||
|
CommonsHttpSolrServer commonsHttpSolrServer = (CommonsHttpSolrServer) getSolrServer();
|
||||||
|
commonsHttpSolrServer.setRequestWriter(new BinaryRequestWriter());
|
||||||
|
commonsHttpSolrServer.deleteByQuery( "*:*" ); // delete everything!
|
||||||
|
final int[] counter = new int[1];
|
||||||
|
counter[0] = 0;
|
||||||
|
commonsHttpSolrServer.addBeans(new Iterator<Bean>() {
|
||||||
|
|
||||||
|
public boolean hasNext() {
|
||||||
|
return counter[0] < numdocs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Bean next() {
|
||||||
|
Bean bean = new Bean();
|
||||||
|
bean.id = "" + (++counter[0]);
|
||||||
|
bean.cat = "foocat";
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove() {
|
||||||
|
//do nothing
|
||||||
|
}
|
||||||
|
});
|
||||||
|
commonsHttpSolrServer.commit();
|
||||||
|
SolrQuery query = new SolrQuery("*:*");
|
||||||
|
QueryResponse response = commonsHttpSolrServer.query(query);
|
||||||
|
assertEquals(0, response.getStatus());
|
||||||
|
assertEquals(numdocs, response.getResults().getNumFound());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Bean{
|
||||||
|
@Field
|
||||||
|
String id;
|
||||||
|
@Field
|
||||||
|
String cat;
|
||||||
|
}
|
||||||
|
|
||||||
private void doIt(CommonsHttpSolrServer commonsHttpSolrServer) throws SolrServerException, IOException {
|
private void doIt(CommonsHttpSolrServer commonsHttpSolrServer) throws SolrServerException, IOException {
|
||||||
final int[] counter = new int[1];
|
final int[] counter = new int[1];
|
||||||
|
|
Loading…
Reference in New Issue