aboutsummaryrefslogtreecommitdiff
path: root/src/datastore/plugin_datastore_postgres.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-08-15 21:54:06 +0000
committerChristian Grothoff <christian@grothoff.org>2011-08-15 21:54:06 +0000
commit5746309cb4be2073d550ad7a6885e918631dbc38 (patch)
tree89455bc4aadf977816b38df13f990372cd81d71a /src/datastore/plugin_datastore_postgres.c
parent6fd3e715cae09fa6e657c96f1c6f9711ee51f42f (diff)
downloadgnunet-5746309cb4be2073d550ad7a6885e918631dbc38.tar.gz
gnunet-5746309cb4be2073d550ad7a6885e918631dbc38.zip
indentation
Diffstat (limited to 'src/datastore/plugin_datastore_postgres.c')
-rw-r--r--src/datastore/plugin_datastore_postgres.c376
1 files changed, 160 insertions, 216 deletions
diff --git a/src/datastore/plugin_datastore_postgres.c b/src/datastore/plugin_datastore_postgres.c
index b79178dac..d71d5e0ef 100644
--- a/src/datastore/plugin_datastore_postgres.c
+++ b/src/datastore/plugin_datastore_postgres.c
@@ -75,9 +75,7 @@ struct Plugin
75 * @return GNUNET_OK if the result is acceptable 75 * @return GNUNET_OK if the result is acceptable
76 */ 76 */
77static int 77static int
78check_result (struct Plugin *plugin, 78check_result (struct Plugin *plugin, PGresult * ret, int expected_status,
79 PGresult * ret,
80 int expected_status,
81 const char *command, const char *args, int line) 79 const char *command, const char *args, int line)
82{ 80{
83 if (ret == NULL) 81 if (ret == NULL)
@@ -92,9 +90,8 @@ check_result (struct Plugin *plugin,
92 { 90 {
93 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 91 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
94 "datastore-postgres", 92 "datastore-postgres",
95 _("`%s:%s' failed at %s:%d with error: %s"), 93 _("`%s:%s' failed at %s:%d with error: %s"), command, args,
96 command, args, __FILE__, line, 94 __FILE__, line, PQerrorMessage (plugin->dbh));
97 PQerrorMessage (plugin->dbh));
98 PQclear (ret); 95 PQclear (ret);
99 return GNUNET_SYSERR; 96 return GNUNET_SYSERR;
100 } 97 }
@@ -114,8 +111,8 @@ pq_exec (struct Plugin *plugin, const char *sql, int line)
114 PGresult *ret; 111 PGresult *ret;
115 112
116 ret = PQexec (plugin->dbh, sql); 113 ret = PQexec (plugin->dbh, sql);
117 if (GNUNET_OK != check_result (plugin, 114 if (GNUNET_OK !=
118 ret, PGRES_COMMAND_OK, "PQexec", sql, line)) 115 check_result (plugin, ret, PGRES_COMMAND_OK, "PQexec", sql, line))
119 return GNUNET_SYSERR; 116 return GNUNET_SYSERR;
120 PQclear (ret); 117 PQclear (ret);
121 return GNUNET_OK; 118 return GNUNET_OK;
@@ -132,8 +129,8 @@ pq_exec (struct Plugin *plugin, const char *sql, int line)
132 * @return GNUNET_OK on success 129 * @return GNUNET_OK on success
133 */ 130 */
134static int 131static int
135pq_prepare (struct Plugin *plugin, 132pq_prepare (struct Plugin *plugin, const char *name, const char *sql,
136 const char *name, const char *sql, int nparams, int line) 133 int nparams, int line)
137{ 134{
138 PGresult *ret; 135 PGresult *ret;
139 136
@@ -160,8 +157,8 @@ init_connection (struct Plugin *plugin)
160 /* Open database and precompile statements */ 157 /* Open database and precompile statements */
161 conninfo = NULL; 158 conninfo = NULL;
162 (void) GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, 159 (void) GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
163 "datastore-postgres", 160 "datastore-postgres", "CONFIG",
164 "CONFIG", &conninfo); 161 &conninfo);
165 plugin->dbh = PQconnectdb (conninfo == NULL ? "" : conninfo); 162 plugin->dbh = PQconnectdb (conninfo == NULL ? "" : conninfo);
166 if (NULL == plugin->dbh) 163 if (NULL == plugin->dbh)
167 { 164 {
@@ -171,8 +168,7 @@ init_connection (struct Plugin *plugin)
171 } 168 }
172 if (PQstatus (plugin->dbh) != CONNECTION_OK) 169 if (PQstatus (plugin->dbh) != CONNECTION_OK)
173 { 170 {
174 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 171 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "datastore-postgres",
175 "datastore-postgres",
176 _ 172 _
177 ("Unable to initialize Postgres with configuration `%s': %s"), 173 ("Unable to initialize Postgres with configuration `%s': %s"),
178 conninfo, PQerrorMessage (plugin->dbh)); 174 conninfo, PQerrorMessage (plugin->dbh));
@@ -182,24 +178,23 @@ init_connection (struct Plugin *plugin)
182 return GNUNET_SYSERR; 178 return GNUNET_SYSERR;
183 } 179 }
184 GNUNET_free_non_null (conninfo); 180 GNUNET_free_non_null (conninfo);
185 ret = PQexec (plugin->dbh, 181 ret =
186 "CREATE TABLE gn090 (" 182 PQexec (plugin->dbh,
187 " repl INTEGER NOT NULL DEFAULT 0," 183 "CREATE TABLE gn090 (" " repl INTEGER NOT NULL DEFAULT 0,"
188 " type INTEGER NOT NULL DEFAULT 0," 184 " type INTEGER NOT NULL DEFAULT 0,"
189 " prio INTEGER NOT NULL DEFAULT 0," 185 " prio INTEGER NOT NULL DEFAULT 0,"
190 " anonLevel INTEGER NOT NULL DEFAULT 0," 186 " anonLevel INTEGER NOT NULL DEFAULT 0,"
191 " expire BIGINT NOT NULL DEFAULT 0," 187 " expire BIGINT NOT NULL DEFAULT 0,"
192 " rvalue BIGINT NOT NULL DEFAULT 0," 188 " rvalue BIGINT NOT NULL DEFAULT 0,"
193 " hash BYTEA NOT NULL DEFAULT ''," 189 " hash BYTEA NOT NULL DEFAULT '',"
194 " vhash BYTEA NOT NULL DEFAULT ''," 190 " vhash BYTEA NOT NULL DEFAULT '',"
195 " value BYTEA NOT NULL DEFAULT '')" "WITH OIDS"); 191 " value BYTEA NOT NULL DEFAULT '')" "WITH OIDS");
196 if ((ret == NULL) || ((PQresultStatus (ret) != PGRES_COMMAND_OK) && (0 != strcmp ("42P07", /* duplicate table */ 192 if ((ret == NULL) || ((PQresultStatus (ret) != PGRES_COMMAND_OK) && (0 != strcmp ("42P07", /* duplicate table */
197 PQresultErrorField 193 PQresultErrorField
198 (ret, 194 (ret,
199 PG_DIAG_SQLSTATE))))) 195 PG_DIAG_SQLSTATE)))))
200 { 196 {
201 (void) check_result (plugin, 197 (void) check_result (plugin, ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090",
202 ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090",
203 __LINE__); 198 __LINE__);
204 PQfinish (plugin->dbh); 199 PQfinish (plugin->dbh);
205 plugin->dbh = NULL; 200 plugin->dbh = NULL;
@@ -211,12 +206,12 @@ init_connection (struct Plugin *plugin)
211 pq_exec (plugin, "CREATE INDEX idx_hash ON gn090 (hash)", __LINE__)) || 206 pq_exec (plugin, "CREATE INDEX idx_hash ON gn090 (hash)", __LINE__)) ||
212 (GNUNET_OK != 207 (GNUNET_OK !=
213 pq_exec (plugin, "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)", 208 pq_exec (plugin, "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)",
214 __LINE__)) 209 __LINE__)) ||
215 || (GNUNET_OK != 210 (GNUNET_OK !=
216 pq_exec (plugin, "CREATE INDEX idx_prio ON gn090 (prio)", __LINE__)) 211 pq_exec (plugin, "CREATE INDEX idx_prio ON gn090 (prio)", __LINE__)) ||
217 || (GNUNET_OK != 212 (GNUNET_OK !=
218 pq_exec (plugin, "CREATE INDEX idx_expire ON gn090 (expire)", 213 pq_exec (plugin, "CREATE INDEX idx_expire ON gn090 (expire)",
219 __LINE__)) || 214 __LINE__)) ||
220 (GNUNET_OK != 215 (GNUNET_OK !=
221 pq_exec (plugin, 216 pq_exec (plugin,
222 "CREATE INDEX idx_prio_anon ON gn090 (prio,anonLevel)", 217 "CREATE INDEX idx_prio_anon ON gn090 (prio,anonLevel)",
@@ -239,11 +234,12 @@ init_connection (struct Plugin *plugin)
239 } 234 }
240 } 235 }
241 PQclear (ret); 236 PQclear (ret);
242 ret = PQexec (plugin->dbh, 237 ret =
243 "ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL"); 238 PQexec (plugin->dbh,
239 "ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL");
244 if (GNUNET_OK != 240 if (GNUNET_OK !=
245 check_result (plugin, 241 check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090",
246 ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090", __LINE__)) 242 __LINE__))
247 { 243 {
248 PQfinish (plugin->dbh); 244 PQfinish (plugin->dbh);
249 plugin->dbh = NULL; 245 plugin->dbh = NULL;
@@ -252,8 +248,8 @@ init_connection (struct Plugin *plugin)
252 PQclear (ret); 248 PQclear (ret);
253 ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN"); 249 ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN");
254 if (GNUNET_OK != 250 if (GNUNET_OK !=
255 check_result (plugin, 251 check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090",
256 ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090", __LINE__)) 252 __LINE__))
257 { 253 {
258 PQfinish (plugin->dbh); 254 PQfinish (plugin->dbh);
259 plugin->dbh = NULL; 255 plugin->dbh = NULL;
@@ -262,8 +258,8 @@ init_connection (struct Plugin *plugin)
262 PQclear (ret); 258 PQclear (ret);
263 ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN"); 259 ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN");
264 if (GNUNET_OK != 260 if (GNUNET_OK !=
265 check_result (plugin, 261 check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090",
266 ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090", __LINE__)) 262 __LINE__))
267 { 263 {
268 PQfinish (plugin->dbh); 264 PQfinish (plugin->dbh);
269 plugin->dbh = NULL; 265 plugin->dbh = NULL;
@@ -271,86 +267,57 @@ init_connection (struct Plugin *plugin)
271 } 267 }
272 PQclear (ret); 268 PQclear (ret);
273 if ((GNUNET_OK != 269 if ((GNUNET_OK !=
274 pq_prepare (plugin, 270 pq_prepare (plugin, "getvt",
275 "getvt",
276 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " 271 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
277 "WHERE hash=$1 AND vhash=$2 AND type=$3 " 272 "WHERE hash=$1 AND vhash=$2 AND type=$3 "
278 "ORDER BY oid ASC LIMIT 1 OFFSET $4", 273 "ORDER BY oid ASC LIMIT 1 OFFSET $4", 4, __LINE__)) ||
279 4,
280 __LINE__)) ||
281 (GNUNET_OK != 274 (GNUNET_OK !=
282 pq_prepare (plugin, 275 pq_prepare (plugin, "gett",
283 "gett",
284 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " 276 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
285 "WHERE hash=$1 AND type=$2 " 277 "WHERE hash=$1 AND type=$2 "
286 "ORDER BY oid ASC LIMIT 1 OFFSET $3", 278 "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3, __LINE__)) ||
287 3,
288 __LINE__)) ||
289 (GNUNET_OK != 279 (GNUNET_OK !=
290 pq_prepare (plugin, 280 pq_prepare (plugin, "getv",
291 "getv",
292 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " 281 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
293 "WHERE hash=$1 AND vhash=$2 " 282 "WHERE hash=$1 AND vhash=$2 "
294 "ORDER BY oid ASC LIMIT 1 OFFSET $3", 283 "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3, __LINE__)) ||
295 3,
296 __LINE__)) ||
297 (GNUNET_OK != 284 (GNUNET_OK !=
298 pq_prepare (plugin, 285 pq_prepare (plugin, "get",
299 "get",
300 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " 286 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
301 "WHERE hash=$1 " 287 "WHERE hash=$1 " "ORDER BY oid ASC LIMIT 1 OFFSET $2", 2,
302 "ORDER BY oid ASC LIMIT 1 OFFSET $2",
303 2,
304 __LINE__)) || 288 __LINE__)) ||
305 (GNUNET_OK != 289 (GNUNET_OK !=
306 pq_prepare (plugin, 290 pq_prepare (plugin, "put",
307 "put",
308 "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) " 291 "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) "
309 "VALUES ($1, $2, $3, $4, $5, RANDOM(), $6, $7, $8)", 292 "VALUES ($1, $2, $3, $4, $5, RANDOM(), $6, $7, $8)", 9,
310 9,
311 __LINE__)) || 293 __LINE__)) ||
312 (GNUNET_OK != 294 (GNUNET_OK !=
313 pq_prepare (plugin, 295 pq_prepare (plugin, "update",
314 "update",
315 "UPDATE gn090 SET prio = prio + $1, expire = CASE WHEN expire < $2 THEN $2 ELSE expire END " 296 "UPDATE gn090 SET prio = prio + $1, expire = CASE WHEN expire < $2 THEN $2 ELSE expire END "
316 "WHERE oid = $3", 297 "WHERE oid = $3", 3, __LINE__)) ||
317 3,
318 __LINE__)) ||
319 (GNUNET_OK != 298 (GNUNET_OK !=
320 pq_prepare (plugin, 299 pq_prepare (plugin, "decrepl",
321 "decrepl",
322 "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) " 300 "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) "
323 "WHERE oid = $1", 301 "WHERE oid = $1", 1, __LINE__)) ||
324 1,
325 __LINE__)) ||
326 (GNUNET_OK != 302 (GNUNET_OK !=
327 pq_prepare (plugin, 303 pq_prepare (plugin, "select_non_anonymous",
328 "select_non_anonymous",
329 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " 304 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
330 "WHERE anonLevel = 0 AND type = $1 ORDER BY oid DESC LIMIT 1 OFFSET $2", 305 "WHERE anonLevel = 0 AND type = $1 ORDER BY oid DESC LIMIT 1 OFFSET $2",
331 1, 306 1, __LINE__)) ||
332 __LINE__)) ||
333 (GNUNET_OK != 307 (GNUNET_OK !=
334 pq_prepare (plugin, 308 pq_prepare (plugin, "select_expiration_order",
335 "select_expiration_order",
336 "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " 309 "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
337 "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) " 310 "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) " "UNION "
338 "UNION "
339 "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " 311 "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
340 "ORDER BY prio ASC LIMIT 1) " 312 "ORDER BY prio ASC LIMIT 1) " "ORDER BY expire ASC LIMIT 1",
341 "ORDER BY expire ASC LIMIT 1", 313 1, __LINE__)) ||
342 1,
343 __LINE__)) ||
344 (GNUNET_OK != 314 (GNUNET_OK !=
345 pq_prepare (plugin, 315 pq_prepare (plugin, "select_replication_order",
346 "select_replication_order",
347 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " 316 "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
348 "ORDER BY repl DESC,RANDOM() LIMIT 1", 317 "ORDER BY repl DESC,RANDOM() LIMIT 1", 0, __LINE__)) ||
349 0,
350 __LINE__)) ||
351 (GNUNET_OK != 318 (GNUNET_OK !=
352 pq_prepare (plugin, 319 pq_prepare (plugin, "delrow", "DELETE FROM gn090 " "WHERE oid=$1", 1,
353 "delrow", "DELETE FROM gn090 " "WHERE oid=$1", 1, __LINE__))) 320 __LINE__)))
354 { 321 {
355 PQfinish (plugin->dbh); 322 PQfinish (plugin->dbh);
356 plugin->dbh = NULL; 323 plugin->dbh = NULL;
@@ -378,12 +345,11 @@ delete_by_rowid (struct Plugin *plugin, unsigned int rowid)
378 PGresult *ret; 345 PGresult *ret;
379 346
380 browid = htonl (rowid); 347 browid = htonl (rowid);
381 ret = PQexecPrepared (plugin->dbh, 348 ret =
382 "delrow", 349 PQexecPrepared (plugin->dbh, "delrow", 1, paramValues, paramLengths,
383 1, paramValues, paramLengths, paramFormats, 1); 350 paramFormats, 1);
384 if (GNUNET_OK != 351 if (GNUNET_OK !=
385 check_result (plugin, 352 check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow",
386 ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow",
387 __LINE__)) 353 __LINE__))
388 { 354 {
389 return GNUNET_SYSERR; 355 return GNUNET_SYSERR;
@@ -407,18 +373,17 @@ postgres_plugin_estimate_size (void *cls)
407 unsigned long long total; 373 unsigned long long total;
408 PGresult *ret; 374 PGresult *ret;
409 375
410 ret = PQexecParams (plugin->dbh, 376 ret =
411 "SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090", 377 PQexecParams (plugin->dbh,
412 0, NULL, NULL, NULL, NULL, 1); 378 "SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090", 0,
413 if (GNUNET_OK != check_result (plugin, 379 NULL, NULL, NULL, NULL, 1);
414 ret, 380 if (GNUNET_OK !=
415 PGRES_TUPLES_OK, 381 check_result (plugin, ret, PGRES_TUPLES_OK, "PQexecParams", "get_size",
416 "PQexecParams", "get_size", __LINE__)) 382 __LINE__))
417 { 383 {
418 return 0; 384 return 0;
419 } 385 }
420 if ((PQntuples (ret) != 1) || 386 if ((PQntuples (ret) != 1) || (PQnfields (ret) != 1) ||
421 (PQnfields (ret) != 1) ||
422 (PQgetlength (ret, 0, 0) != sizeof (unsigned long long))) 387 (PQgetlength (ret, 0, 0) != sizeof (unsigned long long)))
423 { 388 {
424 GNUNET_break (0); 389 GNUNET_break (0);
@@ -447,13 +412,9 @@ postgres_plugin_estimate_size (void *cls)
447 * @return GNUNET_OK on success 412 * @return GNUNET_OK on success
448 */ 413 */
449static int 414static int
450postgres_plugin_put (void *cls, 415postgres_plugin_put (void *cls, const GNUNET_HashCode * key, uint32_t size,
451 const GNUNET_HashCode * key, 416 const void *data, enum GNUNET_BLOCK_Type type,
452 uint32_t size, 417 uint32_t priority, uint32_t anonymity,
453 const void *data,
454 enum GNUNET_BLOCK_Type type,
455 uint32_t priority,
456 uint32_t anonymity,
457 uint32_t replication, 418 uint32_t replication,
458 struct GNUNET_TIME_Absolute expiration, char **msg) 419 struct GNUNET_TIME_Absolute expiration, char **msg)
459{ 420{
@@ -489,17 +450,17 @@ postgres_plugin_put (void *cls,
489 const int paramFormats[] = { 1, 1, 1, 1, 1, 1, 1, 1 }; 450 const int paramFormats[] = { 1, 1, 1, 1, 1, 1, 1, 1 };
490 451
491 GNUNET_CRYPTO_hash (data, size, &vhash); 452 GNUNET_CRYPTO_hash (data, size, &vhash);
492 ret = PQexecPrepared (plugin->dbh, 453 ret =
493 "put", 8, paramValues, paramLengths, paramFormats, 1); 454 PQexecPrepared (plugin->dbh, "put", 8, paramValues, paramLengths,
494 if (GNUNET_OK != check_result (plugin, ret, 455 paramFormats, 1);
495 PGRES_COMMAND_OK, 456 if (GNUNET_OK !=
496 "PQexecPrepared", "put", __LINE__)) 457 check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put",
458 __LINE__))
497 return GNUNET_SYSERR; 459 return GNUNET_SYSERR;
498 PQclear (ret); 460 PQclear (ret);
499 plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD); 461 plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
500#if DEBUG_POSTGRES 462#if DEBUG_POSTGRES
501 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 463 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
502 "datastore-postgres",
503 "Stored %u bytes in database\n", (unsigned int) size); 464 "Stored %u bytes in database\n", (unsigned int) size);
504#endif 465#endif
505 return GNUNET_OK; 466 return GNUNET_OK;
@@ -517,9 +478,8 @@ postgres_plugin_put (void *cls,
517 * @param line line number for error messages 478 * @param line line number for error messages
518 */ 479 */
519static void 480static void
520process_result (struct Plugin *plugin, 481process_result (struct Plugin *plugin, PluginDatumProcessor proc,
521 PluginDatumProcessor proc, void *proc_cls, 482 void *proc_cls, PGresult * res, int line)
522 PGresult * res, int line)
523{ 483{
524 int iret; 484 int iret;
525 enum GNUNET_BLOCK_Type type; 485 enum GNUNET_BLOCK_Type type;
@@ -530,14 +490,12 @@ process_result (struct Plugin *plugin,
530 struct GNUNET_TIME_Absolute expiration_time; 490 struct GNUNET_TIME_Absolute expiration_time;
531 GNUNET_HashCode key; 491 GNUNET_HashCode key;
532 492
533 if (GNUNET_OK != check_result (plugin, 493 if (GNUNET_OK !=
534 res, 494 check_result (plugin, res, PGRES_TUPLES_OK, "PQexecPrepared", "select",
535 PGRES_TUPLES_OK, 495 line))
536 "PQexecPrepared", "select", line))
537 { 496 {
538#if DEBUG_POSTGRES 497#if DEBUG_POSTGRES
539 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 498 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
540 "datastore-postgres",
541 "Ending iteration (postgres error)\n"); 499 "Ending iteration (postgres error)\n");
542#endif 500#endif
543 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 501 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
@@ -548,16 +506,14 @@ process_result (struct Plugin *plugin,
548 { 506 {
549 /* no result */ 507 /* no result */
550#if DEBUG_POSTGRES 508#if DEBUG_POSTGRES
551 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 509 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
552 "datastore-postgres",
553 "Ending iteration (no more results)\n"); 510 "Ending iteration (no more results)\n");
554#endif 511#endif
555 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 512 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
556 PQclear (res); 513 PQclear (res);
557 return; 514 return;
558 } 515 }
559 if ((1 != PQntuples (res)) || 516 if ((1 != PQntuples (res)) || (7 != PQnfields (res)) ||
560 (7 != PQnfields (res)) ||
561 (sizeof (uint32_t) != PQfsize (res, 0)) || 517 (sizeof (uint32_t) != PQfsize (res, 0)) ||
562 (sizeof (uint32_t) != PQfsize (res, 6))) 518 (sizeof (uint32_t) != PQfsize (res, 6)))
563 { 519 {
@@ -588,17 +544,14 @@ process_result (struct Plugin *plugin,
588 memcpy (&key, PQgetvalue (res, 0, 4), sizeof (GNUNET_HashCode)); 544 memcpy (&key, PQgetvalue (res, 0, 4), sizeof (GNUNET_HashCode));
589 size = PQgetlength (res, 0, 5); 545 size = PQgetlength (res, 0, 5);
590#if DEBUG_POSTGRES 546#if DEBUG_POSTGRES
591 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 547 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
592 "datastore-postgres",
593 "Found result of size %u bytes and type %u in database\n", 548 "Found result of size %u bytes and type %u in database\n",
594 (unsigned int) size, (unsigned int) type); 549 (unsigned int) size, (unsigned int) type);
595#endif 550#endif
596 iret = proc (proc_cls, 551 iret =
597 &key, 552 proc (proc_cls, &key, size, PQgetvalue (res, 0, 5),
598 size, 553 (enum GNUNET_BLOCK_Type) type, priority, anonymity, expiration_time,
599 PQgetvalue (res, 0, 5), 554 rowid);
600 (enum GNUNET_BLOCK_Type) type,
601 priority, anonymity, expiration_time, rowid);
602 PQclear (res); 555 PQclear (res);
603 if (iret == GNUNET_NO) 556 if (iret == GNUNET_NO)
604 { 557 {
@@ -609,16 +562,14 @@ process_result (struct Plugin *plugin,
609 if (GNUNET_OK == delete_by_rowid (plugin, rowid)) 562 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
610 { 563 {
611#if DEBUG_POSTGRES 564#if DEBUG_POSTGRES
612 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 565 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
613 "datastore-postgres",
614 "Deleting %u bytes from database\n", 566 "Deleting %u bytes from database\n",
615 (unsigned int) size); 567 (unsigned int) size);
616#endif 568#endif
617 plugin->env->duc (plugin->env->cls, 569 plugin->env->duc (plugin->env->cls,
618 -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD)); 570 -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
619#if DEBUG_POSTGRES 571#if DEBUG_POSTGRES
620 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 572 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
621 "datastore-postgres",
622 "Deleted %u bytes from database\n", (unsigned int) size); 573 "Deleted %u bytes from database\n", (unsigned int) size);
623#endif 574#endif
624 } 575 }
@@ -646,12 +597,11 @@ process_result (struct Plugin *plugin,
646 * @param proc_cls closure for iter 597 * @param proc_cls closure for iter
647 */ 598 */
648static void 599static void
649postgres_plugin_get_key (void *cls, 600postgres_plugin_get_key (void *cls, uint64_t offset,
650 uint64_t offset,
651 const GNUNET_HashCode * key, 601 const GNUNET_HashCode * key,
652 const GNUNET_HashCode * vhash, 602 const GNUNET_HashCode * vhash,
653 enum GNUNET_BLOCK_Type type, 603 enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc,
654 PluginDatumProcessor proc, void *proc_cls) 604 void *proc_cls)
655{ 605{
656 struct Plugin *plugin = cls; 606 struct Plugin *plugin = cls;
657 const int paramFormats[] = { 1, 1, 1, 1, 1 }; 607 const int paramFormats[] = { 1, 1, 1, 1, 1 };
@@ -680,9 +630,10 @@ postgres_plugin_get_key (void *cls,
680 paramLengths[3] = sizeof (blimit_off); 630 paramLengths[3] = sizeof (blimit_off);
681 nparams = 4; 631 nparams = 4;
682 pname = "getvt"; 632 pname = "getvt";
683 ret = PQexecParams (plugin->dbh, 633 ret =
684 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2 AND type=$3", 634 PQexecParams (plugin->dbh,
685 3, NULL, paramValues, paramLengths, paramFormats, 1); 635 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2 AND type=$3",
636 3, NULL, paramValues, paramLengths, paramFormats, 1);
686 } 637 }
687 else 638 else
688 { 639 {
@@ -692,9 +643,10 @@ postgres_plugin_get_key (void *cls,
692 paramLengths[2] = sizeof (blimit_off); 643 paramLengths[2] = sizeof (blimit_off);
693 nparams = 3; 644 nparams = 3;
694 pname = "gett"; 645 pname = "gett";
695 ret = PQexecParams (plugin->dbh, 646 ret =
696 "SELECT count(*) FROM gn090 WHERE hash=$1 AND type=$2", 647 PQexecParams (plugin->dbh,
697 2, NULL, paramValues, paramLengths, paramFormats, 1); 648 "SELECT count(*) FROM gn090 WHERE hash=$1 AND type=$2",
649 2, NULL, paramValues, paramLengths, paramFormats, 1);
698 } 650 }
699 } 651 }
700 else 652 else
@@ -707,9 +659,10 @@ postgres_plugin_get_key (void *cls,
707 paramLengths[2] = sizeof (blimit_off); 659 paramLengths[2] = sizeof (blimit_off);
708 nparams = 3; 660 nparams = 3;
709 pname = "getv"; 661 pname = "getv";
710 ret = PQexecParams (plugin->dbh, 662 ret =
711 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2", 663 PQexecParams (plugin->dbh,
712 2, NULL, paramValues, paramLengths, paramFormats, 1); 664 "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2",
665 2, NULL, paramValues, paramLengths, paramFormats, 1);
713 } 666 }
714 else 667 else
715 { 668 {
@@ -717,21 +670,19 @@ postgres_plugin_get_key (void *cls,
717 paramLengths[1] = sizeof (blimit_off); 670 paramLengths[1] = sizeof (blimit_off);
718 nparams = 2; 671 nparams = 2;
719 pname = "get"; 672 pname = "get";
720 ret = PQexecParams (plugin->dbh, 673 ret =
721 "SELECT count(*) FROM gn090 WHERE hash=$1", 674 PQexecParams (plugin->dbh, "SELECT count(*) FROM gn090 WHERE hash=$1",
722 1, NULL, paramValues, paramLengths, paramFormats, 1); 675 1, NULL, paramValues, paramLengths, paramFormats, 1);
723 } 676 }
724 } 677 }
725 if (GNUNET_OK != check_result (plugin, 678 if (GNUNET_OK !=
726 ret, 679 check_result (plugin, ret, PGRES_TUPLES_OK, "PQexecParams", pname,
727 PGRES_TUPLES_OK, 680 __LINE__))
728 "PQexecParams", pname, __LINE__))
729 { 681 {
730 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 682 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
731 return; 683 return;
732 } 684 }
733 if ((PQntuples (ret) != 1) || 685 if ((PQntuples (ret) != 1) || (PQnfields (ret) != 1) ||
734 (PQnfields (ret) != 1) ||
735 (PQgetlength (ret, 0, 0) != sizeof (unsigned long long))) 686 (PQgetlength (ret, 0, 0) != sizeof (unsigned long long)))
736 { 687 {
737 GNUNET_break (0); 688 GNUNET_break (0);
@@ -747,9 +698,9 @@ postgres_plugin_get_key (void *cls,
747 return; 698 return;
748 } 699 }
749 blimit_off = GNUNET_htonll (offset % total); 700 blimit_off = GNUNET_htonll (offset % total);
750 ret = PQexecPrepared (plugin->dbh, 701 ret =
751 pname, 702 PQexecPrepared (plugin->dbh, pname, nparams, paramValues, paramLengths,
752 nparams, paramValues, paramLengths, paramFormats, 1); 703 paramFormats, 1);
753 process_result (plugin, proc, proc_cls, ret, __LINE__); 704 process_result (plugin, proc, proc_cls, ret, __LINE__);
754} 705}
755 706
@@ -766,8 +717,7 @@ postgres_plugin_get_key (void *cls,
766 * @param proc_cls closure for proc 717 * @param proc_cls closure for proc
767 */ 718 */
768static void 719static void
769postgres_plugin_get_zero_anonymity (void *cls, 720postgres_plugin_get_zero_anonymity (void *cls, uint64_t offset,
770 uint64_t offset,
771 enum GNUNET_BLOCK_Type type, 721 enum GNUNET_BLOCK_Type type,
772 PluginDatumProcessor proc, void *proc_cls) 722 PluginDatumProcessor proc, void *proc_cls)
773{ 723{
@@ -781,9 +731,9 @@ postgres_plugin_get_zero_anonymity (void *cls,
781 731
782 btype = htonl ((uint32_t) type); 732 btype = htonl ((uint32_t) type);
783 boff = GNUNET_htonll (offset); 733 boff = GNUNET_htonll (offset);
784 ret = PQexecPrepared (plugin->dbh, 734 ret =
785 "select_non_anonymous", 735 PQexecPrepared (plugin->dbh, "select_non_anonymous", 2, paramValues,
786 2, paramValues, paramLengths, paramFormats, 1); 736 paramLengths, paramFormats, 1);
787 process_result (plugin, proc, proc_cls, ret, __LINE__); 737 process_result (plugin, proc, proc_cls, ret, __LINE__);
788} 738}
789 739
@@ -833,14 +783,10 @@ struct ReplCtx
833 * GNUNET_NO to delete the item and continue (if supported) 783 * GNUNET_NO to delete the item and continue (if supported)
834 */ 784 */
835static int 785static int
836repl_proc (void *cls, 786repl_proc (void *cls, const GNUNET_HashCode * key, uint32_t size,
837 const GNUNET_HashCode * key, 787 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
838 uint32_t size, 788 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
839 const void *data, 789 uint64_t uid)
840 enum GNUNET_BLOCK_Type type,
841 uint32_t priority,
842 uint32_t anonymity,
843 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
844{ 790{
845 struct ReplCtx *rc = cls; 791 struct ReplCtx *rc = cls;
846 struct Plugin *plugin = rc->plugin; 792 struct Plugin *plugin = rc->plugin;
@@ -848,8 +794,9 @@ repl_proc (void *cls,
848 PGresult *qret; 794 PGresult *qret;
849 uint32_t boid; 795 uint32_t boid;
850 796
851 ret = rc->proc (rc->proc_cls, 797 ret =
852 key, size, data, type, priority, anonymity, expiration, uid); 798 rc->proc (rc->proc_cls, key, size, data, type, priority, anonymity,
799 expiration, uid);
853 if (NULL != key) 800 if (NULL != key)
854 { 801 {
855 boid = htonl ((uint32_t) uid); 802 boid = htonl ((uint32_t) uid);
@@ -860,13 +807,12 @@ repl_proc (void *cls,
860 sizeof (boid), 807 sizeof (boid),
861 }; 808 };
862 const int paramFormats[] = { 1 }; 809 const int paramFormats[] = { 1 };
863 qret = PQexecPrepared (plugin->dbh, 810 qret =
864 "decrepl", 811 PQexecPrepared (plugin->dbh, "decrepl", 1, paramValues, paramLengths,
865 1, paramValues, paramLengths, paramFormats, 1); 812 paramFormats, 1);
866 if (GNUNET_OK != check_result (plugin, 813 if (GNUNET_OK !=
867 qret, 814 check_result (plugin, qret, PGRES_COMMAND_OK, "PQexecPrepared",
868 PGRES_COMMAND_OK, 815 "decrepl", __LINE__))
869 "PQexecPrepared", "decrepl", __LINE__))
870 return GNUNET_SYSERR; 816 return GNUNET_SYSERR;
871 PQclear (qret); 817 PQclear (qret);
872 } 818 }
@@ -885,8 +831,8 @@ repl_proc (void *cls,
885 * @param proc_cls closure for proc 831 * @param proc_cls closure for proc
886 */ 832 */
887static void 833static void
888postgres_plugin_get_replication (void *cls, 834postgres_plugin_get_replication (void *cls, PluginDatumProcessor proc,
889 PluginDatumProcessor proc, void *proc_cls) 835 void *proc_cls)
890{ 836{
891 struct Plugin *plugin = cls; 837 struct Plugin *plugin = cls;
892 struct ReplCtx rc; 838 struct ReplCtx rc;
@@ -895,8 +841,9 @@ postgres_plugin_get_replication (void *cls,
895 rc.plugin = plugin; 841 rc.plugin = plugin;
896 rc.proc = proc; 842 rc.proc = proc;
897 rc.proc_cls = proc_cls; 843 rc.proc_cls = proc_cls;
898 ret = PQexecPrepared (plugin->dbh, 844 ret =
899 "select_replication_order", 0, NULL, NULL, NULL, 1); 845 PQexecPrepared (plugin->dbh, "select_replication_order", 0, NULL, NULL,
846 NULL, 1);
900 process_result (plugin, &repl_proc, &rc, ret, __LINE__); 847 process_result (plugin, &repl_proc, &rc, ret, __LINE__);
901} 848}
902 849
@@ -910,8 +857,8 @@ postgres_plugin_get_replication (void *cls,
910 * @param proc_cls closure for proc 857 * @param proc_cls closure for proc
911 */ 858 */
912static void 859static void
913postgres_plugin_get_expiration (void *cls, 860postgres_plugin_get_expiration (void *cls, PluginDatumProcessor proc,
914 PluginDatumProcessor proc, void *proc_cls) 861 void *proc_cls)
915{ 862{
916 struct Plugin *plugin = cls; 863 struct Plugin *plugin = cls;
917 uint64_t btime; 864 uint64_t btime;
@@ -921,9 +868,9 @@ postgres_plugin_get_expiration (void *cls,
921 PGresult *ret; 868 PGresult *ret;
922 869
923 btime = GNUNET_htonll (GNUNET_TIME_absolute_get ().abs_value); 870 btime = GNUNET_htonll (GNUNET_TIME_absolute_get ().abs_value);
924 ret = PQexecPrepared (plugin->dbh, 871 ret =
925 "select_expiration_order", 872 PQexecPrepared (plugin->dbh, "select_expiration_order", 1, paramValues,
926 1, paramValues, paramLengths, paramFormats, 1); 873 paramLengths, paramFormats, 1);
927 process_result (plugin, proc, proc_cls, ret, __LINE__); 874 process_result (plugin, proc, proc_cls, ret, __LINE__);
928} 875}
929 876
@@ -952,10 +899,8 @@ postgres_plugin_get_expiration (void *cls,
952 * @return GNUNET_OK on success 899 * @return GNUNET_OK on success
953 */ 900 */
954static int 901static int
955postgres_plugin_update (void *cls, 902postgres_plugin_update (void *cls, uint64_t uid, int delta,
956 uint64_t uid, 903 struct GNUNET_TIME_Absolute expire, char **msg)
957 int delta, struct GNUNET_TIME_Absolute expire,
958 char **msg)
959{ 904{
960 struct Plugin *plugin = cls; 905 struct Plugin *plugin = cls;
961 PGresult *ret; 906 PGresult *ret;
@@ -975,13 +920,12 @@ postgres_plugin_update (void *cls,
975 }; 920 };
976 const int paramFormats[] = { 1, 1, 1 }; 921 const int paramFormats[] = { 1, 1, 1 };
977 922
978 ret = PQexecPrepared (plugin->dbh, 923 ret =
979 "update", 924 PQexecPrepared (plugin->dbh, "update", 3, paramValues, paramLengths,
980 3, paramValues, paramLengths, paramFormats, 1); 925 paramFormats, 1);
981 if (GNUNET_OK != check_result (plugin, 926 if (GNUNET_OK !=
982 ret, 927 check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "update",
983 PGRES_COMMAND_OK, 928 __LINE__))
984 "PQexecPrepared", "update", __LINE__))
985 return GNUNET_SYSERR; 929 return GNUNET_SYSERR;
986 PQclear (ret); 930 PQclear (ret);
987 return GNUNET_OK; 931 return GNUNET_OK;
@@ -1030,8 +974,8 @@ libgnunet_plugin_datastore_postgres_init (void *cls)
1030 api->get_expiration = &postgres_plugin_get_expiration; 974 api->get_expiration = &postgres_plugin_get_expiration;
1031 api->get_zero_anonymity = &postgres_plugin_get_zero_anonymity; 975 api->get_zero_anonymity = &postgres_plugin_get_zero_anonymity;
1032 api->drop = &postgres_plugin_drop; 976 api->drop = &postgres_plugin_drop;
1033 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, 977 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "datastore-postgres",
1034 "datastore-postgres", _("Postgres database running\n")); 978 _("Postgres database running\n"));
1035 return api; 979 return api;
1036} 980}
1037 981