diff options
Diffstat (limited to 'src/datacache/plugin_datacache_postgres.c')
-rw-r--r-- | src/datacache/plugin_datacache_postgres.c | 416 |
1 files changed, 195 insertions, 221 deletions
diff --git a/src/datacache/plugin_datacache_postgres.c b/src/datacache/plugin_datacache_postgres.c index e391469fd..fdc377b8f 100644 --- a/src/datacache/plugin_datacache_postgres.c +++ b/src/datacache/plugin_datacache_postgres.c | |||
@@ -38,7 +38,7 @@ | |||
38 | /** | 38 | /** |
39 | * Context for all functions in this plugin. | 39 | * Context for all functions in this plugin. |
40 | */ | 40 | */ |
41 | struct Plugin | 41 | struct Plugin |
42 | { | 42 | { |
43 | /** | 43 | /** |
44 | * Our execution environment. | 44 | * Our execution environment. |
@@ -62,27 +62,28 @@ struct Plugin | |||
62 | */ | 62 | */ |
63 | static int | 63 | static int |
64 | check_result (struct Plugin *plugin, | 64 | check_result (struct Plugin *plugin, |
65 | PGresult * ret, | 65 | PGresult * ret, |
66 | int expected_status, | 66 | int expected_status, |
67 | const char *command, const char *args, int line) | 67 | const char *command, const char *args, int line) |
68 | { | 68 | { |
69 | if (ret == NULL) | 69 | if (ret == NULL) |
70 | { | 70 | { |
71 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 71 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
72 | "datastore-postgres", | 72 | "datastore-postgres", |
73 | "Postgres failed to allocate result for `%s:%s' at %d\n", | 73 | "Postgres failed to allocate result for `%s:%s' at %d\n", |
74 | command, args, line); | 74 | command, args, line); |
75 | return GNUNET_SYSERR; | 75 | return GNUNET_SYSERR; |
76 | } | 76 | } |
77 | if (PQresultStatus (ret) != expected_status) | 77 | if (PQresultStatus (ret) != expected_status) |
78 | { | 78 | { |
79 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 79 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
80 | "datastore-postgres", | 80 | "datastore-postgres", |
81 | _("`%s:%s' failed at %s:%d with error: %s"), | 81 | _("`%s:%s' failed at %s:%d with error: %s"), |
82 | command, args, __FILE__, line, PQerrorMessage (plugin->dbh)); | 82 | command, args, __FILE__, line, |
83 | PQclear (ret); | 83 | PQerrorMessage (plugin->dbh)); |
84 | return GNUNET_SYSERR; | 84 | PQclear (ret); |
85 | } | 85 | return GNUNET_SYSERR; |
86 | } | ||
86 | return GNUNET_OK; | 87 | return GNUNET_OK; |
87 | } | 88 | } |
88 | 89 | ||
@@ -91,14 +92,13 @@ check_result (struct Plugin *plugin, | |||
91 | * Run simple SQL statement (without results). | 92 | * Run simple SQL statement (without results). |
92 | */ | 93 | */ |
93 | static int | 94 | static int |
94 | pq_exec (struct Plugin *plugin, | 95 | pq_exec (struct Plugin *plugin, const char *sql, int line) |
95 | const char *sql, int line) | ||
96 | { | 96 | { |
97 | PGresult *ret; | 97 | PGresult *ret; |
98 | |||
98 | ret = PQexec (plugin->dbh, sql); | 99 | ret = PQexec (plugin->dbh, sql); |
99 | if (GNUNET_OK != check_result (plugin, | 100 | if (GNUNET_OK != check_result (plugin, |
100 | ret, | 101 | ret, PGRES_COMMAND_OK, "PQexec", sql, line)) |
101 | PGRES_COMMAND_OK, "PQexec", sql, line)) | ||
102 | return GNUNET_SYSERR; | 102 | return GNUNET_SYSERR; |
103 | PQclear (ret); | 103 | PQclear (ret); |
104 | return GNUNET_OK; | 104 | return GNUNET_OK; |
@@ -110,13 +110,13 @@ pq_exec (struct Plugin *plugin, | |||
110 | */ | 110 | */ |
111 | static int | 111 | static int |
112 | pq_prepare (struct Plugin *plugin, | 112 | pq_prepare (struct Plugin *plugin, |
113 | const char *name, const char *sql, int nparms, int line) | 113 | const char *name, const char *sql, int nparms, int line) |
114 | { | 114 | { |
115 | PGresult *ret; | 115 | PGresult *ret; |
116 | |||
116 | ret = PQprepare (plugin->dbh, name, sql, nparms, NULL); | 117 | ret = PQprepare (plugin->dbh, name, sql, nparms, NULL); |
117 | if (GNUNET_OK != | 118 | if (GNUNET_OK != |
118 | check_result (plugin, | 119 | check_result (plugin, ret, PGRES_COMMAND_OK, "PQprepare", sql, line)) |
119 | ret, PGRES_COMMAND_OK, "PQprepare", sql, line)) | ||
120 | return GNUNET_SYSERR; | 120 | return GNUNET_SYSERR; |
121 | PQclear (ret); | 121 | PQclear (ret); |
122 | return GNUNET_OK; | 122 | return GNUNET_OK; |
@@ -134,128 +134,121 @@ init_connection (struct Plugin *plugin) | |||
134 | PGresult *ret; | 134 | PGresult *ret; |
135 | 135 | ||
136 | /* Open database and precompile statements */ | 136 | /* Open database and precompile statements */ |
137 | if (GNUNET_OK != | 137 | if (GNUNET_OK != |
138 | GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, | 138 | GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, |
139 | "datacache-postgres", | 139 | "datacache-postgres", |
140 | "CONFIG", | 140 | "CONFIG", &conninfo)) |
141 | &conninfo)) | ||
142 | conninfo = NULL; | 141 | conninfo = NULL; |
143 | plugin->dbh = PQconnectdb (conninfo == NULL ? "" : conninfo); | 142 | plugin->dbh = PQconnectdb (conninfo == NULL ? "" : conninfo); |
144 | GNUNET_free_non_null (conninfo); | 143 | GNUNET_free_non_null (conninfo); |
145 | if (NULL == plugin->dbh) | 144 | if (NULL == plugin->dbh) |
146 | { | 145 | { |
147 | /* FIXME: warn about out-of-memory? */ | 146 | /* FIXME: warn about out-of-memory? */ |
148 | return GNUNET_SYSERR; | 147 | return GNUNET_SYSERR; |
149 | } | 148 | } |
150 | if (PQstatus (plugin->dbh) != CONNECTION_OK) | 149 | if (PQstatus (plugin->dbh) != CONNECTION_OK) |
151 | { | 150 | { |
152 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | 151 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, |
153 | "datacache-postgres", | 152 | "datacache-postgres", |
154 | _("Unable to initialize Postgres: %s"), | 153 | _("Unable to initialize Postgres: %s"), |
155 | PQerrorMessage (plugin->dbh)); | 154 | PQerrorMessage (plugin->dbh)); |
156 | PQfinish (plugin->dbh); | 155 | PQfinish (plugin->dbh); |
157 | plugin->dbh = NULL; | 156 | plugin->dbh = NULL; |
158 | return GNUNET_SYSERR; | 157 | return GNUNET_SYSERR; |
159 | } | 158 | } |
160 | ret = PQexec (plugin->dbh, | 159 | ret = PQexec (plugin->dbh, |
161 | "CREATE TEMPORARY TABLE gn090dc (" | 160 | "CREATE TEMPORARY TABLE gn090dc (" |
162 | " type INTEGER NOT NULL DEFAULT 0," | 161 | " type INTEGER NOT NULL DEFAULT 0," |
163 | " discard_time BIGINT NOT NULL DEFAULT 0," | 162 | " discard_time BIGINT NOT NULL DEFAULT 0," |
164 | " key BYTEA NOT NULL DEFAULT ''," | 163 | " key BYTEA NOT NULL DEFAULT ''," |
165 | " value BYTEA NOT NULL DEFAULT '')" "WITH OIDS"); | 164 | " value BYTEA NOT NULL DEFAULT '')" "WITH OIDS"); |
166 | if ( (ret == NULL) || | 165 | if ((ret == NULL) || ((PQresultStatus (ret) != PGRES_COMMAND_OK) && (0 != strcmp ("42P07", /* duplicate table */ |
167 | ( (PQresultStatus (ret) != PGRES_COMMAND_OK) && | 166 | PQresultErrorField |
168 | (0 != strcmp ("42P07", /* duplicate table */ | 167 | (ret, |
169 | PQresultErrorField | 168 | PG_DIAG_SQLSTATE))))) |
170 | (ret, | 169 | { |
171 | PG_DIAG_SQLSTATE))))) | 170 | (void) check_result (plugin, |
171 | ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090dc", | ||
172 | __LINE__); | ||
173 | PQfinish (plugin->dbh); | ||
174 | plugin->dbh = NULL; | ||
175 | return GNUNET_SYSERR; | ||
176 | } | ||
177 | if (PQresultStatus (ret) == PGRES_COMMAND_OK) | ||
178 | { | ||
179 | if ((GNUNET_OK != | ||
180 | pq_exec (plugin, "CREATE INDEX idx_key ON gn090dc (key)", __LINE__)) || | ||
181 | (GNUNET_OK != | ||
182 | pq_exec (plugin, "CREATE INDEX idx_dt ON gn090dc (discard_time)", | ||
183 | __LINE__))) | ||
172 | { | 184 | { |
173 | (void) check_result (plugin, | 185 | PQclear (ret); |
174 | ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090dc", __LINE__); | ||
175 | PQfinish (plugin->dbh); | 186 | PQfinish (plugin->dbh); |
176 | plugin->dbh = NULL; | 187 | plugin->dbh = NULL; |
177 | return GNUNET_SYSERR; | 188 | return GNUNET_SYSERR; |
178 | } | 189 | } |
179 | if (PQresultStatus (ret) == PGRES_COMMAND_OK) | 190 | } |
180 | { | ||
181 | if ((GNUNET_OK != | ||
182 | pq_exec (plugin, "CREATE INDEX idx_key ON gn090dc (key)", __LINE__)) || | ||
183 | (GNUNET_OK != | ||
184 | pq_exec (plugin, "CREATE INDEX idx_dt ON gn090dc (discard_time)", | ||
185 | __LINE__)) ) | ||
186 | { | ||
187 | PQclear (ret); | ||
188 | PQfinish (plugin->dbh); | ||
189 | plugin->dbh = NULL; | ||
190 | return GNUNET_SYSERR; | ||
191 | } | ||
192 | } | ||
193 | PQclear (ret); | 191 | PQclear (ret); |
194 | #if 1 | 192 | #if 1 |
195 | ret = PQexec (plugin->dbh, | 193 | ret = PQexec (plugin->dbh, |
196 | "ALTER TABLE gn090dc ALTER value SET STORAGE EXTERNAL"); | 194 | "ALTER TABLE gn090dc ALTER value SET STORAGE EXTERNAL"); |
197 | if (GNUNET_OK != | 195 | if (GNUNET_OK != |
198 | check_result (plugin, | 196 | check_result (plugin, |
199 | ret, PGRES_COMMAND_OK, | 197 | ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090dc", __LINE__)) |
200 | "ALTER TABLE", "gn090dc", __LINE__)) | 198 | { |
201 | { | 199 | PQfinish (plugin->dbh); |
202 | PQfinish (plugin->dbh); | 200 | plugin->dbh = NULL; |
203 | plugin->dbh = NULL; | 201 | return GNUNET_SYSERR; |
204 | return GNUNET_SYSERR; | 202 | } |
205 | } | ||
206 | PQclear (ret); | 203 | PQclear (ret); |
207 | ret = PQexec (plugin->dbh, | 204 | ret = PQexec (plugin->dbh, "ALTER TABLE gn090dc ALTER key SET STORAGE PLAIN"); |
208 | "ALTER TABLE gn090dc ALTER key SET STORAGE PLAIN"); | ||
209 | if (GNUNET_OK != | 205 | if (GNUNET_OK != |
210 | check_result (plugin, | 206 | check_result (plugin, |
211 | ret, PGRES_COMMAND_OK, | 207 | ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090dc", __LINE__)) |
212 | "ALTER TABLE", "gn090dc", __LINE__)) | 208 | { |
213 | { | 209 | PQfinish (plugin->dbh); |
214 | PQfinish (plugin->dbh); | 210 | plugin->dbh = NULL; |
215 | plugin->dbh = NULL; | 211 | return GNUNET_SYSERR; |
216 | return GNUNET_SYSERR; | 212 | } |
217 | } | ||
218 | PQclear (ret); | 213 | PQclear (ret); |
219 | #endif | 214 | #endif |
220 | if ((GNUNET_OK != | 215 | if ((GNUNET_OK != |
221 | pq_prepare (plugin, | 216 | pq_prepare (plugin, |
222 | "getkt", | 217 | "getkt", |
223 | "SELECT discard_time,type,value FROM gn090dc " | 218 | "SELECT discard_time,type,value FROM gn090dc " |
224 | "WHERE key=$1 AND type=$2 ", | 219 | "WHERE key=$1 AND type=$2 ", |
225 | 2, | 220 | 2, |
226 | __LINE__)) || | 221 | __LINE__)) || |
227 | (GNUNET_OK != | 222 | (GNUNET_OK != |
228 | pq_prepare (plugin, | 223 | pq_prepare (plugin, |
229 | "getk", | 224 | "getk", |
230 | "SELECT discard_time,type,value FROM gn090dc " | 225 | "SELECT discard_time,type,value FROM gn090dc " |
231 | "WHERE key=$1", | 226 | "WHERE key=$1", |
232 | 1, | 227 | 1, |
233 | __LINE__)) || | 228 | __LINE__)) || |
234 | (GNUNET_OK != | 229 | (GNUNET_OK != |
235 | pq_prepare (plugin, | 230 | pq_prepare (plugin, |
236 | "getm", | 231 | "getm", |
237 | "SELECT length(value),oid,key FROM gn090dc " | 232 | "SELECT length(value),oid,key FROM gn090dc " |
238 | "ORDER BY discard_time ASC LIMIT 1", | 233 | "ORDER BY discard_time ASC LIMIT 1", |
239 | 0, | 234 | 0, |
240 | __LINE__)) || | 235 | __LINE__)) || |
241 | (GNUNET_OK != | 236 | (GNUNET_OK != |
242 | pq_prepare (plugin, | 237 | pq_prepare (plugin, |
243 | "delrow", | 238 | "delrow", |
244 | "DELETE FROM gn090dc WHERE oid=$1", | 239 | "DELETE FROM gn090dc WHERE oid=$1", |
245 | 1, | 240 | 1, |
246 | __LINE__)) || | 241 | __LINE__)) || |
247 | (GNUNET_OK != | 242 | (GNUNET_OK != |
248 | pq_prepare (plugin, | 243 | pq_prepare (plugin, |
249 | "put", | 244 | "put", |
250 | "INSERT INTO gn090dc (type, discard_time, key, value) " | 245 | "INSERT INTO gn090dc (type, discard_time, key, value) " |
251 | "VALUES ($1, $2, $3, $4)", | 246 | "VALUES ($1, $2, $3, $4)", 4, __LINE__))) |
252 | 4, | 247 | { |
253 | __LINE__)) ) | 248 | PQfinish (plugin->dbh); |
254 | { | 249 | plugin->dbh = NULL; |
255 | PQfinish (plugin->dbh); | 250 | return GNUNET_SYSERR; |
256 | plugin->dbh = NULL; | 251 | } |
257 | return GNUNET_SYSERR; | ||
258 | } | ||
259 | return GNUNET_OK; | 252 | return GNUNET_OK; |
260 | } | 253 | } |
261 | 254 | ||
@@ -267,8 +260,7 @@ init_connection (struct Plugin *plugin) | |||
267 | * @return GNUNET_OK on success | 260 | * @return GNUNET_OK on success |
268 | */ | 261 | */ |
269 | static int | 262 | static int |
270 | delete_by_rowid (struct Plugin *plugin, | 263 | delete_by_rowid (struct Plugin *plugin, uint32_t rowid) |
271 | uint32_t rowid) | ||
272 | { | 264 | { |
273 | uint32_t brow = htonl (rowid); | 265 | uint32_t brow = htonl (rowid); |
274 | const char *paramValues[] = { (const char *) &brow }; | 266 | const char *paramValues[] = { (const char *) &brow }; |
@@ -281,11 +273,11 @@ delete_by_rowid (struct Plugin *plugin, | |||
281 | 1, paramValues, paramLengths, paramFormats, 1); | 273 | 1, paramValues, paramLengths, paramFormats, 1); |
282 | if (GNUNET_OK != | 274 | if (GNUNET_OK != |
283 | check_result (plugin, | 275 | check_result (plugin, |
284 | ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow", | 276 | ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow", |
285 | __LINE__)) | 277 | __LINE__)) |
286 | { | 278 | { |
287 | return GNUNET_SYSERR; | 279 | return GNUNET_SYSERR; |
288 | } | 280 | } |
289 | PQclear (ret); | 281 | PQclear (ret); |
290 | return GNUNET_OK; | 282 | return GNUNET_OK; |
291 | } | 283 | } |
@@ -302,18 +294,19 @@ delete_by_rowid (struct Plugin *plugin, | |||
302 | * @param discard_time when to discard the value in any case | 294 | * @param discard_time when to discard the value in any case |
303 | * @return 0 on error, number of bytes used otherwise | 295 | * @return 0 on error, number of bytes used otherwise |
304 | */ | 296 | */ |
305 | static size_t | 297 | static size_t |
306 | postgres_plugin_put (void *cls, | 298 | postgres_plugin_put (void *cls, |
307 | const GNUNET_HashCode * key, | 299 | const GNUNET_HashCode * key, |
308 | size_t size, | 300 | size_t size, |
309 | const char *data, | 301 | const char *data, |
310 | enum GNUNET_BLOCK_Type type, | 302 | enum GNUNET_BLOCK_Type type, |
311 | struct GNUNET_TIME_Absolute discard_time) | 303 | struct GNUNET_TIME_Absolute discard_time) |
312 | { | 304 | { |
313 | struct Plugin *plugin = cls; | 305 | struct Plugin *plugin = cls; |
314 | PGresult *ret; | 306 | PGresult *ret; |
315 | uint32_t btype = htonl (type); | 307 | uint32_t btype = htonl (type); |
316 | uint64_t bexpi = GNUNET_TIME_absolute_hton (discard_time).abs_value__; | 308 | uint64_t bexpi = GNUNET_TIME_absolute_hton (discard_time).abs_value__; |
309 | |||
317 | const char *paramValues[] = { | 310 | const char *paramValues[] = { |
318 | (const char *) &btype, | 311 | (const char *) &btype, |
319 | (const char *) &bexpi, | 312 | (const char *) &bexpi, |
@@ -350,15 +343,15 @@ postgres_plugin_put (void *cls, | |||
350 | * @param iter_cls closure for iter | 343 | * @param iter_cls closure for iter |
351 | * @return the number of results found | 344 | * @return the number of results found |
352 | */ | 345 | */ |
353 | static unsigned int | 346 | static unsigned int |
354 | postgres_plugin_get (void *cls, | 347 | postgres_plugin_get (void *cls, |
355 | const GNUNET_HashCode * key, | 348 | const GNUNET_HashCode * key, |
356 | enum GNUNET_BLOCK_Type type, | 349 | enum GNUNET_BLOCK_Type type, |
357 | GNUNET_DATACACHE_Iterator iter, | 350 | GNUNET_DATACACHE_Iterator iter, void *iter_cls) |
358 | void *iter_cls) | ||
359 | { | 351 | { |
360 | struct Plugin *plugin = cls; | 352 | struct Plugin *plugin = cls; |
361 | uint32_t btype = htonl (type); | 353 | uint32_t btype = htonl (type); |
354 | |||
362 | const char *paramValues[] = { | 355 | const char *paramValues[] = { |
363 | (const char *) key, | 356 | (const char *) key, |
364 | (const char *) &btype, | 357 | (const char *) &btype, |
@@ -376,80 +369,73 @@ postgres_plugin_get (void *cls, | |||
376 | 369 | ||
377 | cnt = 0; | 370 | cnt = 0; |
378 | res = PQexecPrepared (plugin->dbh, | 371 | res = PQexecPrepared (plugin->dbh, |
379 | (type == 0) ? "getk" : "getkt", | 372 | (type == 0) ? "getk" : "getkt", |
380 | (type == 0) ? 1 : 2, | 373 | (type == 0) ? 1 : 2, |
381 | paramValues, | 374 | paramValues, paramLengths, paramFormats, 1); |
382 | paramLengths, | ||
383 | paramFormats, | ||
384 | 1); | ||
385 | if (GNUNET_OK != check_result (plugin, | 375 | if (GNUNET_OK != check_result (plugin, |
386 | res, | 376 | res, |
387 | PGRES_TUPLES_OK, | 377 | PGRES_TUPLES_OK, |
388 | "PQexecPrepared", | 378 | "PQexecPrepared", |
389 | (type == 0) ? "getk" : "getkt", | 379 | (type == 0) ? "getk" : "getkt", __LINE__)) |
390 | __LINE__)) | 380 | { |
391 | { | ||
392 | #if DEBUG_POSTGRES | 381 | #if DEBUG_POSTGRES |
393 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 382 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
394 | "datacache-postgres", | 383 | "datacache-postgres", |
395 | "Ending iteration (postgres error)\n"); | 384 | "Ending iteration (postgres error)\n"); |
396 | #endif | 385 | #endif |
397 | return 0; | 386 | return 0; |
398 | } | 387 | } |
399 | 388 | ||
400 | if (0 == (cnt = PQntuples (res))) | 389 | if (0 == (cnt = PQntuples (res))) |
401 | { | 390 | { |
402 | /* no result */ | 391 | /* no result */ |
403 | #if DEBUG_POSTGRES | 392 | #if DEBUG_POSTGRES |
404 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 393 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
405 | "datacache-postgres", | 394 | "datacache-postgres", |
406 | "Ending iteration (no more results)\n"); | 395 | "Ending iteration (no more results)\n"); |
407 | #endif | 396 | #endif |
408 | PQclear (res); | 397 | PQclear (res); |
409 | return 0; | 398 | return 0; |
410 | } | 399 | } |
411 | if (iter == NULL) | 400 | if (iter == NULL) |
412 | { | 401 | { |
413 | PQclear (res); | 402 | PQclear (res); |
414 | return cnt; | 403 | return cnt; |
415 | } | 404 | } |
416 | if ( (3 != PQnfields (res)) || | 405 | if ((3 != PQnfields (res)) || |
417 | (sizeof (uint64_t) != PQfsize (res, 0)) || | 406 | (sizeof (uint64_t) != PQfsize (res, 0)) || |
418 | (sizeof (uint32_t) != PQfsize (res, 1))) | 407 | (sizeof (uint32_t) != PQfsize (res, 1))) |
419 | { | 408 | { |
420 | GNUNET_break (0); | 409 | GNUNET_break (0); |
421 | PQclear (res); | 410 | PQclear (res); |
422 | return 0; | 411 | return 0; |
423 | } | 412 | } |
424 | for (i=0;i<cnt;i++) | 413 | for (i = 0; i < cnt; i++) |
425 | { | 414 | { |
426 | expiration_time.abs_value = GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, i, 0)); | 415 | expiration_time.abs_value = |
427 | type = ntohl (*(uint32_t *) PQgetvalue (res, i, 1)); | 416 | GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, i, 0)); |
428 | size = PQgetlength (res, i, 2); | 417 | type = ntohl (*(uint32_t *) PQgetvalue (res, i, 1)); |
418 | size = PQgetlength (res, i, 2); | ||
429 | #if DEBUG_POSTGRES | 419 | #if DEBUG_POSTGRES |
430 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 420 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
431 | "datacache-postgres", | 421 | "datacache-postgres", |
432 | "Found result of size %u bytes and type %u in database\n", | 422 | "Found result of size %u bytes and type %u in database\n", |
433 | (unsigned int) size, | 423 | (unsigned int) size, (unsigned int) type); |
434 | (unsigned int) type); | ||
435 | #endif | 424 | #endif |
436 | if (GNUNET_SYSERR == | 425 | if (GNUNET_SYSERR == |
437 | iter (iter_cls, | 426 | iter (iter_cls, |
438 | expiration_time, | 427 | expiration_time, |
439 | key, | 428 | key, size, PQgetvalue (res, i, 2), (enum GNUNET_BLOCK_Type) type)) |
440 | size, | 429 | { |
441 | PQgetvalue (res, i, 2), | ||
442 | (enum GNUNET_BLOCK_Type) type)) | ||
443 | { | ||
444 | #if DEBUG_POSTGRES | 430 | #if DEBUG_POSTGRES |
445 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 431 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
446 | "datacache-postgres", | 432 | "datacache-postgres", |
447 | "Ending iteration (client error)\n"); | 433 | "Ending iteration (client error)\n"); |
448 | #endif | 434 | #endif |
449 | PQclear (res); | 435 | PQclear (res); |
450 | return cnt; | 436 | return cnt; |
451 | } | 437 | } |
452 | } | 438 | } |
453 | PQclear (res); | 439 | PQclear (res); |
454 | return cnt; | 440 | return cnt; |
455 | } | 441 | } |
@@ -461,8 +447,8 @@ postgres_plugin_get (void *cls, | |||
461 | * | 447 | * |
462 | * @param cls closure (our "struct Plugin") | 448 | * @param cls closure (our "struct Plugin") |
463 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | 449 | * @return GNUNET_OK on success, GNUNET_SYSERR on error |
464 | */ | 450 | */ |
465 | static int | 451 | static int |
466 | postgres_plugin_del (void *cls) | 452 | postgres_plugin_del (void *cls) |
467 | { | 453 | { |
468 | struct Plugin *plugin = cls; | 454 | struct Plugin *plugin = cls; |
@@ -471,55 +457,46 @@ postgres_plugin_del (void *cls) | |||
471 | GNUNET_HashCode key; | 457 | GNUNET_HashCode key; |
472 | PGresult *res; | 458 | PGresult *res; |
473 | 459 | ||
474 | res = PQexecPrepared (plugin->dbh, | 460 | res = PQexecPrepared (plugin->dbh, "getm", 0, NULL, NULL, NULL, 1); |
475 | "getm", | ||
476 | 0, NULL, NULL, NULL, | ||
477 | 1); | ||
478 | if (GNUNET_OK != check_result (plugin, | 461 | if (GNUNET_OK != check_result (plugin, |
479 | res, | 462 | res, |
480 | PGRES_TUPLES_OK, | 463 | PGRES_TUPLES_OK, |
481 | "PQexecPrepared", | 464 | "PQexecPrepared", "getm", __LINE__)) |
482 | "getm", | 465 | { |
483 | __LINE__)) | ||
484 | { | ||
485 | #if DEBUG_POSTGRES | 466 | #if DEBUG_POSTGRES |
486 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 467 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
487 | "datacache-postgres", | 468 | "datacache-postgres", |
488 | "Ending iteration (postgres error)\n"); | 469 | "Ending iteration (postgres error)\n"); |
489 | #endif | 470 | #endif |
490 | return 0; | 471 | return 0; |
491 | } | 472 | } |
492 | if (0 == PQntuples (res)) | 473 | if (0 == PQntuples (res)) |
493 | { | 474 | { |
494 | /* no result */ | 475 | /* no result */ |
495 | #if DEBUG_POSTGRES | 476 | #if DEBUG_POSTGRES |
496 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 477 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
497 | "datacache-postgres", | 478 | "datacache-postgres", |
498 | "Ending iteration (no more results)\n"); | 479 | "Ending iteration (no more results)\n"); |
499 | #endif | 480 | #endif |
500 | PQclear (res); | 481 | PQclear (res); |
501 | return GNUNET_SYSERR; | 482 | return GNUNET_SYSERR; |
502 | } | 483 | } |
503 | if ( (3 != PQnfields (res)) || | 484 | if ((3 != PQnfields (res)) || |
504 | (sizeof (size) != PQfsize (res, 0)) || | 485 | (sizeof (size) != PQfsize (res, 0)) || |
505 | (sizeof (oid) != PQfsize (res, 1)) || | 486 | (sizeof (oid) != PQfsize (res, 1)) || |
506 | (sizeof (GNUNET_HashCode) != PQgetlength (res, 0, 2)) ) | 487 | (sizeof (GNUNET_HashCode) != PQgetlength (res, 0, 2))) |
507 | { | 488 | { |
508 | GNUNET_break (0); | 489 | GNUNET_break (0); |
509 | PQclear (res); | 490 | PQclear (res); |
510 | return 0; | 491 | return 0; |
511 | } | 492 | } |
512 | size = ntohl (*(uint32_t *) PQgetvalue (res, 0, 0)); | 493 | size = ntohl (*(uint32_t *) PQgetvalue (res, 0, 0)); |
513 | oid = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1)); | 494 | oid = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1)); |
514 | memcpy (&key, | 495 | memcpy (&key, PQgetvalue (res, 0, 2), sizeof (GNUNET_HashCode)); |
515 | PQgetvalue (res, 0, 2), | ||
516 | sizeof (GNUNET_HashCode)); | ||
517 | PQclear (res); | 496 | PQclear (res); |
518 | if (GNUNET_OK != delete_by_rowid (plugin, oid)) | 497 | if (GNUNET_OK != delete_by_rowid (plugin, oid)) |
519 | return GNUNET_SYSERR; | 498 | return GNUNET_SYSERR; |
520 | plugin->env->delete_notify (plugin->env->cls, | 499 | plugin->env->delete_notify (plugin->env->cls, &key, size + OVERHEAD); |
521 | &key, | ||
522 | size + OVERHEAD); | ||
523 | return GNUNET_OK; | 500 | return GNUNET_OK; |
524 | } | 501 | } |
525 | 502 | ||
@@ -540,12 +517,11 @@ libgnunet_plugin_datacache_postgres_init (void *cls) | |||
540 | plugin = GNUNET_malloc (sizeof (struct Plugin)); | 517 | plugin = GNUNET_malloc (sizeof (struct Plugin)); |
541 | plugin->env = env; | 518 | plugin->env = env; |
542 | 519 | ||
543 | if (GNUNET_OK != | 520 | if (GNUNET_OK != init_connection (plugin)) |
544 | init_connection (plugin)) | 521 | { |
545 | { | 522 | GNUNET_free (plugin); |
546 | GNUNET_free (plugin); | 523 | return NULL; |
547 | return NULL; | 524 | } |
548 | } | ||
549 | 525 | ||
550 | api = GNUNET_malloc (sizeof (struct GNUNET_DATACACHE_PluginFunctions)); | 526 | api = GNUNET_malloc (sizeof (struct GNUNET_DATACACHE_PluginFunctions)); |
551 | api->cls = plugin; | 527 | api->cls = plugin; |
@@ -553,8 +529,7 @@ libgnunet_plugin_datacache_postgres_init (void *cls) | |||
553 | api->put = &postgres_plugin_put; | 529 | api->put = &postgres_plugin_put; |
554 | api->del = &postgres_plugin_del; | 530 | api->del = &postgres_plugin_del; |
555 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, | 531 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, |
556 | "datacache-postgres", | 532 | "datacache-postgres", _("Postgres datacache running\n")); |
557 | _("Postgres datacache running\n")); | ||
558 | return api; | 533 | return api; |
559 | } | 534 | } |
560 | 535 | ||
@@ -580,4 +555,3 @@ libgnunet_plugin_datacache_postgres_done (void *cls) | |||
580 | 555 | ||
581 | 556 | ||
582 | /* end of plugin_datacache_postgres.c */ | 557 | /* end of plugin_datacache_postgres.c */ |
583 | |||