Apache Solr open-source search software
SOLR-885 -- Changing NamedListCodec to return Object
Shalin Shekhar Mangar56ce81233532
diff --git a/src/common/org/apache/solr/common/util/NamedListCodec.java b/src/common/org/apache/solr/common/util/NamedListCodec.javaindex df3f6e03138..11150ef4313 100755--- a/src/common/org/apache/solr/common/util/NamedListCodec.java+++ b/src/common/org/apache/solr/common/util/NamedListCodec.java@@ -30,6 +30,9 @@ import java.util.*;* It is expected that this class is used on both end of the pipes.* The class has one read method and one write method for each of the datatypes*+ * Note -- Never re-use an instance of this class for more than one marshal or unmarshall operation.+ * Always create a new instance.+ **/public class NamedListCodec {@@ -63,9 +66,9 @@ public class NamedListCodec {EXTERN_STRING = (byte)(7 << 5);- private byte VERSION = 1;+ private static byte VERSION = 1;private ObjectResolver resolver;- private FastOutputStream daos;+ protected FastOutputStream daos;public NamedListCodec() { }@@ -73,20 +76,24 @@ public class NamedListCodec {this.resolver = resolver;}- public void marshal(NamedList nl, OutputStream os) throws IOException {+ public void marshal(Object nl, OutputStream os) throws IOException {daos = FastOutputStream.wrap(os);try {daos.writeByte(VERSION);- writeNamedList(nl);+ writeVal(nl);} finally {daos.flushBuffer();}}+ byte version;- public NamedList unmarshal(InputStream is) throws IOException {+ public Object unmarshal(InputStream is) throws IOException {FastInputStream dis = FastInputStream.wrap(is);- byte version = dis.readByte();- return (NamedList)readVal(dis);+ version = dis.readByte();+ if(version != VERSION){+ throw new RuntimeException("Invalid version or the data in not in 'javabin' format");+ }+ return (Object)readVal(dis);}@@ -136,7 +143,7 @@ public class NamedListCodec {writeVal(val.getClass().getName() + ':' + val.toString());}- private static final Object END_OBJ = new Object();+ protected static final Object END_OBJ = new Object();byte tagByte;public Object readVal(FastInputStream dis) throws IOException {@@ -386,7 +393,7 @@ public class NamedListCodec {char[] charArr;- private String readStr(FastInputStream dis) throws IOException {+ public String readStr(FastInputStream dis) throws IOException {int sz = readSize(dis);if (charArr==null || charArr.length < sz) {charArr = new char[sz];diff --git a/src/java/org/apache/solr/handler/SnapPuller.java b/src/java/org/apache/solr/handler/SnapPuller.javaindex 2592c6d6096..d63af39eaf5 100644--- a/src/java/org/apache/solr/handler/SnapPuller.java+++ b/src/java/org/apache/solr/handler/SnapPuller.java@@ -171,7 +171,7 @@ public class SnapPuller {throw new SolrException(SolrException.ErrorCode.SERVICE_UNAVAILABLE,"Request failed for the url " + method);}- return new NamedListCodec().unmarshal(method.getResponseBodyAsStream());+ return (NamedList) new NamedListCodec().unmarshal(method.getResponseBodyAsStream());} finally {try {method.releaseConnection();diff --git a/src/java/org/apache/solr/request/BinaryResponseWriter.java b/src/java/org/apache/solr/request/BinaryResponseWriter.javaindex 5fc1ba8cc9c..ebad121eb1b 100755--- a/src/java/org/apache/solr/request/BinaryResponseWriter.java+++ b/src/java/org/apache/solr/request/BinaryResponseWriter.java@@ -181,11 +181,10 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {Resolver resolver = new Resolver(req, rsp.getReturnFields());ByteArrayOutputStream out = new ByteArrayOutputStream();- NamedListCodec codec = new NamedListCodec(resolver);- codec.marshal(rsp.getValues(), out);+ new NamedListCodec(resolver).marshal(rsp.getValues(), out);InputStream in = new ByteArrayInputStream(out.toByteArray());- return codec.unmarshal(in);+ return (NamedList<Object>) new NamedListCodec(resolver).unmarshal(in);}catch (Exception ex) {throw new RuntimeException(ex);diff --git a/src/solrj/org/apache/solr/client/solrj/impl/BinaryResponseParser.java b/src/solrj/org/apache/solr/client/solrj/impl/BinaryResponseParser.javaindex 466b07c35fd..c6193f61568 100755--- a/src/solrj/org/apache/solr/client/solrj/impl/BinaryResponseParser.java+++ b/src/solrj/org/apache/solr/client/solrj/impl/BinaryResponseParser.java@@ -36,7 +36,7 @@ public class BinaryResponseParser extends ResponseParser {public NamedList<Object> processResponse(InputStream body, String encoding) {try {- return new NamedListCodec().unmarshal(body);+ return (NamedList<Object>) new NamedListCodec().unmarshal(body);} catch (IOException e) {throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "parsing error", e);diff --git a/src/test/org/apache/solr/common/util/TestNamedListCodec.java b/src/test/org/apache/solr/common/util/TestNamedListCodec.javaindex 69cf151a7ba..065473ffafc 100755--- a/src/test/org/apache/solr/common/util/TestNamedListCodec.java+++ b/src/test/org/apache/solr/common/util/TestNamedListCodec.java@@ -79,7 +79,7 @@ public class TestNamedListCodec extends TestCase {new NamedListCodec(null).marshal(nl,baos);byte[] arr = baos.toByteArray();- nl = new NamedListCodec().unmarshal(new ByteArrayInputStream(arr));+ nl = (NamedList) new NamedListCodec().unmarshal(new ByteArrayInputStream(arr));assertEquals(3, nl.size());@@ -119,7 +119,7 @@ public class TestNamedListCodec extends TestCase {new NamedListCodec(null).marshal(nl,baos);byte[] arr = baos.toByteArray();- nl = new NamedListCodec().unmarshal(new ByteArrayInputStream(arr));+ nl = (NamedList) new NamedListCodec().unmarshal(new ByteArrayInputStream(arr));List l = (List) nl.get("zzz");assertEquals(list.size(), l.size());@@ -143,7 +143,7 @@ public class TestNamedListCodec extends TestCase {byte[] arr = baos.toByteArray();try {- NamedList result = new NamedListCodec().unmarshal(new ByteArrayInputStream(arr));+ NamedList result = (NamedList) new NamedListCodec().unmarshal(new ByteArrayInputStream(arr));assertTrue("result is null and it shouldn't be", result != null);List keys = (List) result.get("keys");assertTrue("keys is null and it shouldn't be", keys != null);@@ -247,7 +247,7 @@ public class TestNamedListCodec extends TestCase {new NamedListCodec(null).marshal(nl,baos);byte[] arr = baos.toByteArray();// System.out.println(arr.length);- res = new NamedListCodec().unmarshal(new ByteArrayInputStream(arr));+ res = (NamedList) new NamedListCodec().unmarshal(new ByteArrayInputStream(arr));cmp = TestDistributedSearch.compare(nl,res, 0, null);if (cmp != null) {diff --git a/src/test/org/apache/solr/request/TestBinaryResponseWriter.java b/src/test/org/apache/solr/request/TestBinaryResponseWriter.javaindex 0d77820c134..f1a21f3770a 100644--- a/src/test/org/apache/solr/request/TestBinaryResponseWriter.java+++ b/src/test/org/apache/solr/request/TestBinaryResponseWriter.java@@ -55,7 +55,7 @@ public class TestBinaryResponseWriter extends AbstractSolrTestCase {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()));+ NamedList res = (NamedList) new NamedListCodec().unmarshal(new ByteArrayInputStream(baos.toByteArray()));SolrDocumentList docs = (SolrDocumentList) res.get("response");for (Object doc : docs) {SolrDocument document = (SolrDocument) doc;