You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by no...@apache.org on 2016/09/06 06:12:58 UTC

lucene-solr:branch_6x: Refactor out the serialization & deserialization

Repository: lucene-solr
Updated Branches:
  refs/heads/branch_6x c61ee3346 -> b32de1ec7


Refactor out the serialization & deserialization


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/b32de1ec
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/b32de1ec
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/b32de1ec

Branch: refs/heads/branch_6x
Commit: b32de1ec7f4d04d708a95f51435a1948e6a911e6
Parents: c61ee33
Author: Noble Paul <no...@apache.org>
Authored: Tue Sep 6 11:39:04 2016 +0530
Committer: Noble Paul <no...@apache.org>
Committed: Tue Sep 6 11:42:35 2016 +0530

----------------------------------------------------------------------
 .../solr/common/util/TestJavaBinCodec.java      | 104 ++++++++++---------
 1 file changed, 56 insertions(+), 48 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/b32de1ec/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java b/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java
index cb6f9ed..96ddc8b 100644
--- a/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java
+++ b/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java
@@ -175,33 +175,36 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
       InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN);
       List<Object> unmarshaledObj = (List<Object>) javabin.unmarshal(is);
       List<Object> matchObj = generateAllDataTypes();
-
-      assertEquals(unmarshaledObj.size(), matchObj.size());
-      for(int i=0; i < unmarshaledObj.size(); i++) {
-
-        if(unmarshaledObj.get(i) instanceof byte[] && matchObj.get(i) instanceof byte[]) {
-          byte[] b1 = (byte[]) unmarshaledObj.get(i);
-          byte[] b2 = (byte[]) matchObj.get(i);
-          assertTrue(Arrays.equals(b1, b2));
-        } else if(unmarshaledObj.get(i) instanceof SolrDocument && matchObj.get(i) instanceof SolrDocument ) {
-          assertTrue(compareSolrDocument(unmarshaledObj.get(i), matchObj.get(i)));
-        } else if(unmarshaledObj.get(i) instanceof SolrDocumentList && matchObj.get(i) instanceof SolrDocumentList ) {
-          assertTrue(compareSolrDocumentList(unmarshaledObj.get(i), matchObj.get(i)));
-        } else if(unmarshaledObj.get(i) instanceof SolrInputDocument && matchObj.get(i) instanceof SolrInputDocument) {
-          assertTrue(compareSolrInputDocument(unmarshaledObj.get(i), matchObj.get(i)));
-        } else if(unmarshaledObj.get(i) instanceof SolrInputField && matchObj.get(i) instanceof SolrInputField) {
-          assertTrue(assertSolrInputFieldEquals(unmarshaledObj.get(i), matchObj.get(i)));
-        } else {
-          assertEquals(unmarshaledObj.get(i), matchObj.get(i));
-        }
-
-      }
+      compareObjects(unmarshaledObj, matchObj);
     } catch (IOException e) {
       throw e;
     }
 
   }
 
