Java Code Examples for org.elasticsearch.common.bytes.BytesReference#toBytes()

The following examples show how to use org.elasticsearch.common.bytes.BytesReference#toBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DistributedTranslog.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @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();
    }
}
 
Example 2
Source File: CompressedXContent.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 3
Source File: ChecksumBlobStoreFormat.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Reads blob with specified name without resolving the blobName using using {@link #blobName} method.
 *
 * @param blobContainer blob container
 * @param blobName blob name
 */
public T readBlob(BlobContainer blobContainer, String blobName) throws IOException {
    final BytesReference bytes = Streams.readFully(blobContainer.readBlob(blobName));
    final String resourceDesc = "ChecksumBlobStoreFormat.readBlob(blob=\"" + blobName + "\")";
    try (ByteArrayIndexInput indexInput =
             new ByteArrayIndexInput(resourceDesc, BytesReference.toBytes(bytes))) {
        CodecUtil.checksumEntireFile(indexInput);
        CodecUtil.checkHeader(indexInput, codec, VERSION, VERSION);
        long filePointer = indexInput.getFilePointer();
        long contentSize = indexInput.length() - CodecUtil.footerLength() - filePointer;
        try (XContentParser parser = XContentHelper.createParser(namedXContentRegistry, LoggingDeprecationHandler.INSTANCE,
                                                                 bytes.slice((int) filePointer, (int) contentSize), XContentType.SMILE)) {
            return reader.apply(parser);
        }
    } catch (CorruptIndexException | IndexFormatTooOldException | IndexFormatTooNewException ex) {
        // we trick this into a dedicated exception with the original stacktrace
        throw new CorruptStateException(ex);
    }
}
 
Example 4
Source File: CompressedXContent.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Create a {@link CompressedXContent} out of a {@link ToXContent} instance.
 */
public CompressedXContent(ToXContent xcontent, XContentType type, ToXContent.Params params) throws IOException {
    BytesStreamOutput bStream = new BytesStreamOutput();
    OutputStream compressedStream = CompressorFactory.COMPRESSOR.streamOutput(bStream);
    CRC32 crc32 = new CRC32();
    try (OutputStream checkedStream = new CheckedOutputStream(compressedStream, crc32)) {
        try (XContentBuilder builder = XContentFactory.contentBuilder(type, checkedStream)) {
            builder.startObject();
            xcontent.toXContent(builder, params);
            builder.endObject();
        }
    }
    this.bytes = BytesReference.toBytes(bStream.bytes());
    this.crc32 = (int) crc32.getValue();
    assertConsistent();
}
 
Example 5
Source File: CompressedXContent.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 6
Source File: JsonType.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
protected byte[] encodeAsUTF8Text(@Nonnull Object value) {
    try {
        XContentBuilder builder = JsonXContent.contentBuilder();
        if (value instanceof List) {
            List values = ((List) value);
            builder.startArray();
            for (Object o : values) {
                builder.value(o);
            }
            builder.endArray();
        } else {
            builder.map((Map) value);
        }
        builder.close();
        return BytesReference.toBytes(BytesReference.bytes(builder));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 7
Source File: PartitionName.java    From crate with Apache License 2.0 6 votes vote down vote up
@Nullable
public static String encodeIdent(Collection<? extends String> values) {
    if (values.size() == 0) {
        return null;
    }

    BytesStreamOutput streamOutput = new BytesStreamOutput(estimateSize(values));
    try {
        streamOutput.writeVInt(values.size());
        for (String value : values) {
            writeValueTo(streamOutput, value);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    byte[] bytes = BytesReference.toBytes(streamOutput.bytes());
    String identBase32 = BASE32.encodeAsString(bytes).toLowerCase(Locale.ROOT);
    // decode doesn't need padding, remove it
    int idx = identBase32.indexOf('=');
    if (idx > -1) {
        return identBase32.substring(0, idx);
    }
    return identBase32;
}
 
Example 8
Source File: CompressedXContent.java    From crate with Apache License 2.0 5 votes vote down vote up
/** 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);
    }
}
 
Example 9
Source File: ClusterState.java    From crate with Apache License 2.0 4 votes vote down vote up
public static byte[] toBytes(ClusterState state) throws IOException {
    BytesStreamOutput os = new BytesStreamOutput();
    state.writeTo(os);
    return BytesReference.toBytes(os.bytes());
}