From: David Bremner <david@tethera.net>
To: notmuch@notmuchmail.org
Cc: David Bremner <david@tethera.net>
Subject: [PATCH 05/19] WIP: add notmuch_config_set
Date: Sat, 8 Aug 2020 11:16:39 -0300 [thread overview]
Message-ID: <20200808141653.1124111-6-david@tethera.net> (raw)
In-Reply-To: <20200808141653.1124111-1-david@tethera.net>
To be decided: is the write_through paramater a good idea? Should we
simplify the API?
---
lib/config.cc | 16 ++++++++++++++++
lib/notmuch-private.h | 5 +++++
lib/notmuch.h | 5 +++++
lib/string-map.c | 16 ++++++++++++++++
test/T590-libconfig.sh | 30 ++++++++++++++++++++++++++++++
5 files changed, 72 insertions(+)
diff --git a/lib/config.cc b/lib/config.cc
index 0a91ec03..2cfd2882 100644
--- a/lib/config.cc
+++ b/lib/config.cc
@@ -225,3 +225,19 @@ notmuch_config_get (notmuch_database_t *notmuch, const char *key) {
return _notmuch_string_map_get (notmuch->config, key);
}
+
+notmuch_status_t
+notmuch_config_set (notmuch_database_t *notmuch,
+ const char *key,
+ const char *val,
+ notmuch_bool_t write_through)
+{
+ notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
+
+ _notmuch_string_map_set (notmuch->config, key, val);
+
+ if (write_through)
+ status = notmuch_database_set_config (notmuch, key, val);
+
+ return status;
+}
diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h
index 0f26b371..b545fc46 100644
--- a/lib/notmuch-private.h
+++ b/lib/notmuch-private.h
@@ -638,6 +638,11 @@ _notmuch_string_map_append (notmuch_string_map_t *map,
const char *key,
const char *value);
+void
+_notmuch_string_map_set (notmuch_string_map_t *map,
+ const char *key,
+ const char *value);
+
const char *
_notmuch_string_map_get (notmuch_string_map_t *map, const char *key);
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 777116bb..e147d5e6 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -2400,6 +2400,11 @@ notmuch_config_list_destroy (notmuch_config_list_t *config_list);
const char *
notmuch_config_get (notmuch_database_t *notmuch, const char *key);
+notmuch_status_t
+notmuch_config_set (notmuch_database_t *notmuch, const char *key,
+ const char *val,
+ notmuch_bool_t write_through);
+
/**
* get the current default indexing options for a given database.
*
diff --git a/lib/string-map.c b/lib/string-map.c
index a88404c7..9774dbe8 100644
--- a/lib/string-map.c
+++ b/lib/string-map.c
@@ -143,6 +143,22 @@ bsearch_first (notmuch_string_pair_t *array, size_t len, const char *key, bool e
}
+void
+_notmuch_string_map_set (notmuch_string_map_t *map, const char *key, const char *val)
+{
+ notmuch_string_pair_t *pair;
+
+ /* this means that calling string_map_set invalidates iterators */
+ _notmuch_string_map_sort (map);
+ pair = bsearch_first (map->pairs, map->length, key, true);
+ if (! pair)
+ _notmuch_string_map_append (map, key, val);
+ else {
+ talloc_free (pair->value);
+ pair->value = talloc_strdup (map->pairs, val);
+ }
+}
+
const char *
_notmuch_string_map_get (notmuch_string_map_t *map, const char *key)
{
diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh
index 8a4519e9..c2bce4a2 100755
--- a/test/T590-libconfig.sh
+++ b/test/T590-libconfig.sh
@@ -184,4 +184,34 @@ test.key2 = testvalue2
EOF
test_expect_equal_file EXPECTED OUTPUT
+backup_database
+test_begin_subtest "notmuch_config_set"
+cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
+{
+ char *val;
+ printf("test.key1 = %s\n", notmuch_config_get (db, "test.key1"));
+ EXPECT0(notmuch_config_set (db, "test.key1", "overridden", FALSE));
+ printf("test.key1 = %s\n", notmuch_config_get (db, "test.key1"));
+ printf("test.key2 = %s\n", notmuch_config_get (db, "test.key2"));
+ EXPECT0(notmuch_database_get_config (db, "test.key1", &val));
+ printf("test.key1 (db) = %s\n", val);
+ EXPECT0(notmuch_config_set (db, "test.key2", "overridden2", TRUE));
+ printf("test.key2 = %s\n", notmuch_config_get (db, "test.key2"));
+ EXPECT0(notmuch_database_get_config (db, "test.key2", &val));
+ printf("test.key2 (db) = %s\n", val);
+}
+EOF
+cat <<'EOF' >EXPECTED
+== stdout ==
+test.key1 = testvalue1
+test.key1 = overridden
+test.key2 = testvalue2
+test.key1 (db) = testvalue1
+test.key2 = overridden2
+test.key2 (db) = overridden2
+== stderr ==
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+restore_database
+
test_done
--
2.28.0
next prev parent reply other threads:[~2020-08-08 14:18 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-08 14:16 Initial implementation of merged config David Bremner
2020-08-08 14:16 ` [PATCH 01/19] test: use keys with group 'test' in T590-libconfig David Bremner
2020-08-08 14:16 ` [PATCH 02/19] lib: add stub for notmuch_database_open_with_config David Bremner
2020-08-09 7:19 ` Reto
2020-08-09 10:18 ` David Bremner
2020-08-08 14:16 ` [PATCH 03/19] lib: cache configuration information from database David Bremner
2020-08-08 14:16 ` [PATCH 04/19] WIP: add notmuch_config_get David Bremner
2020-08-08 14:16 ` David Bremner [this message]
2020-08-08 14:16 ` [PATCH 06/19] WIP: initial retrieval of database path from config file David Bremner
2020-08-08 14:16 ` [PATCH 07/19] test/libconfig; use n_database_open_with_config David Bremner
2020-08-08 14:16 ` [PATCH 08/19] WIP: add _notmuch_config_load_from_file David Bremner
2020-08-08 14:16 ` [PATCH 09/19] lib: factor out feature name related code David Bremner
2020-08-08 14:16 ` [PATCH 10/19] lib: factor out prefix related code to its own file David Bremner
2020-08-08 14:16 ` [PATCH 11/19] lib: factor out notmuch_database_open* related code to " David Bremner
2020-08-08 14:16 ` [PATCH 12/19] WIP: adding fallbacks for NULL config_path David Bremner
2020-08-08 14:16 ` [PATCH 13/19] lib/config: delay setting talloc destructor David Bremner
2020-08-08 14:16 ` [PATCH 14/19] WIP: add strsplit_len David Bremner
2020-08-08 14:16 ` [PATCH 15/19] WIP: add config values iterator David Bremner
2020-08-08 14:16 ` [PATCH 16/19] test: add regression test for searching with alternate config David Bremner
2020-08-08 14:16 ` [PATCH 17/19] cli/config: add accessor for config file name David Bremner
2020-08-08 14:16 ` [PATCH 18/19] WIP converting notmuch search to new style config David Bremner
2020-08-08 14:16 ` [PATCH 19/19] WIP: switch notmuch-show to new config framework David Bremner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200808141653.1124111-6-david@tethera.net \
--to=david@tethera.net \
--cc=notmuch@notmuchmail.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).