lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yo...@apache.org
Subject svn commit: r1236894 - in /lucene/dev/trunk/solr/core/src: java/org/apache/solr/response/TextResponseWriter.java test/org/apache/solr/request/JSONWriterTest.java
Date Fri, 27 Jan 2012 21:27:57 GMT
Author: yonik
Date: Fri Jan 27 21:27:57 2012
New Revision: 1236894

URL: http://svn.apache.org/viewvc?rev=1236894&view=rev
Log:
SOLR-3035: make text response writers work with byte, short, and byte[] values

Modified:
    lucene/dev/trunk/solr/core/src/java/org/apache/solr/response/TextResponseWriter.java
    lucene/dev/trunk/solr/core/src/test/org/apache/solr/request/JSONWriterTest.java

Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/response/TextResponseWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/response/TextResponseWriter.java?rev=1236894&r1=1236893&r2=1236894&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/response/TextResponseWriter.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/response/TextResponseWriter.java Fri
Jan 27 21:27:57 2012
@@ -23,8 +23,10 @@ import java.util.*;
 
 import org.apache.lucene.document.Document;
 import org.apache.lucene.index.IndexableField;
+import org.apache.lucene.util.BytesRef;
 import org.apache.solr.common.SolrDocument;
 import org.apache.solr.common.SolrDocumentList;
+import org.apache.solr.common.util.Base64;
 import org.apache.solr.common.util.FastWriter;
 import org.apache.solr.common.util.NamedList;
 import org.apache.solr.request.SolrQueryRequest;
@@ -130,20 +132,29 @@ public abstract class TextResponseWriter
       else {
         writeStr(name, f.stringValue(), true);
       }
-    } else if (val instanceof Integer) {
-      writeInt(name, val.toString());
+    } else if (val instanceof Number) {
+      if (val instanceof Integer) {
+        writeInt(name, val.toString());
+      } else if (val instanceof Long) {
+        writeLong(name, val.toString());
+      } else if (val instanceof Float) {
+        // we pass the float instead of using toString() because
+        // it may need special formatting. same for double.
+        writeFloat(name, ((Float)val).floatValue());
+      } else if (val instanceof Double) {
+        writeDouble(name, ((Double)val).doubleValue());        
+      } else if (val instanceof Short) {
+        writeInt(name, val.toString());
+      } else if (val instanceof Byte) {
+        writeInt(name, val.toString());
+      } else {
+        // default... for debugging only
+        writeStr(name, val.getClass().getName() + ':' + val.toString(), true);
+      }
     } else if (val instanceof Boolean) {
       writeBool(name, val.toString());
-    } else if (val instanceof Long) {
-      writeLong(name, val.toString());
     } else if (val instanceof Date) {
       writeDate(name,(Date)val);
-    } else if (val instanceof Float) {
-      // we pass the float instead of using toString() because
-      // it may need special formatting. same for double.
-      writeFloat(name, ((Float)val).floatValue());
-    } else if (val instanceof Double) {
-      writeDouble(name, ((Double)val).doubleValue());
     } else if (val instanceof Document) {
       SolrDocument doc = toSolrDocument( (Document)val );
       DocTransformer transformer = returnFields.getTransformer();
@@ -181,6 +192,12 @@ public abstract class TextResponseWriter
       writeArray(name,(Object[])val);
     } else if (val instanceof Iterator) {
       writeArray(name,(Iterator)val);
+    } else if (val instanceof byte[]) {
+      byte[] arr = (byte[])val;
+      writeByteArr(name, arr, 0, arr.length);
+    } else if (val instanceof BytesRef) {
+      BytesRef arr = (BytesRef)val;
+      writeByteArr(name, arr.bytes, arr.offset, arr.length);
     } else {
       // default... for debugging only
       writeStr(name, val.getClass().getName() + ':' + val.toString(), true);
@@ -334,4 +351,7 @@ public abstract class TextResponseWriter
   /** if this form of the method is called, val is the Solr ISO8601 based date format */
   public abstract void writeDate(String name, String val) throws IOException;
 
+  public void writeByteArr(String name, byte[] buf, int offset, int len) throws IOException
{
+    writeStr(name, Base64.byteArrayToBase64(buf, offset, len), false);
+  }
 }

Modified: lucene/dev/trunk/solr/core/src/test/org/apache/solr/request/JSONWriterTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/request/JSONWriterTest.java?rev=1236894&r1=1236893&r2=1236894&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/request/JSONWriterTest.java (original)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/request/JSONWriterTest.java Fri Jan
27 21:27:57 2012
@@ -41,7 +41,7 @@ public class JSONWriterTest extends Solr
   }    
   
   @Test
-  public void testNaNInf() throws IOException {
+  public void testTypes() throws IOException {
     SolrQueryRequest req = req("dummy");
     SolrQueryResponse rsp = new SolrQueryResponse();
     QueryResponseWriter w = new PythonResponseWriter();
@@ -77,8 +77,12 @@ public class JSONWriterTest extends Solr
     nl.add(null, 42);
     rsp.add("nl", nl);
 
+    rsp.add("byte", Byte.valueOf((byte)-3));
+    rsp.add("short", Short.valueOf((short)-4));
+    rsp.add("bytes", "abc".getBytes("UTF-8"));
+
     w.write(buf, req, rsp);
-    assertEquals("{\"nl\":[[\"data1\",\"he\\u2028llo\\u2029!\"],[null,42]]}", buf.toString());
+    assertEquals("{\"nl\":[[\"data1\",\"he\\u2028llo\\u2029!\"],[null,42]],\"byte\":-3,\"short\":-4,\"bytes\":\"YWJj\"}",
buf.toString());
     req.close();
   }
   



Mime
View raw message