本文整理汇总了Java中org.elasticsearch.common.bytes.BytesReference.toBytes方法的典型用法代码示例。如果您正苦于以下问题:Java BytesReference.toBytes方法的具体用法?Java BytesReference.toBytes怎么用?Java BytesReference.toBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.elasticsearch.common.bytes.BytesReference
的用法示例。
在下文中一共展示了BytesReference.toBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: CompressedXContent
import org.elasticsearch.common.bytes.BytesReference; //导入方法依赖的package包/类
/**
* Create a {@link CompressedXContent} out of a serialized {@link ToXContent}
* that may already be compressed.
*/
public CompressedXContent(BytesReference data) throws IOException {
Compressor compressor = CompressorFactory.compressor(data);
if (compressor != null) {
// already compressed...
this.bytes = BytesReference.toBytes(data);
this.crc32 = crc32(new BytesArray(uncompressed()));
} else {
BytesStreamOutput out = new BytesStreamOutput();
try (OutputStream compressedOutput = CompressorFactory.COMPRESSOR.streamOutput(out)) {
data.writeTo(compressedOutput);
}
this.bytes = BytesReference.toBytes(out.bytes());
this.crc32 = crc32(data);
}
assertConsistent();
}
示例2: testNamedWriteable
import org.elasticsearch.common.bytes.BytesReference; //导入方法依赖的package包/类
public void testNamedWriteable() throws IOException {
try (BytesStreamOutput out = new BytesStreamOutput()) {
NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Collections.singletonList(
new NamedWriteableRegistry.Entry(BaseNamedWriteable.class, TestNamedWriteable.NAME, TestNamedWriteable::new)));
TestNamedWriteable namedWriteableIn = new TestNamedWriteable(randomAsciiOfLengthBetween(1, 10),
randomAsciiOfLengthBetween(1, 10));
out.writeNamedWriteable(namedWriteableIn);
byte[] bytes = BytesReference.toBytes(out.bytes());
try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(bytes), namedWriteableRegistry)) {
assertEquals(in.available(), bytes.length);
BaseNamedWriteable namedWriteableOut = in.readNamedWriteable(BaseNamedWriteable.class);
assertEquals(namedWriteableIn, namedWriteableOut);
assertEquals(0, in.available());
}
}
}
示例3: testWriteableReaderReturnsWrongName
import org.elasticsearch.common.bytes.BytesReference; //导入方法依赖的package包/类
public void testWriteableReaderReturnsWrongName() throws IOException {
try (BytesStreamOutput out = new BytesStreamOutput()) {
NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(
Collections.singletonList(new NamedWriteableRegistry.Entry(BaseNamedWriteable.class, TestNamedWriteable.NAME,
(StreamInput in) -> new TestNamedWriteable(in) {
@Override
public String getWriteableName() {
return "intentionally-broken";
}
})));
TestNamedWriteable namedWriteableIn = new TestNamedWriteable(randomAsciiOfLengthBetween(1, 10),
randomAsciiOfLengthBetween(1, 10));
out.writeNamedWriteable(namedWriteableIn);
byte[] bytes = BytesReference.toBytes(out.bytes());
try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(bytes), namedWriteableRegistry)) {
assertEquals(in.available(), bytes.length);
AssertionError e = expectThrows(AssertionError.class, () -> in.readNamedWriteable(BaseNamedWriteable.class));
assertThat(e.getMessage(),
endsWith(" claims to have a different name [intentionally-broken] than it was read from [test-named-writeable]."));
}
}
}
示例4: writeOperation
import org.elasticsearch.common.bytes.BytesReference; //导入方法依赖的package包/类
/**
*
* @param operation
* @return
* @throws IOException
*/
public Tuple<Future<DLSN>, Tuple<BytesReference, Long>> writeOperation(Translog.Operation operation, AtomicLong txid) throws IOException {
BytesStreamOutput out = new BytesStreamOutput();
try (ReleasableLock lock = writeLock.acquire()) {
Future<DLSN> writeResult = null;
out.writeByte(operation.opType().id());
operation.writeTo(out);
BytesReference bytes = out.bytes();
LogRecord logRecord = new LogRecord(txid.incrementAndGet(), bytes.toBytes());
writeResult = logWriter.write(logRecord);
sizeInBytes += (20 + logRecord.getPayload().length);
++ numOperations;
return new Tuple<Future<DLSN>, Tuple<BytesReference, Long>>(writeResult, new Tuple<BytesReference, Long>(bytes, txid.get()));
} catch (TransactionIdOutOfOrderException e) {
throw e;
} finally {
out.close();
}
}
示例5: CompressedXContent
import org.elasticsearch.common.bytes.BytesReference; //导入方法依赖的package包/类
/**
* Create a {@link CompressedXContent} out of a serialized {@link ToXContent}
* that may already be compressed.
*/
public CompressedXContent(BytesReference data) throws IOException {
Compressor compressor = CompressorFactory.compressor(data);
if (compressor != null) {
// already compressed...
this.bytes = data.toBytes();
this.crc32 = crc32(new BytesArray(uncompressed()));
} else {
BytesStreamOutput out = new BytesStreamOutput();
try (OutputStream compressedOutput = CompressorFactory.defaultCompressor().streamOutput(out)) {
data.writeTo(compressedOutput);
}
this.bytes = out.bytes().toBytes();
this.crc32 = crc32(data);
}
assertConsistent();
}
示例6: parse
import org.elasticsearch.common.bytes.BytesReference; //导入方法依赖的package包/类
@Override
public Mapper parse(ParseContext context) throws IOException {
QueryShardContext queryShardContext = this.queryShardContext.get();
if (context.doc().getField(queryBuilderField.name()) != null) {
// If a percolator query has been defined in an array object then multiple percolator queries
// could be provided. In order to prevent this we fail if we try to parse more than one query
// for the current document.
throw new IllegalArgumentException("a document can only contain one percolator query");
}
XContentParser parser = context.parser();
QueryBuilder queryBuilder = parseQueryBuilder(
queryShardContext.newParseContext(parser), parser.getTokenLocation()
);
verifyQuery(queryBuilder);
// Fetching of terms, shapes and indexed scripts happen during this rewrite:
queryBuilder = queryBuilder.rewrite(queryShardContext);
try (XContentBuilder builder = XContentFactory.contentBuilder(QUERY_BUILDER_CONTENT_TYPE)) {
queryBuilder.toXContent(builder, new MapParams(Collections.emptyMap()));
builder.flush();
byte[] queryBuilderAsBytes = BytesReference.toBytes(builder.bytes());
context.doc().add(new Field(queryBuilderField.name(), queryBuilderAsBytes, queryBuilderField.fieldType()));
}
Query query = toQuery(queryShardContext, mapUnmappedFieldAsString, queryBuilder);
processQuery(query, context);
return null;
}
示例7: copyToBytesFromClasspath
import org.elasticsearch.common.bytes.BytesReference; //导入方法依赖的package包/类
public static byte[] copyToBytesFromClasspath(String path) throws IOException {
try (InputStream is = Streams.class.getResourceAsStream(path)) {
if (is == null) {
throw new FileNotFoundException("Resource [" + path + "] not found in classpath");
}
try (BytesStreamOutput out = new BytesStreamOutput()) {
Streams.copy(is, out);
return BytesReference.toBytes(out.bytes());
}
}
}
示例8: source
import org.elasticsearch.common.bytes.BytesReference; //导入方法依赖的package包/类
/**
* The source of the document if exists.
*/
public byte[] source() {
if (source == null) {
return null;
}
if (sourceAsBytes != null) {
return sourceAsBytes;
}
this.sourceAsBytes = BytesReference.toBytes(sourceRef());
return this.sourceAsBytes;
}
示例9: uncompressed
import org.elasticsearch.common.bytes.BytesReference; //导入方法依赖的package包/类
/** Return the uncompressed bytes. */
public byte[] uncompressed() {
try {
return BytesReference.toBytes(CompressorFactory.uncompress(new BytesArray(bytes)));
} catch (IOException e) {
throw new IllegalStateException("Cannot decompress compressed string", e);
}
}
示例10: doCreateTestQueryBuilder
import org.elasticsearch.common.bytes.BytesReference; //导入方法依赖的package包/类
@Override
protected WrapperQueryBuilder doCreateTestQueryBuilder() {
QueryBuilder wrappedQuery = RandomQueryBuilder.createQuery(random());
switch (randomInt(2)) {
case 0:
return new WrapperQueryBuilder(wrappedQuery.toString());
case 1:
return new WrapperQueryBuilder(BytesReference.toBytes(((ToXContentToBytes)wrappedQuery).buildAsBytes()));
case 2:
return new WrapperQueryBuilder(((ToXContentToBytes)wrappedQuery).buildAsBytes());
default:
throw new UnsupportedOperationException();
}
}
示例11: testToFromXContent
import org.elasticsearch.common.bytes.BytesReference; //导入方法依赖的package包/类
public void testToFromXContent() throws IOException {
final int iters = scaledRandomIntBetween(1, 10);
for (int iter = 0; iter < iters; iter++) {
final BytesRef hash = new BytesRef(scaledRandomIntBetween(0, 1024 * 1024));
hash.length = hash.bytes.length;
for (int i = 0; i < hash.length; i++) {
hash.bytes[i] = randomByte();
}
StoreFileMetaData meta = new StoreFileMetaData("foobar", Math.abs(randomLong()), randomAsciiOfLengthBetween(1, 10), Version.LATEST, hash);
ByteSizeValue size = new ByteSizeValue(Math.abs(randomLong()));
BlobStoreIndexShardSnapshot.FileInfo info = new BlobStoreIndexShardSnapshot.FileInfo("_foobar", meta, size);
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
BlobStoreIndexShardSnapshot.FileInfo.toXContent(info, builder, ToXContent.EMPTY_PARAMS);
byte[] xcontent = BytesReference.toBytes(shuffleXContent(builder).bytes());
final BlobStoreIndexShardSnapshot.FileInfo parsedInfo;
try (XContentParser parser = createParser(JsonXContent.jsonXContent, xcontent)) {
parser.nextToken();
parsedInfo = BlobStoreIndexShardSnapshot.FileInfo.fromXContent(parser);
}
assertThat(info.name(), equalTo(parsedInfo.name()));
assertThat(info.physicalName(), equalTo(parsedInfo.physicalName()));
assertThat(info.length(), equalTo(parsedInfo.length()));
assertThat(info.checksum(), equalTo(parsedInfo.checksum()));
assertThat(info.partSize(), equalTo(parsedInfo.partSize()));
assertThat(parsedInfo.metadata().hash().length, equalTo(hash.length));
assertThat(parsedInfo.metadata().hash(), equalTo(hash));
assertThat(parsedInfo.metadata().writtenBy(), equalTo(Version.LATEST));
assertThat(parsedInfo.isSame(info.metadata()), is(true));
}
}
示例12: testStoredValue
import org.elasticsearch.common.bytes.BytesReference; //导入方法依赖的package包/类
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);
}
}
示例13: testNamedWriteableReaderReturnsNull
import org.elasticsearch.common.bytes.BytesReference; //导入方法依赖的package包/类
public void testNamedWriteableReaderReturnsNull() throws IOException {
try (BytesStreamOutput out = new BytesStreamOutput()) {
NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Collections.singletonList(
new NamedWriteableRegistry.Entry(BaseNamedWriteable.class, TestNamedWriteable.NAME, (StreamInput in) -> null)));
TestNamedWriteable namedWriteableIn = new TestNamedWriteable(randomAsciiOfLengthBetween(1, 10),
randomAsciiOfLengthBetween(1, 10));
out.writeNamedWriteable(namedWriteableIn);
byte[] bytes = BytesReference.toBytes(out.bytes());
try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(bytes), namedWriteableRegistry)) {
assertEquals(in.available(), bytes.length);
IOException e = expectThrows(IOException.class, () -> in.readNamedWriteable(BaseNamedWriteable.class));
assertThat(e.getMessage(), endsWith("] returned null which is not allowed and probably means it screwed up the stream."));
}
}
}
示例14: toBytes
import org.elasticsearch.common.bytes.BytesReference; //导入方法依赖的package包/类
public static byte[] toBytes(ClusterState state) throws IOException {
BytesStreamOutput os = new BytesStreamOutput();
state.writeTo(os);
return BytesReference.toBytes(os.bytes());
}
示例15: testSimpleStreams
import org.elasticsearch.common.bytes.BytesReference; //导入方法依赖的package包/类
public void testSimpleStreams() throws Exception {
assumeTrue("requires a 64-bit JRE ... ?!", Constants.JRE_IS_64BIT);
BytesStreamOutput out = new BytesStreamOutput();
out.writeBoolean(false);
out.writeByte((byte) 1);
out.writeShort((short) -1);
out.writeInt(-1);
out.writeVInt(2);
out.writeLong(-3);
out.writeVLong(4);
out.writeOptionalLong(11234234L);
out.writeFloat(1.1f);
out.writeDouble(2.2);
int[] intArray = {1, 2, 3};
out.writeGenericValue(intArray);
int[] vIntArray = {4, 5, 6};
out.writeVIntArray(vIntArray);
long[] longArray = {1, 2, 3};
out.writeGenericValue(longArray);
long[] vLongArray = {4, 5, 6};
out.writeVLongArray(vLongArray);
float[] floatArray = {1.1f, 2.2f, 3.3f};
out.writeGenericValue(floatArray);
double[] doubleArray = {1.1, 2.2, 3.3};
out.writeGenericValue(doubleArray);
out.writeString("hello");
out.writeString("goodbye");
out.writeGenericValue(BytesRefs.toBytesRef("bytesref"));
out.writeStringArray(new String[] {"a", "b", "cat"});
out.writeBytesReference(new BytesArray("test"));
out.writeOptionalBytesReference(new BytesArray("test"));
out.writeOptionalDouble(null);
out.writeOptionalDouble(1.2);
out.writeTimeZone(DateTimeZone.forID("CET"));
out.writeOptionalTimeZone(DateTimeZone.getDefault());
out.writeOptionalTimeZone(null);
final byte[] bytes = BytesReference.toBytes(out.bytes());
StreamInput in = StreamInput.wrap(BytesReference.toBytes(out.bytes()));
assertEquals(in.available(), bytes.length);
assertThat(in.readBoolean(), equalTo(false));
assertThat(in.readByte(), equalTo((byte)1));
assertThat(in.readShort(), equalTo((short)-1));
assertThat(in.readInt(), equalTo(-1));
assertThat(in.readVInt(), equalTo(2));
assertThat(in.readLong(), equalTo(-3L));
assertThat(in.readVLong(), equalTo(4L));
assertThat(in.readOptionalLong(), equalTo(11234234L));
assertThat((double)in.readFloat(), closeTo(1.1, 0.0001));
assertThat(in.readDouble(), closeTo(2.2, 0.0001));
assertThat(in.readGenericValue(), equalTo((Object) intArray));
assertThat(in.readVIntArray(), equalTo(vIntArray));
assertThat(in.readGenericValue(), equalTo((Object)longArray));
assertThat(in.readVLongArray(), equalTo(vLongArray));
assertThat(in.readGenericValue(), equalTo((Object)floatArray));
assertThat(in.readGenericValue(), equalTo((Object)doubleArray));
assertThat(in.readString(), equalTo("hello"));
assertThat(in.readString(), equalTo("goodbye"));
assertThat(in.readGenericValue(), equalTo((Object)BytesRefs.toBytesRef("bytesref")));
assertThat(in.readStringArray(), equalTo(new String[] {"a", "b", "cat"}));
assertThat(in.readBytesReference(), equalTo(new BytesArray("test")));
assertThat(in.readOptionalBytesReference(), equalTo(new BytesArray("test")));
assertNull(in.readOptionalDouble());
assertThat(in.readOptionalDouble(), closeTo(1.2, 0.0001));
assertEquals(DateTimeZone.forID("CET"), in.readTimeZone());
assertEquals(DateTimeZone.getDefault(), in.readOptionalTimeZone());
assertNull(in.readOptionalTimeZone());
assertEquals(0, in.available());
in.close();
out.close();
}