1 Return-Path: <bremner@tethera.net>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by arlo.cworth.org (Postfix) with ESMTP id 1459D6DE0955
\r
6 for <notmuch@notmuchmail.org>; Sat, 7 May 2016 17:05:36 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org
\r
11 X-Spam-Status: No, score=-0.013 tagged_above=-999 required=5
\r
12 tests=[AWL=-0.002, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01]
\r
14 Received: from arlo.cworth.org ([127.0.0.1])
\r
15 by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)
\r
16 with ESMTP id 4Ux4tA22Ae13 for <notmuch@notmuchmail.org>;
\r
17 Sat, 7 May 2016 17:05:28 -0700 (PDT)
\r
18 Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])
\r
19 by arlo.cworth.org (Postfix) with ESMTPS id D6BC96DE02C6
\r
20 for <notmuch@notmuchmail.org>; Sat, 7 May 2016 17:05:03 -0700 (PDT)
\r
21 Received: from remotemail by fethera.tethera.net with local (Exim 4.84)
\r
22 (envelope-from <bremner@tethera.net>)
\r
23 id 1azCDR-00014w-RM; Sat, 07 May 2016 20:04:57 -0400
\r
24 Received: (nullmailer pid 17193 invoked by uid 1000);
\r
25 Sun, 08 May 2016 00:04:53 -0000
\r
26 From: David Bremner <david@tethera.net>
\r
27 To: notmuch@notmuchmail.org
\r
28 Subject: [Patch v4 06/12] lib: config list iterators
\r
29 Date: Sat, 7 May 2016 21:04:43 -0300
\r
30 Message-Id: <1462665889-17121-7-git-send-email-david@tethera.net>
\r
31 X-Mailer: git-send-email 2.8.1
\r
32 In-Reply-To: <1462665889-17121-1-git-send-email-david@tethera.net>
\r
33 References: <1462665889-17121-1-git-send-email-david@tethera.net>
\r
34 X-BeenThere: notmuch@notmuchmail.org
\r
35 X-Mailman-Version: 2.1.20
\r
37 List-Id: "Use and development of the notmuch mail system."
\r
38 <notmuch.notmuchmail.org>
\r
39 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,
\r
40 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
41 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>
\r
42 List-Post: <mailto:notmuch@notmuchmail.org>
\r
43 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
44 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,
\r
45 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
46 X-List-Received-Date: Sun, 08 May 2016 00:05:36 -0000
\r
48 Since xapian provides the ability to restrict the iterator to a given
\r
49 prefix, we expose this ability to the user. Otherwise we mimic the other
\r
50 iterator interfances in notmuch (e.g. tags.c).
\r
52 lib/config.cc | 105 +++++++++++++++++++++++++++++++++++++++++++++++++
\r
53 lib/notmuch.h | 44 +++++++++++++++++++++
\r
54 test/T590-libconfig.sh | 60 ++++++++++++++++++++++++++++
\r
55 3 files changed, 209 insertions(+)
\r
57 diff --git a/lib/config.cc b/lib/config.cc
\r
58 index ce8fb31..35c917b 100644
\r
63 static const std::string CONFIG_PREFIX = "C";
\r
65 +struct _notmuch_config_list {
\r
66 + notmuch_database_t *notmuch;
\r
67 + Xapian::TermIterator *iterator;
\r
68 + char *current_key;
\r
69 + char *current_val;
\r
73 +_notmuch_config_list_destroy (notmuch_config_list_t *list)
\r
75 + delete list->iterator;
\r
80 notmuch_database_set_config (notmuch_database_t *notmuch,
\r
82 @@ -89,3 +103,94 @@ notmuch_database_get_config (notmuch_database_t *notmuch,
\r
84 return NOTMUCH_STATUS_SUCCESS;
\r
88 +notmuch_database_get_config_list (notmuch_database_t *notmuch,
\r
89 + const char *prefix,
\r
90 + notmuch_config_list_t **out)
\r
92 + notmuch_config_list_t *list = NULL;
\r
93 + notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
\r
95 + list = talloc (notmuch, notmuch_config_list_t);
\r
97 + status = NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
101 + talloc_set_destructor (list, _notmuch_config_list_destroy);
\r
102 + list->iterator = new Xapian::TermIterator;
\r
103 + list->notmuch = notmuch;
\r
104 + list->current_key = NULL;
\r
105 + list->current_val = NULL;
\r
109 + *list->iterator = notmuch->xapian_db->metadata_keys_begin (CONFIG_PREFIX + (prefix ? prefix : ""));
\r
111 + } catch (const Xapian::Error &error) {
\r
112 + _notmuch_database_log (notmuch, "A Xapian exception occurred getting metadata iterator: %s.\n",
\r
113 + error.get_msg().c_str());
\r
114 + notmuch->exception_reported = TRUE;
\r
115 + status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
\r
121 + if (status && list)
\r
122 + talloc_free (list);
\r
128 +notmuch_config_list_valid (notmuch_config_list_t *metadata)
\r
130 + if (*(metadata->iterator) == metadata->notmuch->xapian_db->metadata_keys_end ())
\r
137 +notmuch_config_list_key (notmuch_config_list_t *list)
\r
139 + if (list->current_key)
\r
140 + talloc_free (list->current_key);
\r
142 + list->current_key = talloc_strdup (list, (**(list->iterator)).c_str () + CONFIG_PREFIX.length ());
\r
144 + return list->current_key;
\r
148 +notmuch_config_list_value (notmuch_config_list_t *list)
\r
150 + std::string strval;
\r
151 + notmuch_status_t status;
\r
152 + const char *key = notmuch_config_list_key (list);
\r
154 + /* TODO: better error reporting?? */
\r
155 + status = _metadata_value (list->notmuch, key, strval);
\r
159 + if (list->current_val)
\r
160 + talloc_free (list->current_val);
\r
162 + list->current_val = talloc_strdup (list, strval.c_str ());
\r
163 + return list->current_val;
\r
167 +notmuch_config_list_move_to_next (notmuch_config_list_t *list)
\r
169 + (*(list->iterator))++;
\r
173 +notmuch_config_list_destroy (notmuch_config_list_t *list)
\r
175 + talloc_free (list);
\r
177 diff --git a/lib/notmuch.h b/lib/notmuch.h
\r
178 index c827e02..bd977c3 100644
\r
179 --- a/lib/notmuch.h
\r
180 +++ b/lib/notmuch.h
\r
181 @@ -206,6 +206,7 @@ typedef struct _notmuch_message notmuch_message_t;
\r
182 typedef struct _notmuch_tags notmuch_tags_t;
\r
183 typedef struct _notmuch_directory notmuch_directory_t;
\r
184 typedef struct _notmuch_filenames notmuch_filenames_t;
\r
185 +typedef struct _notmuch_config_list notmuch_config_list_t;
\r
186 #endif /* __DOXYGEN__ */
\r
189 @@ -1859,6 +1860,49 @@ notmuch_status_t
\r
190 notmuch_database_get_config (notmuch_database_t *db, const char *key, char **value);
\r
193 + * Create an iterator for all config items with keys matching a given prefix
\r
196 +notmuch_database_get_config_list (notmuch_database_t *db, const char *prefix, notmuch_config_list_t **out);
\r
199 + * Is 'config_list' iterator valid (i.e. _key, _value, _move_to_next can be called).
\r
202 +notmuch_config_list_valid (notmuch_config_list_t *config_list);
\r
205 + * return key for current config pair
\r
207 + * return value is owned by the iterator, and will be destroyed by the
\r
208 + * next call to notmuch_config_list_key or notmuch_config_list_destroy.
\r
211 +notmuch_config_list_key (notmuch_config_list_t *config_list);
\r
214 + * return 'value' for current config pair
\r
216 + * return value is owned by the iterator, and will be destroyed by the
\r
217 + * next call to notmuch_config_list_value or notmuch config_list_destroy
\r
220 +notmuch_config_list_value (notmuch_config_list_t *config_list);
\r
224 + * move 'config_list' iterator to the next pair
\r
227 +notmuch_config_list_move_to_next (notmuch_config_list_t *config_list);
\r
230 + * free any resources held by 'config_list'
\r
233 +notmuch_config_list_destroy (notmuch_config_list_t *config_list);
\r
236 * interrogate the library for compile time features
\r
239 diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh
\r
240 index 85e4497..8ca6883 100755
\r
241 --- a/test/T590-libconfig.sh
\r
242 +++ b/test/T590-libconfig.sh
\r
243 @@ -55,4 +55,64 @@ testkey2 = testvalue2
\r
245 test_expect_equal_file EXPECTED OUTPUT
\r
248 +test_begin_subtest "notmuch_database_get_config_list: empty list"
\r
249 +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
\r
251 + notmuch_config_list_t *list;
\r
252 + RUN(notmuch_database_get_config_list (db, "nonexistent", &list));
\r
253 + printf("valid = %d\n", notmuch_config_list_valid (list));
\r
254 + notmuch_config_list_destroy (list);
\r
257 +cat <<'EOF' >EXPECTED
\r
262 +test_expect_equal_file EXPECTED OUTPUT
\r
265 +test_begin_subtest "notmuch_database_get_config_list: all pairs"
\r
266 +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
\r
268 + notmuch_config_list_t *list;
\r
269 + RUN(notmuch_database_set_config (db, "zzzafter", "afterval"));
\r
270 + RUN(notmuch_database_set_config (db, "aaabefore", "beforeval"));
\r
271 + RUN(notmuch_database_get_config_list (db, "", &list));
\r
272 + for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
\r
273 + printf("%s %s\n", notmuch_config_list_key (list), notmuch_config_list_value(list));
\r
275 + notmuch_config_list_destroy (list);
\r
278 +cat <<'EOF' >EXPECTED
\r
280 +aaabefore beforeval
\r
281 +testkey1 testvalue1
\r
282 +testkey2 testvalue2
\r
286 +test_expect_equal_file EXPECTED OUTPUT
\r
288 +test_begin_subtest "notmuch_database_get_config_list: one prefix"
\r
289 +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
\r
291 + notmuch_config_list_t *list;
\r
292 + RUN(notmuch_database_get_config_list (db, "testkey", &list));
\r
293 + for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
\r
294 + printf("%s %s\n", notmuch_config_list_key (list), notmuch_config_list_value(list));
\r
296 + notmuch_config_list_destroy (list);
\r
299 +cat <<'EOF' >EXPECTED
\r
301 +testkey1 testvalue1
\r
302 +testkey2 testvalue2
\r
305 +test_expect_equal_file EXPECTED OUTPUT
\r