Skip to content

Commit f154c0d

Browse files
committed
[index] do not full rebuild when adding a new file
Also, don't do a partial rebuild if there are non-time-ordered formats.
1 parent 4e8fe18 commit f154c0d

2 files changed

Lines changed: 38 additions & 24 deletions

File tree

src/formats/access_log.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"access_log": {
44
"title": "Common Access Log",
55
"description": "The default web access log format for servers like Apache.",
6-
"url": "http://en.wikipedia.org/wiki/Common_Log_Format",
6+
"url": "https://en.wikipedia.org/wiki/Common_Log_Format",
77
"multiline": false,
88
"regex": {
99
"ts-first-noquotes": {

src/logfile_sub_source.cc

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -872,14 +872,14 @@ logfile_sub_source::rebuild_index(std::optional<ui_clock::time_point> deadline)
872872
iterator iter;
873873
size_t total_lines = 0;
874874
size_t est_remaining_lines = 0;
875-
bool full_sort = this->lss_index.empty();
875+
auto all_time_ordered_formats = true;
876+
auto full_sort = this->lss_index.empty();
876877
int file_count = 0;
877-
bool force = this->lss_force_rebuild;
878+
auto force = std::exchange(this->lss_force_rebuild, false);
878879
auto retval = rebuild_result::rr_no_change;
879880
std::optional<timeval> lowest_tv = std::nullopt;
880881
auto search_start = 0_vl;
881882

882-
this->lss_force_rebuild = false;
883883
if (force) {
884884
log_debug("forced to full rebuild");
885885
retval = rebuild_result::rr_full_rebuild;
@@ -925,6 +925,9 @@ logfile_sub_source::rebuild_index(std::optional<ui_clock::time_point> deadline)
925925
full_sort = true;
926926
}
927927
} else {
928+
if (!lf->get_format_ptr()->lf_time_ordered) {
929+
all_time_ordered_formats = false;
930+
}
928931
if (time_left && deadline && ui_clock::now() > deadline.value()) {
929932
log_debug("no time left, skipping %s",
930933
lf->get_filename_as_string().c_str());
@@ -934,9 +937,20 @@ logfile_sub_source::rebuild_index(std::optional<ui_clock::time_point> deadline)
934937
|= lf->get_format_ptr()->lf_timestamp_flags;
935938

936939
if (!this->tss_view->is_paused() && time_left) {
937-
switch (lf->rebuild_index(deadline)) {
940+
auto log_rebuild_res = lf->rebuild_index(deadline);
941+
942+
if (ld.ld_lines_indexed < lf->size()
943+
&& log_rebuild_res
944+
== logfile::rebuild_result_t::NO_NEW_LINES)
945+
{
946+
// This is a bit awkward... if the logfile indexing was
947+
// complete before being added to us, we need to adjust
948+
// the rebuild result to make it look like new lines
949+
// were added.
950+
log_rebuild_res = logfile::rebuild_result_t::NEW_LINES;
951+
}
952+
switch (log_rebuild_res) {
938953
case logfile::rebuild_result_t::NO_NEW_LINES:
939-
// No changes
940954
break;
941955
case logfile::rebuild_result_t::NEW_LINES:
942956
if (retval == rebuild_result::rr_no_change) {
@@ -973,14 +987,13 @@ logfile_sub_source::rebuild_index(std::optional<ui_clock::time_point> deadline)
973987
->get_time<
974988
std::chrono::microseconds>()
975989
.count());
976-
if (retval
977-
<= rebuild_result::rr_partial_rebuild)
990+
if (retval <= rebuild_result::rr_partial_rebuild
991+
&& all_time_ordered_formats)
978992
{
979993
retval = rebuild_result::rr_partial_rebuild;
980-
if (!lowest_tv) {
981-
lowest_tv = new_file_line.get_timeval();
982-
} else if (new_file_line.get_timeval()
983-
< lowest_tv.value())
994+
if (!lowest_tv
995+
|| new_file_line.get_timeval()
996+
< lowest_tv.value())
984997
{
985998
lowest_tv = new_file_line.get_timeval();
986999
}
@@ -1053,34 +1066,36 @@ logfile_sub_source::rebuild_index(std::optional<ui_clock::time_point> deadline)
10531066
continue;
10541067
}
10551068

1069+
require(lf->get_format_ptr()->lf_time_ordered);
1070+
10561071
auto line_iter = lf->find_from_time(lowest_tv.value());
10571072

10581073
if (line_iter) {
1059-
log_debug("%s: lowest line time %ld; line %ld; size %ld",
1060-
lf->get_filename().c_str(),
1074+
log_debug("lowest line time %ld; line %ld; size %ld; path=%s",
10611075
line_iter.value()->get_timeval().tv_sec,
10621076
std::distance(lf->cbegin(), line_iter.value()),
1063-
lf->size());
1077+
lf->size(),
1078+
lf->get_filename_as_string().c_str());
10641079
}
10651080
ld.ld_lines_indexed
10661081
= std::distance(lf->cbegin(), line_iter.value_or(lf->cend()));
10671082
remaining += lf->size() - ld.ld_lines_indexed;
10681083
}
10691084

1070-
auto row_iter = std::lower_bound(this->lss_index.begin(),
1071-
this->lss_index.end(),
1072-
*lowest_tv,
1073-
logline_cmp(*this));
1085+
auto* row_iter = std::lower_bound(this->lss_index.begin(),
1086+
this->lss_index.end(),
1087+
lowest_tv.value(),
1088+
logline_cmp(*this));
10741089
this->lss_index.shrink_to(
10751090
std::distance(this->lss_index.begin(), row_iter));
10761091
log_debug("new index size %ld/%ld; remain %ld",
10771092
this->lss_index.ba_size,
10781093
this->lss_index.ba_capacity,
10791094
remaining);
1080-
auto filt_row_iter = lower_bound(this->lss_filtered_index.begin(),
1081-
this->lss_filtered_index.end(),
1082-
*lowest_tv,
1083-
filtered_logline_cmp(*this));
1095+
auto filt_row_iter = std::lower_bound(this->lss_filtered_index.begin(),
1096+
this->lss_filtered_index.end(),
1097+
lowest_tv.value(),
1098+
filtered_logline_cmp(*this));
10841099
this->lss_filtered_index.resize(
10851100
std::distance(this->lss_filtered_index.begin(), filt_row_iter));
10861101
search_start = vis_line_t(this->lss_filtered_index.size());
@@ -1694,7 +1709,6 @@ logfile_sub_source::insert_file(const std::shared_ptr<logfile>& lf)
16941709
} else {
16951710
(*existing)->set_file(lf);
16961711
}
1697-
this->lss_force_rebuild = true;
16981712

16991713
return true;
17001714
}

0 commit comments

Comments
 (0)