aboutsummaryrefslogtreecommitdiff
path: root/src/datacache/plugin_datacache_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/datacache/plugin_datacache_postgres.c
parent6fd3e715cae09fa6e657c96f1c6f9711ee51f42f (diff)
downloadgnunet-5746309cb4be2073d550ad7a6885e918631dbc38.tar.gz
gnunet-5746309cb4be2073d550ad7a6885e918631dbc38.zip
indentation
Diffstat (limited to 'src/datacache/plugin_datacache_postgres.c')
-rw-r--r--src/datacache/plugin_datacache_postgres.c170
1 files changed, 69 insertions, 101 deletions
diff --git a/src/datacache/plugin_datacache_postgres.c b/src/datacache/plugin_datacache_postgres.c
index fdc377b8f..c5730078a 100644
--- a/src/datacache/plugin_datacache_postgres.c
+++ b/src/datacache/plugin_datacache_postgres.c
@@ -61,9 +61,7 @@ struct Plugin
61 * @return GNUNET_OK if the result is acceptable 61 * @return GNUNET_OK if the result is acceptable
62 */ 62 */
63static int 63static int
64check_result (struct Plugin *plugin, 64check_result (struct Plugin *plugin, PGresult * ret, int expected_status,
65 PGresult * ret,
66 int expected_status,
67 const char *command, const char *args, int line) 65 const char *command, const char *args, int line)
68{ 66{
69 if (ret == NULL) 67 if (ret == NULL)
@@ -78,9 +76,8 @@ check_result (struct Plugin *plugin,
78 { 76 {
79 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 77 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
80 "datastore-postgres", 78 "datastore-postgres",
81 _("`%s:%s' failed at %s:%d with error: %s"), 79 _("`%s:%s' failed at %s:%d with error: %s"), command, args,
82 command, args, __FILE__, line, 80 __FILE__, line, PQerrorMessage (plugin->dbh));
83 PQerrorMessage (plugin->dbh));
84 PQclear (ret); 81 PQclear (ret);
85 return GNUNET_SYSERR; 82 return GNUNET_SYSERR;
86 } 83 }
@@ -97,8 +94,8 @@ pq_exec (struct Plugin *plugin, const char *sql, int line)
97 PGresult *ret; 94 PGresult *ret;
98 95
99 ret = PQexec (plugin->dbh, sql); 96 ret = PQexec (plugin->dbh, sql);
100 if (GNUNET_OK != check_result (plugin, 97 if (GNUNET_OK !=
101 ret, PGRES_COMMAND_OK, "PQexec", sql, line)) 98 check_result (plugin, ret, PGRES_COMMAND_OK, "PQexec", sql, line))
102 return GNUNET_SYSERR; 99 return GNUNET_SYSERR;
103 PQclear (ret); 100 PQclear (ret);
104 return GNUNET_OK; 101 return GNUNET_OK;
@@ -109,8 +106,8 @@ pq_exec (struct Plugin *plugin, const char *sql, int line)
109 * Prepare SQL statement. 106 * Prepare SQL statement.
110 */ 107 */
111static int 108static int
112pq_prepare (struct Plugin *plugin, 109pq_prepare (struct Plugin *plugin, const char *name, const char *sql,
113 const char *name, const char *sql, int nparms, int line) 110 int nparms, int line)
114{ 111{
115 PGresult *ret; 112 PGresult *ret;
116 113
@@ -136,8 +133,8 @@ init_connection (struct Plugin *plugin)
136 /* Open database and precompile statements */ 133 /* Open database and precompile statements */
137 if (GNUNET_OK != 134 if (GNUNET_OK !=
138 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, 135 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
139 "datacache-postgres", 136 "datacache-postgres", "CONFIG",
140 "CONFIG", &conninfo)) 137 &conninfo))
141 conninfo = NULL; 138 conninfo = NULL;
142 plugin->dbh = PQconnectdb (conninfo == NULL ? "" : conninfo); 139 plugin->dbh = PQconnectdb (conninfo == NULL ? "" : conninfo);
143 GNUNET_free_non_null (conninfo); 140 GNUNET_free_non_null (conninfo);
@@ -148,28 +145,27 @@ init_connection (struct Plugin *plugin)
148 } 145 }
149 if (PQstatus (plugin->dbh) != CONNECTION_OK) 146 if (PQstatus (plugin->dbh) != CONNECTION_OK)
150 { 147 {
151 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 148 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "datacache-postgres",
152 "datacache-postgres",
153 _("Unable to initialize Postgres: %s"), 149 _("Unable to initialize Postgres: %s"),
154 PQerrorMessage (plugin->dbh)); 150 PQerrorMessage (plugin->dbh));
155 PQfinish (plugin->dbh); 151 PQfinish (plugin->dbh);
156 plugin->dbh = NULL; 152 plugin->dbh = NULL;
157 return GNUNET_SYSERR; 153 return GNUNET_SYSERR;
158 } 154 }
159 ret = PQexec (plugin->dbh, 155 ret =
160 "CREATE TEMPORARY TABLE gn090dc (" 156 PQexec (plugin->dbh,
161 " type INTEGER NOT NULL DEFAULT 0," 157 "CREATE TEMPORARY TABLE gn090dc ("
162 " discard_time BIGINT NOT NULL DEFAULT 0," 158 " type INTEGER NOT NULL DEFAULT 0,"
163 " key BYTEA NOT NULL DEFAULT ''," 159 " discard_time BIGINT NOT NULL DEFAULT 0,"
164 " value BYTEA NOT NULL DEFAULT '')" "WITH OIDS"); 160 " key BYTEA NOT NULL DEFAULT '',"
161 " value BYTEA NOT NULL DEFAULT '')" "WITH OIDS");
165 if ((ret == NULL) || ((PQresultStatus (ret) != PGRES_COMMAND_OK) && (0 != strcmp ("42P07", /* duplicate table */ 162 if ((ret == NULL) || ((PQresultStatus (ret) != PGRES_COMMAND_OK) && (0 != strcmp ("42P07", /* duplicate table */
166 PQresultErrorField 163 PQresultErrorField
167 (ret, 164 (ret,
168 PG_DIAG_SQLSTATE))))) 165 PG_DIAG_SQLSTATE)))))
169 { 166 {
170 (void) check_result (plugin, 167 (void) check_result (plugin, ret, PGRES_COMMAND_OK, "CREATE TABLE",
171 ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090dc", 168 "gn090dc", __LINE__);
172 __LINE__);
173 PQfinish (plugin->dbh); 169 PQfinish (plugin->dbh);
174 plugin->dbh = NULL; 170 plugin->dbh = NULL;
175 return GNUNET_SYSERR; 171 return GNUNET_SYSERR;
@@ -190,11 +186,12 @@ init_connection (struct Plugin *plugin)
190 } 186 }
191 PQclear (ret); 187 PQclear (ret);
192#if 1 188#if 1
193 ret = PQexec (plugin->dbh, 189 ret =
194 "ALTER TABLE gn090dc ALTER value SET STORAGE EXTERNAL"); 190 PQexec (plugin->dbh,
191 "ALTER TABLE gn090dc ALTER value SET STORAGE EXTERNAL");
195 if (GNUNET_OK != 192 if (GNUNET_OK !=
196 check_result (plugin, 193 check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090dc",
197 ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090dc", __LINE__)) 194 __LINE__))
198 { 195 {
199 PQfinish (plugin->dbh); 196 PQfinish (plugin->dbh);
200 plugin->dbh = NULL; 197 plugin->dbh = NULL;
@@ -203,8 +200,8 @@ init_connection (struct Plugin *plugin)
203 PQclear (ret); 200 PQclear (ret);
204 ret = PQexec (plugin->dbh, "ALTER TABLE gn090dc ALTER key SET STORAGE PLAIN"); 201 ret = PQexec (plugin->dbh, "ALTER TABLE gn090dc ALTER key SET STORAGE PLAIN");
205 if (GNUNET_OK != 202 if (GNUNET_OK !=
206 check_result (plugin, 203 check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090dc",
207 ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090dc", __LINE__)) 204 __LINE__))
208 { 205 {
209 PQfinish (plugin->dbh); 206 PQfinish (plugin->dbh);
210 plugin->dbh = NULL; 207 plugin->dbh = NULL;
@@ -213,35 +210,22 @@ init_connection (struct Plugin *plugin)
213 PQclear (ret); 210 PQclear (ret);
214#endif 211#endif
215 if ((GNUNET_OK != 212 if ((GNUNET_OK !=
216 pq_prepare (plugin, 213 pq_prepare (plugin, "getkt",
217 "getkt",
218 "SELECT discard_time,type,value FROM gn090dc " 214 "SELECT discard_time,type,value FROM gn090dc "
219 "WHERE key=$1 AND type=$2 ", 215 "WHERE key=$1 AND type=$2 ", 2, __LINE__)) ||
220 2,
221 __LINE__)) ||
222 (GNUNET_OK != 216 (GNUNET_OK !=
223 pq_prepare (plugin, 217 pq_prepare (plugin, "getk",
224 "getk",
225 "SELECT discard_time,type,value FROM gn090dc " 218 "SELECT discard_time,type,value FROM gn090dc "
226 "WHERE key=$1", 219 "WHERE key=$1", 1, __LINE__)) ||
227 1,
228 __LINE__)) ||
229 (GNUNET_OK != 220 (GNUNET_OK !=
230 pq_prepare (plugin, 221 pq_prepare (plugin, "getm",
231 "getm",
232 "SELECT length(value),oid,key FROM gn090dc " 222 "SELECT length(value),oid,key FROM gn090dc "
233 "ORDER BY discard_time ASC LIMIT 1", 223 "ORDER BY discard_time ASC LIMIT 1", 0, __LINE__)) ||
234 0,
235 __LINE__)) ||
236 (GNUNET_OK != 224 (GNUNET_OK !=
237 pq_prepare (plugin, 225 pq_prepare (plugin, "delrow", "DELETE FROM gn090dc WHERE oid=$1", 1,
238 "delrow",
239 "DELETE FROM gn090dc WHERE oid=$1",
240 1,
241 __LINE__)) || 226 __LINE__)) ||
242 (GNUNET_OK != 227 (GNUNET_OK !=
243 pq_prepare (plugin, 228 pq_prepare (plugin, "put",
244 "put",
245 "INSERT INTO gn090dc (type, discard_time, key, value) " 229 "INSERT INTO gn090dc (type, discard_time, key, value) "
246 "VALUES ($1, $2, $3, $4)", 4, __LINE__))) 230 "VALUES ($1, $2, $3, $4)", 4, __LINE__)))
247 { 231 {
@@ -268,12 +252,11 @@ delete_by_rowid (struct Plugin *plugin, uint32_t rowid)
268 const int paramFormats[] = { 1 }; 252 const int paramFormats[] = { 1 };
269 PGresult *ret; 253 PGresult *ret;
270 254
271 ret = PQexecPrepared (plugin->dbh, 255 ret =
272 "delrow", 256 PQexecPrepared (plugin->dbh, "delrow", 1, paramValues, paramLengths,
273 1, paramValues, paramLengths, paramFormats, 1); 257 paramFormats, 1);
274 if (GNUNET_OK != 258 if (GNUNET_OK !=
275 check_result (plugin, 259 check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow",
276 ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow",
277 __LINE__)) 260 __LINE__))
278 { 261 {
279 return GNUNET_SYSERR; 262 return GNUNET_SYSERR;
@@ -295,11 +278,8 @@ delete_by_rowid (struct Plugin *plugin, uint32_t rowid)
295 * @return 0 on error, number of bytes used otherwise 278 * @return 0 on error, number of bytes used otherwise
296 */ 279 */
297static size_t 280static size_t
298postgres_plugin_put (void *cls, 281postgres_plugin_put (void *cls, const GNUNET_HashCode * key, size_t size,
299 const GNUNET_HashCode * key, 282 const char *data, enum GNUNET_BLOCK_Type type,
300 size_t size,
301 const char *data,
302 enum GNUNET_BLOCK_Type type,
303 struct GNUNET_TIME_Absolute discard_time) 283 struct GNUNET_TIME_Absolute discard_time)
304{ 284{
305 struct Plugin *plugin = cls; 285 struct Plugin *plugin = cls;
@@ -321,11 +301,12 @@ postgres_plugin_put (void *cls,
321 }; 301 };
322 const int paramFormats[] = { 1, 1, 1, 1 }; 302 const int paramFormats[] = { 1, 1, 1, 1 };
323 303
324 ret = PQexecPrepared (plugin->dbh, 304 ret =
325 "put", 4, paramValues, paramLengths, paramFormats, 1); 305 PQexecPrepared (plugin->dbh, "put", 4, paramValues, paramLengths,
326 if (GNUNET_OK != check_result (plugin, ret, 306 paramFormats, 1);
327 PGRES_COMMAND_OK, 307 if (GNUNET_OK !=
328 "PQexecPrepared", "put", __LINE__)) 308 check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put",
309 __LINE__))
329 return GNUNET_SYSERR; 310 return GNUNET_SYSERR;
330 PQclear (ret); 311 PQclear (ret);
331 return size + OVERHEAD; 312 return size + OVERHEAD;
@@ -344,8 +325,7 @@ postgres_plugin_put (void *cls,
344 * @return the number of results found 325 * @return the number of results found
345 */ 326 */
346static unsigned int 327static unsigned int
347postgres_plugin_get (void *cls, 328postgres_plugin_get (void *cls, const GNUNET_HashCode * key,
348 const GNUNET_HashCode * key,
349 enum GNUNET_BLOCK_Type type, 329 enum GNUNET_BLOCK_Type type,
350 GNUNET_DATACACHE_Iterator iter, void *iter_cls) 330 GNUNET_DATACACHE_Iterator iter, void *iter_cls)
351{ 331{
@@ -368,19 +348,16 @@ postgres_plugin_get (void *cls,
368 PGresult *res; 348 PGresult *res;
369 349
370 cnt = 0; 350 cnt = 0;
371 res = PQexecPrepared (plugin->dbh, 351 res =
372 (type == 0) ? "getk" : "getkt", 352 PQexecPrepared (plugin->dbh, (type == 0) ? "getk" : "getkt",
373 (type == 0) ? 1 : 2, 353 (type == 0) ? 1 : 2, paramValues, paramLengths,
374 paramValues, paramLengths, paramFormats, 1); 354 paramFormats, 1);
375 if (GNUNET_OK != check_result (plugin, 355 if (GNUNET_OK !=
376 res, 356 check_result (plugin, res, PGRES_TUPLES_OK, "PQexecPrepared",
377 PGRES_TUPLES_OK, 357 (type == 0) ? "getk" : "getkt", __LINE__))
378 "PQexecPrepared",
379 (type == 0) ? "getk" : "getkt", __LINE__))
380 { 358 {
381#if DEBUG_POSTGRES 359#if DEBUG_POSTGRES
382 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 360 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
383 "datacache-postgres",
384 "Ending iteration (postgres error)\n"); 361 "Ending iteration (postgres error)\n");
385#endif 362#endif
386 return 0; 363 return 0;
@@ -390,8 +367,7 @@ postgres_plugin_get (void *cls,
390 { 367 {
391 /* no result */ 368 /* no result */
392#if DEBUG_POSTGRES 369#if DEBUG_POSTGRES
393 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 370 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
394 "datacache-postgres",
395 "Ending iteration (no more results)\n"); 371 "Ending iteration (no more results)\n");
396#endif 372#endif
397 PQclear (res); 373 PQclear (res);
@@ -402,8 +378,7 @@ postgres_plugin_get (void *cls,
402 PQclear (res); 378 PQclear (res);
403 return cnt; 379 return cnt;
404 } 380 }
405 if ((3 != PQnfields (res)) || 381 if ((3 != PQnfields (res)) || (sizeof (uint64_t) != PQfsize (res, 0)) ||
406 (sizeof (uint64_t) != PQfsize (res, 0)) ||
407 (sizeof (uint32_t) != PQfsize (res, 1))) 382 (sizeof (uint32_t) != PQfsize (res, 1)))
408 { 383 {
409 GNUNET_break (0); 384 GNUNET_break (0);
@@ -417,19 +392,16 @@ postgres_plugin_get (void *cls,
417 type = ntohl (*(uint32_t *) PQgetvalue (res, i, 1)); 392 type = ntohl (*(uint32_t *) PQgetvalue (res, i, 1));
418 size = PQgetlength (res, i, 2); 393 size = PQgetlength (res, i, 2);
419#if DEBUG_POSTGRES 394#if DEBUG_POSTGRES
420 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 395 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
421 "datacache-postgres",
422 "Found result of size %u bytes and type %u in database\n", 396 "Found result of size %u bytes and type %u in database\n",
423 (unsigned int) size, (unsigned int) type); 397 (unsigned int) size, (unsigned int) type);
424#endif 398#endif
425 if (GNUNET_SYSERR == 399 if (GNUNET_SYSERR ==
426 iter (iter_cls, 400 iter (iter_cls, expiration_time, key, size, PQgetvalue (res, i, 2),
427 expiration_time, 401 (enum GNUNET_BLOCK_Type) type))
428 key, size, PQgetvalue (res, i, 2), (enum GNUNET_BLOCK_Type) type))
429 { 402 {
430#if DEBUG_POSTGRES 403#if DEBUG_POSTGRES
431 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 404 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
432 "datacache-postgres",
433 "Ending iteration (client error)\n"); 405 "Ending iteration (client error)\n");
434#endif 406#endif
435 PQclear (res); 407 PQclear (res);
@@ -458,14 +430,12 @@ postgres_plugin_del (void *cls)
458 PGresult *res; 430 PGresult *res;
459 431
460 res = PQexecPrepared (plugin->dbh, "getm", 0, NULL, NULL, NULL, 1); 432 res = PQexecPrepared (plugin->dbh, "getm", 0, NULL, NULL, NULL, 1);
461 if (GNUNET_OK != check_result (plugin, 433 if (GNUNET_OK !=
462 res, 434 check_result (plugin, res, PGRES_TUPLES_OK, "PQexecPrepared", "getm",
463 PGRES_TUPLES_OK, 435 __LINE__))
464 "PQexecPrepared", "getm", __LINE__))
465 { 436 {
466#if DEBUG_POSTGRES 437#if DEBUG_POSTGRES
467 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 438 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
468 "datacache-postgres",
469 "Ending iteration (postgres error)\n"); 439 "Ending iteration (postgres error)\n");
470#endif 440#endif
471 return 0; 441 return 0;
@@ -474,15 +444,13 @@ postgres_plugin_del (void *cls)
474 { 444 {
475 /* no result */ 445 /* no result */
476#if DEBUG_POSTGRES 446#if DEBUG_POSTGRES
477 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 447 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
478 "datacache-postgres",
479 "Ending iteration (no more results)\n"); 448 "Ending iteration (no more results)\n");
480#endif 449#endif
481 PQclear (res); 450 PQclear (res);
482 return GNUNET_SYSERR; 451 return GNUNET_SYSERR;
483 } 452 }
484 if ((3 != PQnfields (res)) || 453 if ((3 != PQnfields (res)) || (sizeof (size) != PQfsize (res, 0)) ||
485 (sizeof (size) != PQfsize (res, 0)) ||
486 (sizeof (oid) != PQfsize (res, 1)) || 454 (sizeof (oid) != PQfsize (res, 1)) ||
487 (sizeof (GNUNET_HashCode) != PQgetlength (res, 0, 2))) 455 (sizeof (GNUNET_HashCode) != PQgetlength (res, 0, 2)))
488 { 456 {
@@ -528,8 +496,8 @@ libgnunet_plugin_datacache_postgres_init (void *cls)
528 api->get = &postgres_plugin_get; 496 api->get = &postgres_plugin_get;
529 api->put = &postgres_plugin_put; 497 api->put = &postgres_plugin_put;
530 api->del = &postgres_plugin_del; 498 api->del = &postgres_plugin_del;
531 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, 499 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "datacache-postgres",
532 "datacache-postgres", _("Postgres datacache running\n")); 500 _("Postgres datacache running\n"));
533 return api; 501 return api;
534} 502}
535 503