SOLR-885 -- Changing NamedListCodec to return Object

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@728017 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2008-12-19 13:22:05 +00:00
parent 3ea8440652
commit 56ce812335
6 changed files with 25 additions and 19 deletions

View File

@ -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];

View File

@ -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();

View File

@ -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);

View File

@ -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);

View File

@ -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) {

View File

@ -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;