SOLR:10779: JavaBinCodec should use close consistently rather than having marshal() and close() call finish() (which closes the underlying stream)

commit b31178e0c33dbfe81fbc1aec705b3dae3cc895ad
Author: Erick <erick@apache.org>
Date:   Sun Jun 18 22:34:21 2017 -0700

    SOLR-10779: JavaBinCodec should use close consistently rather than having marshal() and close() call finish() (which closes the underlying stream)
This commit is contained in:
Erick 2017-06-18 22:35:57 -07:00
parent 943bf5ab5b
commit 64093d6df1
13 changed files with 160 additions and 133 deletions

View File

@ -277,6 +277,9 @@ Other Changes
* SOLR-10800: Factor out HttpShardHandler.transformReplicasToShardUrls from HttpShardHandler.prepDistributed. * SOLR-10800: Factor out HttpShardHandler.transformReplicasToShardUrls from HttpShardHandler.prepDistributed.
(Domenico Fabio Marino, Christine Poerschke) (Domenico Fabio Marino, Christine Poerschke)
* SOLR-10779: JavaBinCodec should use close consistently rather than having marshal() and close() call finish()
(which closes the underlying stream). (Erick Erickson)
================== 6.7.0 ================== ================== 6.7.0 ==================
Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release. Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.

View File

