From 3253954233c378a0176eb9695fa7908c1ca21c2e Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Tue, 13 Oct 2009 20:42:08 -0700 Subject: [PATCH] Move index_file out from main() into its own function. This is a step toward having a program that will index many messages with a single invocation. --- notmuch-index-message.cc | 144 +++++++++++++++++++++------------------ 1 file changed, 77 insertions(+), 67 deletions(-) diff --git a/notmuch-index-message.cc b/notmuch-index-message.cc index 7fb5a019..79b3bf38 100644 --- a/notmuch-index-message.cc +++ b/notmuch-index-message.cc @@ -319,16 +319,18 @@ gen_terms_body (Xapian::TermGenerator term_gen, g_io_channel_close (channel); } - -int -main (int argc, char **argv) +static void +index_file (Xapian::WritableDatabase db, + Xapian::TermGenerator term_gen, + const char *filename) { + Xapian::Document doc; + GMimeStream *stream; GMimeParser *parser; GMimeMessage *message; InternetAddressList *addresses; - const char *database_path, *filename; FILE *file; const char *value, *from; @@ -337,109 +339,117 @@ main (int argc, char **argv) struct tm gm_time_tm; char date_str[16]; /* YYYYMMDDHHMMSS + 1 for Y100k compatibility ;-) */ - if (argc < 3) { - fprintf (stderr, "Usage: %s \n", - argv[0]); - exit (1); - } - - database_path = argv[1]; - filename = argv[2]; - file = fopen (filename, "r"); if (! file) { fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno)); exit (1); } - g_mime_init (0); - stream = g_mime_stream_file_new (file); parser = g_mime_parser_new_with_stream (stream); message = g_mime_parser_construct_message (parser); - try { - Xapian::WritableDatabase db; - Xapian::TermGenerator term_gen; - Xapian::Document doc; + doc = Xapian::Document (); - doc = Xapian::Document (); + doc.set_data (filename); - doc.set_data (filename); + term_gen.set_stemmer (Xapian::Stem ("english")); - db = Xapian::WritableDatabase (database_path, - Xapian::DB_CREATE_OR_OPEN); + term_gen.set_document (doc); - term_gen = Xapian::TermGenerator (); - term_gen.set_stemmer (Xapian::Stem ("english")); + from = g_mime_message_get_sender (message); + addresses = internet_address_list_parse_string (from); - term_gen.set_document (doc); + gen_terms_address_names (term_gen, addresses, "from_name"); - from = g_mime_message_get_sender (message); - addresses = internet_address_list_parse_string (from); + addresses = g_mime_message_get_all_recipients (message); + gen_terms_address_names (term_gen, addresses, "to_name"); - gen_terms_address_names (term_gen, addresses, "from_name"); + value = g_mime_message_get_subject (message); + value = skip_re_in_subject (value); + gen_terms (term_gen, "subject", value); + gen_terms (term_gen, "body", value); - addresses = g_mime_message_get_all_recipients (message); - gen_terms_address_names (term_gen, addresses, "to_name"); + gen_terms_body (term_gen, filename, + g_mime_parser_get_headers_end (parser)); - value = g_mime_message_get_subject (message); - value = skip_re_in_subject (value); - gen_terms (term_gen, "subject", value); - gen_terms (term_gen, "body", value); + add_terms_references (doc, message); - gen_terms_body (term_gen, filename, - g_mime_parser_get_headers_end (parser)); + from = g_mime_message_get_sender (message); + addresses = internet_address_list_parse_string (from); - add_terms_references (doc, message); + add_terms_address_addrs (doc, addresses, "from_email"); - from = g_mime_message_get_sender (message); - addresses = internet_address_list_parse_string (from); + add_terms_address_addrs (doc, + g_mime_message_get_all_recipients (message), + "to_email"); - add_terms_address_addrs (doc, addresses, "from_email"); + g_mime_message_get_date (message, &time, NULL); - add_terms_address_addrs (doc, - g_mime_message_get_all_recipients (message), - "to_email"); + gmtime_r (&time, &gm_time_tm); - g_mime_message_get_date (message, &time, NULL); + if (strftime (date_str, sizeof (date_str), + "%Y%m%d%H%M%S", &gm_time_tm) == 0) { + fprintf (stderr, "Internal error formatting time\n"); + exit (1); + } - gmtime_r (&time, &gm_time_tm); + add_term (doc, "date", date_str); - if (strftime (date_str, sizeof (date_str), - "%Y%m%d%H%M%S", &gm_time_tm) == 0) { - fprintf (stderr, "Internal error formatting time\n"); - exit (1); - } + add_term (doc, "label", "inbox"); + add_term (doc, "label", "unread"); + add_term (doc, "type", "mail"); + add_term (doc, "source_id", "1"); + + value = g_mime_message_get_message_id (message); + add_term (doc, "msgid", value); + add_term (doc, "thread", value); + + doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, value); + doc.add_value (NOTMUCH_VALUE_THREAD, value); + + doc.add_value (NOTMUCH_VALUE_DATE, Xapian::sortable_serialise (time)); - add_term (doc, "date", date_str); + db.add_document (doc); - add_term (doc, "label", "inbox"); - add_term (doc, "label", "unread"); - add_term (doc, "type", "mail"); - add_term (doc, "source_id", "1"); + g_object_unref (message); + g_object_unref (parser); + g_object_unref (stream); +} + +int +main (int argc, char **argv) +{ + const char *database_path, *filename; + + if (argc < 3) { + fprintf (stderr, "Usage: %s \n", + argv[0]); + exit (1); + } - value = g_mime_message_get_message_id (message); - add_term (doc, "msgid", value); - add_term (doc, "thread", value); + database_path = argv[1]; + filename = argv[2]; - doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, value); - doc.add_value (NOTMUCH_VALUE_THREAD, value); + g_mime_init (0); - doc.add_value (NOTMUCH_VALUE_DATE, Xapian::sortable_serialise (time)); + try { + Xapian::WritableDatabase db; + Xapian::TermGenerator term_gen; - db.add_document (doc); + db = Xapian::WritableDatabase (database_path, + Xapian::DB_CREATE_OR_OPEN); + + term_gen = Xapian::TermGenerator (); + + index_file (db, term_gen, filename); } catch (const Xapian::Error &error) { cerr << "A Xapian exception occurred: " << error.get_msg () << endl; exit (1); } - g_object_unref (message); - g_object_unref (parser); - g_object_unref (stream); - return 0; } -- 2.26.2