Author: yonik
Date: Tue Jul 29 12:41:01 2008
New Revision: 680810

URL: http://svn.apache.org/viewvc?rev=680810&view=rev
Log:
SOLR-486: add efficient Iterator support for binary protocol

Modified:
    lucene/solr/trunk/src/java/org/apache/solr/common/util/NamedListCodec.java
    
lucene/solr/trunk/src/test/org/apache/solr/common/util/TestNamedListCodec.java

Modified: 
lucene/solr/trunk/src/java/org/apache/solr/common/util/NamedListCodec.java
URL: 
http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/common/util/NamedListCodec.java?rev=680810&r1=680809&r2=680810&view=diff
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/common/util/NamedListCodec.java 
(original)
+++ lucene/solr/trunk/src/java/org/apache/solr/common/util/NamedListCodec.java 
Tue Jul 29 12:41:01 2008
@@ -48,6 +48,9 @@
           SOLRDOC = 11,
           SOLRDOCLST = 12,
           BYTEARR = 13,
+          ITERATOR = 14,
+          /** this is a special tag signals an end. No value is associated 
with it*/
+          END = 15,
 
           // types that combine tag + length (or other info) in a single byte
           TAG_AND_LEN=(byte)(1 << 5),
@@ -113,7 +116,7 @@
     writeTag(nl instanceof SimpleOrderedMap ? ORDERED_MAP : NAMED_LST, 
nl.size());
     for (int i = 0; i < nl.size(); i++) {
       String name = nl.getName(i);
-      writeStr(name);
+      writeExternString(name);
       Object val = nl.getVal(i);
       writeVal(val);
     }
@@ -133,6 +136,7 @@
 
     writeVal(val.getClass().getName() + ':' + val.toString());
   }
+  private static final Object END_OBJ = new Object();
 
   byte tagByte;
   public Object readVal(FastInputStream dis) throws IOException {
@@ -167,6 +171,8 @@
       case SOLRDOC : return readSolrDocument(dis);
       case SOLRDOCLST : return readSolrDocumentList(dis);
       case BYTEARR : return readByteArray(dis);
+      case ITERATOR : return readIterator(dis);
+      case END : return END_OBJ;
     }
 
     throw new RuntimeException("Unknown type " + tagByte);
@@ -206,13 +212,16 @@
       }
       return true;
     }
+    if (val instanceof Map) {
+      writeMap((Map) val);
+      return true;
+    }
     if (val instanceof Iterator) {
       writeIterator((Iterator) val);
       return true;
     }
-    if (val instanceof Map) {
-      writeMap((Map) val);
-      return true;
+    if (val instanceof Iterable) {
+      writeIterator(((Iterable)val).iterator());
     }
     return false;
   }
@@ -318,9 +327,21 @@
   }
 
   public void writeIterator(Iterator iter) throws IOException {
+    writeTag(ITERATOR);
+    while (iter.hasNext()) {
+      writeVal(iter.next());
+    }
+    writeVal(END_OBJ);
+  }
+
+  public List readIterator(FastInputStream fis) throws IOException {
     ArrayList l = new ArrayList();
-    while (iter.hasNext()) l.add(iter.next());
-    writeArray(l);
+    while(true){
+      Object  o = readVal(fis);
+      if(o == END_OBJ) break;
+      l.add(o);
+    }
+    return l;
   }
 
   public void writeArray(List l) throws IOException {
@@ -462,6 +483,9 @@
     } else if (val instanceof byte[]) {
       writeByteArray((byte[])val, 0, ((byte[])val).length);
       return true;
+    } else if (val == END_OBJ) {
+      writeTag(END);
+      return true;
     }
     return false;
   }

Modified: 
lucene/solr/trunk/src/test/org/apache/solr/common/util/TestNamedListCodec.java
URL: 
http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/common/util/TestNamedListCodec.java?rev=680810&r1=680809&r2=680810&view=diff
==============================================================================
--- 
lucene/solr/trunk/src/test/org/apache/solr/common/util/TestNamedListCodec.java 
(original)
+++ 
lucene/solr/trunk/src/test/org/apache/solr/common/util/TestNamedListCodec.java 
Tue Jul 29 12:41:01 2008
@@ -87,6 +87,41 @@
     assertEquals(101, ((List)list.get(1).getFieldValue("f")).get(1));
   }
 
+  public void testIterator() throws Exception{
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    NamedList nl = new NamedList();
+    Float fval = new Float( 10.01f );
+    Boolean bval = Boolean.TRUE;
+    String sval = "12qwaszx";
+
+    // Set up a simple document
+    NamedList r = new NamedList();
+    List list =     new ArrayList();
+
+    SolrDocument doc = new SolrDocument();
+    doc.addField( "f", fval );
+    doc.addField( "b", bval );
+    doc.addField( "s", sval );
+    doc.addField( "f", 100 );
+    list.add(doc);
+
+    doc = new SolrDocument();
+    doc.addField( "f", fval );
+    doc.addField( "b", bval );
+    doc.addField( "s", sval );
+    doc.addField( "f", 101 );
+    list.add(doc);
+
+    nl.add("zzz",list.iterator());
+
+    new NamedListCodec(null).marshal(nl,baos);
+    byte[] arr = baos.toByteArray();
+    nl = new NamedListCodec().unmarshal(new ByteArrayInputStream(arr));
+
+    List l = (List) nl.get("zzz");
+    assertEquals(list.size(), l.size());
+  }
+
 
 
   


Reply via email to