diff options
Diffstat (limited to 'src/datastore')
-rw-r--r-- | src/datastore/plugin_datastore_postgres.c | 264 |
1 files changed, 57 insertions, 207 deletions
diff --git a/src/datastore/plugin_datastore_postgres.c b/src/datastore/plugin_datastore_postgres.c index 31ba09a87..b9c27c7b1 100644 --- a/src/datastore/plugin_datastore_postgres.c +++ b/src/datastore/plugin_datastore_postgres.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet | 2 | This file is part of GNUnet |
3 | (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors) | 3 | (C) 2009, 2010, 2011, 2012 Christian Grothoff (and other contributing authors) |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 5 | GNUnet is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published | 6 | it under the terms of the GNU General Public License as published |
@@ -26,6 +26,7 @@ | |||
26 | 26 | ||
27 | #include "platform.h" | 27 | #include "platform.h" |
28 | #include "gnunet_datastore_plugin.h" | 28 | #include "gnunet_datastore_plugin.h" |
29 | #include "gnunet_postgres_lib.h" | ||
29 | #include <postgresql/libpq-fe.h> | 30 | #include <postgresql/libpq-fe.h> |
30 | 31 | ||
31 | #define DEBUG_POSTGRES GNUNET_EXTRA_LOGGING | 32 | #define DEBUG_POSTGRES GNUNET_EXTRA_LOGGING |
@@ -62,87 +63,6 @@ struct Plugin | |||
62 | 63 | ||
63 | 64 | ||
64 | /** | 65 | /** |
65 | * Check if the result obtained from Postgres has | ||
66 | * the desired status code. If not, log an error, clear the | ||
67 | * result and return GNUNET_SYSERR. | ||
68 | * | ||
69 | * @param plugin global context | ||
70 | * @param ret result to check | ||
71 | * @param expected_status expected return value | ||
72 | * @param command name of SQL command that was run | ||
73 | * @param args arguments to SQL command | ||
74 | * @param line line number for error reporting | ||
75 | * @return GNUNET_OK if the result is acceptable | ||
76 | */ | ||
77 | static int | ||
78 | check_result (struct Plugin *plugin, PGresult * ret, int expected_status, | ||
79 | const char *command, const char *args, int line) | ||
80 | { | ||
81 | if (ret == NULL) | ||
82 | { | ||
83 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
84 | "datastore-postgres", | ||
85 | "Postgres failed to allocate result for `%s:%s' at %d\n", | ||
86 | command, args, line); | ||
87 | return GNUNET_SYSERR; | ||
88 | } | ||
89 | if (PQresultStatus (ret) != expected_status) | ||
90 | { | ||
91 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
92 | "datastore-postgres", | ||
93 | _("`%s:%s' failed at %s:%d with error: %s"), command, args, | ||
94 | __FILE__, line, PQerrorMessage (plugin->dbh)); | ||
95 | PQclear (ret); | ||
96 | return GNUNET_SYSERR; | ||
97 | } | ||
98 | return GNUNET_OK; | ||
99 | } | ||
100 | |||
101 | /** | ||
102 | * Run simple SQL statement (without results). | ||
103 | * | ||
104 | * @param plugin global context | ||
105 | * @param sql statement to run | ||
106 | * @param line code line for error reporting | ||
107 | */ | ||
108 | static int | ||
109 | pq_exec (struct Plugin *plugin, const char *sql, int line) | ||
110 | { | ||
111 | PGresult *ret; | ||
112 | |||
113 | ret = PQexec (plugin->dbh, sql); | ||
114 | if (GNUNET_OK != | ||
115 | check_result (plugin, ret, PGRES_COMMAND_OK, "PQexec", sql, line)) | ||
116 | return GNUNET_SYSERR; | ||
117 | PQclear (ret); | ||
118 | return GNUNET_OK; | ||
119 | } | ||
120 | |||
121 | /** | ||
122 | * Prepare SQL statement. | ||
123 | * | ||
124 | * @param plugin global context | ||
125 | * @param name name for the prepared SQL statement | ||
126 | * @param sql SQL code to prepare | ||
127 | * @param nparams number of parameters in sql | ||
128 | * @param line code line for error reporting | ||
129 | * @return GNUNET_OK on success | ||
130 | */ | ||
131 | static int | ||
132 | pq_prepare (struct Plugin *plugin, const char *name, const char *sql, | ||
133 | int nparams, int line) | ||
134 | { | ||
135 | PGresult *ret; | ||
136 | |||
137 | ret = PQprepare (plugin->dbh, name, sql, nparams, NULL); | ||
138 | if (GNUNET_OK != | ||
139 | check_result (plugin, ret, PGRES_COMMAND_OK, "PQprepare", sql, line)) | ||
140 | return GNUNET_SYSERR; | ||
141 | PQclear (ret); | ||
142 | return GNUNET_OK; | ||
143 | } | ||
144 | |||
145 | /** | ||
146 | * @brief Get a database handle | 66 | * @brief Get a database handle |
147 | * | 67 | * |
148 | * @param plugin global context | 68 | * @param plugin global context |
@@ -151,33 +71,11 @@ pq_prepare (struct Plugin *plugin, const char *name, const char *sql, | |||
151 | static int | 71 | static int |
152 | init_connection (struct Plugin *plugin) | 72 | init_connection (struct Plugin *plugin) |
153 | { | 73 | { |
154 | char *conninfo; | ||
155 | PGresult *ret; | 74 | PGresult *ret; |
156 | 75 | ||
157 | /* Open database and precompile statements */ | 76 | plugin->dbh = GNUNET_POSTGRES_connect (plugin->env->cfg, "datastore-postgres"); |
158 | conninfo = NULL; | ||
159 | (void) GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, | ||
160 | "datastore-postgres", "CONFIG", | ||
161 | &conninfo); | ||
162 | plugin->dbh = PQconnectdb (conninfo == NULL ? "" : conninfo); | ||
163 | if (NULL == plugin->dbh) | 77 | if (NULL == plugin->dbh) |
164 | { | ||
165 | /* FIXME: warn about out-of-memory? */ | ||
166 | GNUNET_free_non_null (conninfo); | ||
167 | return GNUNET_SYSERR; | 78 | return GNUNET_SYSERR; |
168 | } | ||
169 | if (PQstatus (plugin->dbh) != CONNECTION_OK) | ||
170 | { | ||
171 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "datastore-postgres", | ||
172 | _ | ||
173 | ("Unable to initialize Postgres with configuration `%s': %s"), | ||
174 | conninfo, PQerrorMessage (plugin->dbh)); | ||
175 | PQfinish (plugin->dbh); | ||
176 | plugin->dbh = NULL; | ||
177 | GNUNET_free_non_null (conninfo); | ||
178 | return GNUNET_SYSERR; | ||
179 | } | ||
180 | GNUNET_free_non_null (conninfo); | ||
181 | ret = | 79 | ret = |
182 | PQexec (plugin->dbh, | 80 | PQexec (plugin->dbh, |
183 | "CREATE TABLE gn090 (" " repl INTEGER NOT NULL DEFAULT 0," | 81 | "CREATE TABLE gn090 (" " repl INTEGER NOT NULL DEFAULT 0," |
@@ -194,8 +92,7 @@ init_connection (struct Plugin *plugin) | |||
194 | (ret, | 92 | (ret, |
195 | PG_DIAG_SQLSTATE))))) | 93 | PG_DIAG_SQLSTATE))))) |
196 | { | 94 | { |
197 | (void) check_result (plugin, ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090", | 95 | (void) GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090"); |
198 | __LINE__); | ||
199 | PQfinish (plugin->dbh); | 96 | PQfinish (plugin->dbh); |
200 | plugin->dbh = NULL; | 97 | plugin->dbh = NULL; |
201 | return GNUNET_SYSERR; | 98 | return GNUNET_SYSERR; |
@@ -203,29 +100,23 @@ init_connection (struct Plugin *plugin) | |||
203 | if (PQresultStatus (ret) == PGRES_COMMAND_OK) | 100 | if (PQresultStatus (ret) == PGRES_COMMAND_OK) |
204 | { | 101 | { |
205 | if ((GNUNET_OK != | 102 | if ((GNUNET_OK != |
206 | pq_exec (plugin, "CREATE INDEX idx_hash ON gn090 (hash)", __LINE__)) || | 103 | GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_hash ON gn090 (hash)")) || |
207 | (GNUNET_OK != | 104 | (GNUNET_OK != |
208 | pq_exec (plugin, "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)", | 105 | GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)")) || |
209 | __LINE__)) || | ||
210 | (GNUNET_OK != | 106 | (GNUNET_OK != |
211 | pq_exec (plugin, "CREATE INDEX idx_prio ON gn090 (prio)", __LINE__)) || | 107 | GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_prio ON gn090 (prio)")) || |
212 | (GNUNET_OK != | 108 | (GNUNET_OK != |
213 | pq_exec (plugin, "CREATE INDEX idx_expire ON gn090 (expire)", | 109 | GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_expire ON gn090 (expire)")) || |
214 | __LINE__)) || | ||
215 | (GNUNET_OK != | 110 | (GNUNET_OK != |
216 | pq_exec (plugin, | 111 | GNUNET_POSTGRES_exec (plugin->dbh, |
217 | "CREATE INDEX idx_prio_anon ON gn090 (prio,anonLevel)", | 112 | "CREATE INDEX idx_prio_anon ON gn090 (prio,anonLevel)")) || |
218 | __LINE__)) || | ||
219 | (GNUNET_OK != | 113 | (GNUNET_OK != |
220 | pq_exec (plugin, | 114 | GNUNET_POSTGRES_exec (plugin->dbh, |
221 | "CREATE INDEX idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)", | 115 | "CREATE INDEX idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)")) || |
222 | __LINE__)) || | ||
223 | (GNUNET_OK != | 116 | (GNUNET_OK != |
224 | pq_exec (plugin, "CREATE INDEX idx_repl_rvalue ON gn090 (repl,rvalue)", | 117 | GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_repl_rvalue ON gn090 (repl,rvalue)")) || |
225 | __LINE__)) || | ||
226 | (GNUNET_OK != | 118 | (GNUNET_OK != |
227 | pq_exec (plugin, "CREATE INDEX idx_expire_hash ON gn090 (expire,hash)", | 119 | GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_expire_hash ON gn090 (expire,hash)"))) |
228 | __LINE__))) | ||
229 | { | 120 | { |
230 | PQclear (ret); | 121 | PQclear (ret); |
231 | PQfinish (plugin->dbh); | 122 | PQfinish (plugin->dbh); |
@@ -238,8 +129,7 @@ init_connection (struct Plugin *plugin) | |||
238 | PQexec (plugin->dbh, | 129 | PQexec (plugin->dbh, |
239 | "ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL"); | 130 | "ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL"); |
240 | if (GNUNET_OK != | 131 | if (GNUNET_OK != |
241 | check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090", | 132 | GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090")) |
242 | __LINE__)) | ||
243 | { | 133 | { |
244 | PQfinish (plugin->dbh); | 134 | PQfinish (plugin->dbh); |
245 | plugin->dbh = NULL; | 135 | plugin->dbh = NULL; |
@@ -248,8 +138,7 @@ init_connection (struct Plugin *plugin) | |||
248 | PQclear (ret); | 138 | PQclear (ret); |
249 | ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN"); | 139 | ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN"); |
250 | if (GNUNET_OK != | 140 | if (GNUNET_OK != |
251 | check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090", | 141 | GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090")) |
252 | __LINE__)) | ||
253 | { | 142 | { |
254 | PQfinish (plugin->dbh); | 143 | PQfinish (plugin->dbh); |
255 | plugin->dbh = NULL; | 144 | plugin->dbh = NULL; |
@@ -258,8 +147,7 @@ init_connection (struct Plugin *plugin) | |||
258 | PQclear (ret); | 147 | PQclear (ret); |
259 | ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN"); | 148 | ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN"); |
260 | if (GNUNET_OK != | 149 | if (GNUNET_OK != |
261 | check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090", | 150 | GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090")) |
262 | __LINE__)) | ||
263 | { | 151 | { |
264 | PQfinish (plugin->dbh); | 152 | PQfinish (plugin->dbh); |
265 | plugin->dbh = NULL; | 153 | plugin->dbh = NULL; |
@@ -267,60 +155,56 @@ init_connection (struct Plugin *plugin) | |||
267 | } | 155 | } |
268 | PQclear (ret); | 156 | PQclear (ret); |
269 | if ((GNUNET_OK != | 157 | if ((GNUNET_OK != |
270 | pq_prepare (plugin, "getvt", | 158 | GNUNET_POSTGRES_prepare (plugin->dbh, "getvt", |
271 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " | 159 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " |
272 | "WHERE hash=$1 AND vhash=$2 AND type=$3 " | 160 | "WHERE hash=$1 AND vhash=$2 AND type=$3 " |
273 | "ORDER BY oid ASC LIMIT 1 OFFSET $4", 4, __LINE__)) || | 161 | "ORDER BY oid ASC LIMIT 1 OFFSET $4", 4)) || |
274 | (GNUNET_OK != | 162 | (GNUNET_OK != |
275 | pq_prepare (plugin, "gett", | 163 | GNUNET_POSTGRES_prepare (plugin->dbh, "gett", |
276 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " | 164 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " |
277 | "WHERE hash=$1 AND type=$2 " | 165 | "WHERE hash=$1 AND type=$2 " |
278 | "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3, __LINE__)) || | 166 | "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3)) || |
279 | (GNUNET_OK != | 167 | (GNUNET_OK != |
280 | pq_prepare (plugin, "getv", | 168 | GNUNET_POSTGRES_prepare (plugin->dbh, "getv", |
281 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " | 169 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " |
282 | "WHERE hash=$1 AND vhash=$2 " | 170 | "WHERE hash=$1 AND vhash=$2 " |
283 | "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3, __LINE__)) || | 171 | "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3)) || |
284 | (GNUNET_OK != | 172 | (GNUNET_OK != |
285 | pq_prepare (plugin, "get", | 173 | GNUNET_POSTGRES_prepare (plugin->dbh, "get", |
286 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " | 174 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " |
287 | "WHERE hash=$1 " "ORDER BY oid ASC LIMIT 1 OFFSET $2", 2, | 175 | "WHERE hash=$1 " "ORDER BY oid ASC LIMIT 1 OFFSET $2", 2)) || |
288 | __LINE__)) || | ||
289 | (GNUNET_OK != | 176 | (GNUNET_OK != |
290 | pq_prepare (plugin, "put", | 177 | GNUNET_POSTGRES_prepare (plugin->dbh, "put", |
291 | "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) " | 178 | "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) " |
292 | "VALUES ($1, $2, $3, $4, $5, RANDOM(), $6, $7, $8)", 9, | 179 | "VALUES ($1, $2, $3, $4, $5, RANDOM(), $6, $7, $8)", 9)) || |
293 | __LINE__)) || | ||
294 | (GNUNET_OK != | 180 | (GNUNET_OK != |
295 | pq_prepare (plugin, "update", | 181 | GNUNET_POSTGRES_prepare (plugin->dbh, "update", |
296 | "UPDATE gn090 SET prio = prio + $1, expire = CASE WHEN expire < $2 THEN $2 ELSE expire END " | 182 | "UPDATE gn090 SET prio = prio + $1, expire = CASE WHEN expire < $2 THEN $2 ELSE expire END " |
297 | "WHERE oid = $3", 3, __LINE__)) || | 183 | "WHERE oid = $3", 3)) || |
298 | (GNUNET_OK != | 184 | (GNUNET_OK != |
299 | pq_prepare (plugin, "decrepl", | 185 | GNUNET_POSTGRES_prepare (plugin->dbh, "decrepl", |
300 | "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) " | 186 | "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) " |
301 | "WHERE oid = $1", 1, __LINE__)) || | 187 | "WHERE oid = $1", 1)) || |
302 | (GNUNET_OK != | 188 | (GNUNET_OK != |
303 | pq_prepare (plugin, "select_non_anonymous", | 189 | GNUNET_POSTGRES_prepare (plugin->dbh, "select_non_anonymous", |
304 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " | 190 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " |
305 | "WHERE anonLevel = 0 AND type = $1 ORDER BY oid DESC LIMIT 1 OFFSET $2", | 191 | "WHERE anonLevel = 0 AND type = $1 ORDER BY oid DESC LIMIT 1 OFFSET $2", |
306 | 1, __LINE__)) || | 192 | 1)) || |
307 | (GNUNET_OK != | 193 | (GNUNET_OK != |
308 | pq_prepare (plugin, "select_expiration_order", | 194 | GNUNET_POSTGRES_prepare (plugin->dbh, "select_expiration_order", |
309 | "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " | 195 | "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " |
310 | "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) " "UNION " | 196 | "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) " "UNION " |
311 | "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " | 197 | "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " |
312 | "ORDER BY prio ASC LIMIT 1) " "ORDER BY expire ASC LIMIT 1", | 198 | "ORDER BY prio ASC LIMIT 1) " "ORDER BY expire ASC LIMIT 1", |
313 | 1, __LINE__)) || | 199 | 1)) || |
314 | (GNUNET_OK != | 200 | (GNUNET_OK != |
315 | pq_prepare (plugin, "select_replication_order", | 201 | GNUNET_POSTGRES_prepare (plugin->dbh, "select_replication_order", |
316 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " | 202 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " |
317 | "ORDER BY repl DESC,RANDOM() LIMIT 1", 0, __LINE__)) || | 203 | "ORDER BY repl DESC,RANDOM() LIMIT 1", 0)) || |
318 | (GNUNET_OK != | 204 | (GNUNET_OK != |
319 | pq_prepare (plugin, "delrow", "DELETE FROM gn090 " "WHERE oid=$1", 1, | 205 | GNUNET_POSTGRES_prepare (plugin->dbh, "delrow", "DELETE FROM gn090 " "WHERE oid=$1", 1)) || |
320 | __LINE__)) || | ||
321 | (GNUNET_OK != | 206 | (GNUNET_OK != |
322 | pq_prepare (plugin, "get_keys", "SELECT hash FROM gn090", 0, | 207 | GNUNET_POSTGRES_prepare (plugin->dbh, "get_keys", "SELECT hash FROM gn090", 0))) |
323 | __LINE__))) | ||
324 | { | 208 | { |
325 | PQfinish (plugin->dbh); | 209 | PQfinish (plugin->dbh); |
326 | plugin->dbh = NULL; | 210 | plugin->dbh = NULL; |
@@ -331,38 +215,6 @@ init_connection (struct Plugin *plugin) | |||
331 | 215 | ||
332 | 216 | ||
333 | /** | 217 | /** |
334 | * Delete the row identified by the given rowid (qid | ||
335 | * in postgres). | ||
336 | * | ||
337 | * @param plugin global context | ||
338 | * @param rowid which row to delete | ||
339 | * @return GNUNET_OK on success | ||
340 | */ | ||
341 | static int | ||
342 | delete_by_rowid (struct Plugin *plugin, uint32_t rowid) | ||
343 | { | ||
344 | uint32_t browid; | ||
345 | const char *paramValues[] = { (const char *) &browid }; | ||
346 | int paramLengths[] = { sizeof (browid) }; | ||
347 | const int paramFormats[] = { 1 }; | ||
348 | PGresult *ret; | ||
349 | |||
350 | browid = htonl (rowid); | ||
351 | ret = | ||
352 | PQexecPrepared (plugin->dbh, "delrow", 1, paramValues, paramLengths, | ||
353 | paramFormats, 1); | ||
354 | if (GNUNET_OK != | ||
355 | check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow", | ||
356 | __LINE__)) | ||
357 | { | ||
358 | return GNUNET_SYSERR; | ||
359 | } | ||
360 | PQclear (ret); | ||
361 | return GNUNET_OK; | ||
362 | } | ||
363 | |||
364 | |||
365 | /** | ||
366 | * Get an estimate of how much space the database is | 218 | * Get an estimate of how much space the database is |
367 | * currently using. | 219 | * currently using. |
368 | * | 220 | * |
@@ -381,8 +233,7 @@ postgres_plugin_estimate_size (void *cls) | |||
381 | "SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090", 0, | 233 | "SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090", 0, |
382 | NULL, NULL, NULL, NULL, 1); | 234 | NULL, NULL, NULL, NULL, 1); |
383 | if (GNUNET_OK != | 235 | if (GNUNET_OK != |
384 | check_result (plugin, ret, PGRES_TUPLES_OK, "PQexecParams", "get_size", | 236 | GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_TUPLES_OK, "PQexecParams", "get_size")) |
385 | __LINE__)) | ||
386 | { | 237 | { |
387 | return 0; | 238 | return 0; |
388 | } | 239 | } |
@@ -457,8 +308,7 @@ postgres_plugin_put (void *cls, const GNUNET_HashCode * key, uint32_t size, | |||
457 | PQexecPrepared (plugin->dbh, "put", 8, paramValues, paramLengths, | 308 | PQexecPrepared (plugin->dbh, "put", 8, paramValues, paramLengths, |
458 | paramFormats, 1); | 309 | paramFormats, 1); |
459 | if (GNUNET_OK != | 310 | if (GNUNET_OK != |
460 | check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put", | 311 | GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put")) |
461 | __LINE__)) | ||
462 | return GNUNET_SYSERR; | 312 | return GNUNET_SYSERR; |
463 | PQclear (ret); | 313 | PQclear (ret); |
464 | plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD); | 314 | plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD); |
@@ -478,11 +328,13 @@ postgres_plugin_put (void *cls, const GNUNET_HashCode * key, uint32_t size, | |||
478 | * @param proc function to call the value (once only). | 328 | * @param proc function to call the value (once only). |
479 | * @param proc_cls closure for proc | 329 | * @param proc_cls closure for proc |
480 | * @param res result from exec | 330 | * @param res result from exec |
331 | * @param filename filename for error messages | ||
481 | * @param line line number for error messages | 332 | * @param line line number for error messages |
482 | */ | 333 | */ |
483 | static void | 334 | static void |
484 | process_result (struct Plugin *plugin, PluginDatumProcessor proc, | 335 | process_result (struct Plugin *plugin, PluginDatumProcessor proc, |
485 | void *proc_cls, PGresult * res, int line) | 336 | void *proc_cls, PGresult * res, |
337 | const char *filename, int line) | ||
486 | { | 338 | { |
487 | int iret; | 339 | int iret; |
488 | enum GNUNET_BLOCK_Type type; | 340 | enum GNUNET_BLOCK_Type type; |
@@ -494,8 +346,8 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc, | |||
494 | GNUNET_HashCode key; | 346 | GNUNET_HashCode key; |
495 | 347 | ||
496 | if (GNUNET_OK != | 348 | if (GNUNET_OK != |
497 | check_result (plugin, res, PGRES_TUPLES_OK, "PQexecPrepared", "select", | 349 | GNUNET_POSTGRES_check_result_ (plugin->dbh, res, PGRES_TUPLES_OK, "PQexecPrepared", "select", |
498 | line)) | 350 | filename, line)) |
499 | { | 351 | { |
500 | #if DEBUG_POSTGRES | 352 | #if DEBUG_POSTGRES |
501 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres", | 353 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres", |
@@ -534,7 +386,7 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc, | |||
534 | { | 386 | { |
535 | GNUNET_break (0); | 387 | GNUNET_break (0); |
536 | PQclear (res); | 388 | PQclear (res); |
537 | delete_by_rowid (plugin, rowid); | 389 | GNUNET_POSTGRES_delete_by_rowid (plugin->dbh, "delrow", rowid); |
538 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); | 390 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); |
539 | return; | 391 | return; |
540 | } | 392 | } |
@@ -562,7 +414,7 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc, | |||
562 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 414 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
563 | "Processor asked for item %u to be removed.\n", rowid); | 415 | "Processor asked for item %u to be removed.\n", rowid); |
564 | #endif | 416 | #endif |
565 | if (GNUNET_OK == delete_by_rowid (plugin, rowid)) | 417 | if (GNUNET_OK == GNUNET_POSTGRES_delete_by_rowid (plugin->dbh, "delrow", rowid)) |
566 | { | 418 | { |
567 | #if DEBUG_POSTGRES | 419 | #if DEBUG_POSTGRES |
568 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres", | 420 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres", |
@@ -679,8 +531,7 @@ postgres_plugin_get_key (void *cls, uint64_t offset, | |||
679 | } | 531 | } |
680 | } | 532 | } |
681 | if (GNUNET_OK != | 533 | if (GNUNET_OK != |
682 | check_result (plugin, ret, PGRES_TUPLES_OK, "PQexecParams", pname, | 534 | GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_TUPLES_OK, "PQexecParams", pname)) |
683 | __LINE__)) | ||
684 | { | 535 | { |
685 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); | 536 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); |
686 | return; | 537 | return; |
@@ -704,7 +555,7 @@ postgres_plugin_get_key (void *cls, uint64_t offset, | |||
704 | ret = | 555 | ret = |
705 | PQexecPrepared (plugin->dbh, pname, nparams, paramValues, paramLengths, | 556 | PQexecPrepared (plugin->dbh, pname, nparams, paramValues, paramLengths, |
706 | paramFormats, 1); | 557 | paramFormats, 1); |
707 | process_result (plugin, proc, proc_cls, ret, __LINE__); | 558 | process_result (plugin, proc, proc_cls, ret, __FILE__, __LINE__); |
708 | } | 559 | } |
709 | 560 | ||
710 | 561 | ||
@@ -739,7 +590,7 @@ postgres_plugin_get_zero_anonymity (void *cls, uint64_t offset, | |||
739 | ret = | 590 | ret = |
740 | PQexecPrepared (plugin->dbh, "select_non_anonymous", 2, paramValues, | 591 | PQexecPrepared (plugin->dbh, "select_non_anonymous", 2, paramValues, |
741 | paramLengths, paramFormats, 1); | 592 | paramLengths, paramFormats, 1); |
742 | process_result (plugin, proc, proc_cls, ret, __LINE__); | 593 | process_result (plugin, proc, proc_cls, ret, __FILE__, __LINE__); |
743 | } | 594 | } |
744 | 595 | ||
745 | 596 | ||
@@ -815,8 +666,8 @@ repl_proc (void *cls, const GNUNET_HashCode * key, uint32_t size, | |||
815 | PQexecPrepared (plugin->dbh, "decrepl", 1, paramValues, paramLengths, | 666 | PQexecPrepared (plugin->dbh, "decrepl", 1, paramValues, paramLengths, |
816 | paramFormats, 1); | 667 | paramFormats, 1); |
817 | if (GNUNET_OK != | 668 | if (GNUNET_OK != |
818 | check_result (plugin, qret, PGRES_COMMAND_OK, "PQexecPrepared", | 669 | GNUNET_POSTGRES_check_result (plugin->dbh, qret, PGRES_COMMAND_OK, "PQexecPrepared", |
819 | "decrepl", __LINE__)) | 670 | "decrepl")) |
820 | return GNUNET_SYSERR; | 671 | return GNUNET_SYSERR; |
821 | PQclear (qret); | 672 | PQclear (qret); |
822 | } | 673 | } |
@@ -848,7 +699,7 @@ postgres_plugin_get_replication (void *cls, PluginDatumProcessor proc, | |||
848 | ret = | 699 | ret = |
849 | PQexecPrepared (plugin->dbh, "select_replication_order", 0, NULL, NULL, | 700 | PQexecPrepared (plugin->dbh, "select_replication_order", 0, NULL, NULL, |
850 | NULL, 1); | 701 | NULL, 1); |
851 | process_result (plugin, &repl_proc, &rc, ret, __LINE__); | 702 | process_result (plugin, &repl_proc, &rc, ret, __FILE__, __LINE__); |
852 | } | 703 | } |
853 | 704 | ||
854 | 705 | ||
@@ -875,7 +726,7 @@ postgres_plugin_get_expiration (void *cls, PluginDatumProcessor proc, | |||
875 | ret = | 726 | ret = |
876 | PQexecPrepared (plugin->dbh, "select_expiration_order", 1, paramValues, | 727 | PQexecPrepared (plugin->dbh, "select_expiration_order", 1, paramValues, |
877 | paramLengths, paramFormats, 1); | 728 | paramLengths, paramFormats, 1); |
878 | process_result (plugin, proc, proc_cls, ret, __LINE__); | 729 | process_result (plugin, proc, proc_cls, ret, __FILE__, __LINE__); |
879 | } | 730 | } |
880 | 731 | ||
881 | 732 | ||
@@ -928,8 +779,7 @@ postgres_plugin_update (void *cls, uint64_t uid, int delta, | |||
928 | PQexecPrepared (plugin->dbh, "update", 3, paramValues, paramLengths, | 779 | PQexecPrepared (plugin->dbh, "update", 3, paramValues, paramLengths, |
929 | paramFormats, 1); | 780 | paramFormats, 1); |
930 | if (GNUNET_OK != | 781 | if (GNUNET_OK != |
931 | check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "update", | 782 | GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "update")) |
932 | __LINE__)) | ||
933 | return GNUNET_SYSERR; | 783 | return GNUNET_SYSERR; |
934 | PQclear (ret); | 784 | PQclear (ret); |
935 | return GNUNET_OK; | 785 | return GNUNET_OK; |
@@ -978,7 +828,7 @@ postgres_plugin_drop (void *cls) | |||
978 | { | 828 | { |
979 | struct Plugin *plugin = cls; | 829 | struct Plugin *plugin = cls; |
980 | 830 | ||
981 | pq_exec (plugin, "DROP TABLE gn090", __LINE__); | 831 | GNUNET_POSTGRES_exec (plugin->dbh, "DROP TABLE gn090"); |
982 | } | 832 | } |
983 | 833 | ||
984 | 834 | ||