Example usage for org.apache.lucene.util PagedBytes copyUsingLengthPrefix

List of usage examples for org.apache.lucene.util PagedBytes copyUsingLengthPrefix

Introduction

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

Prototype


public long copyUsingLengthPrefix(BytesRef bytes) 

Source Link

Document

Copy bytes in, writing the length as a 1 or 2 byte vInt prefix.

Usage

From source file:org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData.java

License:Apache License

@Override
public PagedBytesAtomicFieldData loadDirect(AtomicReaderContext context) throws Exception {
    AtomicReader reader = context.reader();

    PagedBytesEstimator estimator = new PagedBytesEstimator(context, breakerService.getBreaker());
    Terms terms = reader.terms(getFieldNames().indexName());
    if (terms == null) {
        PagedBytesAtomicFieldData emptyData = PagedBytesAtomicFieldData.empty(reader.maxDoc());
        estimator.adjustForNoTerms(emptyData.getMemorySizeInBytes());
        return emptyData;
    }/*w  w w .  jav  a  2 s  .  co  m*/

    final PagedBytes bytes = new PagedBytes(15);

    final MonotonicAppendingLongBuffer termOrdToBytesOffset = new MonotonicAppendingLongBuffer();
    termOrdToBytesOffset.add(0); // first ord is reserved for missing values
    final long numTerms;
    if (regex == null && frequency == null) {
        numTerms = terms.size();
    } else {
        numTerms = -1;
    }
    final float acceptableTransientOverheadRatio = fieldDataType.getSettings().getAsFloat(
            FilterSettingFields.ACCEPTABLE_TRANSIENT_OVERHEAD_RATIO,
            OrdinalsBuilder.DEFAULT_ACCEPTABLE_OVERHEAD_RATIO);

    OrdinalsBuilder builder = new OrdinalsBuilder(numTerms, reader.maxDoc(), acceptableTransientOverheadRatio);

    // 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
    PagedBytesAtomicFieldData data = null;
    TermsEnum termsEnum = estimator.beforeLoad(terms);
    boolean success = false;

    try {
        // 0 is reserved for "unset"
        bytes.copyUsingLengthPrefix(new BytesRef());

        DocsEnum 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.docs(null, docsEnum, DocsEnum.FLAG_NONE);
            for (int docId = docsEnum.nextDoc(); docId != DocsEnum.NO_MORE_DOCS; docId = docsEnum.nextDoc()) {
                builder.addDoc(docId);
            }
        }
        final long sizePointer = bytes.getPointer();
        PagedBytes.Reader bytesReader = bytes.freeze(true);
        final Ordinals ordinals = builder.build(fieldDataType.getSettings());

        data = new PagedBytesAtomicFieldData(bytesReader, sizePointer, termOrdToBytesOffset, 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.getMemorySizeInBytes());
        }
        builder.close();
    }
}