@Override protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException { if (!fieldType().stored()) { return; } byte[] value; if (context.parser().currentToken() == XContentParser.Token.VALUE_NULL) { return; } else { value = context.parser().binaryValue(); if (compress != null && compress && !CompressorFactory.isCompressed(value, 0, value.length)) { if (compressThreshold == -1 || value.length > compressThreshold) { BytesStreamOutput bStream = new BytesStreamOutput(); StreamOutput stream = CompressorFactory.defaultCompressor().streamOutput(bStream); stream.writeBytes(value, 0, value.length); stream.close(); value = bStream.bytes().toBytes(); } } } if (value == null) { return; } fields.add(new Field(names.indexName(), value, fieldType)); }
@Override public void write(MetaData metaData) throws GatewayException { final String newMetaData = "metadata-" + (currentIndex + 1); CachedStreamOutput.Entry cachedEntry = CachedStreamOutput.popEntry(); try { StreamOutput streamOutput; if (compress) { streamOutput = cachedEntry.bytes(CompressorFactory.defaultCompressor()); } else { streamOutput = cachedEntry.bytes(); } XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON, streamOutput); builder.startObject(); MetaData.Builder.toXContent(metaData, builder, ToXContent.EMPTY_PARAMS); builder.endObject(); builder.close(); metaDataBlobContainer.writeBlob( newMetaData, new ByteArrayInputStream( cachedEntry.bytes().underlyingBytes(), 0, cachedEntry.bytes().size()), cachedEntry.bytes().size()); } catch (IOException e) { throw new GatewayException("Failed to write metadata [" + newMetaData + "]", e); } finally { CachedStreamOutput.pushEntry(cachedEntry); } currentIndex++; try { metaDataBlobContainer.deleteBlobsByFilter( new BlobContainer.BlobNameFilter() { @Override public boolean accept(String blobName) { return blobName.startsWith("metadata-") && !newMetaData.equals(blobName); } }); } catch (IOException e) { logger.debug("Failed to delete old metadata, will do it next time", e); } }