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