static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) { try { BytesRef source = XContentHelper.toXContent(toXContent, xContentType, false).toBytesRef(); return new ByteArrayEntity(source.bytes, source.offset, source.length, createContentType(xContentType)); } catch (IOException e) { throw new RuntimeException(e); } }
BytesRef metadataSource = BytesReference.bytes(metadata).toBytesRef(); content.write(metadataSource.bytes, metadataSource.offset, metadataSource.length); content.write(separator); try (XContentBuilder builder = XContentBuilder.builder(bulkContentType.xContent())) { builder.copyCurrentStructure(parser); source = BytesReference.bytes(builder).toBytesRef(); source = XContentHelper.toXContent((UpdateRequest) action, bulkContentType, false).toBytesRef();
/** * Interprets the referenced bytes as UTF8 bytes, returning the resulting string */ public String utf8ToString() { return toBytesRef().utf8ToString(); }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { BytesRef bytes = toBytesRef(); return builder.value(bytes.bytes, bytes.offset, bytes.length); } }
private SourceToParse(String index, String type, String id, BytesReference source, XContentType xContentType) { this.index = Objects.requireNonNull(index); this.type = Objects.requireNonNull(type); this.id = Objects.requireNonNull(id); // we always convert back to byte array, since we store it and Field only supports bytes.. // so, we might as well do it here, and improve the performance of working with direct byte arrays this.source = new BytesArray(Objects.requireNonNull(source).toBytesRef()); this.xContentType = Objects.requireNonNull(xContentType); }
@Override public Object get(int i) { final int startOffset = i == 0 ? 0 : endOffsets[i-1]; final int endOffset = endOffsets[i]; return bytes.slice(startOffset, endOffset - startOffset).toBytesRef(); } @Override
/** * Returns a compact array from the given BytesReference. The returned array won't be copied unless necessary. If you need * to modify the returned array use {@code BytesRef.deepCopyOf(reference.toBytesRef()} instead */ public static byte[] toBytes(BytesReference reference) { final BytesRef bytesRef = reference.toBytesRef(); if (bytesRef.offset == 0 && bytesRef.length == bytesRef.bytes.length) { return bytesRef.bytes; } return BytesRef.deepCopyOf(bytesRef).bytes; }
/** * Guesses the content type based on the provided bytes. * * @deprecated the content type should not be guessed except for few cases where we effectively don't know the content type. * The REST layer should move to reading the Content-Type header instead. There are other places where auto-detection may be needed. * This method is deprecated to prevent usages of it from spreading further without specific reasons. */ @Deprecated public static XContentType xContentType(BytesReference bytes) { BytesRef br = bytes.toBytesRef(); return XContentFactory.xContentType(br.bytes, br.offset, br.length); } }
/** * Returns a BytesRefIterator for this BytesReference. This method allows * access to the internal pages of this reference without copying them. Use with care! * @see BytesRefIterator */ public BytesRefIterator iterator() { return new BytesRefIterator() { BytesRef ref = length() == 0 ? null : toBytesRef(); @Override public BytesRef next() throws IOException { BytesRef r = ref; ref = null; // only return it once... return r; } }; }
public static Request index(IndexRequest indexRequest) { String method = Strings.hasLength(indexRequest.id()) ? HttpMethod.PUT.name() : HttpMethod.POST.name(); boolean isCreate = (indexRequest.opType() == DocWriteRequest.OpType.CREATE); String endpoint = endpoint(indexRequest.index(), indexRequest.type(), indexRequest.id(), isCreate ? "_create" : null); Request request = new Request(method, endpoint); Params parameters = new Params(request); parameters.withRouting(indexRequest.routing()); parameters.withTimeout(indexRequest.timeout()); parameters.withVersion(indexRequest.version()); parameters.withVersionType(indexRequest.versionType()); parameters.withPipeline(indexRequest.getPipeline()); parameters.withRefreshPolicy(indexRequest.getRefreshPolicy()); parameters.withWaitForActiveShards(indexRequest.waitForActiveShards()); BytesRef source = indexRequest.source().toBytesRef(); ContentType contentType = createContentType(indexRequest.getContentType()); request.setEntity(new ByteArrayEntity(source.bytes, source.offset, source.length, contentType)); return request; }
/** * Creates a query builder given a query provided as a {@link BytesReference} */ public WrapperQueryBuilder(BytesReference source) { if (source == null || source.length() == 0) { throw new IllegalArgumentException("query source text cannot be null or empty"); } this.source = BytesRef.deepCopyOf(source.toBytesRef()).bytes; }
@Deprecated public static String convertToJson(BytesReference bytes, boolean reformatJson, boolean prettyPrint) throws IOException { return convertToJson(bytes, reformatJson, prettyPrint, XContentFactory.xContentType(bytes.toBytesRef().bytes)); }
public Fields getFields() throws IOException { if (hasTermVectors() && isExists()) { if (!sourceCopied) { // make the bytes safe headerRef = new BytesArray(headerRef.toBytesRef(), true); termVectors = new BytesArray(termVectors.toBytesRef(), true); } TermVectorsFields termVectorsFields = new TermVectorsFields(headerRef, termVectors); hasScores = termVectorsFields.hasScores; return termVectorsFields; } else { return new Fields() { @Override public Iterator<String> iterator() { return Collections.emptyIterator(); } @Override public Terms terms(String field) throws IOException { return null; } @Override public int size() { return 0; } }; } }
@Override public BytesReference cacheKey() throws IOException { BytesStreamOutput out = new BytesStreamOutput(); this.innerWriteTo(out, true); // copy it over, most requests are small, we might as well copy to make sure we are not sliced... // we could potentially keep it without copying, but then pay the price of extra unused bytes up to a page return new BytesArray(out.bytes().toBytesRef(), true);// do a deep copy }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { if (hasString()) { return builder.value(this.string()); } else { // TODO: TextBytesOptimization we can use a buffer here to convert it? maybe add a // request to jackson to support InputStream as well? BytesRef br = this.bytes().toBytesRef(); return builder.utf8Value(br.bytes, br.offset, br.length); } } }
@Override public void document(int docID, StoredFieldVisitor visitor) throws IOException { if (docID != 0) { throw new IllegalArgumentException("no such doc ID " + docID); } if (visitor.needsField(FAKE_SOURCE_FIELD) == StoredFieldVisitor.Status.YES) { assert operation.source().toBytesRef().offset == 0; assert operation.source().toBytesRef().length == operation.source().toBytesRef().bytes.length; visitor.binaryField(FAKE_SOURCE_FIELD, operation.source().toBytesRef().bytes); } if (operation.routing() != null && visitor.needsField(FAKE_ROUTING_FIELD) == StoredFieldVisitor.Status.YES) { visitor.stringField(FAKE_ROUTING_FIELD, operation.routing().getBytes(StandardCharsets.UTF_8)); } if (visitor.needsField(FAKE_ID_FIELD) == StoredFieldVisitor.Status.YES) { final byte[] id; if (indexVersionCreated.onOrAfter(Version.V_6_0_0)) { BytesRef bytesRef = Uid.encodeId(operation.id()); id = new byte[bytesRef.length]; System.arraycopy(bytesRef.bytes, bytesRef.offset, id, 0, bytesRef.length); } else { // TODO this can go away in 7.0 after backport id = operation.id().getBytes(StandardCharsets.UTF_8); } visitor.stringField(FAKE_ID_FIELD, id); } if (visitor.needsField(FAKE_UID_FIELD) == StoredFieldVisitor.Status.YES) { visitor.stringField(FAKE_UID_FIELD, Uid.createUid(operation.type(), operation.id()).getBytes(StandardCharsets.UTF_8)); } }
long readSnapshotIndexLatestBlob() throws IOException { try (InputStream blob = blobContainer().readBlob(INDEX_LATEST_BLOB)) { BytesStreamOutput out = new BytesStreamOutput(); Streams.copy(blob, out); return Numbers.bytesToLong(out.bytes().toBytesRef()); } }
private void buildValues(XContentBuilder builder, Terms curTerms, int termFreq) throws IOException { if (!(curTerms.hasPayloads() || curTerms.hasOffsets() || curTerms.hasPositions())) { return; } builder.startArray(FieldStrings.TOKENS); for (int i = 0; i < termFreq; i++) { builder.startObject(); if (curTerms.hasPositions()) { builder.field(FieldStrings.POS, currentPositions[i]); } if (curTerms.hasOffsets()) { builder.field(FieldStrings.START_OFFSET, currentStartOffset[i]); builder.field(FieldStrings.END_OFFSET, currentEndOffset[i]); } if (curTerms.hasPayloads() && (currentPayloads[i].length() > 0)) { BytesRef bytesRef = currentPayloads[i].toBytesRef(); builder.field(FieldStrings.PAYLOAD, bytesRef.bytes, bytesRef.offset, bytesRef.length); } builder.endObject(); } builder.endArray(); }
/** * Constructs a new term vector request for a document that will be fetch * from the provided index. Use {@link #type(String)} and * {@link #id(String)} to specify the document to load. */ public TermVectorsRequest(TermVectorsRequest other) { super(other.index()); this.id = other.id(); this.type = other.type(); if (other.doc != null) { this.doc = new BytesArray(other.doc().toBytesRef(), true); this.xContentType = other.xContentType; } this.flagsEnum = other.getFlags().clone(); this.preference = other.preference(); this.routing = other.routing(); this.parent = other.parent(); if (other.selectedFields != null) { this.selectedFields = new HashSet<>(other.selectedFields); } if (other.perFieldAnalyzer != null) { this.perFieldAnalyzer = new HashMap<>(other.perFieldAnalyzer); } this.realtime = other.realtime(); this.version = other.version(); this.versionType = VersionType.fromValue(other.versionType().getValue()); this.filterSettings = other.filterSettings(); }
/** * Marks this store as corrupted. This method writes a {@code corrupted_${uuid}} file containing the given exception * message. If a store contains a {@code corrupted_${uuid}} file {@link #isMarkedCorrupted()} will return <code>true</code>. */ public void markStoreCorrupted(IOException exception) throws IOException { ensureOpen(); if (!isMarkedCorrupted()) { String uuid = CORRUPTED + UUIDs.randomBase64UUID(); try (IndexOutput output = this.directory().createOutput(uuid, IOContext.DEFAULT)) { CodecUtil.writeHeader(output, CODEC, VERSION); BytesStreamOutput out = new BytesStreamOutput(); out.writeException(exception); BytesReference bytes = out.bytes(); output.writeVInt(bytes.length()); BytesRef ref = bytes.toBytesRef(); output.writeBytes(ref.bytes, ref.offset, ref.length); CodecUtil.writeFooter(output); } catch (IOException ex) { logger.warn("Can't mark store as corrupted", ex); } directory().sync(Collections.singleton(uuid)); } }