mirror of
https://github.com/apache/lucene.git
synced 2025-02-28 13:29:26 +00:00
SOLR-486: add efficient Iterator support for binary protocol
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@680810 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2d13d9eab4
commit
b9fcb32589
@ -48,6 +48,9 @@ public class NamedListCodec {
|
|||||||
SOLRDOC = 11,
|
SOLRDOC = 11,
|
||||||
SOLRDOCLST = 12,
|
SOLRDOCLST = 12,
|
||||||
BYTEARR = 13,
|
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
|
// types that combine tag + length (or other info) in a single byte
|
||||||
TAG_AND_LEN=(byte)(1 << 5),
|
TAG_AND_LEN=(byte)(1 << 5),
|
||||||
@ -113,7 +116,7 @@ public class NamedListCodec {
|
|||||||
writeTag(nl instanceof SimpleOrderedMap ? ORDERED_MAP : NAMED_LST, nl.size());
|
writeTag(nl instanceof SimpleOrderedMap ? ORDERED_MAP : NAMED_LST, nl.size());
|
||||||
for (int i = 0; i < nl.size(); i++) {
|
for (int i = 0; i < nl.size(); i++) {
|
||||||
String name = nl.getName(i);
|
String name = nl.getName(i);
|
||||||
writeStr(name);
|
writeExternString(name);
|
||||||
Object val = nl.getVal(i);
|
Object val = nl.getVal(i);
|
||||||
writeVal(val);
|
writeVal(val);
|
||||||
}
|
}
|
||||||
@ -133,6 +136,7 @@ public class NamedListCodec {
|
|||||||
|
|
||||||
writeVal(val.getClass().getName() + ':' + val.toString());
|
writeVal(val.getClass().getName() + ':' + val.toString());
|
||||||
}
|
}
|
||||||
|
private static final Object END_OBJ = new Object();
|
||||||
|
|
||||||
byte tagByte;
|
byte tagByte;
|
||||||
public Object readVal(FastInputStream dis) throws IOException {
|
public Object readVal(FastInputStream dis) throws IOException {
|
||||||
@ -167,6 +171,8 @@ public class NamedListCodec {
|
|||||||
case SOLRDOC : return readSolrDocument(dis);
|
case SOLRDOC : return readSolrDocument(dis);
|
||||||
case SOLRDOCLST : return readSolrDocumentList(dis);
|
case SOLRDOCLST : return readSolrDocumentList(dis);
|
||||||
case BYTEARR : return readByteArray(dis);
|
case BYTEARR : return readByteArray(dis);
|
||||||
|
case ITERATOR : return readIterator(dis);
|
||||||
|
case END : return END_OBJ;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new RuntimeException("Unknown type " + tagByte);
|
throw new RuntimeException("Unknown type " + tagByte);
|
||||||
@ -206,13 +212,16 @@ public class NamedListCodec {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if (val instanceof Map) {
|
||||||
|
writeMap((Map) val);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
if (val instanceof Iterator) {
|
if (val instanceof Iterator) {
|
||||||
writeIterator((Iterator) val);
|
writeIterator((Iterator) val);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (val instanceof Map) {
|
if (val instanceof Iterable) {
|
||||||
writeMap((Map) val);
|
writeIterator(((Iterable)val).iterator());
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -318,9 +327,21 @@ public class NamedListCodec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void writeIterator(Iterator iter) throws IOException {
|
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();
|
ArrayList l = new ArrayList();
|
||||||
while (iter.hasNext()) l.add(iter.next());
|
while(true){
|
||||||
writeArray(l);
|
Object o = readVal(fis);
|
||||||
|
if(o == END_OBJ) break;
|
||||||
|
l.add(o);
|
||||||
|
}
|
||||||
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeArray(List l) throws IOException {
|
public void writeArray(List l) throws IOException {
|
||||||
@ -462,6 +483,9 @@ public class NamedListCodec {
|
|||||||
} else if (val instanceof byte[]) {
|
} else if (val instanceof byte[]) {
|
||||||
writeByteArray((byte[])val, 0, ((byte[])val).length);
|
writeByteArray((byte[])val, 0, ((byte[])val).length);
|
||||||
return true;
|
return true;
|
||||||
|
} else if (val == END_OBJ) {
|
||||||
|
writeTag(END);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -87,6 +87,41 @@ public class TestNamedListCodec extends TestCase {
|
|||||||
assertEquals(101, ((List)list.get(1).getFieldValue("f")).get(1));
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user