SOLR-807 -- BinaryResponseWriter writes fieldType.toExternal if it is not a supported type, otherwise it writes fieldType.toObject. This fixes the bug with encoding/decoding UUIDField.

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@726502 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2008-12-14 17:37:36 +00:00
parent 25c1d17448
commit de702ae19a
4 changed files with 130 additions and 35 deletions

View File

@ -164,6 +164,10 @@ Bug Fixes
16. SOLR-901: FastOutputStream ignores write(byte[]) call. (Noble Paul via shalin) 16. SOLR-901: FastOutputStream ignores write(byte[]) call. (Noble Paul via shalin)
17. SOLR-807: BinaryResponseWriter writes fieldType.toExternal if it is not a supported type,
otherwise it writes fieldType.toObject. This fixes the bug with encoding/decoding UUIDField.
(koji, Noble Paul, shalin)
Other Changes Other Changes
---------------------- ----------------------

View File

@ -16,33 +16,29 @@
*/ */
package org.apache.solr.request; package org.apache.solr.request;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.Fieldable;
import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.params.CommonParams; import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.util.NamedList; import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.NamedListCodec; import org.apache.solr.common.util.NamedListCodec;
import org.apache.solr.schema.FieldType; import org.apache.solr.schema.*;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.search.DocIterator; import org.apache.solr.search.DocIterator;
import org.apache.solr.search.DocList; import org.apache.solr.search.DocList;
import org.apache.solr.search.SolrIndexSearcher; import org.apache.solr.search.SolrIndexSearcher;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class BinaryResponseWriter implements BinaryQueryResponseWriter { public class BinaryResponseWriter implements BinaryQueryResponseWriter {
private static final Logger LOG = LoggerFactory.getLogger(BinaryResponseWriter.class); private static final Logger LOG = LoggerFactory.getLogger(BinaryResponseWriter.class);
private static final Set<Class> KNOWN_TYPES = new HashSet<Class>();
public void write(OutputStream out, SolrQueryRequest req, SolrQueryResponse response) throws IOException { public void write(OutputStream out, SolrQueryRequest req, SolrQueryResponse response) throws IOException {
Resolver resolver = new Resolver(req, response.getReturnFields()); Resolver resolver = new Resolver(req, response.getReturnFields());
@ -147,7 +143,11 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
else val = f.stringValue(); else val = f.stringValue();
} else { } else {
try { try {
val = useFieldObjects ? ft.toObject(f) : ft.toExternal(f); if (useFieldObjects && KNOWN_TYPES.contains(ft.getClass())) {
val = ft.toObject(f);
} else {
val = ft.toExternal(f);
}
} catch (Exception e) { } catch (Exception e) {
// There is a chance of the underlying field not really matching the // There is a chance of the underlying field not really matching the
// actual field type . So ,it can throw exception // actual field type . So ,it can throw exception
@ -169,14 +169,14 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
* *
* @param req * @param req
* @param rsp * @param rsp
* @return a response object equivalent to what you get from the XML/JSON/javabin parser. Documents *
* become SolrDocuments, DocList becomes SolrDocumentList etc. * @return a response object equivalent to what you get from the XML/JSON/javabin parser. Documents become
* SolrDocuments, DocList becomes SolrDocumentList etc.
* *
* @since solr 1.4 * @since solr 1.4
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static NamedList<Object> getParsedResponse( SolrQueryRequest req, SolrQueryResponse rsp ) public static NamedList<Object> getParsedResponse(SolrQueryRequest req, SolrQueryResponse rsp) {
{
try { try {
Resolver resolver = new Resolver(req, rsp.getReturnFields()); Resolver resolver = new Resolver(req, rsp.getReturnFields());
@ -191,4 +191,26 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
} }
static {
KNOWN_TYPES.add(BoolField.class);
KNOWN_TYPES.add(BCDIntField.class);
KNOWN_TYPES.add(BCDLongField.class);
KNOWN_TYPES.add(BCDStrField.class);
KNOWN_TYPES.add(ByteField.class);
KNOWN_TYPES.add(DateField.class);
KNOWN_TYPES.add(DoubleField.class);
KNOWN_TYPES.add(FloatField.class);
KNOWN_TYPES.add(ShortField.class);
KNOWN_TYPES.add(IntField.class);
KNOWN_TYPES.add(LongField.class);
KNOWN_TYPES.add(SortableLongField.class);
KNOWN_TYPES.add(SortableIntField.class);
KNOWN_TYPES.add(SortableFloatField.class);
KNOWN_TYPES.add(SortableDoubleField.class);
KNOWN_TYPES.add(StrField.class);
KNOWN_TYPES.add(TextField.class);
// We do not add UUIDField because UUID object is not a supported type in NamedListCodec
// and if we write UUIDField.toObject, we wouldn't know how to handle it in the client side
}
} }

View File

@ -0,0 +1,66 @@
/**
* 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.request;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.NamedListCodec;
import org.apache.solr.util.AbstractSolrTestCase;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.UUID;
/**
* Test for BinaryResponseWriter
*
* @version $Id$
* @since solr 1.4
*/
public class TestBinaryResponseWriter extends AbstractSolrTestCase {
public String getSchemaFile() {
return "schema.xml";
}
public String getSolrConfigFile() {
return "solrconfig.xml";
}
/**
* Tests known types implementation by asserting correct encoding/decoding of UUIDField
*/
public void testUUID() throws Exception {
String s = UUID.randomUUID().toString().toLowerCase();
assertU(adoc("id", "101", "uuid", s));
assertU(commit());
LocalSolrQueryRequest req = lrf.makeRequest("q", "*:*");
SolrQueryResponse rsp = h.queryAndResponse(req.getParams().get(CommonParams.QT), req);
BinaryQueryResponseWriter writer = (BinaryQueryResponseWriter) h.getCore().getQueryResponseWriter("javabin");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
writer.write(baos, req, rsp);
NamedList res = new NamedListCodec().unmarshal(new ByteArrayInputStream(baos.toByteArray()));
SolrDocumentList docs = (SolrDocumentList) res.get("response");
for (Object doc : docs) {
SolrDocument document = (SolrDocument) doc;
assertEquals("Returned object must be a string", "java.lang.String", document.getFieldValue("uuid").getClass().getName());
assertEquals("Wrong UUID string returned", s, document.getFieldValue("uuid"));
}
}
}

View File

@ -311,11 +311,14 @@
</analyzer> </analyzer>
</fieldtype> </fieldtype>
<fieldType name="uuid" class="solr.UUIDField" />
</types> </types>
<fields> <fields>
<field name="id" type="integer" indexed="true" stored="true" multiValued="false" required="false"/> <field name="id" type="integer" indexed="true" stored="true" multiValued="false" required="false"/>
<field name="uuid" type="uuid" stored="true" />
<field name="name" type="nametext" indexed="true" stored="true"/> <field name="name" type="nametext" indexed="true" stored="true"/>
<field name="text" type="text" indexed="true" stored="false"/> <field name="text" type="text" indexed="true" stored="false"/>
<field name="subject" type="text" indexed="true" stored="true"/> <field name="subject" type="text" indexed="true" stored="true"/>