Skip to content

Commit 9a80fed

Browse files
committed
add simple map stream capability
1 parent ca7a746 commit 9a80fed

File tree

3 files changed

+168
-2
lines changed

3 files changed

+168
-2
lines changed

.idea/dictionaries/kimchy.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.common.io.stream;
21+
22+
import java.io.IOException;
23+
import java.util.ArrayList;
24+
import java.util.HashMap;
25+
import java.util.List;
26+
import java.util.Map;
27+
28+
/**
29+
* @author kimchy (shay.banon)
30+
*/
31+
public class Streamables {
32+
33+
public static Map<String, Object> readMap(StreamInput in) throws IOException {
34+
int size = in.readVInt();
35+
Map<String, Object> map = new HashMap<String, Object>(size);
36+
for (int i = 0; i < size; i++) {
37+
map.put(in.readUTF(), readMapValue(in));
38+
}
39+
return map;
40+
}
41+
42+
public static Object readMapValue(StreamInput in) throws IOException {
43+
byte type = in.readByte();
44+
if (type == -1) {
45+
return null;
46+
} else if (type == 0) {
47+
return in.readUTF();
48+
} else if (type == 1) {
49+
return in.readInt();
50+
} else if (type == 2) {
51+
return in.readLong();
52+
} else if (type == 3) {
53+
return in.readFloat();
54+
} else if (type == 4) {
55+
return in.readDouble();
56+
} else if (type == 5) {
57+
return in.readBoolean();
58+
} else if (type == 6) {
59+
int bytesSize = in.readVInt();
60+
byte[] value = new byte[bytesSize];
61+
in.readFully(value);
62+
return value;
63+
} else if (type == 7) {
64+
int size = in.readVInt();
65+
List list = new ArrayList(size);
66+
for (int i = 0; i < size; i++) {
67+
list.add(readMapValue(in));
68+
}
69+
return list;
70+
} else if (type == 8) {
71+
int size = in.readVInt();
72+
Object[] list = new Object[size];
73+
for (int i = 0; i < size; i++) {
74+
list[i] = readMapValue(in);
75+
}
76+
return list;
77+
} else if (type == 9) {
78+
int size = in.readVInt();
79+
Map map = new HashMap(size);
80+
for (int i = 0; i < size; i++) {
81+
map.put(in.readUTF(), readMapValue(in));
82+
}
83+
return map;
84+
} else {
85+
throw new IOException("Can't read unknown type [" + type + "]");
86+
}
87+
}
88+
89+
public static void writeMap(StreamOutput out, Map<String, Object> map) throws IOException {
90+
out.writeVInt(map.size());
91+
for (Map.Entry<String, Object> entry : map.entrySet()) {
92+
out.writeUTF(entry.getKey());
93+
writeMapValue(out, entry.getValue());
94+
}
95+
}
96+
97+
private static void writeMapValue(StreamOutput out, Object value) throws IOException {
98+
if (value == null) {
99+
out.writeByte((byte) -1);
100+
return;
101+
}
102+
Class type = value.getClass();
103+
if (type == String.class) {
104+
out.writeByte((byte) 0);
105+
out.writeUTF((String) value);
106+
} else if (type == Integer.class) {
107+
out.writeByte((byte) 1);
108+
out.writeInt((Integer) value);
109+
} else if (type == Long.class) {
110+
out.writeByte((byte) 2);
111+
out.writeLong((Long) value);
112+
} else if (type == Float.class) {
113+
out.writeByte((byte) 3);
114+
out.writeFloat((Float) value);
115+
} else if (type == Double.class) {
116+
out.writeByte((byte) 4);
117+
out.writeDouble((Double) value);
118+
} else if (type == Boolean.class) {
119+
out.writeByte((byte) 5);
120+
out.writeBoolean((Boolean) value);
121+
} else if (type == byte[].class) {
122+
out.writeByte((byte) 6);
123+
out.writeVInt(((byte[]) value).length);
124+
out.writeBytes(((byte[]) value));
125+
} else if (value instanceof List) {
126+
out.writeByte((byte) 7);
127+
List list = (List) value;
128+
out.writeVInt(list.size());
129+
for (Object o : list) {
130+
writeMapValue(out, o);
131+
}
132+
} else if (value instanceof Object[]) {
133+
out.writeByte((byte) 8);
134+
Object[] list = (Object[]) value;
135+
out.writeVInt(list.length);
136+
for (Object o : list) {
137+
writeMapValue(out, o);
138+
}
139+
} else if (value instanceof Map) {
140+
out.writeByte((byte) 9);
141+
Map<String, Object> map = (Map<String, Object>) value;
142+
out.writeVInt(map.size());
143+
for (Map.Entry<String, Object> entry : map.entrySet()) {
144+
out.writeUTF(entry.getKey());
145+
writeMapValue(out, entry.getValue());
146+
}
147+
} else {
148+
throw new IOException("Can't write type [" + type + "]");
149+
}
150+
}
151+
}

modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/Lucene.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,14 @@ public static Object readFieldValue(StreamInput in) throws IOException {
310310
return list;
311311
} else if (type == 8) {
312312
int size = in.readVInt();
313-
Map map = new HashMap();
313+
Object[] list = new Object[size];
314+
for (int i = 0; i < size; i++) {
315+
list[i] = readFieldValue(in);
316+
}
317+
return list;
318+
} else if (type == 9) {
319+
int size = in.readVInt();
320+
Map map = new HashMap(size);
314321
for (int i = 0; i < size; i++) {
315322
map.put(in.readUTF(), readFieldValue(in));
316323
}
@@ -355,8 +362,15 @@ public static void writeFieldValue(StreamOutput out, Object value) throws IOExce
355362
for (Object o : list) {
356363
writeFieldValue(out, o);
357364
}
358-
} else if (value instanceof Map) {
365+
} else if (value instanceof Object[]) {
359366
out.writeByte((byte) 8);
367+
Object[] list = (Object[]) value;
368+
out.writeVInt(list.length);
369+
for (Object o : list) {
370+
writeFieldValue(out, o);
371+
}
372+
} else if (value instanceof Map) {
373+
out.writeByte((byte) 9);
360374
Map<String, Object> map = (Map<String, Object>) value;
361375
out.writeVInt(map.size());
362376
for (Map.Entry<String, Object> entry : map.entrySet()) {

0 commit comments

Comments
 (0)