Skip to content

Commit cfd0378

Browse files
committed
[log_format] some scan_match refactoring
1 parent 6ceedc4 commit cfd0378

4 files changed

Lines changed: 41 additions & 32 deletions

File tree

src/log_format.hh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,7 @@ public:
158158
return name_matched{};
159159
}
160160

161-
struct scan_match {
162-
uint32_t sm_quality{0};
163-
uint32_t sm_strikes{0};
164-
uint32_t sm_precision{0};
165-
};
161+
using scan_match = log_format_scan_match;
166162

167163
struct scan_error {
168164
std::string se_message;

src/log_format_fwd.hh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,4 +713,36 @@ struct logline_value_vector {
713713
std::optional<std::string> lvv_thread_id_value;
714714
};
715715

716+
struct log_format_scan_match {
717+
uint32_t sm_quality{0};
718+
uint32_t sm_strikes{0};
719+
uint32_t sm_precision{0};
720+
721+
// Higher quality wins, then higher precision, then fewer strikes.
722+
bool is_better_than(const log_format_scan_match& other) const
723+
{
724+
if (this->sm_quality != other.sm_quality) {
725+
return this->sm_quality > other.sm_quality;
726+
}
727+
if (this->sm_precision != other.sm_precision) {
728+
return this->sm_precision > other.sm_precision;
729+
}
730+
return this->sm_strikes < other.sm_strikes;
731+
}
732+
733+
// Take the best of each field independently.
734+
void merge_best(const log_format_scan_match& other)
735+
{
736+
if (other.sm_quality > this->sm_quality) {
737+
this->sm_quality = other.sm_quality;
738+
}
739+
if (other.sm_precision > this->sm_precision) {
740+
this->sm_precision = other.sm_precision;
741+
}
742+
if (other.sm_strikes < this->sm_strikes) {
743+
this->sm_strikes = other.sm_strikes;
744+
}
745+
}
746+
};
747+
716748
#endif

src/logfile.cc

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -842,12 +842,8 @@ logfile::process_prefix(shared_buffer_ref& sbr,
842842
.get_time<std::chrono::microseconds>();
843843
}
844844
if (this->lf_format != nullptr) {
845-
best_match = std::make_pair(this->lf_format.get(),
846-
log_format::scan_match{
847-
this->lf_format_quality,
848-
0,
849-
this->lf_format_precision,
850-
});
845+
best_match
846+
= std::make_pair(this->lf_format.get(), this->lf_format_match);
851847
}
852848

853849
/*
@@ -961,23 +957,11 @@ logfile::process_prefix(shared_buffer_ref& sbr,
961957
sm.sm_quality,
962958
sm.sm_precision,
963959
sm.sm_strikes);
964-
if (sm.sm_quality > this->lf_format_quality) {
965-
this->lf_format_quality = sm.sm_quality;
966-
}
967-
if (sm.sm_precision > this->lf_format_precision) {
968-
this->lf_format_precision = sm.sm_precision;
969-
}
960+
this->lf_format_match.merge_best(sm);
970961
prev_index_size = this->lf_index.size();
971962
found = best_match->second;
972963
} else if (!best_match
973-
|| (sm.sm_quality > best_match->second.sm_quality
974-
|| (sm.sm_quality
975-
== best_match->second.sm_quality
976-
&& (sm.sm_precision
977-
> best_match->second.sm_precision
978-
|| sm.sm_strikes
979-
< best_match->second
980-
.sm_strikes))))
964+
|| sm.is_better_than(best_match->second))
981965
{
982966
log_info(
983967
" scan with format (%s) matched with quality of "
@@ -1093,9 +1077,8 @@ logfile::process_prefix(shared_buffer_ref& sbr,
10931077
&& (this->lf_format == nullptr
10941078
|| ((this->lf_format->lf_root_format
10951079
!= best_match->first->lf_root_format)
1096-
&& (best_match->second.sm_quality > this->lf_format_quality
1097-
|| best_match->second.sm_precision
1098-
> this->lf_format_precision))))
1080+
&& best_match->second.is_better_than(
1081+
this->lf_format_match))))
10991082
{
11001083
auto winner = best_match.value();
11011084
auto* curr = winner.first;
@@ -1122,8 +1105,7 @@ logfile::process_prefix(shared_buffer_ref& sbr,
11221105
}
11231106
this->lf_level_stats.update_msg_count(ll.get_msg_level());
11241107
}
1125-
this->lf_format_quality = winner.second.sm_quality;
1126-
this->lf_format_precision = winner.second.sm_precision;
1108+
this->lf_format_match = winner.second;
11271109
this->set_format_base_time(this->lf_format.get(), li);
11281110
if (this->lf_format->lf_date_time.dts_fmt_lock != -1) {
11291111
this->lf_content_id

src/logfile.hh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,7 @@ private:
558558
std::string lf_content_id;
559559
struct stat lf_stat{};
560560
std::shared_ptr<log_format> lf_format;
561-
uint32_t lf_format_quality{0};
562-
uint32_t lf_format_precision{0};
561+
log_format_scan_match lf_format_match;
563562
std::vector<logline> lf_index;
564563
std::chrono::microseconds lf_index_time{0};
565564
file_off_t lf_index_size{0};

0 commit comments

Comments
 (0)