+  private void compareObjects(List unmarshaledObj, List matchObj) {
+    assertEquals(unmarshaledObj.size(), matchObj.size());
+    for (int i = 0; i < unmarshaledObj.size(); i++) {
+
+      if (unmarshaledObj.get(i) instanceof byte[] && matchObj.get(i) instanceof byte[]) {
+        byte[] b1 = (byte[]) unmarshaledObj.get(i);
+        byte[] b2 = (byte[]) matchObj.get(i);
+        assertTrue(Arrays.equals(b1, b2));
+      } else if (unmarshaledObj.get(i) instanceof SolrDocument && matchObj.get(i) instanceof SolrDocument) {
+        assertTrue(compareSolrDocument(unmarshaledObj.get(i), matchObj.get(i)));
+      } else if (unmarshaledObj.get(i) instanceof SolrDocumentList && matchObj.get(i) instanceof SolrDocumentList) {
+        assertTrue(compareSolrDocumentList(unmarshaledObj.get(i), matchObj.get(i)));
+      } else if (unmarshaledObj.get(i) instanceof SolrInputDocument && matchObj.get(i) instanceof SolrInputDocument) {
+        assertTrue(compareSolrInputDocument(unmarshaledObj.get(i), matchObj.get(i)));
+      } else if (unmarshaledObj.get(i) instanceof SolrInputField && matchObj.get(i) instanceof SolrInputField) {
+        assertTrue(assertSolrInputFieldEquals(unmarshaledObj.get(i), matchObj.get(i)));
+      } else {
+        assertEquals(unmarshaledObj.get(i), matchObj.get(i));
+      }
+
+    }
+  }
+
   @Test
   public void testBackCompatForSolrDocumentWithChildDocs() throws IOException {
     JavaBinCodec javabin = new JavaBinCodec(){
@@ -267,14 +270,33 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
   }
 
   @Test
-  public void testResponseChildDocuments() throws IOException {
+  public void testAllTypes() throws IOException {
+    List<Object> obj = generateAllDataTypes();
+    compareObjects(
+        (List) getObject(getBytes(obj)),
+        (List) obj
+    );
+  }
 
 
+  private static Object serializeAndDeserialize(Object o) throws IOException {
+    return getObject(getBytes(o));
+  }
+  private static byte[] getBytes(Object o) throws IOException {
     JavaBinCodec javabin = new JavaBinCodec();
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
-    javabin.marshal(generateSolrDocumentWithChildDocs(), baos);
+    javabin.marshal(o, baos);
+    return baos.toByteArray();
+  }
+
+  private static Object getObject(byte[] bytes) throws IOException {
+    return new JavaBinCodec().unmarshal(new ByteArrayInputStream(bytes));
+  }
 
-    SolrDocument result = (SolrDocument) javabin.unmarshal(new ByteArrayInputStream(baos.toByteArray()));
+
+  @Test
+  public void testResponseChildDocuments() throws IOException {
+    SolrDocument result = (SolrDocument) serializeAndDeserialize(generateSolrDocumentWithChildDocs());
     assertEquals(2, result.size());
     assertEquals("1", result.getFieldValue("id"));
     assertEquals("parentDocument", result.getFieldValue("subject"));
@@ -305,13 +327,11 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
   @Test
   public void testStringCaching() throws Exception {
     Map<String, Object> m = Utils.makeMap("key1", "val1", "key2", "val2");
+    byte[] b1 = getBytes(m);//copy 1
+    byte[] b2 = getBytes(m);//copy 2
+    Map m1 = (Map) getObject(b1);
+    Map m2 = (Map) getObject(b1);
 
-    ByteArrayOutputStream os1 = new ByteArrayOutputStream();
-    new JavaBinCodec().marshal(m, os1);
-    Map m1 = (Map) new JavaBinCodec().unmarshal(new ByteArrayInputStream(os1.toByteArray()));
-    ByteArrayOutputStream os2 = new ByteArrayOutputStream();
-    new JavaBinCodec().marshal(m, os2);
-    Map m2 = (Map) new JavaBinCodec().unmarshal(new ByteArrayInputStream(os2.toByteArray()));
     List l1 = new ArrayList<>(m1.keySet());
     List l2 = new ArrayList<>(m2.keySet());
 
@@ -346,8 +366,8 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
     });
 
 
-    m1 = (Map) new JavaBinCodec(null, stringCache).unmarshal(new ByteArrayInputStream(os1.toByteArray()));
-    m2 = (Map) new JavaBinCodec(null, stringCache).unmarshal(new ByteArrayInputStream(os2.toByteArray()));
+    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());
     assertTrue(l1.get(0).equals(l2.get(0)));
@@ -359,26 +379,19 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
   }
 
   public void genBinaryFiles() throws IOException {
-    JavaBinCodec javabin = new JavaBinCodec();
-    ByteArrayOutputStream os = new ByteArrayOutputStream();
-    
+
     Object data = generateAllDataTypes();
-    
-    javabin.marshal(data, os);
-    byte[] out = os.toByteArray();
+    byte[] out = getBytes(data);
     FileOutputStream fs = new FileOutputStream(new File(BIN_FILE_LOCATION));
     BufferedOutputStream bos = new BufferedOutputStream(fs);
     bos.write(out);
     bos.close();
 
     //Binary file with child documents
-    javabin = new JavaBinCodec();
     SolrDocument sdoc = generateSolrDocumentWithChildDocs();
-    os = new ByteArrayOutputStream();
-    javabin.marshal(sdoc, os);
     fs = new FileOutputStream(new File(BIN_FILE_LOCATION_CHILD_DOCS));
     bos = new BufferedOutputStream(fs);
-    bos.write(os.toByteArray());
+    bos.write(getBytes(sdoc));
     bos.close();
 
   }
@@ -553,12 +566,7 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {
       sdoc.put("some_boolean", ""+r.nextBoolean());
       sdoc.put("another_boolean", ""+r.nextBoolean());
 
-
-      JavaBinCodec javabin = new JavaBinCodec();
-      ByteArrayOutputStream os = new ByteArrayOutputStream();
-      javabin.marshal(sdoc, os);
-      os.toByteArray();
-      buffers[bufnum] = os.toByteArray();
+      buffers[bufnum] = getBytes(sdoc);
     }
 
     int ret = 0;