@ -48,7 +48,9 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
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());
if (req.getParams().getBool(CommonParams.OMIT_HEADER, false)) response.removeResponseHeader(); if (req.getParams().getBool(CommonParams.OMIT_HEADER, false)) response.removeResponseHeader();
new JavaBinCodec(resolver).setWritableDocFields(resolver).marshal(response.getValues(), out); try (JavaBinCodec jbc = new JavaBinCodec(resolver)) {
jbc.setWritableDocFields(resolver).marshal(response.getValues(), out);
}
} }
@Override @Override
@ -160,10 +162,14 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
Resolver resolver = new Resolver(req, rsp.getReturnFields()); Resolver resolver = new Resolver(req, rsp.getReturnFields());
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new JavaBinCodec(resolver).setWritableDocFields(resolver).marshal(rsp.getValues(), out); try (JavaBinCodec jbc = new JavaBinCodec(resolver)) {
jbc.setWritableDocFields(resolver).marshal(rsp.getValues(), out);
}
InputStream in = out.toInputStream(); InputStream in = out.toInputStream();
return (NamedList<Object>) new JavaBinCodec(resolver).unmarshal(in); try (JavaBinCodec jbc = new JavaBinCodec(resolver)) {
return (NamedList<Object>) jbc.unmarshal(in);
}
} }
catch (Exception ex) { catch (Exception ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);

View File

@ -183,9 +183,8 @@ public final class CursorMark {
List<Object> pieces = null; List<Object> pieces = null;
try { try {
final byte[] rawData = Base64.base64ToByteArray(serialized); final byte[] rawData = Base64.base64ToByteArray(serialized);
ByteArrayInputStream in = new ByteArrayInputStream(rawData); try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayInputStream in = new ByteArrayInputStream(rawData)){
try { pieces = (List<Object>) jbc.unmarshal(in);
pieces = (List<Object>) new JavaBinCodec().unmarshal(in);
boolean b = false; boolean b = false;
for (Object o : pieces) { for (Object o : pieces) {
if (o instanceof BytesRefBuilder || o instanceof BytesRef || o instanceof String) { if (o instanceof BytesRefBuilder || o instanceof BytesRef || o instanceof String) {
@ -196,8 +195,6 @@ public final class CursorMark {
in.reset(); in.reset();
pieces = (List<Object>) new JavaBinCodec().unmarshal(in); pieces = (List<Object>) new JavaBinCodec().unmarshal(in);
} }
} finally {
in.close();
} }
} catch (Exception ex) { } catch (Exception ex) {
throw new SolrException(ErrorCode.BAD_REQUEST, throw new SolrException(ErrorCode.BAD_REQUEST,
@ -259,19 +256,13 @@ public final class CursorMark {
// the type/name/dir from the SortFields (or a hashCode to act as a checksum) // the type/name/dir from the SortFields (or a hashCode to act as a checksum)
// could help provide more validation beyond just the number of clauses. // could help provide more validation beyond just the number of clauses.
try { try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayOutputStream out = new ByteArrayOutputStream(256)) {
ByteArrayOutputStream out = new ByteArrayOutputStream(256); jbc.marshal(marshalledValues, out);
try { byte[] rawData = out.toByteArray();
new JavaBinCodec().marshal(marshalledValues, out); return Base64.byteArrayToBase64(rawData, 0, rawData.length);
byte[] rawData = out.toByteArray();
return Base64.byteArrayToBase64(rawData, 0, rawData.length);
} finally {
out.close();
}
} catch (Exception ex) { } catch (Exception ex) {
throw new SolrException(ErrorCode.SERVER_ERROR, throw new SolrException(ErrorCode.SERVER_ERROR,
"Unable to format search after totem", ex); "Unable to format search after totem", ex);
} }
} }

View File

@ -60,7 +60,10 @@ public class TestBinaryResponseWriter extends AbstractSolrTestCase {
BinaryQueryResponseWriter writer = (BinaryQueryResponseWriter) h.getCore().getQueryResponseWriter("javabin"); BinaryQueryResponseWriter writer = (BinaryQueryResponseWriter) h.getCore().getQueryResponseWriter("javabin");
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
writer.write(baos, req, rsp); writer.write(baos, req, rsp);
NamedList res = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(baos.toByteArray())); NamedList res;
try (JavaBinCodec jbc = new JavaBinCodec()) {
res = (NamedList) jbc.unmarshal(new ByteArrayInputStream(baos.toByteArray()));
}
SolrDocumentList docs = (SolrDocumentList) res.get("response"); SolrDocumentList docs = (SolrDocumentList) res.get("response");
for (Object doc : docs) { for (Object doc : docs) {
SolrDocument document = (SolrDocument) doc; SolrDocument document = (SolrDocument) doc;

View File

@ -169,15 +169,19 @@ public class TestJavabinTupleStreamParser extends SolrTestCaseJ4 {
public void testSolrDocumentList() throws IOException { public void testSolrDocumentList() throws IOException {
SolrQueryResponse response = new SolrQueryResponse(); SolrQueryResponse response = new SolrQueryResponse();
SolrDocumentList l = constructSolrDocList(response); SolrDocumentList l = constructSolrDocList(response);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
new JavaBinCodec().marshal(response.getValues(), baos); jbc.marshal(response.getValues(), baos);
}
byte[] bytes = serialize(response.getValues()); byte[] bytes = serialize(response.getValues());
Object o = new JavaBinCodec().unmarshal(new ByteArrayInputStream(bytes)); try (JavaBinCodec jbc = new JavaBinCodec()) {
jbc.unmarshal(new ByteArrayInputStream(bytes));
}
List list = new ArrayList<>(); List list = new ArrayList<>();
Map m = null; Map m = null;
JavabinTupleStreamParser parser = new JavabinTupleStreamParser(new ByteArrayInputStream(bytes), false); try (JavabinTupleStreamParser parser = new JavabinTupleStreamParser(new ByteArrayInputStream(bytes), false)) {
while ((m = parser.next()) != null) { while ((m = parser.next()) != null) {
list.add(m); list.add(m);
}
} }
assertEquals(l.size(), list.size()); assertEquals(l.size(), list.size());
for(int i =0;i<list.size();i++){ for(int i =0;i<list.size();i++){
@ -189,7 +193,9 @@ public class TestJavabinTupleStreamParser extends SolrTestCaseJ4 {
SolrQueryResponse response = new SolrQueryResponse(); SolrQueryResponse response = new SolrQueryResponse();
response.getValues().add("results", o); response.getValues().add("results", o);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
new JavaBinCodec().marshal(response.getValues(), baos); try (JavaBinCodec jbc = new JavaBinCodec()) {
jbc.marshal(response.getValues(), baos);
}
return baos.toByteArray(); return baos.toByteArray();
} }
} }

View File

@ -53,8 +53,12 @@ public class TestPushWriter extends SolrTestCaseJ4 {
log.info(new String(baos.toByteArray(), "UTF-8")); log.info(new String(baos.toByteArray(), "UTF-8"));
Map m = (Map) Utils.fromJSON(baos.toByteArray()); Map m = (Map) Utils.fromJSON(baos.toByteArray());
checkValues(m); checkValues(m);
writeData(new JavaBinCodec(baos= new ByteArrayOutputStream(), null)); try (JavaBinCodec jbc = new JavaBinCodec(baos= new ByteArrayOutputStream(), null)) {
m = (Map) new JavaBinCodec().unmarshal(new ByteArrayInputStream(baos.toByteArray())); writeData(jbc);
try (JavaBinCodec jbcUn = new JavaBinCodec()) {
m = (Map) jbcUn.unmarshal(new ByteArrayInputStream(baos.toByteArray()));
}
}
checkValues(m); checkValues(m);
} }

View File

@ -372,28 +372,27 @@ public class TestSubQueryTransformer extends SolrTestCaseJ4 {
johnTwoFL.setParams(params); johnTwoFL.setParams(params);
final NamedList<Object> unmarshalled; final NamedList<Object> unmarshalled;
{ SolrCore core = johnTwoFL.getCore();
SolrCore core = johnTwoFL.getCore(); SolrQueryResponse rsp = new SolrQueryResponse();
SolrQueryResponse rsp = new SolrQueryResponse(); SolrRequestInfo.setRequestInfo(new SolrRequestInfo(johnTwoFL, rsp));
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(johnTwoFL, rsp));
SolrQueryResponse response = h.queryAndResponse( SolrQueryResponse response = h.queryAndResponse(
johnTwoFL.getParams().get(CommonParams.QT), johnTwoFL); johnTwoFL.getParams().get(CommonParams.QT), johnTwoFL);
BinaryQueryResponseWriter responseWriter = (BinaryQueryResponseWriter) core.getQueryResponseWriter(johnTwoFL); BinaryQueryResponseWriter responseWriter = (BinaryQueryResponseWriter) core.getQueryResponseWriter(johnTwoFL);
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ByteArrayOutputStream bytes = new ByteArrayOutputStream();
responseWriter.write(bytes,johnTwoFL,response); responseWriter.write(bytes, johnTwoFL, response);
unmarshalled = (NamedList<Object>) new JavaBinCodec().unmarshal( try (JavaBinCodec jbc = new JavaBinCodec()) {
new ByteArrayInputStream(bytes.toByteArray())); unmarshalled = (NamedList<Object>) jbc.unmarshal(
new ByteArrayInputStream(bytes.toByteArray()));
johnTwoFL.close();
SolrRequestInfo.clearRequestInfo();
} }
johnTwoFL.close();
SolrRequestInfo.clearRequestInfo();
SolrDocumentList resultDocs = (SolrDocumentList)(unmarshalled.get("response")); SolrDocumentList resultDocs = (SolrDocumentList)(unmarshalled.get("response"));
{
Map<String,String> engText = new HashMap<>(); Map<String,String> engText = new HashMap<>();
engText.put("text_t", "These guys develop stuff"); engText.put("text_t", "These guys develop stuff");
@ -415,7 +414,6 @@ public class TestSubQueryTransformer extends SolrTestCaseJ4 {
} }
} }
} }
}
} }
@Test @Test

View File

@ -412,11 +412,15 @@ public class AddBlockUpdateTest extends SolrTestCaseJ4 {
} }
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
new JavaBinCodec().marshal(topDocument, os); try (JavaBinCodec jbc = new JavaBinCodec()) {
jbc.marshal(topDocument, os);
}
byte[] buffer = os.toByteArray(); byte[] buffer = os.toByteArray();
//now read the Object back //now read the Object back
InputStream is = new ByteArrayInputStream(buffer); SolrInputDocument result;
SolrInputDocument result = (SolrInputDocument) new JavaBinCodec().unmarshal(is); try (JavaBinCodec jbc = new JavaBinCodec(); InputStream is = new ByteArrayInputStream(buffer)) {
result = (SolrInputDocument) jbc.unmarshal(is);
}
assertEquals(2, result.size()); assertEquals(2, result.size());
assertEquals("v1", result.getFieldValue("parent_f1")); assertEquals("v1", result.getFieldValue("parent_f1"));
assertEquals("v2", result.getFieldValue("parent_f2")); assertEquals("v2", result.getFieldValue("parent_f2"));

