Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib-dict/dict-client.c @ 9361:a1b92a251bb9 HEAD
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sun, 06 Sep 2009 20:44:00 -0400 |
parents | eed86bcc33aa |
children | 00cd9aacd03c |
rev | line source |
---|---|
8590
b9faf4db2a9f
Updated copyright notices to include year 2009.
Timo Sirainen <tss@iki.fi>
parents:
8105
diff
changeset
|
1 /* Copyright (c) 2005-2009 Dovecot authors, see the included COPYING file */ |
3793 | 2 |
3 #include "lib.h" | |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
4 #include "llist.h" |
3793 | 5 #include "str.h" |
6 #include "network.h" | |
7 #include "istream.h" | |
8 #include "ostream.h" | |
9 #include "dict-private.h" | |
10 #include "dict-client.h" | |
11 | |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
12 #include <stdlib.h> |
3793 | 13 #include <unistd.h> |
14 #include <fcntl.h> | |
15 | |
16 struct client_dict { | |
17 struct dict dict; | |
18 | |
19 pool_t pool; | |
20 int fd; | |
21 const char *uri; | |
22 const char *username; | |
23 const char *path; | |
4516
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
24 enum dict_data_type value_type; |
3793 | 25 |
7008
6950bb5e7921
Don't try to reconnect more often than once/sec.
Timo Sirainen <tss@iki.fi>
parents:
7007
diff
changeset
|
26 time_t last_connect_try; |
3793 | 27 struct istream *input; |
28 struct ostream *output; | |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
29 struct io *io; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
30 |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
31 struct client_dict_transaction_context *transactions; |
3793 | 32 |
33 unsigned int connect_counter; | |
34 unsigned int transaction_id_counter; | |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
35 unsigned int async_commits; |
3793 | 36 |
37 unsigned int in_iteration:1; | |
38 unsigned int handshaked:1; | |
39 }; | |
40 | |
41 struct client_dict_iterate_context { | |
42 struct dict_iterate_context ctx; | |
43 | |
44 pool_t pool; | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3858
diff
changeset
|
45 bool failed; |
3793 | 46 }; |
47 | |
48 struct client_dict_transaction_context { | |
49 struct dict_transaction_context ctx; | |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
50 struct client_dict_transaction_context *prev, *next; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
51 |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
52 /* for async commits */ |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
53 dict_transaction_commit_callback_t *callback; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
54 void *context; |
3793 | 55 |
56 unsigned int id; | |
57 unsigned int connect_counter; | |
58 | |
4512
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
59 unsigned int failed:1; |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
60 unsigned int sent_begin:1; |
3793 | 61 }; |
62 | |
63 static int client_dict_connect(struct client_dict *dict); | |
64 static void client_dict_disconnect(struct client_dict *dict); | |
65 | |
66 const char *dict_client_escape(const char *src) | |
67 { | |
68 const char *p; | |
69 string_t *dest; | |
70 | |
71 /* first do a quick lookup to see if there's anything to escape. | |
72 probably not. */ | |
73 for (p = src; *p != '\0'; p++) { | |
74 if (*p == '\t' || *p == '\n' || *p == '\001') | |
75 break; | |
76 } | |
77 | |
78 if (*p == '\0') | |
79 return src; | |
80 | |
81 dest = t_str_new(256); | |
82 str_append_n(dest, src, p - src); | |
83 | |
3858 | 84 for (; *p != '\0'; p++) { |
3793 | 85 switch (*p) { |
86 case '\t': | |
87 str_append_c(dest, '\001'); | |
88 str_append_c(dest, 't'); | |
89 break; | |
90 case '\n': | |
91 str_append_c(dest, '\001'); | |
92 str_append_c(dest, 'n'); | |
93 break; | |
94 case '\001': | |
95 str_append_c(dest, '\001'); | |
96 str_append_c(dest, '1'); | |
97 break; | |
98 default: | |
99 str_append_c(dest, *p); | |
100 break; | |
101 } | |
102 } | |
103 return str_c(dest); | |
104 } | |
105 | |
106 const char *dict_client_unescape(const char *src) | |
107 { | |
108 const char *p; | |
109 string_t *dest; | |
110 | |
111 /* first do a quick lookup to see if there's anything to unescape. | |
112 probably not. */ | |
113 for (p = src; *p != '\0'; p++) { | |
114 if (*p == '\001') | |
115 break; | |
116 } | |
117 | |
118 if (*p == '\0') | |
119 return src; | |
120 | |
121 dest = t_str_new(256); | |
122 str_append_n(dest, src, p - src); | |
123 for (; *p != '\0'; p++) { | |
124 if (*p != '\001') | |
125 str_append_c(dest, *p); | |
126 else if (p[1] != '\0') { | |
127 p++; | |
128 switch (*p) { | |
129 case '1': | |
130 str_append_c(dest, '\001'); | |
131 break; | |
132 case 't': | |
133 str_append_c(dest, '\t'); | |
134 break; | |
135 case 'n': | |
136 str_append_c(dest, '\n'); | |
137 break; | |
138 } | |
139 } | |
140 } | |
141 return str_c(dest); | |
142 } | |
143 | |
144 static int client_dict_send_query(struct client_dict *dict, const char *query) | |
145 { | |
4368 | 146 if (dict->output == NULL) { |
147 /* not connected currently */ | |
148 if (client_dict_connect(dict) < 0) | |
149 return -1; | |
150 } | |
151 | |
3793 | 152 if (o_stream_send_str(dict->output, query) < 0 || |
153 o_stream_flush(dict->output) < 0) { | |
154 /* Send failed */ | |
155 if (!dict->handshaked) { | |
156 /* we're trying to send hello, don't try to reconnect */ | |
157 return -1; | |
158 } | |
159 | |
160 /* Reconnect and try again. */ | |
161 client_dict_disconnect(dict); | |
162 if (client_dict_connect(dict) < 0) | |
163 return -1; | |
164 | |
165 if (o_stream_send_str(dict->output, query) < 0 || | |
166 o_stream_flush(dict->output) < 0) { | |
167 i_error("write(%s) failed: %m", dict->path); | |
168 return -1; | |
169 } | |
170 } | |
171 return 0; | |
172 } | |
173 | |
4368 | 174 static int |
4512
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
175 client_dict_transaction_send_begin(struct client_dict_transaction_context *ctx) |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
176 { |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
177 struct client_dict *dict = (struct client_dict *)ctx->ctx.dict; |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
178 |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
179 if (ctx->failed) |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
180 return -1; |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
181 |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
182 T_BEGIN { |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
183 const char *query; |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
184 |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
185 query = t_strdup_printf("%c%u\n", DICT_PROTOCOL_CMD_BEGIN, |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
186 ctx->id); |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
187 if (client_dict_send_query(dict, query) < 0) |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
188 ctx->failed = TRUE; |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
189 else |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
190 ctx->connect_counter = dict->connect_counter; |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
191 } T_END; |
4512
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
192 |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
193 return ctx->failed ? -1 : 0; |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
194 } |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
195 |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
196 static int |
4368 | 197 client_dict_send_transaction_query(struct client_dict_transaction_context *ctx, |
198 const char *query) | |
199 { | |
200 struct client_dict *dict = (struct client_dict *)ctx->ctx.dict; | |
201 | |
4512
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
202 if (!ctx->sent_begin) { |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
203 if (client_dict_transaction_send_begin(ctx) < 0) |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
204 return -1; |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
205 ctx->sent_begin = TRUE; |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
206 } |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
207 |
4385 | 208 if (ctx->connect_counter != dict->connect_counter || ctx->failed) |
209 return -1; | |
210 | |
211 if (dict->output == NULL) { | |
4512
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
212 /* not connected, this'll fail */ |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
213 return -1; |
4385 | 214 } |
215 | |
4368 | 216 if (o_stream_send_str(dict->output, query) < 0 || |
217 o_stream_flush(dict->output) < 0) { | |
218 /* Send failed. Our transactions have died, so don't even try | |
219 to re-send the command */ | |
220 ctx->failed = TRUE; | |
221 client_dict_disconnect(dict); | |
222 return -1; | |
223 } | |
224 return 0; | |
225 } | |
226 | |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
227 static struct client_dict_transaction_context * |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
228 client_dict_transaction_find(struct client_dict *dict, unsigned int id) |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
229 { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
230 struct client_dict_transaction_context *ctx; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
231 |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
232 for (ctx = dict->transactions; ctx != NULL; ctx = ctx->next) { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
233 if (ctx->id == id) |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
234 return ctx; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
235 } |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
236 return NULL; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
237 } |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
238 |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
239 static void |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
240 client_dict_finish_transaction(struct client_dict *dict, |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
241 unsigned int id, int ret) |
3793 | 242 { |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
243 struct client_dict_transaction_context *ctx; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
244 |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
245 ctx = client_dict_transaction_find(dict, id); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
246 if (ctx == NULL) { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
247 i_error("dict-client: Unknown transaction id %u", id); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
248 return; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
249 } |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
250 if (ctx->callback != NULL) |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
251 ctx->callback(ret, ctx->context); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
252 |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
253 DLLIST_REMOVE(&dict->transactions, ctx); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
254 i_free(ctx); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
255 |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
256 i_assert(dict->async_commits > 0); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
257 if (--dict->async_commits == 0) |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
258 io_remove(&dict->io); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
259 } |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
260 |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
261 static int client_dict_read_one_line(struct client_dict *dict, char **line_r) |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
262 { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
263 unsigned int id; |
3793 | 264 char *line; |
265 int ret; | |
266 | |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
267 *line_r = NULL; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
268 while ((line = i_stream_next_line(dict->input)) == NULL) { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
269 ret = i_stream_read(dict->input); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
270 switch (ret) { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
271 case -1: |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
272 if (dict->input->stream_errno != 0) |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
273 i_error("read(%s) failed: %m", dict->path); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
274 else { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
275 i_error("read(%s) failed: Remote disconnected", |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
276 dict->path); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
277 } |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
278 return -1; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
279 case -2: |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
280 i_error("read(%s) returned too much data", dict->path); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
281 return -1; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
282 default: |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
283 i_assert(ret > 0); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
284 break; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
285 } |
3793 | 286 } |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
287 if (*line == DICT_PROTOCOL_REPLY_ASYNC_COMMIT) { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
288 switch (line[1]) { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
289 case DICT_PROTOCOL_REPLY_OK: |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
290 ret = 1; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
291 break; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
292 case DICT_PROTOCOL_REPLY_NOTFOUND: |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
293 ret = 0; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
294 break; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
295 case DICT_PROTOCOL_REPLY_FAIL: |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
296 ret = -1; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
297 break; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
298 default: |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
299 i_error("dict-client: Invalid async commit line: %s", |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
300 line); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
301 return 0; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
302 } |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
303 id = strtoul(line+2, NULL, 10); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
304 client_dict_finish_transaction(dict, id, ret); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
305 return 0; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
306 } |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
307 *line_r = line; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
308 return 1; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
309 } |
3793 | 310 |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
311 static char *client_dict_read_line(struct client_dict *dict) |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
312 { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
313 char *line; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
314 |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
315 while (client_dict_read_one_line(dict, &line) == 0) |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
316 ; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
317 return line; |
3793 | 318 } |
319 | |
320 static int client_dict_connect(struct client_dict *dict) | |
321 { | |
322 const char *query; | |
323 | |
324 i_assert(dict->fd == -1); | |
325 | |
7008
6950bb5e7921
Don't try to reconnect more often than once/sec.
Timo Sirainen <tss@iki.fi>
parents:
7007
diff
changeset
|
326 if (dict->last_connect_try == ioloop_time) { |
6950bb5e7921
Don't try to reconnect more often than once/sec.
Timo Sirainen <tss@iki.fi>
parents:
7007
diff
changeset
|
327 /* Try again later */ |
6950bb5e7921
Don't try to reconnect more often than once/sec.
Timo Sirainen <tss@iki.fi>
parents:
7007
diff
changeset
|
328 return -1; |
6950bb5e7921
Don't try to reconnect more often than once/sec.
Timo Sirainen <tss@iki.fi>
parents:
7007
diff
changeset
|
329 } |
6950bb5e7921
Don't try to reconnect more often than once/sec.
Timo Sirainen <tss@iki.fi>
parents:
7007
diff
changeset
|
330 dict->last_connect_try = ioloop_time; |
6950bb5e7921
Don't try to reconnect more often than once/sec.
Timo Sirainen <tss@iki.fi>
parents:
7007
diff
changeset
|
331 |
3793 | 332 dict->fd = net_connect_unix(dict->path); |
333 if (dict->fd == -1) { | |
334 i_error("net_connect_unix(%s) failed: %m", dict->path); | |
335 return -1; | |
336 } | |
337 | |
338 /* Dictionary lookups are blocking */ | |
339 net_set_nonblock(dict->fd, FALSE); | |
340 | |
6162
896cc473c1f0
Renamed i_stream_create_file() to i_stream_create_fd().
Timo Sirainen <tss@iki.fi>
parents:
6161
diff
changeset
|
341 dict->input = i_stream_create_fd(dict->fd, (size_t)-1, FALSE); |
7969
2cd775b45b0c
Set dict proxy istream nonblocking so EINTR (^C) won't crash.
Timo Sirainen <tss@iki.fi>
parents:
7418
diff
changeset
|
342 dict->input->blocking = TRUE; |
6161
c62f7ee79446
Split o_stream_create_file() to _create_fd() and _create_fd_file().
Timo Sirainen <tss@iki.fi>
parents:
6142
diff
changeset
|
343 dict->output = o_stream_create_fd(dict->fd, 4096, FALSE); |
3793 | 344 dict->transaction_id_counter = 0; |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
345 dict->async_commits = 0; |
3793 | 346 |
4516
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
347 query = t_strdup_printf("%c%u\t%u\t%d\t%s\t%s\n", |
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
348 DICT_PROTOCOL_CMD_HELLO, |
3793 | 349 DICT_CLIENT_PROTOCOL_MAJOR_VERSION, |
350 DICT_CLIENT_PROTOCOL_MINOR_VERSION, | |
4516
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
351 dict->value_type, dict->username, dict->uri); |
3793 | 352 if (client_dict_send_query(dict, query) < 0) { |
353 client_dict_disconnect(dict); | |
354 return -1; | |
355 } | |
356 | |
357 dict->handshaked = TRUE; | |
358 return 0; | |
359 } | |
360 | |
361 static void client_dict_disconnect(struct client_dict *dict) | |
362 { | |
363 dict->connect_counter++; | |
364 dict->handshaked = FALSE; | |
365 | |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
366 if (dict->io != NULL) |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
367 io_remove(&dict->io); |
3879
928229f8b3e6
deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
368 if (dict->input != NULL) |
4070
71b8faa84ec6
Added i_stream_destroy() and o_stream_destroy() and used them instead of
Timo Sirainen <tss@iki.fi>
parents:
3990
diff
changeset
|
369 i_stream_destroy(&dict->input); |
3879
928229f8b3e6
deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
370 if (dict->output != NULL) |
4070
71b8faa84ec6
Added i_stream_destroy() and o_stream_destroy() and used them instead of
Timo Sirainen <tss@iki.fi>
parents:
3990
diff
changeset
|
371 o_stream_destroy(&dict->output); |
3793 | 372 |
373 if (dict->fd != -1) { | |
374 if (close(dict->fd) < 0) | |
375 i_error("close(%s) failed: %m", dict->path); | |
376 dict->fd = -1; | |
377 } | |
378 } | |
379 | |
4516
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
380 static struct dict * |
4517
e661182eab75
Berkeley DB dict support is now enabled only when using --with-db configure option.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4516
diff
changeset
|
381 client_dict_init(struct dict *driver, const char *uri, |
9174
eed86bcc33aa
dict proxy: Use base_dir as the default dict-server location.
Timo Sirainen <tss@iki.fi>
parents:
8683
diff
changeset
|
382 enum dict_data_type value_type, const char *username, |
eed86bcc33aa
dict proxy: Use base_dir as the default dict-server location.
Timo Sirainen <tss@iki.fi>
parents:
8683
diff
changeset
|
383 const char *base_dir) |
3793 | 384 { |
385 struct client_dict *dict; | |
3967
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3879
diff
changeset
|
386 const char *dest_uri; |
3793 | 387 pool_t pool; |
388 | |
3967
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3879
diff
changeset
|
389 /* uri = [<path>] ":" <uri> */ |
3793 | 390 dest_uri = strchr(uri, ':'); |
391 if (dest_uri == NULL) { | |
392 i_error("dict-client: Invalid URI: %s", uri); | |
393 return NULL; | |
394 } | |
395 | |
396 pool = pool_alloconly_create("client dict", 1024); | |
397 dict = p_new(pool, struct client_dict, 1); | |
398 dict->pool = pool; | |
4517
e661182eab75
Berkeley DB dict support is now enabled only when using --with-db configure option.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4516
diff
changeset
|
399 dict->dict = *driver; |
4516
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
400 dict->value_type = value_type; |
3967
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3879
diff
changeset
|
401 dict->username = p_strdup(pool, username); |
3793 | 402 |
403 dict->fd = -1; | |
404 | |
3967
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3879
diff
changeset
|
405 if (*uri != ':') { |
3793 | 406 /* path given */ |
3967
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3879
diff
changeset
|
407 dict->path = p_strdup_until(pool, uri, dest_uri); |
3793 | 408 } else { |
9174
eed86bcc33aa
dict proxy: Use base_dir as the default dict-server location.
Timo Sirainen <tss@iki.fi>
parents:
8683
diff
changeset
|
409 dict->path = p_strconcat(pool, base_dir, |
eed86bcc33aa
dict proxy: Use base_dir as the default dict-server location.
Timo Sirainen <tss@iki.fi>
parents:
8683
diff
changeset
|
410 "/"DEFAULT_DICT_SERVER_SOCKET_FNAME, NULL); |
3793 | 411 } |
412 dict->uri = p_strdup(pool, dest_uri + 1); | |
413 return &dict->dict; | |
414 } | |
415 | |
416 static void client_dict_deinit(struct dict *_dict) | |
417 { | |
418 struct client_dict *dict = (struct client_dict *)_dict; | |
419 | |
420 client_dict_disconnect(dict); | |
6428
7cad076906eb
pool_unref() now takes ** pointer.
Timo Sirainen <tss@iki.fi>
parents:
6162
diff
changeset
|
421 pool_unref(&dict->pool); |
3793 | 422 } |
423 | |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
424 static int client_dict_wait(struct dict *_dict) |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
425 { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
426 struct client_dict *dict = (struct client_dict *)_dict; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
427 char *line; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
428 int ret = 0; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
429 |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
430 while (dict->async_commits > 0) { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
431 if (client_dict_read_one_line(dict, &line) < 0) { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
432 ret = -1; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
433 break; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
434 } |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
435 } |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
436 return ret; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
437 } |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
438 |
3793 | 439 static int client_dict_lookup(struct dict *_dict, pool_t pool, |
440 const char *key, const char **value_r) | |
441 { | |
442 struct client_dict *dict = (struct client_dict *)_dict; | |
443 const char *line; | |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
444 int ret; |
3793 | 445 |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
446 T_BEGIN { |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
447 const char *query; |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
448 |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
449 query = t_strdup_printf("%c%s\n", DICT_PROTOCOL_CMD_LOOKUP, |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
450 dict_client_escape(key)); |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
451 ret = client_dict_send_query(dict, query); |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
452 } T_END; |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
453 if (ret < 0) |
3793 | 454 return -1; |
455 | |
456 /* read reply */ | |
457 line = client_dict_read_line(dict); | |
458 if (line == NULL) | |
459 return -1; | |
460 | |
461 if (*line == DICT_PROTOCOL_REPLY_OK) { | |
462 *value_r = p_strdup(pool, dict_client_unescape(line + 1)); | |
463 return 1; | |
464 } else { | |
465 *value_r = NULL; | |
466 return *line == DICT_PROTOCOL_REPLY_NOTFOUND ? 0 : -1; | |
467 } | |
468 } | |
469 | |
470 static struct dict_iterate_context * | |
4516
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
471 client_dict_iterate_init(struct dict *_dict, const char *path, |
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
472 enum dict_iterate_flags flags) |
3793 | 473 { |
474 struct client_dict *dict = (struct client_dict *)_dict; | |
475 struct client_dict_iterate_context *ctx; | |
476 | |
477 if (dict->in_iteration) | |
478 i_panic("dict-client: Only one iteration supported"); | |
479 dict->in_iteration = TRUE; | |
480 | |
481 ctx = i_new(struct client_dict_iterate_context, 1); | |
482 ctx->ctx.dict = _dict; | |
483 ctx->pool = pool_alloconly_create("client dict iteration", 512); | |
484 | |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
485 T_BEGIN { |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
486 const char *query; |
3793 | 487 |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
488 query = t_strdup_printf("%c%d\t%s\n", DICT_PROTOCOL_CMD_ITERATE, |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
489 flags, dict_client_escape(path)); |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
490 if (client_dict_send_query(dict, query) < 0) |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
491 ctx->failed = TRUE; |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
492 } T_END; |
3793 | 493 return &ctx->ctx; |
494 } | |
495 | |
496 static int client_dict_iterate(struct dict_iterate_context *_ctx, | |
497 const char **key_r, const char **value_r) | |
498 { | |
499 struct client_dict_iterate_context *ctx = | |
500 (struct client_dict_iterate_context *)_ctx; | |
501 struct client_dict *dict = (struct client_dict *)_ctx->dict; | |
502 char *line, *value; | |
503 | |
504 if (ctx->failed) | |
505 return -1; | |
506 | |
507 /* read next reply */ | |
508 line = client_dict_read_line(dict); | |
509 if (line == NULL) | |
510 return -1; | |
511 | |
512 if (*line == '\0') { | |
513 /* end of iteration */ | |
514 return 0; | |
515 } | |
516 | |
517 /* line contains key \t value */ | |
518 p_clear(ctx->pool); | |
519 | |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
520 if (*line != DICT_PROTOCOL_REPLY_OK) |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
521 value = NULL; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
522 else |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
523 value = strchr(++line, '\t'); |
3793 | 524 if (value == NULL) { |
525 /* broken protocol */ | |
526 i_error("dict client (%s) sent broken reply", dict->path); | |
527 return -1; | |
528 } | |
529 *value++ = '\0'; | |
530 | |
531 *key_r = p_strdup(ctx->pool, dict_client_unescape(line)); | |
532 *value_r = p_strdup(ctx->pool, dict_client_unescape(value)); | |
533 return 1; | |
534 } | |
535 | |
536 static void client_dict_iterate_deinit(struct dict_iterate_context *_ctx) | |
537 { | |
538 struct client_dict *dict = (struct client_dict *)_ctx->dict; | |
539 struct client_dict_iterate_context *ctx = | |
540 (struct client_dict_iterate_context *)_ctx; | |
541 | |
6428
7cad076906eb
pool_unref() now takes ** pointer.
Timo Sirainen <tss@iki.fi>
parents:
6162
diff
changeset
|
542 pool_unref(&ctx->pool); |
3793 | 543 i_free(ctx); |
8105
ad9ad222729c
Calling dict_iterate_init() multiple times for a dict assert-crashed.
Timo Sirainen <tss@iki.fi>
parents:
7988
diff
changeset
|
544 dict->in_iteration = FALSE; |
3793 | 545 } |
546 | |
547 static struct dict_transaction_context * | |
548 client_dict_transaction_init(struct dict *_dict) | |
549 { | |
550 struct client_dict *dict = (struct client_dict *)_dict; | |
551 struct client_dict_transaction_context *ctx; | |
552 | |
553 ctx = i_new(struct client_dict_transaction_context, 1); | |
554 ctx->ctx.dict = _dict; | |
555 ctx->id = ++dict->transaction_id_counter; | |
4368 | 556 |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
557 DLLIST_PREPEND(&dict->transactions, ctx); |
3793 | 558 return &ctx->ctx; |
559 } | |
560 | |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
561 static void dict_async_input(struct client_dict *dict) |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
562 { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
563 char *line; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
564 size_t size; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
565 int ret; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
566 |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
567 i_assert(!dict->in_iteration); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
568 |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
569 do { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
570 ret = client_dict_read_one_line(dict, &line); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
571 (void)i_stream_get_data(dict->input, &size); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
572 } while (ret == 0 && size > 0); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
573 |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
574 if (ret < 0) |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
575 io_remove(&dict->io); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
576 } |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
577 |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
578 static int |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
579 client_dict_transaction_commit(struct dict_transaction_context *_ctx, |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
580 bool async, |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
581 dict_transaction_commit_callback_t *callback, |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
582 void *context) |
3793 | 583 { |
584 struct client_dict_transaction_context *ctx = | |
585 (struct client_dict_transaction_context *)_ctx; | |
586 struct client_dict *dict = (struct client_dict *)_ctx->dict; | |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
587 int ret = ctx->failed ? -1 : 1; |
3793 | 588 |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
589 if (ctx->sent_begin && !ctx->failed) T_BEGIN { |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
590 const char *query, *line; |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
591 |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
592 query = t_strdup_printf("%c%u\n", !async ? |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
593 DICT_PROTOCOL_CMD_COMMIT : |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
594 DICT_PROTOCOL_CMD_COMMIT_ASYNC, |
8664
446775a31754
dict proxy: Handle async commits better.
Timo Sirainen <tss@iki.fi>
parents:
8660
diff
changeset
|
595 ctx->id); |
4512
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
596 if (client_dict_send_transaction_query(ctx, query) < 0) |
3793 | 597 ret = -1; |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
598 else if (async) { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
599 ctx->callback = callback; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
600 ctx->context = context; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
601 if (dict->async_commits++ == 0) { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
602 dict->io = io_add(dict->fd, IO_READ, |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
603 dict_async_input, dict); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
604 } |
8660
d8a56ea9f408
Added dict_transaction_commit_async().
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
605 } else { |
8683
a68ed51b681d
dict proxy client: Don't hang when doing an async commit.
Timo Sirainen <tss@iki.fi>
parents:
8664
diff
changeset
|
606 /* sync commit, read reply */ |
4512
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
607 line = client_dict_read_line(dict); |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
608 if (line == NULL) |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
609 ret = -1; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
610 else if (*line == DICT_PROTOCOL_REPLY_OK) |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
611 ret = 1; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
612 else if (*line == DICT_PROTOCOL_REPLY_NOTFOUND) |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
613 ret = 0; |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
614 else |
4512
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
615 ret = -1; |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
616 } |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
617 } T_END; |
3793 | 618 |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
619 if (ret < 0 || !async) { |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
620 DLLIST_REMOVE(&dict->transactions, ctx); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
621 i_free(ctx); |
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
622 } |
3793 | 623 return ret; |
624 } | |
625 | |
626 static void | |
627 client_dict_transaction_rollback(struct dict_transaction_context *_ctx) | |
628 { | |
629 struct client_dict_transaction_context *ctx = | |
630 (struct client_dict_transaction_context *)_ctx; | |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
631 struct client_dict *dict = (struct client_dict *)_ctx->dict; |
3793 | 632 |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
633 if (ctx->sent_begin) T_BEGIN { |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
634 const char *query; |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
635 |
4512
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
636 query = t_strdup_printf("%c%u\n", DICT_PROTOCOL_CMD_ROLLBACK, |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
637 ctx->id); |
b5d4c1e9a492
Delay sending the transaction begin command to server until the first
Timo Sirainen <tss@iki.fi>
parents:
4385
diff
changeset
|
638 (void)client_dict_send_transaction_query(ctx, query); |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
639 } T_END; |
4385 | 640 |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
641 DLLIST_REMOVE(&dict->transactions, ctx); |
3793 | 642 i_free(ctx); |
643 } | |
644 | |
645 static void client_dict_set(struct dict_transaction_context *_ctx, | |
646 const char *key, const char *value) | |
647 { | |
648 struct client_dict_transaction_context *ctx = | |
649 (struct client_dict_transaction_context *)_ctx; | |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
650 |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
651 T_BEGIN { |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
652 const char *query; |
3793 | 653 |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
654 query = t_strdup_printf("%c%u\t%s\t%s\n", |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
655 DICT_PROTOCOL_CMD_SET, ctx->id, |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
656 dict_client_escape(key), |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
657 dict_client_escape(value)); |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
658 (void)client_dict_send_transaction_query(ctx, query); |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
659 } T_END; |
3793 | 660 } |
661 | |
4516
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
662 static void client_dict_unset(struct dict_transaction_context *_ctx, |
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
663 const char *key) |
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
664 { |
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
665 struct client_dict_transaction_context *ctx = |
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
666 (struct client_dict_transaction_context *)_ctx; |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
667 |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
668 T_BEGIN { |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
669 const char *query; |
4516
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
670 |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
671 query = t_strdup_printf("%c%u\t%s\n", |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
672 DICT_PROTOCOL_CMD_UNSET, ctx->id, |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
673 dict_client_escape(key)); |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
674 (void)client_dict_send_transaction_query(ctx, query); |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
675 } T_END; |
4516
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
676 } |
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
677 |
3793 | 678 static void client_dict_atomic_inc(struct dict_transaction_context *_ctx, |
3990
e2e6919c6c4d
LF wasn't sent at the end of all commands.
Timo Sirainen <tss@iki.fi>
parents:
3967
diff
changeset
|
679 const char *key, long long diff) |
3793 | 680 { |
681 struct client_dict_transaction_context *ctx = | |
682 (struct client_dict_transaction_context *)_ctx; | |
683 | |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
684 T_BEGIN { |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
685 const char *query; |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
686 query = t_strdup_printf("%c%u\t%s\t%lld\n", |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
687 DICT_PROTOCOL_CMD_ATOMIC_INC, |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
688 ctx->id, dict_client_escape(key), diff); |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6525
diff
changeset
|
689 (void)client_dict_send_transaction_query(ctx, query); |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
690 } T_END; |
3793 | 691 } |
692 | |
4517
e661182eab75
Berkeley DB dict support is now enabled only when using --with-db configure option.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4516
diff
changeset
|
693 struct dict dict_driver_client = { |
3793 | 694 MEMBER(name) "proxy", |
695 | |
696 { | |
697 client_dict_init, | |
698 client_dict_deinit, | |
9361
a1b92a251bb9
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9174
diff
changeset
|
699 client_dict_wait, |
3793 | 700 client_dict_lookup, |
701 client_dict_iterate_init, | |
702 client_dict_iterate, | |
703 client_dict_iterate_deinit, | |
704 client_dict_transaction_init, | |
705 client_dict_transaction_commit, | |
706 client_dict_transaction_rollback, | |
707 client_dict_set, | |
4516
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4512
diff
changeset
|
708 client_dict_unset, |
3793 | 709 client_dict_atomic_inc |
710 } | |
711 }; |