querylog: imp docs
Some checks failed
build / test (macOS-latest) (push) Has been cancelled
build / test (ubuntu-latest) (push) Has been cancelled
build / test (windows-latest) (push) Has been cancelled
lint / go-lint (push) Has been cancelled
lint / eslint (push) Has been cancelled
build / build-release (push) Has been cancelled
build / notify (push) Has been cancelled
lint / notify (push) Has been cancelled

This commit is contained in:
Stanislav Chzhen
2025-10-15 10:13:54 +03:00
parent 412d186336
commit 2c4ad98d86

View File

@@ -175,31 +175,23 @@ type tsSearchState struct {
depth int
}
// maxSeekDepth is the maximum allowed search iterations.
const maxSeekDepth = 100
// maxSearchDepth is the maximum number of search iterations.
const maxSearchDepth = 100
// seekTSStep performs one iteration of the binary search over the qlog file.
// seekTSStep performs one iteration of the binary search over the qlog file. l
// and st must not be nil.
func (q *qLogFile) seekTSStep(
ctx context.Context,
l *slog.Logger,
timestamp int64,
st *tsSearchState,
) (found bool, err error) {
// Get the line at the specified position.
line, lineIdx, lineEndIdx, err := q.readProbeLine(st.probe)
line, lineIdx, lineEndIdx, err := q.readAndValidateProbe(st, timestamp)
if err != nil {
// Don't wrap the error, because it's informative enough as is.
return false, err
}
// Check if the line index is invalid.
err = q.validateQLogLineIdx(lineIdx, st.lastProbeLineIdx, timestamp, st.fileSize)
if err != nil {
return false, err
}
// Save the last probed index.
st.lastProbeLineIdx = lineIdx
// Get the timestamp from the query log record.
ts := readQLogTimestamp(ctx, l, line)
if ts == 0 {
@@ -234,7 +226,7 @@ func (q *qLogFile) seekTSStep(
st.probe = st.start + (st.end-st.start)/2
st.depth++
if st.depth >= maxSeekDepth {
if st.depth >= maxSearchDepth {
return false, fmt.Errorf(
"looking up timestamp %d in %q: depth %d too high: %w",
timestamp,
@@ -247,6 +239,29 @@ func (q *qLogFile) seekTSStep(
return false, nil
}
// readAndValidateProbe reads the probe line and validates the line index.
func (q *qLogFile) readAndValidateProbe(
st *tsSearchState,
timestamp int64,
) (line string, lineIdx, lineEndIdx int64, err error) {
// Get the line at the specified position.
line, lineIdx, lineEndIdx, err = q.readProbeLine(st.probe)
if err != nil {
return "", 0, 0, fmt.Errorf("reading probe line: %w", err)
}
// Check if the line index is invalid.
err = q.validateQLogLineIdx(lineIdx, st.lastProbeLineIdx, timestamp, st.fileSize)
if err != nil {
return "", 0, 0, fmt.Errorf("validating line index: %w", err)
}
// Save the last probed index.
st.lastProbeLineIdx = lineIdx
return line, lineIdx, lineEndIdx, nil
}
// SeekStart changes the current position to the end of the file. Please note,
// that we're reading query log in the reverse order and that's why log start
// is actually the end of file.