View File

@ -45,8 +45,7 @@ public class StreamingBinaryResponseParser extends BinaryResponseParser {
@Override @Override
public NamedList<Object> processResponse(InputStream body, String encoding) { public NamedList<Object> processResponse(InputStream body, String encoding) {
try { try (JavaBinCodec codec = new JavaBinCodec() {
JavaBinCodec codec = new JavaBinCodec() {
@Override @Override
public SolrDocument readSolrDocument(DataInputInputStream dis) throws IOException { public SolrDocument readSolrDocument(DataInputInputStream dis) throws IOException {
@ -80,7 +79,7 @@ public class StreamingBinaryResponseParser extends BinaryResponseParser {
} }
return solrDocs; return solrDocs;
} }
}; };) {
return (NamedList<Object>) codec.unmarshal(body); return (NamedList<Object>) codec.unmarshal(body);
} }

View File

@ -87,8 +87,9 @@ public class JavaBinUpdateRequestCodec {
} }
nl.add("docs", docIter); nl.add("docs", docIter);
} }
JavaBinCodec codec = new JavaBinCodec(); try (JavaBinCodec codec = new JavaBinCodec()) {
codec.marshal(nl, os); codec.marshal(nl, os);
}
} }
/** /**
@ -110,7 +111,7 @@ public class JavaBinUpdateRequestCodec {
Map<String,Map<String,Object>> delByIdMap; Map<String,Map<String,Object>> delByIdMap;
List<String> delByQ; List<String> delByQ;
final NamedList[] namedList = new NamedList[1]; final NamedList[] namedList = new NamedList[1];
JavaBinCodec codec = new JavaBinCodec() { try (JavaBinCodec codec = new JavaBinCodec() {
// NOTE: this only works because this is an anonymous inner class // NOTE: this only works because this is an anonymous inner class
// which will only ever be used on a single stream -- if this class // which will only ever be used on a single stream -- if this class
@ -189,9 +190,10 @@ public class JavaBinUpdateRequestCodec {
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
} }
}; };) {
codec.unmarshal(is); codec.unmarshal(is);
}
// NOTE: if the update request contains only delete commands the params // NOTE: if the update request contains only delete commands the params
// must be loaded now // must be loaded now

View File

@ -150,11 +150,12 @@ public class JavaBinCodec implements PushWriter {
} }
public void marshal(Object nl, OutputStream os) throws IOException { public void marshal(Object nl, OutputStream os) throws IOException {
initWrite(os);
try { try {
initWrite(os);
writeVal(nl); writeVal(nl);
} finally { } finally {
finish(); alreadyMarshalled = true;
daos.flushBuffer();
} }
} }
@ -164,11 +165,6 @@ public class JavaBinCodec implements PushWriter {
daos.writeByte(VERSION); daos.writeByte(VERSION);
} }
protected void finish() throws IOException {
closed = true;
daos.flushBuffer();
alreadyMarshalled = true;
}
/** expert: sets a new output stream */ /** expert: sets a new output stream */
public void init(FastOutputStream os) { public void init(FastOutputStream os) {
@ -1199,11 +1195,10 @@ public class JavaBinCodec implements PushWriter {
} }
} }
private boolean closed;
@Override @Override
public void close() throws IOException { public void close() throws IOException {
if (closed) return; if (daos != null) {
finish(); daos.flushBuffer();
}
} }
} }

