diff options
Diffstat (limited to 'src/datastore/plugin_datastore_postgres.c')
-rw-r--r-- | src/datastore/plugin_datastore_postgres.c | 791 |
1 files changed, 356 insertions, 435 deletions
diff --git a/src/datastore/plugin_datastore_postgres.c b/src/datastore/plugin_datastore_postgres.c index 056138d97..b79178dac 100644 --- a/src/datastore/plugin_datastore_postgres.c +++ b/src/datastore/plugin_datastore_postgres.c | |||
@@ -46,7 +46,7 @@ | |||
46 | /** | 46 | /** |
47 | * Context for all functions in this plugin. | 47 | * Context for all functions in this plugin. |
48 | */ | 48 | */ |
49 | struct Plugin | 49 | struct Plugin |
50 | { | 50 | { |
51 | /** | 51 | /** |
52 | * Our execution environment. | 52 | * Our execution environment. |
@@ -76,27 +76,28 @@ struct Plugin | |||
76 | */ | 76 | */ |
77 | static int | 77 | static int |
78 | check_result (struct Plugin *plugin, | 78 | check_result (struct Plugin *plugin, |
79 | PGresult * ret, | 79 | PGresult * ret, |
80 | int expected_status, | 80 | int expected_status, |
81 | const char *command, const char *args, int line) | 81 | const char *command, const char *args, int line) |
82 | { | 82 | { |
83 | if (ret == NULL) | 83 | if (ret == NULL) |
84 | { | 84 | { |
85 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 85 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
86 | "datastore-postgres", | 86 | "datastore-postgres", |
87 | "Postgres failed to allocate result for `%s:%s' at %d\n", | 87 | "Postgres failed to allocate result for `%s:%s' at %d\n", |
88 | command, args, line); | 88 | command, args, line); |
89 | return GNUNET_SYSERR; | 89 | return GNUNET_SYSERR; |
90 | } | 90 | } |
91 | if (PQresultStatus (ret) != expected_status) | 91 | if (PQresultStatus (ret) != expected_status) |
92 | { | 92 | { |
93 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 93 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
94 | "datastore-postgres", | 94 | "datastore-postgres", |
95 | _("`%s:%s' failed at %s:%d with error: %s"), | 95 | _("`%s:%s' failed at %s:%d with error: %s"), |
96 | command, args, __FILE__, line, PQerrorMessage (plugin->dbh)); | 96 | command, args, __FILE__, line, |
97 | PQclear (ret); | 97 | PQerrorMessage (plugin->dbh)); |
98 | return GNUNET_SYSERR; | 98 | PQclear (ret); |
99 | } | 99 | return GNUNET_SYSERR; |
100 | } | ||
100 | return GNUNET_OK; | 101 | return GNUNET_OK; |
101 | } | 102 | } |
102 | 103 | ||
@@ -108,14 +109,13 @@ check_result (struct Plugin *plugin, | |||
108 | * @param line code line for error reporting | 109 | * @param line code line for error reporting |
109 | */ | 110 | */ |
110 | static int | 111 | static int |
111 | pq_exec (struct Plugin *plugin, | 112 | pq_exec (struct Plugin *plugin, const char *sql, int line) |
112 | const char *sql, int line) | ||
113 | { | 113 | { |
114 | PGresult *ret; | 114 | PGresult *ret; |
115 | |||
115 | ret = PQexec (plugin->dbh, sql); | 116 | ret = PQexec (plugin->dbh, sql); |
116 | if (GNUNET_OK != check_result (plugin, | 117 | if (GNUNET_OK != check_result (plugin, |
117 | ret, | 118 | ret, PGRES_COMMAND_OK, "PQexec", sql, line)) |
118 | PGRES_COMMAND_OK, "PQexec", sql, line)) | ||
119 | return GNUNET_SYSERR; | 119 | return GNUNET_SYSERR; |
120 | PQclear (ret); | 120 | PQclear (ret); |
121 | return GNUNET_OK; | 121 | return GNUNET_OK; |
@@ -133,13 +133,13 @@ pq_exec (struct Plugin *plugin, | |||
133 | */ | 133 | */ |
134 | static int | 134 | static int |
135 | pq_prepare (struct Plugin *plugin, | 135 | pq_prepare (struct Plugin *plugin, |
136 | const char *name, const char *sql, int nparams, int line) | 136 | const char *name, const char *sql, int nparams, int line) |
137 | { | 137 | { |
138 | PGresult *ret; | 138 | PGresult *ret; |
139 | |||
139 | ret = PQprepare (plugin->dbh, name, sql, nparams, NULL); | 140 | ret = PQprepare (plugin->dbh, name, sql, nparams, NULL); |
140 | if (GNUNET_OK != | 141 | if (GNUNET_OK != |
141 | check_result (plugin, | 142 | check_result (plugin, ret, PGRES_COMMAND_OK, "PQprepare", sql, line)) |
142 | ret, PGRES_COMMAND_OK, "PQprepare", sql, line)) | ||
143 | return GNUNET_SYSERR; | 143 | return GNUNET_SYSERR; |
144 | PQclear (ret); | 144 | PQclear (ret); |
145 | return GNUNET_OK; | 145 | return GNUNET_OK; |
@@ -160,28 +160,27 @@ init_connection (struct Plugin *plugin) | |||
160 | /* Open database and precompile statements */ | 160 | /* Open database and precompile statements */ |
161 | conninfo = NULL; | 161 | conninfo = NULL; |
162 | (void) GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, | 162 | (void) GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, |
163 | "datastore-postgres", | 163 | "datastore-postgres", |
164 | "CONFIG", | 164 | "CONFIG", &conninfo); |
165 | &conninfo); | ||
166 | plugin->dbh = PQconnectdb (conninfo == NULL ? "" : conninfo); | 165 | plugin->dbh = PQconnectdb (conninfo == NULL ? "" : conninfo); |
167 | if (NULL == plugin->dbh) | 166 | if (NULL == plugin->dbh) |
168 | { | 167 | { |
169 | /* FIXME: warn about out-of-memory? */ | 168 | /* FIXME: warn about out-of-memory? */ |
170 | GNUNET_free_non_null (conninfo); | 169 | GNUNET_free_non_null (conninfo); |
171 | return GNUNET_SYSERR; | 170 | return GNUNET_SYSERR; |
172 | } | 171 | } |
173 | if (PQstatus (plugin->dbh) != CONNECTION_OK) | 172 | if (PQstatus (plugin->dbh) != CONNECTION_OK) |
174 | { | 173 | { |
175 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | 174 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, |
176 | "datastore-postgres", | 175 | "datastore-postgres", |
177 | _("Unable to initialize Postgres with configuration `%s': %s"), | 176 | _ |
178 | conninfo, | 177 | ("Unable to initialize Postgres with configuration `%s': %s"), |
179 | PQerrorMessage (plugin->dbh)); | 178 | conninfo, PQerrorMessage (plugin->dbh)); |
180 | PQfinish (plugin->dbh); | 179 | PQfinish (plugin->dbh); |
181 | plugin->dbh = NULL; | 180 | plugin->dbh = NULL; |
182 | GNUNET_free_non_null (conninfo); | 181 | GNUNET_free_non_null (conninfo); |
183 | return GNUNET_SYSERR; | 182 | return GNUNET_SYSERR; |
184 | } | 183 | } |
185 | GNUNET_free_non_null (conninfo); | 184 | GNUNET_free_non_null (conninfo); |
186 | ret = PQexec (plugin->dbh, | 185 | ret = PQexec (plugin->dbh, |
187 | "CREATE TABLE gn090 (" | 186 | "CREATE TABLE gn090 (" |
@@ -194,176 +193,169 @@ init_connection (struct Plugin *plugin) | |||
194 | " hash BYTEA NOT NULL DEFAULT ''," | 193 | " hash BYTEA NOT NULL DEFAULT ''," |
195 | " vhash BYTEA NOT NULL DEFAULT ''," | 194 | " vhash BYTEA NOT NULL DEFAULT ''," |
196 | " value BYTEA NOT NULL DEFAULT '')" "WITH OIDS"); | 195 | " value BYTEA NOT NULL DEFAULT '')" "WITH OIDS"); |
197 | if ( (ret == NULL) || | 196 | if ((ret == NULL) || ((PQresultStatus (ret) != PGRES_COMMAND_OK) && (0 != strcmp ("42P07", /* duplicate table */ |
198 | ( (PQresultStatus (ret) != PGRES_COMMAND_OK) && | 197 | PQresultErrorField |
199 | (0 != strcmp ("42P07", /* duplicate table */ | 198 | (ret, |
200 | PQresultErrorField | 199 | PG_DIAG_SQLSTATE))))) |
201 | (ret, | 200 | { |
202 | PG_DIAG_SQLSTATE))))) | 201 | (void) check_result (plugin, |
202 | ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090", | ||
203 | __LINE__); | ||
204 | PQfinish (plugin->dbh); | ||
205 | plugin->dbh = NULL; | ||
206 | return GNUNET_SYSERR; | ||
207 | } | ||
208 | if (PQresultStatus (ret) == PGRES_COMMAND_OK) | ||
209 | { | ||
210 | if ((GNUNET_OK != | ||
211 | pq_exec (plugin, "CREATE INDEX idx_hash ON gn090 (hash)", __LINE__)) || | ||
212 | (GNUNET_OK != | ||
213 | pq_exec (plugin, "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)", | ||
214 | __LINE__)) | ||
215 | || (GNUNET_OK != | ||
216 | pq_exec (plugin, "CREATE INDEX idx_prio ON gn090 (prio)", __LINE__)) | ||
217 | || (GNUNET_OK != | ||
218 | pq_exec (plugin, "CREATE INDEX idx_expire ON gn090 (expire)", | ||
219 | __LINE__)) || | ||
220 | (GNUNET_OK != | ||
221 | pq_exec (plugin, | ||
222 | "CREATE INDEX idx_prio_anon ON gn090 (prio,anonLevel)", | ||
223 | __LINE__)) || | ||
224 | (GNUNET_OK != | ||
225 | pq_exec (plugin, | ||
226 | "CREATE INDEX idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)", | ||
227 | __LINE__)) || | ||
228 | (GNUNET_OK != | ||
229 | pq_exec (plugin, "CREATE INDEX idx_repl_rvalue ON gn090 (repl,rvalue)", | ||
230 | __LINE__)) || | ||
231 | (GNUNET_OK != | ||
232 | pq_exec (plugin, "CREATE INDEX idx_expire_hash ON gn090 (expire,hash)", | ||
233 | __LINE__))) | ||
203 | { | 234 | { |
204 | (void) check_result (plugin, | 235 | PQclear (ret); |
205 | ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090", __LINE__); | ||
206 | PQfinish (plugin->dbh); | 236 | PQfinish (plugin->dbh); |
207 | plugin->dbh = NULL; | 237 | plugin->dbh = NULL; |
208 | return GNUNET_SYSERR; | 238 | return GNUNET_SYSERR; |
209 | } | 239 | } |
210 | if (PQresultStatus (ret) == PGRES_COMMAND_OK) | 240 | } |
211 | { | ||
212 | if ((GNUNET_OK != | ||
213 | pq_exec (plugin, "CREATE INDEX idx_hash ON gn090 (hash)", __LINE__)) || | ||
214 | (GNUNET_OK != | ||
215 | pq_exec (plugin, "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)", | ||
216 | __LINE__)) | ||
217 | || (GNUNET_OK != | ||
218 | pq_exec (plugin, "CREATE INDEX idx_prio ON gn090 (prio)", __LINE__)) | ||
219 | || (GNUNET_OK != | ||
220 | pq_exec (plugin, "CREATE INDEX idx_expire ON gn090 (expire)", __LINE__)) | ||
221 | || (GNUNET_OK != | ||
222 | pq_exec (plugin, "CREATE INDEX idx_prio_anon ON gn090 (prio,anonLevel)", | ||
223 | __LINE__)) | ||
224 | || (GNUNET_OK != | ||
225 | pq_exec | ||
226 | (plugin, "CREATE INDEX idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)", | ||
227 | __LINE__)) | ||
228 | || (GNUNET_OK != | ||
229 | pq_exec | ||
230 | (plugin, "CREATE INDEX idx_repl_rvalue ON gn090 (repl,rvalue)", | ||
231 | __LINE__)) | ||
232 | || (GNUNET_OK != | ||
233 | pq_exec (plugin, "CREATE INDEX idx_expire_hash ON gn090 (expire,hash)", | ||
234 | __LINE__))) | ||
235 | { | ||
236 | PQclear (ret); | ||
237 | PQfinish (plugin->dbh); | ||
238 | plugin->dbh = NULL; | ||
239 | return GNUNET_SYSERR; | ||
240 | } | ||
241 | } | ||
242 | PQclear (ret); | 241 | PQclear (ret); |
243 | ret = PQexec (plugin->dbh, | 242 | ret = PQexec (plugin->dbh, |
244 | "ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL"); | 243 | "ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL"); |
245 | if (GNUNET_OK != | 244 | if (GNUNET_OK != |
246 | check_result (plugin, | 245 | check_result (plugin, |
247 | ret, PGRES_COMMAND_OK, | 246 | ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090", __LINE__)) |
248 | "ALTER TABLE", "gn090", __LINE__)) | 247 | { |
249 | { | 248 | PQfinish (plugin->dbh); |
250 | PQfinish (plugin->dbh); | 249 | plugin->dbh = NULL; |
251 | plugin->dbh = NULL; | 250 | return GNUNET_SYSERR; |
252 | return GNUNET_SYSERR; | 251 | } |
253 | } | ||
254 | PQclear (ret); | 252 | PQclear (ret); |
255 | ret = PQexec (plugin->dbh, | 253 | ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN"); |
256 | "ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN"); | ||
257 | if (GNUNET_OK != | 254 | if (GNUNET_OK != |
258 | check_result (plugin, | 255 | check_result (plugin, |
259 | ret, PGRES_COMMAND_OK, | 256 | ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090", __LINE__)) |
260 | "ALTER TABLE", "gn090", __LINE__)) | 257 | { |
261 | { | 258 | PQfinish (plugin->dbh); |
262 | PQfinish (plugin->dbh); | 259 | plugin->dbh = NULL; |
263 | plugin->dbh = NULL; | 260 | return GNUNET_SYSERR; |
264 | return GNUNET_SYSERR; | 261 | } |
265 | } | ||
266 | PQclear (ret); | 262 | PQclear (ret); |
267 | ret = PQexec (plugin->dbh, | 263 | ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN"); |
268 | "ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN"); | ||
269 | if (GNUNET_OK != | 264 | if (GNUNET_OK != |
270 | check_result (plugin, | 265 | check_result (plugin, |
271 | ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090", __LINE__)) | 266 | ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090", __LINE__)) |
272 | { | 267 | { |
273 | PQfinish (plugin->dbh); | 268 | PQfinish (plugin->dbh); |
274 | plugin->dbh = NULL; | 269 | plugin->dbh = NULL; |
275 | return GNUNET_SYSERR; | 270 | return GNUNET_SYSERR; |
276 | } | 271 | } |
277 | PQclear (ret); | 272 | PQclear (ret); |
278 | if ((GNUNET_OK != | 273 | if ((GNUNET_OK != |
279 | pq_prepare (plugin, | 274 | pq_prepare (plugin, |
280 | "getvt", | 275 | "getvt", |
281 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " | 276 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " |
282 | "WHERE hash=$1 AND vhash=$2 AND type=$3 " | 277 | "WHERE hash=$1 AND vhash=$2 AND type=$3 " |
283 | "ORDER BY oid ASC LIMIT 1 OFFSET $4", | 278 | "ORDER BY oid ASC LIMIT 1 OFFSET $4", |
284 | 4, | 279 | 4, |
285 | __LINE__)) || | 280 | __LINE__)) || |
286 | (GNUNET_OK != | 281 | (GNUNET_OK != |
287 | pq_prepare (plugin, | 282 | pq_prepare (plugin, |
288 | "gett", | 283 | "gett", |
289 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " | 284 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " |
290 | "WHERE hash=$1 AND type=$2 " | 285 | "WHERE hash=$1 AND type=$2 " |
291 | "ORDER BY oid ASC LIMIT 1 OFFSET $3", | 286 | "ORDER BY oid ASC LIMIT 1 OFFSET $3", |
292 | 3, | 287 | 3, |
293 | __LINE__)) || | 288 | __LINE__)) || |
294 | (GNUNET_OK != | 289 | (GNUNET_OK != |
295 | pq_prepare (plugin, | 290 | pq_prepare (plugin, |
296 | "getv", | 291 | "getv", |
297 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " | 292 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " |
298 | "WHERE hash=$1 AND vhash=$2 " | 293 | "WHERE hash=$1 AND vhash=$2 " |
299 | "ORDER BY oid ASC LIMIT 1 OFFSET $3", | 294 | "ORDER BY oid ASC LIMIT 1 OFFSET $3", |
300 | 3, | 295 | 3, |
301 | __LINE__)) || | 296 | __LINE__)) || |
302 | (GNUNET_OK != | 297 | (GNUNET_OK != |
303 | pq_prepare (plugin, | 298 | pq_prepare (plugin, |
304 | "get", | 299 | "get", |
305 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " | 300 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " |
306 | "WHERE hash=$1 " | 301 | "WHERE hash=$1 " |
307 | "ORDER BY oid ASC LIMIT 1 OFFSET $2", | 302 | "ORDER BY oid ASC LIMIT 1 OFFSET $2", |
308 | 2, | 303 | 2, |
309 | __LINE__)) || | 304 | __LINE__)) || |
310 | (GNUNET_OK != | 305 | (GNUNET_OK != |
311 | pq_prepare (plugin, | 306 | pq_prepare (plugin, |
312 | "put", | 307 | "put", |
313 | "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) " | 308 | "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) " |
314 | "VALUES ($1, $2, $3, $4, $5, RANDOM(), $6, $7, $8)", | 309 | "VALUES ($1, $2, $3, $4, $5, RANDOM(), $6, $7, $8)", |
315 | 9, | 310 | 9, |
316 | __LINE__)) || | 311 | __LINE__)) || |
317 | (GNUNET_OK != | 312 | (GNUNET_OK != |
318 | pq_prepare (plugin, | 313 | pq_prepare (plugin, |
319 | "update", | 314 | "update", |
320 | "UPDATE gn090 SET prio = prio + $1, expire = CASE WHEN expire < $2 THEN $2 ELSE expire END " | 315 | "UPDATE gn090 SET prio = prio + $1, expire = CASE WHEN expire < $2 THEN $2 ELSE expire END " |
321 | "WHERE oid = $3", | 316 | "WHERE oid = $3", |
322 | 3, | 317 | 3, |
323 | __LINE__)) || | 318 | __LINE__)) || |
324 | (GNUNET_OK != | 319 | (GNUNET_OK != |
325 | pq_prepare (plugin, | 320 | pq_prepare (plugin, |
326 | "decrepl", | 321 | "decrepl", |
327 | "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) " | 322 | "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) " |
328 | "WHERE oid = $1", | 323 | "WHERE oid = $1", |
329 | 1, | 324 | 1, |
330 | __LINE__)) || | 325 | __LINE__)) || |
331 | (GNUNET_OK != | 326 | (GNUNET_OK != |
332 | pq_prepare (plugin, | 327 | pq_prepare (plugin, |
333 | "select_non_anonymous", | 328 | "select_non_anonymous", |
334 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " | 329 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " |
335 | "WHERE anonLevel = 0 AND type = $1 ORDER BY oid DESC LIMIT 1 OFFSET $2", | 330 | "WHERE anonLevel = 0 AND type = $1 ORDER BY oid DESC LIMIT 1 OFFSET $2", |
336 | 1, | 331 | 1, |
337 | __LINE__)) || | 332 | __LINE__)) || |
338 | (GNUNET_OK != | 333 | (GNUNET_OK != |
339 | pq_prepare (plugin, | 334 | pq_prepare (plugin, |
340 | "select_expiration_order", | 335 | "select_expiration_order", |
341 | "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " | 336 | "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " |
342 | "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) " | 337 | "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) " |
343 | "UNION " | 338 | "UNION " |
344 | "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " | 339 | "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " |
345 | "ORDER BY prio ASC LIMIT 1) " | 340 | "ORDER BY prio ASC LIMIT 1) " |
346 | "ORDER BY expire ASC LIMIT 1", | 341 | "ORDER BY expire ASC LIMIT 1", |
347 | 1, | 342 | 1, |
348 | __LINE__)) || | 343 | __LINE__)) || |
349 | (GNUNET_OK != | 344 | (GNUNET_OK != |
350 | pq_prepare (plugin, | 345 | pq_prepare (plugin, |
351 | "select_replication_order", | 346 | "select_replication_order", |
352 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " \ | 347 | "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 " |
353 | "ORDER BY repl DESC,RANDOM() LIMIT 1", | 348 | "ORDER BY repl DESC,RANDOM() LIMIT 1", |
354 | 0, | 349 | 0, |
355 | __LINE__)) || | 350 | __LINE__)) || |
356 | (GNUNET_OK != | 351 | (GNUNET_OK != |
357 | pq_prepare (plugin, | 352 | pq_prepare (plugin, |
358 | "delrow", | 353 | "delrow", "DELETE FROM gn090 " "WHERE oid=$1", 1, __LINE__))) |
359 | "DELETE FROM gn090 " "WHERE oid=$1", | 354 | { |
360 | 1, | 355 | PQfinish (plugin->dbh); |
361 | __LINE__))) | 356 | plugin->dbh = NULL; |
362 | { | 357 | return GNUNET_SYSERR; |
363 | PQfinish (plugin->dbh); | 358 | } |
364 | plugin->dbh = NULL; | ||
365 | return GNUNET_SYSERR; | ||
366 | } | ||
367 | return GNUNET_OK; | 359 | return GNUNET_OK; |
368 | } | 360 | } |
369 | 361 | ||
@@ -377,8 +369,7 @@ init_connection (struct Plugin *plugin) | |||
377 | * @return GNUNET_OK on success | 369 | * @return GNUNET_OK on success |
378 | */ | 370 | */ |
379 | static int | 371 | static int |
380 | delete_by_rowid (struct Plugin *plugin, | 372 | delete_by_rowid (struct Plugin *plugin, unsigned int rowid) |
381 | unsigned int rowid) | ||
382 | { | 373 | { |
383 | uint32_t browid; | 374 | uint32_t browid; |
384 | const char *paramValues[] = { (const char *) &browid }; | 375 | const char *paramValues[] = { (const char *) &browid }; |
@@ -392,11 +383,11 @@ delete_by_rowid (struct Plugin *plugin, | |||
392 | 1, paramValues, paramLengths, paramFormats, 1); | 383 | 1, paramValues, paramLengths, paramFormats, 1); |
393 | if (GNUNET_OK != | 384 | if (GNUNET_OK != |
394 | check_result (plugin, | 385 | check_result (plugin, |
395 | ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow", | 386 | ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow", |
396 | __LINE__)) | 387 | __LINE__)) |
397 | { | 388 | { |
398 | return GNUNET_SYSERR; | 389 | return GNUNET_SYSERR; |
399 | } | 390 | } |
400 | PQclear (ret); | 391 | PQclear (ret); |
401 | return GNUNET_OK; | 392 | return GNUNET_OK; |
402 | } | 393 | } |
@@ -417,25 +408,23 @@ postgres_plugin_estimate_size (void *cls) | |||
417 | PGresult *ret; | 408 | PGresult *ret; |
418 | 409 | ||
419 | ret = PQexecParams (plugin->dbh, | 410 | ret = PQexecParams (plugin->dbh, |
420 | "SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090", | 411 | "SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090", |
421 | 0, NULL, NULL, NULL, NULL, 1); | 412 | 0, NULL, NULL, NULL, NULL, 1); |
422 | if (GNUNET_OK != check_result (plugin, | 413 | if (GNUNET_OK != check_result (plugin, |
423 | ret, | 414 | ret, |
424 | PGRES_TUPLES_OK, | 415 | PGRES_TUPLES_OK, |
425 | "PQexecParams", | 416 | "PQexecParams", "get_size", __LINE__)) |
426 | "get_size", | 417 | { |
427 | __LINE__)) | 418 | return 0; |
428 | { | 419 | } |
429 | return 0; | ||
430 | } | ||
431 | if ((PQntuples (ret) != 1) || | 420 | if ((PQntuples (ret) != 1) || |
432 | (PQnfields (ret) != 1) || | 421 | (PQnfields (ret) != 1) || |
433 | (PQgetlength (ret, 0, 0) != sizeof (unsigned long long))) | 422 | (PQgetlength (ret, 0, 0) != sizeof (unsigned long long))) |
434 | { | 423 | { |
435 | GNUNET_break (0); | 424 | GNUNET_break (0); |
436 | PQclear (ret); | 425 | PQclear (ret); |
437 | return 0; | 426 | return 0; |
438 | } | 427 | } |
439 | total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0)); | 428 | total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0)); |
440 | PQclear (ret); | 429 | PQclear (ret); |
441 | return total; | 430 | return total; |
@@ -459,15 +448,14 @@ postgres_plugin_estimate_size (void *cls) | |||
459 | */ | 448 | */ |
460 | static int | 449 | static int |
461 | postgres_plugin_put (void *cls, | 450 | postgres_plugin_put (void *cls, |
462 | const GNUNET_HashCode * key, | 451 | const GNUNET_HashCode * key, |
463 | uint32_t size, | 452 | uint32_t size, |
464 | const void *data, | 453 | const void *data, |
465 | enum GNUNET_BLOCK_Type type, | 454 | enum GNUNET_BLOCK_Type type, |
466 | uint32_t priority, | 455 | uint32_t priority, |
467 | uint32_t anonymity, | 456 | uint32_t anonymity, |
468 | uint32_t replication, | 457 | uint32_t replication, |
469 | struct GNUNET_TIME_Absolute expiration, | 458 | struct GNUNET_TIME_Absolute expiration, char **msg) |
470 | char **msg) | ||
471 | { | 459 | { |
472 | struct Plugin *plugin = cls; | 460 | struct Plugin *plugin = cls; |
473 | GNUNET_HashCode vhash; | 461 | GNUNET_HashCode vhash; |
@@ -477,6 +465,7 @@ postgres_plugin_put (void *cls, | |||
477 | uint32_t banon = htonl (anonymity); | 465 | uint32_t banon = htonl (anonymity); |
478 | uint32_t brepl = htonl (replication); | 466 | uint32_t brepl = htonl (replication); |
479 | uint64_t bexpi = GNUNET_TIME_absolute_hton (expiration).abs_value__; | 467 | uint64_t bexpi = GNUNET_TIME_absolute_hton (expiration).abs_value__; |
468 | |||
480 | const char *paramValues[] = { | 469 | const char *paramValues[] = { |
481 | (const char *) &brepl, | 470 | (const char *) &brepl, |
482 | (const char *) &btype, | 471 | (const char *) &btype, |
@@ -510,9 +499,8 @@ postgres_plugin_put (void *cls, | |||
510 | plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD); | 499 | plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD); |
511 | #if DEBUG_POSTGRES | 500 | #if DEBUG_POSTGRES |
512 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 501 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
513 | "datastore-postgres", | 502 | "datastore-postgres", |
514 | "Stored %u bytes in database\n", | 503 | "Stored %u bytes in database\n", (unsigned int) size); |
515 | (unsigned int) size); | ||
516 | #endif | 504 | #endif |
517 | return GNUNET_OK; | 505 | return GNUNET_OK; |
518 | } | 506 | } |
@@ -528,11 +516,10 @@ postgres_plugin_put (void *cls, | |||
528 | * @param res result from exec | 516 | * @param res result from exec |
529 | * @param line line number for error messages | 517 | * @param line line number for error messages |
530 | */ | 518 | */ |
531 | static void | 519 | static void |
532 | process_result (struct Plugin *plugin, | 520 | process_result (struct Plugin *plugin, |
533 | PluginDatumProcessor proc, void *proc_cls, | 521 | PluginDatumProcessor proc, void *proc_cls, |
534 | PGresult *res, | 522 | PGresult * res, int line) |
535 | int line) | ||
536 | { | 523 | { |
537 | int iret; | 524 | int iret; |
538 | enum GNUNET_BLOCK_Type type; | 525 | enum GNUNET_BLOCK_Type type; |
@@ -544,115 +531,98 @@ process_result (struct Plugin *plugin, | |||
544 | GNUNET_HashCode key; | 531 | GNUNET_HashCode key; |
545 | 532 | ||
546 | if (GNUNET_OK != check_result (plugin, | 533 | if (GNUNET_OK != check_result (plugin, |
547 | res, | 534 | res, |
548 | PGRES_TUPLES_OK, | 535 | PGRES_TUPLES_OK, |
549 | "PQexecPrepared", | 536 | "PQexecPrepared", "select", line)) |
550 | "select", | 537 | { |
551 | line)) | ||
552 | { | ||
553 | #if DEBUG_POSTGRES | 538 | #if DEBUG_POSTGRES |
554 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 539 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
555 | "datastore-postgres", | 540 | "datastore-postgres", |
556 | "Ending iteration (postgres error)\n"); | 541 | "Ending iteration (postgres error)\n"); |
557 | #endif | 542 | #endif |
558 | proc (proc_cls, | 543 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); |
559 | NULL, 0, NULL, 0, 0, 0, | 544 | return; |
560 | GNUNET_TIME_UNIT_ZERO_ABS, 0); | 545 | } |
561 | return; | ||
562 | } | ||
563 | 546 | ||
564 | if (0 == PQntuples (res)) | 547 | if (0 == PQntuples (res)) |
565 | { | 548 | { |
566 | /* no result */ | 549 | /* no result */ |
567 | #if DEBUG_POSTGRES | 550 | #if DEBUG_POSTGRES |
568 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 551 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
569 | "datastore-postgres", | 552 | "datastore-postgres", |
570 | "Ending iteration (no more results)\n"); | 553 | "Ending iteration (no more results)\n"); |
571 | #endif | 554 | #endif |
572 | proc (proc_cls, | 555 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); |
573 | NULL, 0, NULL, 0, 0, 0, | 556 | PQclear (res); |
574 | GNUNET_TIME_UNIT_ZERO_ABS, 0); | 557 | return; |
575 | PQclear (res); | 558 | } |
576 | return; | ||
577 | } | ||
578 | if ((1 != PQntuples (res)) || | 559 | if ((1 != PQntuples (res)) || |
579 | (7 != PQnfields (res)) || | 560 | (7 != PQnfields (res)) || |
580 | (sizeof (uint32_t) != PQfsize (res, 0)) || | 561 | (sizeof (uint32_t) != PQfsize (res, 0)) || |
581 | (sizeof (uint32_t) != PQfsize (res, 6))) | 562 | (sizeof (uint32_t) != PQfsize (res, 6))) |
582 | { | 563 | { |
583 | GNUNET_break (0); | 564 | GNUNET_break (0); |
584 | proc (proc_cls, | 565 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); |
585 | NULL, 0, NULL, 0, 0, 0, | 566 | PQclear (res); |
586 | GNUNET_TIME_UNIT_ZERO_ABS, 0); | 567 | return; |
587 | PQclear (res); | 568 | } |
588 | return; | ||
589 | } | ||
590 | rowid = ntohl (*(uint32_t *) PQgetvalue (res, 0, 6)); | 569 | rowid = ntohl (*(uint32_t *) PQgetvalue (res, 0, 6)); |
591 | if ((sizeof (uint32_t) != PQfsize (res, 0)) || | 570 | if ((sizeof (uint32_t) != PQfsize (res, 0)) || |
592 | (sizeof (uint32_t) != PQfsize (res, 1)) || | 571 | (sizeof (uint32_t) != PQfsize (res, 1)) || |
593 | (sizeof (uint32_t) != PQfsize (res, 2)) || | 572 | (sizeof (uint32_t) != PQfsize (res, 2)) || |
594 | (sizeof (uint64_t) != PQfsize (res, 3)) || | 573 | (sizeof (uint64_t) != PQfsize (res, 3)) || |
595 | (sizeof (GNUNET_HashCode) != PQgetlength (res, 0, 4)) ) | 574 | (sizeof (GNUNET_HashCode) != PQgetlength (res, 0, 4))) |
596 | { | 575 | { |
597 | GNUNET_break (0); | 576 | GNUNET_break (0); |
598 | PQclear (res); | 577 | PQclear (res); |
599 | delete_by_rowid (plugin, rowid); | 578 | delete_by_rowid (plugin, rowid); |
600 | proc (proc_cls, | 579 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); |
601 | NULL, 0, NULL, 0, 0, 0, | 580 | return; |
602 | GNUNET_TIME_UNIT_ZERO_ABS, 0); | 581 | } |
603 | return; | ||
604 | } | ||
605 | 582 | ||
606 | type = ntohl (*(uint32_t *) PQgetvalue (res, 0, 0)); | 583 | type = ntohl (*(uint32_t *) PQgetvalue (res, 0, 0)); |
607 | priority = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1)); | 584 | priority = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1)); |
608 | anonymity = ntohl ( *(uint32_t *) PQgetvalue (res, 0, 2)); | 585 | anonymity = ntohl (*(uint32_t *) PQgetvalue (res, 0, 2)); |
609 | expiration_time.abs_value = GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, 0, 3)); | 586 | expiration_time.abs_value = |
610 | memcpy (&key, | 587 | GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, 0, 3)); |
611 | PQgetvalue (res, 0, 4), | 588 | memcpy (&key, PQgetvalue (res, 0, 4), sizeof (GNUNET_HashCode)); |
612 | sizeof (GNUNET_HashCode)); | ||
613 | size = PQgetlength (res, 0, 5); | 589 | size = PQgetlength (res, 0, 5); |
614 | #if DEBUG_POSTGRES | 590 | #if DEBUG_POSTGRES |
615 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 591 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
616 | "datastore-postgres", | 592 | "datastore-postgres", |
617 | "Found result of size %u bytes and type %u in database\n", | 593 | "Found result of size %u bytes and type %u in database\n", |
618 | (unsigned int) size, | 594 | (unsigned int) size, (unsigned int) type); |
619 | (unsigned int) type); | ||
620 | #endif | 595 | #endif |
621 | iret = proc (proc_cls, | 596 | iret = proc (proc_cls, |
622 | &key, | 597 | &key, |
623 | size, | 598 | size, |
624 | PQgetvalue (res, 0, 5), | 599 | PQgetvalue (res, 0, 5), |
625 | (enum GNUNET_BLOCK_Type) type, | 600 | (enum GNUNET_BLOCK_Type) type, |
626 | priority, | 601 | priority, anonymity, expiration_time, rowid); |
627 | anonymity, | ||
628 | expiration_time, | ||
629 | rowid); | ||
630 | PQclear (res); | 602 | PQclear (res); |
631 | if (iret == GNUNET_NO) | 603 | if (iret == GNUNET_NO) |
632 | { | 604 | { |
633 | #if DEBUG_POSTGRES | 605 | #if DEBUG_POSTGRES |
634 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 606 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
635 | "Processor asked for item %u to be removed.\n", | 607 | "Processor asked for item %u to be removed.\n", rowid); |
636 | rowid); | ||
637 | #endif | 608 | #endif |
638 | if (GNUNET_OK == delete_by_rowid (plugin, rowid)) | 609 | if (GNUNET_OK == delete_by_rowid (plugin, rowid)) |
639 | { | 610 | { |
640 | #if DEBUG_POSTGRES | 611 | #if DEBUG_POSTGRES |
641 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 612 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
642 | "datastore-postgres", | 613 | "datastore-postgres", |
643 | "Deleting %u bytes from database\n", | 614 | "Deleting %u bytes from database\n", |
644 | (unsigned int) size); | 615 | (unsigned int) size); |
645 | #endif | 616 | #endif |
646 | plugin->env->duc (plugin->env->cls, | 617 | plugin->env->duc (plugin->env->cls, |
647 | - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD)); | 618 | -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD)); |
648 | #if DEBUG_POSTGRES | 619 | #if DEBUG_POSTGRES |
649 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 620 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
650 | "datastore-postgres", | 621 | "datastore-postgres", |
651 | "Deleted %u bytes from database\n", | 622 | "Deleted %u bytes from database\n", (unsigned int) size); |
652 | (unsigned int) size); | ||
653 | #endif | 623 | #endif |
654 | } | ||
655 | } | 624 | } |
625 | } | ||
656 | } | 626 | } |
657 | 627 | ||
658 | 628 | ||
@@ -677,11 +647,11 @@ process_result (struct Plugin *plugin, | |||
677 | */ | 647 | */ |
678 | static void | 648 | static void |
679 | postgres_plugin_get_key (void *cls, | 649 | postgres_plugin_get_key (void *cls, |
680 | uint64_t offset, | 650 | uint64_t offset, |
681 | const GNUNET_HashCode *key, | 651 | const GNUNET_HashCode * key, |
682 | const GNUNET_HashCode *vhash, | 652 | const GNUNET_HashCode * vhash, |
683 | enum GNUNET_BLOCK_Type type, | 653 | enum GNUNET_BLOCK_Type type, |
684 | PluginDatumProcessor proc, void *proc_cls) | 654 | PluginDatumProcessor proc, void *proc_cls) |
685 | { | 655 | { |
686 | struct Plugin *plugin = cls; | 656 | struct Plugin *plugin = cls; |
687 | const int paramFormats[] = { 1, 1, 1, 1, 1 }; | 657 | const int paramFormats[] = { 1, 1, 1, 1, 1 }; |
@@ -695,121 +665,92 @@ postgres_plugin_get_key (void *cls, | |||
695 | uint32_t btype; | 665 | uint32_t btype; |
696 | 666 | ||
697 | GNUNET_assert (key != NULL); | 667 | GNUNET_assert (key != NULL); |
698 | paramValues[0] = (const char*) key; | 668 | paramValues[0] = (const char *) key; |
699 | paramLengths[0] = sizeof (GNUNET_HashCode); | 669 | paramLengths[0] = sizeof (GNUNET_HashCode); |
700 | btype = htonl (type); | 670 | btype = htonl (type); |
701 | if (type != 0) | 671 | if (type != 0) |
672 | { | ||
673 | if (vhash != NULL) | ||
674 | { | ||
675 | paramValues[1] = (const char *) vhash; | ||
676 | paramLengths[1] = sizeof (GNUNET_HashCode); | ||
677 | paramValues[2] = (const char *) &btype; | ||
678 | paramLengths[2] = sizeof (btype); | ||
679 | paramValues[3] = (const char *) &blimit_off; | ||
680 | paramLengths[3] = sizeof (blimit_off); | ||
681 | nparams = 4; | ||
682 | pname = "getvt"; | ||
683 | ret = PQexecParams (plugin->dbh, | ||
684 | "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2 AND type=$3", | ||
685 | 3, NULL, paramValues, paramLengths, paramFormats, 1); | ||
686 | } | ||
687 | else | ||
702 | { | 688 | { |
703 | if (vhash != NULL) | 689 | paramValues[1] = (const char *) &btype; |
704 | { | 690 | paramLengths[1] = sizeof (btype); |
705 | paramValues[1] = (const char *) vhash; | 691 | paramValues[2] = (const char *) &blimit_off; |
706 | paramLengths[1] = sizeof (GNUNET_HashCode); | 692 | paramLengths[2] = sizeof (blimit_off); |
707 | paramValues[2] = (const char *) &btype; | 693 | nparams = 3; |
708 | paramLengths[2] = sizeof (btype); | 694 | pname = "gett"; |
709 | paramValues[3] = (const char *) &blimit_off; | 695 | ret = PQexecParams (plugin->dbh, |
710 | paramLengths[3] = sizeof (blimit_off); | 696 | "SELECT count(*) FROM gn090 WHERE hash=$1 AND type=$2", |
711 | nparams = 4; | 697 | 2, NULL, paramValues, paramLengths, paramFormats, 1); |
712 | pname = "getvt"; | ||
713 | ret = PQexecParams (plugin->dbh, | ||
714 | "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2 AND type=$3", | ||
715 | 3, | ||
716 | NULL, | ||
717 | paramValues, | ||
718 | paramLengths, | ||
719 | paramFormats, 1); | ||
720 | } | ||
721 | else | ||
722 | { | ||
723 | paramValues[1] = (const char *) &btype; | ||
724 | paramLengths[1] = sizeof (btype); | ||
725 | paramValues[2] = (const char *) &blimit_off; | ||
726 | paramLengths[2] = sizeof (blimit_off); | ||
727 | nparams = 3; | ||
728 | pname = "gett"; | ||
729 | ret = PQexecParams (plugin->dbh, | ||
730 | "SELECT count(*) FROM gn090 WHERE hash=$1 AND type=$2", | ||
731 | 2, | ||
732 | NULL, | ||
733 | paramValues, | ||
734 | paramLengths, | ||
735 | paramFormats, 1); | ||
736 | } | ||
737 | } | 698 | } |
699 | } | ||
738 | else | 700 | else |
701 | { | ||
702 | if (vhash != NULL) | ||
739 | { | 703 | { |
740 | if (vhash != NULL) | 704 | paramValues[1] = (const char *) vhash; |
741 | { | 705 | paramLengths[1] = sizeof (GNUNET_HashCode); |
742 | paramValues[1] = (const char *) vhash; | 706 | paramValues[2] = (const char *) &blimit_off; |
743 | paramLengths[1] = sizeof (GNUNET_HashCode); | 707 | paramLengths[2] = sizeof (blimit_off); |
744 | paramValues[2] = (const char *) &blimit_off; | 708 | nparams = 3; |
745 | paramLengths[2] = sizeof (blimit_off); | 709 | pname = "getv"; |
746 | nparams = 3; | 710 | ret = PQexecParams (plugin->dbh, |
747 | pname = "getv"; | 711 | "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2", |
748 | ret = PQexecParams (plugin->dbh, | 712 | 2, NULL, paramValues, paramLengths, paramFormats, 1); |
749 | "SELECT count(*) FROM gn090 WHERE hash=$1 AND vhash=$2", | ||
750 | 2, | ||
751 | NULL, | ||
752 | paramValues, | ||
753 | paramLengths, | ||
754 | paramFormats, 1); | ||
755 | } | ||
756 | else | ||
757 | { | ||
758 | paramValues[1] = (const char *) &blimit_off; | ||
759 | paramLengths[1] = sizeof (blimit_off); | ||
760 | nparams = 2; | ||
761 | pname = "get"; | ||
762 | ret = PQexecParams (plugin->dbh, | ||
763 | "SELECT count(*) FROM gn090 WHERE hash=$1", | ||
764 | 1, | ||
765 | NULL, | ||
766 | paramValues, | ||
767 | paramLengths, | ||
768 | paramFormats, 1); | ||
769 | } | ||
770 | } | 713 | } |
771 | if (GNUNET_OK != check_result (plugin, | 714 | else |
772 | ret, | ||
773 | PGRES_TUPLES_OK, | ||
774 | "PQexecParams", | ||
775 | pname, | ||
776 | __LINE__)) | ||
777 | { | 715 | { |
778 | proc (proc_cls, | 716 | paramValues[1] = (const char *) &blimit_off; |
779 | NULL, 0, NULL, 0, 0, 0, | 717 | paramLengths[1] = sizeof (blimit_off); |
780 | GNUNET_TIME_UNIT_ZERO_ABS, 0); | 718 | nparams = 2; |
781 | return; | 719 | pname = "get"; |
720 | ret = PQexecParams (plugin->dbh, | ||
721 | "SELECT count(*) FROM gn090 WHERE hash=$1", | ||
722 | 1, NULL, paramValues, paramLengths, paramFormats, 1); | ||
782 | } | 723 | } |
724 | } | ||
725 | if (GNUNET_OK != check_result (plugin, | ||
726 | ret, | ||
727 | PGRES_TUPLES_OK, | ||
728 | "PQexecParams", pname, __LINE__)) | ||
729 | { | ||
730 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); | ||
731 | return; | ||
732 | } | ||
783 | if ((PQntuples (ret) != 1) || | 733 | if ((PQntuples (ret) != 1) || |
784 | (PQnfields (ret) != 1) || | 734 | (PQnfields (ret) != 1) || |
785 | (PQgetlength (ret, 0, 0) != sizeof (unsigned long long))) | 735 | (PQgetlength (ret, 0, 0) != sizeof (unsigned long long))) |
786 | { | 736 | { |
787 | GNUNET_break (0); | 737 | GNUNET_break (0); |
788 | PQclear (ret); | 738 | PQclear (ret); |
789 | proc (proc_cls, | 739 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); |
790 | NULL, 0, NULL, 0, 0, 0, | 740 | return; |
791 | GNUNET_TIME_UNIT_ZERO_ABS, 0); | 741 | } |
792 | return; | ||
793 | } | ||
794 | total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0)); | 742 | total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0)); |
795 | PQclear (ret); | 743 | PQclear (ret); |
796 | if (total == 0) | 744 | if (total == 0) |
797 | { | 745 | { |
798 | proc (proc_cls, | 746 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); |
799 | NULL, 0, NULL, 0, 0, 0, | 747 | return; |
800 | GNUNET_TIME_UNIT_ZERO_ABS, 0); | 748 | } |
801 | return; | ||
802 | } | ||
803 | blimit_off = GNUNET_htonll (offset % total); | 749 | blimit_off = GNUNET_htonll (offset % total); |
804 | ret = PQexecPrepared (plugin->dbh, | 750 | ret = PQexecPrepared (plugin->dbh, |
805 | pname, | 751 | pname, |
806 | nparams, | 752 | nparams, paramValues, paramLengths, paramFormats, 1); |
807 | paramValues, | 753 | process_result (plugin, proc, proc_cls, ret, __LINE__); |
808 | paramLengths, | ||
809 | paramFormats, 1); | ||
810 | process_result (plugin, | ||
811 | proc, proc_cls, | ||
812 | ret, __LINE__); | ||
813 | } | 754 | } |
814 | 755 | ||
815 | 756 | ||
@@ -826,29 +767,24 @@ postgres_plugin_get_key (void *cls, | |||
826 | */ | 767 | */ |
827 | static void | 768 | static void |
828 | postgres_plugin_get_zero_anonymity (void *cls, | 769 | postgres_plugin_get_zero_anonymity (void *cls, |
829 | uint64_t offset, | 770 | uint64_t offset, |
830 | enum GNUNET_BLOCK_Type type, | 771 | enum GNUNET_BLOCK_Type type, |
831 | PluginDatumProcessor proc, void *proc_cls) | 772 | PluginDatumProcessor proc, void *proc_cls) |
832 | { | 773 | { |
833 | struct Plugin *plugin = cls; | 774 | struct Plugin *plugin = cls; |
834 | uint32_t btype; | 775 | uint32_t btype; |
835 | uint64_t boff; | 776 | uint64_t boff; |
836 | const int paramFormats[] = { 1, 1 }; | 777 | const int paramFormats[] = { 1, 1 }; |
837 | int paramLengths[] = { sizeof (btype), sizeof (boff) }; | 778 | int paramLengths[] = { sizeof (btype), sizeof (boff) }; |
838 | const char *paramValues[] = { (const char*) &btype, (const char*) &boff }; | 779 | const char *paramValues[] = { (const char *) &btype, (const char *) &boff }; |
839 | PGresult *ret; | 780 | PGresult *ret; |
840 | 781 | ||
841 | btype = htonl ((uint32_t) type); | 782 | btype = htonl ((uint32_t) type); |
842 | boff = GNUNET_htonll (offset); | 783 | boff = GNUNET_htonll (offset); |
843 | ret = PQexecPrepared (plugin->dbh, | 784 | ret = PQexecPrepared (plugin->dbh, |
844 | "select_non_anonymous", | 785 | "select_non_anonymous", |
845 | 2, | 786 | 2, paramValues, paramLengths, paramFormats, 1); |
846 | paramValues, | 787 | process_result (plugin, proc, proc_cls, ret, __LINE__); |
847 | paramLengths, | ||
848 | paramFormats, 1); | ||
849 | process_result (plugin, | ||
850 | proc, proc_cls, | ||
851 | ret, __LINE__); | ||
852 | } | 788 | } |
853 | 789 | ||
854 | 790 | ||
@@ -857,17 +793,17 @@ postgres_plugin_get_zero_anonymity (void *cls, | |||
857 | */ | 793 | */ |
858 | struct ReplCtx | 794 | struct ReplCtx |
859 | { | 795 | { |
860 | 796 | ||
861 | /** | 797 | /** |
862 | * Plugin handle. | 798 | * Plugin handle. |
863 | */ | 799 | */ |
864 | struct Plugin *plugin; | 800 | struct Plugin *plugin; |
865 | 801 | ||
866 | /** | 802 | /** |
867 | * Function to call for the result (or the NULL). | 803 | * Function to call for the result (or the NULL). |
868 | */ | 804 | */ |
869 | PluginDatumProcessor proc; | 805 | PluginDatumProcessor proc; |
870 | 806 | ||
871 | /** | 807 | /** |
872 | * Closure for proc. | 808 | * Closure for proc. |
873 | */ | 809 | */ |
@@ -898,14 +834,13 @@ struct ReplCtx | |||
898 | */ | 834 | */ |
899 | static int | 835 | static int |
900 | repl_proc (void *cls, | 836 | repl_proc (void *cls, |
901 | const GNUNET_HashCode *key, | 837 | const GNUNET_HashCode * key, |
902 | uint32_t size, | 838 | uint32_t size, |
903 | const void *data, | 839 | const void *data, |
904 | enum GNUNET_BLOCK_Type type, | 840 | enum GNUNET_BLOCK_Type type, |
905 | uint32_t priority, | 841 | uint32_t priority, |
906 | uint32_t anonymity, | 842 | uint32_t anonymity, |
907 | struct GNUNET_TIME_Absolute expiration, | 843 | struct GNUNET_TIME_Absolute expiration, uint64_t uid) |
908 | uint64_t uid) | ||
909 | { | 844 | { |
910 | struct ReplCtx *rc = cls; | 845 | struct ReplCtx *rc = cls; |
911 | struct Plugin *plugin = rc->plugin; | 846 | struct Plugin *plugin = rc->plugin; |
@@ -914,31 +849,27 @@ repl_proc (void *cls, | |||
914 | uint32_t boid; | 849 | uint32_t boid; |
915 | 850 | ||
916 | ret = rc->proc (rc->proc_cls, | 851 | ret = rc->proc (rc->proc_cls, |
917 | key, | 852 | key, size, data, type, priority, anonymity, expiration, uid); |
918 | size, data, | ||
919 | type, priority, anonymity, expiration, | ||
920 | uid); | ||
921 | if (NULL != key) | 853 | if (NULL != key) |
922 | { | 854 | { |
923 | boid = htonl ( (uint32_t) uid); | 855 | boid = htonl ((uint32_t) uid); |
924 | const char *paramValues[] = { | 856 | const char *paramValues[] = { |
925 | (const char *) &boid, | 857 | (const char *) &boid, |
926 | }; | 858 | }; |
927 | int paramLengths[] = { | 859 | int paramLengths[] = { |
928 | sizeof (boid), | 860 | sizeof (boid), |
929 | }; | 861 | }; |
930 | const int paramFormats[] = { 1 }; | 862 | const int paramFormats[] = { 1 }; |
931 | qret = PQexecPrepared (plugin->dbh, | 863 | qret = PQexecPrepared (plugin->dbh, |
932 | "decrepl", | 864 | "decrepl", |
933 | 1, paramValues, paramLengths, paramFormats, 1); | 865 | 1, paramValues, paramLengths, paramFormats, 1); |
934 | if (GNUNET_OK != check_result (plugin, | 866 | if (GNUNET_OK != check_result (plugin, |
935 | qret, | 867 | qret, |
936 | PGRES_COMMAND_OK, | 868 | PGRES_COMMAND_OK, |
937 | "PQexecPrepared", | 869 | "PQexecPrepared", "decrepl", __LINE__)) |
938 | "decrepl", __LINE__)) | 870 | return GNUNET_SYSERR; |
939 | return GNUNET_SYSERR; | 871 | PQclear (qret); |
940 | PQclear (qret); | 872 | } |
941 | } | ||
942 | return ret; | 873 | return ret; |
943 | } | 874 | } |
944 | 875 | ||
@@ -955,7 +886,7 @@ repl_proc (void *cls, | |||
955 | */ | 886 | */ |
956 | static void | 887 | static void |
957 | postgres_plugin_get_replication (void *cls, | 888 | postgres_plugin_get_replication (void *cls, |
958 | PluginDatumProcessor proc, void *proc_cls) | 889 | PluginDatumProcessor proc, void *proc_cls) |
959 | { | 890 | { |
960 | struct Plugin *plugin = cls; | 891 | struct Plugin *plugin = cls; |
961 | struct ReplCtx rc; | 892 | struct ReplCtx rc; |
@@ -965,12 +896,8 @@ postgres_plugin_get_replication (void *cls, | |||
965 | rc.proc = proc; | 896 | rc.proc = proc; |
966 | rc.proc_cls = proc_cls; | 897 | rc.proc_cls = proc_cls; |
967 | ret = PQexecPrepared (plugin->dbh, | 898 | ret = PQexecPrepared (plugin->dbh, |
968 | "select_replication_order", | 899 | "select_replication_order", 0, NULL, NULL, NULL, 1); |
969 | 0, | 900 | process_result (plugin, &repl_proc, &rc, ret, __LINE__); |
970 | NULL, NULL, NULL, 1); | ||
971 | process_result (plugin, | ||
972 | &repl_proc, &rc, | ||
973 | ret, __LINE__); | ||
974 | } | 901 | } |
975 | 902 | ||
976 | 903 | ||
@@ -984,26 +911,20 @@ postgres_plugin_get_replication (void *cls, | |||
984 | */ | 911 | */ |
985 | static void | 912 | static void |
986 | postgres_plugin_get_expiration (void *cls, | 913 | postgres_plugin_get_expiration (void *cls, |
987 | PluginDatumProcessor proc, void *proc_cls) | 914 | PluginDatumProcessor proc, void *proc_cls) |
988 | { | 915 | { |
989 | struct Plugin *plugin = cls; | 916 | struct Plugin *plugin = cls; |
990 | uint64_t btime; | 917 | uint64_t btime; |
991 | const int paramFormats[] = { 1 }; | 918 | const int paramFormats[] = { 1 }; |
992 | int paramLengths[] = { sizeof (btime) }; | 919 | int paramLengths[] = { sizeof (btime) }; |
993 | const char *paramValues[] = { (const char*) &btime }; | 920 | const char *paramValues[] = { (const char *) &btime }; |
994 | PGresult *ret; | 921 | PGresult *ret; |
995 | 922 | ||
996 | btime = GNUNET_htonll (GNUNET_TIME_absolute_get ().abs_value); | 923 | btime = GNUNET_htonll (GNUNET_TIME_absolute_get ().abs_value); |
997 | ret = PQexecPrepared (plugin->dbh, | 924 | ret = PQexecPrepared (plugin->dbh, |
998 | "select_expiration_order", | 925 | "select_expiration_order", |
999 | 1, | 926 | 1, paramValues, paramLengths, paramFormats, 1); |
1000 | paramValues, | 927 | process_result (plugin, proc, proc_cls, ret, __LINE__); |
1001 | paramLengths, | ||
1002 | paramFormats, | ||
1003 | 1); | ||
1004 | process_result (plugin, | ||
1005 | proc, proc_cls, | ||
1006 | ret, __LINE__); | ||
1007 | } | 928 | } |
1008 | 929 | ||
1009 | 930 | ||
@@ -1032,15 +953,16 @@ postgres_plugin_get_expiration (void *cls, | |||
1032 | */ | 953 | */ |
1033 | static int | 954 | static int |
1034 | postgres_plugin_update (void *cls, | 955 | postgres_plugin_update (void *cls, |
1035 | uint64_t uid, | 956 | uint64_t uid, |
1036 | int delta, struct GNUNET_TIME_Absolute expire, | 957 | int delta, struct GNUNET_TIME_Absolute expire, |
1037 | char **msg) | 958 | char **msg) |
1038 | { | 959 | { |
1039 | struct Plugin *plugin = cls; | 960 | struct Plugin *plugin = cls; |
1040 | PGresult *ret; | 961 | PGresult *ret; |
1041 | int32_t bdelta = (int32_t) htonl ((uint32_t) delta); | 962 | int32_t bdelta = (int32_t) htonl ((uint32_t) delta); |
1042 | uint32_t boid = htonl ( (uint32_t) uid); | 963 | uint32_t boid = htonl ((uint32_t) uid); |
1043 | uint64_t bexpire = GNUNET_TIME_absolute_hton (expire).abs_value__; | 964 | uint64_t bexpire = GNUNET_TIME_absolute_hton (expire).abs_value__; |
965 | |||
1044 | const char *paramValues[] = { | 966 | const char *paramValues[] = { |
1045 | (const char *) &bdelta, | 967 | (const char *) &bdelta, |
1046 | (const char *) &bexpire, | 968 | (const char *) &bexpire, |
@@ -1057,7 +979,7 @@ postgres_plugin_update (void *cls, | |||
1057 | "update", | 979 | "update", |
1058 | 3, paramValues, paramLengths, paramFormats, 1); | 980 | 3, paramValues, paramLengths, paramFormats, 1); |
1059 | if (GNUNET_OK != check_result (plugin, | 981 | if (GNUNET_OK != check_result (plugin, |
1060 | ret, | 982 | ret, |
1061 | PGRES_COMMAND_OK, | 983 | PGRES_COMMAND_OK, |
1062 | "PQexecPrepared", "update", __LINE__)) | 984 | "PQexecPrepared", "update", __LINE__)) |
1063 | return GNUNET_SYSERR; | 985 | return GNUNET_SYSERR; |
@@ -1069,7 +991,7 @@ postgres_plugin_update (void *cls, | |||
1069 | /** | 991 | /** |
1070 | * Drop database. | 992 | * Drop database. |
1071 | */ | 993 | */ |
1072 | static void | 994 | static void |
1073 | postgres_plugin_drop (void *cls) | 995 | postgres_plugin_drop (void *cls) |
1074 | { | 996 | { |
1075 | struct Plugin *plugin = cls; | 997 | struct Plugin *plugin = cls; |
@@ -1094,10 +1016,10 @@ libgnunet_plugin_datastore_postgres_init (void *cls) | |||
1094 | plugin = GNUNET_malloc (sizeof (struct Plugin)); | 1016 | plugin = GNUNET_malloc (sizeof (struct Plugin)); |
1095 | plugin->env = env; | 1017 | plugin->env = env; |
1096 | if (GNUNET_OK != init_connection (plugin)) | 1018 | if (GNUNET_OK != init_connection (plugin)) |
1097 | { | 1019 | { |
1098 | GNUNET_free (plugin); | 1020 | GNUNET_free (plugin); |
1099 | return NULL; | 1021 | return NULL; |
1100 | } | 1022 | } |
1101 | api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions)); | 1023 | api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions)); |
1102 | api->cls = plugin; | 1024 | api->cls = plugin; |
1103 | api->estimate_size = &postgres_plugin_estimate_size; | 1025 | api->estimate_size = &postgres_plugin_estimate_size; |
@@ -1109,8 +1031,7 @@ libgnunet_plugin_datastore_postgres_init (void *cls) | |||
1109 | api->get_zero_anonymity = &postgres_plugin_get_zero_anonymity; | 1031 | api->get_zero_anonymity = &postgres_plugin_get_zero_anonymity; |
1110 | api->drop = &postgres_plugin_drop; | 1032 | api->drop = &postgres_plugin_drop; |
1111 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, | 1033 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, |
1112 | "datastore-postgres", | 1034 | "datastore-postgres", _("Postgres database running\n")); |
1113 | _("Postgres database running\n")); | ||
1114 | return api; | 1035 | return api; |
1115 | } | 1036 | } |
1116 | 1037 | ||
@@ -1125,7 +1046,7 @@ libgnunet_plugin_datastore_postgres_done (void *cls) | |||
1125 | { | 1046 | { |
1126 | struct GNUNET_DATASTORE_PluginFunctions *api = cls; | 1047 | struct GNUNET_DATASTORE_PluginFunctions *api = cls; |
1127 | struct Plugin *plugin = api->cls; | 1048 | struct Plugin *plugin = api->cls; |
1128 | 1049 | ||
1129 | PQfinish (plugin->dbh); | 1050 | PQfinish (plugin->dbh); |
1130 | GNUNET_free (plugin); | 1051 | GNUNET_free (plugin); |
1131 | GNUNET_free (api); | 1052 | GNUNET_free (api); |