Search in sources :

Example 1 with BytesStreamInput

use of org.opensearch.common.io.stream.BytesStreamInput in project OpenSearch by opensearch-project.

the class BinaryDocValuesRangeQuery method createWeight.

@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
    return new ConstantScoreWeight(this, boost) {

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            final BinaryDocValues values = context.reader().getBinaryDocValues(fieldName);
            if (values == null) {
                return null;
            }
            final TwoPhaseIterator iterator = new TwoPhaseIterator(values) {

                BytesStreamInput in = new BytesStreamInput();

                BytesRef otherFrom = new BytesRef();

                BytesRef otherTo = new BytesRef();

                @Override
                public boolean matches() throws IOException {
                    BytesRef encodedRanges = values.binaryValue();
                    in.reset(encodedRanges.bytes, encodedRanges.offset, encodedRanges.length);
                    int numRanges = in.readVInt();
                    final byte[] bytes = encodedRanges.bytes;
                    otherFrom.bytes = bytes;
                    otherTo.bytes = bytes;
                    int offset = in.getPosition();
                    for (int i = 0; i < numRanges; i++) {
                        int length = lengthType.readLength(bytes, offset);
                        otherFrom.offset = offset;
                        otherFrom.length = length;
                        offset += length;
                        length = lengthType.readLength(bytes, offset);
                        otherTo.offset = offset;
                        otherTo.length = length;
                        offset += length;
                        if (queryType.matches(from, to, otherFrom, otherTo)) {
                            return true;
                        }
                    }
                    assert offset == encodedRanges.offset + encodedRanges.length;
                    return false;
                }

                @Override
                public float matchCost() {
                    // at most 4 comparisons
                    return 4;
                }
            };
            return new ConstantScoreScorer(this, score(), scoreMode, iterator);
        }

        @Override
        public boolean isCacheable(LeafReaderContext ctx) {
            return DocValues.isCacheable(ctx, fieldName);
        }
    };
}
Also used : TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) BytesStreamInput(org.opensearch.common.io.stream.BytesStreamInput) BytesRef(org.apache.lucene.util.BytesRef)

Example 2 with BytesStreamInput

use of org.opensearch.common.io.stream.BytesStreamInput in project OpenSearch by opensearch-project.

the class BinaryRangeUtil method decodeRanges.

static List<RangeFieldMapper.Range> decodeRanges(BytesRef encodedRanges, RangeType rangeType, TriFunction<byte[], Integer, Integer, Object> decodeBytes) throws IOException {
    RangeType.LengthType lengthType = rangeType.lengthType;
    BytesStreamInput in = new BytesStreamInput(encodedRanges.bytes, encodedRanges.offset, encodedRanges.length);
    int numRanges = in.readVInt();
    List<RangeFieldMapper.Range> ranges = new ArrayList<>(numRanges);
    final byte[] bytes = encodedRanges.bytes;
    int offset = in.getPosition();
    for (int i = 0; i < numRanges; i++) {
        int length = lengthType.readLength(bytes, offset);
        Object from = decodeBytes.apply(bytes, offset, length);
        offset += length;
        length = lengthType.readLength(bytes, offset);
        Object to = decodeBytes.apply(bytes, offset, length);
        offset += length;
        // TODO: Support for exclusive ranges, pending resolution of #40601
        RangeFieldMapper.Range decodedRange = new RangeFieldMapper.Range(rangeType, from, to, true, true);
        ranges.add(decodedRange);
    }
    return ranges;
}
Also used : ArrayList(java.util.ArrayList) BytesStreamInput(org.opensearch.common.io.stream.BytesStreamInput) InetAddressPoint(org.apache.lucene.document.InetAddressPoint)

Example 3 with BytesStreamInput

use of org.opensearch.common.io.stream.BytesStreamInput in project OpenSearch by opensearch-project.

the class AbstractBinaryDVLeafFieldData method getBytesValues.

@Override
public SortedBinaryDocValues getBytesValues() {
    return new SortedBinaryDocValues() {

        int count;

        final BytesStreamInput in = new BytesStreamInput();

        final BytesRef scratch = new BytesRef();

        @Override
        public boolean advanceExact(int doc) throws IOException {
            if (values.advanceExact(doc)) {
                final BytesRef bytes = values.binaryValue();
                assert bytes.length > 0;
                in.reset(bytes.bytes, bytes.offset, bytes.length);
                count = in.readVInt();
                scratch.bytes = bytes.bytes;
                return true;
            } else {
                return false;
            }
        }

        @Override
        public int docValueCount() {
            return count;
        }

        @Override
        public BytesRef nextValue() throws IOException {
            scratch.length = in.readVInt();
            scratch.offset = in.getPosition();
            in.setPosition(scratch.offset + scratch.length);
            return scratch;
        }
    };
}
Also used : BytesStreamInput(org.opensearch.common.io.stream.BytesStreamInput) BytesRef(org.apache.lucene.util.BytesRef) SortedBinaryDocValues(org.opensearch.index.fielddata.SortedBinaryDocValues)

Example 4 with BytesStreamInput

use of org.opensearch.common.io.stream.BytesStreamInput in project OpenSearch by opensearch-project.

the class TransportSearchHelper method parseScrollId.

static ParsedScrollId parseScrollId(String scrollId) {
    try {
        byte[] bytes = Base64.getUrlDecoder().decode(scrollId);
        BytesStreamInput in = new BytesStreamInput(bytes);
        final boolean includeContextUUID;
        final String type;
        final String firstChunk = in.readString();
        if (INCLUDE_CONTEXT_UUID.equals(firstChunk)) {
            includeContextUUID = true;
            type = in.readString();
        } else {
            includeContextUUID = false;
            type = firstChunk;
        }
        SearchContextIdForNode[] context = new SearchContextIdForNode[in.readVInt()];
        for (int i = 0; i < context.length; ++i) {
            final String contextUUID = includeContextUUID ? in.readString() : "";
            long id = in.readLong();
            String target = in.readString();
            String clusterAlias;
            final int index = target.indexOf(RemoteClusterAware.REMOTE_CLUSTER_INDEX_SEPARATOR);
            if (index == -1) {
                clusterAlias = null;
            } else {
                clusterAlias = target.substring(0, index);
                target = target.substring(index + 1);
            }
            context[i] = new SearchContextIdForNode(clusterAlias, target, new ShardSearchContextId(contextUUID, id));
        }
        if (in.getPosition() != bytes.length) {
            throw new IllegalArgumentException("Not all bytes were read");
        }
        return new ParsedScrollId(scrollId, type, context);
    } catch (Exception e) {
        throw new IllegalArgumentException("Cannot parse scroll id", e);
    }
}
Also used : ShardSearchContextId(org.opensearch.search.internal.ShardSearchContextId) BytesStreamInput(org.opensearch.common.io.stream.BytesStreamInput) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Aggregations

BytesStreamInput (org.opensearch.common.io.stream.BytesStreamInput)4 BytesRef (org.apache.lucene.util.BytesRef)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 InetAddressPoint (org.apache.lucene.document.InetAddressPoint)1 BinaryDocValues (org.apache.lucene.index.BinaryDocValues)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 ConstantScoreScorer (org.apache.lucene.search.ConstantScoreScorer)1 ConstantScoreWeight (org.apache.lucene.search.ConstantScoreWeight)1 TwoPhaseIterator (org.apache.lucene.search.TwoPhaseIterator)1 SortedBinaryDocValues (org.opensearch.index.fielddata.SortedBinaryDocValues)1 ShardSearchContextId (org.opensearch.search.internal.ShardSearchContextId)1