View File

@ -55,11 +55,13 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
public void testStrings() throws Exception { public void testStrings() throws Exception {
for (int i = 0; i < 10000 * RANDOM_MULTIPLIER; i++) { for (int i = 0; i < 10000 * RANDOM_MULTIPLIER; i++) {
String s = TestUtil.randomUnicodeString(random()); String s = TestUtil.randomUnicodeString(random());
ByteArrayOutputStream os = new ByteArrayOutputStream(); try (JavaBinCodec jbcO = new JavaBinCodec(); ByteArrayOutputStream os = new ByteArrayOutputStream()) {
new JavaBinCodec().marshal(s, os); jbcO.marshal(s, os);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); try (JavaBinCodec jbcI = new JavaBinCodec(); ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray())) {
Object o = new JavaBinCodec().unmarshal(is); Object o = jbcI.unmarshal(is);
assertEquals(s, o); assertEquals(s, o);
}
}
} }
} }
@ -165,14 +167,13 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
@Test @Test
public void testBackCompat() throws IOException { public void testBackCompat() throws IOException {
JavaBinCodec javabin = new JavaBinCodec(){ try (InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN); JavaBinCodec javabin = new JavaBinCodec(){
@Override @Override
public List<Object> readIterator(DataInputInputStream fis) throws IOException { public List<Object> readIterator(DataInputInputStream fis) throws IOException {
return super.readIterator(fis); return super.readIterator(fis);
} }
}; };)
try { {
InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN);
List<Object> unmarshaledObj = (List<Object>) javabin.unmarshal(is); List<Object> unmarshaledObj = (List<Object>) javabin.unmarshal(is);
List<Object> matchObj = generateAllDataTypes(); List<Object> matchObj = generateAllDataTypes();
compareObjects(unmarshaledObj, matchObj); compareObjects(unmarshaledObj, matchObj);
@ -207,13 +208,13 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
@Test @Test
public void testBackCompatForSolrDocumentWithChildDocs() throws IOException { public void testBackCompatForSolrDocumentWithChildDocs() throws IOException {
JavaBinCodec javabin = new JavaBinCodec(){ try (JavaBinCodec javabin = new JavaBinCodec(){
@Override @Override
public List<Object> readIterator(DataInputInputStream fis) throws IOException { public List<Object> readIterator(DataInputInputStream fis) throws IOException {
return super.readIterator(fis); return super.readIterator(fis);
} }
}; };)
try { {
InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN_CHILD_DOCS); InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN_CHILD_DOCS);
SolrDocument sdoc = (SolrDocument) javabin.unmarshal(is); SolrDocument sdoc = (SolrDocument) javabin.unmarshal(is);
SolrDocument matchSolrDoc = generateSolrDocumentWithChildDocs(); SolrDocument matchSolrDoc = generateSolrDocumentWithChildDocs();
@ -225,34 +226,30 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
@Test @Test
public void testForwardCompat() throws IOException { public void testForwardCompat() throws IOException {
JavaBinCodec javabin = new JavaBinCodec(); try (JavaBinCodec javabin = new JavaBinCodec(); ByteArrayOutputStream os = new ByteArrayOutputStream()) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
Object data = generateAllDataTypes(); Object data = generateAllDataTypes();
try { try {
javabin.marshal(data, os); javabin.marshal(data, os);
byte[] newFormatBytes = os.toByteArray(); byte[] newFormatBytes = os.toByteArray();
InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN); InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN);
byte[] currentFormatBytes = IOUtils.toByteArray(is); byte[] currentFormatBytes = IOUtils.toByteArray(is);
for (int i = 1; i < currentFormatBytes.length; i++) {//ignore the first byte. It is version information for (int i = 1; i < currentFormatBytes.length; i++) {//ignore the first byte. It is version information
assertEquals(newFormatBytes[i], currentFormatBytes[i]); assertEquals(newFormatBytes[i], currentFormatBytes[i]);
}
} catch (IOException e) {
throw e;
} }
} catch (IOException e) {
throw e;
} }
} }
@Test @Test
public void testForwardCompatForSolrDocumentWithChildDocs() throws IOException { public void testForwardCompatForSolrDocumentWithChildDocs() throws IOException {
JavaBinCodec javabin = new JavaBinCodec();
ByteArrayOutputStream os = new ByteArrayOutputStream();
SolrDocument sdoc = generateSolrDocumentWithChildDocs(); SolrDocument sdoc = generateSolrDocumentWithChildDocs();
try { try (JavaBinCodec javabin = new JavaBinCodec(); ByteArrayOutputStream os = new ByteArrayOutputStream()) {
javabin.marshal(sdoc, os); javabin.marshal(sdoc, os);
byte[] newFormatBytes = os.toByteArray(); byte[] newFormatBytes = os.toByteArray();
@ -262,11 +259,9 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
for (int i = 1; i < currentFormatBytes.length; i++) {//ignore the first byte. It is version information for (int i = 1; i < currentFormatBytes.length; i++) {//ignore the first byte. It is version information
assertEquals(newFormatBytes[i], currentFormatBytes[i]); assertEquals(newFormatBytes[i], currentFormatBytes[i]);
} }
} catch (IOException e) { } catch (IOException e) {
throw e; throw e;
} }
} }
@Test @Test
@ -283,14 +278,16 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
return getObject(getBytes(o)); return getObject(getBytes(o));
} }
private static byte[] getBytes(Object o) throws IOException { private static byte[] getBytes(Object o) throws IOException {
JavaBinCodec javabin = new JavaBinCodec(); try (JavaBinCodec javabin = new JavaBinCodec(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); javabin.marshal(o, baos);
javabin.marshal(o, baos); return baos.toByteArray();
return baos.toByteArray(); }
} }
private static Object getObject(byte[] bytes) throws IOException { private static Object getObject(byte[] bytes) throws IOException {
return new JavaBinCodec().unmarshal(new ByteArrayInputStream(bytes)); try (JavaBinCodec jbc = new JavaBinCodec()) {
return jbc.unmarshal(new ByteArrayInputStream(bytes));
}
} }
@ -343,10 +340,15 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
JavaBinCodec.StringCache stringCache = new JavaBinCodec.StringCache(new MapBackedCache<>(new HashMap<>())); JavaBinCodec.StringCache stringCache = new JavaBinCodec.StringCache(new MapBackedCache<>(new HashMap<>()));
m1 = (Map) new JavaBinCodec(null, stringCache).unmarshal(new ByteArrayInputStream(b1)); try (JavaBinCodec c1 = new JavaBinCodec(null, stringCache);
m2 = (Map) new JavaBinCodec(null, stringCache).unmarshal(new ByteArrayInputStream(b2)); JavaBinCodec c2 = new JavaBinCodec(null, stringCache)) {
l1 = new ArrayList<>(m1.keySet());
l2 = new ArrayList<>(m2.keySet()); m1 = (Map) c1.unmarshal(new ByteArrayInputStream(b1));
m2 = (Map) c2.unmarshal(new ByteArrayInputStream(b2));
l1 = new ArrayList<>(m1.keySet());
l2 = new ArrayList<>(m2.keySet());
}
assertTrue(l1.get(0).equals(l2.get(0))); assertTrue(l1.get(0).equals(l2.get(0)));
assertTrue(l1.get(0) == l2.get(0)); assertTrue(l1.get(0) == l2.get(0));
assertTrue(l1.get(1).equals(l2.get(1))); assertTrue(l1.get(1).equals(l2.get(1)));
@ -556,11 +558,12 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
while (--iter >= 0) { while (--iter >= 0) {
if (++bufnum >= buffers.length) bufnum = 0; if (++bufnum >= buffers.length) bufnum = 0;
byte[] buf = buffers[bufnum]; byte[] buf = buffers[bufnum];
JavaBinCodec javabin = new JavaBinCodec(null, stringCache); try (JavaBinCodec javabin = new JavaBinCodec(null, stringCache)) {
FastInputStream in = new FastInputStream(empty, buf, 0, buf.length); FastInputStream in = new FastInputStream(empty, buf, 0, buf.length);
Object o = javabin.unmarshal( in ); Object o = javabin.unmarshal(in);
if (o instanceof SolrDocument) { if (o instanceof SolrDocument) {
ret += ((SolrDocument) o).size(); ret += ((SolrDocument) o).size();
}
} }
} }
return ret; return ret;

