Skip to content

Commit

Permalink
add simple map stream capability
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Oct 23, 2010
1 parent ca7a746 commit 9a80fed
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 2 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/kimchy.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.common.io.stream;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author kimchy (shay.banon)
*/
public class Streamables {

public static Map<String, Object> readMap(StreamInput in) throws IOException {
int size = in.readVInt();
Map<String, Object> map = new HashMap<String, Object>(size);
for (int i = 0; i < size; i++) {
map.put(in.readUTF(), readMapValue(in));
}
return map;
}

public static Object readMapValue(StreamInput in) throws IOException {
byte type = in.readByte();
if (type == -1) {
return null;
} else if (type == 0) {
return in.readUTF();
} else if (type == 1) {
return in.readInt();
} else if (type == 2) {
return in.readLong();
} else if (type == 3) {
return in.readFloat();
} else if (type == 4) {
return in.readDouble();
} else if (type == 5) {
return in.readBoolean();
} else if (type == 6) {
int bytesSize = in.readVInt();
byte[] value = new byte[bytesSize];
in.readFully(value);
return value;
} else if (type == 7) {
int size = in.readVInt();
List list = new ArrayList(size);
for (int i = 0; i < size; i++) {
list.add(readMapValue(in));
}
return list;
} else if (type == 8) {
int size = in.readVInt();
Object[] list = new Object[size];
for (int i = 0; i < size; i++) {
list[i] = readMapValue(in);
}
return list;
} else if (type == 9) {
int size = in.readVInt();
Map map = new HashMap(size);
for (int i = 0; i < size; i++) {
map.put(in.readUTF(), readMapValue(in));
}
return map;
} else {
throw new IOException("Can't read unknown type [" + type + "]");
}
}

public static void writeMap(StreamOutput out, Map<String, Object> map) throws IOException {
out.writeVInt(map.size());
for (Map.Entry<String, Object> entry : map.entrySet()) {
out.writeUTF(entry.getKey());
writeMapValue(out, entry.getValue());
}
}

private static void writeMapValue(StreamOutput out, Object value) throws IOException {
if (value == null) {
out.writeByte((byte) -1);
return;
}
Class type = value.getClass();
if (type == String.class) {
out.writeByte((byte) 0);
out.writeUTF((String) value);
} else if (type == Integer.class) {
out.writeByte((byte) 1);
out.writeInt((Integer) value);
} else if (type == Long.class) {
out.writeByte((byte) 2);
out.writeLong((Long) value);
} else if (type == Float.class) {
out.writeByte((byte) 3);
out.writeFloat((Float) value);
} else if (type == Double.class) {
out.writeByte((byte) 4);
out.writeDouble((Double) value);
} else if (type == Boolean.class) {
out.writeByte((byte) 5);
out.writeBoolean((Boolean) value);
} else if (type == byte[].class) {
out.writeByte((byte) 6);
out.writeVInt(((byte[]) value).length);
out.writeBytes(((byte[]) value));
} else if (value instanceof List) {
out.writeByte((byte) 7);
List list = (List) value;
out.writeVInt(list.size());
for (Object o : list) {
writeMapValue(out, o);
}
} else if (value instanceof Object[]) {
out.writeByte((byte) 8);
Object[] list = (Object[]) value;
out.writeVInt(list.length);
for (Object o : list) {
writeMapValue(out, o);
}
} else if (value instanceof Map) {
out.writeByte((byte) 9);
Map<String, Object> map = (Map<String, Object>) value;
out.writeVInt(map.size());
for (Map.Entry<String, Object> entry : map.entrySet()) {
out.writeUTF(entry.getKey());
writeMapValue(out, entry.getValue());
}
} else {
throw new IOException("Can't write type [" + type + "]");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,14 @@ public static Object readFieldValue(StreamInput in) throws IOException {
return list;
} else if (type == 8) {
int size = in.readVInt();
Map map = new HashMap();
Object[] list = new Object[size];
for (int i = 0; i < size; i++) {
list[i] = readFieldValue(in);
}
return list;
} else if (type == 9) {
int size = in.readVInt();
Map map = new HashMap(size);
for (int i = 0; i < size; i++) {
map.put(in.readUTF(), readFieldValue(in));
}
Expand Down Expand Up @@ -355,8 +362,15 @@ public static void writeFieldValue(StreamOutput out, Object value) throws IOExce
for (Object o : list) {
writeFieldValue(out, o);
}
} else if (value instanceof Map) {
} else if (value instanceof Object[]) {
out.writeByte((byte) 8);
Object[] list = (Object[]) value;
out.writeVInt(list.length);
for (Object o : list) {
writeFieldValue(out, o);
}
} else if (value instanceof Map) {
out.writeByte((byte) 9);
Map<String, Object> map = (Map<String, Object>) value;
out.writeVInt(map.size());
for (Map.Entry<String, Object> entry : map.entrySet()) {
Expand Down

0 comments on commit 9a80fed

Please sign in to comment.