dovecot-2.2: dict-redis: Added support for changing the Redis da...

dovecot at dovecot.org dovecot at dovecot.org
Mon Mar 2 13:08:26 UTC 2015


details:   http://hg.dovecot.org/dovecot-2.2/rev/62e4062302de
changeset: 18282:62e4062302de
user:      Timo Sirainen <tss at iki.fi>
date:      Mon Mar 02 15:07:20 2015 +0200
description:
dict-redis: Added support for changing the Redis database.
Adding :db=N to the dict string specifies the database.

diffstat:

 src/lib-dict/dict-redis.c |  34 +++++++++++++++++++++++++++++++++-
 1 files changed, 33 insertions(+), 1 deletions(-)

diffs (100 lines):

diff -r 98302e52074e -r 62e4062302de src/lib-dict/dict-redis.c
--- a/src/lib-dict/dict-redis.c	Mon Mar 02 14:41:11 2015 +0200
+++ b/src/lib-dict/dict-redis.c	Mon Mar 02 15:07:20 2015 +0200
@@ -13,6 +13,8 @@
 #define DICT_USERNAME_SEPARATOR '/'
 
 enum redis_input_state {
+	/* expecting +OK reply for SELECT */
+	REDIS_INPUT_STATE_SELECT,
 	/* expecting $-1 / $<size> followed by GET reply */
 	REDIS_INPUT_STATE_GET,
 	/* expecting +QUEUED */
@@ -44,7 +46,7 @@
 struct redis_dict {
 	struct dict dict;
 	char *username, *key_prefix, *expire_value;
-	unsigned int timeout_msecs;
+	unsigned int timeout_msecs, db_id;
 
 	struct ioloop *ioloop, *prev_ioloop;
 	struct redis_connection conn;
@@ -54,6 +56,7 @@
 
 	bool connected;
 	bool transaction_open;
+	bool db_id_set;
 };
 
 struct redis_dict_transaction_context {
@@ -96,6 +99,7 @@
 	struct redis_connection *conn = (struct redis_connection *)_conn;
 	const struct redis_dict_reply *reply;
 
+	conn->dict->db_id_set = FALSE;
 	conn->dict->connected = FALSE;
 	connection_disconnect(_conn);
 
@@ -215,6 +219,7 @@
 	switch (state) {
 	case REDIS_INPUT_STATE_GET:
 		i_unreached();
+	case REDIS_INPUT_STATE_SELECT:
 	case REDIS_INPUT_STATE_MULTI:
 	case REDIS_INPUT_STATE_DISCARD:
 		if (line[0] != '+')
@@ -362,6 +367,12 @@
 		} else if (strncmp(*args, "prefix=", 7) == 0) {
 			i_free(dict->key_prefix);
 			dict->key_prefix = i_strdup(*args + 7);
+		} else if (strncmp(*args, "db=", 3) == 0) {
+			if (str_to_uint(*args+3, &dict->db_id) < 0) {
+				*error_r = t_strdup_printf(
+					"Invalid db number: %s", *args+3);
+				ret = -1;
+			}
 		} else if (strncmp(*args, "expire_secs=", 12) == 0) {
 			const char *value = *args + 12;
 
@@ -458,6 +469,24 @@
 	return key;
 }
 
+static void redis_dict_select_db(struct redis_dict *dict)
+{
+	const char *cmd, *db_str;
+
+	if (dict->db_id_set)
+		return;
+	dict->db_id_set = TRUE;
+	if (dict->db_id == 0) {
+		/* 0 is the default */
+		return;
+	}
+	db_str = dec2str(dict->db_id);
+	cmd = t_strdup_printf("*2\r\n$6\r\nSELECT\r\n$%d\r\n%s\r\n",
+			      (int)strlen(db_str), db_str);
+	o_stream_nsend_str(dict->conn.conn.output, cmd);
+	redis_input_state_add(dict, REDIS_INPUT_STATE_SELECT);
+}
+
 static int
 redis_dict_lookup_real(struct redis_dict *dict, pool_t pool,
 		       const char *key, const char **value_r)
@@ -488,6 +517,7 @@
 		}
 
 		if (dict->connected) {
+			redis_dict_select_db(dict);
 			cmd = t_strdup_printf("*2\r\n$3\r\nGET\r\n$%d\r\n%s\r\n",
 					      (int)strlen(key), key);
 			o_stream_nsend_str(dict->conn.conn.output, cmd);
@@ -556,6 +586,8 @@
 		/* wait for connection */
 		redis_wait(dict);
 	}
+	if (dict->connected)
+		redis_dict_select_db(dict);
 	return &ctx->ctx;
 }
 


More information about the dovecot-cvs mailing list