Example usage for org.apache.lucene.util BytesRefIterator BytesRefIterator

List of usage examples for org.apache.lucene.util BytesRefIterator BytesRefIterator

Introduction

In this page you can find the example usage for org.apache.lucene.util BytesRefIterator BytesRefIterator.

Prototype

BytesRefIterator

Source Link

Usage

From source file:org.codelibs.elasticsearch.common.bytes.BytesReference.java

License:Apache License

/**
 * 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/*from w  w  w .  j a v  a 2s .  c  o m*/
 */
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;
        }
    };
}

From source file:org.codelibs.elasticsearch.common.bytes.CompositeBytesReference.java

License:Apache License

@Override
public BytesRefIterator iterator() {
    if (references.length > 0) {
        return new BytesRefIterator() {
            int index = 0;
            private BytesRefIterator current = references[index++].iterator();

            @Override//from   w w  w .  jav a  2 s  . com
            public BytesRef next() throws IOException {
                BytesRef next = current.next();
                if (next == null) {
                    while (index < references.length) {
                        current = references[index++].iterator();
                        next = current.next();
                        if (next != null) {
                            break;
                        }
                    }
                }
                return next;
            }
        };
    } else {
        return () -> null;
    }

}

From source file:org.codelibs.elasticsearch.common.bytes.PagedBytesReference.java

License:Apache License

@Override
public final BytesRefIterator iterator() {
    final int offset = this.offset;
    final int length = this.length;
    // this iteration is page aligned to ensure we do NOT materialize the pages from the ByteArray
    // we calculate the initial fragment size here to ensure that if this reference is a slice we are still page aligned
    // across the entire iteration. The first page is smaller if our offset != 0 then we start in the middle of the page
    // otherwise we iterate full pages until we reach the last chunk which also might end within a page.
    final int initialFragmentSize = offset != 0 ? PAGE_SIZE - (offset % PAGE_SIZE) : PAGE_SIZE;
    return new BytesRefIterator() {
        int position = 0;
        int nextFragmentSize = Math.min(length, initialFragmentSize);
        // this BytesRef is reused across the iteration on purpose - BytesRefIterator interface was designed for this
        final BytesRef slice = new BytesRef();

        @Override// w w  w  .  j  a v a 2s.  com
        public BytesRef next() throws IOException {
            if (nextFragmentSize != 0) {
                final boolean materialized = byteArray.get(offset + position, nextFragmentSize, slice);
                assert materialized == false : "iteration should be page aligned but array got materialized";
                position += nextFragmentSize;
                final int remaining = length - position;
                nextFragmentSize = Math.min(remaining, PAGE_SIZE);
                return slice;
            } else {
                assert nextFragmentSize == 0 : "fragmentSize expected [0] but was: [" + nextFragmentSize + "]";
                return null; // we are done with this iteration
            }
        }
    };
}

From source file:org.elasticsearch.index.fielddata.ordinals.OrdinalsBuilder.java

License:Apache License

/**
 * This method iterates all terms in the given {@link TermsEnum} and
 * associates each terms ordinal with the terms documents. The caller must
 * exhaust the returned {@link BytesRefIterator} which returns all values
 * where the first returned value is associted with the ordinal <tt>1</tt>
 * etc./*  w w w  . j  a  v a  2 s.  com*/
 * <p>
 * If the {@link TermsEnum} contains prefix coded numerical values the terms
 * enum should be wrapped with either {@link #wrapNumeric32Bit(TermsEnum)}
 * or {@link #wrapNumeric64Bit(TermsEnum)} depending on its precision. If
 * the {@link TermsEnum} is not wrapped the returned
 * {@link BytesRefIterator} will contain partial precision terms rather than
 * only full-precision terms.
 * </p>
 */
public BytesRefIterator buildFromTerms(final TermsEnum termsEnum) throws IOException {
    return new BytesRefIterator() {
        private DocsEnum docsEnum = null;

        @Override
        public BytesRef next() throws IOException {
            BytesRef ref;
            if ((ref = termsEnum.next()) != null) {
                docsEnum = termsEnum.docs(null, docsEnum, DocsEnum.FLAG_NONE);
                nextOrdinal();
                int docId;
                while ((docId = docsEnum.nextDoc()) != DocsEnum.NO_MORE_DOCS) {
                    addDoc(docId);
                }
            }
            return ref;
        }

        @Override
        public Comparator<BytesRef> getComparator() {
            return termsEnum.getComparator();
        }
    };
}

From source file:uk.co.flax.luwak.analysis.TestTermsEnumTokenFilter.java

License:Apache License

@Test
public void testPosIncAttributeOverflow() throws IOException {

    final BytesRef foo = new BytesRef("foo");
    final BytesRef bar = new BytesRef("bar");

    BytesRefIterator terms = new BytesRefIterator() {

        long count = 1000;

        @Override/*from   w  ww .j av a2 s . c o  m*/
        public BytesRef next() throws IOException {
            if (count-- > 100)
                return foo;
            if (count-- > 0)
                return bar;
            return null;
        }
    };

    try (TokenStream ts = new LeapfrogTokenFilter(new TermsEnumTokenStream(terms))) {
        while (ts.incrementToken()) {
            // This tight loop will throw an exception if clearAttributes() is not called
            // by TermsEnumTokenStream.  See issue #46
        }
    }
}