aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/plugin_namestore_postgres.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-10-16 21:52:04 +0000
committerChristian Grothoff <christian@grothoff.org>2013-10-16 21:52:04 +0000
commitdbc823a07a03e1085172038125b0edf15b0dc6fe (patch)
tree4e7c3bf60ce83e801c69a6566ea36d514b571cba /src/namestore/plugin_namestore_postgres.c
parenteec0e5088ec9437f5c0cf9d3ffef87603ad2777a (diff)
downloadgnunet-dbc823a07a03e1085172038125b0edf15b0dc6fe.tar.gz
gnunet-dbc823a07a03e1085172038125b0edf15b0dc6fe.zip
-finishing split of namestore into namestore and namecache (#3065) -- in theory; in practice, somehow something broke badly, so the tests are now failing
Diffstat (limited to 'src/namestore/plugin_namestore_postgres.c')
-rw-r--r--src/namestore/plugin_namestore_postgres.c265
1 files changed, 1 insertions, 264 deletions
diff --git a/src/namestore/plugin_namestore_postgres.c b/src/namestore/plugin_namestore_postgres.c
index e5cb75eb4..e84413400 100644
--- a/src/namestore/plugin_namestore_postgres.c
+++ b/src/namestore/plugin_namestore_postgres.c
@@ -81,12 +81,6 @@ create_indices (PGconn * dbh)
81 /* create indices */ 81 /* create indices */
82 if ( (GNUNET_OK != 82 if ( (GNUNET_OK !=
83 GNUNET_POSTGRES_exec (dbh, 83 GNUNET_POSTGRES_exec (dbh,
84 "CREATE INDEX ir_query_hash ON ns096blocks (query,expiration_time)")) ||
85 (GNUNET_OK !=
86 GNUNET_POSTGRES_exec (dbh,
87 "CREATE INDEX ir_block_expiration ON ns096blocks (expiration_time)")) ||
88 (GNUNET_OK !=
89 GNUNET_POSTGRES_exec (dbh,
90 "CREATE INDEX ir_pkey_reverse ON ns097records (zone_private_key,pkey)")) || 84 "CREATE INDEX ir_pkey_reverse ON ns097records (zone_private_key,pkey)")) ||
91 (GNUNET_OK != 85 (GNUNET_OK !=
92 GNUNET_POSTGRES_exec (dbh, 86 GNUNET_POSTGRES_exec (dbh,
@@ -159,69 +153,10 @@ database_setup (struct Plugin *plugin)
159 plugin->dbh = NULL; 153 plugin->dbh = NULL;
160 return GNUNET_SYSERR; 154 return GNUNET_SYSERR;
161 } 155 }
162 156 create_indices (plugin->dbh);
163
164 if (GNUNET_YES ==
165 GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg,
166 "namestore-postgres",
167 "TEMPORARY_TABLE"))
168 {
169 res =
170 PQexec (plugin->dbh,
171 "CREATE TEMPORARY TABLE ns096blocks ("
172 " query BYTEA NOT NULL DEFAULT '',"
173 " block BYTEA NOT NULL DEFAULT '',"
174 " expiration_time BIGINT NOT NULL DEFAULT 0"
175 ")" "WITH OIDS");
176 }
177 else
178 {
179 res =
180 PQexec (plugin->dbh,
181 "CREATE TABLE ns096blocks ("
182 " query BYTEA NOT NULL DEFAULT '',"
183 " block BYTEA NOT NULL DEFAULT '',"
184 " expiration_time BIGINT NOT NULL DEFAULT 0"
185 ")" "WITH OIDS");
186 }
187 if ( (NULL == res) ||
188 ((PQresultStatus (res) != PGRES_COMMAND_OK) &&
189 (0 != strcmp ("42P07", /* duplicate table */
190 PQresultErrorField
191 (res,
192 PG_DIAG_SQLSTATE)))))
193 {
194 (void) GNUNET_POSTGRES_check_result (plugin->dbh, res,
195 PGRES_COMMAND_OK, "CREATE TABLE",
196 "ns096blocks");
197 PQfinish (plugin->dbh);
198 plugin->dbh = NULL;
199 return GNUNET_SYSERR;
200 }
201 if (PQresultStatus (res) == PGRES_COMMAND_OK)
202 create_indices (plugin->dbh);
203 PQclear (res);
204 157
205 if ((GNUNET_OK != 158 if ((GNUNET_OK !=
206 GNUNET_POSTGRES_prepare (plugin->dbh, 159 GNUNET_POSTGRES_prepare (plugin->dbh,
207 "cache_block",
208 "INSERT INTO ns096blocks (query, block, expiration_time) VALUES "
209 "($1, $2, $3)", 3)) ||
210 (GNUNET_OK !=
211 GNUNET_POSTGRES_prepare (plugin->dbh,
212 "expire_blocks",
213 "DELETE FROM ns096blocks WHERE expiration_time<$1", 1)) ||
214 (GNUNET_OK !=
215 GNUNET_POSTGRES_prepare (plugin->dbh,
216 "delete_block",
217 "DELETE FROM ns096blocks WHERE query=$1 AND expiration_time<=$2", 2)) ||
218 (GNUNET_OK !=
219 GNUNET_POSTGRES_prepare (plugin->dbh,
220 "lookup_block",
221 "SELECT block FROM ns096blocks WHERE query=$1"
222 " ORDER BY expiration_time DESC LIMIT 1", 1)) ||
223 (GNUNET_OK !=
224 GNUNET_POSTGRES_prepare (plugin->dbh,
225 "store_records", 160 "store_records",
226 "INSERT INTO ns097records (zone_private_key, pkey, rvalue, record_count, record_data, label) VALUES " 161 "INSERT INTO ns097records (zone_private_key, pkey, rvalue, record_count, record_data, label) VALUES "
227 "($1, $2, $3, $4, $5, $6)", 6)) || 162 "($1, $2, $3, $4, $5, $6)", 6)) ||
@@ -254,202 +189,6 @@ database_setup (struct Plugin *plugin)
254 189
255 190
256/** 191/**
257 * Removes any expired block.
258 *
259 * @param plugin the plugin
260 */
261static void
262namestore_postgres_expire_blocks (struct Plugin *plugin)
263{
264 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
265 struct GNUNET_TIME_AbsoluteNBO now_be = GNUNET_TIME_absolute_hton (now);
266 const char *paramValues[] = {
267 (const char *) &now_be
268 };
269 int paramLengths[] = {
270 sizeof (now_be)
271 };
272 const int paramFormats[] = { 1 };
273 PGresult *res;
274
275 res =
276 PQexecPrepared (plugin->dbh, "expire_blocks", 1,
277 paramValues, paramLengths, paramFormats, 1);
278 if (GNUNET_OK !=
279 GNUNET_POSTGRES_check_result (plugin->dbh,
280 res,
281 PGRES_COMMAND_OK,
282 "PQexecPrepared",
283 "expire_blocks"))
284 return;
285 PQclear (res);
286}
287
288
289/**
290 * Delete older block in the datastore.
291 *
292 * @param the plugin
293 * @param query query for the block
294 * @param expiration time how old does the block have to be for deletion
295 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
296 */
297static void
298delete_old_block (struct Plugin *plugin,
299 const struct GNUNET_HashCode *query,
300 struct GNUNET_TIME_AbsoluteNBO expiration_time)
301{
302 const char *paramValues[] = {
303 (const char *) query,
304 (const char *) &expiration_time
305 };
306 int paramLengths[] = {
307 sizeof (*query),
308 sizeof (expiration_time)
309 };
310 const int paramFormats[] = { 1, 1 };
311 PGresult *res;
312
313 res =
314 PQexecPrepared (plugin->dbh, "delete_block", 2,
315 paramValues, paramLengths, paramFormats, 1);
316 if (GNUNET_OK !=
317 GNUNET_POSTGRES_check_result (plugin->dbh,
318 res,
319 PGRES_COMMAND_OK,
320 "PQexecPrepared",
321 "delete_block"))
322 return;
323 PQclear (res);
324}
325
326
327/**
328 * Cache a block in the datastore.
329 *
330 * @param cls closure (internal context for the plugin)
331 * @param block block to cache
332 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
333 */
334static int
335namestore_postgres_cache_block (void *cls,
336 const struct GNUNET_GNSRECORD_Block *block)
337{
338 struct Plugin *plugin = cls;
339 struct GNUNET_HashCode query;
340 size_t block_size = ntohl (block->purpose.size) +
341 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
342 sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
343 const char *paramValues[] = {
344 (const char *) &query,
345 (const char *) block,
346 (const char *) &block->expiration_time
347 };
348 int paramLengths[] = {
349 sizeof (query),
350 (int) block_size,
351 sizeof (block->expiration_time)
352 };
353 const int paramFormats[] = { 1, 1, 1 };
354 PGresult *res;
355
356 namestore_postgres_expire_blocks (plugin);
357 GNUNET_CRYPTO_hash (&block->derived_key,
358 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
359 &query);
360 if (block_size > 64 * 65536)
361 {
362 GNUNET_break (0);
363 return GNUNET_SYSERR;
364 }
365 delete_old_block (plugin, &query, block->expiration_time);
366
367 res =
368 PQexecPrepared (plugin->dbh, "cache_block", 3,
369 paramValues, paramLengths, paramFormats, 1);
370 if (GNUNET_OK !=
371 GNUNET_POSTGRES_check_result (plugin->dbh,
372 res,
373 PGRES_COMMAND_OK,
374 "PQexecPrepared",
375 "cache_block"))
376 return GNUNET_SYSERR;
377 PQclear (res);
378 return GNUNET_OK;
379}
380
381
382/**
383 * Get the block for a particular zone and label in the
384 * datastore. Will return at most one result to the iterator.
385 *
386 * @param cls closure (internal context for the plugin)
387 * @param query hash of public key derived from the zone and the label
388 * @param iter function to call with the result
389 * @param iter_cls closure for @a iter
390 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
391 */
392static int
393namestore_postgres_lookup_block (void *cls,
394 const struct GNUNET_HashCode *query,
395 GNUNET_GNSRECORD_BlockCallback iter, void *iter_cls)
396{
397 struct Plugin *plugin = cls;
398 const char *paramValues[] = {
399 (const char *) query
400 };
401 int paramLengths[] = {
402 sizeof (*query)
403 };
404 const int paramFormats[] = { 1 };
405 PGresult *res;
406 unsigned int cnt;
407 size_t bsize;
408 const struct GNUNET_GNSRECORD_Block *block;
409
410 res = PQexecPrepared (plugin->dbh,
411 "lookup_block", 1,
412 paramValues, paramLengths, paramFormats,
413 1);
414 if (GNUNET_OK !=
415 GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_TUPLES_OK,
416 "PQexecPrepared",
417 "lookup_block"))
418 {
419 LOG (GNUNET_ERROR_TYPE_DEBUG,
420 "Failing lookup (postgres error)\n");
421 return GNUNET_SYSERR;
422 }
423 if (0 == (cnt = PQntuples (res)))
424 {
425 /* no result */
426 LOG (GNUNET_ERROR_TYPE_DEBUG,
427 "Ending iteration (no more results)\n");
428 PQclear (res);
429 return GNUNET_NO;
430 }
431 GNUNET_assert (1 == cnt);
432 GNUNET_assert (1 != PQnfields (res));
433 bsize = PQgetlength (res, 0, 0);
434 block = (const struct GNUNET_GNSRECORD_Block *) PQgetvalue (res, 0, 0);
435 if ( (bsize < sizeof (*block)) ||
436 (bsize != ntohl (block->purpose.size) +
437 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
438 sizeof (struct GNUNET_CRYPTO_EcdsaSignature)) )
439 {
440 GNUNET_break (0);
441 LOG (GNUNET_ERROR_TYPE_DEBUG,
442 "Failing lookup (corrupt block)\n");
443 PQclear (res);
444 return GNUNET_SYSERR;
445 }
446 iter (iter_cls, block);
447 PQclear (res);
448 return GNUNET_OK;
449}
450
451
452/**
453 * Store a record in the datastore. Removes any existing record in the 192 * Store a record in the datastore. Removes any existing record in the
454 * same zone with the same name. 193 * same zone with the same name.
455 * 194 *
@@ -773,8 +512,6 @@ libgnunet_plugin_namestore_postgres_init (void *cls)
773 } 512 }
774 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions); 513 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
775 api->cls = &plugin; 514 api->cls = &plugin;
776 api->cache_block = &namestore_postgres_cache_block;
777 api->lookup_block = &namestore_postgres_lookup_block;
778 api->store_records = &namestore_postgres_store_records; 515 api->store_records = &namestore_postgres_store_records;
779 api->iterate_records = &namestore_postgres_iterate_records; 516 api->iterate_records = &namestore_postgres_iterate_records;
780 api->zone_to_name = &namestore_postgres_zone_to_name; 517 api->zone_to_name = &namestore_postgres_zone_to_name;