X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=lib%2Fdirectory.cc;h=16492c0d754d9bf6ccfaf46c3ed96b2519410c50;hp=461c0cc367801b72819f429013a214552efc279d;hb=d87db8843266caf6b11c1f2f1874328830b23878;hpb=f93b7218c3e2d11c5b3cdd4c367a42ca7a7ede77 diff --git a/lib/directory.cc b/lib/directory.cc index 461c0cc3..16492c0d 100644 --- a/lib/directory.cc +++ b/lib/directory.cc @@ -21,30 +21,6 @@ #include "notmuch-private.h" #include "database-private.h" -#include - -struct _notmuch_filenames { - Xapian::TermIterator iterator; - Xapian::TermIterator end; - int prefix_len; - char *filename; -}; - -/* We end up having to call the destructors explicitly because we had - * to use "placement new" in order to initialize C++ objects within a - * block that we allocated with talloc. So C++ is making talloc - * slightly less simple to use, (we wouldn't need - * talloc_set_destructor at all otherwise). - */ -static int -_notmuch_filenames_destructor (notmuch_filenames_t *filenames) -{ - filenames->iterator.~TermIterator (); - filenames->end.~TermIterator (); - - return 0; -} - /* Create an iterator to iterate over the basenames of files (or * directories) that all share a common parent directory. * @@ -52,80 +28,32 @@ _notmuch_filenames_destructor (notmuch_filenames_t *filenames) * iterating over the non-prefixed portion of terms sharing a common * prefix. */ -notmuch_filenames_t * -_notmuch_filenames_create (void *ctx, - notmuch_database_t *notmuch, - const char *prefix) +static notmuch_filenames_t * +_create_filenames_for_terms_with_prefix (void *ctx, + notmuch_database_t *notmuch, + const char *prefix) { notmuch_filenames_t *filenames; + Xapian::TermIterator i, end; + int prefix_len = strlen (prefix); - filenames = talloc (ctx, notmuch_filenames_t); + filenames = _notmuch_filenames_create (ctx); if (unlikely (filenames == NULL)) return NULL; - new (&filenames->iterator) Xapian::TermIterator (); - new (&filenames->end) Xapian::TermIterator (); - - talloc_set_destructor (filenames, _notmuch_filenames_destructor); - - filenames->iterator = notmuch->xapian_db->allterms_begin (prefix); - filenames->end = notmuch->xapian_db->allterms_end (prefix); - - filenames->prefix_len = strlen (prefix); - - filenames->filename = NULL; - - return filenames; -} - -notmuch_bool_t -notmuch_filenames_has_more (notmuch_filenames_t *filenames) -{ - if (filenames == NULL) - return NULL; - - return (filenames->iterator != filenames->end); -} + end = notmuch->xapian_db->allterms_end (prefix); -const char * -notmuch_filenames_get (notmuch_filenames_t *filenames) -{ - if (filenames == NULL || filenames->iterator == filenames->end) - return NULL; - - if (filenames->filename == NULL) { - std::string term = *filenames->iterator; + for (i = notmuch->xapian_db->allterms_begin (prefix); i != end; i++) + { + std::string term = *i; - filenames->filename = talloc_strdup (filenames, - term.c_str () + - filenames->prefix_len); + _notmuch_filenames_add_filename (filenames, term.c_str () + + prefix_len); } - return filenames->filename; -} + _notmuch_filenames_move_to_first (filenames); -void -notmuch_filenames_advance (notmuch_filenames_t *filenames) -{ - if (filenames == NULL) - return; - - if (filenames->filename) { - talloc_free (filenames->filename); - filenames->filename = NULL; - } - - if (filenames->iterator != filenames->end) - filenames->iterator++; -} - -void -notmuch_filenames_destroy (notmuch_filenames_t *filenames) -{ - if (filenames == NULL) - return; - - talloc_free (filenames); + return filenames; } struct _notmuch_directory { @@ -213,7 +141,7 @@ _notmuch_directory_create (notmuch_database_t *notmuch, Xapian::docid parent_id; char *term = talloc_asprintf (local, "%s%s", _find_prefix ("directory"), db_path); - directory->doc.add_term (term); + directory->doc.add_term (term, 0); directory->doc.set_data (path); @@ -225,13 +153,14 @@ _notmuch_directory_create (notmuch_database_t *notmuch, term = talloc_asprintf (local, "%s%u:%s", _find_prefix ("directory-direntry"), parent_id, basename); - directory->doc.add_term (term); + directory->doc.add_term (term, 0); } directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP, Xapian::sortable_serialise (0)); - directory->document_id = db->add_document (directory->doc); + directory->document_id = _notmuch_database_generate_doc_id (notmuch); + db->replace_document (directory->document_id, directory->doc); talloc_free (local); } @@ -305,8 +234,9 @@ notmuch_directory_get_child_files (notmuch_directory_t *directory) _find_prefix ("file-direntry"), directory->document_id); - child_files = _notmuch_filenames_create (directory, - directory->notmuch, term); + child_files = _create_filenames_for_terms_with_prefix (directory, + directory->notmuch, + term); talloc_free (term); @@ -323,8 +253,8 @@ notmuch_directory_get_child_directories (notmuch_directory_t *directory) _find_prefix ("directory-direntry"), directory->document_id); - child_directories = _notmuch_filenames_create (directory, - directory->notmuch, term); + child_directories = _create_filenames_for_terms_with_prefix (directory, + directory->notmuch, term); talloc_free (term);