aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/directory.cc
diff options
context:
space:
mode:
authorGravatar Carl Worth <cworth@cworth.org>2010-11-10 23:26:31 -0800
committerGravatar Carl Worth <cworth@cworth.org>2010-11-11 03:40:19 -0800
commitd87db8843266caf6b11c1f2f1874328830b23878 (patch)
tree2c3fc3cdeb793a70ffe48ef88614861a3e076baa /lib/directory.cc
parentd422dcf0a276f2806d5563fbdc48fa7d69e4c3eb (diff)
lib: Add new implementation of notmuch_filenames_t
The new implementation is simply a talloc-based list of strings. The former support (a list of database terms with a common prefix) is implemented by simply pre-iterating over the terms and populating the list. This should provide no performance disadvantage as callers of thigns like notmuch_directory_get_child_files are very likely to always iterate over all filenames anyway. This new implementation of notmuch_filenames_t is in preparation for adding API to query all of the filenames for a single message.
Diffstat (limited to 'lib/directory.cc')
-rw-r--r--lib/directory.cc107
1 files changed, 19 insertions, 88 deletions
diff --git a/lib/directory.cc b/lib/directory.cc
index 2540ca76..16492c0d 100644
--- a/lib/directory.cc
+++ b/lib/directory.cc
@@ -21,28 +21,6 @@
#include "notmuch-private.h"
#include "database-private.h"
-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.
*
@@ -51,79 +29,31 @@ _notmuch_filenames_destructor (notmuch_filenames_t *filenames)
* prefix.
*/
static notmuch_filenames_t *
-_notmuch_filenames_create (void *ctx,
- notmuch_database_t *notmuch,
- const char *prefix)
+_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_valid (notmuch_filenames_t *filenames)
-{
- if (filenames == NULL)
- return NULL;
-
- return (filenames->iterator != filenames->end);
-}
-
-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;
-
- filenames->filename = talloc_strdup (filenames,
- term.c_str () +
- filenames->prefix_len);
- }
-
- return filenames->filename;
-}
+ end = notmuch->xapian_db->allterms_end (prefix);
-void
-notmuch_filenames_move_to_next (notmuch_filenames_t *filenames)
-{
- if (filenames == NULL)
- return;
+ for (i = notmuch->xapian_db->allterms_begin (prefix); i != end; i++)
+ {
+ std::string term = *i;
- if (filenames->filename) {
- talloc_free (filenames->filename);
- filenames->filename = NULL;
+ _notmuch_filenames_add_filename (filenames, term.c_str () +
+ prefix_len);
}
- if (filenames->iterator != filenames->end)
- filenames->iterator++;
-}
+ _notmuch_filenames_move_to_first (filenames);
-void
-notmuch_filenames_destroy (notmuch_filenames_t *filenames)
-{
- if (filenames == NULL)
- return;
-
- talloc_free (filenames);
+ return filenames;
}
struct _notmuch_directory {
@@ -304,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);
@@ -322,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);