mirror of
https://git.notmuchmail.org/git/notmuch
synced 2024-11-21 18:38:08 +01:00
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.
This commit is contained in:
parent
d422dcf0a2
commit
d87db88432
5 changed files with 144 additions and 90 deletions
|
@ -47,6 +47,7 @@ extra_cflags += -I$(dir) -fPIC
|
|||
|
||||
libnotmuch_c_srcs = \
|
||||
$(notmuch_compat_srcs) \
|
||||
$(dir)/filenames.c \
|
||||
$(dir)/libsha1.c \
|
||||
$(dir)/message-file.c \
|
||||
$(dir)/messages.c \
|
||||
|
|
101
lib/directory.cc
101
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,81 +29,33 @@ _notmuch_filenames_destructor (notmuch_filenames_t *filenames)
|
|||
* prefix.
|
||||
*/
|
||||
static notmuch_filenames_t *
|
||||
_notmuch_filenames_create (void *ctx,
|
||||
_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 ();
|
||||
end = notmuch->xapian_db->allterms_end (prefix);
|
||||
|
||||
talloc_set_destructor (filenames, _notmuch_filenames_destructor);
|
||||
for (i = notmuch->xapian_db->allterms_begin (prefix); i != end; i++)
|
||||
{
|
||||
std::string term = *i;
|
||||
|
||||
filenames->iterator = notmuch->xapian_db->allterms_begin (prefix);
|
||||
filenames->end = notmuch->xapian_db->allterms_end (prefix);
|
||||
_notmuch_filenames_add_filename (filenames, term.c_str () +
|
||||
prefix_len);
|
||||
}
|
||||
|
||||
filenames->prefix_len = strlen (prefix);
|
||||
|
||||
filenames->filename = NULL;
|
||||
_notmuch_filenames_move_to_first (filenames);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void
|
||||
notmuch_filenames_move_to_next (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);
|
||||
}
|
||||
|
||||
struct _notmuch_directory {
|
||||
notmuch_database_t *notmuch;
|
||||
Xapian::docid document_id;
|
||||
|
@ -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,7 +253,7 @@ notmuch_directory_get_child_directories (notmuch_directory_t *directory)
|
|||
_find_prefix ("directory-direntry"),
|
||||
directory->document_id);
|
||||
|
||||
child_directories = _notmuch_filenames_create (directory,
|
||||
child_directories = _create_filenames_for_terms_with_prefix (directory,
|
||||
directory->notmuch, term);
|
||||
|
||||
talloc_free (term);
|
||||
|
|
111
lib/filenames.c
Normal file
111
lib/filenames.c
Normal file
|
@ -0,0 +1,111 @@
|
|||
/* filenames.c - Iterator for a list of filenames
|
||||
*
|
||||
* Copyright © 2010 Intel Corporation
|
||||
*
|
||||
* 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 Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see http://www.gnu.org/licenses/ .
|
||||
*
|
||||
* Author: Carl Worth <cworth@cworth.org>
|
||||
*/
|
||||
|
||||
#include "notmuch-private.h"
|
||||
|
||||
typedef struct _notmuch_filenames_node {
|
||||
char *filename;
|
||||
struct _notmuch_filenames_node *next;
|
||||
} notmuch_filenames_node_t;
|
||||
|
||||
struct _notmuch_filenames {
|
||||
notmuch_filenames_node_t *head;
|
||||
notmuch_filenames_node_t **tail;
|
||||
notmuch_filenames_node_t *iterator;
|
||||
};
|
||||
|
||||
/* Create a new notmuch_filenames_t object, with 'ctx' as its
|
||||
* talloc owner.
|
||||
*
|
||||
* This function can return NULL in case of out-of-memory.
|
||||
*/
|
||||
notmuch_filenames_t *
|
||||
_notmuch_filenames_create (const void *ctx)
|
||||
{
|
||||
notmuch_filenames_t *filenames;
|
||||
|
||||
filenames = talloc (ctx, notmuch_filenames_t);
|
||||
if (unlikely (filenames == NULL))
|
||||
return NULL;
|
||||
|
||||
filenames->head = NULL;
|
||||
filenames->tail = &filenames->head;
|
||||
|
||||
filenames->iterator = NULL;
|
||||
|
||||
return filenames;
|
||||
}
|
||||
|
||||
/* Append a single 'node' to the end of 'filenames'.
|
||||
*/
|
||||
void
|
||||
_notmuch_filenames_add_filename (notmuch_filenames_t *filenames,
|
||||
const char *filename)
|
||||
{
|
||||
/* Create and initialize new node. */
|
||||
notmuch_filenames_node_t *node = talloc (filenames,
|
||||
notmuch_filenames_node_t);
|
||||
|
||||
node->filename = talloc_strdup (node, filename);
|
||||
node->next = NULL;
|
||||
|
||||
/* Append the node to the list. */
|
||||
*(filenames->tail) = node;
|
||||
filenames->tail = &node->next;
|
||||
}
|
||||
|
||||
notmuch_bool_t
|
||||
notmuch_filenames_valid (notmuch_filenames_t *filenames)
|
||||
{
|
||||
if (filenames == NULL)
|
||||
return FALSE;
|
||||
|
||||
return (filenames->iterator != NULL);
|
||||
}
|
||||
|
||||
const char *
|
||||
notmuch_filenames_get (notmuch_filenames_t *filenames)
|
||||
{
|
||||
if (filenames->iterator == NULL)
|
||||
return NULL;
|
||||
|
||||
return filenames->iterator->filename;
|
||||
}
|
||||
|
||||
void
|
||||
_notmuch_filenames_move_to_first (notmuch_filenames_t *filenames)
|
||||
{
|
||||
filenames->iterator = filenames->head;
|
||||
}
|
||||
|
||||
void
|
||||
notmuch_filenames_move_to_next (notmuch_filenames_t *filenames)
|
||||
{
|
||||
if (filenames->iterator == NULL)
|
||||
return;
|
||||
|
||||
filenames->iterator = filenames->iterator->next;
|
||||
}
|
||||
|
||||
void
|
||||
notmuch_filenames_destroy (notmuch_filenames_t *filenames)
|
||||
{
|
||||
talloc_free (filenames);
|
||||
}
|
|
@ -42,8 +42,7 @@ _notmuch_message_list_create (const void *ctx)
|
|||
return list;
|
||||
}
|
||||
|
||||
/* Append 'node' (which can of course point to an arbitrarily long
|
||||
* list of nodes) to the end of 'list'.
|
||||
/* Append a single 'node' to the end of 'list'.
|
||||
*/
|
||||
void
|
||||
_notmuch_message_list_append (notmuch_message_list_t *list,
|
||||
|
|
|
@ -448,6 +448,18 @@ _notmuch_tags_add_tag (notmuch_tags_t *tags, const char *tag);
|
|||
void
|
||||
_notmuch_tags_prepare_iterator (notmuch_tags_t *tags);
|
||||
|
||||
/* filenames.c */
|
||||
|
||||
notmuch_filenames_t *
|
||||
_notmuch_filenames_create (const void *ctx);
|
||||
|
||||
void
|
||||
_notmuch_filenames_add_filename (notmuch_filenames_t *filenames,
|
||||
const char *filename);
|
||||
|
||||
void
|
||||
_notmuch_filenames_move_to_first (notmuch_filenames_t *filenames);
|
||||
|
||||
#pragma GCC visibility pop
|
||||
|
||||
NOTMUCH_END_DECLS
|
||||
|
|
Loading…
Reference in a new issue