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:
Shalin Shekhar Mangar 2009-04-05 22:24:36 +00:00
parent 3ee3b599e4
commit 55592e8d7c
2 changed files with 67 additions and 0 deletions

View File

@ -612,4 +612,33 @@ public class CommonsHttpSolrServer extends SolrServer
req.setDocIterator(docIterator);
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);
}
}

View File

@ -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.response.QueryResponse;
import org.apache.solr.client.solrj.request.RequestWriter;
import org.apache.solr.client.solrj.beans.Field;
import org.apache.solr.common.SolrInputDocument;
import java.util.Iterator;
@ -54,6 +55,43 @@ public class TestBatchUpdate extends SolrExampleTestBase {
commonsHttpSolrServer.deleteByQuery( "*:*" ); // delete everything!
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 {
final int[] counter = new int[1];