Skip to content

Commit

Permalink
mu: add --reindex option for mu index
Browse files Browse the repository at this point in the history
I.e. without having to reinit explicitly.
  • Loading branch information
djcb committed Apr 5, 2024
1 parent 4ddbac5 commit f813498
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 16 deletions.
4 changes: 4 additions & 0 deletions man/mu-index.1.org
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ messages.

disable the database cleanup that *mu* does by default after indexing.

** --reindex

perform a complete reindexing of all the messages in the maildir.

#+include: "muhome.inc" :minlevel 2

#+include: "common-options.inc" :minlevel 1
Expand Down
28 changes: 20 additions & 8 deletions mu/mu-cmd-index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,21 @@ print_stats(const Indexer::Progress& stats, bool color)
}

Result<void>
Mu::mu_cmd_index(Store& store, const Options& opts)
Mu::mu_cmd_index(const Options& opts)
{
const auto mdir{store.root_maildir()};
auto store = std::invoke([&]{
if (opts.index.reindex)
return Store::make(opts.runtime_path(RuntimePath::XapianDb),
Store::Options::ReInit|Store::Options::Writable);
else
return Store::make(opts.runtime_path(RuntimePath::XapianDb),
Store::Options::Writable);
});

if (!store)
return Err(store.error());

const auto mdir{store->root_maildir()};
if (G_UNLIKELY(::access(mdir.c_str(), R_OK) != 0))
return Err(Error::Code::File, "'{}' is not readable: {}",
mdir, g_strerror(errno));
Expand All @@ -93,19 +105,19 @@ Mu::mu_cmd_index(Store& store, const Options& opts)

mu_println("indexing maildir {}{}{} -> "
"store {}{}{}",
col.fg(Color::Green), store.root_maildir(), col.reset(),
col.fg(Color::Blue), store.path(), col.reset());
col.fg(Color::Green), store->root_maildir(), col.reset(),
col.fg(Color::Blue), store->path(), col.reset());
}

Mu::Indexer::Config conf{};
conf.cleanup = !opts.index.nocleanup;
conf.lazy_check = opts.index.lazycheck;
// ignore .noupdate with an empty store.
conf.ignore_noupdate = store.empty();
conf.ignore_noupdate = store->empty();

install_sig_handler();

auto& indexer{store.indexer()};
auto& indexer{store->indexer()};
indexer.start(conf);
while (!caught_signal && indexer.is_running()) {
if (!opts.quiet)
Expand All @@ -119,10 +131,10 @@ Mu::mu_cmd_index(Store& store, const Options& opts)
}
}

store.indexer().stop();
indexer.stop();

if (!opts.quiet) {
print_stats(store.indexer().progress(), !opts.nocolor);
print_stats(indexer.progress(), !opts.nocolor);
mu_print("\n");
::fflush({});
}
Expand Down
8 changes: 5 additions & 3 deletions mu/mu-cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,12 @@ Mu::mu_cmd_execute(const Options& opts) try {
return with_writable_store(mu_cmd_remove, opts);
case Options::SubCommand::Move:
return with_writable_store(mu_cmd_move, opts);
case Options::SubCommand::Index:
return with_writable_store(mu_cmd_index, opts);

/* commands instantiate store themselves */
/*
* commands instantiate store themselves
*/
case Options::SubCommand::Index:
return mu_cmd_index(opts);
case Options::SubCommand::Init:
return mu_cmd_init(opts);
case Options::SubCommand::Server:
Expand Down
3 changes: 1 addition & 2 deletions mu/mu-cmd.hh
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,11 @@ Result<void> mu_cmd_find(const Store& store, const Options& opts);
/**
* execute the 'index' command
*
* @param store store object to use
* @param opts configuration options
*
* @return Ok() or some error
*/
Result<void> mu_cmd_index(Store& store, const Options& opt);
Result<void> mu_cmd_index(const Options& opt);

/**
* execute the 'info' command
Expand Down
4 changes: 3 additions & 1 deletion mu/mu-options.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** Copyright (C) 2022-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** Copyright (C) 2022-2024 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
Expand Down Expand Up @@ -430,6 +430,8 @@ sub_index(CLI::App& sub, Options& opts)
"Skip based on dir-timestamps");
sub.add_flag("--nocleanup", opts.index.nocleanup,
"Don't clean up database after indexing");
sub.add_flag("--reindex", opts.index.reindex,
"Perform a complete reindexing");
}


Expand Down
1 change: 1 addition & 0 deletions mu/mu-options.hh
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ struct Options {
struct Index {
bool nocleanup; /**< don't cleanup del'd mails */
bool lazycheck; /**< don't check uptodate dirs */
bool reindex; /**< do a full re-index */
} index;


Expand Down
8 changes: 6 additions & 2 deletions mu/mu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ handle_result(const Result<void>& res, const Mu::Options& opts)
col.fg(Color::Blue), col.reset(),
col.fg(Color::Green), res.error().hint(), col.reset());

if (res.error().exit_code() != 0 && !res.error().is_soft_error())
mu_warning("mu finishing with error: {}", format_as(res.error()));
if (res.error().exit_code() != 0 && !res.error().is_soft_error()) {
mu_warning("mu finishing with error: {}",
format_as(res.error()));
if (const auto& hint = res.error().hint(); !hint.empty())
mu_info("hint: {}", hint);
}

return res.error().exit_code();
}
Expand Down

0 comments on commit f813498

Please sign in to comment.