Skip to content

Commit

Permalink
Move brToString(BytesRef) to ToStringUtils (#13068)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabi0 authored and uschindler committed Feb 15, 2024
1 parent c7da1ab commit 4feaf3b
Show file tree
Hide file tree
Showing 24 changed files with 341 additions and 530 deletions.
3 changes: 2 additions & 1 deletion lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Bug Fixes

Other
---------------------
(No changes)

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

======================== Lucene 9.10.0 =======================

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.bytesRefToString(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.bytesRefToString(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,8 +316,9 @@ 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?="
// + termExists + ") validIndexPrefix=" + validIndexPrefix);
// fr.fieldInfo.name + ":" + ToStringUtils.bytesRefToString(target) + " current=" +
// ToStringUtils.bytesRefToString(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.bytesRefToString(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.bytesRefToString(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.bytesRefToString(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.bytesRefToString(target) +
// " current=" + ToStringUtils.bytesRefToString(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,8 @@ 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.bytesRefToString(term));
// }
return SeekStatus.NOT_FOUND;
} else {
Expand All @@ -829,7 +811,8 @@ 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.bytesRefToString(term));
// }
return result;
}
Expand Down Expand Up @@ -1029,9 +1012,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.bytesRefToString(term) + " termExists?=" + termExists + " field=" +
// fr.fieldInfo.name + " termBlockOrd=" + currentFrame.state.termBlockOrd +
// " validIndexPrefix=" + validIndexPrefix);
// printSeekState(System.out);
// }

Expand Down Expand Up @@ -1095,8 +1079,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.bytesRefToString(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,14 @@ 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.bytesRefToString(target) +
// " term=" + ToStringUtils.bytesRefToString(term));

assert nextEnt != -1;

Expand Down Expand Up @@ -617,7 +603,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.bytesRefToString(suffixBytesRef));
// }

startBytePos = suffixesReader.getPosition();
Expand Down Expand Up @@ -682,8 +668,9 @@ 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.bytesRefToString(target) +
// " term=" + ToStringUtils.bytesRefToString(term));

assert nextEnt != -1;

Expand Down Expand Up @@ -711,7 +698,8 @@ 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.bytesRefToString(suffixBytesRef));
// }

final int termLen = prefix + suffix;
Expand Down Expand Up @@ -743,8 +731,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

0 comments on commit 4feaf3b

Please sign in to comment.