Search in sources :

Example 1 with OrdinalsBuilder

use of org.elasticsearch.index.fielddata.ordinals.OrdinalsBuilder in project elasticsearch by elastic.

the class PagedBytesIndexFieldData method loadDirect.

@Override
public AtomicOrdinalsFieldData loadDirect(LeafReaderContext context) throws Exception {
    LeafReader reader = context.reader();
    AtomicOrdinalsFieldData data = null;
    PagedBytesEstimator estimator = new PagedBytesEstimator(context, breakerService.getBreaker(CircuitBreaker.FIELDDATA), getFieldName());
    Terms terms = reader.terms(getFieldName());
    if (terms == null) {
        data = AbstractAtomicOrdinalsFieldData.empty();
        estimator.afterLoad(null, data.ramBytesUsed());
        return data;
    }
    final PagedBytes bytes = new PagedBytes(15);
    final PackedLongValues.Builder termOrdToBytesOffset = PackedLongValues.monotonicBuilder(PackedInts.COMPACT);
    final float acceptableTransientOverheadRatio = OrdinalsBuilder.DEFAULT_ACCEPTABLE_OVERHEAD_RATIO;
    // Wrap the context in an estimator and use it to either estimate
    // the entire set, or wrap the TermsEnum so it can be calculated
    // per-term
    TermsEnum termsEnum = estimator.beforeLoad(terms);
    boolean success = false;
    try (OrdinalsBuilder builder = new OrdinalsBuilder(reader.maxDoc(), acceptableTransientOverheadRatio)) {
        PostingsEnum docsEnum = null;
        for (BytesRef term = termsEnum.next(); term != null; term = termsEnum.next()) {
            final long termOrd = builder.nextOrdinal();
            assert termOrd == termOrdToBytesOffset.size();
            termOrdToBytesOffset.add(bytes.copyUsingLengthPrefix(term));
            docsEnum = termsEnum.postings(docsEnum, PostingsEnum.NONE);
            for (int docId = docsEnum.nextDoc(); docId != DocIdSetIterator.NO_MORE_DOCS; docId = docsEnum.nextDoc()) {
                builder.addDoc(docId);
            }
        }
        PagedBytes.Reader bytesReader = bytes.freeze(true);
        final Ordinals ordinals = builder.build();
        data = new PagedBytesAtomicFieldData(bytesReader, termOrdToBytesOffset.build(), ordinals);
        success = true;
        return data;
    } finally {
        if (!success) {
            // If something went wrong, unwind any current estimations we've made
            estimator.afterLoad(termsEnum, 0);
        } else {
            // Call .afterLoad() to adjust the breaker now that we have an exact size
            estimator.afterLoad(termsEnum, data.ramBytesUsed());
        }
    }
}
Also used : Ordinals(org.elasticsearch.index.fielddata.ordinals.Ordinals) PackedLongValues(org.apache.lucene.util.packed.PackedLongValues) LeafReader(org.apache.lucene.index.LeafReader) Terms(org.apache.lucene.index.Terms) RamAccountingTermsEnum(org.elasticsearch.index.fielddata.RamAccountingTermsEnum) TermsEnum(org.apache.lucene.index.TermsEnum) AtomicOrdinalsFieldData(org.elasticsearch.index.fielddata.AtomicOrdinalsFieldData) PagedBytes(org.apache.lucene.util.PagedBytes) OrdinalsBuilder(org.elasticsearch.index.fielddata.ordinals.OrdinalsBuilder) PostingsEnum(org.apache.lucene.index.PostingsEnum) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

LeafReader (org.apache.lucene.index.LeafReader)1 PostingsEnum (org.apache.lucene.index.PostingsEnum)1 Terms (org.apache.lucene.index.Terms)1 TermsEnum (org.apache.lucene.index.TermsEnum)1 BytesRef (org.apache.lucene.util.BytesRef)1 PagedBytes (org.apache.lucene.util.PagedBytes)1 PackedLongValues (org.apache.lucene.util.packed.PackedLongValues)1 AtomicOrdinalsFieldData (org.elasticsearch.index.fielddata.AtomicOrdinalsFieldData)1 RamAccountingTermsEnum (org.elasticsearch.index.fielddata.RamAccountingTermsEnum)1 Ordinals (org.elasticsearch.index.fielddata.ordinals.Ordinals)1 OrdinalsBuilder (org.elasticsearch.index.fielddata.ordinals.OrdinalsBuilder)1