mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-09 06:25:07 +00:00
Transport: read/write support for list of strings
Add support for reading and writng string lists to existing StreamInput and StreamOutput class.
This commit is contained in:
parent
a536bd5f81
commit
acc42d5599
@ -335,6 +335,21 @@ public abstract class StreamInput extends InputStream {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read in a list of strings. List can be empty but not {@code null}.
|
||||||
|
*/
|
||||||
|
public List<String> readStringList() throws IOException {
|
||||||
|
int size = readVInt();
|
||||||
|
if (size == 0) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
List<String> ret = new ArrayList<>(size);
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
ret.add(readString());
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public Map<String, Object> readMap() throws IOException {
|
public Map<String, Object> readMap() throws IOException {
|
||||||
return (Map<String, Object>) readGenericValue();
|
return (Map<String, Object>) readGenericValue();
|
||||||
|
@ -283,6 +283,16 @@ public abstract class StreamOutput extends OutputStream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a list of strings. List can be empty but not {@code null}.
|
||||||
|
*/
|
||||||
|
public void writeStringList(List<String> stringList) throws IOException {
|
||||||
|
writeVInt(stringList.size());
|
||||||
|
for (String s : stringList) {
|
||||||
|
writeString(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a string array, for nullable string, writes it as 0 (empty string).
|
* Writes a string array, for nullable string, writes it as 0 (empty string).
|
||||||
*/
|
*/
|
||||||
|
@ -28,6 +28,8 @@ import org.elasticsearch.test.ElasticsearchTestCase;
|
|||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.closeTo;
|
import static org.hamcrest.Matchers.closeTo;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
||||||
@ -282,6 +284,7 @@ public class BytesStreamsTests extends ElasticsearchTestCase {
|
|||||||
out.writeGenericValue(doubleArray);
|
out.writeGenericValue(doubleArray);
|
||||||
out.writeString("hello");
|
out.writeString("hello");
|
||||||
out.writeString("goodbye");
|
out.writeString("goodbye");
|
||||||
|
out.writeStringList(Arrays.asList(new String[]{"Hello", "Again"}));
|
||||||
out.writeGenericValue(BytesRefs.toBytesRef("bytesref"));
|
out.writeGenericValue(BytesRefs.toBytesRef("bytesref"));
|
||||||
BytesStreamInput in = new BytesStreamInput(out.bytes().toBytes());
|
BytesStreamInput in = new BytesStreamInput(out.bytes().toBytes());
|
||||||
assertThat(in.readBoolean(), equalTo(false));
|
assertThat(in.readBoolean(), equalTo(false));
|
||||||
@ -299,6 +302,7 @@ public class BytesStreamsTests extends ElasticsearchTestCase {
|
|||||||
assertThat(in.readGenericValue(), equalTo((Object)doubleArray));
|
assertThat(in.readGenericValue(), equalTo((Object)doubleArray));
|
||||||
assertThat(in.readString(), equalTo("hello"));
|
assertThat(in.readString(), equalTo("hello"));
|
||||||
assertThat(in.readString(), equalTo("goodbye"));
|
assertThat(in.readString(), equalTo("goodbye"));
|
||||||
|
assertThat(in.readStringList(), equalTo(Arrays.asList(new String[]{"Hello", "Again"})));
|
||||||
assertThat(in.readGenericValue(), equalTo((Object)BytesRefs.toBytesRef("bytesref")));
|
assertThat(in.readGenericValue(), equalTo((Object)BytesRefs.toBytesRef("bytesref")));
|
||||||
in.close();
|
in.close();
|
||||||
out.close();
|
out.close();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user