Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move brToString(BytesRef) to ToStringUtils #13068

Merged
merged 11 commits into from
Feb 15, 2024
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ Other

* GITHUB#13053: Minor AnyQueryNode code cleanup (Dmitry Cherniachenko)

* GITHUB#13068: Replace numerous `brToString(BytesRef)` copies with a `ToStringUtils` method (Dmitry Cherniachenko)

======================== Lucene 9.9.2 =======================

Bug Fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public int getDocCount() {
@Override
public TermsEnum intersect(CompiledAutomaton compiled, BytesRef startTerm) throws IOException {
// if (DEBUG) System.out.println(" FieldReader.intersect startTerm=" +
// BlockTreeTermsWriter.brToString(startTerm));
// ToStringUtils.brToString(startTerm));
// System.out.println("intersect: " + compiled.type + " a=" + compiled.automaton);
// TODO: we could push "it's a range" or "it's a prefix" down into IntersectTermsEnum?
// can we optimize knowing that...?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,19 +543,6 @@ private BytesRef _next() throws IOException {
}
}

// for debugging
@SuppressWarnings("unused")
static String brToString(BytesRef b) {
try {
return b.utf8ToString() + " " + b;
} catch (Throwable t) {
// If BytesRef isn't actually UTF8, or it's eg a
// prefix of UTF8 that ends mid-unicode-char, we
// fallback to hex:
return b.toString();
}
}

