mirror of https://github.com/apache/lucene.git
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:
parent
943bf5ab5b
commit
64093d6df1
|
@ -277,6 +277,9 @@ Other Changes
|
|||
* SOLR-10800: Factor out HttpShardHandler.transformReplicasToShardUrls from HttpShardHandler.prepDistributed.
|
||||
(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 ==================
|
||||
|
||||
Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.
|
||||
|
|
|
@ -48,7 +48,9 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
|
|||
public void write(OutputStream out, SolrQueryRequest req, SolrQueryResponse response) throws IOException {
|
||||
Resolver resolver = new Resolver(req, response.getReturnFields());
|
||||
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
|
||||
|
@ -160,10 +162,14 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
|
|||
Resolver resolver = new Resolver(req, rsp.getReturnFields());
|
||||
|
||||
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();
|
||||
return (NamedList<Object>) new JavaBinCodec(resolver).unmarshal(in);
|
||||
try (JavaBinCodec jbc = new JavaBinCodec(resolver)) {
|
||||
return (NamedList<Object>) jbc.unmarshal(in);
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
|
|
|
@ -183,9 +183,8 @@ public final class CursorMark {
|
|||
List<Object> pieces = null;
|
||||
try {
|
||||
final byte[] rawData = Base64.base64ToByteArray(serialized);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(rawData);
|
||||
try {
|
||||
pieces = (List<Object>) new JavaBinCodec().unmarshal(in);
|
||||
try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayInputStream in = new ByteArrayInputStream(rawData)){
|
||||
pieces = (List<Object>) jbc.unmarshal(in);
|
||||
boolean b = false;
|
||||
for (Object o : pieces) {
|
||||
if (o instanceof BytesRefBuilder || o instanceof BytesRef || o instanceof String) {
|
||||
|
@ -196,8 +195,6 @@ public final class CursorMark {
|
|||
in.reset();
|
||||
pieces = (List<Object>) new JavaBinCodec().unmarshal(in);
|
||||
}
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
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)
|
||||
// could help provide more validation beyond just the number of clauses.
|
||||
|
||||
try {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(256);
|
||||
try {
|
||||
new JavaBinCodec().marshal(marshalledValues, out);
|
||||
byte[] rawData = out.toByteArray();
|
||||
return Base64.byteArrayToBase64(rawData, 0, rawData.length);
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayOutputStream out = new ByteArrayOutputStream(256)) {
|
||||
jbc.marshal(marshalledValues, out);
|
||||
byte[] rawData = out.toByteArray();
|
||||
return Base64.byteArrayToBase64(rawData, 0, rawData.length);
|
||||
} catch (Exception ex) {
|
||||
throw new SolrException(ErrorCode.SERVER_ERROR,
|
||||
"Unable to format search after totem", ex);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -60,7 +60,10 @@ public class TestBinaryResponseWriter extends AbstractSolrTestCase {
|
|||
BinaryQueryResponseWriter writer = (BinaryQueryResponseWriter) h.getCore().getQueryResponseWriter("javabin");
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
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");
|
||||
for (Object doc : docs) {
|
||||
SolrDocument document = (SolrDocument) doc;
|
||||
|
|
|
@ -169,15 +169,19 @@ public class TestJavabinTupleStreamParser extends SolrTestCaseJ4 {
|
|||
public void testSolrDocumentList() throws IOException {
|
||||
SolrQueryResponse response = new SolrQueryResponse();
|
||||
SolrDocumentList l = constructSolrDocList(response);
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
new JavaBinCodec().marshal(response.getValues(), baos);
|
||||
try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
||||
jbc.marshal(response.getValues(), baos);
|
||||
}
|
||||
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<>();
|
||||
Map m = null;
|
||||
JavabinTupleStreamParser parser = new JavabinTupleStreamParser(new ByteArrayInputStream(bytes), false);
|
||||
while ((m = parser.next()) != null) {
|
||||
list.add(m);
|
||||
try (JavabinTupleStreamParser parser = new JavabinTupleStreamParser(new ByteArrayInputStream(bytes), false)) {
|
||||
while ((m = parser.next()) != null) {
|
||||
list.add(m);
|
||||
}
|
||||
}
|
||||
assertEquals(l.size(), list.size());
|
||||
for(int i =0;i<list.size();i++){
|
||||
|
@ -189,7 +193,9 @@ public class TestJavabinTupleStreamParser extends SolrTestCaseJ4 {
|
|||
SolrQueryResponse response = new SolrQueryResponse();
|
||||
response.getValues().add("results", o);
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
new JavaBinCodec().marshal(response.getValues(), baos);
|
||||
try (JavaBinCodec jbc = new JavaBinCodec()) {
|
||||
jbc.marshal(response.getValues(), baos);
|
||||
}
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,8 +53,12 @@ public class TestPushWriter extends SolrTestCaseJ4 {
|
|||
log.info(new String(baos.toByteArray(), "UTF-8"));
|
||||
Map m = (Map) Utils.fromJSON(baos.toByteArray());
|
||||
checkValues(m);
|
||||
writeData(new JavaBinCodec(baos= new ByteArrayOutputStream(), null));
|
||||
m = (Map) new JavaBinCodec().unmarshal(new ByteArrayInputStream(baos.toByteArray()));
|
||||
try (JavaBinCodec jbc = new JavaBinCodec(baos= new ByteArrayOutputStream(), null)) {
|
||||
writeData(jbc);
|
||||
try (JavaBinCodec jbcUn = new JavaBinCodec()) {
|
||||
m = (Map) jbcUn.unmarshal(new ByteArrayInputStream(baos.toByteArray()));
|
||||
}
|
||||
}
|
||||
checkValues(m);
|
||||
}
|
||||
|
||||
|
|
|
@ -372,28 +372,27 @@ public class TestSubQueryTransformer extends SolrTestCaseJ4 {
|
|||
johnTwoFL.setParams(params);
|
||||
|
||||
final NamedList<Object> unmarshalled;
|
||||
{
|
||||
SolrCore core = johnTwoFL.getCore();
|
||||
SolrQueryResponse rsp = new SolrQueryResponse();
|
||||
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(johnTwoFL, rsp));
|
||||
|
||||
SolrCore core = johnTwoFL.getCore();
|
||||
SolrQueryResponse rsp = new SolrQueryResponse();
|
||||
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(johnTwoFL, rsp));
|
||||
|
||||
SolrQueryResponse response = h.queryAndResponse(
|
||||
johnTwoFL.getParams().get(CommonParams.QT), johnTwoFL);
|
||||
|
||||
|
||||
BinaryQueryResponseWriter responseWriter = (BinaryQueryResponseWriter) core.getQueryResponseWriter(johnTwoFL);
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
responseWriter.write(bytes,johnTwoFL,response);
|
||||
|
||||
unmarshalled = (NamedList<Object>) new JavaBinCodec().unmarshal(
|
||||
new ByteArrayInputStream(bytes.toByteArray()));
|
||||
|
||||
johnTwoFL.close();
|
||||
SolrRequestInfo.clearRequestInfo();
|
||||
responseWriter.write(bytes, johnTwoFL, response);
|
||||
|
||||
try (JavaBinCodec jbc = new JavaBinCodec()) {
|
||||
unmarshalled = (NamedList<Object>) jbc.unmarshal(
|
||||
new ByteArrayInputStream(bytes.toByteArray()));
|
||||
}
|
||||
|
||||
johnTwoFL.close();
|
||||
SolrRequestInfo.clearRequestInfo();
|
||||
|
||||
SolrDocumentList resultDocs = (SolrDocumentList)(unmarshalled.get("response"));
|
||||
|
||||
{
|
||||
Map<String,String> engText = new HashMap<>();
|
||||
engText.put("text_t", "These guys develop stuff");
|
||||
|
||||
|
@ -415,7 +414,6 @@ public class TestSubQueryTransformer extends SolrTestCaseJ4 {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -412,11 +412,15 @@ public class AddBlockUpdateTest extends SolrTestCaseJ4 {
|
|||
}
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
new JavaBinCodec().marshal(topDocument, os);
|
||||
try (JavaBinCodec jbc = new JavaBinCodec()) {
|
||||
jbc.marshal(topDocument, os);
|
||||
}
|
||||
byte[] buffer = os.toByteArray();
|
||||
//now read the Object back
|
||||
InputStream is = new ByteArrayInputStream(buffer);
|
||||
SolrInputDocument result = (SolrInputDocument) new JavaBinCodec().unmarshal(is);
|
||||
SolrInputDocument result;
|
||||
try (JavaBinCodec jbc = new JavaBinCodec(); InputStream is = new ByteArrayInputStream(buffer)) {
|
||||
result = (SolrInputDocument) jbc.unmarshal(is);
|
||||
}
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("v1", result.getFieldValue("parent_f1"));
|
||||
assertEquals("v2", result.getFieldValue("parent_f2"));
|
||||
|
|
|
@ -45,8 +45,7 @@ public class StreamingBinaryResponseParser extends BinaryResponseParser {
|
|||
|
||||
@Override
|
||||
public NamedList<Object> processResponse(InputStream body, String encoding) {
|
||||
try {
|
||||
JavaBinCodec codec = new JavaBinCodec() {
|
||||
try (JavaBinCodec codec = new JavaBinCodec() {
|
||||
|
||||
@Override
|
||||
public SolrDocument readSolrDocument(DataInputInputStream dis) throws IOException {
|
||||
|
@ -80,7 +79,7 @@ public class StreamingBinaryResponseParser extends BinaryResponseParser {
|
|||
}
|
||||
return solrDocs;
|
||||
}
|
||||
};
|
||||
};) {
|
||||
|
||||
return (NamedList<Object>) codec.unmarshal(body);
|
||||
}
|
||||
|
|
|
@ -87,8 +87,9 @@ public class JavaBinUpdateRequestCodec {
|
|||
}
|
||||
nl.add("docs", docIter);
|
||||
}
|
||||
JavaBinCodec codec = new JavaBinCodec();
|
||||
codec.marshal(nl, os);
|
||||
try (JavaBinCodec codec = new JavaBinCodec()) {
|
||||
codec.marshal(nl, os);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -110,7 +111,7 @@ public class JavaBinUpdateRequestCodec {
|
|||
Map<String,Map<String,Object>> delByIdMap;
|
||||
List<String> delByQ;
|
||||
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
|
||||
// which will only ever be used on a single stream -- if this class
|
||||
|
@ -189,9 +190,10 @@ public class JavaBinUpdateRequestCodec {
|
|||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
};
|
||||
};) {
|
||||
|
||||
codec.unmarshal(is);
|
||||
codec.unmarshal(is);
|
||||
}
|
||||
|
||||
// NOTE: if the update request contains only delete commands the params
|
||||
// must be loaded now
|
||||
|
|
|
@ -150,11 +150,12 @@ public class JavaBinCodec implements PushWriter {
|
|||
}
|
||||
|
||||
public void marshal(Object nl, OutputStream os) throws IOException {
|
||||
initWrite(os);
|
||||
try {
|
||||
initWrite(os);
|
||||
writeVal(nl);
|
||||
} finally {
|
||||
finish();
|
||||
alreadyMarshalled = true;
|
||||
daos.flushBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -164,11 +165,6 @@ public class JavaBinCodec implements PushWriter {
|
|||
daos.writeByte(VERSION);
|
||||
}
|
||||
|
||||
protected void finish() throws IOException {
|
||||
closed = true;
|
||||
daos.flushBuffer();
|
||||
alreadyMarshalled = true;
|
||||
}
|
||||
|
||||
/** expert: sets a new output stream */
|
||||
public void init(FastOutputStream os) {
|
||||
|
@ -1199,11 +1195,10 @@ public class JavaBinCodec implements PushWriter {
|
|||
}
|
||||
}
|
||||
|
||||
private boolean closed;
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (closed) return;
|
||||
finish();
|
||||
if (daos != null) {
|
||||
daos.flushBuffer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,11 +55,13 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
|
|||
public void testStrings() throws Exception {
|
||||
for (int i = 0; i < 10000 * RANDOM_MULTIPLIER; i++) {
|
||||
String s = TestUtil.randomUnicodeString(random());
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
new JavaBinCodec().marshal(s, os);
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
Object o = new JavaBinCodec().unmarshal(is);
|
||||
assertEquals(s, o);
|
||||
try (JavaBinCodec jbcO = new JavaBinCodec(); ByteArrayOutputStream os = new ByteArrayOutputStream()) {
|
||||
jbcO.marshal(s, os);
|
||||
try (JavaBinCodec jbcI = new JavaBinCodec(); ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray())) {
|
||||
Object o = jbcI.unmarshal(is);
|
||||
assertEquals(s, o);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -165,14 +167,13 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
|
|||
|
||||
@Test
|
||||
public void testBackCompat() throws IOException {
|
||||
JavaBinCodec javabin = new JavaBinCodec(){
|
||||
try (InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN); JavaBinCodec javabin = new JavaBinCodec(){
|
||||
@Override
|
||||
public List<Object> readIterator(DataInputInputStream fis) throws IOException {
|
||||
return super.readIterator(fis);
|
||||
}
|
||||
};
|
||||
try {
|
||||
InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN);
|
||||
};)
|
||||
{
|
||||
List<Object> unmarshaledObj = (List<Object>) javabin.unmarshal(is);
|
||||
List<Object> matchObj = generateAllDataTypes();
|
||||
compareObjects(unmarshaledObj, matchObj);
|
||||
|
@ -207,13 +208,13 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
|
|||
|
||||
@Test
|
||||
public void testBackCompatForSolrDocumentWithChildDocs() throws IOException {
|
||||
JavaBinCodec javabin = new JavaBinCodec(){
|
||||
try (JavaBinCodec javabin = new JavaBinCodec(){
|
||||
@Override
|
||||
public List<Object> readIterator(DataInputInputStream fis) throws IOException {
|
||||
return super.readIterator(fis);
|
||||
}
|
||||
};
|
||||
try {
|
||||
};)
|
||||
{
|
||||
InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN_CHILD_DOCS);
|
||||
SolrDocument sdoc = (SolrDocument) javabin.unmarshal(is);
|
||||
SolrDocument matchSolrDoc = generateSolrDocumentWithChildDocs();
|
||||
|
@ -225,34 +226,30 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
|
|||
|
||||
@Test
|
||||
public void testForwardCompat() throws IOException {
|
||||
JavaBinCodec javabin = new JavaBinCodec();
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
try (JavaBinCodec javabin = new JavaBinCodec(); ByteArrayOutputStream os = new ByteArrayOutputStream()) {
|
||||
|
||||
Object data = generateAllDataTypes();
|
||||
try {
|
||||
javabin.marshal(data, os);
|
||||
byte[] newFormatBytes = os.toByteArray();
|
||||
Object data = generateAllDataTypes();
|
||||
try {
|
||||
javabin.marshal(data, os);
|
||||
byte[] newFormatBytes = os.toByteArray();
|
||||
|
||||
InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN);
|
||||
byte[] currentFormatBytes = IOUtils.toByteArray(is);
|
||||
InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN);
|
||||
byte[] currentFormatBytes = IOUtils.toByteArray(is);
|
||||
|
||||
for (int i = 1; i < currentFormatBytes.length; i++) {//ignore the first byte. It is version information
|
||||
assertEquals(newFormatBytes[i], currentFormatBytes[i]);
|
||||
for (int i = 1; i < currentFormatBytes.length; i++) {//ignore the first byte. It is version information
|
||||
assertEquals(newFormatBytes[i], currentFormatBytes[i]);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForwardCompatForSolrDocumentWithChildDocs() throws IOException {
|
||||
JavaBinCodec javabin = new JavaBinCodec();
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
|
||||
SolrDocument sdoc = generateSolrDocumentWithChildDocs();
|
||||
try {
|
||||
try (JavaBinCodec javabin = new JavaBinCodec(); ByteArrayOutputStream os = new ByteArrayOutputStream()) {
|
||||
javabin.marshal(sdoc, os);
|
||||
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
|
||||
assertEquals(newFormatBytes[i], currentFormatBytes[i]);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -283,14 +278,16 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
|
|||
return getObject(getBytes(o));
|
||||
}
|
||||
private static byte[] getBytes(Object o) throws IOException {
|
||||
JavaBinCodec javabin = new JavaBinCodec();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
javabin.marshal(o, baos);
|
||||
return baos.toByteArray();
|
||||
try (JavaBinCodec javabin = new JavaBinCodec(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
||||
javabin.marshal(o, baos);
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
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<>()));
|
||||
|
||||
|
||||
m1 = (Map) new JavaBinCodec(null, stringCache).unmarshal(new ByteArrayInputStream(b1));
|
||||
m2 = (Map) new JavaBinCodec(null, stringCache).unmarshal(new ByteArrayInputStream(b2));
|
||||
l1 = new ArrayList<>(m1.keySet());
|
||||
l2 = new ArrayList<>(m2.keySet());
|
||||
try (JavaBinCodec c1 = new JavaBinCodec(null, stringCache);
|
||||
JavaBinCodec c2 = new JavaBinCodec(null, stringCache)) {
|
||||
|
||||
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) == l2.get(0));
|
||||
assertTrue(l1.get(1).equals(l2.get(1)));
|
||||
|
@ -556,11 +558,12 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
|
|||
while (--iter >= 0) {
|
||||
if (++bufnum >= buffers.length) bufnum = 0;
|
||||
byte[] buf = buffers[bufnum];
|
||||
JavaBinCodec javabin = new JavaBinCodec(null, stringCache);
|
||||
FastInputStream in = new FastInputStream(empty, buf, 0, buf.length);
|
||||
Object o = javabin.unmarshal( in );
|
||||
if (o instanceof SolrDocument) {
|
||||
ret += ((SolrDocument) o).size();
|
||||
try (JavaBinCodec javabin = new JavaBinCodec(null, stringCache)) {
|
||||
FastInputStream in = new FastInputStream(empty, buf, 0, buf.length);
|
||||
Object o = javabin.unmarshal(in);
|
||||
if (o instanceof SolrDocument) {
|
||||
ret += ((SolrDocument) o).size();
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
|
|
|
@ -31,7 +31,7 @@ import java.util.HashMap;
|
|||
|
||||
public class TestNamedListCodec extends LuceneTestCase {
|
||||
public void testSimple() throws Exception{
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
NamedList nl = new NamedList();
|
||||
Float fval = new Float( 10.01f );
|
||||
Boolean bval = Boolean.TRUE;
|
||||
|
@ -74,10 +74,14 @@ public class TestNamedListCodec extends LuceneTestCase {
|
|||
list.add(doc);
|
||||
|
||||
nl.add("zzz",doc);
|
||||
|
||||
new JavaBinCodec(null).marshal(nl,baos);
|
||||
byte[] arr = baos.toByteArray();
|
||||
nl = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(arr));
|
||||
byte[] arr;
|
||||
try (JavaBinCodec jbc = new JavaBinCodec(null); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
||||
jbc.marshal(nl, baos);
|
||||
arr = baos.toByteArray();
|
||||
}
|
||||
try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayInputStream bais = new ByteArrayInputStream(arr)) {
|
||||
nl = (NamedList) jbc.unmarshal(bais);
|
||||
}
|
||||
|
||||
|
||||
assertEquals(3, nl.size());
|
||||
|
@ -89,7 +93,7 @@ public class TestNamedListCodec extends LuceneTestCase {
|
|||
}
|
||||
|
||||
public void testIterator() throws Exception{
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
NamedList nl = new NamedList();
|
||||
Float fval = new Float( 10.01f );
|
||||
Boolean bval = Boolean.TRUE;
|
||||
|
@ -114,17 +118,21 @@ public class TestNamedListCodec extends LuceneTestCase {
|
|||
list.add(doc);
|
||||
|
||||
nl.add("zzz",list.iterator());
|
||||
|
||||
new JavaBinCodec(null).marshal(nl,baos);
|
||||
byte[] arr = baos.toByteArray();
|
||||
nl = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(arr));
|
||||
byte[] arr;
|
||||
try (JavaBinCodec jbc = new JavaBinCodec(null); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
||||
jbc.marshal(nl, baos);
|
||||
arr = baos.toByteArray();
|
||||
}
|
||||
try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayInputStream bais = new ByteArrayInputStream(arr)) {
|
||||
nl = (NamedList) jbc.unmarshal(bais);
|
||||
}
|
||||
|
||||
List l = (List) nl.get("zzz");
|
||||
assertEquals(list.size(), l.size());
|
||||
}
|
||||
|
||||
public void testIterable() throws Exception {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
|
||||
NamedList r = new NamedList();
|
||||
|
||||
|
@ -137,11 +145,14 @@ public class TestNamedListCodec extends LuceneTestCase {
|
|||
r.add("more", "less");
|
||||
r.add("values", map.values());
|
||||
r.add("finally", "the end");
|
||||
new JavaBinCodec(null).marshal(r,baos);
|
||||
byte[] arr = baos.toByteArray();
|
||||
byte[] arr;
|
||||
try (JavaBinCodec jbc = new JavaBinCodec(null); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
||||
jbc.marshal(r,baos);
|
||||
arr = baos.toByteArray();
|
||||
}
|
||||
|
||||
try {
|
||||
NamedList result = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(arr));
|
||||
try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayInputStream bais = new ByteArrayInputStream(arr)) {
|
||||
NamedList result = (NamedList) jbc.unmarshal(bais);
|
||||
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,13 +258,16 @@ public class TestNamedListCodec extends LuceneTestCase {
|
|||
|
||||
for (int i=0; i<10000; i++) { // pump up the iterations for good stress testing
|
||||
nl = rNamedList(3);
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
new JavaBinCodec(null).marshal(nl,baos);
|
||||
byte[] arr = baos.toByteArray();
|
||||
byte[] arr;
|
||||
try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
||||
jbc.marshal(nl, baos);
|
||||
arr = baos.toByteArray();
|
||||
}
|
||||
// System.out.println(arr.length);
|
||||
res = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(arr));
|
||||
cmp = BaseDistributedSearchTestCase.compare(nl, res, 0, null);
|
||||
|
||||
try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayInputStream bais = new ByteArrayInputStream(arr)) {
|
||||
res = (NamedList) jbc.unmarshal(bais);
|
||||
cmp = BaseDistributedSearchTestCase.compare(nl, res, 0, null);
|
||||
}
|
||||
if (cmp != null) {
|
||||
System.out.println(nl);
|
||||
System.out.println(res);
|
||||
|
@ -261,5 +275,4 @@ public class TestNamedListCodec extends LuceneTestCase {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue