/** * Add the content of the provided {@link DocIdSetIterator} to this builder. NOTE: if you need to * build a {@link DocIdSet} out of a single {@link DocIdSetIterator}, you should rather use {@link * RoaringDocIdSet.Builder}. */ public void add(DocIdSetIterator iter) throws IOException { grow((int) Math.min(Integer.MAX_VALUE, iter.cost())); if (bitSet != null) { bitSet.or(iter); } else { while (true) { assert buffer.length <= threshold; final int end = buffer.length; for (int i = bufferSize; i < end; ++i) { final int doc = iter.nextDoc(); if (doc == DocIdSetIterator.NO_MORE_DOCS) { bufferSize = i; return; } buffer[bufferSize++] = doc; } bufferSize = end; if (bufferSize + 1 >= threshold) { break; } growBuffer(bufferSize + 1); } upgradeToBitSet(); for (int doc = iter.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = iter.nextDoc()) { bitSet.set(doc); } } }
/** Reserve space so that this builder can hold {@code numDocs} MORE documents. */ public void grow(int numDocs) { if (bitSet == null) { final long newLength = bufferSize + numDocs; if (newLength < threshold) { growBuffer((int) newLength); } else { upgradeToBitSet(); } } }
/** * Add a document to this builder. NOTE: doc IDs do not need to be provided in order. NOTE: if you * plan on adding several docs at once, look into using {@link #grow(int)} to reserve space. */ public void add(int doc) { if (bitSet != null) { bitSet.set(doc); } else { if (bufferSize + 1 > buffer.length) { if (bufferSize + 1 >= threshold) { upgradeToBitSet(); bitSet.set(doc); return; } growBuffer(bufferSize + 1); } buffer[bufferSize++] = doc; } }