SOLR-486: make javabin the default for SolrJ

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@668661 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2008-06-17 13:37:55 +00:00
parent e108b5375c
commit 0b20b5e658
5 changed files with 81 additions and 17 deletions

View File

@ -258,8 +258,9 @@ New Features
and QueryResponseTest (Shalin Shekhar Mangar via gsingers)
44. SOLR-486: Binary response format, faster and smaller
than XML and JSON response formats (use wt=javabin).
BinaryResponseParser for utilizing the binary format via SolrJ.
than XML and JSON response formats (use wt=javabin).
BinaryResponseParser for utilizing the binary format via SolrJ
and is now the default.
(Noble Paul, yonik)
45. SOLR-521: StopFilterFactory support for "enablePositionIncrements"

View File

@ -85,7 +85,7 @@ public class CommonsHttpSolrServer extends SolrServer
* will use this SolrServer.
*/
public CommonsHttpSolrServer(String solrServerUrl, HttpClient httpClient) throws MalformedURLException {
this(new URL(solrServerUrl), httpClient, new XMLResponseParser());
this(new URL(solrServerUrl), httpClient, new BinaryResponseParser());
}
public CommonsHttpSolrServer(String solrServerUrl, HttpClient httpClient, ResponseParser parser) throws MalformedURLException {
@ -100,7 +100,7 @@ public class CommonsHttpSolrServer extends SolrServer
*/
public CommonsHttpSolrServer(URL baseURL)
{
this(baseURL, null, new XMLResponseParser());
this(baseURL, null, new BinaryResponseParser());
}
public CommonsHttpSolrServer(URL baseURL, HttpClient client){

View File

@ -57,7 +57,7 @@ public class NamedListCodec {
ARR = (byte)(4 << 5), //
ORDERED_MAP=(byte)(5 << 5), // SimpleOrderedMap (a NamedList subclass, and more common)
NAMED_LST = (byte)(6 << 5), // NamedList
RESERVED2 = (byte)(7 << 5);
EXTERN_STRING = (byte)(7 << 5);
private byte VERSION = 1;
@ -149,6 +149,7 @@ public class NamedListCodec {
case ARR >>> 5 : return readArray(dis);
case ORDERED_MAP >>> 5 : return readOrderedMap(dis);
case NAMED_LST >>> 5 : return readNamedList(dis);
case EXTERN_STRING >>> 5 : return readExternString(dis);
}
switch(tagByte){
@ -190,7 +191,19 @@ public class NamedListCodec {
return true;
}
if (val instanceof SolrDocument) {
writeSolrDocument((SolrDocument) val);
//this needs special treatment to know which fields are to be written
if(resolver == null){
writeSolrDocument((SolrDocument) val);
}else {
Object retVal = resolver.resolve(val, this);
if(retVal != null) {
if (retVal instanceof SolrDocument) {
writeSolrDocument((SolrDocument) retVal);
} else {
writeVal(retVal);
}
}
}
return true;
}
if (val instanceof Iterator) {
@ -234,13 +247,26 @@ public class NamedListCodec {
}
public void writeSolrDocument(SolrDocument doc) throws IOException {
writeSolrDocument(doc, null);
}
public void writeSolrDocument(SolrDocument doc, Set<String> fields) throws IOException {
int count = 0;
if (fields == null) {
count = doc.getFieldNames().size();
} else {
for (Map.Entry<String, Object> entry : doc) {
if (fields.contains(entry.getKey())) count++;
}
}
writeTag(SOLRDOC);
writeTag(ORDERED_MAP, doc.getFieldNames().size());
writeTag(ORDERED_MAP, count);
for (Map.Entry<String, Object> entry : doc) {
String name = entry.getKey();
writeStr(name);
Object val = entry.getValue();
writeVal(val);
if (fields == null || fields.contains(entry.getKey())) {
String name = entry.getKey();
writeExternString(name);
Object val = entry.getValue();
writeVal(val);
}
}
}
@ -557,6 +583,36 @@ public class NamedListCodec {
}
}
private int stringsCount = 0;
private Map<String,Integer> stringsMap;
private List<String > stringsList;
public void writeExternString(String s) throws IOException {
if(s == null) {
writeTag(NULL) ;
return;
}
Integer idx = stringsMap == null ? null : stringsMap.get(s);
if(idx == null) idx =0;
writeTag(EXTERN_STRING,idx);
if(idx == 0){
writeStr(s);
if(stringsMap == null) stringsMap = new HashMap<String, Integer>();
stringsMap.put(s,++stringsCount);
}
}
public String readExternString(FastInputStream fis) throws IOException {
int idx = readSize(fis);
if (idx != 0) {// idx != 0 is the index of the extern string
return stringsList.get(idx-1);
} else {// idx == 0 means it has a string value
String s = (String) readVal(fis);
if(stringsList == null ) stringsList = new ArrayList<String>();
stringsList.add(s);
return s;
}
}
public static interface ObjectResolver{
public Object resolve(Object o, NamedListCodec codec) throws IOException;

View File

@ -70,7 +70,7 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
this.schema = req.getSchema();
this.searcher = req.getSearcher();
this.includeScore = returnFields!=null && returnFields.contains("score");
if (returnFields != null) {
if (returnFields.size() == 0 || (returnFields.size() == 1 && includeScore) || returnFields.contains("*")) {
returnFields = null; // null means return all stored fields
@ -84,7 +84,11 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
writeDocList((DocList) o, codec);
return null; // null means we completely handled it
}
if (o instanceof SolrDocument) {
SolrDocument solrDocument = (SolrDocument) o;
codec.writeSolrDocument(solrDocument,returnFields);
return null;
}
if (o instanceof Document) {
return getDoc((Document) o);
}

View File

@ -305,8 +305,8 @@ public class TestDistributedSearch extends TestCase {
cmp = compare(a.getMaxScore(), b.getMaxScore(), 0, handle);
if (cmp != null) return ".maxScore" + cmp;
} else {
if (a.getMaxScore() != null) {
if (b.getMaxScore() == null) {
if (b.getMaxScore() != null) {
if (a.getMaxScore() == null) {
return ".maxScore missing";
}
}
@ -453,18 +453,21 @@ public class TestDistributedSearch extends TestCase {
// these queries should be exactly ordered and scores should exactly match
query("q","*:*", "sort",i1+" desc");
query("q","{!func}"+i1);
handle.put("maxScore", SKIPVAL);
query("q","{!func}"+i1);// does not expect maxScore. So if it comes ,ignore it. NamedListCodec.writeSolrDocumentList()
//is agnostic of request params.
handle.remove("maxScore");
query("q","{!func}"+i1, "fl","*,score"); // even scores should match exactly here
handle.put("highlighting", UNORDERED);
handle.put("response", UNORDERED);
handle.put("maxScore", SKIPVAL);
query("q","quick");
query("q","all","fl","id","start","0");
query("q","all","fl","foofoofoo","start","0"); // no fields in returned docs
query("q","all","fl","id","start","100");
handle.put("maxScore", SKIPVAL);
handle.put("score", SKIPVAL);
query("q","quick","fl","*,score");
query("q","all","fl","*,score","start","1");