@Override public void copy(MutableValue source) { MutableValueStr s = (MutableValueStr) source; exists = s.exists; value.copyBytes(s.value); }
@Override public MutableValue duplicate() { MutableValueStr v = new MutableValueStr(); v.value.copyBytes(value); v.exists = this.exists; return v; }
@Override public boolean seekExact(BytesRef text) throws IOException { int ord = values.lookupTerm(text); if (ord >= 0) { currentOrd = ord; scratch.copyBytes(text); return true; } else { return false; } }
@Override public boolean seekExact(BytesRef text) throws IOException { long ord = values.lookupTerm(text); if (ord >= 0) { currentOrd = ord; scratch.copyBytes(text); return true; } else { return false; } }
@Override public void seekExact(long ord) throws IOException { assert ord >= 0 && ord < values.getValueCount(); currentOrd = (int) ord; scratch.copyBytes(values.lookupOrd(currentOrd)); }
@Override public SeekStatus seekCeil(BytesRef text) throws IOException { int ord = values.lookupTerm(text); if (ord >= 0) { currentOrd = ord; scratch.copyBytes(text); return SeekStatus.FOUND; } else { currentOrd = -ord-1; if (currentOrd == values.getValueCount()) { return SeekStatus.END; } else { // TODO: hmm can we avoid this "extra" lookup?: scratch.copyBytes(values.lookupOrd(currentOrd)); return SeekStatus.NOT_FOUND; } } }
@Override public SeekStatus seekCeil(BytesRef text) throws IOException { long ord = values.lookupTerm(text); if (ord >= 0) { currentOrd = ord; scratch.copyBytes(text); return SeekStatus.FOUND; } else { currentOrd = -ord-1; if (currentOrd == values.getValueCount()) { return SeekStatus.END; } else { // TODO: hmm can we avoid this "extra" lookup?: scratch.copyBytes(values.lookupOrd(currentOrd)); return SeekStatus.NOT_FOUND; } } }
@Override public void seekExact(long ord) throws IOException { assert ord >= 0 && ord < values.getValueCount(); currentOrd = (int) ord; scratch.copyBytes(values.lookupOrd(currentOrd)); }
/** Return an array that contains the min and max values for the [offset, offset+length] interval * of the given {@link BytesRef}s. */ private static BytesRef[] computeMinMax(int count, IntFunction<BytesRef> packedValues, int offset, int length) { assert length > 0; BytesRefBuilder min = new BytesRefBuilder(); BytesRefBuilder max = new BytesRefBuilder(); BytesRef first = packedValues.apply(0); min.copyBytes(first.bytes, first.offset + offset, length); max.copyBytes(first.bytes, first.offset + offset, length); for (int i = 1; i < count; ++i) { BytesRef candidate = packedValues.apply(i); if (FutureArrays.compareUnsigned(min.bytes(), 0, length, candidate.bytes, candidate.offset + offset, candidate.offset + offset + length) > 0) { min.copyBytes(candidate.bytes, candidate.offset + offset, length); } else if (FutureArrays.compareUnsigned(max.bytes(), 0, length, candidate.bytes, candidate.offset + offset, candidate.offset + offset + length) < 0) { max.copyBytes(candidate.bytes, candidate.offset + offset, length); } } return new BytesRef[]{min.get(), max.get()}; }
@Override public CharTermAttributeImpl clone() { CharTermAttributeImpl t = (CharTermAttributeImpl)super.clone(); // Do a deep clone t.termBuffer = new char[this.termLength]; System.arraycopy(this.termBuffer, 0, t.termBuffer, 0, this.termLength); t.builder = new BytesRefBuilder(); t.builder.copyBytes(builder.get()); return t; }
@Override public BytesRef next() throws IOException { currentOrd++; if (currentOrd >= values.getValueCount()) { return null; } scratch.copyBytes(values.lookupOrd(currentOrd)); return scratch.get(); }
@Override public BytesRef next() throws IOException { currentOrd++; if (currentOrd >= values.getValueCount()) { return null; } scratch.copyBytes(values.lookupOrd(currentOrd)); return scratch.get(); }
@Override public void copy(int slot, int doc) throws IOException { final BytesRef comparableBytes = getValueForDoc(doc); if (comparableBytes == null) { values[slot] = null; } else { if (tempBRs[slot] == null) { tempBRs[slot] = new BytesRefBuilder(); } tempBRs[slot].copyBytes(comparableBytes); values[slot] = tempBRs[slot].get(); } }
private boolean compareToLastTerm(BytesRef t) { if (lastTerm == null && t != null) { lastTerm = new BytesRefBuilder(); lastTerm.append(t); } else if (t == null) { lastTerm = null; } else { assert lastTerm.get().compareTo(t) < 0: "lastTerm=" + lastTerm + " t=" + t; lastTerm.copyBytes(t); } return true; }
/** Pushes the new term to the top of the stack, and writes new blocks. */ private void pushTerm(BytesRef text) throws IOException { int limit = Math.min(lastTerm.length(), text.length); // Find common prefix between last term and current term: int pos = 0; while (pos < limit && lastTerm.byteAt(pos) == text.bytes[text.offset+pos]) { pos++; } // if (DEBUG) System.out.println(" shared=" + pos + " lastTerm.length=" + lastTerm.length); // Close the "abandoned" suffix now: for(int i=lastTerm.length()-1;i>=pos;i--) { // How many items on top of the stack share the current suffix // we are closing: int prefixTopSize = pending.size() - prefixStarts[i]; if (prefixTopSize >= minItemsInBlock) { // if (DEBUG) System.out.println("pushTerm i=" + i + " prefixTopSize=" + prefixTopSize + " minItemsInBlock=" + minItemsInBlock); writeBlocks(i+1, prefixTopSize); prefixStarts[i] -= prefixTopSize-1; } } if (prefixStarts.length < text.length) { prefixStarts = ArrayUtil.grow(prefixStarts, text.length); } // Init new tail: for(int i=pos;i<text.length;i++) { prefixStarts[i] = pending.size(); } lastTerm.copyBytes(text); }
@Override public void copy(int slot, int doc) throws IOException { int ord = getOrdForDoc(doc); if (ord == -1) { ord = missingOrd; values[slot] = null; } else { assert ord >= 0; if (tempBRs[slot] == null) { tempBRs[slot] = new BytesRefBuilder(); } tempBRs[slot].copyBytes(termsIndex.lookupOrd(ord)); values[slot] = tempBRs[slot].get(); } ords[slot] = ord; readerGen[slot] = currentReaderGen; }
/** add a term. This fully consumes in the incoming {@link BytesRef}. */ public void add(String field, BytesRef bytes) { assert lastTerm.equals(new Term("")) || new Term(field, bytes).compareTo(lastTerm) > 0; try { final int prefix; if (size > 0 && field.equals(lastTerm.field)) { // same field as the last term prefix = StringHelper.bytesDifference(lastTerm.bytes, bytes); output.writeVInt(prefix << 1); } else { // field change prefix = 0; output.writeVInt(1); output.writeString(field); } int suffix = bytes.length - prefix; output.writeVInt(suffix); output.writeBytes(bytes.bytes, bytes.offset + prefix, suffix); lastTermBytes.copyBytes(bytes); lastTerm.bytes = lastTermBytes.get(); lastTerm.field = field; size += 1; } catch (IOException e) { throw new RuntimeException(e); } }
/** * Creates a new {@link TermInSetQuery} from the given collection of terms. */ public TermInSetQuery(String field, Collection<BytesRef> terms) { BytesRef[] sortedTerms = terms.toArray(new BytesRef[terms.size()]); // already sorted if we are a SortedSet with natural order boolean sorted = terms instanceof SortedSet && ((SortedSet<BytesRef>)terms).comparator() == null; if (!sorted) { ArrayUtil.timSort(sortedTerms); } PrefixCodedTerms.Builder builder = new PrefixCodedTerms.Builder(); BytesRefBuilder previous = null; for (BytesRef term : sortedTerms) { if (previous == null) { previous = new BytesRefBuilder(); } else if (previous.get().equals(term)) { continue; // deduplicate } builder.add(field, term); previous.copyBytes(term); } this.field = field; termData = builder.finish(); termDataHashCode = termData.hashCode(); }
@Override protected BytesRef nextSeekTerm(final BytesRef term) throws IOException { //System.out.println("ATE.nextSeekTerm term=" + term); if (term == null) { assert seekBytesRef.length() == 0; // return the empty term, as it's valid if (runAutomaton.isAccept(0)) { return seekBytesRef.get(); } } else { seekBytesRef.copyBytes(term); } // seek to the next possible string; if (nextString()) { return seekBytesRef.get(); // reposition } else { return null; // no more possible strings can match } }
@Override public void seekExact(BytesRef target, TermState otherState) { // if (DEBUG) { // System.out.println("BTTR.seekExact termState seg=" + segment + " target=" + target.utf8ToString() + " " + target + " state=" + otherState); // } assert clearEOF(); if (target.compareTo(term.get()) != 0 || !termExists) { assert otherState != null && otherState instanceof BlockTermState; currentFrame = staticFrame; currentFrame.state.copyFrom(otherState); term.copyBytes(target); currentFrame.metaDataUpto = currentFrame.getTermBlockOrd(); assert currentFrame.metaDataUpto > 0; validIndexPrefix = 0; } else { // if (DEBUG) { // System.out.println(" skip seek: already on target state=" + currentFrame.state); // } } }