use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class ThreadPoolStatsTests method testThreadPoolStatsToXContent.
public void testThreadPoolStatsToXContent() throws IOException {
try (BytesStreamOutput os = new BytesStreamOutput()) {
List<ThreadPoolStats.Stats> stats = new ArrayList<>();
stats.add(new ThreadPoolStats.Stats(ThreadPool.Names.SEARCH, -1, 0, 0, 0, 0, 0L));
stats.add(new ThreadPoolStats.Stats(ThreadPool.Names.WARMER, -1, 0, 0, 0, 0, 0L));
stats.add(new ThreadPoolStats.Stats(ThreadPool.Names.GENERIC, -1, 0, 0, 0, 0, 0L));
stats.add(new ThreadPoolStats.Stats(ThreadPool.Names.FORCE_MERGE, -1, 0, 0, 0, 0, 0L));
stats.add(new ThreadPoolStats.Stats(ThreadPool.Names.SAME, -1, 0, 0, 0, 0, 0L));
ThreadPoolStats threadPoolStats = new ThreadPoolStats(stats);
try (XContentBuilder builder = new XContentBuilder(XContentType.JSON.xContent(), os)) {
builder.startObject();
threadPoolStats.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
}
try (XContentParser parser = createParser(JsonXContent.jsonXContent, os.bytes())) {
XContentParser.Token token = parser.currentToken();
assertNull(token);
token = parser.nextToken();
assertThat(token, equalTo(XContentParser.Token.START_OBJECT));
token = parser.nextToken();
assertThat(token, equalTo(XContentParser.Token.FIELD_NAME));
assertThat(parser.currentName(), equalTo(ThreadPoolStats.Fields.THREAD_POOL));
token = parser.nextToken();
assertThat(token, equalTo(XContentParser.Token.START_OBJECT));
token = parser.nextToken();
assertThat(token, equalTo(XContentParser.Token.FIELD_NAME));
List<String> names = new ArrayList<>();
while (token == XContentParser.Token.FIELD_NAME) {
names.add(parser.currentName());
token = parser.nextToken();
assertThat(token, equalTo(XContentParser.Token.START_OBJECT));
parser.skipChildren();
token = parser.nextToken();
}
assertThat(names, contains(ThreadPool.Names.FORCE_MERGE, ThreadPool.Names.GENERIC, ThreadPool.Names.SAME, ThreadPool.Names.SEARCH, ThreadPool.Names.WARMER));
}
}
}
use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class RoundTripTests method roundTrip.
private void roundTrip(Version version, Streamable example, Streamable empty) throws IOException {
BytesStreamOutput out = new BytesStreamOutput();
out.setVersion(version);
example.writeTo(out);
StreamInput in = out.bytes().streamInput();
in.setVersion(version);
empty.readFrom(in);
}
use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class FieldStatsTests method assertSerialization.
private void assertSerialization(FieldStats stats, Version version) throws IOException {
BytesStreamOutput output = new BytesStreamOutput();
output.setVersion(version);
stats.writeTo(output);
output.flush();
StreamInput input = output.bytes().streamInput();
input.setVersion(version);
FieldStats deserializedStats = FieldStats.readFrom(input);
assertEquals(stats, deserializedStats);
assertEquals(stats.hashCode(), deserializedStats.hashCode());
}
use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class BinaryFieldMapperTests method testStoredValue.
public void testStoredValue() throws IOException {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "binary").field("store", true).endObject().endObject().endObject().endObject().string();
DocumentMapper mapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
// case 1: a simple binary value
final byte[] binaryValue1 = new byte[100];
binaryValue1[56] = 1;
// case 2: a value that looks compressed: this used to fail in 1.x
BytesStreamOutput out = new BytesStreamOutput();
try (StreamOutput compressed = CompressorFactory.COMPRESSOR.streamOutput(out)) {
new BytesArray(binaryValue1).writeTo(compressed);
}
final byte[] binaryValue2 = BytesReference.toBytes(out.bytes());
assertTrue(CompressorFactory.isCompressed(new BytesArray(binaryValue2)));
for (byte[] value : Arrays.asList(binaryValue1, binaryValue2)) {
ParsedDocument doc = mapper.parse("test", "type", "id", XContentFactory.jsonBuilder().startObject().field("field", value).endObject().bytes());
BytesRef indexedValue = doc.rootDoc().getBinaryValue("field");
assertEquals(new BytesRef(value), indexedValue);
FieldMapper fieldMapper = mapper.mappers().smartNameFieldMapper("field");
Object originalValue = fieldMapper.fieldType().valueForDisplay(indexedValue);
assertEquals(new BytesArray(value), originalValue);
}
}
use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class InnerHitBuilderTests method testSerializationOrder.
/**
* Test that if we serialize and deserialize an object, further
* serialization leads to identical bytes representation.
*
* This is necessary to ensure because we use the serialized BytesReference
* of this builder as part of the cacheKey in
* {@link ShardSearchLocalRequest} (via
* {@link SearchSourceBuilder#collapse(org.elasticsearch.search.collapse.CollapseBuilder)})
*/
public void testSerializationOrder() throws Exception {
for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) {
InnerHitBuilder original = randomInnerHits();
InnerHitBuilder deserialized = serializedCopy(original);
assertEquals(deserialized, original);
assertEquals(deserialized.hashCode(), original.hashCode());
assertNotSame(deserialized, original);
BytesStreamOutput out1 = new BytesStreamOutput();
BytesStreamOutput out2 = new BytesStreamOutput();
original.writeTo(out1);
deserialized.writeTo(out2);
assertEquals(out1.bytes(), out2.bytes());
}
}
Aggregations