Search in sources :

Example 6 with LongBitSet

use of org.apache.lucene.util.LongBitSet in project lucene-solr by apache.

the class SimpleTextBKDWriter method finish.

/** Writes the BKD tree to the provided {@link IndexOutput} and returns the file offset where index was written. */
public long finish(IndexOutput out) throws IOException {
    // Catch user silliness:
    if (heapPointWriter == null && tempInput == null) {
        throw new IllegalStateException("already finished");
    }
    if (offlinePointWriter != null) {
        offlinePointWriter.close();
    }
    if (pointCount == 0) {
        throw new IllegalStateException("must index at least one point");
    }
    LongBitSet ordBitSet;
    if (numDims > 1) {
        if (singleValuePerDoc) {
            ordBitSet = new LongBitSet(maxDoc);
        } else {
            ordBitSet = new LongBitSet(pointCount);
        }
    } else {
        ordBitSet = null;
    }
    long countPerLeaf = pointCount;
    long innerNodeCount = 1;
    while (countPerLeaf > maxPointsInLeafNode) {
        countPerLeaf = (countPerLeaf + 1) / 2;
        innerNodeCount *= 2;
    }
    int numLeaves = (int) innerNodeCount;
    checkMaxLeafNodeCount(numLeaves);
    // NOTE: we could save the 1+ here, to use a bit less heap at search time, but then we'd need a somewhat costly check at each
    // step of the recursion to recompute the split dim:
    // Indexed by nodeID, but first (root) nodeID is 1.  We do 1+ because the lead byte at each recursion says which dim we split on.
    byte[] splitPackedValues = new byte[Math.toIntExact(numLeaves * (1 + bytesPerDim))];
    // +1 because leaf count is power of 2 (e.g. 8), and innerNodeCount is power of 2 minus 1 (e.g. 7)
    long[] leafBlockFPs = new long[numLeaves];
    // Make sure the math above "worked":
    assert pointCount / numLeaves <= maxPointsInLeafNode : "pointCount=" + pointCount + " numLeaves=" + numLeaves + " maxPointsInLeafNode=" + maxPointsInLeafNode;
    // Sort all docs once by each dimension:
    PathSlice[] sortedPointWriters = new PathSlice[numDims];
    // This is only used on exception; on normal code paths we close all files we opened:
    List<Closeable> toCloseHeroically = new ArrayList<>();
    boolean success = false;
    try {
        //long t0 = System.nanoTime();
        for (int dim = 0; dim < numDims; dim++) {
            sortedPointWriters[dim] = new PathSlice(sort(dim), 0, pointCount);
        }
        if (tempInput != null) {
            tempDir.deleteFile(tempInput.getName());
            tempInput = null;
        } else {
            assert heapPointWriter != null;
            heapPointWriter = null;
        }
        build(1, numLeaves, sortedPointWriters, ordBitSet, out, minPackedValue, maxPackedValue, splitPackedValues, leafBlockFPs, toCloseHeroically);
        for (PathSlice slice : sortedPointWriters) {
            slice.writer.destroy();
        }
        // If no exception, we should have cleaned everything up:
        assert tempDir.getCreatedFiles().isEmpty();
        //long t2 = System.nanoTime();
        //System.out.println("write time: " + ((t2-t1)/1000000.0) + " msec");
        success = true;
    } finally {
        if (success == false) {
            IOUtils.deleteFilesIgnoringExceptions(tempDir, tempDir.getCreatedFiles());
            IOUtils.closeWhileHandlingException(toCloseHeroically);
        }
    }
    //System.out.println("Total nodes: " + innerNodeCount);
    // Write index:
    long indexFP = out.getFilePointer();
    writeIndex(out, leafBlockFPs, splitPackedValues);
    return indexFP;
}
Also used : Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) LongBitSet(org.apache.lucene.util.LongBitSet)

Example 7 with LongBitSet

use of org.apache.lucene.util.LongBitSet in project lucene-solr by apache.

the class CheckIndex method checkSortedSetDocValues.

