diff options
Diffstat (limited to 'src/datastore/plugin_datastore_postgres.c')
-rw-r--r-- | src/datastore/plugin_datastore_postgres.c | 936 |
1 files changed, 474 insertions, 462 deletions
diff --git a/src/datastore/plugin_datastore_postgres.c b/src/datastore/plugin_datastore_postgres.c index 17b645585..181cf8cf8 100644 --- a/src/datastore/plugin_datastore_postgres.c +++ b/src/datastore/plugin_datastore_postgres.c | |||
@@ -44,7 +44,8 @@ | |||
44 | /** | 44 | /** |
45 | * Context for all functions in this plugin. | 45 | * Context for all functions in this plugin. |
46 | */ | 46 | */ |
47 | struct Plugin { | 47 | struct Plugin |
48 | { | ||
48 | /** | 49 | /** |
49 | * Our execution environment. | 50 | * Our execution environment. |
50 | */ | 51 | */ |
@@ -64,7 +65,7 @@ struct Plugin { | |||
64 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 65 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
65 | */ | 66 | */ |
66 | static int | 67 | static int |
67 | init_connection(struct Plugin *plugin) | 68 | init_connection (struct Plugin *plugin) |
68 | { | 69 | { |
69 | struct GNUNET_PQ_ExecuteStatement es[] = { | 70 | struct GNUNET_PQ_ExecuteStatement es[] = { |
70 | /* FIXME: PostgreSQL does not have unsigned integers! This is ok for the type column because | 71 | /* FIXME: PostgreSQL does not have unsigned integers! This is ok for the type column because |
@@ -73,111 +74,119 @@ init_connection(struct Plugin *plugin) | |||
73 | * This will also cause problems for expiration times after 294247-01-10-04:00:54 UTC. | 74 | * This will also cause problems for expiration times after 294247-01-10-04:00:54 UTC. |
74 | * PostgreSQL also recommends against using WITH OIDS. | 75 | * PostgreSQL also recommends against using WITH OIDS. |
75 | */ | 76 | */ |
76 | GNUNET_PQ_make_execute("CREATE TABLE IF NOT EXISTS gn090 (" | 77 | GNUNET_PQ_make_execute ("CREATE TABLE IF NOT EXISTS gn090 (" |
77 | " repl INTEGER NOT NULL DEFAULT 0," | 78 | " repl INTEGER NOT NULL DEFAULT 0," |
78 | " type INTEGER NOT NULL DEFAULT 0," | 79 | " type INTEGER NOT NULL DEFAULT 0," |
79 | " prio INTEGER NOT NULL DEFAULT 0," | 80 | " prio INTEGER NOT NULL DEFAULT 0," |
80 | " anonLevel INTEGER NOT NULL DEFAULT 0," | 81 | " anonLevel INTEGER NOT NULL DEFAULT 0," |
81 | " expire BIGINT NOT NULL DEFAULT 0," | 82 | " expire BIGINT NOT NULL DEFAULT 0," |
82 | " rvalue BIGINT NOT NULL DEFAULT 0," | 83 | " rvalue BIGINT NOT NULL DEFAULT 0," |
83 | " hash BYTEA NOT NULL DEFAULT ''," | 84 | " hash BYTEA NOT NULL DEFAULT ''," |
84 | " vhash BYTEA NOT NULL DEFAULT ''," | 85 | " vhash BYTEA NOT NULL DEFAULT ''," |
85 | " value BYTEA NOT NULL DEFAULT '')" | 86 | " value BYTEA NOT NULL DEFAULT '')" |
86 | "WITH OIDS"), | 87 | "WITH OIDS"), |
87 | GNUNET_PQ_make_try_execute("CREATE INDEX IF NOT EXISTS idx_hash ON gn090 (hash)"), | 88 | GNUNET_PQ_make_try_execute ( |
88 | GNUNET_PQ_make_try_execute("CREATE INDEX IF NOT EXISTS idx_prio ON gn090 (prio)"), | 89 | "CREATE INDEX IF NOT EXISTS idx_hash ON gn090 (hash)"), |
89 | GNUNET_PQ_make_try_execute("CREATE INDEX IF NOT EXISTS idx_expire ON gn090 (expire)"), | 90 | GNUNET_PQ_make_try_execute ( |
90 | GNUNET_PQ_make_try_execute("CREATE INDEX IF NOT EXISTS idx_prio_anon ON gn090 (prio,anonLevel)"), | 91 | "CREATE INDEX IF NOT EXISTS idx_prio ON gn090 (prio)"), |
91 | GNUNET_PQ_make_try_execute("CREATE INDEX IF NOT EXISTS idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)"), | 92 | GNUNET_PQ_make_try_execute ( |
92 | GNUNET_PQ_make_try_execute("CREATE INDEX IF NOT EXISTS idx_repl_rvalue ON gn090 (repl,rvalue)"), | 93 | "CREATE INDEX IF NOT EXISTS idx_expire ON gn090 (expire)"), |
93 | GNUNET_PQ_make_try_execute("CREATE INDEX IF NOT EXISTS idx_expire_hash ON gn090 (expire,hash)"), | 94 | GNUNET_PQ_make_try_execute ( |
94 | GNUNET_PQ_make_execute("ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL"), | 95 | "CREATE INDEX IF NOT EXISTS idx_prio_anon ON gn090 (prio,anonLevel)"), |
95 | GNUNET_PQ_make_execute("ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN"), | 96 | GNUNET_PQ_make_try_execute ( |
96 | GNUNET_PQ_make_execute("ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN"), | 97 | "CREATE INDEX IF NOT EXISTS idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)"), |
98 | GNUNET_PQ_make_try_execute ( | ||
99 | "CREATE INDEX IF NOT EXISTS idx_repl_rvalue ON gn090 (repl,rvalue)"), | ||
100 | GNUNET_PQ_make_try_execute ( | ||
101 | "CREATE INDEX IF NOT EXISTS idx_expire_hash ON gn090 (expire,hash)"), | ||
102 | GNUNET_PQ_make_execute ( | ||
103 | "ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL"), | ||
104 | GNUNET_PQ_make_execute ("ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN"), | ||
105 | GNUNET_PQ_make_execute ("ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN"), | ||
97 | GNUNET_PQ_EXECUTE_STATEMENT_END | 106 | GNUNET_PQ_EXECUTE_STATEMENT_END |
98 | }; | 107 | }; |
99 | 108 | ||
100 | #define RESULT_COLUMNS "repl, type, prio, anonLevel, expire, hash, value, oid" | 109 | #define RESULT_COLUMNS "repl, type, prio, anonLevel, expire, hash, value, oid" |
101 | struct GNUNET_PQ_PreparedStatement ps[] = { | 110 | struct GNUNET_PQ_PreparedStatement ps[] = { |
102 | GNUNET_PQ_make_prepare("get", | 111 | GNUNET_PQ_make_prepare ("get", |
103 | "SELECT " RESULT_COLUMNS " FROM gn090" | 112 | "SELECT " RESULT_COLUMNS " FROM gn090" |
104 | " WHERE oid >= $1::bigint AND" | 113 | " WHERE oid >= $1::bigint AND" |
105 | " (rvalue >= $2 OR 0 = $3::smallint) AND" | 114 | " (rvalue >= $2 OR 0 = $3::smallint) AND" |
106 | " (hash = $4 OR 0 = $5::smallint) AND" | 115 | " (hash = $4 OR 0 = $5::smallint) AND" |
107 | " (type = $6 OR 0 = $7::smallint)" | 116 | " (type = $6 OR 0 = $7::smallint)" |
108 | " ORDER BY oid ASC LIMIT 1", | 117 | " ORDER BY oid ASC LIMIT 1", |
109 | 7), | 118 | 7), |
110 | GNUNET_PQ_make_prepare("put", | 119 | GNUNET_PQ_make_prepare ("put", |
111 | "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) " | 120 | "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) " |
112 | "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)", | 121 | "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)", |
113 | 9), | 122 | 9), |
114 | GNUNET_PQ_make_prepare("update", | 123 | GNUNET_PQ_make_prepare ("update", |
115 | "UPDATE gn090" | 124 | "UPDATE gn090" |
116 | " SET prio = prio + $1," | 125 | " SET prio = prio + $1," |
117 | " repl = repl + $2," | 126 | " repl = repl + $2," |
118 | " expire = GREATEST(expire, $3)" | 127 | " expire = GREATEST(expire, $3)" |
119 | " WHERE hash = $4 AND vhash = $5", | 128 | " WHERE hash = $4 AND vhash = $5", |
120 | 5), | 129 | 5), |
121 | GNUNET_PQ_make_prepare("decrepl", | 130 | GNUNET_PQ_make_prepare ("decrepl", |
122 | "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) " | 131 | "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) " |
123 | "WHERE oid = $1", | 132 | "WHERE oid = $1", |
124 | 1), | 133 | 1), |
125 | GNUNET_PQ_make_prepare("select_non_anonymous", | 134 | GNUNET_PQ_make_prepare ("select_non_anonymous", |
126 | "SELECT " RESULT_COLUMNS " FROM gn090 " | 135 | "SELECT " RESULT_COLUMNS " FROM gn090 " |
127 | "WHERE anonLevel = 0 AND type = $1 AND oid >= $2::bigint " | 136 | "WHERE anonLevel = 0 AND type = $1 AND oid >= $2::bigint " |
128 | "ORDER BY oid ASC LIMIT 1", | 137 | "ORDER BY oid ASC LIMIT 1", |
129 | 2), | 138 | 2), |
130 | GNUNET_PQ_make_prepare("select_expiration_order", | 139 | GNUNET_PQ_make_prepare ("select_expiration_order", |
131 | "(SELECT " RESULT_COLUMNS " FROM gn090 " | 140 | "(SELECT " RESULT_COLUMNS " FROM gn090 " |
132 | "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) " | 141 | "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) " |
133 | "UNION " | 142 | "UNION " |
134 | "(SELECT " RESULT_COLUMNS " FROM gn090 " | 143 | "(SELECT " RESULT_COLUMNS " FROM gn090 " |
135 | "ORDER BY prio ASC LIMIT 1) " | 144 | "ORDER BY prio ASC LIMIT 1) " |
136 | "ORDER BY expire ASC LIMIT 1", | 145 | "ORDER BY expire ASC LIMIT 1", |
137 | 1), | 146 | 1), |
138 | GNUNET_PQ_make_prepare("select_replication_order", | 147 | GNUNET_PQ_make_prepare ("select_replication_order", |
139 | "SELECT " RESULT_COLUMNS " FROM gn090 " | 148 | "SELECT " RESULT_COLUMNS " FROM gn090 " |
140 | "ORDER BY repl DESC,RANDOM() LIMIT 1", | 149 | "ORDER BY repl DESC,RANDOM() LIMIT 1", |
141 | 0), | 150 | 0), |
142 | GNUNET_PQ_make_prepare("delrow", | 151 | GNUNET_PQ_make_prepare ("delrow", |
143 | "DELETE FROM gn090 " | 152 | "DELETE FROM gn090 " |
144 | "WHERE oid=$1", | 153 | "WHERE oid=$1", |
145 | 1), | 154 | 1), |
146 | GNUNET_PQ_make_prepare("remove", | 155 | GNUNET_PQ_make_prepare ("remove", |
147 | "DELETE FROM gn090" | 156 | "DELETE FROM gn090" |
148 | " WHERE hash = $1 AND" | 157 | " WHERE hash = $1 AND" |
149 | " value = $2", | 158 | " value = $2", |
150 | 2), | 159 | 2), |
151 | GNUNET_PQ_make_prepare("get_keys", | 160 | GNUNET_PQ_make_prepare ("get_keys", |
152 | "SELECT hash FROM gn090", | 161 | "SELECT hash FROM gn090", |
153 | 0), | 162 | 0), |
154 | GNUNET_PQ_make_prepare("estimate_size", | 163 | GNUNET_PQ_make_prepare ("estimate_size", |
155 | "SELECT CASE WHEN NOT EXISTS" | 164 | "SELECT CASE WHEN NOT EXISTS" |
156 | " (SELECT 1 FROM gn090)" | 165 | " (SELECT 1 FROM gn090)" |
157 | " THEN 0" | 166 | " THEN 0" |
158 | " ELSE (SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090)" | 167 | " ELSE (SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090)" |
159 | "END AS total", | 168 | "END AS total", |
160 | 0), | 169 | 0), |
161 | GNUNET_PQ_PREPARED_STATEMENT_END | 170 | GNUNET_PQ_PREPARED_STATEMENT_END |
162 | }; | 171 | }; |
163 | #undef RESULT_COLUMNS | 172 | #undef RESULT_COLUMNS |
164 | 173 | ||
165 | plugin->dbh = GNUNET_PQ_connect_with_cfg(plugin->env->cfg, | 174 | plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->env->cfg, |
166 | "datastore-postgres"); | 175 | "datastore-postgres"); |
167 | if (NULL == plugin->dbh) | 176 | if (NULL == plugin->dbh) |
168 | return GNUNET_SYSERR; | 177 | return GNUNET_SYSERR; |
169 | 178 | ||
170 | if ((GNUNET_OK != | 179 | if ((GNUNET_OK != |
171 | GNUNET_PQ_exec_statements(plugin->dbh, | 180 | GNUNET_PQ_exec_statements (plugin->dbh, |
172 | es)) || | 181 | es)) || |
173 | (GNUNET_OK != | 182 | (GNUNET_OK != |
174 | GNUNET_PQ_prepare_statements(plugin->dbh, | 183 | GNUNET_PQ_prepare_statements (plugin->dbh, |
175 | ps))) | 184 | ps))) |
176 | { | 185 | { |
177 | PQfinish(plugin->dbh); | 186 | PQfinish (plugin->dbh); |
178 | plugin->dbh = NULL; | 187 | plugin->dbh = NULL; |
179 | return GNUNET_SYSERR; | 188 | return GNUNET_SYSERR; |
180 | } | 189 | } |
181 | return GNUNET_OK; | 190 | return GNUNET_OK; |
182 | } | 191 | } |
183 | 192 | ||
@@ -190,8 +199,8 @@ init_connection(struct Plugin *plugin) | |||
190 | * @return number of bytes used on disk | 199 | * @return number of bytes used on disk |
191 | */ | 200 | */ |
192 | static void | 201 | static void |
193 | postgres_plugin_estimate_size(void *cls, | 202 | postgres_plugin_estimate_size (void *cls, |
194 | unsigned long long *estimate) | 203 | unsigned long long *estimate) |
195 | { | 204 | { |
196 | struct Plugin *plugin = cls; | 205 | struct Plugin *plugin = cls; |
197 | uint64_t total; | 206 | uint64_t total; |
@@ -199,23 +208,23 @@ postgres_plugin_estimate_size(void *cls, | |||
199 | GNUNET_PQ_query_param_end | 208 | GNUNET_PQ_query_param_end |
200 | }; | 209 | }; |
201 | struct GNUNET_PQ_ResultSpec rs[] = { | 210 | struct GNUNET_PQ_ResultSpec rs[] = { |
202 | GNUNET_PQ_result_spec_uint64("total", | 211 | GNUNET_PQ_result_spec_uint64 ("total", |
203 | &total), | 212 | &total), |
204 | GNUNET_PQ_result_spec_end | 213 | GNUNET_PQ_result_spec_end |
205 | }; | 214 | }; |
206 | enum GNUNET_DB_QueryStatus ret; | 215 | enum GNUNET_DB_QueryStatus ret; |
207 | 216 | ||
208 | if (NULL == estimate) | 217 | if (NULL == estimate) |
209 | return; | 218 | return; |
210 | ret = GNUNET_PQ_eval_prepared_singleton_select(plugin->dbh, | 219 | ret = GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh, |
211 | "estimate_size", | 220 | "estimate_size", |
212 | params, | 221 | params, |
213 | rs); | 222 | rs); |
214 | if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != ret) | 223 | if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != ret) |
215 | { | 224 | { |
216 | *estimate = 0LL; | 225 | *estimate = 0LL; |
217 | return; | 226 | return; |
218 | } | 227 | } |
219 | *estimate = total; | 228 | *estimate = total; |
220 | } | 229 | } |
221 | 230 | ||
@@ -237,108 +246,109 @@ postgres_plugin_estimate_size(void *cls, | |||
237 | * @param cont_cls continuation closure | 246 | * @param cont_cls continuation closure |
238 | */ | 247 | */ |
239 | static void | 248 | static void |
240 | postgres_plugin_put(void *cls, | 249 | postgres_plugin_put (void *cls, |
241 | const struct GNUNET_HashCode *key, | 250 | const struct GNUNET_HashCode *key, |
242 | bool absent, | 251 | bool absent, |
243 | uint32_t size, | 252 | uint32_t size, |
244 | const void *data, | 253 | const void *data, |
245 | enum GNUNET_BLOCK_Type type, | 254 | enum GNUNET_BLOCK_Type type, |
246 | uint32_t priority, | 255 | uint32_t priority, |
247 | uint32_t anonymity, | 256 | uint32_t anonymity, |
248 | uint32_t replication, | 257 | uint32_t replication, |
249 | struct GNUNET_TIME_Absolute expiration, | 258 | struct GNUNET_TIME_Absolute expiration, |
250 | PluginPutCont cont, | 259 | PluginPutCont cont, |
251 | void *cont_cls) | 260 | void *cont_cls) |
252 | { | 261 | { |
253 | struct Plugin *plugin = cls; | 262 | struct Plugin *plugin = cls; |
254 | struct GNUNET_HashCode vhash; | 263 | struct GNUNET_HashCode vhash; |
255 | enum GNUNET_DB_QueryStatus ret; | 264 | enum GNUNET_DB_QueryStatus ret; |
256 | 265 | ||
257 | GNUNET_CRYPTO_hash(data, | 266 | GNUNET_CRYPTO_hash (data, |
258 | size, | 267 | size, |
259 | &vhash); | 268 | &vhash); |
260 | if (!absent) | 269 | if (! absent) |
270 | { | ||
271 | struct GNUNET_PQ_QueryParam params[] = { | ||
272 | GNUNET_PQ_query_param_uint32 (&priority), | ||
273 | GNUNET_PQ_query_param_uint32 (&replication), | ||
274 | GNUNET_PQ_query_param_absolute_time (&expiration), | ||
275 | GNUNET_PQ_query_param_auto_from_type (key), | ||
276 | GNUNET_PQ_query_param_auto_from_type (&vhash), | ||
277 | GNUNET_PQ_query_param_end | ||
278 | }; | ||
279 | ret = GNUNET_PQ_eval_prepared_non_select (plugin->dbh, | ||
280 | "update", | ||
281 | params); | ||
282 | if (0 > ret) | ||
261 | { | 283 | { |
262 | struct GNUNET_PQ_QueryParam params[] = { | 284 | cont (cont_cls, |
263 | GNUNET_PQ_query_param_uint32(&priority), | 285 | key, |
264 | GNUNET_PQ_query_param_uint32(&replication), | 286 | size, |
265 | GNUNET_PQ_query_param_absolute_time(&expiration), | 287 | GNUNET_SYSERR, |
266 | GNUNET_PQ_query_param_auto_from_type(key), | 288 | _ ("Postgress exec failure")); |
267 | GNUNET_PQ_query_param_auto_from_type(&vhash), | 289 | return; |
268 | GNUNET_PQ_query_param_end | 290 | } |
269 | }; | 291 | bool affected = (0 != ret); |
270 | ret = GNUNET_PQ_eval_prepared_non_select(plugin->dbh, | 292 | if (affected) |
271 | "update", | 293 | { |
272 | params); | 294 | cont (cont_cls, |
273 | if (0 > ret) | 295 | key, |
274 | { | 296 | size, |
275 | cont(cont_cls, | 297 | GNUNET_NO, |
276 | key, | 298 | NULL); |
277 | size, | 299 | return; |
278 | GNUNET_SYSERR, | ||
279 | _("Postgress exec failure")); | ||
280 | return; | ||
281 | } | ||
282 | bool affected = (0 != ret); | ||
283 | if (affected) | ||
284 | { | ||
285 | cont(cont_cls, | ||
286 | key, | ||
287 | size, | ||
288 | GNUNET_NO, | ||
289 | NULL); | ||
290 | return; | ||
291 | } | ||
292 | } | 300 | } |
301 | } | ||
293 | 302 | ||
294 | { | 303 | { |
295 | uint32_t utype = (uint32_t)type; | 304 | uint32_t utype = (uint32_t) type; |
296 | uint64_t rvalue = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, | 305 | uint64_t rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, |
297 | UINT64_MAX); | 306 | UINT64_MAX); |
298 | struct GNUNET_PQ_QueryParam params[] = { | 307 | struct GNUNET_PQ_QueryParam params[] = { |
299 | GNUNET_PQ_query_param_uint32(&replication), | 308 | GNUNET_PQ_query_param_uint32 (&replication), |
300 | GNUNET_PQ_query_param_uint32(&utype), | 309 | GNUNET_PQ_query_param_uint32 (&utype), |
301 | GNUNET_PQ_query_param_uint32(&priority), | 310 | GNUNET_PQ_query_param_uint32 (&priority), |
302 | GNUNET_PQ_query_param_uint32(&anonymity), | 311 | GNUNET_PQ_query_param_uint32 (&anonymity), |
303 | GNUNET_PQ_query_param_absolute_time(&expiration), | 312 | GNUNET_PQ_query_param_absolute_time (&expiration), |
304 | GNUNET_PQ_query_param_uint64(&rvalue), | 313 | GNUNET_PQ_query_param_uint64 (&rvalue), |
305 | GNUNET_PQ_query_param_auto_from_type(key), | 314 | GNUNET_PQ_query_param_auto_from_type (key), |
306 | GNUNET_PQ_query_param_auto_from_type(&vhash), | 315 | GNUNET_PQ_query_param_auto_from_type (&vhash), |
307 | GNUNET_PQ_query_param_fixed_size(data, size), | 316 | GNUNET_PQ_query_param_fixed_size (data, size), |
308 | GNUNET_PQ_query_param_end | 317 | GNUNET_PQ_query_param_end |
309 | }; | 318 | }; |
310 | 319 | ||
311 | ret = GNUNET_PQ_eval_prepared_non_select(plugin->dbh, | 320 | ret = GNUNET_PQ_eval_prepared_non_select (plugin->dbh, |
312 | "put", | 321 | "put", |
313 | params); | 322 | params); |
314 | if (0 > ret) | 323 | if (0 > ret) |
315 | { | 324 | { |
316 | cont(cont_cls, | 325 | cont (cont_cls, |
317 | key, | 326 | key, |
318 | size, | 327 | size, |
319 | GNUNET_SYSERR, | 328 | GNUNET_SYSERR, |
320 | "Postgress exec failure"); | 329 | "Postgress exec failure"); |
321 | return; | 330 | return; |
322 | } | 331 | } |
323 | } | 332 | } |
324 | plugin->env->duc(plugin->env->cls, | 333 | plugin->env->duc (plugin->env->cls, |
325 | size + GNUNET_DATASTORE_ENTRY_OVERHEAD); | 334 | size + GNUNET_DATASTORE_ENTRY_OVERHEAD); |
326 | GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, | 335 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
327 | "datastore-postgres", | 336 | "datastore-postgres", |
328 | "Stored %u bytes in database\n", | 337 | "Stored %u bytes in database\n", |
329 | (unsigned int)size); | 338 | (unsigned int) size); |
330 | cont(cont_cls, | 339 | cont (cont_cls, |
331 | key, | 340 | key, |
332 | size, | 341 | size, |
333 | GNUNET_OK, | 342 | GNUNET_OK, |
334 | NULL); | 343 | NULL); |
335 | } | 344 | } |
336 | 345 | ||
337 | 346 | ||
338 | /** | 347 | /** |
339 | * Closure for #process_result. | 348 | * Closure for #process_result. |
340 | */ | 349 | */ |
341 | struct ProcessResultContext { | 350 | struct ProcessResultContext |
351 | { | ||
342 | /** | 352 | /** |
343 | * The plugin handle. | 353 | * The plugin handle. |
344 | */ | 354 | */ |
@@ -365,111 +375,111 @@ struct ProcessResultContext { | |||
365 | * @param num_results number of results in @a res | 375 | * @param num_results number of results in @a res |
366 | */ | 376 | */ |
367 | static void | 377 | static void |
368 | process_result(void *cls, | 378 | process_result (void *cls, |
369 | PGresult *res, | 379 | PGresult *res, |
370 | unsigned int num_results) | 380 | unsigned int num_results) |
371 | { | 381 | { |
372 | struct ProcessResultContext *prc = cls; | 382 | struct ProcessResultContext *prc = cls; |
373 | struct Plugin *plugin = prc->plugin; | 383 | struct Plugin *plugin = prc->plugin; |
374 | 384 | ||
375 | if (0 == num_results) | 385 | if (0 == num_results) |
376 | { | 386 | { |
377 | /* no result */ | 387 | /* no result */ |
378 | GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, | 388 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
379 | "datastore-postgres", | 389 | "datastore-postgres", |
380 | "Ending iteration (no more results)\n"); | 390 | "Ending iteration (no more results)\n"); |
381 | prc->proc(prc->proc_cls, NULL, 0, NULL, 0, 0, 0, 0, | 391 | prc->proc (prc->proc_cls, NULL, 0, NULL, 0, 0, 0, 0, |
382 | GNUNET_TIME_UNIT_ZERO_ABS, 0); | 392 | GNUNET_TIME_UNIT_ZERO_ABS, 0); |
383 | return; | 393 | return; |
384 | } | 394 | } |
385 | if (1 != num_results) | 395 | if (1 != num_results) |
386 | { | 396 | { |
387 | GNUNET_break(0); | 397 | GNUNET_break (0); |
388 | prc->proc(prc->proc_cls, NULL, 0, NULL, 0, 0, 0, 0, | 398 | prc->proc (prc->proc_cls, NULL, 0, NULL, 0, 0, 0, 0, |
389 | GNUNET_TIME_UNIT_ZERO_ABS, 0); | 399 | GNUNET_TIME_UNIT_ZERO_ABS, 0); |
390 | return; | 400 | return; |
391 | } | 401 | } |
392 | /* Technically we don't need the loop here, but nicer in case | 402 | /* Technically we don't need the loop here, but nicer in case |
393 | we ever relax the condition above. */ | 403 | we ever relax the condition above. */ |
394 | for (unsigned int i = 0; i < num_results; i++) | 404 | for (unsigned int i = 0; i < num_results; i++) |
405 | { | ||
406 | int iret; | ||
407 | uint32_t rowid; | ||
408 | uint32_t utype; | ||
409 | uint32_t anonymity; | ||
410 | uint32_t replication; | ||
411 | uint32_t priority; | ||
412 | size_t size; | ||
413 | void *data; | ||
414 | struct GNUNET_TIME_Absolute expiration_time; | ||
415 | struct GNUNET_HashCode key; | ||
416 | struct GNUNET_PQ_ResultSpec rs[] = { | ||
417 | GNUNET_PQ_result_spec_uint32 ("repl", &replication), | ||
418 | GNUNET_PQ_result_spec_uint32 ("type", &utype), | ||
419 | GNUNET_PQ_result_spec_uint32 ("prio", &priority), | ||
420 | GNUNET_PQ_result_spec_uint32 ("anonLevel", &anonymity), | ||
421 | GNUNET_PQ_result_spec_absolute_time ("expire", &expiration_time), | ||
422 | GNUNET_PQ_result_spec_auto_from_type ("hash", &key), | ||
423 | GNUNET_PQ_result_spec_variable_size ("value", &data, &size), | ||
424 | GNUNET_PQ_result_spec_uint32 ("oid", &rowid), | ||
425 | GNUNET_PQ_result_spec_end | ||
426 | }; | ||
427 | |||
428 | if (GNUNET_OK != | ||
429 | GNUNET_PQ_extract_result (res, | ||
430 | rs, | ||
431 | i)) | ||
395 | { | 432 | { |
396 | int iret; | 433 | GNUNET_break (0); |
397 | uint32_t rowid; | 434 | prc->proc (prc->proc_cls, NULL, 0, NULL, 0, 0, 0, 0, |
398 | uint32_t utype; | 435 | GNUNET_TIME_UNIT_ZERO_ABS, 0); |
399 | uint32_t anonymity; | 436 | return; |
400 | uint32_t replication; | 437 | } |
401 | uint32_t priority; | 438 | |
402 | size_t size; | 439 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
403 | void *data; | 440 | "datastore-postgres", |
404 | struct GNUNET_TIME_Absolute expiration_time; | 441 | "Found result of size %u bytes and type %u in database\n", |
405 | struct GNUNET_HashCode key; | 442 | (unsigned int) size, |
406 | struct GNUNET_PQ_ResultSpec rs[] = { | 443 | (unsigned int) utype); |
407 | GNUNET_PQ_result_spec_uint32("repl", &replication), | 444 | iret = prc->proc (prc->proc_cls, |
408 | GNUNET_PQ_result_spec_uint32("type", &utype), | 445 | &key, |
409 | GNUNET_PQ_result_spec_uint32("prio", &priority), | 446 | size, |
410 | GNUNET_PQ_result_spec_uint32("anonLevel", &anonymity), | 447 | data, |
411 | GNUNET_PQ_result_spec_absolute_time("expire", &expiration_time), | 448 | (enum GNUNET_BLOCK_Type) utype, |
412 | GNUNET_PQ_result_spec_auto_from_type("hash", &key), | 449 | priority, |
413 | GNUNET_PQ_result_spec_variable_size("value", &data, &size), | 450 | anonymity, |
414 | GNUNET_PQ_result_spec_uint32("oid", &rowid), | 451 | replication, |
415 | GNUNET_PQ_result_spec_end | 452 | expiration_time, |
453 | rowid); | ||
454 | if (iret == GNUNET_NO) | ||
455 | { | ||
456 | struct GNUNET_PQ_QueryParam param[] = { | ||
457 | GNUNET_PQ_query_param_uint32 (&rowid), | ||
458 | GNUNET_PQ_query_param_end | ||
416 | }; | 459 | }; |
417 | 460 | ||
418 | if (GNUNET_OK != | 461 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
419 | GNUNET_PQ_extract_result(res, | 462 | "Processor asked for item %u to be removed.\n", |
420 | rs, | 463 | (unsigned int) rowid); |
421 | i)) | 464 | if (0 < |
422 | { | 465 | GNUNET_PQ_eval_prepared_non_select (plugin->dbh, |
423 | GNUNET_break(0); | 466 | "delrow", |
424 | prc->proc(prc->proc_cls, NULL, 0, NULL, 0, 0, 0, 0, | 467 | param)) |
425 | GNUNET_TIME_UNIT_ZERO_ABS, 0); | 468 | { |
426 | return; | 469 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
427 | } | 470 | "datastore-postgres", |
428 | 471 | "Deleting %u bytes from database\n", | |
429 | GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, | 472 | (unsigned int) size); |
430 | "datastore-postgres", | 473 | plugin->env->duc (plugin->env->cls, |
431 | "Found result of size %u bytes and type %u in database\n", | 474 | -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD)); |
432 | (unsigned int)size, | 475 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
433 | (unsigned int)utype); | 476 | "datastore-postgres", |
434 | iret = prc->proc(prc->proc_cls, | 477 | "Deleted %u bytes from database\n", |
435 | &key, | 478 | (unsigned int) size); |
436 | size, | 479 | } |
437 | data, | 480 | } |
438 | (enum GNUNET_BLOCK_Type)utype, | 481 | GNUNET_PQ_cleanup_result (rs); |
439 | priority, | 482 | } /* for (i) */ |
440 | anonymity, | ||
441 | replication, | ||
442 | expiration_time, | ||
443 | rowid); | ||
444 | if (iret == GNUNET_NO) | ||
445 | { | ||
446 | struct GNUNET_PQ_QueryParam param[] = { | ||
447 | GNUNET_PQ_query_param_uint32(&rowid), | ||
448 | GNUNET_PQ_query_param_end | ||
449 | }; | ||
450 | |||
451 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
452 | "Processor asked for item %u to be removed.\n", | ||
453 | (unsigned int)rowid); | ||
454 | if (0 < | ||
455 | GNUNET_PQ_eval_prepared_non_select(plugin->dbh, | ||
456 | "delrow", | ||
457 | param)) | ||
458 | { | ||
459 | GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, | ||
460 | "datastore-postgres", | ||
461 | "Deleting %u bytes from database\n", | ||
462 | (unsigned int)size); | ||
463 | plugin->env->duc(plugin->env->cls, | ||
464 | -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD)); | ||
465 | GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, | ||
466 | "datastore-postgres", | ||
467 | "Deleted %u bytes from database\n", | ||
468 | (unsigned int)size); | ||
469 | } | ||
470 | } | ||
471 | GNUNET_PQ_cleanup_result(rs); | ||
472 | } /* for (i) */ | ||
473 | } | 483 | } |
474 | 484 | ||
475 | 485 | ||
@@ -487,13 +497,13 @@ process_result(void *cls, | |||
487 | * @param proc_cls closure for @a proc | 497 | * @param proc_cls closure for @a proc |
488 | */ | 498 | */ |
489 | static void | 499 | static void |
490 | postgres_plugin_get_key(void *cls, | 500 | postgres_plugin_get_key (void *cls, |
491 | uint64_t next_uid, | 501 | uint64_t next_uid, |
492 | bool random, | 502 | bool random, |
493 | const struct GNUNET_HashCode *key, | 503 | const struct GNUNET_HashCode *key, |
494 | enum GNUNET_BLOCK_Type type, | 504 | enum GNUNET_BLOCK_Type type, |
495 | PluginDatumProcessor proc, | 505 | PluginDatumProcessor proc, |
496 | void *proc_cls) | 506 | void *proc_cls) |
497 | { | 507 | { |
498 | struct Plugin *plugin = cls; | 508 | struct Plugin *plugin = cls; |
499 | uint32_t utype = type; | 509 | uint32_t utype = type; |
@@ -502,40 +512,40 @@ postgres_plugin_get_key(void *cls, | |||
502 | uint16_t use_type = GNUNET_BLOCK_TYPE_ANY != type; | 512 | uint16_t use_type = GNUNET_BLOCK_TYPE_ANY != type; |
503 | uint64_t rvalue; | 513 | uint64_t rvalue; |
504 | struct GNUNET_PQ_QueryParam params[] = { | 514 | struct GNUNET_PQ_QueryParam params[] = { |
505 | GNUNET_PQ_query_param_uint64(&next_uid), | 515 | GNUNET_PQ_query_param_uint64 (&next_uid), |
506 | GNUNET_PQ_query_param_uint64(&rvalue), | 516 | GNUNET_PQ_query_param_uint64 (&rvalue), |
507 | GNUNET_PQ_query_param_uint16(&use_rvalue), | 517 | GNUNET_PQ_query_param_uint16 (&use_rvalue), |
508 | GNUNET_PQ_query_param_auto_from_type(key), | 518 | GNUNET_PQ_query_param_auto_from_type (key), |
509 | GNUNET_PQ_query_param_uint16(&use_key), | 519 | GNUNET_PQ_query_param_uint16 (&use_key), |
510 | GNUNET_PQ_query_param_uint32(&utype), | 520 | GNUNET_PQ_query_param_uint32 (&utype), |
511 | GNUNET_PQ_query_param_uint16(&use_type), | 521 | GNUNET_PQ_query_param_uint16 (&use_type), |
512 | GNUNET_PQ_query_param_end | 522 | GNUNET_PQ_query_param_end |
513 | }; | 523 | }; |
514 | struct ProcessResultContext prc; | 524 | struct ProcessResultContext prc; |
515 | enum GNUNET_DB_QueryStatus res; | 525 | enum GNUNET_DB_QueryStatus res; |
516 | 526 | ||
517 | if (random) | 527 | if (random) |
518 | { | 528 | { |
519 | rvalue = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, | 529 | rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, |
520 | UINT64_MAX); | 530 | UINT64_MAX); |
521 | next_uid = 0; | 531 | next_uid = 0; |
522 | } | 532 | } |
523 | else | 533 | else |
524 | { | 534 | { |
525 | rvalue = 0; | 535 | rvalue = 0; |
526 | } | 536 | } |
527 | prc.plugin = plugin; | 537 | prc.plugin = plugin; |
528 | prc.proc = proc; | 538 | prc.proc = proc; |
529 | prc.proc_cls = proc_cls; | 539 | prc.proc_cls = proc_cls; |
530 | 540 | ||
531 | res = GNUNET_PQ_eval_prepared_multi_select(plugin->dbh, | 541 | res = GNUNET_PQ_eval_prepared_multi_select (plugin->dbh, |
532 | "get", | 542 | "get", |
533 | params, | 543 | params, |
534 | &process_result, | 544 | &process_result, |
535 | &prc); | 545 | &prc); |
536 | if (0 > res) | 546 | if (0 > res) |
537 | proc(proc_cls, NULL, 0, NULL, 0, 0, 0, 0, | 547 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, |
538 | GNUNET_TIME_UNIT_ZERO_ABS, 0); | 548 | GNUNET_TIME_UNIT_ZERO_ABS, 0); |
539 | } | 549 | } |
540 | 550 | ||
541 | 551 | ||
@@ -552,17 +562,17 @@ postgres_plugin_get_key(void *cls, | |||
552 | * @param proc_cls closure for @a proc | 562 | * @param proc_cls closure for @a proc |
553 | */ | 563 | */ |
554 | static void | 564 | static void |
555 | postgres_plugin_get_zero_anonymity(void *cls, | 565 | postgres_plugin_get_zero_anonymity (void *cls, |
556 | uint64_t next_uid, | 566 | uint64_t next_uid, |
557 | enum GNUNET_BLOCK_Type type, | 567 | enum GNUNET_BLOCK_Type type, |
558 | PluginDatumProcessor proc, | 568 | PluginDatumProcessor proc, |
559 | void *proc_cls) | 569 | void *proc_cls) |
560 | { | 570 | { |
561 | struct Plugin *plugin = cls; | 571 | struct Plugin *plugin = cls; |
562 | uint32_t utype = type; | 572 | uint32_t utype = type; |
563 | struct GNUNET_PQ_QueryParam params[] = { | 573 | struct GNUNET_PQ_QueryParam params[] = { |
564 | GNUNET_PQ_query_param_uint32(&utype), | 574 | GNUNET_PQ_query_param_uint32 (&utype), |
565 | GNUNET_PQ_query_param_uint64(&next_uid), | 575 | GNUNET_PQ_query_param_uint64 (&next_uid), |
566 | GNUNET_PQ_query_param_end | 576 | GNUNET_PQ_query_param_end |
567 | }; | 577 | }; |
568 | struct ProcessResultContext prc; | 578 | struct ProcessResultContext prc; |
@@ -571,21 +581,22 @@ postgres_plugin_get_zero_anonymity(void *cls, | |||
571 | prc.plugin = plugin; | 581 | prc.plugin = plugin; |
572 | prc.proc = proc; | 582 | prc.proc = proc; |
573 | prc.proc_cls = proc_cls; | 583 | prc.proc_cls = proc_cls; |
574 | res = GNUNET_PQ_eval_prepared_multi_select(plugin->dbh, | 584 | res = GNUNET_PQ_eval_prepared_multi_select (plugin->dbh, |
575 | "select_non_anonymous", | 585 | "select_non_anonymous", |
576 | params, | 586 | params, |
577 | &process_result, | 587 | &process_result, |
578 | &prc); | 588 | &prc); |
579 | if (0 > res) | 589 | if (0 > res) |
580 | proc(proc_cls, NULL, 0, NULL, 0, 0, 0, 0, | 590 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, |
581 | GNUNET_TIME_UNIT_ZERO_ABS, 0); | 591 | GNUNET_TIME_UNIT_ZERO_ABS, 0); |
582 | } | 592 | } |
583 | 593 | ||
584 | 594 | ||
585 | /** | 595 | /** |
586 | * Context for #repl_iter() function. | 596 | * Context for #repl_iter() function. |
587 | */ | 597 | */ |
588 | struct ReplCtx { | 598 | struct ReplCtx |
599 | { | ||
589 | /** | 600 | /** |
590 | * Plugin handle. | 601 | * Plugin handle. |
591 | */ | 602 | */ |
@@ -625,42 +636,42 @@ struct ReplCtx { | |||
625 | * #GNUNET_NO to delete the item and continue (if supported) | 636 | * #GNUNET_NO to delete the item and continue (if supported) |
626 | */ | 637 | */ |
627 | static int | 638 | static int |
628 | repl_proc(void *cls, | 639 | repl_proc (void *cls, |
629 | const struct GNUNET_HashCode *key, | 640 | const struct GNUNET_HashCode *key, |
630 | uint32_t size, | 641 | uint32_t size, |
631 | const void *data, | 642 | const void *data, |
632 | enum GNUNET_BLOCK_Type type, | 643 | enum GNUNET_BLOCK_Type type, |
633 | uint32_t priority, | 644 | uint32_t priority, |
634 | uint32_t anonymity, | 645 | uint32_t anonymity, |
635 | uint32_t replication, | 646 | uint32_t replication, |
636 | struct GNUNET_TIME_Absolute expiration, | 647 | struct GNUNET_TIME_Absolute expiration, |
637 | uint64_t uid) | 648 | uint64_t uid) |
638 | { | 649 | { |
639 | struct ReplCtx *rc = cls; | 650 | struct ReplCtx *rc = cls; |
640 | struct Plugin *plugin = rc->plugin; | 651 | struct Plugin *plugin = rc->plugin; |
641 | int ret; | 652 | int ret; |
642 | uint32_t oid = (uint32_t)uid; | 653 | uint32_t oid = (uint32_t) uid; |
643 | struct GNUNET_PQ_QueryParam params[] = { | 654 | struct GNUNET_PQ_QueryParam params[] = { |
644 | GNUNET_PQ_query_param_uint32(&oid), | 655 | GNUNET_PQ_query_param_uint32 (&oid), |
645 | GNUNET_PQ_query_param_end | 656 | GNUNET_PQ_query_param_end |
646 | }; | 657 | }; |
647 | enum GNUNET_DB_QueryStatus qret; | 658 | enum GNUNET_DB_QueryStatus qret; |
648 | 659 | ||
649 | ret = rc->proc(rc->proc_cls, | 660 | ret = rc->proc (rc->proc_cls, |
650 | key, | 661 | key, |
651 | size, | 662 | size, |
652 | data, | 663 | data, |
653 | type, | 664 | type, |
654 | priority, | 665 | priority, |
655 | anonymity, | 666 | anonymity, |
656 | replication, | 667 | replication, |
657 | expiration, | 668 | expiration, |
658 | uid); | 669 | uid); |
659 | if (NULL == key) | 670 | if (NULL == key) |
660 | return ret; | 671 | return ret; |
661 | qret = GNUNET_PQ_eval_prepared_non_select(plugin->dbh, | 672 | qret = GNUNET_PQ_eval_prepared_non_select (plugin->dbh, |
662 | "decrepl", | 673 | "decrepl", |
663 | params); | 674 | params); |
664 | if (0 > qret) | 675 | if (0 > qret) |
665 | return GNUNET_SYSERR; | 676 | return GNUNET_SYSERR; |
666 | return ret; | 677 | return ret; |
@@ -679,9 +690,9 @@ repl_proc(void *cls, | |||
679 | * @param proc_cls closure for @a proc | 690 | * @param proc_cls closure for @a proc |
680 | */ | 691 | */ |
681 | static void | 692 | static void |
682 | postgres_plugin_get_replication(void *cls, | 693 | postgres_plugin_get_replication (void *cls, |
683 | PluginDatumProcessor proc, | 694 | PluginDatumProcessor proc, |
684 | void *proc_cls) | 695 | void *proc_cls) |
685 | { | 696 | { |
686 | struct Plugin *plugin = cls; | 697 | struct Plugin *plugin = cls; |
687 | struct GNUNET_PQ_QueryParam params[] = { | 698 | struct GNUNET_PQ_QueryParam params[] = { |
@@ -697,14 +708,14 @@ postgres_plugin_get_replication(void *cls, | |||
697 | prc.plugin = plugin; | 708 | prc.plugin = plugin; |
698 | prc.proc = &repl_proc; | 709 | prc.proc = &repl_proc; |
699 | prc.proc_cls = &rc; | 710 | prc.proc_cls = &rc; |
700 | res = GNUNET_PQ_eval_prepared_multi_select(plugin->dbh, | 711 | res = GNUNET_PQ_eval_prepared_multi_select (plugin->dbh, |
701 | "select_replication_order", | 712 | "select_replication_order", |
702 | params, | 713 | params, |
703 | &process_result, | 714 | &process_result, |
704 | &prc); | 715 | &prc); |
705 | if (0 > res) | 716 | if (0 > res) |
706 | proc(proc_cls, NULL, 0, NULL, 0, 0, 0, 0, | 717 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, |
707 | GNUNET_TIME_UNIT_ZERO_ABS, 0); | 718 | GNUNET_TIME_UNIT_ZERO_ABS, 0); |
708 | } | 719 | } |
709 | 720 | ||
710 | 721 | ||
@@ -717,34 +728,35 @@ postgres_plugin_get_replication(void *cls, | |||
717 | * @param proc_cls closure for @a proc | 728 | * @param proc_cls closure for @a proc |
718 | */ | 729 | */ |
719 | static void | 730 | static void |
720 | postgres_plugin_get_expiration(void *cls, | 731 | postgres_plugin_get_expiration (void *cls, |
721 | PluginDatumProcessor proc, | 732 | PluginDatumProcessor proc, |
722 | void *proc_cls) | 733 | void *proc_cls) |
723 | { | 734 | { |
724 | struct Plugin *plugin = cls; | 735 | struct Plugin *plugin = cls; |
725 | struct GNUNET_TIME_Absolute now; | 736 | struct GNUNET_TIME_Absolute now; |
726 | struct GNUNET_PQ_QueryParam params[] = { | 737 | struct GNUNET_PQ_QueryParam params[] = { |
727 | GNUNET_PQ_query_param_absolute_time(&now), | 738 | GNUNET_PQ_query_param_absolute_time (&now), |
728 | GNUNET_PQ_query_param_end | 739 | GNUNET_PQ_query_param_end |
729 | }; | 740 | }; |
730 | struct ProcessResultContext prc; | 741 | struct ProcessResultContext prc; |
731 | 742 | ||
732 | now = GNUNET_TIME_absolute_get(); | 743 | now = GNUNET_TIME_absolute_get (); |
733 | prc.plugin = plugin; | 744 | prc.plugin = plugin; |
734 | prc.proc = proc; | 745 | prc.proc = proc; |
735 | prc.proc_cls = proc_cls; | 746 | prc.proc_cls = proc_cls; |
736 | (void)GNUNET_PQ_eval_prepared_multi_select(plugin->dbh, | 747 | (void) GNUNET_PQ_eval_prepared_multi_select (plugin->dbh, |
737 | "select_expiration_order", | 748 | "select_expiration_order", |
738 | params, | 749 | params, |
739 | &process_result, | 750 | &process_result, |
740 | &prc); | 751 | &prc); |
741 | } | 752 | } |
742 | 753 | ||
743 | 754 | ||
744 | /** | 755 | /** |
745 | * Closure for #process_keys. | 756 | * Closure for #process_keys. |
746 | */ | 757 | */ |
747 | struct ProcessKeysContext { | 758 | struct ProcessKeysContext |
759 | { | ||
748 | /** | 760 | /** |
749 | * Function to call for each key. | 761 | * Function to call for each key. |
750 | */ | 762 | */ |
@@ -766,34 +778,34 @@ struct ProcessKeysContext { | |||
766 | * @param num_result the number of results in @a result | 778 | * @param num_result the number of results in @a result |
767 | */ | 779 | */ |
768 | static void | 780 | static void |
769 | process_keys(void *cls, | 781 | process_keys (void *cls, |
770 | PGresult *result, | 782 | PGresult *result, |
771 | unsigned int num_results) | 783 | unsigned int num_results) |
772 | { | 784 | { |
773 | struct ProcessKeysContext *pkc = cls; | 785 | struct ProcessKeysContext *pkc = cls; |
774 | 786 | ||
775 | for (unsigned i = 0; i < num_results; i++) | 787 | for (unsigned i = 0; i < num_results; i++) |
776 | { | 788 | { |
777 | struct GNUNET_HashCode key; | 789 | struct GNUNET_HashCode key; |
778 | struct GNUNET_PQ_ResultSpec rs[] = { | 790 | struct GNUNET_PQ_ResultSpec rs[] = { |
779 | GNUNET_PQ_result_spec_auto_from_type("hash", | 791 | GNUNET_PQ_result_spec_auto_from_type ("hash", |
780 | &key), | 792 | &key), |
781 | GNUNET_PQ_result_spec_end | 793 | GNUNET_PQ_result_spec_end |
782 | }; | 794 | }; |
783 | 795 | ||
784 | if (GNUNET_OK != | 796 | if (GNUNET_OK != |
785 | GNUNET_PQ_extract_result(result, | 797 | GNUNET_PQ_extract_result (result, |
786 | rs, | 798 | rs, |
787 | i)) | 799 | i)) |
788 | { | 800 | { |
789 | GNUNET_break(0); | 801 | GNUNET_break (0); |
790 | continue; | 802 | continue; |
791 | } | ||
792 | pkc->proc(pkc->proc_cls, | ||
793 | &key, | ||
794 | 1); | ||
795 | GNUNET_PQ_cleanup_result(rs); | ||
796 | } | 803 | } |
804 | pkc->proc (pkc->proc_cls, | ||
805 | &key, | ||
806 | 1); | ||
807 | GNUNET_PQ_cleanup_result (rs); | ||
808 | } | ||
797 | } | 809 | } |
798 | 810 | ||
799 | 811 | ||
@@ -805,9 +817,9 @@ process_keys(void *cls, | |||
805 | * @param proc_cls closure for @a proc | 817 | * @param proc_cls closure for @a proc |
806 | */ | 818 | */ |
807 | static void | 819 | static void |
808 | postgres_plugin_get_keys(void *cls, | 820 | postgres_plugin_get_keys (void *cls, |
809 | PluginKeyProcessor proc, | 821 | PluginKeyProcessor proc, |
810 | void *proc_cls) | 822 | void *proc_cls) |
811 | { | 823 | { |
812 | struct Plugin *plugin = cls; | 824 | struct Plugin *plugin = cls; |
813 | struct GNUNET_PQ_QueryParam params[] = { | 825 | struct GNUNET_PQ_QueryParam params[] = { |
@@ -817,14 +829,14 @@ postgres_plugin_get_keys(void *cls, | |||
817 | 829 | ||
818 | pkc.proc = proc; | 830 | pkc.proc = proc; |
819 | pkc.proc_cls = proc_cls; | 831 | pkc.proc_cls = proc_cls; |
820 | (void)GNUNET_PQ_eval_prepared_multi_select(plugin->dbh, | 832 | (void) GNUNET_PQ_eval_prepared_multi_select (plugin->dbh, |
821 | "get_keys", | 833 | "get_keys", |
822 | params, | 834 | params, |
823 | &process_keys, | 835 | &process_keys, |
824 | &pkc); | 836 | &pkc); |
825 | proc(proc_cls, | 837 | proc (proc_cls, |
826 | NULL, | 838 | NULL, |
827 | 0); | 839 | 0); |
828 | } | 840 | } |
829 | 841 | ||
830 | 842 | ||
@@ -834,20 +846,20 @@ postgres_plugin_get_keys(void *cls, | |||
834 | * @param cls closure with the `struct Plugin *` | 846 | * @param cls closure with the `struct Plugin *` |
835 | */ | 847 | */ |
836 | static void | 848 | static void |
837 | postgres_plugin_drop(void *cls) | 849 | postgres_plugin_drop (void *cls) |
838 | { | 850 | { |
839 | struct Plugin *plugin = cls; | 851 | struct Plugin *plugin = cls; |
840 | struct GNUNET_PQ_ExecuteStatement es[] = { | 852 | struct GNUNET_PQ_ExecuteStatement es[] = { |
841 | GNUNET_PQ_make_execute("DROP TABLE gn090"), | 853 | GNUNET_PQ_make_execute ("DROP TABLE gn090"), |
842 | GNUNET_PQ_EXECUTE_STATEMENT_END | 854 | GNUNET_PQ_EXECUTE_STATEMENT_END |
843 | }; | 855 | }; |
844 | 856 | ||
845 | if (GNUNET_OK != | 857 | if (GNUNET_OK != |
846 | GNUNET_PQ_exec_statements(plugin->dbh, | 858 | GNUNET_PQ_exec_statements (plugin->dbh, |
847 | es)) | 859 | es)) |
848 | GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, | 860 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, |
849 | "postgres", | 861 | "postgres", |
850 | _("Failed to drop table from database.\n")); | 862 | _ ("Failed to drop table from database.\n")); |
851 | } | 863 | } |
852 | 864 | ||
853 | 865 | ||
@@ -862,53 +874,53 @@ postgres_plugin_drop(void *cls) | |||
862 | * @param cont_cls continuation closure for @a cont | 874 | * @param cont_cls continuation closure for @a cont |
863 | */ | 875 | */ |
864 | static void | 876 | static void |
865 | postgres_plugin_remove_key(void *cls, | 877 | postgres_plugin_remove_key (void *cls, |
866 | const struct GNUNET_HashCode *key, | 878 | const struct GNUNET_HashCode *key, |
867 | uint32_t size, | 879 | uint32_t size, |
868 | const void *data, | 880 | const void *data, |
869 | PluginRemoveCont cont, | 881 | PluginRemoveCont cont, |
870 | void *cont_cls) | 882 | void *cont_cls) |
871 | { | 883 | { |
872 | struct Plugin *plugin = cls; | 884 | struct Plugin *plugin = cls; |
873 | enum GNUNET_DB_QueryStatus ret; | 885 | enum GNUNET_DB_QueryStatus ret; |
874 | struct GNUNET_PQ_QueryParam params[] = { | 886 | struct GNUNET_PQ_QueryParam params[] = { |
875 | GNUNET_PQ_query_param_auto_from_type(key), | 887 | GNUNET_PQ_query_param_auto_from_type (key), |
876 | GNUNET_PQ_query_param_fixed_size(data, size), | 888 | GNUNET_PQ_query_param_fixed_size (data, size), |
877 | GNUNET_PQ_query_param_end | 889 | GNUNET_PQ_query_param_end |
878 | }; | 890 | }; |
879 | 891 | ||
880 | ret = GNUNET_PQ_eval_prepared_non_select(plugin->dbh, | 892 | ret = GNUNET_PQ_eval_prepared_non_select (plugin->dbh, |
881 | "remove", | 893 | "remove", |
882 | params); | 894 | params); |
883 | if (0 > ret) | 895 | if (0 > ret) |
884 | { | 896 | { |
885 | cont(cont_cls, | 897 | cont (cont_cls, |
886 | key, | 898 | key, |
887 | size, | 899 | size, |
888 | GNUNET_SYSERR, | 900 | GNUNET_SYSERR, |
889 | _("Postgress exec failure")); | 901 | _ ("Postgress exec failure")); |
890 | return; | 902 | return; |
891 | } | 903 | } |
892 | if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == ret) | 904 | if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == ret) |
893 | { | 905 | { |
894 | cont(cont_cls, | 906 | cont (cont_cls, |
895 | key, | 907 | key, |
896 | size, | 908 | size, |
897 | GNUNET_NO, | 909 | GNUNET_NO, |
898 | NULL); | 910 | NULL); |
899 | return; | 911 | return; |
900 | } | 912 | } |
901 | plugin->env->duc(plugin->env->cls, | 913 | plugin->env->duc (plugin->env->cls, |
902 | -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD)); | 914 | -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD)); |
903 | GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, | 915 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
904 | "datastore-postgres", | 916 | "datastore-postgres", |
905 | "Deleted %u bytes from database\n", | 917 | "Deleted %u bytes from database\n", |
906 | (unsigned int)size); | 918 | (unsigned int) size); |
907 | cont(cont_cls, | 919 | cont (cont_cls, |
908 | key, | 920 | key, |
909 | size, | 921 | size, |
910 | GNUNET_OK, | 922 | GNUNET_OK, |
911 | NULL); | 923 | NULL); |
912 | } | 924 | } |
913 | 925 | ||
914 | 926 | ||
@@ -919,20 +931,20 @@ postgres_plugin_remove_key(void *cls, | |||
919 | * @return our `struct Plugin *` | 931 | * @return our `struct Plugin *` |
920 | */ | 932 | */ |
921 | void * | 933 | void * |
922 | libgnunet_plugin_datastore_postgres_init(void *cls) | 934 | libgnunet_plugin_datastore_postgres_init (void *cls) |
923 | { | 935 | { |
924 | struct GNUNET_DATASTORE_PluginEnvironment *env = cls; | 936 | struct GNUNET_DATASTORE_PluginEnvironment *env = cls; |
925 | struct GNUNET_DATASTORE_PluginFunctions *api; | 937 | struct GNUNET_DATASTORE_PluginFunctions *api; |
926 | struct Plugin *plugin; | 938 | struct Plugin *plugin; |
927 | 939 | ||
928 | plugin = GNUNET_new(struct Plugin); | 940 | plugin = GNUNET_new (struct Plugin); |
929 | plugin->env = env; | 941 | plugin->env = env; |
930 | if (GNUNET_OK != init_connection(plugin)) | 942 | if (GNUNET_OK != init_connection (plugin)) |
931 | { | 943 | { |
932 | GNUNET_free(plugin); | 944 | GNUNET_free (plugin); |
933 | return NULL; | 945 | return NULL; |
934 | } | 946 | } |
935 | api = GNUNET_new(struct GNUNET_DATASTORE_PluginFunctions); | 947 | api = GNUNET_new (struct GNUNET_DATASTORE_PluginFunctions); |
936 | api->cls = plugin; | 948 | api->cls = plugin; |
937 | api->estimate_size = &postgres_plugin_estimate_size; | 949 | api->estimate_size = &postgres_plugin_estimate_size; |
938 | api->put = &postgres_plugin_put; | 950 | api->put = &postgres_plugin_put; |
@@ -943,9 +955,9 @@ libgnunet_plugin_datastore_postgres_init(void *cls) | |||
943 | api->get_keys = &postgres_plugin_get_keys; | 955 | api->get_keys = &postgres_plugin_get_keys; |
944 | api->drop = &postgres_plugin_drop; | 956 | api->drop = &postgres_plugin_drop; |
945 | api->remove_key = &postgres_plugin_remove_key; | 957 | api->remove_key = &postgres_plugin_remove_key; |
946 | GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, | 958 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, |
947 | "datastore-postgres", | 959 | "datastore-postgres", |
948 | _("Postgres database running\n")); | 960 | _ ("Postgres database running\n")); |
949 | return api; | 961 | return api; |
950 | } | 962 | } |
951 | 963 | ||
@@ -957,14 +969,14 @@ libgnunet_plugin_datastore_postgres_init(void *cls) | |||
957 | * @return always NULL | 969 | * @return always NULL |
958 | */ | 970 | */ |
959 | void * | 971 | void * |
960 | libgnunet_plugin_datastore_postgres_done(void *cls) | 972 | libgnunet_plugin_datastore_postgres_done (void *cls) |
961 | { | 973 | { |
962 | struct GNUNET_DATASTORE_PluginFunctions *api = cls; | 974 | struct GNUNET_DATASTORE_PluginFunctions *api = cls; |
963 | struct Plugin *plugin = api->cls; | 975 | struct Plugin *plugin = api->cls; |
964 | 976 | ||
965 | PQfinish(plugin->dbh); | 977 | PQfinish (plugin->dbh); |
966 | GNUNET_free(plugin); | 978 | GNUNET_free (plugin); |
967 | GNUNET_free(api); | 979 | GNUNET_free (api); |
968 | return NULL; | 980 | return NULL; |
969 | } | 981 | } |
970 | 982 | ||