private void copyTerm() {
final int len = currentFrame.prefix + currentFrame.suffix;
if (term.bytes.length < len) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,24 +354,6 @@ public int size() {
return fieldMap.size();
}

// for debugging
String brToString(BytesRef b) {
if (b == null) {
return "null";
} else {
try {
return b.utf8ToString() + " " + b;
} catch (
@SuppressWarnings("unused")
Throwable t) {
// If BytesRef isn't actually UTF8, or it's eg a
// prefix of UTF8 that ends mid-unicode-char, we
// fallback to hex:
return b.toString();
}
}
}

@Override
public void checkIntegrity() throws IOException {
// terms index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ SegmentTermsEnumFrame pushFrame(FST.Arc<BytesRef> arc, long fp, int length) thro
final SegmentTermsEnumFrame f = getFrame(1 + currentFrame.ord);
f.arc = arc;
if (f.fpOrig == fp && f.nextEnt != -1) {
// if (DEBUG) System.out.println(" push reused frame ord=" + f.ord + " fp=" + f.fp + "
// isFloor?=" + f.isFloor + " hasTerms=" + f.hasTerms + " pref=" + term + " nextEnt=" +
// if (DEBUG) System.out.println(" push reused frame ord=" + f.ord + " fp=" + f.fp +
// " isFloor?=" + f.isFloor + " hasTerms=" + f.hasTerms + " pref=" + term + " nextEnt=" +
// f.nextEnt + " targetBeforeCurrentLength=" + targetBeforeCurrentLength + " term.length=" +
// term.length + " vs prefix=" + f.prefix);
// if (f.prefix > targetBeforeCurrentLength) {
Expand All @@ -279,7 +279,7 @@ SegmentTermsEnumFrame pushFrame(FST.Arc<BytesRef> arc, long fp, int length) thro
// final int sav = term.length;
// term.length = length;
// System.out.println(" push new frame ord=" + f.ord + " fp=" + f.fp + " hasTerms=" +
// f.hasTerms + " isFloor=" + f.isFloor + " pref=" + brToString(term));
// f.hasTerms + " isFloor=" + f.isFloor + " pref=" + ToStringUtils.brToString(term));
// term.length = sav;
// }
}
Expand All @@ -299,27 +299,6 @@ private boolean setEOF() {
return true;
}

/*
// for debugging
@SuppressWarnings("unused")
static String brToString(BytesRef b) {
try {
return b.utf8ToString() + " " + b;
} catch (Throwable t) {
// If BytesRef isn't actually UTF8, or it's eg a
// prefix of UTF8 that ends mid-unicode-char, we
// fallback to hex:
return b.toString();
}
}

// for debugging
@SuppressWarnings("unused")
static String brToString(BytesRefBuilder b) {
return brToString(b.get());
}
*/

@Override
public boolean seekExact(BytesRef target) throws IOException {

Expand All @@ -337,7 +316,8 @@ public boolean seekExact(BytesRef target) throws IOException {

// if (DEBUG) {
// System.out.println("\nBTTR.seekExact seg=" + fr.parent.segment + " target=" +
// fr.fieldInfo.name + ":" + brToString(target) + " current=" + brToString(term) + " (exists?="
// fr.fieldInfo.name + ":" + ToStringUtils.brToString(target) + " current=" +
// ToStringUtils.brToString(term) + " (exists?="
// + termExists + ") validIndexPrefix=" + validIndexPrefix);
// printSeekState(System.out);
// }
Expand Down Expand Up @@ -496,8 +476,8 @@ public boolean seekExact(BytesRef target) throws IOException {
}

// if (DEBUG) {
// System.out.println(" start index loop targetUpto=" + targetUpto + " output=" + output + "
// currentFrame.ord=" + currentFrame.ord + " targetBeforeCurrentLength=" +
// System.out.println(" start index loop targetUpto=" + targetUpto + " output=" + output +
// " currentFrame.ord=" + currentFrame.ord + " targetBeforeCurrentLength=" +
// targetBeforeCurrentLength);
// }

Expand Down Expand Up @@ -528,7 +508,7 @@ public boolean seekExact(BytesRef target) throws IOException {
term.setByteAt(targetUpto, (byte) targetLabel);
term.setLength(1 + targetUpto);
// if (DEBUG) {
// System.out.println(" FAST NOT_FOUND term=" + brToString(term));
// System.out.println(" FAST NOT_FOUND term=" + ToStringUtils.brToString(term));
// }
return false;
}
Expand All @@ -544,7 +524,7 @@ public boolean seekExact(BytesRef target) throws IOException {
} else {
// if (DEBUG) {
// System.out.println(" got " + result + "; return NOT_FOUND term=" +
// brToString(term));
// ToStringUtils.brToString(term));
// }
return false;
}
Expand Down Expand Up @@ -587,7 +567,7 @@ public boolean seekExact(BytesRef target) throws IOException {
termExists = false;
term.setLength(targetUpto);
// if (DEBUG) {
// System.out.println(" FAST NOT_FOUND term=" + brToString(term));
// System.out.println(" FAST NOT_FOUND term=" + ToStringUtils.brToString(term));
// }
return false;
}
Expand Down Expand Up @@ -623,7 +603,8 @@ public SeekStatus seekCeil(BytesRef target) throws IOException {

// if (DEBUG) {
// System.out.println("\nBTTR.seekCeil seg=" + fr.parent.segment + " target=" +
// fr.fieldInfo.name + ":" + brToString(target) + " " + target + " current=" + brToString(term)
// fr.fieldInfo.name + ":" + ToStringUtils.brToString(target) + " " + target +
// " current=" + ToStringUtils.brToString(term)
// + " (exists?=" + termExists + ") validIndexPrefix= " + validIndexPrefix);
// printSeekState(System.out);
// }
Expand Down Expand Up @@ -667,9 +648,9 @@ public SeekStatus seekCeil(BytesRef target) throws IOException {
cmp = (term.byteAt(targetUpto) & 0xFF) - (target.bytes[target.offset + targetUpto] & 0xFF);
// if (DEBUG) {
// System.out.println(" cycle targetUpto=" + targetUpto + " (vs limit=" + targetLimit +
// ") cmp=" + cmp + " (targetLabel=" + (char) (target.bytes[target.offset + targetUpto]) + "
// vs termLabel=" + (char) (term.byteAt(targetUpto)) + ")" + " arc.output=" + arc.output +
// " output=" + output);
// ") cmp=" + cmp + " (targetLabel=" + (char) (target.bytes[target.offset + targetUpto]) +
// " vs termLabel=" + (char) (term.byteAt(targetUpto)) + ")" + " arc.output=" + arc.output
// + " output=" + output);
// }
if (cmp != 0) {
break;
Expand Down Expand Up @@ -781,8 +762,8 @@ public SeekStatus seekCeil(BytesRef target) throws IOException {
}

// if (DEBUG) {
// System.out.println(" start index loop targetUpto=" + targetUpto + " output=" + output + "
// currentFrame.ord+1=" + currentFrame.ord + " targetBeforeCurrentLength=" +
// System.out.println(" start index loop targetUpto=" + targetUpto + " output=" + output +
// " currentFrame.ord+1=" + currentFrame.ord + " targetBeforeCurrentLength=" +
// targetBeforeCurrentLength);
// }

Expand Down Expand Up @@ -818,7 +799,7 @@ public SeekStatus seekCeil(BytesRef target) throws IOException {

if (next() != null) {
// if (DEBUG) {
// System.out.println(" return NOT_FOUND term=" + brToString(term));
// System.out.println(" return NOT_FOUND term=" + ToStringUtils.brToString(term));
// }
return SeekStatus.NOT_FOUND;
} else {
Expand All @@ -829,7 +810,7 @@ public SeekStatus seekCeil(BytesRef target) throws IOException {
}
} else {
// if (DEBUG) {
// System.out.println(" return " + result + " term=" + brToString(term));
// System.out.println(" return " + result + " term=" + ToStringUtils.brToString(term));
// }
return result;
}
Expand Down Expand Up @@ -1029,9 +1010,10 @@ public BytesRef next() throws IOException {

assert !eof;
// if (DEBUG) {
// System.out.println("\nBTTR.next seg=" + fr.parent.segment + " term=" + brToString(term) + "
// termExists?=" + termExists + " field=" + fr.fieldInfo.name + " termBlockOrd=" +
// currentFrame.state.termBlockOrd + " validIndexPrefix=" + validIndexPrefix);
// System.out.println("\nBTTR.next seg=" + fr.parent.segment + " term=" +
// ToStringUtils.brToString(term) + " termExists?=" + termExists + " field=" +
// fr.fieldInfo.name + " termBlockOrd=" + currentFrame.state.termBlockOrd +
// " validIndexPrefix=" + validIndexPrefix);
// printSeekState(System.out);
// }

Expand Down Expand Up @@ -1095,8 +1077,8 @@ public BytesRef next() throws IOException {
// try to scan to the right floor frame:
currentFrame.loadBlock();
} else {
// if (DEBUG) System.out.println(" return term=" + brToString(term) + " currentFrame.ord="
// + currentFrame.ord);
// if (DEBUG) System.out.println(" return term=" + ToStringUtils.brToString(term) +
// " currentFrame.ord=" + currentFrame.ord);
return term.get();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ public boolean next() throws IOException {
}

public void nextLeaf() {
// if (DEBUG) System.out.println(" frame.next ord=" + ord + " nextEnt=" + nextEnt + "
// entCount=" + entCount);
// if (DEBUG) System.out.println(" frame.next ord=" + ord + " nextEnt=" + nextEnt +
// " entCount=" + entCount);
assert nextEnt != -1 && nextEnt < entCount
: "nextEnt=" + nextEnt + " entCount=" + entCount + " fp=" + fp;
nextEnt++;
Expand Down Expand Up @@ -410,8 +410,8 @@ public void scanToFloorFrame(BytesRef target) {
newFP = fpOrig + (code >>> 1);
hasTerms = (code & 1) != 0;
// if (DEBUG) {
// System.out.println(" label=" + toHex(nextFloorLabel) + " fp=" + newFP + "
// hasTerms?=" + hasTerms + " numFollowFloor=" + numFollowFloorBlocks);
// System.out.println(" label=" + toHex(nextFloorLabel) + " fp=" + newFP +
// " hasTerms?=" + hasTerms + " numFollowFloor=" + numFollowFloorBlocks);
// }

isLastInFloor = numFollowFloorBlocks == 1;
Expand Down Expand Up @@ -566,28 +566,13 @@ public SeekStatus scanToTerm(BytesRef target, boolean exactOnly) throws IOExcept
private long subCode;
CompressionAlgorithm compressionAlg = CompressionAlgorithm.NO_COMPRESSION;

// for debugging
/*
@SuppressWarnings("unused")
static String brToString(BytesRef b) {
try {
return b.utf8ToString() + " " + b;
} catch (Throwable t) {
// If BytesRef isn't actually UTF8, or it's eg a
// prefix of UTF8 that ends mid-unicode-char, we
// fallback to hex:
return b.toString();
}
}
*/

// Target's prefix matches this block's prefix; we
// scan the entries check if the suffix matches.
public SeekStatus scanToTermLeaf(BytesRef target, boolean exactOnly) throws IOException {

// if (DEBUG) System.out.println(" scanToTermLeaf: block fp=" + fp + " prefix=" + prefix + "
// nextEnt=" + nextEnt + " (of " + entCount + ") target=" + brToString(target) + " term=" +
// brToString(term));
// if (DEBUG) System.out.println(" scanToTermLeaf: block fp=" + fp + " prefix=" + prefix +
// " nextEnt=" + nextEnt + " (of " + entCount + ") target=" + ToStringUtils.brToString(target) +
// " term=" + ToStringUtils.brToString(term));

assert nextEnt != -1;

Expand Down Expand Up @@ -617,7 +602,7 @@ public SeekStatus scanToTermLeaf(BytesRef target, boolean exactOnly) throws IOEx
// suffixBytesRef.offset = suffixesReader.getPosition();
// suffixBytesRef.length = suffix;
// System.out.println(" cycle: term " + (nextEnt-1) + " (of " + entCount + ") suffix="
// + brToString(suffixBytesRef));
// + ToStringUtils.brToString(suffixBytesRef));
// }

startBytePos = suffixesReader.getPosition();
Expand Down Expand Up @@ -682,8 +667,8 @@ public SeekStatus scanToTermLeaf(BytesRef target, boolean exactOnly) throws IOEx
public SeekStatus scanToTermNonLeaf(BytesRef target, boolean exactOnly) throws IOException {

// if (DEBUG) System.out.println(" scanToTermNonLeaf: block fp=" + fp + " prefix=" + prefix +
// " nextEnt=" + nextEnt + " (of " + entCount + ") target=" + brToString(target) + " term=" +
// brToString(target));
// " nextEnt=" + nextEnt + " (of " + entCount + ") target=" + ToStringUtils.brToString(target) +
// " term=" + ToStringUtils.brToString(target));

assert nextEnt != -1;

Expand Down Expand Up @@ -711,7 +696,7 @@ public SeekStatus scanToTermNonLeaf(BytesRef target, boolean exactOnly) throws I
// suffixBytesRef.offset = suffixesReader.getPosition();
// suffixBytesRef.length = suffix;
// System.out.println(" cycle: " + ((code&1)==1 ? "sub-block" : "term") + " " +
// (nextEnt-1) + " (of " + entCount + ") suffix=" + brToString(suffixBytesRef));
// (nextEnt-1) + " (of " + entCount + ") suffix=" + ToStringUtils.brToString(suffixBytesRef));
// }

final int termLen = prefix + suffix;
Expand Down Expand Up @@ -743,8 +728,8 @@ public SeekStatus scanToTermNonLeaf(BytesRef target, boolean exactOnly) throws I
// return NOT_FOUND:
fillTerm();

// if (DEBUG) System.out.println(" maybe done exactOnly=" + exactOnly + "
// ste.termExists=" + ste.termExists);
// if (DEBUG) System.out.println(" maybe done exactOnly=" + exactOnly +
// " ste.termExists=" + ste.termExists);

if (!exactOnly && !ste.termExists) {
// System.out.println(" now pushFrame");
Expand Down
Loading
Loading