private static void checkSortedSetDocValues(String fieldName, int maxDoc, SortedSetDocValues dv) throws IOException {
    final long maxOrd = dv.getValueCount() - 1;
    LongBitSet seenOrds = new LongBitSet(dv.getValueCount());
    long maxOrd2 = -1;
    int docID;
    while ((docID = dv.nextDoc()) != NO_MORE_DOCS) {
        long lastOrd = -1;
        long ord;
        int ordCount = 0;
        while ((ord = dv.nextOrd()) != SortedSetDocValues.NO_MORE_ORDS) {
            if (ord <= lastOrd) {
                throw new RuntimeException("ords out of order: " + ord + " <= " + lastOrd + " for doc: " + docID);
            }
            if (ord < 0 || ord > maxOrd) {
                throw new RuntimeException("ord out of bounds: " + ord);
            }
            lastOrd = ord;
            maxOrd2 = Math.max(maxOrd2, ord);
            seenOrds.set(ord);
            ordCount++;
        }
        if (ordCount == 0) {
            throw new RuntimeException("dv for field: " + fieldName + " returned docID=" + docID + " yet has no ordinals");
        }
    }
    if (maxOrd != maxOrd2) {
        throw new RuntimeException("dv for field: " + fieldName + " reports wrong maxOrd=" + maxOrd + " but this is not the case: " + maxOrd2);
    }
    if (seenOrds.cardinality() != dv.getValueCount()) {
        throw new RuntimeException("dv for field: " + fieldName + " has holes in its ords, valueCount=" + dv.getValueCount() + " but only used: " + seenOrds.cardinality());
    }
    BytesRef lastValue = null;
    for (long i = 0; i <= maxOrd; i++) {
        final BytesRef term = dv.lookupOrd(i);
        assert term.isValid();
        if (lastValue != null) {
            if (term.compareTo(lastValue) <= 0) {
                throw new RuntimeException("dv for field: " + fieldName + " has ords out of order: " + lastValue + " >=" + term);
            }
        }
        lastValue = BytesRef.deepCopyOf(term);
    }
}
Also used : LongBitSet(org.apache.lucene.util.LongBitSet) BytesRef(org.apache.lucene.util.BytesRef)

Example 8 with LongBitSet

use of org.apache.lucene.util.LongBitSet in project lucene-solr by apache.

the class DocValuesTermsQuery method createWeight.

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

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            final SortedSetDocValues values = DocValues.getSortedSet(context.reader(), field);
            final LongBitSet bits = new LongBitSet(values.getValueCount());
            boolean matchesAtLeastOneTerm = false;
            TermIterator iterator = termData.iterator();
            for (BytesRef term = iterator.next(); term != null; term = iterator.next()) {
                final long ord = values.lookupTerm(term);
                if (ord >= 0) {
                    matchesAtLeastOneTerm = true;
                    bits.set(ord);
                }
            }
            if (matchesAtLeastOneTerm == false) {
                return null;
            }
            return new ConstantScoreScorer(this, score(), new TwoPhaseIterator(values) {

                @Override
                public boolean matches() throws IOException {
                    for (long ord = values.nextOrd(); ord != SortedSetDocValues.NO_MORE_ORDS; ord = values.nextOrd()) {
                        if (bits.get(ord)) {
                            return true;
                        }
                    }
                    return false;
                }

                @Override
                public float matchCost() {
                    // lookup in a bitset
                    return 3;
                }
            });
        }
    };
}
Also used : SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) TermIterator(org.apache.lucene.index.PrefixCodedTerms.TermIterator) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) LongBitSet(org.apache.lucene.util.LongBitSet) IOException(java.io.IOException) BytesRef(org.apache.lucene.util.BytesRef)

Example 9 with LongBitSet

use of org.apache.lucene.util.LongBitSet in project lucene-solr by apache.

the class DocValuesConsumer method mergeSortedField.

/**
   * Merges the sorted docvalues from <code>toMerge</code>.
   * <p>
   * The default implementation calls {@link #addSortedField}, passing
   * an Iterable that merges ordinals and values and filters deleted documents .
   */
