Skip to content

Commit

Permalink
dict-client: Support multiple values for lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
sirainen authored and GitLab committed Jan 15, 2017
1 parent 85b2346 commit 2176834
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
28 changes: 26 additions & 2 deletions src/dict/dict-commands.c
Expand Up @@ -162,15 +162,39 @@ dict_cmd_reply_handle_timings(struct dict_connection_cmd *cmd,
(unsigned int)ioloop_timeval.tv_usec);
}

static void
cmd_lookup_write_reply(struct dict_connection_cmd *cmd,
const char *const *values, string_t *str)
{
string_t *tmp;

i_assert(values[0] != NULL);

if (cmd->conn->minor_version < DICT_CLIENT_PROTOCOL_VERSION_MIN_MULTI_OK ||
values[1] == NULL) {
str_append_c(str, DICT_PROTOCOL_REPLY_OK);
str_append_tabescaped(str, values[0]);
return;
}
/* the results get double-tabescaped so they end up becoming a single
parameter */
tmp = t_str_new(128);
for (unsigned int i = 0; values[i] != NULL; i++) {
str_append_c(tmp, '\t');
str_append_tabescaped(tmp, values[i]);
}
str_append_c(str, DICT_PROTOCOL_REPLY_MULTI_OK);
str_append_tabescaped(str, str_c(tmp) + 1);
}

static void
cmd_lookup_callback(const struct dict_lookup_result *result, void *context)
{
struct dict_connection_cmd *cmd = context;
string_t *str = t_str_new(128);

if (result->ret > 0) {
str_append_c(str, DICT_PROTOCOL_REPLY_OK);
str_append_tabescaped(str, result->value);
cmd_lookup_write_reply(cmd, result->values, str);
} else if (result->ret == 0) {
str_append_c(str, DICT_PROTOCOL_REPLY_NOTFOUND);
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/lib-dict/dict-client.c
Expand Up @@ -938,6 +938,11 @@ client_dict_lookup_async_callback(struct client_dict_cmd *cmd,
result.values = values;
result.ret = 1;
break;
case DICT_PROTOCOL_REPLY_MULTI_OK:
result.values = t_strsplit_tabescaped(value);
result.value = result.values[0];
result.ret = 1;
break;
case DICT_PROTOCOL_REPLY_NOTFOUND:
result.ret = 0;
break;
Expand Down
5 changes: 4 additions & 1 deletion src/lib-dict/dict-client.h
Expand Up @@ -6,7 +6,9 @@
#define DEFAULT_DICT_SERVER_SOCKET_FNAME "dict"

#define DICT_CLIENT_PROTOCOL_MAJOR_VERSION 2
#define DICT_CLIENT_PROTOCOL_MINOR_VERSION 1
#define DICT_CLIENT_PROTOCOL_MINOR_VERSION 2

#define DICT_CLIENT_PROTOCOL_VERSION_MIN_MULTI_OK 2

#define DICT_CLIENT_MAX_LINE_LENGTH (64*1024)

Expand All @@ -32,6 +34,7 @@ enum dict_protocol_reply {
DICT_PROTOCOL_REPLY_ERROR = -1,

DICT_PROTOCOL_REPLY_OK = 'O', /* <value> */
DICT_PROTOCOL_REPLY_MULTI_OK = 'M', /* protocol v2.2+ */
DICT_PROTOCOL_REPLY_NOTFOUND = 'N',
DICT_PROTOCOL_REPLY_FAIL = 'F',
DICT_PROTOCOL_REPLY_WRITE_UNCERTAIN = 'W',
Expand Down

0 comments on commit 2176834

Please sign in to comment.