View File

@ -31,7 +31,7 @@ import java.util.HashMap;
public class TestNamedListCodec extends LuceneTestCase { public class TestNamedListCodec extends LuceneTestCase {
public void testSimple() throws Exception{ public void testSimple() throws Exception{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
NamedList nl = new NamedList(); NamedList nl = new NamedList();
Float fval = new Float( 10.01f ); Float fval = new Float( 10.01f );
Boolean bval = Boolean.TRUE; Boolean bval = Boolean.TRUE;
@ -74,10 +74,14 @@ public class TestNamedListCodec extends LuceneTestCase {
list.add(doc); list.add(doc);
nl.add("zzz",doc); nl.add("zzz",doc);
byte[] arr;
new JavaBinCodec(null).marshal(nl,baos); try (JavaBinCodec jbc = new JavaBinCodec(null); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
byte[] arr = baos.toByteArray(); jbc.marshal(nl, baos);
nl = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(arr)); arr = baos.toByteArray();
}
try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayInputStream bais = new ByteArrayInputStream(arr)) {
nl = (NamedList) jbc.unmarshal(bais);
}
assertEquals(3, nl.size()); assertEquals(3, nl.size());
@ -89,7 +93,7 @@ public class TestNamedListCodec extends LuceneTestCase {
} }
public void testIterator() throws Exception{ public void testIterator() throws Exception{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
NamedList nl = new NamedList(); NamedList nl = new NamedList();
Float fval = new Float( 10.01f ); Float fval = new Float( 10.01f );
Boolean bval = Boolean.TRUE; Boolean bval = Boolean.TRUE;
@ -114,17 +118,21 @@ public class TestNamedListCodec extends LuceneTestCase {
list.add(doc); list.add(doc);
nl.add("zzz",list.iterator()); nl.add("zzz",list.iterator());
byte[] arr;
new JavaBinCodec(null).marshal(nl,baos); try (JavaBinCodec jbc = new JavaBinCodec(null); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
byte[] arr = baos.toByteArray(); jbc.marshal(nl, baos);
nl = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(arr)); arr = baos.toByteArray();
}
try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayInputStream bais = new ByteArrayInputStream(arr)) {
nl = (NamedList) jbc.unmarshal(bais);
}
List l = (List) nl.get("zzz"); List l = (List) nl.get("zzz");
assertEquals(list.size(), l.size()); assertEquals(list.size(), l.size());
} }
public void testIterable() throws Exception { public void testIterable() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
NamedList r = new NamedList(); NamedList r = new NamedList();
@ -137,11 +145,14 @@ public class TestNamedListCodec extends LuceneTestCase {
r.add("more", "less"); r.add("more", "less");
r.add("values", map.values()); r.add("values", map.values());
r.add("finally", "the end"); r.add("finally", "the end");
new JavaBinCodec(null).marshal(r,baos); byte[] arr;
byte[] arr = baos.toByteArray(); try (JavaBinCodec jbc = new JavaBinCodec(null); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
jbc.marshal(r,baos);
arr = baos.toByteArray();
}
try { try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayInputStream bais = new ByteArrayInputStream(arr)) {
NamedList result = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(arr)); NamedList result = (NamedList) jbc.unmarshal(bais);
assertTrue("result is null and it shouldn't be", result != null); assertTrue("result is null and it shouldn't be", result != null);
List keys = (List) result.get("keys"); List keys = (List) result.get("keys");
assertTrue("keys is null and it shouldn't be", keys != null); assertTrue("keys is null and it shouldn't be", keys != null);
@ -247,13 +258,16 @@ public class TestNamedListCodec extends LuceneTestCase {
for (int i=0; i<10000; i++) { // pump up the iterations for good stress testing for (int i=0; i<10000; i++) { // pump up the iterations for good stress testing
nl = rNamedList(3); nl = rNamedList(3);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] arr;
new JavaBinCodec(null).marshal(nl,baos); try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
byte[] arr = baos.toByteArray(); jbc.marshal(nl, baos);
arr = baos.toByteArray();
}
// System.out.println(arr.length); // System.out.println(arr.length);
res = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(arr)); try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayInputStream bais = new ByteArrayInputStream(arr)) {
cmp = BaseDistributedSearchTestCase.compare(nl, res, 0, null); res = (NamedList) jbc.unmarshal(bais);
cmp = BaseDistributedSearchTestCase.compare(nl, res, 0, null);
}
if (cmp != null) { if (cmp != null) {
System.out.println(nl); System.out.println(nl);
System.out.println(res); System.out.println(res);
@ -261,5 +275,4 @@ public class TestNamedListCodec extends LuceneTestCase {
} }
} }
} }
} }