public void mergeSortedField(FieldInfo fieldInfo, final MergeState mergeState) throws IOException {
    List<SortedDocValues> toMerge = new ArrayList<>();
    for (int i = 0; i < mergeState.docValuesProducers.length; i++) {
        SortedDocValues values = null;
        DocValuesProducer docValuesProducer = mergeState.docValuesProducers[i];
        if (docValuesProducer != null) {
            FieldInfo readerFieldInfo = mergeState.fieldInfos[i].fieldInfo(fieldInfo.name);
            if (readerFieldInfo != null && readerFieldInfo.getDocValuesType() == DocValuesType.SORTED) {
                values = docValuesProducer.getSorted(fieldInfo);
            }
        }
        if (values == null) {
            values = DocValues.emptySorted();
        }
        toMerge.add(values);
    }
    final int numReaders = toMerge.size();
    final SortedDocValues[] dvs = toMerge.toArray(new SortedDocValues[numReaders]);
    // step 1: iterate thru each sub and mark terms still in use
    TermsEnum[] liveTerms = new TermsEnum[dvs.length];
    long[] weights = new long[liveTerms.length];
    for (int sub = 0; sub < numReaders; sub++) {
        SortedDocValues dv = dvs[sub];
        Bits liveDocs = mergeState.liveDocs[sub];
        if (liveDocs == null) {
            liveTerms[sub] = dv.termsEnum();
            weights[sub] = dv.getValueCount();
        } else {
            LongBitSet bitset = new LongBitSet(dv.getValueCount());
            int docID;
            while ((docID = dv.nextDoc()) != NO_MORE_DOCS) {
                if (liveDocs.get(docID)) {
                    int ord = dv.ordValue();
                    if (ord >= 0) {
                        bitset.set(ord);
                    }
                }
            }
            liveTerms[sub] = new BitsFilteredTermsEnum(dv.termsEnum(), bitset);
            weights[sub] = bitset.cardinality();
        }
    }
    // step 2: create ordinal map (this conceptually does the "merging")
    final OrdinalMap map = OrdinalMap.build(null, liveTerms, weights, PackedInts.COMPACT);
    // step 3: add field
    addSortedField(fieldInfo, new EmptyDocValuesProducer() {

        @Override
        public SortedDocValues getSorted(FieldInfo fieldInfoIn) throws IOException {
            if (fieldInfoIn != fieldInfo) {
                throw new IllegalArgumentException("wrong FieldInfo");
            }
            // We must make new iterators + DocIDMerger for each iterator:
            List<SortedDocValuesSub> subs = new ArrayList<>();
            long cost = 0;
            for (int i = 0; i < mergeState.docValuesProducers.length; i++) {
                SortedDocValues values = null;
                DocValuesProducer docValuesProducer = mergeState.docValuesProducers[i];
                if (docValuesProducer != null) {
                    FieldInfo readerFieldInfo = mergeState.fieldInfos[i].fieldInfo(fieldInfo.name);
                    if (readerFieldInfo != null && readerFieldInfo.getDocValuesType() == DocValuesType.SORTED) {
                        values = docValuesProducer.getSorted(readerFieldInfo);
                    }
                }
                if (values == null) {
                    values = DocValues.emptySorted();
                }
                cost += values.cost();
                subs.add(new SortedDocValuesSub(mergeState.docMaps[i], values, map.getGlobalOrds(i)));
            }
            final long finalCost = cost;
            final DocIDMerger<SortedDocValuesSub> docIDMerger = DocIDMerger.of(subs, mergeState.needsIndexSort);
            return new SortedDocValues() {

                private int docID = -1;

                private int ord;

                @Override
                public int docID() {
                    return docID;
                }

                @Override
                public int nextDoc() throws IOException {
                    SortedDocValuesSub sub = docIDMerger.next();
                    if (sub == null) {
                        return docID = NO_MORE_DOCS;
                    }
                    int subOrd = sub.values.ordValue();
                    assert subOrd != -1;
                    ord = (int) sub.map.get(subOrd);
                    docID = sub.mappedDocID;
                    return docID;
                }

                @Override
                public int ordValue() {
                    return ord;
                }

                @Override
                public int advance(int target) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public boolean advanceExact(int target) throws IOException {
                    throw new UnsupportedOperationException();
                }

                @Override
                public long cost() {
                    return finalCost;
                }

                @Override
                public int getValueCount() {
                    return (int) map.getValueCount();
                }

                @Override
                public BytesRef lookupOrd(int ord) throws IOException {
                    int segmentNumber = map.getFirstSegmentNumber(ord);
                    int segmentOrd = (int) map.getFirstSegmentOrd(ord);
                    return dvs[segmentNumber].lookupOrd(segmentOrd);
                }
            };
        }
    });
}
Also used : ArrayList(java.util.ArrayList) EmptyDocValuesProducer(org.apache.lucene.index.EmptyDocValuesProducer) LongBitSet(org.apache.lucene.util.LongBitSet) IOException(java.io.IOException) SortedDocValues(org.apache.lucene.index.SortedDocValues) OrdinalMap(org.apache.lucene.index.MultiDocValues.OrdinalMap) TermsEnum(org.apache.lucene.index.TermsEnum) FilteredTermsEnum(org.apache.lucene.index.FilteredTermsEnum) EmptyDocValuesProducer(org.apache.lucene.index.EmptyDocValuesProducer) DocIDMerger(org.apache.lucene.index.DocIDMerger) Bits(org.apache.lucene.util.Bits) ArrayList(java.util.ArrayList) List(java.util.List) FieldInfo(org.apache.lucene.index.FieldInfo) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

LongBitSet (org.apache.lucene.util.LongBitSet)9 BytesRef (org.apache.lucene.util.BytesRef)6 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 Closeable (java.io.Closeable)2 List (java.util.List)2 TreeSet (java.util.TreeSet)2 DocIDMerger (org.apache.lucene.index.DocIDMerger)2 EmptyDocValuesProducer (org.apache.lucene.index.EmptyDocValuesProducer)2 FieldInfo (org.apache.lucene.index.FieldInfo)2 FilteredTermsEnum (org.apache.lucene.index.FilteredTermsEnum)2 OrdinalMap (org.apache.lucene.index.MultiDocValues.OrdinalMap)2 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)2 TermsEnum (org.apache.lucene.index.TermsEnum)2 Bits (org.apache.lucene.util.Bits)2 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)2 OrdinalsFilter (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude.OrdinalsFilter)2 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 TermIterator (org.apache.lucene.index.PrefixCodedTerms.TermIterator)1 RandomAccessOrds (org.apache.lucene.index.RandomAccessOrds)1