/** Decompress the provided {@link BytesReference}. */ public static BytesReference uncompress(BytesReference bytes) throws IOException { Compressor compressor = compressor(bytes); if (compressor == null) { throw new NotCompressedException(); } return uncompress(bytes, compressor); }
@Override public String toString() { return string(); } }
private CompressedXContent(byte[] compressed, int crc32) { this.bytes = compressed; this.crc32 = crc32; assertConsistent(); }
/** * 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(); }
private void assertConsistent() { assert CompressorFactory.compressor(new BytesArray(bytes)) != null; assert this.crc32 == crc32(new BytesArray(uncompressed())); }
@Nullable public static Compressor compressor(BytesReference bytes) { if (COMPRESSOR.isCompressed(bytes)) { // bytes should be either detected as compressed or as xcontent, // if we have bytes that can be either detected as compressed or // as a xcontent, we have a problem assert XContentHelper.xContentType(bytes) == null; return COMPRESSOR; } XContentType contentType = XContentHelper.xContentType(bytes); if (contentType == null) { if (isAncient(bytes)) { throw new IllegalStateException("unsupported compression: index was created before v2.0.0.beta1 and wasn't upgraded?"); } throw new NotXContentException("Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"); } return null; }
/** * Uncompress the provided data, data can be detected as compressed using {@link #isCompressed(BytesReference)}. * @throws NullPointerException a NullPointerException will be thrown when bytes is null */ public static BytesReference uncompressIfNeeded(BytesReference bytes) throws IOException { Compressor compressor = compressor(Objects.requireNonNull(bytes, "the BytesReference must not be null")); BytesReference uncompressed; if (compressor != null) { uncompressed = uncompress(bytes, compressor); } else { uncompressed = bytes; } return uncompressed; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CompressedXContent that = (CompressedXContent) o; if (Arrays.equals(compressed(), that.compressed())) { return true; } if (crc32 != that.crc32) { return false; } return Arrays.equals(uncompressed(), that.uncompressed()); }
JsonNode rootNode; try { rootNode = objecMapper.readTree(mappingMetaData.source().uncompressed());
CompressibleBytesOutputStream(BytesStream bytesStreamOutput, boolean shouldCompress) throws IOException { this.bytesStreamOutput = bytesStreamOutput; this.shouldCompress = shouldCompress; if (shouldCompress) { this.stream = CompressorFactory.COMPRESSOR.streamOutput(Streams.flushOnCloseStream(bytesStreamOutput)); } else { this.stream = bytesStreamOutput; } }
public Builder putMapping(String mappingType, String mappingSource) throws IOException { mappings.put(mappingType, new CompressedXContent(mappingSource)); return this; }
public static boolean isCompressed(BytesReference bytes) { return compressor(bytes) != null; }
/** * Returns bytes reference, also uncompress the source if needed. */ public BytesReference getSourceRef() { if (this.source == null) { return null; } try { this.source = CompressorFactory.uncompressIfNeeded(this.source); return this.source; } catch (IOException e) { throw new ElasticsearchParseException("failed to decompress source", e); } }
@Override public int hashCode() { int result = alias != null ? alias.hashCode() : 0; result = 31 * result + (filter != null ? filter.hashCode() : 0); result = 31 * result + (indexRouting != null ? indexRouting.hashCode() : 0); result = 31 * result + (searchRouting != null ? searchRouting.hashCode() : 0); result = 31 * result + (writeIndex != null ? writeIndex.hashCode() : 0); return result; }
@Test @ExcludeOnTravis public void testMapping() throws Exception { configurationSource.save("foo", "bar"); refresh(); final GetMappingsResponse mappings = client.admin().indices().prepareGetMappings("stagemonitor-configuration").setTypes("configuration").get(); assertEquals(1, mappings.getMappings().size()); assertEquals("{\"configuration\":{" + "\"_all\":{\"enabled\":false}," + "\"properties\":{\"configuration\":{\"properties\":{" + "\"key\":{\"type\":\"keyword\"}," + "\"value\":{\"type\":\"keyword\"}}}}" + "}" + "}", mappings.getMappings().get("stagemonitor-configuration").get("configuration").source().toString()); } }
public String string() { return new BytesRef(uncompressed()).utf8ToString(); }
protected BytesReference write(T obj) throws IOException { try (BytesStreamOutput bytesStreamOutput = new BytesStreamOutput()) { if (compress) { try (StreamOutput compressedStreamOutput = CompressorFactory.COMPRESSOR.streamOutput(bytesStreamOutput)) { write(obj, compressedStreamOutput); } } else { write(obj, bytesStreamOutput); } return bytesStreamOutput.bytes(); } }
public static CompressedXContent readCompressedString(StreamInput in) throws IOException { int crc32 = in.readInt(); byte[] compressed = new byte[in.readVInt()]; in.readBytes(compressed, 0, compressed.length); return new CompressedXContent(compressed, crc32); }
/** * Returns bytes reference, also un compress the source if needed. */ public BytesReference sourceRef() { if (source == null) { return null; } try { this.source = CompressorFactory.uncompressIfNeeded(this.source); return this.source; } catch (IOException e) { throw new ElasticsearchParseException("failed to decompress source", e); } }
@Override public int hashCode() { int result = type.hashCode(); result = 31 * result + source.hashCode(); result = 31 * result + routing.hashCode(); return result; }