refactor: extract helpers for verify print, scan loop, and per-contact export

- extract _show_integrity_issues() from cmd_history verify block
- extract _ff_scan_lines() shared by _ff_state_build and _ff_state_extend
- extract _export_one_contact() from log_database_export_to_flatfile loop
This commit is contained in:
2026-03-28 12:58:27 +03:00
parent a96a4ad41e
commit 7ba7e53291
3 changed files with 265 additions and 271 deletions

View File

@@ -163,23 +163,12 @@ _ff_maybe_index_line(ff_contact_state_t* state, const char* buf, off_t pos)
g_date_time_unref(dt);
}
static gboolean
_ff_state_build(ff_contact_state_t* state)
// Scan lines from an already-positioned file pointer, caching IDs and
// building index entries. Returns the number of messages scanned.
static size_t
_ff_scan_lines(ff_contact_state_t* state, FILE* fp)
{
FILE* fp = fopen(state->filepath, "r");
if (!fp)
return FALSE;
state->bom_len = ff_skip_bom(fp);
state->n_entries = 0;
state->total_lines = 0;
size_t msg_count = 0;
// Clear ID caches for full rebuild
g_hash_table_remove_all(state->archive_ids);
g_hash_table_remove_all(state->stanza_senders);
size_t count = 0;
while (1) {
off_t pos = ftell(fp);
gboolean truncated = FALSE;
@@ -197,16 +186,34 @@ _ff_state_build(ff_contact_state_t* state)
}
state->total_lines++;
// Extract IDs for O(1) dedup/LMC cache
_ff_cache_line_ids(state, buf);
if (msg_count % FF_INDEX_STEP == 0) {
if (count % FF_INDEX_STEP == 0) {
_ff_maybe_index_line(state, buf, pos);
}
msg_count++;
count++;
free(buf);
}
return count;
}
static gboolean
_ff_state_build(ff_contact_state_t* state)
{
FILE* fp = fopen(state->filepath, "r");
if (!fp)
return FALSE;
state->bom_len = ff_skip_bom(fp);
state->n_entries = 0;
state->total_lines = 0;
// Clear ID caches for full rebuild
g_hash_table_remove_all(state->archive_ids);
g_hash_table_remove_all(state->stanza_senders);
_ff_scan_lines(state, fp);
fclose(fp);
@@ -247,37 +254,8 @@ _ff_state_extend(ff_contact_state_t* state)
}
}
// Use a local counter for new messages so index step alignment
// doesn't depend on the total from the initial build.
size_t new_count = 0;
while (1) {
off_t pos = ftell(fp);
gboolean truncated = FALSE;
char* buf = ff_readline(fp, &truncated);
if (!buf)
break;
if (truncated) {
free(buf);
break;
}
if (buf[0] == '\0' || buf[0] == '#') {
free(buf);
continue;
}
state->total_lines++;
// Extract IDs for O(1) dedup/LMC cache
_ff_cache_line_ids(state, buf);
if (new_count % FF_INDEX_STEP == 0) {
_ff_maybe_index_line(state, buf, pos);
}
new_count++;
free(buf);
}
// Use _ff_scan_lines for the shared read/index/cache loop.
_ff_scan_lines(state, fp);
fclose(fp);