aboutsummaryrefslogtreecommitdiff
path: root/src/datastore
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-08-15 21:46:35 +0000
committerChristian Grothoff <christian@grothoff.org>2011-08-15 21:46:35 +0000
commit502af2167f7c218366666ca4944bd7cc54b5b19a (patch)
treea91fec5cc9769d260640bd91c6633cb9cf395524 /src/datastore
parent03af5a603b7cc53432249d5854cd412aa90dde0d (diff)
downloadgnunet-502af2167f7c218366666ca4944bd7cc54b5b19a.tar.gz
gnunet-502af2167f7c218366666ca4944bd7cc54b5b19a.zip
indentation
Diffstat (limited to 'src/datastore')
-rw-r--r--src/datastore/datastore.h4
-rw-r--r--src/datastore/datastore_api.c1150
-rw-r--r--src/datastore/gnunet-service-datastore.c1289
-rw-r--r--src/datastore/perf_datastore_api.c340
-rw-r--r--src/datastore/perf_plugin_datastore.c467
-rw-r--r--src/datastore/plugin_datastore_mysql.c1070
-rw-r--r--src/datastore/plugin_datastore_postgres.c791
-rw-r--r--src/datastore/plugin_datastore_sqlite.c1072
-rw-r--r--src/datastore/plugin_datastore_template.c51
-rw-r--r--src/datastore/test_datastore_api.c718
-rw-r--r--src/datastore/test_datastore_api_management.c309
-rw-r--r--src/datastore/test_plugin_datastore.c324
12 files changed, 3514 insertions, 4071 deletions
diff --git a/src/datastore/datastore.h b/src/datastore/datastore.h
index 200473be9..39a680e42 100644
--- a/src/datastore/datastore.h
+++ b/src/datastore/datastore.h
@@ -213,7 +213,7 @@ struct DataMessage
213 * Priority of the item (NBO), zero for remove. 213 * Priority of the item (NBO), zero for remove.
214 */ 214 */
215 uint32_t priority GNUNET_PACKED; 215 uint32_t priority GNUNET_PACKED;
216 216
217 /** 217 /**
218 * Desired anonymity level (NBO), zero for remove. 218 * Desired anonymity level (NBO), zero for remove.
219 */ 219 */
@@ -236,7 +236,7 @@ struct DataMessage
236 * the key and content). 236 * the key and content).
237 */ 237 */
238 uint64_t uid; 238 uint64_t uid;
239 239
240 /** 240 /**
241 * Expiration time (NBO); zero for remove. 241 * Expiration time (NBO); zero for remove.
242 */ 242 */
diff --git a/src/datastore/datastore_api.c b/src/datastore/datastore_api.c
index f355dfab5..85e402a4d 100644
--- a/src/datastore/datastore_api.c
+++ b/src/datastore/datastore_api.c
@@ -82,7 +82,7 @@ union QueueContext
82{ 82{
83 83
84 struct StatusContext sc; 84 struct StatusContext sc;
85 85
86 struct ResultContext rc; 86 struct ResultContext rc;
87 87
88}; 88};
@@ -121,7 +121,7 @@ struct GNUNET_DATASTORE_QueueEntry
121 * Function to call after transmission of the request. 121 * Function to call after transmission of the request.
122 */ 122 */
123 GNUNET_DATASTORE_ContinuationWithStatus cont; 123 GNUNET_DATASTORE_ContinuationWithStatus cont;
124 124
125 /** 125 /**
126 * Closure for 'cont'. 126 * Closure for 'cont'.
127 */ 127 */
@@ -167,7 +167,7 @@ struct GNUNET_DATASTORE_QueueEntry
167 * multiple of 64 bits. 167 * multiple of 64 bits.
168 */ 168 */
169 int was_transmitted; 169 int was_transmitted;
170 170
171}; 171};
172 172
173/** 173/**
@@ -250,22 +250,19 @@ struct GNUNET_DATASTORE_Handle
250 * @return handle to use to access the service 250 * @return handle to use to access the service
251 */ 251 */
252struct GNUNET_DATASTORE_Handle * 252struct GNUNET_DATASTORE_Handle *
253GNUNET_DATASTORE_connect (const struct 253GNUNET_DATASTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
254 GNUNET_CONFIGURATION_Handle
255 *cfg)
256{ 254{
257 struct GNUNET_CLIENT_Connection *c; 255 struct GNUNET_CLIENT_Connection *c;
258 struct GNUNET_DATASTORE_Handle *h; 256 struct GNUNET_DATASTORE_Handle *h;
259 257
260 c = GNUNET_CLIENT_connect ("datastore", cfg); 258 c = GNUNET_CLIENT_connect ("datastore", cfg);
261 if (c == NULL) 259 if (c == NULL)
262 return NULL; /* oops */ 260 return NULL; /* oops */
263 h = GNUNET_malloc (sizeof(struct GNUNET_DATASTORE_Handle) + 261 h = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_Handle) +
264 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1); 262 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1);
265 h->client = c; 263 h->client = c;
266 h->cfg = cfg; 264 h->cfg = cfg;
267 h->stats = GNUNET_STATISTICS_create ("datastore-api", 265 h->stats = GNUNET_STATISTICS_create ("datastore-api", cfg);
268 cfg);
269 return h; 266 return h;
270} 267}
271 268
@@ -279,26 +276,24 @@ GNUNET_DATASTORE_connect (const struct
279 * @return number of bytes written to buf 276 * @return number of bytes written to buf
280 */ 277 */
281static size_t 278static size_t
282transmit_drop (void *cls, 279transmit_drop (void *cls, size_t size, void *buf)
283 size_t size,
284 void *buf)
285{ 280{
286 struct GNUNET_DATASTORE_Handle *h = cls; 281 struct GNUNET_DATASTORE_Handle *h = cls;
287 struct GNUNET_MessageHeader *hdr; 282 struct GNUNET_MessageHeader *hdr;
288 283
289 if (buf == NULL) 284 if (buf == NULL)
290 { 285 {
291 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
292 _("Failed to transmit request to drop database.\n")); 287 _("Failed to transmit request to drop database.\n"));
293 GNUNET_DATASTORE_disconnect (h, GNUNET_NO); 288 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
294 return 0; 289 return 0;
295 } 290 }
296 GNUNET_assert (size >= sizeof(struct GNUNET_MessageHeader)); 291 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
297 hdr = buf; 292 hdr = buf;
298 hdr->size = htons(sizeof(struct GNUNET_MessageHeader)); 293 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
299 hdr->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DROP); 294 hdr->type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
300 GNUNET_DATASTORE_disconnect (h, GNUNET_NO); 295 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
301 return sizeof(struct GNUNET_MessageHeader); 296 return sizeof (struct GNUNET_MessageHeader);
302} 297}
303 298
304 299
@@ -310,55 +305,51 @@ transmit_drop (void *cls,
310 * @param drop set to GNUNET_YES to delete all data in datastore (!) 305 * @param drop set to GNUNET_YES to delete all data in datastore (!)
311 */ 306 */
312void 307void
313GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h, 308GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h, int drop)
314 int drop)
315{ 309{
316 struct GNUNET_DATASTORE_QueueEntry *qe; 310 struct GNUNET_DATASTORE_QueueEntry *qe;
317 311
318#if DEBUG_DATASTORE 312#if DEBUG_DATASTORE
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Datastore disconnect\n");
320 "Datastore disconnect\n");
321#endif 314#endif
322 if (NULL != h->th) 315 if (NULL != h->th)
323 { 316 {
324 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th); 317 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
325 h->th = NULL; 318 h->th = NULL;
326 } 319 }
327 if (h->client != NULL) 320 if (h->client != NULL)
328 { 321 {
329 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO); 322 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
330 h->client = NULL; 323 h->client = NULL;
331 } 324 }
332 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK) 325 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
333 { 326 {
334 GNUNET_SCHEDULER_cancel (h->reconnect_task); 327 GNUNET_SCHEDULER_cancel (h->reconnect_task);
335 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK; 328 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
336 } 329 }
337 while (NULL != (qe = h->queue_head)) 330 while (NULL != (qe = h->queue_head))
331 {
332 GNUNET_assert (NULL != qe->response_proc);
333 qe->response_proc (h, NULL);
334 }
335 if (GNUNET_YES == drop)
336 {
337 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
338 if (h->client != NULL)
338 { 339 {
339 GNUNET_assert (NULL != qe->response_proc); 340 if (NULL !=
340 qe->response_proc (h, NULL); 341 GNUNET_CLIENT_notify_transmit_ready (h->client,
341 } 342 sizeof (struct
342 if (GNUNET_YES == drop) 343 GNUNET_MessageHeader),
343 { 344 GNUNET_TIME_UNIT_MINUTES,
344 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg); 345 GNUNET_YES, &transmit_drop, h))
345 if (h->client != NULL) 346 return;
346 { 347 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
347 if (NULL != 348 h->client = NULL;
348 GNUNET_CLIENT_notify_transmit_ready (h->client,
349 sizeof(struct GNUNET_MessageHeader),
350 GNUNET_TIME_UNIT_MINUTES,
351 GNUNET_YES,
352 &transmit_drop,
353 h))
354 return;
355 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
356 h->client = NULL;
357 }
358 GNUNET_break (0);
359 } 349 }
360 GNUNET_STATISTICS_destroy (h->stats, 350 GNUNET_break (0);
361 GNUNET_NO); 351 }
352 GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
362 h->stats = NULL; 353 h->stats = NULL;
363 GNUNET_free (h); 354 GNUNET_free (h);
364} 355}
@@ -371,20 +362,18 @@ GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
371 * @param tc scheduler context 362 * @param tc scheduler context
372 */ 363 */
373static void 364static void
374timeout_queue_entry (void *cls, 365timeout_queue_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
375 const struct GNUNET_SCHEDULER_TaskContext *tc)
376{ 366{
377 struct GNUNET_DATASTORE_QueueEntry *qe = cls; 367 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
378 368
379 GNUNET_STATISTICS_update (qe->h->stats, 369 GNUNET_STATISTICS_update (qe->h->stats,
380 gettext_noop ("# queue entry timeouts"), 370 gettext_noop ("# queue entry timeouts"),
381 1, 371 1, GNUNET_NO);
382 GNUNET_NO);
383 qe->task = GNUNET_SCHEDULER_NO_TASK; 372 qe->task = GNUNET_SCHEDULER_NO_TASK;
384 GNUNET_assert (qe->was_transmitted == GNUNET_NO); 373 GNUNET_assert (qe->was_transmitted == GNUNET_NO);
385#if DEBUG_DATASTORE 374#if DEBUG_DATASTORE
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "Timeout of request in datastore queue\n"); 376 "Timeout of request in datastore queue\n");
388#endif 377#endif
389 qe->response_proc (qe->h, NULL); 378 qe->response_proc (qe->h, NULL);
390} 379}
@@ -406,12 +395,12 @@ timeout_queue_entry (void *cls,
406 */ 395 */
407static struct GNUNET_DATASTORE_QueueEntry * 396static struct GNUNET_DATASTORE_QueueEntry *
408make_queue_entry (struct GNUNET_DATASTORE_Handle *h, 397make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
409 size_t msize, 398 size_t msize,
410 unsigned int queue_priority, 399 unsigned int queue_priority,
411 unsigned int max_queue_size, 400 unsigned int max_queue_size,
412 struct GNUNET_TIME_Relative timeout, 401 struct GNUNET_TIME_Relative timeout,
413 GNUNET_CLIENT_MessageHandler response_proc, 402 GNUNET_CLIENT_MessageHandler response_proc,
414 const union QueueContext *qc) 403 const union QueueContext *qc)
415{ 404{
416 struct GNUNET_DATASTORE_QueueEntry *ret; 405 struct GNUNET_DATASTORE_QueueEntry *ret;
417 struct GNUNET_DATASTORE_QueueEntry *pos; 406 struct GNUNET_DATASTORE_QueueEntry *pos;
@@ -419,21 +408,18 @@ make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
419 408
420 c = 0; 409 c = 0;
421 pos = h->queue_head; 410 pos = h->queue_head;
422 while ( (pos != NULL) && 411 while ((pos != NULL) &&
423 (c < max_queue_size) && 412 (c < max_queue_size) && (pos->priority >= queue_priority))
424 (pos->priority >= queue_priority) ) 413 {
425 { 414 c++;
426 c++; 415 pos = pos->next;
427 pos = pos->next; 416 }
428 }
429 if (c >= max_queue_size) 417 if (c >= max_queue_size)
430 { 418 {
431 GNUNET_STATISTICS_update (h->stats, 419 GNUNET_STATISTICS_update (h->stats,
432 gettext_noop ("# queue overflows"), 420 gettext_noop ("# queue overflows"), 1, GNUNET_NO);
433 1, 421 return NULL;
434 GNUNET_NO); 422 }
435 return NULL;
436 }
437 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize); 423 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
438 ret->h = h; 424 ret->h = h;
439 ret->response_proc = response_proc; 425 ret->response_proc = response_proc;
@@ -444,61 +430,49 @@ make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
444 ret->message_size = msize; 430 ret->message_size = msize;
445 ret->was_transmitted = GNUNET_NO; 431 ret->was_transmitted = GNUNET_NO;
446 if (pos == NULL) 432 if (pos == NULL)
447 { 433 {
448 /* append at the tail */ 434 /* append at the tail */
449 pos = h->queue_tail; 435 pos = h->queue_tail;
450 } 436 }
451 else 437 else
452 { 438 {
453 pos = pos->prev; 439 pos = pos->prev;
454 /* do not insert at HEAD if HEAD query was already 440 /* do not insert at HEAD if HEAD query was already
455 transmitted and we are still receiving replies! */ 441 * transmitted and we are still receiving replies! */
456 if ( (pos == NULL) && 442 if ((pos == NULL) && (h->queue_head->was_transmitted))
457 (h->queue_head->was_transmitted) ) 443 pos = h->queue_head;
458 pos = h->queue_head; 444 }
459 }
460 c++; 445 c++;
461 GNUNET_STATISTICS_update (h->stats, 446 GNUNET_STATISTICS_update (h->stats,
462 gettext_noop ("# queue entries created"), 447 gettext_noop ("# queue entries created"),
463 1, 448 1, GNUNET_NO);
464 GNUNET_NO); 449 GNUNET_CONTAINER_DLL_insert_after (h->queue_head, h->queue_tail, pos, ret);
465 GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
466 h->queue_tail,
467 pos,
468 ret);
469 h->queue_size++; 450 h->queue_size++;
470 ret->task = GNUNET_SCHEDULER_add_delayed (timeout, 451 ret->task = GNUNET_SCHEDULER_add_delayed (timeout, &timeout_queue_entry, ret);
471 &timeout_queue_entry,
472 ret);
473 pos = ret->next; 452 pos = ret->next;
474 while (pos != NULL) 453 while (pos != NULL)
454 {
455 if ((pos->max_queue < h->queue_size) && (pos->was_transmitted == GNUNET_NO))
475 { 456 {
476 if ( (pos->max_queue < h->queue_size) && 457 GNUNET_assert (pos->response_proc != NULL);
477 (pos->was_transmitted == GNUNET_NO) ) 458 /* move 'pos' element to head so that it will be
478 { 459 * killed on 'NULL' call below */
479 GNUNET_assert (pos->response_proc != NULL);
480 /* move 'pos' element to head so that it will be
481 killed on 'NULL' call below */
482#if DEBUG_DATASTORE 460#if DEBUG_DATASTORE
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484 "Dropping request from datastore queue\n"); 462 "Dropping request from datastore queue\n");
485#endif 463#endif
486 GNUNET_CONTAINER_DLL_remove (h->queue_head, 464 GNUNET_CONTAINER_DLL_remove (h->queue_head, h->queue_tail, pos);
487 h->queue_tail, 465 GNUNET_CONTAINER_DLL_insert (h->queue_head, h->queue_tail, pos);
488 pos); 466 GNUNET_STATISTICS_update (h->stats,
489 GNUNET_CONTAINER_DLL_insert (h->queue_head, 467 gettext_noop
490 h->queue_tail, 468 ("# Requests dropped from datastore queue"), 1,
491 pos); 469 GNUNET_NO);
492 GNUNET_STATISTICS_update (h->stats, 470 GNUNET_assert (h->queue_head == pos);
493 gettext_noop ("# Requests dropped from datastore queue"), 471 pos->response_proc (h, NULL);
494 1, 472 break;
495 GNUNET_NO);
496 GNUNET_assert (h->queue_head == pos);
497 pos->response_proc (h, NULL);
498 break;
499 }
500 pos = pos->next;
501 } 473 }
474 pos = pos->next;
475 }
502 return ret; 476 return ret;
503} 477}
504 478
@@ -509,8 +483,7 @@ make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
509 * 483 *
510 * @param h handle to the datastore 484 * @param h handle to the datastore
511 */ 485 */
512static void 486static void process_queue (struct GNUNET_DATASTORE_Handle *h);
513process_queue (struct GNUNET_DATASTORE_Handle *h);
514 487
515 488
516/** 489/**
@@ -520,8 +493,7 @@ process_queue (struct GNUNET_DATASTORE_Handle *h);
520 * @param tc scheduler context 493 * @param tc scheduler context
521 */ 494 */
522static void 495static void
523try_reconnect (void *cls, 496try_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
524 const struct GNUNET_SCHEDULER_TaskContext *tc)
525{ 497{
526 struct GNUNET_DATASTORE_Handle *h = cls; 498 struct GNUNET_DATASTORE_Handle *h = cls;
527 499
@@ -534,18 +506,17 @@ try_reconnect (void *cls,
534 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK; 506 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
535 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg); 507 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
536 if (h->client == NULL) 508 if (h->client == NULL)
537 { 509 {
538 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 510 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
539 "DATASTORE reconnect failed (fatally)\n"); 511 "DATASTORE reconnect failed (fatally)\n");
540 return; 512 return;
541 } 513 }
542 GNUNET_STATISTICS_update (h->stats, 514 GNUNET_STATISTICS_update (h->stats,
543 gettext_noop ("# datastore connections (re)created"), 515 gettext_noop
544 1, 516 ("# datastore connections (re)created"), 1,
545 GNUNET_NO); 517 GNUNET_NO);
546#if DEBUG_DATASTORE 518#if DEBUG_DATASTORE
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reconnected to DATASTORE\n");
548 "Reconnected to DATASTORE\n");
549#endif 520#endif
550 process_queue (h); 521 process_queue (h);
551} 522}
@@ -561,25 +532,23 @@ static void
561do_disconnect (struct GNUNET_DATASTORE_Handle *h) 532do_disconnect (struct GNUNET_DATASTORE_Handle *h)
562{ 533{
563 if (h->client == NULL) 534 if (h->client == NULL)
564 { 535 {
565#if DEBUG_DATASTORE 536#if DEBUG_DATASTORE
566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
567 "client NULL in disconnect, will not try to reconnect\n"); 538 "client NULL in disconnect, will not try to reconnect\n");
568#endif 539#endif
569 return; 540 return;
570 } 541 }
571#if 0 542#if 0
572 GNUNET_STATISTICS_update (stats, 543 GNUNET_STATISTICS_update (stats,
573 gettext_noop ("# reconnected to DATASTORE"), 544 gettext_noop ("# reconnected to DATASTORE"),
574 1, 545 1, GNUNET_NO);
575 GNUNET_NO);
576#endif 546#endif
577 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO); 547 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
578 h->skip_next_messages = 0; 548 h->skip_next_messages = 0;
579 h->client = NULL; 549 h->client = NULL;
580 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_time, 550 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_time,
581 &try_reconnect, 551 &try_reconnect, h);
582 h);
583} 552}
584 553
585 554
@@ -590,30 +559,28 @@ do_disconnect (struct GNUNET_DATASTORE_Handle *h)
590 * @param cls the 'struct GNUNET_DATASTORE_Handle' 559 * @param cls the 'struct GNUNET_DATASTORE_Handle'
591 * @param msg the received message 560 * @param msg the received message
592 */ 561 */
593static void 562static void
594receive_cb (void *cls, 563receive_cb (void *cls, const struct GNUNET_MessageHeader *msg)
595 const struct GNUNET_MessageHeader *msg)
596{ 564{
597 struct GNUNET_DATASTORE_Handle *h = cls; 565 struct GNUNET_DATASTORE_Handle *h = cls;
598 struct GNUNET_DATASTORE_QueueEntry *qe; 566 struct GNUNET_DATASTORE_QueueEntry *qe;
599 567
600 h->in_receive = GNUNET_NO; 568 h->in_receive = GNUNET_NO;
601#if DEBUG_DATASTORE 569#if DEBUG_DATASTORE
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving reply from datastore\n");
603 "Receiving reply from datastore\n");
604#endif 571#endif
605 if (h->skip_next_messages > 0) 572 if (h->skip_next_messages > 0)
606 { 573 {
607 h->skip_next_messages--; 574 h->skip_next_messages--;
608 process_queue (h); 575 process_queue (h);
609 return; 576 return;
610 } 577 }
611 if (NULL == (qe = h->queue_head)) 578 if (NULL == (qe = h->queue_head))
612 { 579 {
613 GNUNET_break (0); 580 GNUNET_break (0);
614 process_queue (h); 581 process_queue (h);
615 return; 582 return;
616 } 583 }
617 qe->response_proc (h, msg); 584 qe->response_proc (h, msg);
618} 585}
619 586
@@ -627,9 +594,7 @@ receive_cb (void *cls,
627 * @return number of bytes written to buf 594 * @return number of bytes written to buf
628 */ 595 */
629static size_t 596static size_t
630transmit_request (void *cls, 597transmit_request (void *cls, size_t size, void *buf)
631 size_t size,
632 void *buf)
633{ 598{
634 struct GNUNET_DATASTORE_Handle *h = cls; 599 struct GNUNET_DATASTORE_Handle *h = cls;
635 struct GNUNET_DATASTORE_QueueEntry *qe; 600 struct GNUNET_DATASTORE_QueueEntry *qe;
@@ -637,27 +602,25 @@ transmit_request (void *cls,
637 602
638 h->th = NULL; 603 h->th = NULL;
639 if (NULL == (qe = h->queue_head)) 604 if (NULL == (qe = h->queue_head))
640 return 0; /* no entry in queue */ 605 return 0; /* no entry in queue */
641 if (buf == NULL) 606 if (buf == NULL)
642 { 607 {
643 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 608 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
644 _("Failed to transmit request to DATASTORE.\n")); 609 _("Failed to transmit request to DATASTORE.\n"));
645 GNUNET_STATISTICS_update (h->stats, 610 GNUNET_STATISTICS_update (h->stats,
646 gettext_noop ("# transmission request failures"), 611 gettext_noop ("# transmission request failures"),
647 1, 612 1, GNUNET_NO);
648 GNUNET_NO); 613 do_disconnect (h);
649 do_disconnect (h); 614 return 0;
650 return 0; 615 }
651 }
652 if (size < (msize = qe->message_size)) 616 if (size < (msize = qe->message_size))
653 { 617 {
654 process_queue (h); 618 process_queue (h);
655 return 0; 619 return 0;
656 } 620 }
657 #if DEBUG_DATASTORE 621#if DEBUG_DATASTORE
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
659 "Transmitting %u byte request to DATASTORE\n", 623 "Transmitting %u byte request to DATASTORE\n", msize);
660 msize);
661#endif 624#endif
662 memcpy (buf, &qe[1], msize); 625 memcpy (buf, &qe[1], msize);
663 qe->was_transmitted = GNUNET_YES; 626 qe->was_transmitted = GNUNET_YES;
@@ -666,13 +629,11 @@ transmit_request (void *cls,
666 GNUNET_assert (GNUNET_NO == h->in_receive); 629 GNUNET_assert (GNUNET_NO == h->in_receive);
667 h->in_receive = GNUNET_YES; 630 h->in_receive = GNUNET_YES;
668 GNUNET_CLIENT_receive (h->client, 631 GNUNET_CLIENT_receive (h->client,
669 &receive_cb, 632 &receive_cb,
670 h, 633 h, GNUNET_TIME_absolute_get_remaining (qe->timeout));
671 GNUNET_TIME_absolute_get_remaining (qe->timeout));
672 GNUNET_STATISTICS_update (h->stats, 634 GNUNET_STATISTICS_update (h->stats,
673 gettext_noop ("# bytes sent to datastore"), 635 gettext_noop ("# bytes sent to datastore"),
674 1, 636 1, GNUNET_NO);
675 GNUNET_NO);
676 return msize; 637 return msize;
677} 638}
678 639
@@ -689,53 +650,47 @@ process_queue (struct GNUNET_DATASTORE_Handle *h)
689 struct GNUNET_DATASTORE_QueueEntry *qe; 650 struct GNUNET_DATASTORE_QueueEntry *qe;
690 651
691 if (NULL == (qe = h->queue_head)) 652 if (NULL == (qe = h->queue_head))
692 { 653 {
693#if DEBUG_DATASTORE > 1 654#if DEBUG_DATASTORE > 1
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Queue empty\n");
695 "Queue empty\n");
696#endif 656#endif
697 return; /* no entry in queue */ 657 return; /* no entry in queue */
698 } 658 }
699 if (qe->was_transmitted == GNUNET_YES) 659 if (qe->was_transmitted == GNUNET_YES)
700 { 660 {
701#if DEBUG_DATASTORE > 1 661#if DEBUG_DATASTORE > 1
702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Head request already transmitted\n");
703 "Head request already transmitted\n");
704#endif 663#endif
705 return; /* waiting for replies */ 664 return; /* waiting for replies */
706 } 665 }
707 if (h->th != NULL) 666 if (h->th != NULL)
708 { 667 {
709#if DEBUG_DATASTORE > 1 668#if DEBUG_DATASTORE > 1
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Pending transmission request\n");
711 "Pending transmission request\n");
712#endif 670#endif
713 return; /* request pending */ 671 return; /* request pending */
714 } 672 }
715 if (h->client == NULL) 673 if (h->client == NULL)
716 { 674 {
717#if DEBUG_DATASTORE > 1 675#if DEBUG_DATASTORE > 1
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not connected\n");
719 "Not connected\n");
720#endif 677#endif
721 return; /* waiting for reconnect */ 678 return; /* waiting for reconnect */
722 } 679 }
723 if (GNUNET_YES == h->in_receive) 680 if (GNUNET_YES == h->in_receive)
724 { 681 {
725 /* wait for response to previous query */ 682 /* wait for response to previous query */
726 return; 683 return;
727 } 684 }
728#if DEBUG_DATASTORE 685#if DEBUG_DATASTORE
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
730 "Queueing %u byte request to DATASTORE\n", 687 "Queueing %u byte request to DATASTORE\n", qe->message_size);
731 qe->message_size);
732#endif 688#endif
733 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, 689 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
734 qe->message_size, 690 qe->message_size,
735 GNUNET_TIME_absolute_get_remaining (qe->timeout), 691 GNUNET_TIME_absolute_get_remaining
736 GNUNET_YES, 692 (qe->timeout), GNUNET_YES,
737 &transmit_request, 693 &transmit_request, h);
738 h);
739 GNUNET_assert (GNUNET_NO == h->in_receive); 694 GNUNET_assert (GNUNET_NO == h->in_receive);
740 GNUNET_break (NULL != h->th); 695 GNUNET_break (NULL != h->th);
741} 696}
@@ -767,16 +722,14 @@ free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
767{ 722{
768 struct GNUNET_DATASTORE_Handle *h = qe->h; 723 struct GNUNET_DATASTORE_Handle *h = qe->h;
769 724
770 GNUNET_CONTAINER_DLL_remove (h->queue_head, 725 GNUNET_CONTAINER_DLL_remove (h->queue_head, h->queue_tail, qe);
771 h->queue_tail,
772 qe);
773 if (qe->task != GNUNET_SCHEDULER_NO_TASK) 726 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
774 { 727 {
775 GNUNET_SCHEDULER_cancel (qe->task); 728 GNUNET_SCHEDULER_cancel (qe->task);
776 qe->task = GNUNET_SCHEDULER_NO_TASK; 729 qe->task = GNUNET_SCHEDULER_NO_TASK;
777 } 730 }
778 h->queue_size--; 731 h->queue_size--;
779 qe->was_transmitted = GNUNET_SYSERR; /* use-after-free warning */ 732 qe->was_transmitted = GNUNET_SYSERR; /* use-after-free warning */
780 GNUNET_free (qe); 733 GNUNET_free (qe);
781} 734}
782 735
@@ -788,10 +741,8 @@ free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
788 * @param cls closure 741 * @param cls closure
789 * @param msg message received, NULL on timeout or fatal error 742 * @param msg message received, NULL on timeout or fatal error
790 */ 743 */
791static void 744static void
792process_status_message (void *cls, 745process_status_message (void *cls, const struct GNUNET_MessageHeader *msg)
793 const struct
794 GNUNET_MessageHeader * msg)
795{ 746{
796 struct GNUNET_DATASTORE_Handle *h = cls; 747 struct GNUNET_DATASTORE_Handle *h = cls;
797 struct GNUNET_DATASTORE_QueueEntry *qe; 748 struct GNUNET_DATASTORE_QueueEntry *qe;
@@ -802,74 +753,68 @@ process_status_message (void *cls,
802 int was_transmitted; 753 int was_transmitted;
803 754
804 if (NULL == (qe = h->queue_head)) 755 if (NULL == (qe = h->queue_head))
805 { 756 {
806 GNUNET_break (0); 757 GNUNET_break (0);
807 do_disconnect (h); 758 do_disconnect (h);
808 return; 759 return;
809 } 760 }
810 rc = qe->qc.sc; 761 rc = qe->qc.sc;
811 if (msg == NULL) 762 if (msg == NULL)
812 { 763 {
813 was_transmitted = qe->was_transmitted; 764 was_transmitted = qe->was_transmitted;
814 free_queue_entry (qe); 765 free_queue_entry (qe);
815 if (was_transmitted == GNUNET_YES) 766 if (was_transmitted == GNUNET_YES)
816 do_disconnect (h); 767 do_disconnect (h);
817 else 768 else
818 process_queue (h); 769 process_queue (h);
819 if (rc.cont != NULL) 770 if (rc.cont != NULL)
820 rc.cont (rc.cont_cls, 771 rc.cont (rc.cont_cls,
821 GNUNET_SYSERR, 772 GNUNET_SYSERR,
822 _("Failed to receive status response from database.")); 773 _("Failed to receive status response from database."));
823 return; 774 return;
824 } 775 }
825 GNUNET_assert (GNUNET_YES == qe->was_transmitted); 776 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
826 free_queue_entry (qe); 777 free_queue_entry (qe);
827 if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) || 778 if ((ntohs (msg->size) < sizeof (struct StatusMessage)) ||
828 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) ) 779 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS))
829 { 780 {
830 GNUNET_break (0); 781 GNUNET_break (0);
831 h->retry_time = GNUNET_TIME_UNIT_ZERO; 782 h->retry_time = GNUNET_TIME_UNIT_ZERO;
832 do_disconnect (h); 783 do_disconnect (h);
833 if (rc.cont != NULL) 784 if (rc.cont != NULL)
834 rc.cont (rc.cont_cls, 785 rc.cont (rc.cont_cls,
835 GNUNET_SYSERR, 786 GNUNET_SYSERR,
836 _("Error reading response from datastore service")); 787 _("Error reading response from datastore service"));
837 return; 788 return;
838 } 789 }
839 sm = (const struct StatusMessage*) msg; 790 sm = (const struct StatusMessage *) msg;
840 status = ntohl(sm->status); 791 status = ntohl (sm->status);
841 emsg = NULL; 792 emsg = NULL;
842 if (ntohs(msg->size) > sizeof(struct StatusMessage)) 793 if (ntohs (msg->size) > sizeof (struct StatusMessage))
843 { 794 {
844 emsg = (const char*) &sm[1]; 795 emsg = (const char *) &sm[1];
845 if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0') 796 if (emsg[ntohs (msg->size) - sizeof (struct StatusMessage) - 1] != '\0')
846 {
847 GNUNET_break (0);
848 emsg = _("Invalid error message received from datastore service");
849 }
850 }
851 if ( (status == GNUNET_SYSERR) &&
852 (emsg == NULL) )
853 { 797 {
854 GNUNET_break (0); 798 GNUNET_break (0);
855 emsg = _("Invalid error message received from datastore service"); 799 emsg = _("Invalid error message received from datastore service");
856 } 800 }
801 }
802 if ((status == GNUNET_SYSERR) && (emsg == NULL))
803 {
804 GNUNET_break (0);
805 emsg = _("Invalid error message received from datastore service");
806 }
857#if DEBUG_DATASTORE 807#if DEBUG_DATASTORE
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Received status %d/%s\n", 809 "Received status %d/%s\n", (int) status, emsg);
860 (int) status,
861 emsg);
862#endif 810#endif
863 GNUNET_STATISTICS_update (h->stats, 811 GNUNET_STATISTICS_update (h->stats,
864 gettext_noop ("# status messages received"), 812 gettext_noop ("# status messages received"),
865 1, 813 1, GNUNET_NO);
866 GNUNET_NO);
867 h->retry_time.rel_value = 0; 814 h->retry_time.rel_value = 0;
868 process_queue (h); 815 process_queue (h);
869 if (rc.cont != NULL) 816 if (rc.cont != NULL)
870 rc.cont (rc.cont_cls, 817 rc.cont (rc.cont_cls, status, emsg);
871 status,
872 emsg);
873} 818}
874 819
875 820
@@ -901,20 +846,20 @@ process_status_message (void *cls,
901 */ 846 */
902struct GNUNET_DATASTORE_QueueEntry * 847struct GNUNET_DATASTORE_QueueEntry *
903GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h, 848GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
904 uint32_t rid, 849 uint32_t rid,
905 const GNUNET_HashCode * key, 850 const GNUNET_HashCode * key,
906 size_t size, 851 size_t size,
907 const void *data, 852 const void *data,
908 enum GNUNET_BLOCK_Type type, 853 enum GNUNET_BLOCK_Type type,
909 uint32_t priority, 854 uint32_t priority,
910 uint32_t anonymity, 855 uint32_t anonymity,
911 uint32_t replication, 856 uint32_t replication,
912 struct GNUNET_TIME_Absolute expiration, 857 struct GNUNET_TIME_Absolute expiration,
913 unsigned int queue_priority, 858 unsigned int queue_priority,
914 unsigned int max_queue_size, 859 unsigned int max_queue_size,
915 struct GNUNET_TIME_Relative timeout, 860 struct GNUNET_TIME_Relative timeout,
916 GNUNET_DATASTORE_ContinuationWithStatus cont, 861 GNUNET_DATASTORE_ContinuationWithStatus cont,
917 void *cont_cls) 862 void *cont_cls)
918{ 863{
919 struct GNUNET_DATASTORE_QueueEntry *qe; 864 struct GNUNET_DATASTORE_QueueEntry *qe;
920 struct DataMessage *dm; 865 struct DataMessage *dm;
@@ -923,42 +868,41 @@ GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
923 868
924#if DEBUG_DATASTORE 869#if DEBUG_DATASTORE
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926 "Asked to put %u bytes of data under key `%s' for %llu ms\n", 871 "Asked to put %u bytes of data under key `%s' for %llu ms\n",
927 size, 872 size,
928 GNUNET_h2s (key), 873 GNUNET_h2s (key),
929 GNUNET_TIME_absolute_get_remaining (expiration).rel_value); 874 GNUNET_TIME_absolute_get_remaining (expiration).rel_value);
930#endif 875#endif
931 msize = sizeof(struct DataMessage) + size; 876 msize = sizeof (struct DataMessage) + size;
932 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE); 877 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
933 qc.sc.cont = cont; 878 qc.sc.cont = cont;
934 qc.sc.cont_cls = cont_cls; 879 qc.sc.cont_cls = cont_cls;
935 qe = make_queue_entry (h, msize, 880 qe = make_queue_entry (h, msize,
936 queue_priority, max_queue_size, timeout, 881 queue_priority, max_queue_size, timeout,
937 &process_status_message, &qc); 882 &process_status_message, &qc);
938 if (qe == NULL) 883 if (qe == NULL)
939 { 884 {
940#if DEBUG_DATASTORE 885#if DEBUG_DATASTORE
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
942 "Could not create queue entry for PUT\n"); 887 "Could not create queue entry for PUT\n");
943#endif 888#endif
944 return NULL; 889 return NULL;
945 } 890 }
946 GNUNET_STATISTICS_update (h->stats, 891 GNUNET_STATISTICS_update (h->stats,
947 gettext_noop ("# PUT requests executed"), 892 gettext_noop ("# PUT requests executed"),
948 1, 893 1, GNUNET_NO);
949 GNUNET_NO); 894 dm = (struct DataMessage *) &qe[1];
950 dm = (struct DataMessage* ) &qe[1]; 895 dm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
951 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT); 896 dm->header.size = htons (msize);
952 dm->header.size = htons(msize); 897 dm->rid = htonl (rid);
953 dm->rid = htonl(rid); 898 dm->size = htonl ((uint32_t) size);
954 dm->size = htonl( (uint32_t) size); 899 dm->type = htonl (type);
955 dm->type = htonl(type); 900 dm->priority = htonl (priority);
956 dm->priority = htonl(priority); 901 dm->anonymity = htonl (anonymity);
957 dm->anonymity = htonl(anonymity);
958 dm->replication = htonl (replication); 902 dm->replication = htonl (replication);
959 dm->reserved = htonl (0); 903 dm->reserved = htonl (0);
960 dm->uid = GNUNET_htonll(0); 904 dm->uid = GNUNET_htonll (0);
961 dm->expiration = GNUNET_TIME_absolute_hton(expiration); 905 dm->expiration = GNUNET_TIME_absolute_hton (expiration);
962 dm->key = *key; 906 dm->key = *key;
963 memcpy (&dm[1], data, size); 907 memcpy (&dm[1], data, size);
964 process_queue (h); 908 process_queue (h);
@@ -987,13 +931,13 @@ GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
987 */ 931 */
988struct GNUNET_DATASTORE_QueueEntry * 932struct GNUNET_DATASTORE_QueueEntry *
989GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h, 933GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
990 uint64_t amount, 934 uint64_t amount,
991 uint32_t entries, 935 uint32_t entries,
992 unsigned int queue_priority, 936 unsigned int queue_priority,
993 unsigned int max_queue_size, 937 unsigned int max_queue_size,
994 struct GNUNET_TIME_Relative timeout, 938 struct GNUNET_TIME_Relative timeout,
995 GNUNET_DATASTORE_ContinuationWithStatus cont, 939 GNUNET_DATASTORE_ContinuationWithStatus cont,
996 void *cont_cls) 940 void *cont_cls)
997{ 941{
998 struct GNUNET_DATASTORE_QueueEntry *qe; 942 struct GNUNET_DATASTORE_QueueEntry *qe;
999 struct ReserveMessage *rm; 943 struct ReserveMessage *rm;
@@ -1003,32 +947,30 @@ GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
1003 cont = &drop_status_cont; 947 cont = &drop_status_cont;
1004#if DEBUG_DATASTORE 948#if DEBUG_DATASTORE
1005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006 "Asked to reserve %llu bytes of data and %u entries\n", 950 "Asked to reserve %llu bytes of data and %u entries\n",
1007 (unsigned long long) amount, 951 (unsigned long long) amount, (unsigned int) entries);
1008 (unsigned int) entries);
1009#endif 952#endif
1010 qc.sc.cont = cont; 953 qc.sc.cont = cont;
1011 qc.sc.cont_cls = cont_cls; 954 qc.sc.cont_cls = cont_cls;
1012 qe = make_queue_entry (h, sizeof(struct ReserveMessage), 955 qe = make_queue_entry (h, sizeof (struct ReserveMessage),
1013 queue_priority, max_queue_size, timeout, 956 queue_priority, max_queue_size, timeout,
1014 &process_status_message, &qc); 957 &process_status_message, &qc);
1015 if (qe == NULL) 958 if (qe == NULL)
1016 { 959 {
1017#if DEBUG_DATASTORE 960#if DEBUG_DATASTORE
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019 "Could not create queue entry to reserve\n"); 962 "Could not create queue entry to reserve\n");
1020#endif 963#endif
1021 return NULL; 964 return NULL;
1022 } 965 }
1023 GNUNET_STATISTICS_update (h->stats, 966 GNUNET_STATISTICS_update (h->stats,
1024 gettext_noop ("# RESERVE requests executed"), 967 gettext_noop ("# RESERVE requests executed"),
1025 1, 968 1, GNUNET_NO);
1026 GNUNET_NO); 969 rm = (struct ReserveMessage *) &qe[1];
1027 rm = (struct ReserveMessage*) &qe[1]; 970 rm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
1028 rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE); 971 rm->header.size = htons (sizeof (struct ReserveMessage));
1029 rm->header.size = htons(sizeof (struct ReserveMessage)); 972 rm->entries = htonl (entries);
1030 rm->entries = htonl(entries); 973 rm->amount = GNUNET_htonll (amount);
1031 rm->amount = GNUNET_htonll(amount);
1032 process_queue (h); 974 process_queue (h);
1033 return qe; 975 return qe;
1034} 976}
@@ -1057,12 +999,12 @@ GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
1057 */ 999 */
1058struct GNUNET_DATASTORE_QueueEntry * 1000struct GNUNET_DATASTORE_QueueEntry *
1059GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h, 1001GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
1060 uint32_t rid, 1002 uint32_t rid,
1061 unsigned int queue_priority, 1003 unsigned int queue_priority,
1062 unsigned int max_queue_size, 1004 unsigned int max_queue_size,
1063 struct GNUNET_TIME_Relative timeout, 1005 struct GNUNET_TIME_Relative timeout,
1064 GNUNET_DATASTORE_ContinuationWithStatus cont, 1006 GNUNET_DATASTORE_ContinuationWithStatus cont,
1065 void *cont_cls) 1007 void *cont_cls)
1066{ 1008{
1067 struct GNUNET_DATASTORE_QueueEntry *qe; 1009 struct GNUNET_DATASTORE_QueueEntry *qe;
1068 struct ReleaseReserveMessage *rrm; 1010 struct ReleaseReserveMessage *rrm;
@@ -1071,31 +1013,29 @@ GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
1071 if (cont == NULL) 1013 if (cont == NULL)
1072 cont = &drop_status_cont; 1014 cont = &drop_status_cont;
1073#if DEBUG_DATASTORE 1015#if DEBUG_DATASTORE
1074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asked to release reserve %d\n", rid);
1075 "Asked to release reserve %d\n",
1076 rid);
1077#endif 1017#endif
1078 qc.sc.cont = cont; 1018 qc.sc.cont = cont;
1079 qc.sc.cont_cls = cont_cls; 1019 qc.sc.cont_cls = cont_cls;
1080 qe = make_queue_entry (h, sizeof(struct ReleaseReserveMessage), 1020 qe = make_queue_entry (h, sizeof (struct ReleaseReserveMessage),
1081 queue_priority, max_queue_size, timeout, 1021 queue_priority, max_queue_size, timeout,
1082 &process_status_message, &qc); 1022 &process_status_message, &qc);
1083 if (qe == NULL) 1023 if (qe == NULL)
1084 { 1024 {
1085#if DEBUG_DATASTORE 1025#if DEBUG_DATASTORE
1086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1087 "Could not create queue entry to release reserve\n"); 1027 "Could not create queue entry to release reserve\n");
1088#endif 1028#endif
1089 return NULL; 1029 return NULL;
1090 } 1030 }
1091 GNUNET_STATISTICS_update (h->stats, 1031 GNUNET_STATISTICS_update (h->stats,
1092 gettext_noop ("# RELEASE RESERVE requests executed"), 1032 gettext_noop
1093 1, 1033 ("# RELEASE RESERVE requests executed"), 1,
1094 GNUNET_NO); 1034 GNUNET_NO);
1095 rrm = (struct ReleaseReserveMessage*) &qe[1]; 1035 rrm = (struct ReleaseReserveMessage *) &qe[1];
1096 rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE); 1036 rrm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
1097 rrm->header.size = htons(sizeof (struct ReleaseReserveMessage)); 1037 rrm->header.size = htons (sizeof (struct ReleaseReserveMessage));
1098 rrm->rid = htonl(rid); 1038 rrm->rid = htonl (rid);
1099 process_queue (h); 1039 process_queue (h);
1100 return qe; 1040 return qe;
1101} 1041}
@@ -1120,14 +1060,14 @@ GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
1120 */ 1060 */
1121struct GNUNET_DATASTORE_QueueEntry * 1061struct GNUNET_DATASTORE_QueueEntry *
1122GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h, 1062GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
1123 uint64_t uid, 1063 uint64_t uid,
1124 uint32_t priority, 1064 uint32_t priority,
1125 struct GNUNET_TIME_Absolute expiration, 1065 struct GNUNET_TIME_Absolute expiration,
1126 unsigned int queue_priority, 1066 unsigned int queue_priority,
1127 unsigned int max_queue_size, 1067 unsigned int max_queue_size,
1128 struct GNUNET_TIME_Relative timeout, 1068 struct GNUNET_TIME_Relative timeout,
1129 GNUNET_DATASTORE_ContinuationWithStatus cont, 1069 GNUNET_DATASTORE_ContinuationWithStatus cont,
1130 void *cont_cls) 1070 void *cont_cls)
1131{ 1071{
1132 struct GNUNET_DATASTORE_QueueEntry *qe; 1072 struct GNUNET_DATASTORE_QueueEntry *qe;
1133 struct UpdateMessage *um; 1073 struct UpdateMessage *um;
@@ -1137,34 +1077,33 @@ GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
1137 cont = &drop_status_cont; 1077 cont = &drop_status_cont;
1138#if DEBUG_DATASTORE 1078#if DEBUG_DATASTORE
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 "Asked to update entry %llu raising priority by %u and expiration to %llu\n", 1080 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
1141 uid, 1081 uid,
1142 (unsigned int) priority, 1082 (unsigned int) priority,
1143 (unsigned long long) expiration.abs_value); 1083 (unsigned long long) expiration.abs_value);
1144#endif 1084#endif
1145 qc.sc.cont = cont; 1085 qc.sc.cont = cont;
1146 qc.sc.cont_cls = cont_cls; 1086 qc.sc.cont_cls = cont_cls;
1147 qe = make_queue_entry (h, sizeof(struct UpdateMessage), 1087 qe = make_queue_entry (h, sizeof (struct UpdateMessage),
1148 queue_priority, max_queue_size, timeout, 1088 queue_priority, max_queue_size, timeout,
1149 &process_status_message, &qc); 1089 &process_status_message, &qc);
1150 if (qe == NULL) 1090 if (qe == NULL)
1151 { 1091 {
1152#if DEBUG_DATASTORE 1092#if DEBUG_DATASTORE
1153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1154 "Could not create queue entry for UPDATE\n"); 1094 "Could not create queue entry for UPDATE\n");
1155#endif 1095#endif
1156 return NULL; 1096 return NULL;
1157 } 1097 }
1158 GNUNET_STATISTICS_update (h->stats, 1098 GNUNET_STATISTICS_update (h->stats,
1159 gettext_noop ("# UPDATE requests executed"), 1099 gettext_noop ("# UPDATE requests executed"),
1160 1, 1100 1, GNUNET_NO);
1161 GNUNET_NO); 1101 um = (struct UpdateMessage *) &qe[1];
1162 um = (struct UpdateMessage*) &qe[1]; 1102 um->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
1163 um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE); 1103 um->header.size = htons (sizeof (struct UpdateMessage));
1164 um->header.size = htons(sizeof (struct UpdateMessage)); 1104 um->priority = htonl (priority);
1165 um->priority = htonl(priority); 1105 um->expiration = GNUNET_TIME_absolute_hton (expiration);
1166 um->expiration = GNUNET_TIME_absolute_hton(expiration); 1106 um->uid = GNUNET_htonll (uid);
1167 um->uid = GNUNET_htonll(uid);
1168 process_queue (h); 1107 process_queue (h);
1169 return qe; 1108 return qe;
1170} 1109}
@@ -1193,14 +1132,14 @@ GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
1193 */ 1132 */
1194struct GNUNET_DATASTORE_QueueEntry * 1133struct GNUNET_DATASTORE_QueueEntry *
1195GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h, 1134GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
1196 const GNUNET_HashCode *key, 1135 const GNUNET_HashCode * key,
1197 size_t size, 1136 size_t size,
1198 const void *data, 1137 const void *data,
1199 unsigned int queue_priority, 1138 unsigned int queue_priority,
1200 unsigned int max_queue_size, 1139 unsigned int max_queue_size,
1201 struct GNUNET_TIME_Relative timeout, 1140 struct GNUNET_TIME_Relative timeout,
1202 GNUNET_DATASTORE_ContinuationWithStatus cont, 1141 GNUNET_DATASTORE_ContinuationWithStatus cont,
1203 void *cont_cls) 1142 void *cont_cls)
1204{ 1143{
1205 struct GNUNET_DATASTORE_QueueEntry *qe; 1144 struct GNUNET_DATASTORE_QueueEntry *qe;
1206 struct DataMessage *dm; 1145 struct DataMessage *dm;
@@ -1211,39 +1150,37 @@ GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
1211 cont = &drop_status_cont; 1150 cont = &drop_status_cont;
1212#if DEBUG_DATASTORE 1151#if DEBUG_DATASTORE
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1214 "Asked to remove %u bytes under key `%s'\n", 1153 "Asked to remove %u bytes under key `%s'\n",
1215 size, 1154 size, GNUNET_h2s (key));
1216 GNUNET_h2s (key));
1217#endif 1155#endif
1218 qc.sc.cont = cont; 1156 qc.sc.cont = cont;
1219 qc.sc.cont_cls = cont_cls; 1157 qc.sc.cont_cls = cont_cls;
1220 msize = sizeof(struct DataMessage) + size; 1158 msize = sizeof (struct DataMessage) + size;
1221 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE); 1159 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1222 qe = make_queue_entry (h, msize, 1160 qe = make_queue_entry (h, msize,
1223 queue_priority, max_queue_size, timeout, 1161 queue_priority, max_queue_size, timeout,
1224 &process_status_message, &qc); 1162 &process_status_message, &qc);
1225 if (qe == NULL) 1163 if (qe == NULL)
1226 { 1164 {
1227#if DEBUG_DATASTORE 1165#if DEBUG_DATASTORE
1228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1229 "Could not create queue entry for REMOVE\n"); 1167 "Could not create queue entry for REMOVE\n");
1230#endif 1168#endif
1231 return NULL; 1169 return NULL;
1232 } 1170 }
1233 GNUNET_STATISTICS_update (h->stats, 1171 GNUNET_STATISTICS_update (h->stats,
1234 gettext_noop ("# REMOVE requests executed"), 1172 gettext_noop ("# REMOVE requests executed"),
1235 1, 1173 1, GNUNET_NO);
1236 GNUNET_NO); 1174 dm = (struct DataMessage *) &qe[1];
1237 dm = (struct DataMessage*) &qe[1]; 1175 dm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
1238 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE); 1176 dm->header.size = htons (msize);
1239 dm->header.size = htons(msize); 1177 dm->rid = htonl (0);
1240 dm->rid = htonl(0); 1178 dm->size = htonl (size);
1241 dm->size = htonl(size); 1179 dm->type = htonl (0);
1242 dm->type = htonl(0); 1180 dm->priority = htonl (0);
1243 dm->priority = htonl(0); 1181 dm->anonymity = htonl (0);
1244 dm->anonymity = htonl(0); 1182 dm->uid = GNUNET_htonll (0);
1245 dm->uid = GNUNET_htonll(0); 1183 dm->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_ZERO_ABS);
1246 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
1247 dm->key = *key; 1184 dm->key = *key;
1248 memcpy (&dm[1], data, size); 1185 memcpy (&dm[1], data, size);
1249 process_queue (h); 1186 process_queue (h);
@@ -1258,9 +1195,8 @@ GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
1258 * @param cls closure 1195 * @param cls closure
1259 * @param msg message received, NULL on timeout or fatal error 1196 * @param msg message received, NULL on timeout or fatal error
1260 */ 1197 */
1261static void 1198static void
1262process_result_message (void *cls, 1199process_result_message (void *cls, const struct GNUNET_MessageHeader *msg)
1263 const struct GNUNET_MessageHeader *msg)
1264{ 1200{
1265 struct GNUNET_DATASTORE_Handle *h = cls; 1201 struct GNUNET_DATASTORE_Handle *h = cls;
1266 struct GNUNET_DATASTORE_QueueEntry *qe; 1202 struct GNUNET_DATASTORE_QueueEntry *qe;
@@ -1269,104 +1205,98 @@ process_result_message (void *cls,
1269 int was_transmitted; 1205 int was_transmitted;
1270 1206
1271 if (msg == NULL) 1207 if (msg == NULL)
1208 {
1209 qe = h->queue_head;
1210 GNUNET_assert (NULL != qe);
1211 rc = qe->qc.rc;
1212 was_transmitted = qe->was_transmitted;
1213 free_queue_entry (qe);
1214 if (was_transmitted == GNUNET_YES)
1272 { 1215 {
1273 qe = h->queue_head; 1216 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1274 GNUNET_assert (NULL != qe); 1217 _("Failed to receive response from database.\n"));
1275 rc = qe->qc.rc; 1218 do_disconnect (h);
1276 was_transmitted = qe->was_transmitted;
1277 free_queue_entry (qe);
1278 if (was_transmitted == GNUNET_YES)
1279 {
1280 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1281 _("Failed to receive response from database.\n"));
1282 do_disconnect (h);
1283 }
1284 else
1285 {
1286 process_queue (h);
1287 }
1288 if (rc.proc != NULL)
1289 rc.proc (rc.proc_cls,
1290 NULL, 0, NULL, 0, 0, 0,
1291 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1292 return;
1293 } 1219 }
1294 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END) 1220 else
1295 { 1221 {
1296 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
1297 qe = h->queue_head;
1298 rc = qe->qc.rc;
1299 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1300 free_queue_entry (qe);
1301#if DEBUG_DATASTORE
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1303 "Received end of result set, new queue size is %u\n",
1304 h->queue_size);
1305#endif
1306 if (rc.proc != NULL)
1307 rc.proc (rc.proc_cls,
1308 NULL, 0, NULL, 0, 0, 0,
1309 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1310 h->retry_time.rel_value = 0;
1311 h->result_count = 0;
1312 process_queue (h); 1222 process_queue (h);
1313 return;
1314 } 1223 }
1224 if (rc.proc != NULL)
1225 rc.proc (rc.proc_cls,
1226 NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1227 return;
1228 }
1229 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1230 {
1231 GNUNET_break (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader));
1232 qe = h->queue_head;
1233 rc = qe->qc.rc;
1234 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1235 free_queue_entry (qe);
1236#if DEBUG_DATASTORE
1237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1238 "Received end of result set, new queue size is %u\n",
1239 h->queue_size);
1240#endif
1241 if (rc.proc != NULL)
1242 rc.proc (rc.proc_cls,
1243 NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1244 h->retry_time.rel_value = 0;
1245 h->result_count = 0;
1246 process_queue (h);
1247 return;
1248 }
1315 qe = h->queue_head; 1249 qe = h->queue_head;
1316 GNUNET_assert (NULL != qe); 1250 GNUNET_assert (NULL != qe);
1317 rc = qe->qc.rc; 1251 rc = qe->qc.rc;
1318 if (GNUNET_YES != qe->was_transmitted) 1252 if (GNUNET_YES != qe->was_transmitted)
1319 { 1253 {
1320 GNUNET_break (0); 1254 GNUNET_break (0);
1321 free_queue_entry (qe); 1255 free_queue_entry (qe);
1322 h->retry_time = GNUNET_TIME_UNIT_ZERO; 1256 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1323 do_disconnect (h); 1257 do_disconnect (h);
1324 if (rc.proc != NULL) 1258 if (rc.proc != NULL)
1325 rc.proc (rc.proc_cls, 1259 rc.proc (rc.proc_cls,
1326 NULL, 0, NULL, 0, 0, 0, 1260 NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1327 GNUNET_TIME_UNIT_ZERO_ABS, 0); 1261 return;
1328 return; 1262 }
1329 } 1263 if ((ntohs (msg->size) < sizeof (struct DataMessage)) ||
1330 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) || 1264 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1331 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) || 1265 (ntohs (msg->size) !=
1332 (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) ) 1266 sizeof (struct DataMessage) +
1333 { 1267 ntohl (((const struct DataMessage *) msg)->size)))
1334 GNUNET_break (0); 1268 {
1335 free_queue_entry (qe); 1269 GNUNET_break (0);
1336 h->retry_time = GNUNET_TIME_UNIT_ZERO; 1270 free_queue_entry (qe);
1337 do_disconnect (h); 1271 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1338 if (rc.proc != NULL) 1272 do_disconnect (h);
1339 rc.proc (rc.proc_cls, 1273 if (rc.proc != NULL)
1340 NULL, 0, NULL, 0, 0, 0, 1274 rc.proc (rc.proc_cls,
1341 GNUNET_TIME_UNIT_ZERO_ABS, 0); 1275 NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1342 return; 1276 return;
1343 } 1277 }
1344 GNUNET_STATISTICS_update (h->stats, 1278 GNUNET_STATISTICS_update (h->stats,
1345 gettext_noop ("# Results received"), 1279 gettext_noop ("# Results received"), 1, GNUNET_NO);
1346 1, 1280 dm = (const struct DataMessage *) msg;
1347 GNUNET_NO);
1348 dm = (const struct DataMessage*) msg;
1349#if DEBUG_DATASTORE 1281#if DEBUG_DATASTORE
1350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1351 "Received result %llu with type %u and size %u with key %s\n", 1283 "Received result %llu with type %u and size %u with key %s\n",
1352 (unsigned long long) GNUNET_ntohll(dm->uid), 1284 (unsigned long long) GNUNET_ntohll (dm->uid),
1353 ntohl(dm->type), 1285 ntohl (dm->type), ntohl (dm->size), GNUNET_h2s (&dm->key));
1354 ntohl(dm->size),
1355 GNUNET_h2s(&dm->key));
1356#endif 1286#endif
1357 free_queue_entry (qe); 1287 free_queue_entry (qe);
1358 h->retry_time.rel_value = 0; 1288 h->retry_time.rel_value = 0;
1359 process_queue (h); 1289 process_queue (h);
1360 if (rc.proc != NULL) 1290 if (rc.proc != NULL)
1361 rc.proc (rc.proc_cls, 1291 rc.proc (rc.proc_cls,
1362 &dm->key, 1292 &dm->key,
1363 ntohl(dm->size), 1293 ntohl (dm->size),
1364 &dm[1], 1294 &dm[1],
1365 ntohl(dm->type), 1295 ntohl (dm->type),
1366 ntohl(dm->priority), 1296 ntohl (dm->priority),
1367 ntohl(dm->anonymity), 1297 ntohl (dm->anonymity),
1368 GNUNET_TIME_absolute_ntoh(dm->expiration), 1298 GNUNET_TIME_absolute_ntoh (dm->expiration),
1369 GNUNET_ntohll(dm->uid)); 1299 GNUNET_ntohll (dm->uid));
1370} 1300}
1371 1301
1372 1302
@@ -1391,11 +1321,11 @@ process_result_message (void *cls,
1391 */ 1321 */
1392struct GNUNET_DATASTORE_QueueEntry * 1322struct GNUNET_DATASTORE_QueueEntry *
1393GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h, 1323GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
1394 unsigned int queue_priority, 1324 unsigned int queue_priority,
1395 unsigned int max_queue_size, 1325 unsigned int max_queue_size,
1396 struct GNUNET_TIME_Relative timeout, 1326 struct GNUNET_TIME_Relative timeout,
1397 GNUNET_DATASTORE_DatumProcessor proc, 1327 GNUNET_DATASTORE_DatumProcessor proc,
1398 void *proc_cls) 1328 void *proc_cls)
1399{ 1329{
1400 struct GNUNET_DATASTORE_QueueEntry *qe; 1330 struct GNUNET_DATASTORE_QueueEntry *qe;
1401 struct GNUNET_MessageHeader *m; 1331 struct GNUNET_MessageHeader *m;
@@ -1404,29 +1334,29 @@ GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
1404 GNUNET_assert (NULL != proc); 1334 GNUNET_assert (NULL != proc);
1405#if DEBUG_DATASTORE 1335#if DEBUG_DATASTORE
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1407 "Asked to get replication entry in %llu ms\n", 1337 "Asked to get replication entry in %llu ms\n",
1408 (unsigned long long) timeout.rel_value); 1338 (unsigned long long) timeout.rel_value);
1409#endif 1339#endif
1410 qc.rc.proc = proc; 1340 qc.rc.proc = proc;
1411 qc.rc.proc_cls = proc_cls; 1341 qc.rc.proc_cls = proc_cls;
1412 qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader), 1342 qe = make_queue_entry (h, sizeof (struct GNUNET_MessageHeader),
1413 queue_priority, max_queue_size, timeout, 1343 queue_priority, max_queue_size, timeout,
1414 &process_result_message, &qc); 1344 &process_result_message, &qc);
1415 if (qe == NULL) 1345 if (qe == NULL)
1416 { 1346 {
1417#if DEBUG_DATASTORE 1347#if DEBUG_DATASTORE
1418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1419 "Could not create queue entry for GET REPLICATION\n"); 1349 "Could not create queue entry for GET REPLICATION\n");
1420#endif 1350#endif
1421 return NULL; 1351 return NULL;
1422 } 1352 }
1423 GNUNET_STATISTICS_update (h->stats, 1353 GNUNET_STATISTICS_update (h->stats,
1424 gettext_noop ("# GET REPLICATION requests executed"), 1354 gettext_noop
1425 1, 1355 ("# GET REPLICATION requests executed"), 1,
1426 GNUNET_NO); 1356 GNUNET_NO);
1427 m = (struct GNUNET_MessageHeader*) &qe[1]; 1357 m = (struct GNUNET_MessageHeader *) &qe[1];
1428 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION); 1358 m->type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION);
1429 m->size = htons(sizeof (struct GNUNET_MessageHeader)); 1359 m->size = htons (sizeof (struct GNUNET_MessageHeader));
1430 process_queue (h); 1360 process_queue (h);
1431 return qe; 1361 return qe;
1432} 1362}
@@ -1454,13 +1384,13 @@ GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
1454 */ 1384 */
1455struct GNUNET_DATASTORE_QueueEntry * 1385struct GNUNET_DATASTORE_QueueEntry *
1456GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h, 1386GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
1457 uint64_t offset, 1387 uint64_t offset,
1458 unsigned int queue_priority, 1388 unsigned int queue_priority,
1459 unsigned int max_queue_size, 1389 unsigned int max_queue_size,
1460 struct GNUNET_TIME_Relative timeout, 1390 struct GNUNET_TIME_Relative timeout,
1461 enum GNUNET_BLOCK_Type type, 1391 enum GNUNET_BLOCK_Type type,
1462 GNUNET_DATASTORE_DatumProcessor proc, 1392 GNUNET_DATASTORE_DatumProcessor proc,
1463 void *proc_cls) 1393 void *proc_cls)
1464{ 1394{
1465 struct GNUNET_DATASTORE_QueueEntry *qe; 1395 struct GNUNET_DATASTORE_QueueEntry *qe;
1466 struct GetZeroAnonymityMessage *m; 1396 struct GetZeroAnonymityMessage *m;
@@ -1470,31 +1400,30 @@ GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
1470 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY); 1400 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
1471#if DEBUG_DATASTORE 1401#if DEBUG_DATASTORE
1472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1473 "Asked to get %llu-th zero-anonymity entry of type %d in %llu ms\n", 1403 "Asked to get %llu-th zero-anonymity entry of type %d in %llu ms\n",
1474 (unsigned long long) offset, 1404 (unsigned long long) offset,
1475 type, 1405 type, (unsigned long long) timeout.rel_value);
1476 (unsigned long long) timeout.rel_value);
1477#endif 1406#endif
1478 qc.rc.proc = proc; 1407 qc.rc.proc = proc;
1479 qc.rc.proc_cls = proc_cls; 1408 qc.rc.proc_cls = proc_cls;
1480 qe = make_queue_entry (h, sizeof(struct GetZeroAnonymityMessage), 1409 qe = make_queue_entry (h, sizeof (struct GetZeroAnonymityMessage),
1481 queue_priority, max_queue_size, timeout, 1410 queue_priority, max_queue_size, timeout,
1482 &process_result_message, &qc); 1411 &process_result_message, &qc);
1483 if (qe == NULL) 1412 if (qe == NULL)
1484 { 1413 {
1485#if DEBUG_DATASTORE 1414#if DEBUG_DATASTORE
1486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1487 "Could not create queue entry for zero-anonymity procation\n"); 1416 "Could not create queue entry for zero-anonymity procation\n");
1488#endif 1417#endif
1489 return NULL; 1418 return NULL;
1490 } 1419 }
1491 GNUNET_STATISTICS_update (h->stats, 1420 GNUNET_STATISTICS_update (h->stats,
1492 gettext_noop ("# GET ZERO ANONYMITY requests executed"), 1421 gettext_noop
1493 1, 1422 ("# GET ZERO ANONYMITY requests executed"), 1,
1494 GNUNET_NO); 1423 GNUNET_NO);
1495 m = (struct GetZeroAnonymityMessage*) &qe[1]; 1424 m = (struct GetZeroAnonymityMessage *) &qe[1];
1496 m->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY); 1425 m->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY);
1497 m->header.size = htons(sizeof (struct GetZeroAnonymityMessage)); 1426 m->header.size = htons (sizeof (struct GetZeroAnonymityMessage));
1498 m->type = htonl ((uint32_t) type); 1427 m->type = htonl ((uint32_t) type);
1499 m->offset = GNUNET_htonll (offset); 1428 m->offset = GNUNET_htonll (offset);
1500 process_queue (h); 1429 process_queue (h);
@@ -1525,14 +1454,13 @@ GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
1525 */ 1454 */
1526struct GNUNET_DATASTORE_QueueEntry * 1455struct GNUNET_DATASTORE_QueueEntry *
1527GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h, 1456GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h,
1528 uint64_t offset, 1457 uint64_t offset,
1529 const GNUNET_HashCode * key, 1458 const GNUNET_HashCode * key,
1530 enum GNUNET_BLOCK_Type type, 1459 enum GNUNET_BLOCK_Type type,
1531 unsigned int queue_priority, 1460 unsigned int queue_priority,
1532 unsigned int max_queue_size, 1461 unsigned int max_queue_size,
1533 struct GNUNET_TIME_Relative timeout, 1462 struct GNUNET_TIME_Relative timeout,
1534 GNUNET_DATASTORE_DatumProcessor proc, 1463 GNUNET_DATASTORE_DatumProcessor proc, void *proc_cls)
1535 void *proc_cls)
1536{ 1464{
1537 struct GNUNET_DATASTORE_QueueEntry *qe; 1465 struct GNUNET_DATASTORE_QueueEntry *qe;
1538 struct GetMessage *gm; 1466 struct GetMessage *gm;
@@ -1541,41 +1469,39 @@ GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h,
1541 GNUNET_assert (NULL != proc); 1469 GNUNET_assert (NULL != proc);
1542#if DEBUG_DATASTORE 1470#if DEBUG_DATASTORE
1543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1544 "Asked to look for data of type %u under key `%s'\n", 1472 "Asked to look for data of type %u under key `%s'\n",
1545 (unsigned int) type, 1473 (unsigned int) type, GNUNET_h2s (key));
1546 GNUNET_h2s (key));
1547#endif 1474#endif
1548 qc.rc.proc = proc; 1475 qc.rc.proc = proc;
1549 qc.rc.proc_cls = proc_cls; 1476 qc.rc.proc_cls = proc_cls;
1550 qe = make_queue_entry (h, sizeof(struct GetMessage), 1477 qe = make_queue_entry (h, sizeof (struct GetMessage),
1551 queue_priority, max_queue_size, timeout, 1478 queue_priority, max_queue_size, timeout,
1552 &process_result_message, &qc); 1479 &process_result_message, &qc);
1553 if (qe == NULL) 1480 if (qe == NULL)
1554 { 1481 {
1555#if DEBUG_DATASTORE 1482#if DEBUG_DATASTORE
1556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1557 "Could not queue request for `%s'\n", 1484 "Could not queue request for `%s'\n", GNUNET_h2s (key));
1558 GNUNET_h2s (key));
1559#endif 1485#endif
1560 return NULL; 1486 return NULL;
1561 } 1487 }
1562 GNUNET_STATISTICS_update (h->stats, 1488 GNUNET_STATISTICS_update (h->stats,
1563 gettext_noop ("# GET requests executed"), 1489 gettext_noop ("# GET requests executed"),
1564 1, 1490 1, GNUNET_NO);
1565 GNUNET_NO); 1491 gm = (struct GetMessage *) &qe[1];
1566 gm = (struct GetMessage*) &qe[1]; 1492 gm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1567 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET); 1493 gm->type = htonl (type);
1568 gm->type = htonl(type);
1569 gm->offset = GNUNET_htonll (offset); 1494 gm->offset = GNUNET_htonll (offset);
1570 if (key != NULL) 1495 if (key != NULL)
1571 { 1496 {
1572 gm->header.size = htons(sizeof (struct GetMessage)); 1497 gm->header.size = htons (sizeof (struct GetMessage));
1573 gm->key = *key; 1498 gm->key = *key;
1574 } 1499 }
1575 else 1500 else
1576 { 1501 {
1577 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode)); 1502 gm->header.size =
1578 } 1503 htons (sizeof (struct GetMessage) - sizeof (GNUNET_HashCode));
1504 }
1579 process_queue (h); 1505 process_queue (h);
1580 return qe; 1506 return qe;
1581} 1507}
@@ -1595,18 +1521,16 @@ GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1595 GNUNET_assert (GNUNET_SYSERR != qe->was_transmitted); 1521 GNUNET_assert (GNUNET_SYSERR != qe->was_transmitted);
1596 h = qe->h; 1522 h = qe->h;
1597#if DEBUG_DATASTORE 1523#if DEBUG_DATASTORE
1598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1599 "Pending DATASTORE request %p cancelled (%d, %d)\n", 1525 "Pending DATASTORE request %p cancelled (%d, %d)\n",
1600 qe, 1526 qe, qe->was_transmitted, h->queue_head == qe);
1601 qe->was_transmitted,
1602 h->queue_head == qe);
1603#endif 1527#endif
1604 if (GNUNET_YES == qe->was_transmitted) 1528 if (GNUNET_YES == qe->was_transmitted)
1605 { 1529 {
1606 free_queue_entry (qe); 1530 free_queue_entry (qe);
1607 h->skip_next_messages++; 1531 h->skip_next_messages++;
1608 return; 1532 return;
1609 } 1533 }
1610 free_queue_entry (qe); 1534 free_queue_entry (qe);
1611 process_queue (h); 1535 process_queue (h);
1612} 1536}
diff --git a/src/datastore/gnunet-service-datastore.c b/src/datastore/gnunet-service-datastore.c
index 39354acaf..c01de5891 100644
--- a/src/datastore/gnunet-service-datastore.c
+++ b/src/datastore/gnunet-service-datastore.c
@@ -92,7 +92,7 @@ struct DatastorePlugin
92/** 92/**
93 * Linked list of active reservations. 93 * Linked list of active reservations.
94 */ 94 */
95struct ReservationList 95struct ReservationList
96{ 96{
97 97
98 /** 98 /**
@@ -161,7 +161,7 @@ static unsigned long long cache_size;
161 * How much space have we currently reserved? 161 * How much space have we currently reserved?
162 */ 162 */
163static unsigned long long reserved; 163static unsigned long long reserved;
164 164
165/** 165/**
166 * How much data are we currently storing 166 * How much data are we currently storing
167 * in the database? 167 * in the database?
@@ -202,13 +202,10 @@ static struct GNUNET_STATISTICS_Handle *stats;
202 * Synchronize our utilization statistics with the 202 * Synchronize our utilization statistics with the
203 * statistics service. 203 * statistics service.
204 */ 204 */
205static void 205static void
206sync_stats () 206sync_stats ()
207{ 207{
208 GNUNET_STATISTICS_set (stats, 208 GNUNET_STATISTICS_set (stats, QUOTA_STAT_NAME, payload, GNUNET_YES);
209 QUOTA_STAT_NAME,
210 payload,
211 GNUNET_YES);
212 lastSync = 0; 209 lastSync = 0;
213} 210}
214 211
@@ -217,24 +214,24 @@ sync_stats ()
217/** 214/**
218 * Context for transmitting replies to clients. 215 * Context for transmitting replies to clients.
219 */ 216 */
220struct TransmitCallbackContext 217struct TransmitCallbackContext
221{ 218{
222 219
223 /** 220 /**
224 * We keep these in a doubly-linked list (for cleanup). 221 * We keep these in a doubly-linked list (for cleanup).
225 */ 222 */
226 struct TransmitCallbackContext *next; 223 struct TransmitCallbackContext *next;
227 224
228 /** 225 /**
229 * We keep these in a doubly-linked list (for cleanup). 226 * We keep these in a doubly-linked list (for cleanup).
230 */ 227 */
231 struct TransmitCallbackContext *prev; 228 struct TransmitCallbackContext *prev;
232 229
233 /** 230 /**
234 * The message that we're asked to transmit. 231 * The message that we're asked to transmit.
235 */ 232 */
236 struct GNUNET_MessageHeader *msg; 233 struct GNUNET_MessageHeader *msg;
237 234
238 /** 235 /**
239 * Handle for the transmission request. 236 * Handle for the transmission request.
240 */ 237 */
@@ -247,7 +244,7 @@ struct TransmitCallbackContext
247 244
248}; 245};
249 246
250 247
251/** 248/**
252 * Head of the doubly-linked list (for cleanup). 249 * Head of the doubly-linked list (for cleanup).
253 */ 250 */
@@ -278,10 +275,9 @@ static struct GNUNET_STATISTICS_GetHandle *stat_get;
278 * 275 *
279 * @param cls not used 276 * @param cls not used
280 * @param tc task context 277 * @param tc task context
281 */ 278 */
282static void 279static void
283delete_expired (void *cls, 280delete_expired (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
284 const struct GNUNET_SCHEDULER_TaskContext *tc);
285 281
286 282
287/** 283/**
@@ -304,55 +300,46 @@ delete_expired (void *cls,
304 * (continue on call to "next", of course), 300 * (continue on call to "next", of course),
305 * GNUNET_NO to delete the item and continue (if supported) 301 * GNUNET_NO to delete the item and continue (if supported)
306 */ 302 */
307static int 303static int
308expired_processor (void *cls, 304expired_processor (void *cls,
309 const GNUNET_HashCode * key, 305 const GNUNET_HashCode * key,
310 uint32_t size, 306 uint32_t size,
311 const void *data, 307 const void *data,
312 enum GNUNET_BLOCK_Type type, 308 enum GNUNET_BLOCK_Type type,
313 uint32_t priority, 309 uint32_t priority,
314 uint32_t anonymity, 310 uint32_t anonymity,
315 struct GNUNET_TIME_Absolute 311 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
316 expiration,
317 uint64_t uid)
318{ 312{
319 struct GNUNET_TIME_Absolute now; 313 struct GNUNET_TIME_Absolute now;
320 314
321 if (key == NULL) 315 if (key == NULL)
322 { 316 {
323 expired_kill_task 317 expired_kill_task
324 = GNUNET_SCHEDULER_add_delayed (MAX_EXPIRE_DELAY, 318 = GNUNET_SCHEDULER_add_delayed (MAX_EXPIRE_DELAY,
325 &delete_expired, 319 &delete_expired, NULL);
326 NULL); 320 return GNUNET_SYSERR;
327 return GNUNET_SYSERR; 321 }
328 }
329 now = GNUNET_TIME_absolute_get (); 322 now = GNUNET_TIME_absolute_get ();
330 if (expiration.abs_value > now.abs_value) 323 if (expiration.abs_value > now.abs_value)
331 { 324 {
332 /* finished processing */ 325 /* finished processing */
333 expired_kill_task 326 expired_kill_task
334 = GNUNET_SCHEDULER_add_delayed (MAX_EXPIRE_DELAY, 327 = GNUNET_SCHEDULER_add_delayed (MAX_EXPIRE_DELAY,
335 &delete_expired, 328 &delete_expired, NULL);
336 NULL); 329 return GNUNET_SYSERR;
337 return GNUNET_SYSERR; 330 }
338 }
339#if DEBUG_DATASTORE 331#if DEBUG_DATASTORE
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 "Deleting content `%s' of type %u that expired %llu ms ago\n", 333 "Deleting content `%s' of type %u that expired %llu ms ago\n",
342 GNUNET_h2s (key), 334 GNUNET_h2s (key),
343 type, 335 type,
344 (unsigned long long) (now.abs_value - expiration.abs_value)); 336 (unsigned long long) (now.abs_value - expiration.abs_value));
345#endif 337#endif
346 GNUNET_STATISTICS_update (stats, 338 GNUNET_STATISTICS_update (stats,
347 gettext_noop ("# bytes expired"), 339 gettext_noop ("# bytes expired"), size, GNUNET_YES);
348 size, 340 GNUNET_CONTAINER_bloomfilter_remove (filter, key);
349 GNUNET_YES); 341 expired_kill_task
350 GNUNET_CONTAINER_bloomfilter_remove (filter, 342 = GNUNET_SCHEDULER_add_delayed (MIN_EXPIRE_DELAY, &delete_expired, NULL);
351 key);
352 expired_kill_task
353 = GNUNET_SCHEDULER_add_delayed (MIN_EXPIRE_DELAY,
354 &delete_expired,
355 NULL);
356 return GNUNET_NO; 343 return GNUNET_NO;
357} 344}
358 345
@@ -365,15 +352,12 @@ expired_processor (void *cls,
365 * 352 *
366 * @param cls not used 353 * @param cls not used
367 * @param tc task context 354 * @param tc task context
368 */ 355 */
369static void 356static void
370delete_expired (void *cls, 357delete_expired (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
371 const struct GNUNET_SCHEDULER_TaskContext *tc)
372{ 358{
373 expired_kill_task = GNUNET_SCHEDULER_NO_TASK; 359 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
374 plugin->api->get_expiration (plugin->api->cls, 360 plugin->api->get_expiration (plugin->api->cls, &expired_processor, NULL);
375 &expired_processor,
376 NULL);
377} 361}
378 362
379 363
@@ -396,39 +380,34 @@ delete_expired (void *cls,
396 * (continue on call to "next", of course), 380 * (continue on call to "next", of course),
397 * GNUNET_NO to delete the item and continue (if supported) 381 * GNUNET_NO to delete the item and continue (if supported)
398 */ 382 */
399static int 383static int
400quota_processor (void *cls, 384quota_processor (void *cls,
401 const GNUNET_HashCode * key, 385 const GNUNET_HashCode * key,
402 uint32_t size, 386 uint32_t size,
403 const void *data, 387 const void *data,
404 enum GNUNET_BLOCK_Type type, 388 enum GNUNET_BLOCK_Type type,
405 uint32_t priority, 389 uint32_t priority,
406 uint32_t anonymity, 390 uint32_t anonymity,
407 struct GNUNET_TIME_Absolute expiration, 391 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
408 uint64_t uid)
409{ 392{
410 unsigned long long *need = cls; 393 unsigned long long *need = cls;
411 394
412 if (NULL == key) 395 if (NULL == key)
413 return GNUNET_SYSERR; 396 return GNUNET_SYSERR;
414#if DEBUG_DATASTORE 397#if DEBUG_DATASTORE
415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
416 "Deleting %llu bytes of low-priority content `%s' of type %u (still trying to free another %llu bytes)\n", 399 "Deleting %llu bytes of low-priority content `%s' of type %u (still trying to free another %llu bytes)\n",
417 (unsigned long long) (size + GNUNET_DATASTORE_ENTRY_OVERHEAD), 400 (unsigned long long) (size + GNUNET_DATASTORE_ENTRY_OVERHEAD),
418 GNUNET_h2s (key), 401 GNUNET_h2s (key), type, *need);
419 type,
420 *need);
421#endif 402#endif
422 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need) 403 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
423 *need = 0; 404 *need = 0;
424 else 405 else
425 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD; 406 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
426 GNUNET_STATISTICS_update (stats, 407 GNUNET_STATISTICS_update (stats,
427 gettext_noop ("# bytes purged (low-priority)"), 408 gettext_noop ("# bytes purged (low-priority)"),
428 size, 409 size, GNUNET_YES);
429 GNUNET_YES); 410 GNUNET_CONTAINER_bloomfilter_remove (filter, key);
430 GNUNET_CONTAINER_bloomfilter_remove (filter,
431 key);
432 return GNUNET_NO; 411 return GNUNET_NO;
433} 412}
434 413
@@ -452,18 +431,14 @@ manage_space (unsigned long long need)
452 431
453#if DEBUG_DATASTORE 432#if DEBUG_DATASTORE
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 "Asked to free up %llu bytes of cache space\n", 434 "Asked to free up %llu bytes of cache space\n", need);
456 need);
457#endif 435#endif
458 last = 0; 436 last = 0;
459 while ( (need > 0) && 437 while ((need > 0) && (last != need))
460 (last != need) ) 438 {
461 { 439 last = need;
462 last = need; 440 plugin->api->get_expiration (plugin->api->cls, &quota_processor, &need);
463 plugin->api->get_expiration (plugin->api->cls, 441 }
464 &quota_processor,
465 &need);
466 }
467} 442}
468 443
469 444
@@ -479,27 +454,24 @@ manage_space (unsigned long long need)
479 * @return number of bytes written to buf 454 * @return number of bytes written to buf
480 */ 455 */
481static size_t 456static size_t
482transmit_callback (void *cls, 457transmit_callback (void *cls, size_t size, void *buf)
483 size_t size, void *buf)
484{ 458{
485 struct TransmitCallbackContext *tcc = cls; 459 struct TransmitCallbackContext *tcc = cls;
486 size_t msize; 460 size_t msize;
487 461
488 tcc->th = NULL; 462 tcc->th = NULL;
489 GNUNET_CONTAINER_DLL_remove (tcc_head, 463 GNUNET_CONTAINER_DLL_remove (tcc_head, tcc_tail, tcc);
490 tcc_tail, 464 msize = ntohs (tcc->msg->size);
491 tcc);
492 msize = ntohs(tcc->msg->size);
493 if (size == 0) 465 if (size == 0)
494 { 466 {
495 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 467 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
496 _("Transmission to client failed!\n")); 468 _("Transmission to client failed!\n"));
497 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR); 469 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
498 GNUNET_SERVER_client_drop (tcc->client); 470 GNUNET_SERVER_client_drop (tcc->client);
499 GNUNET_free (tcc->msg); 471 GNUNET_free (tcc->msg);
500 GNUNET_free (tcc); 472 GNUNET_free (tcc);
501 return 0; 473 return 0;
502 } 474 }
503 GNUNET_assert (size >= msize); 475 GNUNET_assert (size >= msize);
504 memcpy (buf, tcc->msg, msize); 476 memcpy (buf, tcc->msg, msize);
505 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK); 477 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
@@ -517,41 +489,37 @@ transmit_callback (void *cls,
517 * @param msg message to transmit, will be freed! 489 * @param msg message to transmit, will be freed!
518 */ 490 */
519static void 491static void
520transmit (struct GNUNET_SERVER_Client *client, 492transmit (struct GNUNET_SERVER_Client *client, struct GNUNET_MessageHeader *msg)
521 struct GNUNET_MessageHeader *msg)
522{ 493{
523 struct TransmitCallbackContext *tcc; 494 struct TransmitCallbackContext *tcc;
524 495
525 if (GNUNET_YES == cleaning_done) 496 if (GNUNET_YES == cleaning_done)
526 { 497 {
527#if DEBUG_DATASTORE 498#if DEBUG_DATASTORE
528 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 499 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
529 "Shutdown in progress, aborting transmission.\n"); 500 "Shutdown in progress, aborting transmission.\n");
530#endif 501#endif
531 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
532 GNUNET_free (msg); 503 GNUNET_free (msg);
533 return; 504 return;
534 } 505 }
535 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext)); 506 tcc = GNUNET_malloc (sizeof (struct TransmitCallbackContext));
536 tcc->msg = msg; 507 tcc->msg = msg;
537 tcc->client = client; 508 tcc->client = client;
538 if (NULL == 509 if (NULL ==
539 (tcc->th = GNUNET_SERVER_notify_transmit_ready (client, 510 (tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
540 ntohs(msg->size), 511 ntohs (msg->size),
541 GNUNET_TIME_UNIT_FOREVER_REL, 512 GNUNET_TIME_UNIT_FOREVER_REL,
542 &transmit_callback, 513 &transmit_callback, tcc)))
543 tcc))) 514 {
544 { 515 GNUNET_break (0);
545 GNUNET_break (0); 516 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
546 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 517 GNUNET_free (msg);
547 GNUNET_free (msg); 518 GNUNET_free (tcc);
548 GNUNET_free (tcc); 519 return;
549 return; 520 }
550 }
551 GNUNET_SERVER_client_keep (client); 521 GNUNET_SERVER_client_keep (client);
552 GNUNET_CONTAINER_DLL_insert (tcc_head, 522 GNUNET_CONTAINER_DLL_insert (tcc_head, tcc_tail, tcc);
553 tcc_tail,
554 tcc);
555} 523}
556 524
557 525
@@ -563,27 +531,23 @@ transmit (struct GNUNET_SERVER_Client *client,
563 * @param msg optional error message (can be NULL) 531 * @param msg optional error message (can be NULL)
564 */ 532 */
565static void 533static void
566transmit_status (struct GNUNET_SERVER_Client *client, 534transmit_status (struct GNUNET_SERVER_Client *client, int code, const char *msg)
567 int code,
568 const char *msg)
569{ 535{
570 struct StatusMessage *sm; 536 struct StatusMessage *sm;
571 size_t slen; 537 size_t slen;
572 538
573#if DEBUG_DATASTORE 539#if DEBUG_DATASTORE
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 "Transmitting `%s' message with value %d and message `%s'\n", 541 "Transmitting `%s' message with value %d and message `%s'\n",
576 "STATUS", 542 "STATUS", code, msg != NULL ? msg : "(none)");
577 code,
578 msg != NULL ? msg : "(none)");
579#endif 543#endif
580 slen = (msg == NULL) ? 0 : strlen(msg) + 1; 544 slen = (msg == NULL) ? 0 : strlen (msg) + 1;
581 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen); 545 sm = GNUNET_malloc (sizeof (struct StatusMessage) + slen);
582 sm->header.size = htons(sizeof(struct StatusMessage) + slen); 546 sm->header.size = htons (sizeof (struct StatusMessage) + slen);
583 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS); 547 sm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
584 sm->status = htonl(code); 548 sm->status = htonl (code);
585 if (slen > 0) 549 if (slen > 0)
586 memcpy (&sm[1], msg, slen); 550 memcpy (&sm[1], msg, slen);
587 transmit (client, &sm->header); 551 transmit (client, &sm->header);
588} 552}
589 553
@@ -609,62 +573,59 @@ transmit_status (struct GNUNET_SERVER_Client *client,
609 */ 573 */
610static int 574static int
611transmit_item (void *cls, 575transmit_item (void *cls,
612 const GNUNET_HashCode * key, 576 const GNUNET_HashCode * key,
613 uint32_t size, 577 uint32_t size,
614 const void *data, 578 const void *data,
615 enum GNUNET_BLOCK_Type type, 579 enum GNUNET_BLOCK_Type type,
616 uint32_t priority, 580 uint32_t priority,
617 uint32_t anonymity, 581 uint32_t anonymity,
618 struct GNUNET_TIME_Absolute 582 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
619 expiration, uint64_t uid)
620{ 583{
621 struct GNUNET_SERVER_Client *client = cls; 584 struct GNUNET_SERVER_Client *client = cls;
622 struct GNUNET_MessageHeader *end; 585 struct GNUNET_MessageHeader *end;
623 struct DataMessage *dm; 586 struct DataMessage *dm;
624 587
625 if (key == NULL) 588 if (key == NULL)
626 { 589 {
627 /* transmit 'DATA_END' */ 590 /* transmit 'DATA_END' */
628#if DEBUG_DATASTORE 591#if DEBUG_DATASTORE
629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
630 "Transmitting `%s' message\n", 593 "Transmitting `%s' message\n", "DATA_END");
631 "DATA_END");
632#endif 594#endif
633 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader)); 595 end = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader));
634 end->size = htons(sizeof(struct GNUNET_MessageHeader)); 596 end->size = htons (sizeof (struct GNUNET_MessageHeader));
635 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END); 597 end->type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
636 transmit (client, end); 598 transmit (client, end);
637 GNUNET_SERVER_client_drop (client); 599 GNUNET_SERVER_client_drop (client);
638 return GNUNET_OK; 600 return GNUNET_OK;
639 } 601 }
640 GNUNET_assert (sizeof (struct DataMessage) + size < GNUNET_SERVER_MAX_MESSAGE_SIZE); 602 GNUNET_assert (sizeof (struct DataMessage) + size <
641 dm = GNUNET_malloc (sizeof(struct DataMessage) + size); 603 GNUNET_SERVER_MAX_MESSAGE_SIZE);
642 dm->header.size = htons(sizeof(struct DataMessage) + size); 604 dm = GNUNET_malloc (sizeof (struct DataMessage) + size);
643 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA); 605 dm->header.size = htons (sizeof (struct DataMessage) + size);
644 dm->rid = htonl(0); 606 dm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
645 dm->size = htonl(size); 607 dm->rid = htonl (0);
646 dm->type = htonl(type); 608 dm->size = htonl (size);
647 dm->priority = htonl(priority); 609 dm->type = htonl (type);
648 dm->anonymity = htonl(anonymity); 610 dm->priority = htonl (priority);
611 dm->anonymity = htonl (anonymity);
649 dm->replication = htonl (0); 612 dm->replication = htonl (0);
650 dm->reserved = htonl (0); 613 dm->reserved = htonl (0);
651 dm->expiration = GNUNET_TIME_absolute_hton(expiration); 614 dm->expiration = GNUNET_TIME_absolute_hton (expiration);
652 dm->uid = GNUNET_htonll(uid); 615 dm->uid = GNUNET_htonll (uid);
653 dm->key = *key; 616 dm->key = *key;
654 memcpy (&dm[1], data, size); 617 memcpy (&dm[1], data, size);
655#if DEBUG_DATASTORE 618#if DEBUG_DATASTORE
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
657 "Transmitting `%s' message for `%s' of type %u with expiration %llu (now: %llu)\n", 620 "Transmitting `%s' message for `%s' of type %u with expiration %llu (now: %llu)\n",
658 "DATA", 621 "DATA",
659 GNUNET_h2s (key), 622 GNUNET_h2s (key),
660 type, 623 type,
661 (unsigned long long) expiration.abs_value, 624 (unsigned long long) expiration.abs_value,
662 (unsigned long long) GNUNET_TIME_absolute_get ().abs_value); 625 (unsigned long long) GNUNET_TIME_absolute_get ().abs_value);
663#endif 626#endif
664 GNUNET_STATISTICS_update (stats, 627 GNUNET_STATISTICS_update (stats,
665 gettext_noop ("# results found"), 628 gettext_noop ("# results found"), 1, GNUNET_NO);
666 1,
667 GNUNET_NO);
668 transmit (client, &dm->header); 629 transmit (client, &dm->header);
669 GNUNET_SERVER_client_drop (client); 630 GNUNET_SERVER_client_drop (client);
670 return GNUNET_OK; 631 return GNUNET_OK;
@@ -680,15 +641,15 @@ transmit_item (void *cls,
680 */ 641 */
681static void 642static void
682handle_reserve (void *cls, 643handle_reserve (void *cls,
683 struct GNUNET_SERVER_Client *client, 644 struct GNUNET_SERVER_Client *client,
684 const struct GNUNET_MessageHeader *message) 645 const struct GNUNET_MessageHeader *message)
685{ 646{
686 /** 647 /**
687 * Static counter to produce reservation identifiers. 648 * Static counter to produce reservation identifiers.
688 */ 649 */
689 static int reservation_gen; 650 static int reservation_gen;
690 651
691 const struct ReserveMessage *msg = (const struct ReserveMessage*) message; 652 const struct ReserveMessage *msg = (const struct ReserveMessage *) message;
692 struct ReservationList *e; 653 struct ReservationList *e;
693 unsigned long long used; 654 unsigned long long used;
694 unsigned long long req; 655 unsigned long long req;
@@ -696,51 +657,48 @@ handle_reserve (void *cls,
696 uint32_t entries; 657 uint32_t entries;
697 658
698#if DEBUG_DATASTORE 659#if DEBUG_DATASTORE
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing `%s' request\n", "RESERVE");
700 "Processing `%s' request\n",
701 "RESERVE");
702#endif 661#endif
703 amount = GNUNET_ntohll(msg->amount); 662 amount = GNUNET_ntohll (msg->amount);
704 entries = ntohl(msg->entries); 663 entries = ntohl (msg->entries);
705 used = payload + reserved; 664 used = payload + reserved;
706 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries; 665 req =
666 amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
707 if (used + req > quota) 667 if (used + req > quota)
668 {
669 if (quota < used)
670 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
671 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
672 _
673 ("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
674 quota - used, "RESERVE", req);
675 if (cache_size < req)
708 { 676 {
709 if (quota < used) 677 /* TODO: document this in the FAQ; essentially, if this
710 used = quota; /* cheat a bit for error message (to avoid negative numbers) */ 678 * message happens, the insertion request could be blocked
679 * by less-important content from migration because it is
680 * larger than 1/8th of the overall available space, and
681 * we only reserve 1/8th for "fresh" insertions */
711 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 682 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
712 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"), 683 _
713 quota - used, 684 ("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
714 "RESERVE", 685 req, cache_size);
715 req); 686 transmit_status (client, 0,
716 if (cache_size < req) 687 gettext_noop
717 { 688 ("Insufficient space to satisfy request and "
718 /* TODO: document this in the FAQ; essentially, if this 689 "requested amount is larger than cache size"));
719 message happens, the insertion request could be blocked
720 by less-important content from migration because it is
721 larger than 1/8th of the overall available space, and
722 we only reserve 1/8th for "fresh" insertions */
723 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
724 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
725 req,
726 cache_size);
727 transmit_status (client, 0,
728 gettext_noop ("Insufficient space to satisfy request and "
729 "requested amount is larger than cache size"));
730 }
731 else
732 {
733 transmit_status (client, 0,
734 gettext_noop ("Insufficient space to satisfy request"));
735 }
736 return;
737 } 690 }
691 else
692 {
693 transmit_status (client, 0,
694 gettext_noop ("Insufficient space to satisfy request"));
695 }
696 return;
697 }
738 reserved += req; 698 reserved += req;
739 GNUNET_STATISTICS_set (stats, 699 GNUNET_STATISTICS_set (stats,
740 gettext_noop ("# reserved"), 700 gettext_noop ("# reserved"), reserved, GNUNET_NO);
741 reserved, 701 e = GNUNET_malloc (sizeof (struct ReservationList));
742 GNUNET_NO);
743 e = GNUNET_malloc (sizeof(struct ReservationList));
744 e->next = reservations; 702 e->next = reservations;
745 reservations = e; 703 reservations = e;
746 e->client = client; 704 e->client = client;
@@ -748,7 +706,7 @@ handle_reserve (void *cls,
748 e->entries = entries; 706 e->entries = entries;
749 e->rid = ++reservation_gen; 707 e->rid = ++reservation_gen;
750 if (reservation_gen < 0) 708 if (reservation_gen < 0)
751 reservation_gen = 0; /* wrap around */ 709 reservation_gen = 0; /* wrap around */
752 transmit_status (client, e->rid, NULL); 710 transmit_status (client, e->rid, NULL);
753} 711}
754 712
@@ -762,52 +720,53 @@ handle_reserve (void *cls,
762 */ 720 */
763static void 721static void
764handle_release_reserve (void *cls, 722handle_release_reserve (void *cls,
765 struct GNUNET_SERVER_Client *client, 723 struct GNUNET_SERVER_Client *client,
766 const struct GNUNET_MessageHeader *message) 724 const struct GNUNET_MessageHeader *message)
767{ 725{
768 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message; 726 const struct ReleaseReserveMessage *msg =
727 (const struct ReleaseReserveMessage *) message;
769 struct ReservationList *pos; 728 struct ReservationList *pos;
770 struct ReservationList *prev; 729 struct ReservationList *prev;
771 struct ReservationList *next; 730 struct ReservationList *next;
772 int rid = ntohl(msg->rid); 731 int rid = ntohl (msg->rid);
773 unsigned long long rem; 732 unsigned long long rem;
774 733
775#if DEBUG_DATASTORE 734#if DEBUG_DATASTORE
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
777 "Processing `%s' request\n", 736 "Processing `%s' request\n", "RELEASE_RESERVE");
778 "RELEASE_RESERVE");
779#endif 737#endif
780 next = reservations; 738 next = reservations;
781 prev = NULL; 739 prev = NULL;
782 while (NULL != (pos = next)) 740 while (NULL != (pos = next))
741 {
742 next = pos->next;
743 if (rid == pos->rid)
783 { 744 {
784 next = pos->next; 745 if (prev == NULL)
785 if (rid == pos->rid) 746 reservations = next;
786 { 747 else
787 if (prev == NULL) 748 prev->next = next;
788 reservations = next; 749 rem =
789 else 750 pos->amount +
790 prev->next = next; 751 ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
791 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries; 752 GNUNET_assert (reserved >= rem);
792 GNUNET_assert (reserved >= rem); 753 reserved -= rem;
793 reserved -= rem; 754 GNUNET_STATISTICS_set (stats,
794 GNUNET_STATISTICS_set (stats, 755 gettext_noop ("# reserved"), reserved, GNUNET_NO);
795 gettext_noop ("# reserved"),
796 reserved,
797 GNUNET_NO);
798#if DEBUG_DATASTORE 756#if DEBUG_DATASTORE
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 "Returning %llu remaining reserved bytes to storage pool\n", 758 "Returning %llu remaining reserved bytes to storage pool\n",
801 rem); 759 rem);
802#endif 760#endif
803 GNUNET_free (pos); 761 GNUNET_free (pos);
804 transmit_status (client, GNUNET_OK, NULL); 762 transmit_status (client, GNUNET_OK, NULL);
805 return; 763 return;
806 }
807 prev = pos;
808 } 764 }
765 prev = pos;
766 }
809 GNUNET_break (0); 767 GNUNET_break (0);
810 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation")); 768 transmit_status (client, GNUNET_SYSERR,
769 gettext_noop ("Could not find matching reservation"));
811} 770}
812 771
813 772
@@ -823,19 +782,19 @@ check_data (const struct GNUNET_MessageHeader *message)
823 uint32_t dsize; 782 uint32_t dsize;
824 const struct DataMessage *dm; 783 const struct DataMessage *dm;
825 784
826 size = ntohs(message->size); 785 size = ntohs (message->size);
827 if (size < sizeof(struct DataMessage)) 786 if (size < sizeof (struct DataMessage))
828 { 787 {
829 GNUNET_break (0); 788 GNUNET_break (0);
830 return NULL; 789 return NULL;
831 } 790 }
832 dm = (const struct DataMessage *) message; 791 dm = (const struct DataMessage *) message;
833 dsize = ntohl(dm->size); 792 dsize = ntohl (dm->size);
834 if (size != dsize + sizeof(struct DataMessage)) 793 if (size != dsize + sizeof (struct DataMessage))
835 { 794 {
836 GNUNET_break (0); 795 GNUNET_break (0);
837 return NULL; 796 return NULL;
838 } 797 }
839 return dm; 798 return dm;
840} 799}
841 800
@@ -853,8 +812,8 @@ struct PutContext
853 812
854#if ! HAVE_UNALIGNED_64_ACCESS 813#if ! HAVE_UNALIGNED_64_ACCESS
855 void *reserved; 814 void *reserved;
856#endif 815#endif
857 816
858 /* followed by the 'struct DataMessage' */ 817 /* followed by the 'struct DataMessage' */
859}; 818};
860 819
@@ -863,54 +822,46 @@ struct PutContext
863 * Actually put the data message. 822 * Actually put the data message.
864 */ 823 */
865static void 824static void
866execute_put (struct GNUNET_SERVER_Client *client, 825execute_put (struct GNUNET_SERVER_Client *client, const struct DataMessage *dm)
867 const struct DataMessage *dm)
868{ 826{
869 uint32_t size; 827 uint32_t size;
870 char *msg; 828 char *msg;
871 int ret; 829 int ret;
872 830
873 size = ntohl(dm->size); 831 size = ntohl (dm->size);
874 msg = NULL; 832 msg = NULL;
875 ret = plugin->api->put (plugin->api->cls, 833 ret = plugin->api->put (plugin->api->cls,
876 &dm->key, 834 &dm->key,
877 size, 835 size,
878 &dm[1], 836 &dm[1],
879 ntohl(dm->type), 837 ntohl (dm->type),
880 ntohl(dm->priority), 838 ntohl (dm->priority),
881 ntohl(dm->anonymity), 839 ntohl (dm->anonymity),
882 ntohl(dm->replication), 840 ntohl (dm->replication),
883 GNUNET_TIME_absolute_ntoh(dm->expiration), 841 GNUNET_TIME_absolute_ntoh (dm->expiration), &msg);
884 &msg);
885 if (GNUNET_OK == ret) 842 if (GNUNET_OK == ret)
886 { 843 {
887 GNUNET_STATISTICS_update (stats, 844 GNUNET_STATISTICS_update (stats,
888 gettext_noop ("# bytes stored"), 845 gettext_noop ("# bytes stored"),
889 size, 846 size, GNUNET_YES);
890 GNUNET_YES); 847 GNUNET_CONTAINER_bloomfilter_add (filter, &dm->key);
891 GNUNET_CONTAINER_bloomfilter_add (filter,
892 &dm->key);
893#if DEBUG_DATASTORE 848#if DEBUG_DATASTORE
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
895 "Successfully stored %u bytes of type %u under key `%s'\n", 850 "Successfully stored %u bytes of type %u under key `%s'\n",
896 size, 851 size, ntohl (dm->type), GNUNET_h2s (&dm->key));
897 ntohl(dm->type),
898 GNUNET_h2s (&dm->key));
899#endif 852#endif
900 } 853 }
901 transmit_status (client, 854 transmit_status (client, ret, msg);
902 ret,
903 msg);
904 GNUNET_free_non_null (msg); 855 GNUNET_free_non_null (msg);
905 if (quota - reserved - cache_size < payload) 856 if (quota - reserved - cache_size < payload)
906 { 857 {
907 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 858 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
908 _("Need %llu bytes more space (%llu allowed, using %llu)\n"), 859 _("Need %llu bytes more space (%llu allowed, using %llu)\n"),
909 (unsigned long long) size + GNUNET_DATASTORE_ENTRY_OVERHEAD, 860 (unsigned long long) size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
910 (unsigned long long) (quota - reserved - cache_size), 861 (unsigned long long) (quota - reserved - cache_size),
911 (unsigned long long) payload); 862 (unsigned long long) payload);
912 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD); 863 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
913 } 864 }
914} 865}
915 866
916 867
@@ -934,56 +885,51 @@ execute_put (struct GNUNET_SERVER_Client *client,
934 */ 885 */
935static int 886static int
936check_present (void *cls, 887check_present (void *cls,
937 const GNUNET_HashCode * key, 888 const GNUNET_HashCode * key,
938 uint32_t size, 889 uint32_t size,
939 const void *data, 890 const void *data,
940 enum GNUNET_BLOCK_Type type, 891 enum GNUNET_BLOCK_Type type,
941 uint32_t priority, 892 uint32_t priority,
942 uint32_t anonymity, 893 uint32_t anonymity,
943 struct GNUNET_TIME_Absolute 894 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
944 expiration, uint64_t uid)
945{ 895{
946 struct PutContext *pc = cls; 896 struct PutContext *pc = cls;
947 const struct DataMessage *dm; 897 const struct DataMessage *dm;
948 898
949 dm = (const struct DataMessage*) &pc[1]; 899 dm = (const struct DataMessage *) &pc[1];
950 if (key == NULL) 900 if (key == NULL)
951 { 901 {
952 execute_put (pc->client, dm); 902 execute_put (pc->client, dm);
953 GNUNET_SERVER_client_drop (pc->client); 903 GNUNET_SERVER_client_drop (pc->client);
954 GNUNET_free (pc); 904 GNUNET_free (pc);
955 return GNUNET_OK; 905 return GNUNET_OK;
956 } 906 }
957 if ( (GNUNET_BLOCK_TYPE_FS_DBLOCK == type) || 907 if ((GNUNET_BLOCK_TYPE_FS_DBLOCK == type) ||
958 (GNUNET_BLOCK_TYPE_FS_IBLOCK == type) || 908 (GNUNET_BLOCK_TYPE_FS_IBLOCK == type) ||
959 ( (size == ntohl(dm->size)) && 909 ((size == ntohl (dm->size)) && (0 == memcmp (&dm[1], data, size))))
960 (0 == memcmp (&dm[1], 910 {
961 data,
962 size)) ) )
963 {
964#if DEBUG_MYSQL 911#if DEBUG_MYSQL
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
966 "Result already present in datastore\n"); 913 "Result already present in datastore\n");
967#endif 914#endif
968 /* FIXME: change API to allow increasing 'replication' counter */ 915 /* FIXME: change API to allow increasing 'replication' counter */
969 if ( (ntohl (dm->priority) > 0) || 916 if ((ntohl (dm->priority) > 0) ||
970 (GNUNET_TIME_absolute_ntoh(dm->expiration).abs_value > 917 (GNUNET_TIME_absolute_ntoh (dm->expiration).abs_value >
971 expiration.abs_value) ) 918 expiration.abs_value))
972 plugin->api->update (plugin->api->cls, 919 plugin->api->update (plugin->api->cls,
973 uid, 920 uid,
974 (int32_t) ntohl(dm->priority), 921 (int32_t) ntohl (dm->priority),
975 GNUNET_TIME_absolute_ntoh(dm->expiration), 922 GNUNET_TIME_absolute_ntoh (dm->expiration), NULL);
976 NULL); 923 transmit_status (pc->client, GNUNET_NO, NULL);
977 transmit_status (pc->client, GNUNET_NO, NULL); 924 GNUNET_SERVER_client_drop (pc->client);
978 GNUNET_SERVER_client_drop (pc->client); 925 GNUNET_free (pc);
979 GNUNET_free (pc); 926 }
980 }
981 else 927 else
982 { 928 {
983 execute_put (pc->client, dm); 929 execute_put (pc->client, dm);
984 GNUNET_SERVER_client_drop (pc->client); 930 GNUNET_SERVER_client_drop (pc->client);
985 GNUNET_free (pc); 931 GNUNET_free (pc);
986 } 932 }
987 return GNUNET_OK; 933 return GNUNET_OK;
988} 934}
989 935
@@ -997,8 +943,8 @@ check_present (void *cls,
997 */ 943 */
998static void 944static void
999handle_put (void *cls, 945handle_put (void *cls,
1000 struct GNUNET_SERVER_Client *client, 946 struct GNUNET_SERVER_Client *client,
1001 const struct GNUNET_MessageHeader *message) 947 const struct GNUNET_MessageHeader *message)
1002{ 948{
1003 const struct DataMessage *dm = check_data (message); 949 const struct DataMessage *dm = check_data (message);
1004 int rid; 950 int rid;
@@ -1007,59 +953,50 @@ handle_put (void *cls,
1007 GNUNET_HashCode vhash; 953 GNUNET_HashCode vhash;
1008 uint32_t size; 954 uint32_t size;
1009 955
1010 if ( (dm == NULL) || 956 if ((dm == NULL) || (ntohl (dm->type) == 0))
1011 (ntohl(dm->type) == 0) ) 957 {
1012 { 958 GNUNET_break (0);
1013 GNUNET_break (0); 959 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 960 return;
1015 return; 961 }
1016 }
1017#if DEBUG_DATASTORE 962#if DEBUG_DATASTORE
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019 "Processing `%s' request for `%s' of type %u\n", 964 "Processing `%s' request for `%s' of type %u\n",
1020 "PUT", 965 "PUT", GNUNET_h2s (&dm->key), ntohl (dm->type));
1021 GNUNET_h2s (&dm->key),
1022 ntohl (dm->type));
1023#endif 966#endif
1024 rid = ntohl(dm->rid); 967 rid = ntohl (dm->rid);
1025 size = ntohl(dm->size); 968 size = ntohl (dm->size);
1026 if (rid > 0) 969 if (rid > 0)
970 {
971 pos = reservations;
972 while ((NULL != pos) && (rid != pos->rid))
973 pos = pos->next;
974 GNUNET_break (pos != NULL);
975 if (NULL != pos)
1027 { 976 {
1028 pos = reservations; 977 GNUNET_break (pos->entries > 0);
1029 while ( (NULL != pos) && 978 GNUNET_break (pos->amount >= size);
1030 (rid != pos->rid) ) 979 pos->entries--;
1031 pos = pos->next; 980 pos->amount -= size;
1032 GNUNET_break (pos != NULL); 981 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
1033 if (NULL != pos) 982 GNUNET_STATISTICS_set (stats,
1034 { 983 gettext_noop ("# reserved"), reserved, GNUNET_NO);
1035 GNUNET_break (pos->entries > 0);
1036 GNUNET_break (pos->amount >= size);
1037 pos->entries--;
1038 pos->amount -= size;
1039 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
1040 GNUNET_STATISTICS_set (stats,
1041 gettext_noop ("# reserved"),
1042 reserved,
1043 GNUNET_NO);
1044 }
1045 }
1046 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (filter,
1047 &dm->key))
1048 {
1049 GNUNET_CRYPTO_hash (&dm[1], size, &vhash);
1050 pc = GNUNET_malloc (sizeof (struct PutContext) + size + sizeof (struct DataMessage));
1051 pc->client = client;
1052 GNUNET_SERVER_client_keep (client);
1053 memcpy (&pc[1], dm, size + sizeof (struct DataMessage));
1054 plugin->api->get_key (plugin->api->cls,
1055 0,
1056 &dm->key,
1057 &vhash,
1058 ntohl (dm->type),
1059 &check_present,
1060 pc);
1061 return;
1062 } 984 }
985 }
986 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (filter, &dm->key))
987 {
988 GNUNET_CRYPTO_hash (&dm[1], size, &vhash);
989 pc = GNUNET_malloc (sizeof (struct PutContext) + size +
990 sizeof (struct DataMessage));
991 pc->client = client;
992 GNUNET_SERVER_client_keep (client);
993 memcpy (&pc[1], dm, size + sizeof (struct DataMessage));
994 plugin->api->get_key (plugin->api->cls,
995 0,
996 &dm->key,
997 &vhash, ntohl (dm->type), &check_present, pc);
998 return;
999 }
1063 execute_put (client, dm); 1000 execute_put (client, dm);
1064} 1001}
1065 1002
@@ -1073,60 +1010,52 @@ handle_put (void *cls,
1073 */ 1010 */
1074static void 1011static void
1075handle_get (void *cls, 1012handle_get (void *cls,
1076 struct GNUNET_SERVER_Client *client, 1013 struct GNUNET_SERVER_Client *client,
1077 const struct GNUNET_MessageHeader *message) 1014 const struct GNUNET_MessageHeader *message)
1078{ 1015{
1079 const struct GetMessage *msg; 1016 const struct GetMessage *msg;
1080 uint16_t size; 1017 uint16_t size;
1081 1018
1082 size = ntohs(message->size); 1019 size = ntohs (message->size);
1083 if ( (size != sizeof(struct GetMessage)) && 1020 if ((size != sizeof (struct GetMessage)) &&
1084 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) ) 1021 (size != sizeof (struct GetMessage) - sizeof (GNUNET_HashCode)))
1085 { 1022 {
1086 GNUNET_break (0); 1023 GNUNET_break (0);
1087 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 1024 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1088 return; 1025 return;
1089 } 1026 }
1090 msg = (const struct GetMessage*) message; 1027 msg = (const struct GetMessage *) message;
1091#if DEBUG_DATASTORE 1028#if DEBUG_DATASTORE
1092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1093 "Processing `%s' request for `%s' of type %u\n", 1030 "Processing `%s' request for `%s' of type %u\n",
1094 "GET", 1031 "GET", GNUNET_h2s (&msg->key), ntohl (msg->type));
1095 GNUNET_h2s (&msg->key),
1096 ntohl (msg->type));
1097#endif 1032#endif
1098 GNUNET_STATISTICS_update (stats, 1033 GNUNET_STATISTICS_update (stats,
1099 gettext_noop ("# GET requests received"), 1034 gettext_noop ("# GET requests received"),
1100 1, 1035 1, GNUNET_NO);
1101 GNUNET_NO);
1102 GNUNET_SERVER_client_keep (client); 1036 GNUNET_SERVER_client_keep (client);
1103 if ( (size == sizeof(struct GetMessage)) && 1037 if ((size == sizeof (struct GetMessage)) &&
1104 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter, 1038 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter, &msg->key)))
1105 &msg->key)) ) 1039 {
1106 { 1040 /* don't bother database... */
1107 /* don't bother database... */
1108#if DEBUG_DATASTORE 1041#if DEBUG_DATASTORE
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110 "Empty result set for `%s' request for `%s' (bloomfilter).\n", 1043 "Empty result set for `%s' request for `%s' (bloomfilter).\n",
1111 "GET", 1044 "GET", GNUNET_h2s (&msg->key));
1112 GNUNET_h2s (&msg->key)); 1045#endif
1113#endif 1046 GNUNET_STATISTICS_update (stats,
1114 GNUNET_STATISTICS_update (stats, 1047 gettext_noop
1115 gettext_noop ("# requests filtered by bloomfilter"), 1048 ("# requests filtered by bloomfilter"), 1,
1116 1, 1049 GNUNET_NO);
1117 GNUNET_NO); 1050 transmit_item (client, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1118 transmit_item (client, 1051 0);
1119 NULL, 0, NULL, 0, 0, 0, 1052 return;
1120 GNUNET_TIME_UNIT_ZERO_ABS, 0); 1053 }
1121 return;
1122 }
1123 plugin->api->get_key (plugin->api->cls, 1054 plugin->api->get_key (plugin->api->cls,
1124 GNUNET_ntohll (msg->offset), 1055 GNUNET_ntohll (msg->offset),
1125 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL), 1056 ((size ==
1126 NULL, 1057 sizeof (struct GetMessage)) ? &msg->key : NULL), NULL,
1127 ntohl(msg->type), 1058 ntohl (msg->type), &transmit_item, client);
1128 &transmit_item,
1129 client);
1130} 1059}
1131 1060
1132 1061
@@ -1139,30 +1068,28 @@ handle_get (void *cls,
1139 */ 1068 */
1140static void 1069static void
1141handle_update (void *cls, 1070handle_update (void *cls,
1142 struct GNUNET_SERVER_Client *client, 1071 struct GNUNET_SERVER_Client *client,
1143 const struct GNUNET_MessageHeader *message) 1072 const struct GNUNET_MessageHeader *message)
1144{ 1073{
1145 const struct UpdateMessage *msg; 1074 const struct UpdateMessage *msg;
1146 int ret; 1075 int ret;
1147 char *emsg; 1076 char *emsg;
1148 1077
1149 GNUNET_STATISTICS_update (stats, 1078 GNUNET_STATISTICS_update (stats,
1150 gettext_noop ("# UPDATE requests received"), 1079 gettext_noop ("# UPDATE requests received"),
1151 1, 1080 1, GNUNET_NO);
1152 GNUNET_NO); 1081 msg = (const struct UpdateMessage *) message;
1153 msg = (const struct UpdateMessage*) message;
1154 emsg = NULL; 1082 emsg = NULL;
1155#if DEBUG_DATASTORE 1083#if DEBUG_DATASTORE
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1157 "Processing `%s' request for %llu\n", 1085 "Processing `%s' request for %llu\n",
1158 "UPDATE", 1086 "UPDATE", (unsigned long long) GNUNET_ntohll (msg->uid));
1159 (unsigned long long) GNUNET_ntohll (msg->uid));
1160#endif 1087#endif
1161 ret = plugin->api->update (plugin->api->cls, 1088 ret = plugin->api->update (plugin->api->cls,
1162 GNUNET_ntohll(msg->uid), 1089 GNUNET_ntohll (msg->uid),
1163 (int32_t) ntohl(msg->priority), 1090 (int32_t) ntohl (msg->priority),
1164 GNUNET_TIME_absolute_ntoh(msg->expiration), 1091 GNUNET_TIME_absolute_ntoh (msg->expiration),
1165 &emsg); 1092 &emsg);
1166 transmit_status (client, ret, emsg); 1093 transmit_status (client, ret, emsg);
1167 GNUNET_free_non_null (emsg); 1094 GNUNET_free_non_null (emsg);
1168} 1095}
@@ -1177,22 +1104,19 @@ handle_update (void *cls,
1177 */ 1104 */
1178static void 1105static void
1179handle_get_replication (void *cls, 1106handle_get_replication (void *cls,
1180 struct GNUNET_SERVER_Client *client, 1107 struct GNUNET_SERVER_Client *client,
1181 const struct GNUNET_MessageHeader *message) 1108 const struct GNUNET_MessageHeader *message)
1182{ 1109{
1183#if DEBUG_DATASTORE 1110#if DEBUG_DATASTORE
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1185 "Processing `%s' request\n", 1112 "Processing `%s' request\n", "GET_REPLICATION");
1186 "GET_REPLICATION");
1187#endif 1113#endif
1188 GNUNET_STATISTICS_update (stats, 1114 GNUNET_STATISTICS_update (stats,
1189 gettext_noop ("# GET REPLICATION requests received"), 1115 gettext_noop
1190 1, 1116 ("# GET REPLICATION requests received"), 1,
1191 GNUNET_NO); 1117 GNUNET_NO);
1192 GNUNET_SERVER_client_keep (client); 1118 GNUNET_SERVER_client_keep (client);
1193 plugin->api->get_replication (plugin->api->cls, 1119 plugin->api->get_replication (plugin->api->cls, &transmit_item, client);
1194 &transmit_item,
1195 client);
1196} 1120}
1197 1121
1198 1122
@@ -1205,34 +1129,32 @@ handle_get_replication (void *cls,
1205 */ 1129 */
1206static void 1130static void
1207handle_get_zero_anonymity (void *cls, 1131handle_get_zero_anonymity (void *cls,
1208 struct GNUNET_SERVER_Client *client, 1132 struct GNUNET_SERVER_Client *client,
1209 const struct GNUNET_MessageHeader *message) 1133 const struct GNUNET_MessageHeader *message)
1210{ 1134{
1211 const struct GetZeroAnonymityMessage * msg = (const struct GetZeroAnonymityMessage*) message; 1135 const struct GetZeroAnonymityMessage *msg =
1136 (const struct GetZeroAnonymityMessage *) message;
1212 enum GNUNET_BLOCK_Type type; 1137 enum GNUNET_BLOCK_Type type;
1213 1138
1214 type = (enum GNUNET_BLOCK_Type) ntohl (msg->type); 1139 type = (enum GNUNET_BLOCK_Type) ntohl (msg->type);
1215 if (type == GNUNET_BLOCK_TYPE_ANY) 1140 if (type == GNUNET_BLOCK_TYPE_ANY)
1216 { 1141 {
1217 GNUNET_break (0); 1142 GNUNET_break (0);
1218 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 1143 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1219 return; 1144 return;
1220 } 1145 }
1221#if DEBUG_DATASTORE 1146#if DEBUG_DATASTORE
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "Processing `%s' request\n", 1148 "Processing `%s' request\n", "GET_ZERO_ANONYMITY");
1224 "GET_ZERO_ANONYMITY");
1225#endif 1149#endif
1226 GNUNET_STATISTICS_update (stats, 1150 GNUNET_STATISTICS_update (stats,
1227 gettext_noop ("# GET ZERO ANONYMITY requests received"), 1151 gettext_noop
1228 1, 1152 ("# GET ZERO ANONYMITY requests received"), 1,
1229 GNUNET_NO); 1153 GNUNET_NO);
1230 GNUNET_SERVER_client_keep (client); 1154 GNUNET_SERVER_client_keep (client);
1231 plugin->api->get_zero_anonymity (plugin->api->cls, 1155 plugin->api->get_zero_anonymity (plugin->api->cls,
1232 GNUNET_ntohll (msg->offset), 1156 GNUNET_ntohll (msg->offset),
1233 type, 1157 type, &transmit_item, client);
1234 &transmit_item,
1235 client);
1236} 1158}
1237 1159
1238 1160
@@ -1242,43 +1164,36 @@ handle_get_zero_anonymity (void *cls,
1242 */ 1164 */
1243static int 1165static int
1244remove_callback (void *cls, 1166remove_callback (void *cls,
1245 const GNUNET_HashCode * key, 1167 const GNUNET_HashCode * key,
1246 uint32_t size, 1168 uint32_t size,
1247 const void *data, 1169 const void *data,
1248 enum GNUNET_BLOCK_Type type, 1170 enum GNUNET_BLOCK_Type type,
1249 uint32_t priority, 1171 uint32_t priority,
1250 uint32_t anonymity, 1172 uint32_t anonymity,
1251 struct GNUNET_TIME_Absolute 1173 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
1252 expiration, uint64_t uid)
1253{ 1174{
1254 struct GNUNET_SERVER_Client *client = cls; 1175 struct GNUNET_SERVER_Client *client = cls;
1255 1176
1256 if (key == NULL) 1177 if (key == NULL)
1257 { 1178 {
1258#if DEBUG_DATASTORE 1179#if DEBUG_DATASTORE
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1260 "No further matches for `%s' request.\n", 1181 "No further matches for `%s' request.\n", "REMOVE");
1261 "REMOVE"); 1182#endif
1262#endif 1183 transmit_status (client, GNUNET_NO, _("Content not found"));
1263 transmit_status (client, GNUNET_NO, _("Content not found")); 1184 GNUNET_SERVER_client_drop (client);
1264 GNUNET_SERVER_client_drop (client); 1185 return GNUNET_OK; /* last item */
1265 return GNUNET_OK; /* last item */ 1186 }
1266 }
1267#if DEBUG_DATASTORE 1187#if DEBUG_DATASTORE
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269 "Item %llu matches `%s' request for key `%s' and type %u.\n", 1189 "Item %llu matches `%s' request for key `%s' and type %u.\n",
1270 (unsigned long long) uid, 1190 (unsigned long long) uid, "REMOVE", GNUNET_h2s (key), type);
1271 "REMOVE", 1191#endif
1272 GNUNET_h2s (key),
1273 type);
1274#endif
1275 GNUNET_STATISTICS_update (stats, 1192 GNUNET_STATISTICS_update (stats,
1276 gettext_noop ("# bytes removed (explicit request)"), 1193 gettext_noop ("# bytes removed (explicit request)"),
1277 size, 1194 size, GNUNET_YES);
1278 GNUNET_YES); 1195 GNUNET_CONTAINER_bloomfilter_remove (filter, key);
1279 GNUNET_CONTAINER_bloomfilter_remove (filter, 1196 transmit_status (client, GNUNET_OK, NULL);
1280 key);
1281 transmit_status (client, GNUNET_OK, NULL);
1282 GNUNET_SERVER_client_drop (client); 1197 GNUNET_SERVER_client_drop (client);
1283 return GNUNET_NO; 1198 return GNUNET_NO;
1284} 1199}
@@ -1293,40 +1208,34 @@ remove_callback (void *cls,
1293 */ 1208 */
1294static void 1209static void
1295handle_remove (void *cls, 1210handle_remove (void *cls,
1296 struct GNUNET_SERVER_Client *client, 1211 struct GNUNET_SERVER_Client *client,
1297 const struct GNUNET_MessageHeader *message) 1212 const struct GNUNET_MessageHeader *message)
1298{ 1213{
1299 const struct DataMessage *dm = check_data (message); 1214 const struct DataMessage *dm = check_data (message);
1300 GNUNET_HashCode vhash; 1215 GNUNET_HashCode vhash;
1301 1216
1302 if (dm == NULL) 1217 if (dm == NULL)
1303 { 1218 {
1304 GNUNET_break (0); 1219 GNUNET_break (0);
1305 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 1220 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1306 return; 1221 return;
1307 } 1222 }
1308#if DEBUG_DATASTORE 1223#if DEBUG_DATASTORE
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1310 "Processing `%s' request for `%s' of type %u\n", 1225 "Processing `%s' request for `%s' of type %u\n",
1311 "REMOVE", 1226 "REMOVE", GNUNET_h2s (&dm->key), ntohl (dm->type));
1312 GNUNET_h2s (&dm->key),
1313 ntohl (dm->type));
1314#endif 1227#endif
1315 GNUNET_STATISTICS_update (stats, 1228 GNUNET_STATISTICS_update (stats,
1316 gettext_noop ("# REMOVE requests received"), 1229 gettext_noop ("# REMOVE requests received"),
1317 1, 1230 1, GNUNET_NO);
1318 GNUNET_NO);
1319 GNUNET_SERVER_client_keep (client); 1231 GNUNET_SERVER_client_keep (client);
1320 GNUNET_CRYPTO_hash (&dm[1], 1232 GNUNET_CRYPTO_hash (&dm[1], ntohl (dm->size), &vhash);
1321 ntohl(dm->size),
1322 &vhash);
1323 plugin->api->get_key (plugin->api->cls, 1233 plugin->api->get_key (plugin->api->cls,
1324 0, 1234 0,
1325 &dm->key, 1235 &dm->key,
1326 &vhash, 1236 &vhash,
1327 (enum GNUNET_BLOCK_Type) ntohl(dm->type), 1237 (enum GNUNET_BLOCK_Type) ntohl (dm->type),
1328 &remove_callback, 1238 &remove_callback, client);
1329 client);
1330} 1239}
1331 1240
1332 1241
@@ -1339,13 +1248,11 @@ handle_remove (void *cls,
1339 */ 1248 */
1340static void 1249static void
1341handle_drop (void *cls, 1250handle_drop (void *cls,
1342 struct GNUNET_SERVER_Client *client, 1251 struct GNUNET_SERVER_Client *client,
1343 const struct GNUNET_MessageHeader *message) 1252 const struct GNUNET_MessageHeader *message)
1344{ 1253{
1345#if DEBUG_DATASTORE 1254#if DEBUG_DATASTORE
1346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing `%s' request\n", "DROP");
1347 "Processing `%s' request\n",
1348 "DROP");
1349#endif 1256#endif
1350 do_drop = GNUNET_YES; 1257 do_drop = GNUNET_YES;
1351 GNUNET_SERVER_receive_done (client, GNUNET_OK); 1258 GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -1361,20 +1268,18 @@ handle_drop (void *cls,
1361 * 0 for "reset to empty" 1268 * 0 for "reset to empty"
1362 */ 1269 */
1363static void 1270static void
1364disk_utilization_change_cb (void *cls, 1271disk_utilization_change_cb (void *cls, int delta)
1365 int delta)
1366{ 1272{
1367 if ( (delta < 0) && 1273 if ((delta < 0) && (payload < -delta))
1368 (payload < -delta) ) 1274 {
1369 { 1275 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1370 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1276 _
1371 _("Datastore payload inaccurate (%lld < %lld). Trying to fix.\n"), 1277 ("Datastore payload inaccurate (%lld < %lld). Trying to fix.\n"),
1372 (long long) payload, 1278 (long long) payload, (long long) -delta);
1373 (long long) -delta); 1279 payload = plugin->api->estimate_size (plugin->api->cls);
1374 payload = plugin->api->estimate_size (plugin->api->cls); 1280 sync_stats ();
1375 sync_stats (); 1281 return;
1376 return; 1282 }
1377 }
1378 payload += delta; 1283 payload += delta;
1379 lastSync++; 1284 lastSync++;
1380 if (lastSync >= MAX_STAT_SYNC_LAG) 1285 if (lastSync >= MAX_STAT_SYNC_LAG)
@@ -1394,32 +1299,28 @@ disk_utilization_change_cb (void *cls,
1394 */ 1299 */
1395static int 1300static int
1396process_stat_in (void *cls, 1301process_stat_in (void *cls,
1397 const char *subsystem, 1302 const char *subsystem,
1398 const char *name, 1303 const char *name, uint64_t value, int is_persistent)
1399 uint64_t value,
1400 int is_persistent)
1401{ 1304{
1402 GNUNET_assert (stats_worked == GNUNET_NO); 1305 GNUNET_assert (stats_worked == GNUNET_NO);
1403 stats_worked = GNUNET_YES; 1306 stats_worked = GNUNET_YES;
1404 payload += value; 1307 payload += value;
1405#if DEBUG_SQLITE 1308#if DEBUG_SQLITE
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1407 "Notification from statistics about existing payload (%llu), new payload is %llu\n", 1310 "Notification from statistics about existing payload (%llu), new payload is %llu\n",
1408 abs_value, 1311 abs_value, payload);
1409 payload);
1410#endif 1312#endif
1411 return GNUNET_OK; 1313 return GNUNET_OK;
1412} 1314}
1413 1315
1414 1316
1415static void 1317static void
1416process_stat_done (void *cls, 1318process_stat_done (void *cls, int success)
1417 int success)
1418{ 1319{
1419 struct DatastorePlugin *plugin = cls; 1320 struct DatastorePlugin *plugin = cls;
1420 1321
1421 stat_get = NULL; 1322 stat_get = NULL;
1422 if (stats_worked == GNUNET_NO) 1323 if (stats_worked == GNUNET_NO)
1423 payload = plugin->api->estimate_size (plugin->api->cls); 1324 payload = plugin->api->estimate_size (plugin->api->cls);
1424} 1325}
1425 1326
@@ -1428,7 +1329,7 @@ process_stat_done (void *cls,
1428 * Load the datastore plugin. 1329 * Load the datastore plugin.
1429 */ 1330 */
1430static struct DatastorePlugin * 1331static struct DatastorePlugin *
1431load_plugin () 1332load_plugin ()
1432{ 1333{
1433 struct DatastorePlugin *ret; 1334 struct DatastorePlugin *ret;
1434 char *libname; 1335 char *libname;
@@ -1437,14 +1338,13 @@ load_plugin ()
1437 if (GNUNET_OK != 1338 if (GNUNET_OK !=
1438 GNUNET_CONFIGURATION_get_value_string (cfg, 1339 GNUNET_CONFIGURATION_get_value_string (cfg,
1439 "DATASTORE", "DATABASE", &name)) 1340 "DATASTORE", "DATABASE", &name))
1440 { 1341 {
1441 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1342 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1442 _("No `%s' specified for `%s' in configuration!\n"), 1343 _("No `%s' specified for `%s' in configuration!\n"),
1443 "DATABASE", 1344 "DATABASE", "DATASTORE");
1444 "DATASTORE"); 1345 return NULL;
1445 return NULL; 1346 }
1446 } 1347 ret = GNUNET_malloc (sizeof (struct DatastorePlugin));
1447 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1448 ret->env.cfg = cfg; 1348 ret->env.cfg = cfg;
1449 ret->env.duc = &disk_utilization_change_cb; 1349 ret->env.duc = &disk_utilization_change_cb;
1450 ret->env.cls = NULL; 1350 ret->env.cls = NULL;
@@ -1455,14 +1355,14 @@ load_plugin ()
1455 ret->lib_name = libname; 1355 ret->lib_name = libname;
1456 ret->api = GNUNET_PLUGIN_load (libname, &ret->env); 1356 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1457 if (ret->api == NULL) 1357 if (ret->api == NULL)
1458 { 1358 {
1459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1460 _("Failed to load datastore plugin for `%s'\n"), name); 1360 _("Failed to load datastore plugin for `%s'\n"), name);
1461 GNUNET_free (ret->short_name); 1361 GNUNET_free (ret->short_name);
1462 GNUNET_free (libname); 1362 GNUNET_free (libname);
1463 GNUNET_free (ret); 1363 GNUNET_free (ret);
1464 return NULL; 1364 return NULL;
1465 } 1365 }
1466 return ret; 1366 return ret;
1467} 1367}
1468 1368
@@ -1492,30 +1392,29 @@ unload_plugin (struct DatastorePlugin *plug)
1492 * statistics. 1392 * statistics.
1493 */ 1393 */
1494static void 1394static void
1495unload_task (void *cls, 1395unload_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1496 const struct GNUNET_SCHEDULER_TaskContext *tc)
1497{ 1396{
1498 if (GNUNET_YES == do_drop) 1397 if (GNUNET_YES == do_drop)
1499 plugin->api->drop (plugin->api->cls); 1398 plugin->api->drop (plugin->api->cls);
1500 unload_plugin (plugin); 1399 unload_plugin (plugin);
1501 plugin = NULL; 1400 plugin = NULL;
1502 if (filter != NULL) 1401 if (filter != NULL)
1503 { 1402 {
1504 GNUNET_CONTAINER_bloomfilter_free (filter); 1403 GNUNET_CONTAINER_bloomfilter_free (filter);
1505 filter = NULL; 1404 filter = NULL;
1506 } 1405 }
1507 if (lastSync > 0) 1406 if (lastSync > 0)
1508 sync_stats (); 1407 sync_stats ();
1509 if (stat_get != NULL) 1408 if (stat_get != NULL)
1510 { 1409 {
1511 GNUNET_STATISTICS_get_cancel (stat_get); 1410 GNUNET_STATISTICS_get_cancel (stat_get);
1512 stat_get = NULL; 1411 stat_get = NULL;
1513 } 1412 }
1514 if (stats != NULL) 1413 if (stats != NULL)
1515 { 1414 {
1516 GNUNET_STATISTICS_destroy (stats, GNUNET_YES); 1415 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1517 stats = NULL; 1416 stats = NULL;
1518 } 1417 }
1519} 1418}
1520 1419
1521 1420
@@ -1524,33 +1423,29 @@ unload_task (void *cls,
1524 * the transport and core. 1423 * the transport and core.
1525 */ 1424 */
1526static void 1425static void
1527cleaning_task (void *cls, 1426cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1528 const struct GNUNET_SCHEDULER_TaskContext *tc)
1529{ 1427{
1530 struct TransmitCallbackContext *tcc; 1428 struct TransmitCallbackContext *tcc;
1531 1429
1532 cleaning_done = GNUNET_YES; 1430 cleaning_done = GNUNET_YES;
1533 while (NULL != (tcc = tcc_head)) 1431 while (NULL != (tcc = tcc_head))
1432 {
1433 GNUNET_CONTAINER_DLL_remove (tcc_head, tcc_tail, tcc);
1434 if (tcc->th != NULL)
1534 { 1435 {
1535 GNUNET_CONTAINER_DLL_remove (tcc_head, 1436 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1536 tcc_tail, 1437 GNUNET_SERVER_client_drop (tcc->client);
1537 tcc);
1538 if (tcc->th != NULL)
1539 {
1540 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1541 GNUNET_SERVER_client_drop (tcc->client);
1542 }
1543 GNUNET_free (tcc->msg);
1544 GNUNET_free (tcc);
1545 } 1438 }
1439 GNUNET_free (tcc->msg);
1440 GNUNET_free (tcc);
1441 }
1546 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK) 1442 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1547 { 1443 {
1548 GNUNET_SCHEDULER_cancel (expired_kill_task); 1444 GNUNET_SCHEDULER_cancel (expired_kill_task);
1549 expired_kill_task = GNUNET_SCHEDULER_NO_TASK; 1445 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1550 } 1446 }
1551 GNUNET_SCHEDULER_add_continuation (&unload_task, 1447 GNUNET_SCHEDULER_add_continuation (&unload_task,
1552 NULL, 1448 NULL, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1553 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1554} 1449}
1555 1450
1556 1451
@@ -1563,8 +1458,7 @@ cleaning_task (void *cls,
1563 * @param client identification of the client 1458 * @param client identification of the client
1564 */ 1459 */
1565static void 1460static void
1566cleanup_reservations (void *cls, 1461cleanup_reservations (void *cls, struct GNUNET_SERVER_Client *client)
1567 struct GNUNET_SERVER_Client *client)
1568{ 1462{
1569 struct ReservationList *pos; 1463 struct ReservationList *pos;
1570 struct ReservationList *prev; 1464 struct ReservationList *prev;
@@ -1575,27 +1469,25 @@ cleanup_reservations (void *cls,
1575 prev = NULL; 1469 prev = NULL;
1576 pos = reservations; 1470 pos = reservations;
1577 while (NULL != pos) 1471 while (NULL != pos)
1472 {
1473 next = pos->next;
1474 if (pos->client == client)
1578 { 1475 {
1579 next = pos->next; 1476 if (prev == NULL)
1580 if (pos->client == client) 1477 reservations = next;
1581 {
1582 if (prev == NULL)
1583 reservations = next;
1584 else
1585 prev->next = next;
1586 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1587 GNUNET_free (pos);
1588 }
1589 else 1478 else
1590 { 1479 prev->next = next;
1591 prev = pos; 1480 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1592 } 1481 GNUNET_free (pos);
1593 pos = next; 1482 }
1483 else
1484 {
1485 prev = pos;
1594 } 1486 }
1487 pos = next;
1488 }
1595 GNUNET_STATISTICS_set (stats, 1489 GNUNET_STATISTICS_set (stats,
1596 gettext_noop ("# reserved"), 1490 gettext_noop ("# reserved"), reserved, GNUNET_NO);
1597 reserved,
1598 GNUNET_NO);
1599} 1491}
1600 1492
1601 1493
@@ -1612,21 +1504,24 @@ run (void *cls,
1612 const struct GNUNET_CONFIGURATION_Handle *c) 1504 const struct GNUNET_CONFIGURATION_Handle *c)
1613{ 1505{
1614 static const struct GNUNET_SERVER_MessageHandler handlers[] = { 1506 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1615 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE, 1507 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1616 sizeof(struct ReserveMessage) }, 1508 sizeof (struct ReserveMessage)},
1617 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE, 1509 {&handle_release_reserve, NULL,
1618 sizeof(struct ReleaseReserveMessage) }, 1510 GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1619 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 }, 1511 sizeof (struct ReleaseReserveMessage)},
1620 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE, 1512 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0},
1621 sizeof (struct UpdateMessage) }, 1513 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1622 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 }, 1514 sizeof (struct UpdateMessage)},
1623 {&handle_get_replication, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION, 1515 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0},
1624 sizeof(struct GNUNET_MessageHeader) }, 1516 {&handle_get_replication, NULL,
1625 {&handle_get_zero_anonymity, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY, 1517 GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION,
1626 sizeof(struct GetZeroAnonymityMessage) }, 1518 sizeof (struct GNUNET_MessageHeader)},
1627 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 }, 1519 {&handle_get_zero_anonymity, NULL,
1628 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP, 1520 GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY,
1629 sizeof(struct GNUNET_MessageHeader) }, 1521 sizeof (struct GetZeroAnonymityMessage)},
1522 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0},
1523 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1524 sizeof (struct GNUNET_MessageHeader)},
1630 {NULL, NULL, 0, 0} 1525 {NULL, NULL, 0, 0}
1631 }; 1526 };
1632 char *fn; 1527 char *fn;
@@ -1634,81 +1529,72 @@ run (void *cls,
1634 1529
1635 cfg = c; 1530 cfg = c;
1636 if (GNUNET_OK != 1531 if (GNUNET_OK !=
1637 GNUNET_CONFIGURATION_get_value_number (cfg, 1532 GNUNET_CONFIGURATION_get_value_number (cfg, "DATASTORE", "QUOTA", &quota))
1638 "DATASTORE", "QUOTA", &quota)) 1533 {
1639 { 1534 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1640 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1535 _("No `%s' specified for `%s' in configuration!\n"),
1641 _("No `%s' specified for `%s' in configuration!\n"), 1536 "QUOTA", "DATASTORE");
1642 "QUOTA", 1537 return;
1643 "DATASTORE"); 1538 }
1644 return;
1645 }
1646 stats = GNUNET_STATISTICS_create ("datastore", cfg); 1539 stats = GNUNET_STATISTICS_create ("datastore", cfg);
1540 GNUNET_STATISTICS_set (stats, gettext_noop ("# quota"), quota, GNUNET_NO);
1541 cache_size = quota / 8; /* Or should we make this an option? */
1647 GNUNET_STATISTICS_set (stats, 1542 GNUNET_STATISTICS_set (stats,
1648 gettext_noop ("# quota"), 1543 gettext_noop ("# cache size"), cache_size, GNUNET_NO);
1649 quota, 1544 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1650 GNUNET_NO);
1651 cache_size = quota / 8; /* Or should we make this an option? */
1652 GNUNET_STATISTICS_set (stats,
1653 gettext_noop ("# cache size"),
1654 cache_size,
1655 GNUNET_NO);
1656 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1657 fn = NULL; 1545 fn = NULL;
1658 if ( (GNUNET_OK != 1546 if ((GNUNET_OK !=
1659 GNUNET_CONFIGURATION_get_value_filename (cfg, 1547 GNUNET_CONFIGURATION_get_value_filename (cfg,
1660 "DATASTORE", 1548 "DATASTORE",
1661 "BLOOMFILTER", 1549 "BLOOMFILTER",
1662 &fn)) || 1550 &fn)) ||
1663 (GNUNET_OK != 1551 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)))
1664 GNUNET_DISK_directory_create_for_file (fn)) ) 1552 {
1665 { 1553 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1666 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1554 _("Could not use specified filename `%s' for bloomfilter.\n"),
1667 _("Could not use specified filename `%s' for bloomfilter.\n"), 1555 fn != NULL ? fn : "");
1668 fn != NULL ? fn : ""); 1556 GNUNET_free_non_null (fn);
1669 GNUNET_free_non_null (fn); 1557 fn = NULL;
1670 fn = NULL; 1558 }
1671 }
1672 if (fn != NULL) 1559 if (fn != NULL)
1673 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */ 1560 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1674 else 1561 else
1675 filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */ 1562 filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
1676 GNUNET_free_non_null (fn); 1563 GNUNET_free_non_null (fn);
1677 if (filter == NULL) 1564 if (filter == NULL)
1565 {
1566 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1567 _("Failed to initialize bloomfilter.\n"));
1568 if (stats != NULL)
1678 { 1569 {
1679 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1570 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1680 _("Failed to initialize bloomfilter.\n")); 1571 stats = NULL;
1681 if (stats != NULL)
1682 {
1683 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1684 stats = NULL;
1685 }
1686 return;
1687 } 1572 }
1573 return;
1574 }
1688 plugin = load_plugin (); 1575 plugin = load_plugin ();
1689 if (NULL == plugin) 1576 if (NULL == plugin)
1577 {
1578 GNUNET_CONTAINER_bloomfilter_free (filter);
1579 filter = NULL;
1580 if (stats != NULL)
1690 { 1581 {
1691 GNUNET_CONTAINER_bloomfilter_free (filter); 1582 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1692 filter = NULL; 1583 stats = NULL;
1693 if (stats != NULL)
1694 {
1695 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1696 stats = NULL;
1697 }
1698 return;
1699 } 1584 }
1585 return;
1586 }
1700 stat_get = GNUNET_STATISTICS_get (stats, 1587 stat_get = GNUNET_STATISTICS_get (stats,
1701 "datastore", 1588 "datastore",
1702 QUOTA_STAT_NAME, 1589 QUOTA_STAT_NAME,
1703 GNUNET_TIME_UNIT_SECONDS, 1590 GNUNET_TIME_UNIT_SECONDS,
1704 &process_stat_done, 1591 &process_stat_done,
1705 &process_stat_in, 1592 &process_stat_in, plugin);
1706 plugin);
1707 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL); 1593 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1708 GNUNET_SERVER_add_handlers (server, handlers); 1594 GNUNET_SERVER_add_handlers (server, handlers);
1709 expired_kill_task 1595 expired_kill_task
1710 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, 1596 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1711 &delete_expired, NULL); 1597 &delete_expired, NULL);
1712 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 1598 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1713 &cleaning_task, NULL); 1599 &cleaning_task, NULL);
1714} 1600}
@@ -1730,8 +1616,7 @@ main (int argc, char *const *argv)
1730 GNUNET_SERVICE_run (argc, 1616 GNUNET_SERVICE_run (argc,
1731 argv, 1617 argv,
1732 "datastore", 1618 "datastore",
1733 GNUNET_SERVICE_OPTION_NONE, 1619 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1734 &run, NULL)) ? 0 : 1;
1735 return ret; 1620 return ret;
1736} 1621}
1737 1622
diff --git a/src/datastore/perf_datastore_api.c b/src/datastore/perf_datastore_api.c
index 9d7755342..9d567aacd 100644
--- a/src/datastore/perf_datastore_api.c
+++ b/src/datastore/perf_datastore_api.c
@@ -84,13 +84,13 @@ static struct GNUNET_TIME_Absolute start_time;
84static int ok; 84static int ok;
85 85
86enum RunPhase 86enum RunPhase
87 { 87{
88 RP_DONE = 0, 88 RP_DONE = 0,
89 RP_PUT, 89 RP_PUT,
90 RP_CUT, 90 RP_CUT,
91 RP_REPORT, 91 RP_REPORT,
92 RP_ERROR 92 RP_ERROR
93 }; 93};
94 94
95 95
96struct CpsRunContext 96struct CpsRunContext
@@ -105,28 +105,23 @@ struct CpsRunContext
105 105
106 106
107static void 107static void
108run_continuation (void *cls, 108run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
109 const struct GNUNET_SCHEDULER_TaskContext *tc);
110 109
111 110
112 111
113 112
114static void 113static void
115check_success (void *cls, 114check_success (void *cls, int success, const char *msg)
116 int success,
117 const char *msg)
118{ 115{
119 struct CpsRunContext *crc = cls; 116 struct CpsRunContext *crc = cls;
120 117
121 if (GNUNET_OK != success) 118 if (GNUNET_OK != success)
122 { 119 {
123 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 120 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Check success failed: `%s'\n", msg);
124 "Check success failed: `%s'\n", msg); 121 crc->phase = RP_ERROR;
125 crc->phase = RP_ERROR; 122 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
126 GNUNET_SCHEDULER_add_now (&run_continuation, 123 return;
127 crc); 124 }
128 return;
129 }
130#if REPORT_ID 125#if REPORT_ID
131 fprintf (stderr, "I"); 126 fprintf (stderr, "I");
132#endif 127#endif
@@ -135,17 +130,16 @@ check_success (void *cls,
135 stored_entries++; 130 stored_entries++;
136 crc->j++; 131 crc->j++;
137 if (crc->j >= PUT_10) 132 if (crc->j >= PUT_10)
138 { 133 {
139 crc->j = 0; 134 crc->j = 0;
140 crc->i++; 135 crc->i++;
141 if (crc->i == ITERATIONS) 136 if (crc->i == ITERATIONS)
142 crc->phase = RP_DONE; 137 crc->phase = RP_DONE;
143 else 138 else
144 crc->phase = RP_CUT; 139 crc->phase = RP_CUT;
145 } 140 }
146 GNUNET_SCHEDULER_add_continuation (&run_continuation, 141 GNUNET_SCHEDULER_add_continuation (&run_continuation,
147 crc, 142 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
148 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
149} 143}
150 144
151 145
@@ -157,41 +151,35 @@ check_success (void *cls,
157 * @param success GNUNET_SYSERR on failure 151 * @param success GNUNET_SYSERR on failure
158 * @param msg NULL on success, otherwise an error message 152 * @param msg NULL on success, otherwise an error message
159 */ 153 */
160static void 154static void
161remove_next(void *cls, 155remove_next (void *cls, int success, const char *msg)
162 int success,
163 const char *msg)
164{ 156{
165 struct CpsRunContext *crc = cls; 157 struct CpsRunContext *crc = cls;
166 158
167 if (GNUNET_OK != success) 159 if (GNUNET_OK != success)
168 { 160 {
169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "remove_next failed: `%s'\n", msg);
170 "remove_next failed: `%s'\n", msg); 162 crc->phase = RP_ERROR;
171 crc->phase = RP_ERROR; 163 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
172 GNUNET_SCHEDULER_add_now (&run_continuation, 164 return;
173 crc); 165 }
174 return;
175 }
176#if REPORT_ID 166#if REPORT_ID
177 fprintf (stderr, "D"); 167 fprintf (stderr, "D");
178#endif 168#endif
179 GNUNET_assert (GNUNET_OK == success); 169 GNUNET_assert (GNUNET_OK == success);
180 GNUNET_SCHEDULER_add_now (&run_continuation, 170 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
181 crc);
182} 171}
183 172
184 173
185static void 174static void
186delete_value (void *cls, 175delete_value (void *cls,
187 const GNUNET_HashCode *key, 176 const GNUNET_HashCode * key,
188 size_t size, 177 size_t size,
189 const void *data, 178 const void *data,
190 enum GNUNET_BLOCK_Type type, 179 enum GNUNET_BLOCK_Type type,
191 uint32_t priority, 180 uint32_t priority,
192 uint32_t anonymity, 181 uint32_t anonymity,
193 struct GNUNET_TIME_Absolute 182 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
194 expiration, uint64_t uid)
195{ 183{
196 struct CpsRunContext *crc = cls; 184 struct CpsRunContext *crc = cls;
197 185
@@ -203,19 +191,16 @@ delete_value (void *cls,
203 if (stored_bytes < MAX_SIZE) 191 if (stored_bytes < MAX_SIZE)
204 crc->phase = RP_PUT; 192 crc->phase = RP_PUT;
205 GNUNET_assert (NULL != 193 GNUNET_assert (NULL !=
206 GNUNET_DATASTORE_remove (datastore, 194 GNUNET_DATASTORE_remove (datastore,
207 key, 195 key,
208 size, 196 size,
209 data, 197 data,
210 1, 1, TIMEOUT, 198 1, 1, TIMEOUT, &remove_next, crc));
211 &remove_next,
212 crc));
213} 199}
214 200
215 201
216static void 202static void
217run_continuation (void *cls, 203run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
218 const struct GNUNET_SCHEDULER_TaskContext *tc)
219{ 204{
220 struct CpsRunContext *crc = cls; 205 struct CpsRunContext *crc = cls;
221 size_t size; 206 size_t size;
@@ -227,133 +212,127 @@ run_continuation (void *cls,
227 212
228 ok = (int) crc->phase; 213 ok = (int) crc->phase;
229 switch (crc->phase) 214 switch (crc->phase)
230 { 215 {
231 case RP_PUT: 216 case RP_PUT:
232 memset (&key, 256 - crc->i, sizeof (GNUNET_HashCode)); 217 memset (&key, 256 - crc->i, sizeof (GNUNET_HashCode));
233 i = crc->j; 218 i = crc->j;
234 k = crc->i; 219 k = crc->i;
235 /* most content is 32k */ 220 /* most content is 32k */
236 size = 32 * 1024; 221 size = 32 * 1024;
237 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */ 222 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
238 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024); 223 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
239 crc->size = size = size - (size & 7); /* always multiple of 8 */ 224 crc->size = size = size - (size & 7); /* always multiple of 8 */
240 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key); 225 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
241 memset (data, i, size); 226 memset (data, i, size);
242 if (i > 255) 227 if (i > 255)
243 memset (data, i - 255, size / 2); 228 memset (data, i - 255, size / 2);
244 data[0] = k; 229 data[0] = k;
245 GNUNET_assert (NULL != 230 GNUNET_assert (NULL !=
246 GNUNET_DATASTORE_put (datastore, 231 GNUNET_DATASTORE_put (datastore,
247 0, 232 0,
248 &key, 233 &key,
249 size, 234 size,
250 data, 235 data,
251 i+1, 236 i + 1,
252 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100), 237 GNUNET_CRYPTO_random_u32
253 i, 0, 238 (GNUNET_CRYPTO_QUALITY_WEAK, 100), i,
254 GNUNET_TIME_relative_to_absolute 239 0,
255 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 240 GNUNET_TIME_relative_to_absolute
256 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))), 241 (GNUNET_TIME_relative_multiply
257 1, 1, TIMEOUT, 242 (GNUNET_TIME_UNIT_SECONDS,
258 &check_success, 243 GNUNET_CRYPTO_random_u32
259 crc)); 244 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
260 break; 245 1, 1, TIMEOUT, &check_success, crc));
261 case RP_CUT: 246 break;
262 /* trim down below MAX_SIZE again */ 247 case RP_CUT:
263 GNUNET_assert (NULL != 248 /* trim down below MAX_SIZE again */
264 GNUNET_DATASTORE_get_for_replication (datastore, 249 GNUNET_assert (NULL !=
265 1, 1, TIMEOUT, 250 GNUNET_DATASTORE_get_for_replication (datastore,
266 &delete_value, 251 1, 1, TIMEOUT,
267 crc)); 252 &delete_value, crc));
268 break; 253 break;
269 case RP_REPORT: 254 case RP_REPORT:
270 printf ( 255 printf (
271#if REPORT_ID 256#if REPORT_ID
272 "\n" 257 "\n"
273#endif 258#endif
274 "Stored %llu kB / %lluk ops / %llu ops/s\n", 259 "Stored %llu kB / %lluk ops / %llu ops/s\n", stored_bytes / 1024, /* used size in k */
275 stored_bytes / 1024, /* used size in k */ 260 stored_ops / 1024, /* total operations (in k) */
276 stored_ops / 1024, /* total operations (in k) */ 261 1000 * stored_ops / (1 +
277 1000 * stored_ops / (1 + GNUNET_TIME_absolute_get_duration(start_time).rel_value)); 262 GNUNET_TIME_absolute_get_duration
278 crc->phase = RP_PUT; 263 (start_time).rel_value));
279 crc->j = 0; 264 crc->phase = RP_PUT;
280 GNUNET_SCHEDULER_add_continuation (&run_continuation, 265 crc->j = 0;
281 crc, 266 GNUNET_SCHEDULER_add_continuation (&run_continuation,
282 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 267 crc,
283 break; 268 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
284 case RP_DONE: 269 break;
285 GNUNET_snprintf (gstr, sizeof (gstr), 270 case RP_DONE:
286 "DATASTORE-%s", 271 GNUNET_snprintf (gstr, sizeof (gstr), "DATASTORE-%s", plugin_name);
287 plugin_name); 272 if ((crc->i == ITERATIONS) && (stored_ops > 0))
288 if ( (crc->i == ITERATIONS) && 273 GAUGER (gstr,
289 (stored_ops > 0) ) 274 "PUT operation duration",
290 GAUGER (gstr, 275 GNUNET_TIME_absolute_get_duration (start_time).rel_value /
291 "PUT operation duration", 276 stored_ops, "ms/operation");
292 GNUNET_TIME_absolute_get_duration(start_time).rel_value / stored_ops, 277 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
293 "ms/operation"); 278 GNUNET_free (crc);
294 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES); 279 ok = 0;
295 GNUNET_free (crc); 280 break;
296 ok = 0; 281 case RP_ERROR:
297 break; 282 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
298 case RP_ERROR: 283 GNUNET_free (crc);
299 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES); 284 ok = 1;
300 GNUNET_free (crc); 285 break;
301 ok = 1; 286 default:
302 break; 287 GNUNET_assert (0);
303 default: 288 }
304 GNUNET_assert (0);
305 }
306} 289}
307 290
308 291
309static void 292static void
310run_tests (void *cls, 293run_tests (void *cls, int success, const char *msg)
311 int success,
312 const char *msg)
313{ 294{
314 struct CpsRunContext *crc = cls; 295 struct CpsRunContext *crc = cls;
315 296
316 if (success != GNUNET_YES) 297 if (success != GNUNET_YES)
317 { 298 {
318 fprintf (stderr, 299 fprintf (stderr,
319 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.", 300 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.",
320 msg); 301 msg);
321 GNUNET_free (crc); 302 GNUNET_free (crc);
322 return; 303 return;
323 } 304 }
324 GNUNET_SCHEDULER_add_continuation (&run_continuation, 305 GNUNET_SCHEDULER_add_continuation (&run_continuation,
325 crc, 306 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
326 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
327} 307}
328 308
329 309
330static void 310static void
331run (void *cls, 311run (void *cls,
332 char *const *args, 312 char *const *args,
333 const char *cfgfile, 313 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
334 const struct GNUNET_CONFIGURATION_Handle *cfg)
335{ 314{
336 struct CpsRunContext *crc; 315 struct CpsRunContext *crc;
337 static GNUNET_HashCode zkey; 316 static GNUNET_HashCode zkey;
338 317
339 datastore = GNUNET_DATASTORE_connect (cfg); 318 datastore = GNUNET_DATASTORE_connect (cfg);
340 start_time = GNUNET_TIME_absolute_get (); 319 start_time = GNUNET_TIME_absolute_get ();
341 crc = GNUNET_malloc(sizeof(struct CpsRunContext)); 320 crc = GNUNET_malloc (sizeof (struct CpsRunContext));
342 crc->cfg = cfg; 321 crc->cfg = cfg;
343 crc->phase = RP_PUT; 322 crc->phase = RP_PUT;
344 if (NULL == 323 if (NULL ==
345 GNUNET_DATASTORE_put (datastore, 0, 324 GNUNET_DATASTORE_put (datastore, 0,
346 &zkey, 4, "TEST", 325 &zkey, 4, "TEST",
347 GNUNET_BLOCK_TYPE_TEST, 326 GNUNET_BLOCK_TYPE_TEST,
348 0, 0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS), 327 0, 0, 0,
349 0, 1, GNUNET_TIME_UNIT_MINUTES, 328 GNUNET_TIME_relative_to_absolute
350 &run_tests, crc)) 329 (GNUNET_TIME_UNIT_SECONDS), 0, 1,
351 { 330 GNUNET_TIME_UNIT_MINUTES, &run_tests, crc))
352 fprintf (stderr, 331 {
353 "Test 'put' operation failed.\n"); 332 fprintf (stderr, "Test 'put' operation failed.\n");
354 ok = 1; 333 ok = 1;
355 GNUNET_free (crc); 334 GNUNET_free (crc);
356 } 335 }
357} 336}
358 337
359 338
@@ -362,7 +341,8 @@ check ()
362{ 341{
363 struct GNUNET_OS_Process *proc; 342 struct GNUNET_OS_Process *proc;
364 char cfg_name[128]; 343 char cfg_name[128];
365 char *const argv[] = { 344
345 char *const argv[] = {
366 "perf-datastore-api", 346 "perf-datastore-api",
367 "-c", 347 "-c",
368 cfg_name, 348 cfg_name,
@@ -376,25 +356,24 @@ check ()
376 }; 356 };
377 357
378 GNUNET_snprintf (cfg_name, 358 GNUNET_snprintf (cfg_name,
379 sizeof (cfg_name), 359 sizeof (cfg_name),
380 "test_datastore_api_data_%s.conf", 360 "test_datastore_api_data_%s.conf", plugin_name);
381 plugin_name);
382 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm", 361 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
383 "gnunet-service-arm", 362 "gnunet-service-arm",
384#if VERBOSE 363#if VERBOSE
385 "-L", "DEBUG", 364 "-L", "DEBUG",
386#endif 365#endif
387 "-c", cfg_name, NULL); 366 "-c", cfg_name, NULL);
388 GNUNET_assert (NULL != proc); 367 GNUNET_assert (NULL != proc);
389 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, 368 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
390 argv, "perf-datastore-api", "nohelp", 369 argv, "perf-datastore-api", "nohelp",
391 options, &run, NULL); 370 options, &run, NULL);
392 sleep (1); /* give datastore chance to process 'DROP' */ 371 sleep (1); /* give datastore chance to process 'DROP' */
393 if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) 372 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
394 { 373 {
395 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 374 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
396 ok = 1; 375 ok = 1;
397 } 376 }
398 GNUNET_OS_process_wait (proc); 377 GNUNET_OS_process_wait (proc);
399 GNUNET_OS_process_close (proc); 378 GNUNET_OS_process_close (proc);
400 proc = NULL; 379 proc = NULL;
@@ -412,21 +391,20 @@ main (int argc, char *argv[])
412 sleep (1); 391 sleep (1);
413 /* determine name of plugin to use */ 392 /* determine name of plugin to use */
414 plugin_name = argv[0]; 393 plugin_name = argv[0];
415 while (NULL != (pos = strstr(plugin_name, "_"))) 394 while (NULL != (pos = strstr (plugin_name, "_")))
416 plugin_name = pos+1; 395 plugin_name = pos + 1;
417 if (NULL != (pos = strstr(plugin_name, "."))) 396 if (NULL != (pos = strstr (plugin_name, ".")))
418 pos[0] = 0; 397 pos[0] = 0;
419 else 398 else
420 pos = (char *) plugin_name; 399 pos = (char *) plugin_name;
421 400
422 GNUNET_snprintf (dir_name, 401 GNUNET_snprintf (dir_name,
423 sizeof (dir_name), 402 sizeof (dir_name),
424 "/tmp/test-gnunet-datastore-%s", 403 "/tmp/test-gnunet-datastore-%s", plugin_name);
425 plugin_name);
426 GNUNET_DISK_directory_remove (dir_name); 404 GNUNET_DISK_directory_remove (dir_name);
427 GNUNET_log_setup ("perf-datastore-api", 405 GNUNET_log_setup ("perf-datastore-api",
428#if VERBOSE 406#if VERBOSE
429 "DEBUG", 407 "DEBUG",
430#else 408#else
431 "WARNING", 409 "WARNING",
432#endif 410#endif
diff --git a/src/datastore/perf_plugin_datastore.c b/src/datastore/perf_plugin_datastore.c
index 3829c7af5..f8a3705e4 100644
--- a/src/datastore/perf_plugin_datastore.c
+++ b/src/datastore/perf_plugin_datastore.c
@@ -61,14 +61,14 @@ static const char *plugin_name;
61static int ok; 61static int ok;
62 62
63enum RunPhase 63enum RunPhase
64 { 64{
65 RP_ERROR = 0, 65 RP_ERROR = 0,
66 RP_PUT, 66 RP_PUT,
67 RP_REP_GET, 67 RP_REP_GET,
68 RP_ZA_GET, 68 RP_ZA_GET,
69 RP_EXP_GET, 69 RP_EXP_GET,
70 RP_DONE 70 RP_DONE
71 }; 71};
72 72
73 73
74struct CpsRunContext 74struct CpsRunContext
@@ -77,7 +77,7 @@ struct CpsRunContext
77 struct GNUNET_TIME_Absolute start; 77 struct GNUNET_TIME_Absolute start;
78 struct GNUNET_TIME_Absolute end; 78 struct GNUNET_TIME_Absolute end;
79 const struct GNUNET_CONFIGURATION_Handle *cfg; 79 const struct GNUNET_CONFIGURATION_Handle *cfg;
80 struct GNUNET_DATASTORE_PluginFunctions * api; 80 struct GNUNET_DATASTORE_PluginFunctions *api;
81 enum RunPhase phase; 81 enum RunPhase phase;
82 unsigned int cnt; 82 unsigned int cnt;
83 unsigned int iter; 83 unsigned int iter;
@@ -94,15 +94,13 @@ struct CpsRunContext
94 * 0 for "reset to empty" 94 * 0 for "reset to empty"
95 */ 95 */
96static void 96static void
97disk_utilization_change_cb (void *cls, 97disk_utilization_change_cb (void *cls, int delta)
98 int delta)
99{ 98{
100} 99}
101 100
102 101
103static void 102static void
104putValue (struct GNUNET_DATASTORE_PluginFunctions * api, 103putValue (struct GNUNET_DATASTORE_PluginFunctions *api, int i, int k)
105 int i, int k)
106{ 104{
107 char value[65536]; 105 char value[65536];
108 size_t size; 106 size_t size;
@@ -113,7 +111,7 @@ putValue (struct GNUNET_DATASTORE_PluginFunctions * api,
113 111
114 /* most content is 32k */ 112 /* most content is 32k */
115 size = 32 * 1024; 113 size = 32 * 1024;
116 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */ 114 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
117 size = 8 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024); 115 size = 8 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
118 size = size - (size & 7); /* always multiple of 8 */ 116 size = size - (size & 7); /* always multiple of 8 */
119 117
@@ -127,46 +125,38 @@ putValue (struct GNUNET_DATASTORE_PluginFunctions * api,
127 memcpy (&value[4], &i, sizeof (i)); 125 memcpy (&value[4], &i, sizeof (i));
128 msg = NULL; 126 msg = NULL;
129 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100); 127 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
130 if (GNUNET_OK != api->put (api->cls, 128 if (GNUNET_OK != api->put (api->cls, &key, size, value, 1 + i % 4 /* type */ ,
131 &key, 129 prio, i % 4 /* anonymity */ ,
132 size, 130 0 /* replication */ ,
133 value, 131 GNUNET_TIME_relative_to_absolute
134 1 + i % 4 /* type */, 132 (GNUNET_TIME_relative_multiply
135 prio, 133 (GNUNET_TIME_UNIT_MILLISECONDS,
136 i % 4 /* anonymity */, 134 60 * 60 * 60 * 1000 +
137 0 /* replication */, 135 GNUNET_CRYPTO_random_u32
138 GNUNET_TIME_relative_to_absolute 136 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))), &msg))
139 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 137 {
140 60 * 60 * 60 * 1000 + 138 fprintf (stderr, "ERROR: `%s'\n", msg);
141 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))), 139 GNUNET_free_non_null (msg);
142 &msg)) 140 return;
143 { 141 }
144 fprintf (stderr, "ERROR: `%s'\n", msg);
145 GNUNET_free_non_null (msg);
146 return;
147 }
148 ic++; 142 ic++;
149 stored_bytes += size; 143 stored_bytes += size;
150 stored_ops++; 144 stored_ops++;
151 stored_entries++; 145 stored_entries++;
152} 146}
153 147
154static void 148static void test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
155test (void *cls,
156 const struct GNUNET_SCHEDULER_TaskContext *tc);
157 149
158 150
159static int 151static int
160iterate_zeros (void *cls, 152iterate_zeros (void *cls,
161 const GNUNET_HashCode * key, 153 const GNUNET_HashCode * key,
162 uint32_t size, 154 uint32_t size,
163 const void *data, 155 const void *data,
164 enum GNUNET_BLOCK_Type type, 156 enum GNUNET_BLOCK_Type type,
165 uint32_t priority, 157 uint32_t priority,
166 uint32_t anonymity, 158 uint32_t anonymity,
167 struct GNUNET_TIME_Absolute 159 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
168 expiration,
169 uint64_t uid)
170{ 160{
171 struct CpsRunContext *crc = cls; 161 struct CpsRunContext *crc = cls;
172 int i; 162 int i;
@@ -175,38 +165,37 @@ iterate_zeros (void *cls,
175 GNUNET_assert (key != NULL); 165 GNUNET_assert (key != NULL);
176 GNUNET_assert (size >= 8); 166 GNUNET_assert (size >= 8);
177 memcpy (&i, &cdata[4], sizeof (i)); 167 memcpy (&i, &cdata[4], sizeof (i));
178 hits[i/8] |= (1 << (i % 8)); 168 hits[i / 8] |= (1 << (i % 8));
179 169
180#if VERBOSE 170#if VERBOSE
181 fprintf (stderr, "Found result type=%u, priority=%u, size=%u, expire=%llu\n", 171 fprintf (stderr, "Found result type=%u, priority=%u, size=%u, expire=%llu\n",
182 type, priority, size, 172 type, priority, size, (unsigned long long) expiration.abs_value);
183 (unsigned long long) expiration.abs_value);
184#endif 173#endif
185 crc->cnt++; 174 crc->cnt++;
186 if (crc->cnt == PUT_10 / 4 - 1) 175 if (crc->cnt == PUT_10 / 4 - 1)
187 { 176 {
188 unsigned int bc; 177 unsigned int bc;
189 178
190 bc = 0; 179 bc = 0;
191 for (i = 0;i<PUT_10;i++) 180 for (i = 0; i < PUT_10; i++)
192 if (0 != (hits[i/8] & (1 << (i % 8)))) 181 if (0 != (hits[i / 8] & (1 << (i % 8))))
193 bc++; 182 bc++;
194 183
195 crc->end = GNUNET_TIME_absolute_get(); 184 crc->end = GNUNET_TIME_absolute_get ();
196 printf ("%s took %llu ms yielding %u/%u items\n", 185 printf ("%s took %llu ms yielding %u/%u items\n",
197 "Select random zero-anonymity item", 186 "Select random zero-anonymity item",
198 (unsigned long long) (crc->end.abs_value - crc->start.abs_value), 187 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
199 bc, 188 bc, crc->cnt);
200 crc->cnt); 189 if (crc->cnt > 0)
201 if (crc->cnt > 0) 190 GAUGER (category,
202 GAUGER (category, 191 "Select random zero-anonymity item",
203 "Select random zero-anonymity item", 192 (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
204 (crc->end.abs_value - crc->start.abs_value) / crc->cnt, "ms/item"); 193 "ms/item");
205 memset (hits, 0, sizeof (hits)); 194 memset (hits, 0, sizeof (hits));
206 crc->phase++; 195 crc->phase++;
207 crc->cnt = 0; 196 crc->cnt = 0;
208 crc->start = GNUNET_TIME_absolute_get (); 197 crc->start = GNUNET_TIME_absolute_get ();
209 } 198 }
210 GNUNET_SCHEDULER_add_now (&test, crc); 199 GNUNET_SCHEDULER_add_now (&test, crc);
211 return GNUNET_OK; 200 return GNUNET_OK;
212} 201}
@@ -214,15 +203,13 @@ iterate_zeros (void *cls,
214 203
215static int 204static int
216expiration_get (void *cls, 205expiration_get (void *cls,
217 const GNUNET_HashCode * key, 206 const GNUNET_HashCode * key,
218 uint32_t size, 207 uint32_t size,
219 const void *data, 208 const void *data,
220 enum GNUNET_BLOCK_Type type, 209 enum GNUNET_BLOCK_Type type,
221 uint32_t priority, 210 uint32_t priority,
222 uint32_t anonymity, 211 uint32_t anonymity,
223 struct GNUNET_TIME_Absolute 212 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
224 expiration,
225 uint64_t uid)
226{ 213{
227 struct CpsRunContext *crc = cls; 214 struct CpsRunContext *crc = cls;
228 int i; 215 int i;
@@ -230,36 +217,35 @@ expiration_get (void *cls,
230 217
231 GNUNET_assert (size >= 8); 218 GNUNET_assert (size >= 8);
232 memcpy (&i, &cdata[4], sizeof (i)); 219 memcpy (&i, &cdata[4], sizeof (i));
233 hits[i/8] |= (1 << (i % 8)); 220 hits[i / 8] |= (1 << (i % 8));
234 crc->cnt++; 221 crc->cnt++;
235 if (PUT_10 <= crc->cnt) 222 if (PUT_10 <= crc->cnt)
236 { 223 {
237 unsigned int bc; 224 unsigned int bc;
238 225
239 bc = 0; 226 bc = 0;
240 for (i = 0;i<PUT_10;i++) 227 for (i = 0; i < PUT_10; i++)
241 if (0 != (hits[i/8] & (1 << (i % 8)))) 228 if (0 != (hits[i / 8] & (1 << (i % 8))))
242 bc++; 229 bc++;
243 230
244 crc->end = GNUNET_TIME_absolute_get(); 231 crc->end = GNUNET_TIME_absolute_get ();
245 printf ("%s took %llu ms yielding %u/%u items\n", 232 printf ("%s took %llu ms yielding %u/%u items\n",
246 "Selecting and deleting by expiration", 233 "Selecting and deleting by expiration",
247 (unsigned long long) (crc->end.abs_value - crc->start.abs_value), 234 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
248 bc, 235 bc, (unsigned int) PUT_10);
249 (unsigned int) PUT_10); 236 if (crc->cnt > 0)
250 if (crc->cnt > 0) 237 GAUGER (category,
251 GAUGER (category, 238 "Selecting and deleting by expiration",
252 "Selecting and deleting by expiration", 239 (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
253 (crc->end.abs_value - crc->start.abs_value) / crc->cnt, 240 "ms/item");
254 "ms/item"); 241 memset (hits, 0, sizeof (hits));
255 memset (hits, 0, sizeof (hits)); 242 if (++crc->iter == ITERATIONS)
256 if (++crc->iter == ITERATIONS) 243 crc->phase++;
257 crc->phase++; 244 else
258 else 245 crc->phase = RP_PUT;
259 crc->phase = RP_PUT; 246 crc->cnt = 0;
260 crc->cnt = 0; 247 crc->start = GNUNET_TIME_absolute_get ();
261 crc->start = GNUNET_TIME_absolute_get (); 248 }
262 }
263 GNUNET_SCHEDULER_add_now (&test, crc); 249 GNUNET_SCHEDULER_add_now (&test, crc);
264 return GNUNET_NO; 250 return GNUNET_NO;
265} 251}
@@ -267,15 +253,13 @@ expiration_get (void *cls,
267 253
268static int 254static int
269replication_get (void *cls, 255replication_get (void *cls,
270 const GNUNET_HashCode * key, 256 const GNUNET_HashCode * key,
271 uint32_t size, 257 uint32_t size,
272 const void *data, 258 const void *data,
273 enum GNUNET_BLOCK_Type type, 259 enum GNUNET_BLOCK_Type type,
274 uint32_t priority, 260 uint32_t priority,
275 uint32_t anonymity, 261 uint32_t anonymity,
276 struct GNUNET_TIME_Absolute 262 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
277 expiration,
278 uint64_t uid)
279{ 263{
280 struct CpsRunContext *crc = cls; 264 struct CpsRunContext *crc = cls;
281 int i; 265 int i;
@@ -284,35 +268,34 @@ replication_get (void *cls,
284 GNUNET_assert (NULL != key); 268 GNUNET_assert (NULL != key);
285 GNUNET_assert (size >= 8); 269 GNUNET_assert (size >= 8);
286 memcpy (&i, &cdata[4], sizeof (i)); 270 memcpy (&i, &cdata[4], sizeof (i));
287 hits[i/8] |= (1 << (i % 8)); 271 hits[i / 8] |= (1 << (i % 8));
288 crc->cnt++; 272 crc->cnt++;
289 if (PUT_10 <= crc->cnt) 273 if (PUT_10 <= crc->cnt)
290 { 274 {
291 unsigned int bc; 275 unsigned int bc;
292 276
293 bc = 0; 277 bc = 0;
294 for (i = 0;i<PUT_10;i++) 278 for (i = 0; i < PUT_10; i++)
295 if (0 != (hits[i/8] & (1 << (i % 8)))) 279 if (0 != (hits[i / 8] & (1 << (i % 8))))
296 bc++; 280 bc++;
297 281
298 crc->end = GNUNET_TIME_absolute_get(); 282 crc->end = GNUNET_TIME_absolute_get ();
299 printf ("%s took %llu ms yielding %u/%u items\n", 283 printf ("%s took %llu ms yielding %u/%u items\n",
300 "Selecting random item for replication", 284 "Selecting random item for replication",
301 (unsigned long long) (crc->end.abs_value - crc->start.abs_value), 285 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
302 bc, 286 bc, (unsigned int) PUT_10);
303 (unsigned int) PUT_10); 287 if (crc->cnt > 0)
304 if (crc->cnt > 0) 288 GAUGER (category,
305 GAUGER (category, 289 "Selecting random item for replication",
306 "Selecting random item for replication", 290 (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
307 (crc->end.abs_value - crc->start.abs_value) / crc->cnt, 291 "ms/item");
308 "ms/item"); 292 memset (hits, 0, sizeof (hits));
309 memset (hits, 0, sizeof (hits)); 293 crc->phase++;
310 crc->phase++; 294 crc->offset = 0;
311 crc->offset = 0; 295 crc->cnt = 0;
312 crc->cnt = 0; 296 crc->start = GNUNET_TIME_absolute_get ();
313 crc->start = GNUNET_TIME_absolute_get (); 297 }
314 } 298
315
316 GNUNET_SCHEDULER_add_now (&test, crc); 299 GNUNET_SCHEDULER_add_now (&test, crc);
317 return GNUNET_OK; 300 return GNUNET_OK;
318} 301}
@@ -326,8 +309,8 @@ replication_get (void *cls,
326 * @param cfg configuration to use 309 * @param cfg configuration to use
327 */ 310 */
328static void 311static void
329unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api, 312unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
330 const struct GNUNET_CONFIGURATION_Handle *cfg) 313 const struct GNUNET_CONFIGURATION_Handle *cfg)
331{ 314{
332 char *name; 315 char *name;
333 char *libname; 316 char *libname;
@@ -335,13 +318,12 @@ unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api,
335 if (GNUNET_OK != 318 if (GNUNET_OK !=
336 GNUNET_CONFIGURATION_get_value_string (cfg, 319 GNUNET_CONFIGURATION_get_value_string (cfg,
337 "DATASTORE", "DATABASE", &name)) 320 "DATASTORE", "DATABASE", &name))
338 { 321 {
339 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
340 _("No `%s' specified for `%s' in configuration!\n"), 323 _("No `%s' specified for `%s' in configuration!\n"),
341 "DATABASE", 324 "DATABASE", "DATASTORE");
342 "DATASTORE"); 325 return;
343 return; 326 }
344 }
345 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name); 327 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
346 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api)); 328 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
347 GNUNET_free (libname); 329 GNUNET_free (libname);
@@ -365,76 +347,66 @@ cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
365 347
366 348
367static void 349static void
368test (void *cls, 350test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
369 const struct GNUNET_SCHEDULER_TaskContext *tc) 351{
370{
371 struct CpsRunContext *crc = cls; 352 struct CpsRunContext *crc = cls;
372 int j; 353 int j;
373 354
374 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 355 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
375 { 356 {
376 GNUNET_break (0); 357 GNUNET_break (0);
377 crc->phase = RP_ERROR; 358 crc->phase = RP_ERROR;
378 } 359 }
379#if VERBOSE 360#if VERBOSE
380 fprintf (stderr, "In phase %d, iteration %u\n", 361 fprintf (stderr, "In phase %d, iteration %u\n", crc->phase, crc->cnt);
381 crc->phase,
382 crc->cnt);
383#endif 362#endif
384 switch (crc->phase) 363 switch (crc->phase)
364 {
365 case RP_ERROR:
366 GNUNET_break (0);
367 crc->api->drop (crc->api->cls);
368 ok = 1;
369 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
370 &cleaning_task, crc);
371 break;
372 case RP_PUT:
373 crc->start = GNUNET_TIME_absolute_get ();
374 for (j = 0; j < PUT_10; j++)
375 putValue (crc->api, j, crc->i);
376 crc->end = GNUNET_TIME_absolute_get ();
385 { 377 {
386 case RP_ERROR: 378 printf ("%s took %llu ms for %llu items\n",
387 GNUNET_break (0); 379 "Storing an item",
388 crc->api->drop (crc->api->cls); 380 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
389 ok = 1; 381 PUT_10);
390 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, 382 if (PUT_10 > 0)
391 &cleaning_task, crc); 383 GAUGER (category,
392 break; 384 "Storing an item",
393 case RP_PUT: 385 (crc->end.abs_value - crc->start.abs_value) / PUT_10,
394 crc->start = GNUNET_TIME_absolute_get (); 386 "ms/item");
395 for (j=0;j<PUT_10;j++)
396 putValue (crc->api, j, crc->i);
397 crc->end = GNUNET_TIME_absolute_get ();
398 {
399 printf ("%s took %llu ms for %llu items\n",
400 "Storing an item",
401 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
402 PUT_10);
403 if (PUT_10 > 0)
404 GAUGER (category,
405 "Storing an item",
406 (crc->end.abs_value - crc->start.abs_value) / PUT_10,
407 "ms/item");
408 }
409 crc->i++;
410 crc->start = GNUNET_TIME_absolute_get ();
411 crc->phase++;
412 GNUNET_SCHEDULER_add_now (&test, crc);
413 break;
414 case RP_REP_GET:
415 crc->api->get_replication (crc->api->cls,
416 &replication_get,
417 crc);
418 break;
419 case RP_ZA_GET:
420 crc->api->get_zero_anonymity (crc->api->cls,
421 crc->offset++,
422 1,
423 &iterate_zeros,
424 crc);
425 break;
426 case RP_EXP_GET:
427 crc->api->get_expiration (crc->api->cls,
428 &expiration_get,
429 crc);
430 break;
431 case RP_DONE:
432 crc->api->drop (crc->api->cls);
433 ok = 0;
434 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
435 &cleaning_task, crc);
436 break;
437 } 387 }
388 crc->i++;
389 crc->start = GNUNET_TIME_absolute_get ();
390 crc->phase++;
391 GNUNET_SCHEDULER_add_now (&test, crc);
392 break;
393 case RP_REP_GET:
394 crc->api->get_replication (crc->api->cls, &replication_get, crc);
395 break;
396 case RP_ZA_GET:
397 crc->api->get_zero_anonymity (crc->api->cls,
398 crc->offset++, 1, &iterate_zeros, crc);
399 break;
400 case RP_EXP_GET:
401 crc->api->get_expiration (crc->api->cls, &expiration_get, crc);
402 break;
403 case RP_DONE:
404 crc->api->drop (crc->api->cls);
405 ok = 0;
406 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
407 &cleaning_task, crc);
408 break;
409 }
438} 410}
439 411
440 412
@@ -445,20 +417,19 @@ static struct GNUNET_DATASTORE_PluginFunctions *
445load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg) 417load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
446{ 418{
447 static struct GNUNET_DATASTORE_PluginEnvironment env; 419 static struct GNUNET_DATASTORE_PluginEnvironment env;
448 struct GNUNET_DATASTORE_PluginFunctions * ret; 420 struct GNUNET_DATASTORE_PluginFunctions *ret;
449 char *name; 421 char *name;
450 char *libname; 422 char *libname;
451 423
452 if (GNUNET_OK != 424 if (GNUNET_OK !=
453 GNUNET_CONFIGURATION_get_value_string (cfg, 425 GNUNET_CONFIGURATION_get_value_string (cfg,
454 "DATASTORE", "DATABASE", &name)) 426 "DATASTORE", "DATABASE", &name))
455 { 427 {
456 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 428 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
457 _("No `%s' specified for `%s' in configuration!\n"), 429 _("No `%s' specified for `%s' in configuration!\n"),
458 "DATABASE", 430 "DATABASE", "DATASTORE");
459 "DATASTORE"); 431 return NULL;
460 return NULL; 432 }
461 }
462 env.cfg = cfg; 433 env.cfg = cfg;
463 env.duc = &disk_utilization_change_cb; 434 env.duc = &disk_utilization_change_cb;
464 env.cls = NULL; 435 env.cls = NULL;
@@ -466,12 +437,10 @@ load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
466 _("Loading `%s' datastore plugin\n"), name); 437 _("Loading `%s' datastore plugin\n"), name);
467 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name); 438 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
468 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env))) 439 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
469 { 440 {
470 fprintf (stderr, 441 fprintf (stderr, "Failed to load plugin `%s'!\n", name);
471 "Failed to load plugin `%s'!\n", 442 return NULL;
472 name); 443 }
473 return NULL;
474 }
475 GNUNET_free (libname); 444 GNUNET_free (libname);
476 GNUNET_free (name); 445 GNUNET_free (name);
477 return ret; 446 return ret;
@@ -481,20 +450,19 @@ load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
481static void 450static void
482run (void *cls, 451run (void *cls,
483 char *const *args, 452 char *const *args,
484 const char *cfgfile, 453 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
485 const struct GNUNET_CONFIGURATION_Handle *c)
486{ 454{
487 struct GNUNET_DATASTORE_PluginFunctions *api; 455 struct GNUNET_DATASTORE_PluginFunctions *api;
488 struct CpsRunContext *crc; 456 struct CpsRunContext *crc;
489 457
490 api = load_plugin (c); 458 api = load_plugin (c);
491 if (api == NULL) 459 if (api == NULL)
492 { 460 {
493 fprintf (stderr, 461 fprintf (stderr,
494 "Could not initialize plugin, assuming database not configured. Test not run!\n"); 462 "Could not initialize plugin, assuming database not configured. Test not run!\n");
495 return; 463 return;
496 } 464 }
497 crc = GNUNET_malloc(sizeof(struct CpsRunContext)); 465 crc = GNUNET_malloc (sizeof (struct CpsRunContext));
498 crc->api = api; 466 crc->api = api;
499 crc->cfg = c; 467 crc->cfg = c;
500 crc->phase = RP_PUT; 468 crc->phase = RP_PUT;
@@ -507,7 +475,8 @@ static int
507check () 475check ()
508{ 476{
509 char cfg_name[128]; 477 char cfg_name[128];
510 char *const argv[] = { 478
479 char *const argv[] = {
511 "perf-plugin-datastore", 480 "perf-plugin-datastore",
512 "-c", 481 "-c",
513 cfg_name, 482 cfg_name,
@@ -520,13 +489,10 @@ check ()
520 GNUNET_GETOPT_OPTION_END 489 GNUNET_GETOPT_OPTION_END
521 }; 490 };
522 491
523 GNUNET_snprintf (category, sizeof (category), 492 GNUNET_snprintf (category, sizeof (category), "DATASTORE-%s", plugin_name);
524 "DATASTORE-%s",
525 plugin_name);
526 GNUNET_snprintf (cfg_name, 493 GNUNET_snprintf (cfg_name,
527 sizeof (cfg_name), 494 sizeof (cfg_name),
528 "perf_plugin_datastore_data_%s.conf", 495 "perf_plugin_datastore_data_%s.conf", plugin_name);
529 plugin_name);
530 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, 496 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
531 argv, "perf-plugin-datastore", "nohelp", 497 argv, "perf-plugin-datastore", "nohelp",
532 options, &run, NULL); 498 options, &run, NULL);
@@ -546,17 +512,16 @@ main (int argc, char *argv[])
546 sleep (1); 512 sleep (1);
547 /* determine name of plugin to use */ 513 /* determine name of plugin to use */
548 plugin_name = argv[0]; 514 plugin_name = argv[0];
549 while (NULL != (pos = strstr(plugin_name, "_"))) 515 while (NULL != (pos = strstr (plugin_name, "_")))
550 plugin_name = pos+1; 516 plugin_name = pos + 1;
551 if (NULL != (pos = strstr(plugin_name, "."))) 517 if (NULL != (pos = strstr (plugin_name, ".")))
552 pos[0] = 0; 518 pos[0] = 0;
553 else 519 else
554 pos = (char *) plugin_name; 520 pos = (char *) plugin_name;
555 521
556 GNUNET_snprintf (dir_name, 522 GNUNET_snprintf (dir_name,
557 sizeof (dir_name), 523 sizeof (dir_name),
558 "/tmp/perf-gnunet-datastore-%s", 524 "/tmp/perf-gnunet-datastore-%s", plugin_name);
559 plugin_name);
560 GNUNET_DISK_directory_remove (dir_name); 525 GNUNET_DISK_directory_remove (dir_name);
561 GNUNET_log_setup ("perf-plugin-datastore", 526 GNUNET_log_setup ("perf-plugin-datastore",
562#if VERBOSE 527#if VERBOSE
@@ -574,5 +539,3 @@ main (int argc, char *argv[])
574} 539}
575 540
576/* end of perf_plugin_datastore.c */ 541/* end of perf_plugin_datastore.c */
577
578
diff --git a/src/datastore/plugin_datastore_mysql.c b/src/datastore/plugin_datastore_mysql.c
index 3d0da61a0..a9b039981 100644
--- a/src/datastore/plugin_datastore_mysql.c
+++ b/src/datastore/plugin_datastore_mysql.c
@@ -164,7 +164,7 @@ struct GNUNET_MysqlStatementHandle
164/** 164/**
165 * Context for all functions in this plugin. 165 * Context for all functions in this plugin.
166 */ 166 */
167struct Plugin 167struct Plugin
168{ 168{
169 /** 169 /**
170 * Our execution environment. 170 * Our execution environment.
@@ -175,7 +175,7 @@ struct Plugin
175 * Handle to talk to MySQL. 175 * Handle to talk to MySQL.
176 */ 176 */
177 MYSQL *dbf; 177 MYSQL *dbf;
178 178
179 /** 179 /**
180 * We keep all prepared statements in a DLL. This is the head. 180 * We keep all prepared statements in a DLL. This is the head.
181 */ 181 */
@@ -196,13 +196,13 @@ struct Plugin
196 */ 196 */
197#define INSERT_ENTRY "INSERT INTO gn090 (repl,type,prio,anonLevel,expire,rvalue,hash,vhash,value) VALUES (?,?,?,?,?,?,?,?,?)" 197#define INSERT_ENTRY "INSERT INTO gn090 (repl,type,prio,anonLevel,expire,rvalue,hash,vhash,value) VALUES (?,?,?,?,?,?,?,?,?)"
198 struct GNUNET_MysqlStatementHandle *insert_entry; 198 struct GNUNET_MysqlStatementHandle *insert_entry;
199 199
200#define DELETE_ENTRY_BY_UID "DELETE FROM gn090 WHERE uid=?" 200#define DELETE_ENTRY_BY_UID "DELETE FROM gn090 WHERE uid=?"
201 struct GNUNET_MysqlStatementHandle *delete_entry_by_uid; 201 struct GNUNET_MysqlStatementHandle *delete_entry_by_uid;
202 202
203#define COUNT_ENTRY_BY_HASH "SELECT count(*) FROM gn090 FORCE INDEX (idx_hash) WHERE hash=?" 203#define COUNT_ENTRY_BY_HASH "SELECT count(*) FROM gn090 FORCE INDEX (idx_hash) WHERE hash=?"
204 struct GNUNET_MysqlStatementHandle *count_entry_by_hash; 204 struct GNUNET_MysqlStatementHandle *count_entry_by_hash;
205 205
206#define SELECT_ENTRY_BY_HASH "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX (idx_hash) WHERE hash=? ORDER BY uid LIMIT 1 OFFSET ?" 206#define SELECT_ENTRY_BY_HASH "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX (idx_hash) WHERE hash=? ORDER BY uid LIMIT 1 OFFSET ?"
207 struct GNUNET_MysqlStatementHandle *select_entry_by_hash; 207 struct GNUNET_MysqlStatementHandle *select_entry_by_hash;
208 208
@@ -211,16 +211,16 @@ struct Plugin
211 211
212#define SELECT_ENTRY_BY_HASH_AND_VHASH "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX (idx_hash_vhash) WHERE hash=? AND vhash=? ORDER BY uid LIMIT 1 OFFSET ?" 212#define SELECT_ENTRY_BY_HASH_AND_VHASH "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX (idx_hash_vhash) WHERE hash=? AND vhash=? ORDER BY uid LIMIT 1 OFFSET ?"
213 struct GNUNET_MysqlStatementHandle *select_entry_by_hash_and_vhash; 213 struct GNUNET_MysqlStatementHandle *select_entry_by_hash_and_vhash;
214 214
215#define COUNT_ENTRY_BY_HASH_AND_TYPE "SELECT count(*) FROM gn090 FORCE INDEX (idx_hash_type_uid) WHERE hash=? AND type=?" 215#define COUNT_ENTRY_BY_HASH_AND_TYPE "SELECT count(*) FROM gn090 FORCE INDEX (idx_hash_type_uid) WHERE hash=? AND type=?"
216 struct GNUNET_MysqlStatementHandle *count_entry_by_hash_and_type; 216 struct GNUNET_MysqlStatementHandle *count_entry_by_hash_and_type;
217 217
218#define SELECT_ENTRY_BY_HASH_AND_TYPE "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX (idx_hash_type_uid) WHERE hash=? AND type=? ORDER BY uid LIMIT 1 OFFSET ?" 218#define SELECT_ENTRY_BY_HASH_AND_TYPE "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX (idx_hash_type_uid) WHERE hash=? AND type=? ORDER BY uid LIMIT 1 OFFSET ?"
219 struct GNUNET_MysqlStatementHandle *select_entry_by_hash_and_type; 219 struct GNUNET_MysqlStatementHandle *select_entry_by_hash_and_type;
220 220
221#define COUNT_ENTRY_BY_HASH_VHASH_AND_TYPE "SELECT count(*) FROM gn090 FORCE INDEX (idx_hash_vhash) WHERE hash=? AND vhash=? AND type=?" 221#define COUNT_ENTRY_BY_HASH_VHASH_AND_TYPE "SELECT count(*) FROM gn090 FORCE INDEX (idx_hash_vhash) WHERE hash=? AND vhash=? AND type=?"
222 struct GNUNET_MysqlStatementHandle *count_entry_by_hash_vhash_and_type; 222 struct GNUNET_MysqlStatementHandle *count_entry_by_hash_vhash_and_type;
223 223
224#define SELECT_ENTRY_BY_HASH_VHASH_AND_TYPE "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX (idx_hash_vhash) WHERE hash=? AND vhash=? AND type=? ORDER BY uid ASC LIMIT 1 OFFSET ?" 224#define SELECT_ENTRY_BY_HASH_VHASH_AND_TYPE "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX (idx_hash_vhash) WHERE hash=? AND vhash=? AND type=? ORDER BY uid ASC LIMIT 1 OFFSET ?"
225 struct GNUNET_MysqlStatementHandle *select_entry_by_hash_vhash_and_type; 225 struct GNUNET_MysqlStatementHandle *select_entry_by_hash_vhash_and_type;
226 226
@@ -274,6 +274,7 @@ get_my_cnf_path (const struct GNUNET_CONFIGURATION_Handle *cfg)
274 char *cnffile; 274 char *cnffile;
275 char *home_dir; 275 char *home_dir;
276 struct stat st; 276 struct stat st;
277
277#ifndef WINDOWS 278#ifndef WINDOWS
278 struct passwd *pw; 279 struct passwd *pw;
279#endif 280#endif
@@ -282,44 +283,47 @@ get_my_cnf_path (const struct GNUNET_CONFIGURATION_Handle *cfg)
282#ifndef WINDOWS 283#ifndef WINDOWS
283 pw = getpwuid (getuid ()); 284 pw = getpwuid (getuid ());
284 if (!pw) 285 if (!pw)
285 { 286 {
286 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, 287 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "getpwuid");
287 "getpwuid"); 288 return NULL;
288 return NULL; 289 }
289 }
290 if (GNUNET_YES == 290 if (GNUNET_YES ==
291 GNUNET_CONFIGURATION_have_value (cfg, 291 GNUNET_CONFIGURATION_have_value (cfg, "datastore-mysql", "CONFIG"))
292 "datastore-mysql", "CONFIG")) 292 {
293 { 293 GNUNET_assert (GNUNET_OK ==
294 GNUNET_assert (GNUNET_OK == 294 GNUNET_CONFIGURATION_get_value_filename (cfg,
295 GNUNET_CONFIGURATION_get_value_filename (cfg, 295 "datastore-mysql",
296 "datastore-mysql", "CONFIG", &cnffile)); 296 "CONFIG",
297 configured = GNUNET_YES; 297 &cnffile));
298 } 298 configured = GNUNET_YES;
299 }
299 else 300 else
300 { 301 {
301 home_dir = GNUNET_strdup (pw->pw_dir); 302 home_dir = GNUNET_strdup (pw->pw_dir);
303 GNUNET_asprintf (&cnffile, "%s/.my.cnf", home_dir);
304 GNUNET_free (home_dir);
305 configured = GNUNET_NO;
306 }
302#else 307#else
303 home_dir = (char *) GNUNET_malloc (_MAX_PATH + 1); 308 home_dir = (char *) GNUNET_malloc (_MAX_PATH + 1);
304 plibc_conv_to_win_path ("~/", home_dir); 309 plibc_conv_to_win_path ("~/", home_dir);
310 GNUNET_asprintf (&cnffile, "%s/.my.cnf", home_dir);
311 GNUNET_free (home_dir);
312 configured = GNUNET_NO;
305#endif 313#endif
306 GNUNET_asprintf (&cnffile, "%s/.my.cnf", home_dir); 314
307 GNUNET_free (home_dir);
308 configured = GNUNET_NO;
309 }
310 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 315 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
311 _("Trying to use file `%s' for MySQL configuration.\n"), 316 _("Trying to use file `%s' for MySQL configuration.\n"), cnffile);
312 cnffile);
313 if ((0 != STAT (cnffile, &st)) || 317 if ((0 != STAT (cnffile, &st)) ||
314 (0 != ACCESS (cnffile, R_OK)) || (!S_ISREG (st.st_mode))) 318 (0 != ACCESS (cnffile, R_OK)) || (!S_ISREG (st.st_mode)))
315 { 319 {
316 if (configured == GNUNET_YES) 320 if (configured == GNUNET_YES)
317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 321 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
318 _("Could not access file `%s': %s\n"), cnffile, 322 _("Could not access file `%s': %s\n"), cnffile,
319 STRERROR (errno)); 323 STRERROR (errno));
320 GNUNET_free (cnffile); 324 GNUNET_free (cnffile);
321 return NULL; 325 return NULL;
322 } 326 }
323 return cnffile; 327 return cnffile;
324} 328}
325 329
@@ -336,18 +340,18 @@ iclose (struct Plugin *plugin)
336 struct GNUNET_MysqlStatementHandle *s; 340 struct GNUNET_MysqlStatementHandle *s;
337 341
338 for (s = plugin->shead; s != NULL; s = s->next) 342 for (s = plugin->shead; s != NULL; s = s->next)
343 {
344 if (s->valid)
339 { 345 {
340 if (s->valid) 346 mysql_stmt_close (s->statement);
341 { 347 s->valid = GNUNET_NO;
342 mysql_stmt_close (s->statement);
343 s->valid = GNUNET_NO;
344 }
345 } 348 }
349 }
346 if (plugin->dbf != NULL) 350 if (plugin->dbf != NULL)
347 { 351 {
348 mysql_close (plugin->dbf); 352 mysql_close (plugin->dbf);
349 plugin->dbf = NULL; 353 plugin->dbf = NULL;
350 } 354 }
351 return GNUNET_OK; 355 return GNUNET_OK;
352} 356}
353 357
@@ -378,75 +382,79 @@ iopen (struct Plugin *plugin)
378 mysql_options (plugin->dbf, MYSQL_READ_DEFAULT_GROUP, "client"); 382 mysql_options (plugin->dbf, MYSQL_READ_DEFAULT_GROUP, "client");
379 reconnect = 0; 383 reconnect = 0;
380 mysql_options (plugin->dbf, MYSQL_OPT_RECONNECT, &reconnect); 384 mysql_options (plugin->dbf, MYSQL_OPT_RECONNECT, &reconnect);
381 timeout = 120; /* in seconds */ 385 timeout = 120; /* in seconds */
382 mysql_options (plugin->dbf, 386 mysql_options (plugin->dbf,
383 MYSQL_OPT_CONNECT_TIMEOUT, (const void *) &timeout); 387 MYSQL_OPT_CONNECT_TIMEOUT, (const void *) &timeout);
384 mysql_options(plugin->dbf, MYSQL_SET_CHARSET_NAME, "UTF8"); 388 mysql_options (plugin->dbf, MYSQL_SET_CHARSET_NAME, "UTF8");
385 timeout = 60; /* in seconds */ 389 timeout = 60; /* in seconds */
386 mysql_options (plugin->dbf, MYSQL_OPT_READ_TIMEOUT, (const void *) &timeout); 390 mysql_options (plugin->dbf, MYSQL_OPT_READ_TIMEOUT, (const void *) &timeout);
387 mysql_options (plugin->dbf, MYSQL_OPT_WRITE_TIMEOUT, (const void *) &timeout); 391 mysql_options (plugin->dbf, MYSQL_OPT_WRITE_TIMEOUT, (const void *) &timeout);
388 mysql_dbname = NULL; 392 mysql_dbname = NULL;
389 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (plugin->env->cfg, 393 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (plugin->env->cfg,
390 "datastore-mysql", "DATABASE")) 394 "datastore-mysql",
391 GNUNET_assert (GNUNET_OK == 395 "DATABASE"))
392 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, 396 GNUNET_assert (GNUNET_OK ==
393 "datastore-mysql", "DATABASE", 397 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
394 &mysql_dbname)); 398 "datastore-mysql",
399 "DATABASE",
400 &mysql_dbname));
395 else 401 else
396 mysql_dbname = GNUNET_strdup ("gnunet"); 402 mysql_dbname = GNUNET_strdup ("gnunet");
397 mysql_user = NULL; 403 mysql_user = NULL;
398 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (plugin->env->cfg, 404 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (plugin->env->cfg,
399 "datastore-mysql", "USER")) 405 "datastore-mysql", "USER"))
400 { 406 {
401 GNUNET_assert (GNUNET_OK == 407 GNUNET_assert (GNUNET_OK ==
402 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, 408 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
403 "datastore-mysql", "USER", 409 "datastore-mysql",
404 &mysql_user)); 410 "USER", &mysql_user));
405 } 411 }
406 mysql_password = NULL; 412 mysql_password = NULL;
407 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (plugin->env->cfg, 413 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (plugin->env->cfg,
408 "datastore-mysql", "PASSWORD")) 414 "datastore-mysql",
409 { 415 "PASSWORD"))
410 GNUNET_assert (GNUNET_OK == 416 {
411 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, 417 GNUNET_assert (GNUNET_OK ==
412 "datastore-mysql", "PASSWORD", 418 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
413 &mysql_password)); 419 "datastore-mysql",
414 } 420 "PASSWORD",
421 &mysql_password));
422 }
415 mysql_server = NULL; 423 mysql_server = NULL;
416 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (plugin->env->cfg, 424 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (plugin->env->cfg,
417 "datastore-mysql", "HOST")) 425 "datastore-mysql", "HOST"))
418 { 426 {
419 GNUNET_assert (GNUNET_OK == 427 GNUNET_assert (GNUNET_OK ==
420 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, 428 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
421 "datastore-mysql", "HOST", 429 "datastore-mysql",
422 &mysql_server)); 430 "HOST",
423 } 431 &mysql_server));
432 }
424 mysql_port = 0; 433 mysql_port = 0;
425 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (plugin->env->cfg, 434 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (plugin->env->cfg,
426 "datastore-mysql", "PORT")) 435 "datastore-mysql", "PORT"))
427 { 436 {
428 GNUNET_assert (GNUNET_OK == 437 GNUNET_assert (GNUNET_OK ==
429 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, "datastore-mysql", 438 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
430 "PORT", &mysql_port)); 439 "datastore-mysql",
431 } 440 "PORT", &mysql_port));
441 }
432 442
433 GNUNET_assert (mysql_dbname != NULL); 443 GNUNET_assert (mysql_dbname != NULL);
434 mysql_real_connect (plugin->dbf, 444 mysql_real_connect (plugin->dbf,
435 mysql_server, 445 mysql_server,
436 mysql_user, mysql_password, 446 mysql_user, mysql_password,
437 mysql_dbname, 447 mysql_dbname,
438 (unsigned int) mysql_port, NULL, 448 (unsigned int) mysql_port, NULL, CLIENT_IGNORE_SIGPIPE);
439 CLIENT_IGNORE_SIGPIPE);
440 GNUNET_free_non_null (mysql_server); 449 GNUNET_free_non_null (mysql_server);
441 GNUNET_free_non_null (mysql_user); 450 GNUNET_free_non_null (mysql_user);
442 GNUNET_free_non_null (mysql_password); 451 GNUNET_free_non_null (mysql_password);
443 GNUNET_free (mysql_dbname); 452 GNUNET_free (mysql_dbname);
444 if (mysql_error (plugin->dbf)[0]) 453 if (mysql_error (plugin->dbf)[0])
445 { 454 {
446 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, 455 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_real_connect", plugin);
447 "mysql_real_connect", plugin); 456 return GNUNET_SYSERR;
448 return GNUNET_SYSERR; 457 }
449 }
450 return GNUNET_OK; 458 return GNUNET_OK;
451} 459}
452 460
@@ -459,19 +467,17 @@ iopen (struct Plugin *plugin)
459 * @return GNUNET_OK on success, GNUNET_SYSERR on error 467 * @return GNUNET_OK on success, GNUNET_SYSERR on error
460 */ 468 */
461static int 469static int
462run_statement (struct Plugin *plugin, 470run_statement (struct Plugin *plugin, const char *statement)
463 const char *statement)
464{ 471{
465 if ((NULL == plugin->dbf) && (GNUNET_OK != iopen (plugin))) 472 if ((NULL == plugin->dbf) && (GNUNET_OK != iopen (plugin)))
466 return GNUNET_SYSERR; 473 return GNUNET_SYSERR;
467 mysql_query (plugin->dbf, statement); 474 mysql_query (plugin->dbf, statement);
468 if (mysql_error (plugin->dbf)[0]) 475 if (mysql_error (plugin->dbf)[0])
469 { 476 {
470 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, 477 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_query", plugin);
471 "mysql_query", plugin); 478 iclose (plugin);
472 iclose (plugin); 479 return GNUNET_SYSERR;
473 return GNUNET_SYSERR; 480 }
474 }
475 return GNUNET_OK; 481 return GNUNET_OK;
476} 482}
477 483
@@ -484,16 +490,13 @@ run_statement (struct Plugin *plugin,
484 * @return NULL on error 490 * @return NULL on error
485 */ 491 */
486static struct GNUNET_MysqlStatementHandle * 492static struct GNUNET_MysqlStatementHandle *
487prepared_statement_create (struct Plugin *plugin, 493prepared_statement_create (struct Plugin *plugin, const char *statement)
488 const char *statement)
489{ 494{
490 struct GNUNET_MysqlStatementHandle *ret; 495 struct GNUNET_MysqlStatementHandle *ret;
491 496
492 ret = GNUNET_malloc (sizeof (struct GNUNET_MysqlStatementHandle)); 497 ret = GNUNET_malloc (sizeof (struct GNUNET_MysqlStatementHandle));
493 ret->query = GNUNET_strdup (statement); 498 ret->query = GNUNET_strdup (statement);
494 GNUNET_CONTAINER_DLL_insert (plugin->shead, 499 GNUNET_CONTAINER_DLL_insert (plugin->shead, plugin->stail, ret);
495 plugin->stail,
496 ret);
497 return ret; 500 return ret;
498} 501}
499 502
@@ -506,36 +509,30 @@ prepared_statement_create (struct Plugin *plugin,
506 * @return GNUNET_OK on success 509 * @return GNUNET_OK on success
507 */ 510 */
508static int 511static int
509prepare_statement (struct Plugin *plugin, 512prepare_statement (struct Plugin *plugin,
510 struct GNUNET_MysqlStatementHandle *ret) 513 struct GNUNET_MysqlStatementHandle *ret)
511{ 514{
512 if (GNUNET_YES == ret->valid) 515 if (GNUNET_YES == ret->valid)
513 return GNUNET_OK; 516 return GNUNET_OK;
514 if ((NULL == plugin->dbf) && 517 if ((NULL == plugin->dbf) && (GNUNET_OK != iopen (plugin)))
515 (GNUNET_OK != iopen (plugin)))
516 return GNUNET_SYSERR; 518 return GNUNET_SYSERR;
517 ret->statement = mysql_stmt_init (plugin->dbf); 519 ret->statement = mysql_stmt_init (plugin->dbf);
518 if (ret->statement == NULL) 520 if (ret->statement == NULL)
519 { 521 {
520 iclose (plugin); 522 iclose (plugin);
521 return GNUNET_SYSERR; 523 return GNUNET_SYSERR;
522 } 524 }
523 if (mysql_stmt_prepare (ret->statement, 525 if (mysql_stmt_prepare (ret->statement, ret->query, strlen (ret->query)))
524 ret->query, 526 {
525 strlen (ret->query))) 527 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
526 { 528 "mysql",
527 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 529 _("Failed to prepare statement `%s'\n"), ret->query);
528 "mysql", 530 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_stmt_prepare", plugin);
529 _("Failed to prepare statement `%s'\n"), 531 mysql_stmt_close (ret->statement);
530 ret->query); 532 ret->statement = NULL;
531 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, 533 iclose (plugin);
532 "mysql_stmt_prepare", 534 return GNUNET_SYSERR;
533 plugin); 535 }
534 mysql_stmt_close (ret->statement);
535 ret->statement = NULL;
536 iclose (plugin);
537 return GNUNET_SYSERR;
538 }
539 ret->valid = GNUNET_YES; 536 ret->valid = GNUNET_YES;
540 return GNUNET_OK; 537 return GNUNET_OK;
541 538
@@ -553,8 +550,7 @@ prepare_statement (struct Plugin *plugin,
553 */ 550 */
554static int 551static int
555init_params (struct Plugin *plugin, 552init_params (struct Plugin *plugin,
556 struct GNUNET_MysqlStatementHandle *s, 553 struct GNUNET_MysqlStatementHandle *s, va_list ap)
557 va_list ap)
558{ 554{
559 MYSQL_BIND qbind[MAX_PARAM]; 555 MYSQL_BIND qbind[MAX_PARAM];
560 unsigned int pc; 556 unsigned int pc;
@@ -563,69 +559,72 @@ init_params (struct Plugin *plugin,
563 559
564 pc = mysql_stmt_param_count (s->statement); 560 pc = mysql_stmt_param_count (s->statement);
565 if (pc > MAX_PARAM) 561 if (pc > MAX_PARAM)
566 { 562 {
567 /* increase internal constant! */ 563 /* increase internal constant! */
568 GNUNET_break (0); 564 GNUNET_break (0);
569 return GNUNET_SYSERR; 565 return GNUNET_SYSERR;
570 } 566 }
571 memset (qbind, 0, sizeof (qbind)); 567 memset (qbind, 0, sizeof (qbind));
572 off = 0; 568 off = 0;
573 ft = 0; 569 ft = 0;
574 while ((pc > 0) && (-1 != (int) (ft = va_arg (ap, enum enum_field_types)))) 570 while ((pc > 0) && (-1 != (int) (ft = va_arg (ap, enum enum_field_types))))
571 {
572 qbind[off].buffer_type = ft;
573 switch (ft)
575 { 574 {
576 qbind[off].buffer_type = ft; 575 case MYSQL_TYPE_FLOAT:
577 switch (ft) 576 qbind[off].buffer = va_arg (ap, float *);
578 { 577
579 case MYSQL_TYPE_FLOAT: 578 break;
580 qbind[off].buffer = va_arg (ap, float *); 579 case MYSQL_TYPE_LONGLONG:
581 break; 580 qbind[off].buffer = va_arg (ap, unsigned long long *);
582 case MYSQL_TYPE_LONGLONG: 581 qbind[off].is_unsigned = va_arg (ap, int);
583 qbind[off].buffer = va_arg (ap, unsigned long long *); 582
584 qbind[off].is_unsigned = va_arg (ap, int); 583 break;
585 break; 584 case MYSQL_TYPE_LONG:
586 case MYSQL_TYPE_LONG: 585 qbind[off].buffer = va_arg (ap, unsigned int *);
587 qbind[off].buffer = va_arg (ap, unsigned int *); 586 qbind[off].is_unsigned = va_arg (ap, int);
588 qbind[off].is_unsigned = va_arg (ap, int); 587
589 break; 588 break;
590 case MYSQL_TYPE_VAR_STRING: 589 case MYSQL_TYPE_VAR_STRING:
591 case MYSQL_TYPE_STRING: 590 case MYSQL_TYPE_STRING:
592 case MYSQL_TYPE_BLOB: 591 case MYSQL_TYPE_BLOB:
593 qbind[off].buffer = va_arg (ap, void *); 592 qbind[off].buffer = va_arg (ap, void *);
594 qbind[off].buffer_length = va_arg (ap, unsigned long); 593 qbind[off].buffer_length = va_arg (ap, unsigned long);
595 qbind[off].length = va_arg (ap, unsigned long *); 594 qbind[off].length = va_arg (ap, unsigned long *);
596 break; 595
597 default: 596 break;
598 /* unsupported type */ 597 default:
599 GNUNET_break (0); 598 /* unsupported type */
600 return GNUNET_SYSERR; 599 GNUNET_break (0);
601 }
602 pc--;
603 off++;
604 }
605 if (! ( (pc == 0) && (-1 != (int) ft) && (va_arg (ap, int) == -1) ) )
606 {
607 GNUNET_assert (0);
608 return GNUNET_SYSERR; 600 return GNUNET_SYSERR;
609 } 601 }
602 pc--;
603 off++;
604 }
605 if (!((pc == 0) && (-1 != (int) ft) && (va_arg (ap, int) == -1)))
606 {
607 GNUNET_assert (0);
608 return GNUNET_SYSERR;
609 }
610 if (mysql_stmt_bind_param (s->statement, qbind)) 610 if (mysql_stmt_bind_param (s->statement, qbind))
611 { 611 {
612 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 612 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
613 _("`%s' failed at %s:%d with error: %s\n"), 613 _("`%s' failed at %s:%d with error: %s\n"),
614 "mysql_stmt_bind_param", 614 "mysql_stmt_bind_param",
615 __FILE__, __LINE__, mysql_stmt_error (s->statement)); 615 __FILE__, __LINE__, mysql_stmt_error (s->statement));
616 iclose (plugin); 616 iclose (plugin);
617 return GNUNET_SYSERR; 617 return GNUNET_SYSERR;
618 } 618 }
619 if (mysql_stmt_execute (s->statement)) 619 if (mysql_stmt_execute (s->statement))
620 { 620 {
621 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 621 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
622 _("`%s' for `%s' failed at %s:%d with error: %s\n"), 622 _("`%s' for `%s' failed at %s:%d with error: %s\n"),
623 "mysql_stmt_execute", 623 "mysql_stmt_execute",
624 s->query, 624 s->query, __FILE__, __LINE__, mysql_stmt_error (s->statement));
625 __FILE__, __LINE__, mysql_stmt_error (s->statement)); 625 iclose (plugin);
626 iclose (plugin); 626 return GNUNET_SYSERR;
627 return GNUNET_SYSERR; 627 }
628 }
629 return GNUNET_OK; 628 return GNUNET_OK;
630} 629}
631 630
@@ -645,51 +644,50 @@ init_params (struct Plugin *plugin,
645 */ 644 */
646static int 645static int
647prepared_statement_run_select_va (struct Plugin *plugin, 646prepared_statement_run_select_va (struct Plugin *plugin,
648 struct GNUNET_MysqlStatementHandle *s, 647 struct GNUNET_MysqlStatementHandle *s,
649 unsigned int result_size, 648 unsigned int result_size,
650 MYSQL_BIND *results, 649 MYSQL_BIND * results, va_list ap)
651 va_list ap)
652{ 650{
653 int ret; 651 int ret;
654 unsigned int rsize; 652 unsigned int rsize;
655 653
656 if (GNUNET_OK != prepare_statement (plugin, s)) 654 if (GNUNET_OK != prepare_statement (plugin, s))
657 { 655 {
658 GNUNET_break (0); 656 GNUNET_break (0);
659 return GNUNET_SYSERR; 657 return GNUNET_SYSERR;
660 } 658 }
661 if (GNUNET_OK != init_params (plugin, s, ap)) 659 if (GNUNET_OK != init_params (plugin, s, ap))
662 { 660 {
663 GNUNET_break (0); 661 GNUNET_break (0);
664 return GNUNET_SYSERR; 662 return GNUNET_SYSERR;
665 } 663 }
666 rsize = mysql_stmt_field_count (s->statement); 664 rsize = mysql_stmt_field_count (s->statement);
667 if (rsize > result_size) 665 if (rsize > result_size)
668 { 666 {
669 GNUNET_break (0); 667 GNUNET_break (0);
670 return GNUNET_SYSERR; 668 return GNUNET_SYSERR;
671 } 669 }
672 if (mysql_stmt_bind_result (s->statement, results)) 670 if (mysql_stmt_bind_result (s->statement, results))
673 { 671 {
674 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
675 _("`%s' failed at %s:%d with error: %s\n"), 673 _("`%s' failed at %s:%d with error: %s\n"),
676 "mysql_stmt_bind_result", 674 "mysql_stmt_bind_result",
677 __FILE__, __LINE__, mysql_stmt_error (s->statement)); 675 __FILE__, __LINE__, mysql_stmt_error (s->statement));
678 iclose (plugin); 676 iclose (plugin);
679 return GNUNET_SYSERR; 677 return GNUNET_SYSERR;
680 } 678 }
681 ret = mysql_stmt_fetch (s->statement); 679 ret = mysql_stmt_fetch (s->statement);
682 if (ret == MYSQL_NO_DATA) 680 if (ret == MYSQL_NO_DATA)
683 return GNUNET_NO; 681 return GNUNET_NO;
684 if (ret != 0) 682 if (ret != 0)
685 { 683 {
686 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 684 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
687 _("`%s' failed at %s:%d with error: %s\n"), 685 _("`%s' failed at %s:%d with error: %s\n"),
688 "mysql_stmt_fetch", 686 "mysql_stmt_fetch",
689 __FILE__, __LINE__, mysql_stmt_error (s->statement)); 687 __FILE__, __LINE__, mysql_stmt_error (s->statement));
690 iclose (plugin); 688 iclose (plugin);
691 return GNUNET_SYSERR; 689 return GNUNET_SYSERR;
692 } 690 }
693 mysql_stmt_reset (s->statement); 691 mysql_stmt_reset (s->statement);
694 return GNUNET_OK; 692 return GNUNET_OK;
695} 693}
@@ -711,18 +709,15 @@ prepared_statement_run_select_va (struct Plugin *plugin,
711 */ 709 */
712static int 710static int
713prepared_statement_run_select (struct Plugin *plugin, 711prepared_statement_run_select (struct Plugin *plugin,
714 struct GNUNET_MysqlStatementHandle *s, 712 struct GNUNET_MysqlStatementHandle *s,
715 unsigned int result_size, 713 unsigned int result_size,
716 MYSQL_BIND *results, 714 MYSQL_BIND * results, ...)
717 ...)
718{ 715{
719 va_list ap; 716 va_list ap;
720 int ret; 717 int ret;
721 718
722 va_start (ap, results); 719 va_start (ap, results);
723 ret = prepared_statement_run_select_va (plugin, s, 720 ret = prepared_statement_run_select_va (plugin, s, result_size, results, ap);
724 result_size, results,
725 ap);
726 va_end (ap); 721 va_end (ap);
727 return ret; 722 return ret;
728} 723}
@@ -743,8 +738,8 @@ prepared_statement_run_select (struct Plugin *plugin,
743 */ 738 */
744static int 739static int
745prepared_statement_run (struct Plugin *plugin, 740prepared_statement_run (struct Plugin *plugin,
746 struct GNUNET_MysqlStatementHandle *s, 741 struct GNUNET_MysqlStatementHandle *s,
747 unsigned long long *insert_id, ...) 742 unsigned long long *insert_id, ...)
748{ 743{
749 va_list ap; 744 va_list ap;
750 int affected; 745 int affected;
@@ -753,10 +748,10 @@ prepared_statement_run (struct Plugin *plugin,
753 return GNUNET_SYSERR; 748 return GNUNET_SYSERR;
754 va_start (ap, insert_id); 749 va_start (ap, insert_id);
755 if (GNUNET_OK != init_params (plugin, s, ap)) 750 if (GNUNET_OK != init_params (plugin, s, ap))
756 { 751 {
757 va_end (ap); 752 va_end (ap);
758 return GNUNET_SYSERR; 753 return GNUNET_SYSERR;
759 } 754 }
760 va_end (ap); 755 va_end (ap);
761 affected = mysql_stmt_affected_rows (s->statement); 756 affected = mysql_stmt_affected_rows (s->statement);
762 if (NULL != insert_id) 757 if (NULL != insert_id)
@@ -774,26 +769,22 @@ prepared_statement_run (struct Plugin *plugin,
774 * @return GNUNET_OK on success, GNUNET_NO if no such value exists, GNUNET_SYSERR on error 769 * @return GNUNET_OK on success, GNUNET_NO if no such value exists, GNUNET_SYSERR on error
775 */ 770 */
776static int 771static int
777do_delete_entry (struct Plugin *plugin, 772do_delete_entry (struct Plugin *plugin, unsigned long long uid)
778 unsigned long long uid)
779{ 773{
780 int ret; 774 int ret;
781 775
782#if DEBUG_MYSQL 776#if DEBUG_MYSQL
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
784 "Deleting value %llu from gn090 table\n", 778 "Deleting value %llu from gn090 table\n", uid);
785 uid);
786#endif 779#endif
787 ret = prepared_statement_run (plugin, 780 ret = prepared_statement_run (plugin,
788 plugin->delete_entry_by_uid, 781 plugin->delete_entry_by_uid,
789 NULL, 782 NULL,
790 MYSQL_TYPE_LONGLONG, &uid, GNUNET_YES, 783 MYSQL_TYPE_LONGLONG, &uid, GNUNET_YES, -1);
791 -1);
792 if (ret >= 0) 784 if (ret >= 0)
793 return GNUNET_OK; 785 return GNUNET_OK;
794 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 786 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
795 "Deleting value %llu from gn090 table failed\n", 787 "Deleting value %llu from gn090 table failed\n", uid);
796 uid);
797 return ret; 788 return ret;
798} 789}
799 790
@@ -817,11 +808,8 @@ mysql_plugin_estimate_size (void *cls)
817 cbind[0].buffer_type = MYSQL_TYPE_LONGLONG; 808 cbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
818 cbind[0].buffer = &total; 809 cbind[0].buffer = &total;
819 cbind[0].is_unsigned = GNUNET_NO; 810 cbind[0].is_unsigned = GNUNET_NO;
820 if (GNUNET_OK != 811 if (GNUNET_OK !=
821 prepared_statement_run_select (plugin, 812 prepared_statement_run_select (plugin, plugin->get_size, 1, cbind, -1))
822 plugin->get_size,
823 1, cbind,
824 -1))
825 return 0; 813 return 0;
826 return total; 814 return total;
827} 815}
@@ -844,61 +832,58 @@ mysql_plugin_estimate_size (void *cls)
844 */ 832 */
845static int 833static int
846mysql_plugin_put (void *cls, 834mysql_plugin_put (void *cls,
847 const GNUNET_HashCode * key, 835 const GNUNET_HashCode * key,
848 uint32_t size, 836 uint32_t size,
849 const void *data, 837 const void *data,
850 enum GNUNET_BLOCK_Type type, 838 enum GNUNET_BLOCK_Type type,
851 uint32_t priority, 839 uint32_t priority,
852 uint32_t anonymity, 840 uint32_t anonymity,
853 uint32_t replication, 841 uint32_t replication,
854 struct GNUNET_TIME_Absolute expiration, 842 struct GNUNET_TIME_Absolute expiration, char **msg)
855 char **msg)
856{ 843{
857 struct Plugin *plugin = cls; 844 struct Plugin *plugin = cls;
858 unsigned int irepl = replication; 845 unsigned int irepl = replication;
859 unsigned int ipriority = priority; 846 unsigned int ipriority = priority;
860 unsigned int ianonymity = anonymity; 847 unsigned int ianonymity = anonymity;
861 unsigned long long lexpiration = expiration.abs_value; 848 unsigned long long lexpiration = expiration.abs_value;
862 unsigned long long lrvalue = (unsigned long long) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, 849 unsigned long long lrvalue =
863 UINT64_MAX); 850 (unsigned long long) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
851 UINT64_MAX);
864 unsigned long hashSize; 852 unsigned long hashSize;
865 unsigned long hashSize2; 853 unsigned long hashSize2;
866 unsigned long lsize; 854 unsigned long lsize;
867 GNUNET_HashCode vhash; 855 GNUNET_HashCode vhash;
868 856
869 if (size > MAX_DATUM_SIZE) 857 if (size > MAX_DATUM_SIZE)
870 { 858 {
871 GNUNET_break (0); 859 GNUNET_break (0);
872 return GNUNET_SYSERR; 860 return GNUNET_SYSERR;
873 } 861 }
874 hashSize = sizeof (GNUNET_HashCode); 862 hashSize = sizeof (GNUNET_HashCode);
875 hashSize2 = sizeof (GNUNET_HashCode); 863 hashSize2 = sizeof (GNUNET_HashCode);
876 lsize = size; 864 lsize = size;
877 GNUNET_CRYPTO_hash (data, size, &vhash); 865 GNUNET_CRYPTO_hash (data, size, &vhash);
878 if (GNUNET_OK != 866 if (GNUNET_OK !=
879 prepared_statement_run (plugin, 867 prepared_statement_run (plugin,
880 plugin->insert_entry, 868 plugin->insert_entry,
881 NULL, 869 NULL,
882 MYSQL_TYPE_LONG, &irepl, GNUNET_YES, 870 MYSQL_TYPE_LONG, &irepl, GNUNET_YES,
883 MYSQL_TYPE_LONG, &type, GNUNET_YES, 871 MYSQL_TYPE_LONG, &type, GNUNET_YES,
884 MYSQL_TYPE_LONG, &ipriority, GNUNET_YES, 872 MYSQL_TYPE_LONG, &ipriority, GNUNET_YES,
885 MYSQL_TYPE_LONG, &ianonymity, GNUNET_YES, 873 MYSQL_TYPE_LONG, &ianonymity, GNUNET_YES,
886 MYSQL_TYPE_LONGLONG, &lexpiration, GNUNET_YES, 874 MYSQL_TYPE_LONGLONG, &lexpiration, GNUNET_YES,
887 MYSQL_TYPE_LONGLONG, &lrvalue, GNUNET_YES, 875 MYSQL_TYPE_LONGLONG, &lrvalue, GNUNET_YES,
888 MYSQL_TYPE_BLOB, key, hashSize, &hashSize, 876 MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
889 MYSQL_TYPE_BLOB, &vhash, hashSize2, &hashSize2, 877 MYSQL_TYPE_BLOB, &vhash, hashSize2, &hashSize2,
890 MYSQL_TYPE_BLOB, data, lsize, &lsize, 878 MYSQL_TYPE_BLOB, data, lsize, &lsize, -1))
891 -1)) 879 return GNUNET_SYSERR;
892 return GNUNET_SYSERR;
893#if DEBUG_MYSQL 880#if DEBUG_MYSQL
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
895 "Inserted value `%s' with size %u into gn090 table\n", 882 "Inserted value `%s' with size %u into gn090 table\n",
896 GNUNET_h2s (key), 883 GNUNET_h2s (key), (unsigned int) size);
897 (unsigned int) size);
898#endif 884#endif
899 if (size > 0) 885 if (size > 0)
900 plugin->env->duc (plugin->env->cls, 886 plugin->env->duc (plugin->env->cls, size);
901 size);
902 return GNUNET_OK; 887 return GNUNET_OK;
903} 888}
904 889
@@ -928,10 +913,8 @@ mysql_plugin_put (void *cls,
928 */ 913 */
929static int 914static int
930mysql_plugin_update (void *cls, 915mysql_plugin_update (void *cls,
931 uint64_t uid, 916 uint64_t uid,
932 int delta, 917 int delta, struct GNUNET_TIME_Absolute expire, char **msg)
933 struct GNUNET_TIME_Absolute expire,
934 char **msg)
935{ 918{
936 struct Plugin *plugin = cls; 919 struct Plugin *plugin = cls;
937 unsigned long long vkey = uid; 920 unsigned long long vkey = uid;
@@ -940,25 +923,21 @@ mysql_plugin_update (void *cls,
940 923
941#if DEBUG_MYSQL 924#if DEBUG_MYSQL
942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
943 "Updating value %llu adding %d to priority and maxing exp at %llu\n", 926 "Updating value %llu adding %d to priority and maxing exp at %llu\n",
944 vkey, 927 vkey, delta, lexpire);
945 delta,
946 lexpire);
947#endif 928#endif
948 ret = prepared_statement_run (plugin, 929 ret = prepared_statement_run (plugin,
949 plugin->update_entry, 930 plugin->update_entry,
950 NULL, 931 NULL,
951 MYSQL_TYPE_LONG, &delta, GNUNET_NO, 932 MYSQL_TYPE_LONG, &delta, GNUNET_NO,
952 MYSQL_TYPE_LONGLONG, &lexpire, GNUNET_YES, 933 MYSQL_TYPE_LONGLONG, &lexpire, GNUNET_YES,
953 MYSQL_TYPE_LONGLONG, &lexpire, GNUNET_YES, 934 MYSQL_TYPE_LONGLONG, &lexpire, GNUNET_YES,
954 MYSQL_TYPE_LONGLONG, &vkey, GNUNET_YES, 935 MYSQL_TYPE_LONGLONG, &vkey, GNUNET_YES, -1);
955 -1);
956 if (ret != GNUNET_OK) 936 if (ret != GNUNET_OK)
957 { 937 {
958 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 938 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
959 "Failed to update value %llu\n", 939 "Failed to update value %llu\n", vkey);
960 vkey); 940 }
961 }
962 return ret; 941 return ret;
963} 942}
964 943
@@ -973,11 +952,10 @@ mysql_plugin_update (void *cls,
973 * @param proc_cls closure for proc 952 * @param proc_cls closure for proc
974 * @param ... arguments to initialize stmt 953 * @param ... arguments to initialize stmt
975 */ 954 */
976static void 955static void
977execute_select (struct Plugin *plugin, 956execute_select (struct Plugin *plugin,
978 struct GNUNET_MysqlStatementHandle *stmt, 957 struct GNUNET_MysqlStatementHandle *stmt,
979 PluginDatumProcessor proc, void *proc_cls, 958 PluginDatumProcessor proc, void *proc_cls, ...)
980 ...)
981{ 959{
982 va_list ap; 960 va_list ap;
983 int ret; 961 int ret;
@@ -1020,51 +998,36 @@ execute_select (struct Plugin *plugin,
1020 rbind[6].is_unsigned = 1; 998 rbind[6].is_unsigned = 1;
1021 999
1022 va_start (ap, proc_cls); 1000 va_start (ap, proc_cls);
1023 ret = prepared_statement_run_select_va (plugin, 1001 ret = prepared_statement_run_select_va (plugin, stmt, 7, rbind, ap);
1024 stmt,
1025 7, rbind,
1026 ap);
1027 va_end (ap); 1002 va_end (ap);
1028 if (ret <= 0) 1003 if (ret <= 0)
1029 { 1004 {
1030 proc (proc_cls, 1005 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1031 NULL, 0, NULL, 0, 0, 0, 1006 return;
1032 GNUNET_TIME_UNIT_ZERO_ABS, 0); 1007 }
1033 return; 1008 GNUNET_assert (size <= sizeof (value));
1034 } 1009 if ((rbind[4].buffer_length != sizeof (GNUNET_HashCode)) ||
1035 GNUNET_assert (size <= sizeof(value)); 1010 (hashSize != sizeof (GNUNET_HashCode)))
1036 if ( (rbind[4].buffer_length != sizeof (GNUNET_HashCode)) || 1011 {
1037 (hashSize != sizeof (GNUNET_HashCode)) ) 1012 GNUNET_break (0);
1038 { 1013 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1039 GNUNET_break (0); 1014 return;
1040 proc (proc_cls, 1015 }
1041 NULL, 0, NULL, 0, 0, 0,
1042 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1043 return;
1044 }
1045#if DEBUG_MYSQL 1016#if DEBUG_MYSQL
1046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1047 "Found %u-byte value under key `%s' with prio %u, anon %u, expire %llu selecting from gn090 table\n", 1018 "Found %u-byte value under key `%s' with prio %u, anon %u, expire %llu selecting from gn090 table\n",
1048 (unsigned int) size, 1019 (unsigned int) size, GNUNET_h2s (&key), priority, anonymity, exp);
1049 GNUNET_h2s (&key),
1050 priority,
1051 anonymity,
1052 exp);
1053#endif 1020#endif
1054 GNUNET_assert (size < MAX_DATUM_SIZE); 1021 GNUNET_assert (size < MAX_DATUM_SIZE);
1055 expiration.abs_value = exp; 1022 expiration.abs_value = exp;
1056 ret = proc (proc_cls, 1023 ret = proc (proc_cls,
1057 &key, 1024 &key, size, value, type, priority, anonymity, expiration, uid);
1058 size, value,
1059 type, priority, anonymity, expiration,
1060 uid);
1061 if (ret == GNUNET_NO) 1025 if (ret == GNUNET_NO)
1062 { 1026 {
1063 do_delete_entry (plugin, uid); 1027 do_delete_entry (plugin, uid);
1064 if (size != 0) 1028 if (size != 0)
1065 plugin->env->duc (plugin->env->cls, 1029 plugin->env->duc (plugin->env->cls, -size);
1066 - size); 1030 }
1067 }
1068} 1031}
1069 1032
1070 1033
@@ -1089,11 +1052,11 @@ execute_select (struct Plugin *plugin,
1089 */ 1052 */
1090static void 1053static void
1091mysql_plugin_get_key (void *cls, 1054mysql_plugin_get_key (void *cls,
1092 uint64_t offset, 1055 uint64_t offset,
1093 const GNUNET_HashCode *key, 1056 const GNUNET_HashCode * key,
1094 const GNUNET_HashCode *vhash, 1057 const GNUNET_HashCode * vhash,
1095 enum GNUNET_BLOCK_Type type, 1058 enum GNUNET_BLOCK_Type type,
1096 PluginDatumProcessor proc, void *proc_cls) 1059 PluginDatumProcessor proc, void *proc_cls)
1097{ 1060{
1098 struct Plugin *plugin = cls; 1061 struct Plugin *plugin = cls;
1099 int ret; 1062 int ret;
@@ -1113,115 +1076,107 @@ mysql_plugin_get_key (void *cls,
1113 cbind[0].buffer = &total; 1076 cbind[0].buffer = &total;
1114 cbind[0].is_unsigned = GNUNET_NO; 1077 cbind[0].is_unsigned = GNUNET_NO;
1115 if (type != 0) 1078 if (type != 0)
1079 {
1080 if (vhash != NULL)
1116 { 1081 {
1117 if (vhash != NULL) 1082 ret =
1118 { 1083 prepared_statement_run_select (plugin,
1119 ret = 1084 plugin->
1120 prepared_statement_run_select (plugin, 1085 count_entry_by_hash_vhash_and_type, 1,
1121 plugin->count_entry_by_hash_vhash_and_type, 1086 cbind, MYSQL_TYPE_BLOB, key, hashSize,
1122 1, cbind, 1087 &hashSize, MYSQL_TYPE_BLOB, vhash,
1123 MYSQL_TYPE_BLOB, key, hashSize, &hashSize, 1088 hashSize2, &hashSize2, MYSQL_TYPE_LONG,
1124 MYSQL_TYPE_BLOB, vhash, hashSize2, &hashSize2, 1089 &type, GNUNET_YES, -1);
1125 MYSQL_TYPE_LONG, &type, GNUNET_YES,
1126 -1);
1127 }
1128 else
1129 {
1130 ret =
1131 prepared_statement_run_select (plugin,
1132 plugin->count_entry_by_hash_and_type,
1133 1, cbind,
1134 MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1135 MYSQL_TYPE_LONG, &type, GNUNET_YES,
1136 -1);
1137 }
1138 } 1090 }
1091 else
1092 {
1093 ret =
1094 prepared_statement_run_select (plugin,
1095 plugin->count_entry_by_hash_and_type,
1096 1, cbind,
1097 MYSQL_TYPE_BLOB, key, hashSize,
1098 &hashSize, MYSQL_TYPE_LONG, &type,
1099 GNUNET_YES, -1);
1100 }
1101 }
1139 else 1102 else
1103 {
1104 if (vhash != NULL)
1140 { 1105 {
1141 if (vhash != NULL) 1106 ret =
1142 { 1107 prepared_statement_run_select (plugin,
1143 ret = 1108 plugin->count_entry_by_hash_and_vhash,
1144 prepared_statement_run_select (plugin, 1109 1, cbind,
1145 plugin->count_entry_by_hash_and_vhash, 1110 MYSQL_TYPE_BLOB, key, hashSize,
1146 1, cbind, 1111 &hashSize, MYSQL_TYPE_BLOB, vhash,
1147 MYSQL_TYPE_BLOB, key, hashSize, &hashSize, 1112 hashSize2, &hashSize2, -1);
1148 MYSQL_TYPE_BLOB, vhash, hashSize2, &hashSize2, 1113
1149 -1);
1150
1151 }
1152 else
1153 {
1154 ret =
1155 prepared_statement_run_select (plugin,
1156 plugin->count_entry_by_hash,
1157 1, cbind,
1158 MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1159 -1);
1160 }
1161 } 1114 }
1162 if ((ret != GNUNET_OK) || (0 >= total)) 1115 else
1163 { 1116 {
1164 proc (proc_cls, 1117 ret =
1165 NULL, 0, NULL, 0, 0, 0, 1118 prepared_statement_run_select (plugin,
1166 GNUNET_TIME_UNIT_ZERO_ABS, 0); 1119 plugin->count_entry_by_hash,
1167 return; 1120 1, cbind,
1121 MYSQL_TYPE_BLOB, key, hashSize,
1122 &hashSize, -1);
1168 } 1123 }
1124 }
1125 if ((ret != GNUNET_OK) || (0 >= total))
1126 {
1127 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1128 return;
1129 }
1169 offset = offset % total; 1130 offset = offset % total;
1170 off = (unsigned long long) offset; 1131 off = (unsigned long long) offset;
1171#if DEBUG_MYSQL 1132#if DEBUG_MYSQL
1172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1173 "Obtaining %llu/%lld result for GET `%s'\n", 1134 "Obtaining %llu/%lld result for GET `%s'\n",
1174 off, 1135 off, total, GNUNET_h2s (key));
1175 total,
1176 GNUNET_h2s (key));
1177#endif 1136#endif
1178 1137
1179 if (type != GNUNET_BLOCK_TYPE_ANY) 1138 if (type != GNUNET_BLOCK_TYPE_ANY)
1139 {
1140 if (NULL != vhash)
1180 { 1141 {
1181 if (NULL != vhash) 1142 execute_select (plugin,
1182 { 1143 plugin->select_entry_by_hash_vhash_and_type,
1183 execute_select (plugin, 1144 proc, proc_cls,
1184 plugin->select_entry_by_hash_vhash_and_type, 1145 MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1185 proc, proc_cls, 1146 MYSQL_TYPE_BLOB, vhash, hashSize, &hashSize,
1186 MYSQL_TYPE_BLOB, key, hashSize, &hashSize, 1147 MYSQL_TYPE_LONG, &type, GNUNET_YES,
1187 MYSQL_TYPE_BLOB, vhash, hashSize, &hashSize, 1148 MYSQL_TYPE_LONGLONG, &off, GNUNET_YES, -1);
1188 MYSQL_TYPE_LONG, &type, GNUNET_YES, 1149 }
1189 MYSQL_TYPE_LONGLONG, &off, GNUNET_YES, 1150 else
1190 -1); 1151 {
1191 } 1152 execute_select (plugin,
1192 else 1153 plugin->select_entry_by_hash_and_type,
1193 { 1154 proc, proc_cls,
1194 execute_select (plugin, 1155 MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1195 plugin->select_entry_by_hash_and_type, 1156 MYSQL_TYPE_LONG, &type, GNUNET_YES,
1196 proc, proc_cls, 1157 MYSQL_TYPE_LONGLONG, &off, GNUNET_YES, -1);
1197 MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1198 MYSQL_TYPE_LONG, &type, GNUNET_YES,
1199 MYSQL_TYPE_LONGLONG, &off, GNUNET_YES,
1200 -1);
1201 }
1202 } 1158 }
1159 }
1203 else 1160 else
1161 {
1162 if (NULL != vhash)
1204 { 1163 {
1205 if (NULL != vhash) 1164 execute_select (plugin,
1206 { 1165 plugin->select_entry_by_hash_and_vhash,
1207 execute_select (plugin, 1166 proc, proc_cls,
1208 plugin->select_entry_by_hash_and_vhash, 1167 MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1209 proc, proc_cls, 1168 MYSQL_TYPE_BLOB, vhash, hashSize, &hashSize,
1210 MYSQL_TYPE_BLOB, key, hashSize, &hashSize, 1169 MYSQL_TYPE_LONGLONG, &off, GNUNET_YES, -1);
1211 MYSQL_TYPE_BLOB, vhash, hashSize, &hashSize,
1212 MYSQL_TYPE_LONGLONG, &off, GNUNET_YES,
1213 -1);
1214 }
1215 else
1216 {
1217 execute_select (plugin,
1218 plugin->select_entry_by_hash,
1219 proc, proc_cls,
1220 MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1221 MYSQL_TYPE_LONGLONG, &off, GNUNET_YES,
1222 -1);
1223 }
1224 } 1170 }
1171 else
1172 {
1173 execute_select (plugin,
1174 plugin->select_entry_by_hash,
1175 proc, proc_cls,
1176 MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1177 MYSQL_TYPE_LONGLONG, &off, GNUNET_YES, -1);
1178 }
1179 }
1225} 1180}
1226 1181
1227 1182
@@ -1237,21 +1192,22 @@ mysql_plugin_get_key (void *cls,
1237 */ 1192 */
1238static void 1193static void
1239mysql_plugin_get_zero_anonymity (void *cls, 1194mysql_plugin_get_zero_anonymity (void *cls,
1240 uint64_t offset, 1195 uint64_t offset,
1241 enum GNUNET_BLOCK_Type type, 1196 enum GNUNET_BLOCK_Type type,
1242 PluginDatumProcessor proc, void *proc_cls) 1197 PluginDatumProcessor proc, void *proc_cls)
1243{ 1198{
1244 struct Plugin *plugin = cls; 1199 struct Plugin *plugin = cls;
1245 unsigned long long rvalue = (unsigned long long) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, 1200 unsigned long long rvalue =
1246 UINT64_MAX); 1201 (unsigned long long) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1202 UINT64_MAX);
1203
1247 execute_select (plugin, 1204 execute_select (plugin,
1248 plugin->zero_iter, 1205 plugin->zero_iter,
1249 proc, proc_cls, 1206 proc, proc_cls,
1250 MYSQL_TYPE_LONG, &type, GNUNET_YES, 1207 MYSQL_TYPE_LONG, &type, GNUNET_YES,
1251 MYSQL_TYPE_LONGLONG, &rvalue, GNUNET_YES, 1208 MYSQL_TYPE_LONGLONG, &rvalue, GNUNET_YES,
1252 MYSQL_TYPE_LONG, &type, GNUNET_YES, 1209 MYSQL_TYPE_LONG, &type, GNUNET_YES,
1253 MYSQL_TYPE_LONGLONG, &rvalue, GNUNET_YES, 1210 MYSQL_TYPE_LONGLONG, &rvalue, GNUNET_YES, -1);
1254 -1);
1255} 1211}
1256 1212
1257 1213
@@ -1260,17 +1216,17 @@ mysql_plugin_get_zero_anonymity (void *cls,
1260 */ 1216 */
1261struct ReplCtx 1217struct ReplCtx
1262{ 1218{
1263 1219
1264 /** 1220 /**
1265 * Plugin handle. 1221 * Plugin handle.
1266 */ 1222 */
1267 struct Plugin *plugin; 1223 struct Plugin *plugin;
1268 1224
1269 /** 1225 /**
1270 * Function to call for the result (or the NULL). 1226 * Function to call for the result (or the NULL).
1271 */ 1227 */
1272 PluginDatumProcessor proc; 1228 PluginDatumProcessor proc;
1273 1229
1274 /** 1230 /**
1275 * Closure for proc. 1231 * Closure for proc.
1276 */ 1232 */
@@ -1300,14 +1256,13 @@ struct ReplCtx
1300 */ 1256 */
1301static int 1257static int
1302repl_proc (void *cls, 1258repl_proc (void *cls,
1303 const GNUNET_HashCode *key, 1259 const GNUNET_HashCode * key,
1304 uint32_t size, 1260 uint32_t size,
1305 const void *data, 1261 const void *data,
1306 enum GNUNET_BLOCK_Type type, 1262 enum GNUNET_BLOCK_Type type,
1307 uint32_t priority, 1263 uint32_t priority,
1308 uint32_t anonymity, 1264 uint32_t anonymity,
1309 struct GNUNET_TIME_Absolute expiration, 1265 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
1310 uint64_t uid)
1311{ 1266{
1312 struct ReplCtx *rc = cls; 1267 struct ReplCtx *rc = cls;
1313 struct Plugin *plugin = rc->plugin; 1268 struct Plugin *plugin = rc->plugin;
@@ -1316,25 +1271,21 @@ repl_proc (void *cls,
1316 int iret; 1271 int iret;
1317 1272
1318 ret = rc->proc (rc->proc_cls, 1273 ret = rc->proc (rc->proc_cls,
1319 key, 1274 key, size, data, type, priority, anonymity, expiration, uid);
1320 size, data,
1321 type, priority, anonymity, expiration,
1322 uid);
1323 if (NULL != key) 1275 if (NULL != key)
1276 {
1277 oid = (unsigned long long) uid;
1278 iret = prepared_statement_run (plugin,
1279 plugin->dec_repl,
1280 NULL,
1281 MYSQL_TYPE_LONGLONG, &oid, GNUNET_YES, -1);
1282 if (iret == GNUNET_SYSERR)
1324 { 1283 {
1325 oid = (unsigned long long) uid; 1284 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1326 iret = prepared_statement_run (plugin, 1285 "Failed to reduce replication counter\n");
1327 plugin->dec_repl, 1286 return GNUNET_SYSERR;
1328 NULL,
1329 MYSQL_TYPE_LONGLONG, &oid, GNUNET_YES,
1330 -1);
1331 if (iret == GNUNET_SYSERR)
1332 {
1333 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1334 "Failed to reduce replication counter\n");
1335 return GNUNET_SYSERR;
1336 }
1337 } 1287 }
1288 }
1338 return ret; 1289 return ret;
1339} 1290}
1340 1291
@@ -1352,14 +1303,14 @@ repl_proc (void *cls,
1352 */ 1303 */
1353static void 1304static void
1354mysql_plugin_get_replication (void *cls, 1305mysql_plugin_get_replication (void *cls,
1355 PluginDatumProcessor proc, void *proc_cls) 1306 PluginDatumProcessor proc, void *proc_cls)
1356{ 1307{
1357 struct Plugin *plugin = cls; 1308 struct Plugin *plugin = cls;
1358 struct ReplCtx rc; 1309 struct ReplCtx rc;
1359 unsigned long long rvalue; 1310 unsigned long long rvalue;
1360 unsigned long repl; 1311 unsigned long repl;
1361 MYSQL_BIND results; 1312 MYSQL_BIND results;
1362 1313
1363 rc.plugin = plugin; 1314 rc.plugin = plugin;
1364 rc.proc = proc; 1315 rc.proc = proc;
1365 rc.proc_cls = proc_cls; 1316 rc.proc_cls = proc_cls;
@@ -1369,28 +1320,19 @@ mysql_plugin_get_replication (void *cls,
1369 results.is_unsigned = GNUNET_YES; 1320 results.is_unsigned = GNUNET_YES;
1370 1321
1371 if (1 != 1322 if (1 !=
1372 prepared_statement_run_select (plugin, 1323 prepared_statement_run_select (plugin, plugin->max_repl, 1, &results, -1))
1373 plugin->max_repl, 1324 {
1374 1, 1325 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1375 &results, 1326 return;
1376 -1)) 1327 }
1377 { 1328
1378 proc (proc_cls, 1329 rvalue =
1379 NULL, 0, NULL, 0, 0, 0, 1330 (unsigned long long) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1380 GNUNET_TIME_UNIT_ZERO_ABS, 0); 1331 UINT64_MAX);
1381 return; 1332 execute_select (plugin, plugin->select_replication, &repl_proc, &rc,
1382 } 1333 MYSQL_TYPE_LONG, &repl, GNUNET_YES, MYSQL_TYPE_LONGLONG,
1383 1334 &rvalue, GNUNET_YES, MYSQL_TYPE_LONG, &repl, GNUNET_YES,
1384 rvalue = (unsigned long long) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, 1335 MYSQL_TYPE_LONGLONG, &rvalue, GNUNET_YES, -1);
1385 UINT64_MAX);
1386 execute_select (plugin,
1387 plugin->select_replication,
1388 &repl_proc, &rc,
1389 MYSQL_TYPE_LONG, &repl, GNUNET_YES,
1390 MYSQL_TYPE_LONGLONG, &rvalue, GNUNET_YES,
1391 MYSQL_TYPE_LONG, &repl, GNUNET_YES,
1392 MYSQL_TYPE_LONGLONG, &rvalue, GNUNET_YES,
1393 -1);
1394 1336
1395} 1337}
1396 1338
@@ -1400,17 +1342,17 @@ mysql_plugin_get_replication (void *cls,
1400 */ 1342 */
1401struct ExpiCtx 1343struct ExpiCtx
1402{ 1344{
1403 1345
1404 /** 1346 /**
1405 * Plugin handle. 1347 * Plugin handle.
1406 */ 1348 */
1407 struct Plugin *plugin; 1349 struct Plugin *plugin;
1408 1350
1409 /** 1351 /**
1410 * Function to call for the result (or the NULL). 1352 * Function to call for the result (or the NULL).
1411 */ 1353 */
1412 PluginDatumProcessor proc; 1354 PluginDatumProcessor proc;
1413 1355
1414 /** 1356 /**
1415 * Closure for proc. 1357 * Closure for proc.
1416 */ 1358 */
@@ -1441,31 +1383,25 @@ struct ExpiCtx
1441 */ 1383 */
1442static int 1384static int
1443expi_proc (void *cls, 1385expi_proc (void *cls,
1444 const GNUNET_HashCode *key, 1386 const GNUNET_HashCode * key,
1445 uint32_t size, 1387 uint32_t size,
1446 const void *data, 1388 const void *data,
1447 enum GNUNET_BLOCK_Type type, 1389 enum GNUNET_BLOCK_Type type,
1448 uint32_t priority, 1390 uint32_t priority,
1449 uint32_t anonymity, 1391 uint32_t anonymity,
1450 struct GNUNET_TIME_Absolute expiration, 1392 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
1451 uint64_t uid)
1452{ 1393{
1453 struct ExpiCtx *rc = cls; 1394 struct ExpiCtx *rc = cls;
1454 struct Plugin *plugin = rc->plugin; 1395 struct Plugin *plugin = rc->plugin;
1455 1396
1456 if (NULL == key) 1397 if (NULL == key)
1457 { 1398 {
1458 execute_select (plugin, 1399 execute_select (plugin,
1459 plugin->select_priority, 1400 plugin->select_priority, rc->proc, rc->proc_cls, -1);
1460 rc->proc, rc->proc_cls, 1401 return GNUNET_SYSERR;
1461 -1); 1402 }
1462 return GNUNET_SYSERR;
1463 }
1464 return rc->proc (rc->proc_cls, 1403 return rc->proc (rc->proc_cls,
1465 key, 1404 key, size, data, type, priority, anonymity, expiration, uid);
1466 size, data,
1467 type, priority, anonymity, expiration,
1468 uid);
1469} 1405}
1470 1406
1471 1407
@@ -1479,21 +1415,19 @@ expi_proc (void *cls,
1479 */ 1415 */
1480static void 1416static void
1481mysql_plugin_get_expiration (void *cls, 1417mysql_plugin_get_expiration (void *cls,
1482 PluginDatumProcessor proc, void *proc_cls) 1418 PluginDatumProcessor proc, void *proc_cls)
1483{ 1419{
1484 struct Plugin *plugin = cls; 1420 struct Plugin *plugin = cls;
1485 long long nt; 1421 long long nt;
1486 struct ExpiCtx rc; 1422 struct ExpiCtx rc;
1487 1423
1488 rc.plugin = plugin; 1424 rc.plugin = plugin;
1489 rc.proc = proc; 1425 rc.proc = proc;
1490 rc.proc_cls = proc_cls; 1426 rc.proc_cls = proc_cls;
1491 nt = (long long) GNUNET_TIME_absolute_get().abs_value; 1427 nt = (long long) GNUNET_TIME_absolute_get ().abs_value;
1492 execute_select (plugin, 1428 execute_select (plugin,
1493 plugin->select_expiration, 1429 plugin->select_expiration,
1494 expi_proc, &rc, 1430 expi_proc, &rc, MYSQL_TYPE_LONGLONG, &nt, GNUNET_YES, -1);
1495 MYSQL_TYPE_LONGLONG, &nt, GNUNET_YES,
1496 -1);
1497 1431
1498} 1432}
1499 1433
@@ -1503,14 +1437,13 @@ mysql_plugin_get_expiration (void *cls,
1503 * 1437 *
1504 * @param cls the "struct Plugin*" 1438 * @param cls the "struct Plugin*"
1505 */ 1439 */
1506static void 1440static void
1507mysql_plugin_drop (void *cls) 1441mysql_plugin_drop (void *cls)
1508{ 1442{
1509 struct Plugin *plugin = cls; 1443 struct Plugin *plugin = cls;
1510 1444
1511 if (GNUNET_OK != run_statement (plugin, 1445 if (GNUNET_OK != run_statement (plugin, "DROP TABLE gn090"))
1512 "DROP TABLE gn090")) 1446 return; /* error */
1513 return; /* error */
1514 plugin->env->duc (plugin->env->cls, 0); 1447 plugin->env->duc (plugin->env->cls, 0);
1515} 1448}
1516 1449
@@ -1532,12 +1465,12 @@ libgnunet_plugin_datastore_mysql_init (void *cls)
1532 plugin->env = env; 1465 plugin->env = env;
1533 plugin->cnffile = get_my_cnf_path (env->cfg); 1466 plugin->cnffile = get_my_cnf_path (env->cfg);
1534 if (GNUNET_OK != iopen (plugin)) 1467 if (GNUNET_OK != iopen (plugin))
1535 { 1468 {
1536 iclose (plugin); 1469 iclose (plugin);
1537 GNUNET_free_non_null (plugin->cnffile); 1470 GNUNET_free_non_null (plugin->cnffile);
1538 GNUNET_free (plugin); 1471 GNUNET_free (plugin);
1539 return NULL; 1472 return NULL;
1540 } 1473 }
1541#define MRUNS(a) (GNUNET_OK != run_statement (plugin, a) ) 1474#define MRUNS(a) (GNUNET_OK != run_statement (plugin, a) )
1542#define PINIT(a,b) (NULL == (a = prepared_statement_create(plugin, b))) 1475#define PINIT(a,b) (NULL == (a = prepared_statement_create(plugin, b)))
1543 if (MRUNS ("CREATE TABLE IF NOT EXISTS gn090 (" 1476 if (MRUNS ("CREATE TABLE IF NOT EXISTS gn090 ("
@@ -1565,29 +1498,32 @@ libgnunet_plugin_datastore_mysql_init (void *cls)
1565 PINIT (plugin->insert_entry, INSERT_ENTRY) || 1498 PINIT (plugin->insert_entry, INSERT_ENTRY) ||
1566 PINIT (plugin->delete_entry_by_uid, DELETE_ENTRY_BY_UID) || 1499 PINIT (plugin->delete_entry_by_uid, DELETE_ENTRY_BY_UID) ||
1567 PINIT (plugin->select_entry_by_hash, SELECT_ENTRY_BY_HASH) || 1500 PINIT (plugin->select_entry_by_hash, SELECT_ENTRY_BY_HASH) ||
1568 PINIT (plugin->select_entry_by_hash_and_vhash, SELECT_ENTRY_BY_HASH_AND_VHASH) 1501 PINIT (plugin->select_entry_by_hash_and_vhash,
1569 || PINIT (plugin->select_entry_by_hash_and_type, SELECT_ENTRY_BY_HASH_AND_TYPE) 1502 SELECT_ENTRY_BY_HASH_AND_VHASH) ||
1570 || PINIT (plugin->select_entry_by_hash_vhash_and_type, 1503 PINIT (plugin->select_entry_by_hash_and_type,
1571 SELECT_ENTRY_BY_HASH_VHASH_AND_TYPE) 1504 SELECT_ENTRY_BY_HASH_AND_TYPE) ||
1572 || PINIT (plugin->count_entry_by_hash, COUNT_ENTRY_BY_HASH) 1505 PINIT (plugin->select_entry_by_hash_vhash_and_type,
1573 || PINIT (plugin->get_size, SELECT_SIZE) 1506 SELECT_ENTRY_BY_HASH_VHASH_AND_TYPE) ||
1574 || PINIT (plugin->count_entry_by_hash_and_vhash, COUNT_ENTRY_BY_HASH_AND_VHASH) 1507 PINIT (plugin->count_entry_by_hash, COUNT_ENTRY_BY_HASH) ||
1575 || PINIT (plugin->count_entry_by_hash_and_type, COUNT_ENTRY_BY_HASH_AND_TYPE) 1508 PINIT (plugin->get_size, SELECT_SIZE) ||
1509 PINIT (plugin->count_entry_by_hash_and_vhash,
1510 COUNT_ENTRY_BY_HASH_AND_VHASH) ||
1511 PINIT (plugin->count_entry_by_hash_and_type, COUNT_ENTRY_BY_HASH_AND_TYPE)
1576 || PINIT (plugin->count_entry_by_hash_vhash_and_type, 1512 || PINIT (plugin->count_entry_by_hash_vhash_and_type,
1577 COUNT_ENTRY_BY_HASH_VHASH_AND_TYPE) 1513 COUNT_ENTRY_BY_HASH_VHASH_AND_TYPE) ||
1578 || PINIT (plugin->update_entry, UPDATE_ENTRY) 1514 PINIT (plugin->update_entry, UPDATE_ENTRY) ||
1579 || PINIT (plugin->dec_repl, DEC_REPL) 1515 PINIT (plugin->dec_repl, DEC_REPL) ||
1580 || PINIT (plugin->zero_iter, SELECT_IT_NON_ANONYMOUS) 1516 PINIT (plugin->zero_iter, SELECT_IT_NON_ANONYMOUS) ||
1581 || PINIT (plugin->select_expiration, SELECT_IT_EXPIRATION) 1517 PINIT (plugin->select_expiration, SELECT_IT_EXPIRATION) ||
1582 || PINIT (plugin->select_priority, SELECT_IT_PRIORITY) 1518 PINIT (plugin->select_priority, SELECT_IT_PRIORITY) ||
1583 || PINIT (plugin->max_repl, SELECT_MAX_REPL) 1519 PINIT (plugin->max_repl, SELECT_MAX_REPL) ||
1584 || PINIT (plugin->select_replication, SELECT_IT_REPLICATION) ) 1520 PINIT (plugin->select_replication, SELECT_IT_REPLICATION))
1585 { 1521 {
1586 iclose (plugin); 1522 iclose (plugin);
1587 GNUNET_free_non_null (plugin->cnffile); 1523 GNUNET_free_non_null (plugin->cnffile);
1588 GNUNET_free (plugin); 1524 GNUNET_free (plugin);
1589 return NULL; 1525 return NULL;
1590 } 1526 }
1591#undef PINIT 1527#undef PINIT
1592#undef MRUNS 1528#undef MRUNS
1593 1529
@@ -1621,13 +1557,11 @@ libgnunet_plugin_datastore_mysql_done (void *cls)
1621 1557
1622 iclose (plugin); 1558 iclose (plugin);
1623 while (NULL != (s = plugin->shead)) 1559 while (NULL != (s = plugin->shead))
1624 { 1560 {
1625 GNUNET_CONTAINER_DLL_remove (plugin->shead, 1561 GNUNET_CONTAINER_DLL_remove (plugin->shead, plugin->stail, s);
1626 plugin->stail, 1562 GNUNET_free (s->query);
1627 s); 1563 GNUNET_free (s);
1628 GNUNET_free (s->query); 1564 }
1629 GNUNET_free (s);
1630 }
1631 GNUNET_free_non_null (plugin->cnffile); 1565 GNUNET_free_non_null (plugin->cnffile);
1632 GNUNET_free (plugin); 1566 GNUNET_free (plugin);
1633 GNUNET_free (api); 1567 GNUNET_free (api);
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 */
49struct Plugin 49struct Plugin
50{ 50{
51 /** 51 /**
52 * Our execution environment. 52 * Our execution environment.
@@ -76,27 +76,28 @@ struct Plugin
76 */ 76 */
77static int 77static int
78check_result (struct Plugin *plugin, 78check_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 */
110static int 111static int
111pq_exec (struct Plugin *plugin, 112pq_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 */
134static int 134static int
135pq_prepare (struct Plugin *plugin, 135pq_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 */
379static int 371static int
380delete_by_rowid (struct Plugin *plugin, 372delete_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 */
460static int 449static int
461postgres_plugin_put (void *cls, 450postgres_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 */
531static void 519static void
532process_result (struct Plugin *plugin, 520process_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 */
678static void 648static void
679postgres_plugin_get_key (void *cls, 649postgres_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 */
827static void 768static void
828postgres_plugin_get_zero_anonymity (void *cls, 769postgres_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 */
858struct ReplCtx 794struct 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 */
899static int 835static int
900repl_proc (void *cls, 836repl_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 */
956static void 887static void
957postgres_plugin_get_replication (void *cls, 888postgres_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 */
985static void 912static void
986postgres_plugin_get_expiration (void *cls, 913postgres_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 */
1033static int 954static int
1034postgres_plugin_update (void *cls, 955postgres_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 */
1072static void 994static void
1073postgres_plugin_drop (void *cls) 995postgres_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);
diff --git a/src/datastore/plugin_datastore_sqlite.c b/src/datastore/plugin_datastore_sqlite.c
index f551f048d..075d31623 100644
--- a/src/datastore/plugin_datastore_sqlite.c
+++ b/src/datastore/plugin_datastore_sqlite.c
@@ -1,22 +1,22 @@
1 /* 1 /*
2 This file is part of GNUnet 2 * This file is part of GNUnet
3 (C) 2009, 2011 Christian Grothoff (and other contributing authors) 3 * (C) 2009, 2011 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
7 by the Free Software Foundation; either version 3, or (at your 7 * by the Free Software Foundation; either version 3, or (at your
8 option) any later version. 8 * option) any later version.
9 9 *
10 GNUnet is distributed in the hope that it will be useful, but 10 * GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details. 13 * General Public License for more details.
14 14 *
15 You should have received a copy of the GNU General Public License 15 * You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the 16 * along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 * Boston, MA 02111-1307, USA.
19*/ 19 */
20 20
21/** 21/**
22 * @file datastore/plugin_datastore_sqlite.c 22 * @file datastore/plugin_datastore_sqlite.c
@@ -65,7 +65,7 @@
65/** 65/**
66 * Context for all functions in this plugin. 66 * Context for all functions in this plugin.
67 */ 67 */
68struct Plugin 68struct Plugin
69{ 69{
70 /** 70 /**
71 * Our execution environment. 71 * Our execution environment.
@@ -139,25 +139,17 @@ struct Plugin
139 * @return 0 on success 139 * @return 0 on success
140 */ 140 */
141static int 141static int
142sq_prepare (sqlite3 * dbh, 142sq_prepare (sqlite3 * dbh, const char *zSql, sqlite3_stmt ** ppStmt)
143 const char *zSql,
144 sqlite3_stmt ** ppStmt)
145{ 143{
146 char *dummy; 144 char *dummy;
147 int result; 145 int result;
148 146
149 result = sqlite3_prepare_v2 (dbh, 147 result = sqlite3_prepare_v2 (dbh,
150 zSql, 148 zSql,
151 strlen (zSql), 149 strlen (zSql), ppStmt, (const char **) &dummy);
152 ppStmt,
153 (const char **) &dummy);
154#if DEBUG_SQLITE && 0 150#if DEBUG_SQLITE && 0
155 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 151 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
156 "sqlite", 152 "sqlite", "Prepared `%s' / %p: %d\n", zSql, *ppStmt, result);
157 "Prepared `%s' / %p: %d\n",
158 zSql,
159 *ppStmt,
160 result);
161#endif 153#endif
162 return result; 154 return result;
163} 155}
@@ -172,35 +164,40 @@ static void
172create_indices (sqlite3 * dbh) 164create_indices (sqlite3 * dbh)
173{ 165{
174 /* create indices */ 166 /* create indices */
175 if ( (SQLITE_OK != 167 if ((SQLITE_OK !=
176 sqlite3_exec (dbh, 168 sqlite3_exec (dbh,
177 "CREATE INDEX IF NOT EXISTS idx_hash ON gn090 (hash)", NULL, NULL, NULL)) || 169 "CREATE INDEX IF NOT EXISTS idx_hash ON gn090 (hash)",
178 (SQLITE_OK != 170 NULL, NULL, NULL)) ||
179 sqlite3_exec (dbh, 171 (SQLITE_OK !=
180 "CREATE INDEX IF NOT EXISTS idx_hash_vhash ON gn090 (hash,vhash)", NULL, 172 sqlite3_exec (dbh,
181 NULL, NULL)) || 173 "CREATE INDEX IF NOT EXISTS idx_hash_vhash ON gn090 (hash,vhash)",
182 (SQLITE_OK != 174 NULL, NULL, NULL)) ||
183 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS idx_expire_repl ON gn090 (expire ASC,repl DESC)", NULL, NULL, 175 (SQLITE_OK !=
184 NULL)) || 176 sqlite3_exec (dbh,
185 (SQLITE_OK != 177 "CREATE INDEX IF NOT EXISTS idx_expire_repl ON gn090 (expire ASC,repl DESC)",
186 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS idx_comb ON gn090 (anonLevel ASC,expire ASC,prio,type,hash)", 178 NULL, NULL, NULL)) ||
187 NULL, NULL, NULL)) || 179 (SQLITE_OK !=
188 (SQLITE_OK != 180 sqlite3_exec (dbh,
189 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS idx_anon_type_hash ON gn090 (anonLevel ASC,type,hash)", 181 "CREATE INDEX IF NOT EXISTS idx_comb ON gn090 (anonLevel ASC,expire ASC,prio,type,hash)",
190 NULL, NULL, NULL)) || 182 NULL, NULL, NULL)) ||
191 (SQLITE_OK != 183 (SQLITE_OK !=
192 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS idx_expire ON gn090 (expire ASC)", 184 sqlite3_exec (dbh,
193 NULL, NULL, NULL)) || 185 "CREATE INDEX IF NOT EXISTS idx_anon_type_hash ON gn090 (anonLevel ASC,type,hash)",
194 (SQLITE_OK != 186 NULL, NULL, NULL)) ||
195 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS idx_repl_rvalue ON gn090 (repl,rvalue)", 187 (SQLITE_OK !=
196 NULL, NULL, NULL)) || 188 sqlite3_exec (dbh,
197 (SQLITE_OK != 189 "CREATE INDEX IF NOT EXISTS idx_expire ON gn090 (expire ASC)",
198 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS idx_repl ON gn090 (repl DESC)", 190 NULL, NULL, NULL)) ||
199 NULL, NULL, NULL)) ) 191 (SQLITE_OK !=
200 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 192 sqlite3_exec (dbh,
201 "sqlite", 193 "CREATE INDEX IF NOT EXISTS idx_repl_rvalue ON gn090 (repl,rvalue)",
202 "Failed to create indices: %s\n", 194 NULL, NULL, NULL)) ||
203 sqlite3_errmsg (dbh)); 195 (SQLITE_OK !=
196 sqlite3_exec (dbh,
197 "CREATE INDEX IF NOT EXISTS idx_repl ON gn090 (repl DESC)",
198 NULL, NULL, NULL)))
199 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "sqlite",
200 "Failed to create indices: %s\n", sqlite3_errmsg (dbh));
204} 201}
205 202
206 203
@@ -225,56 +222,55 @@ create_indices (sqlite3 * dbh)
225 */ 222 */
226static int 223static int
227database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg, 224database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
228 struct Plugin *plugin) 225 struct Plugin *plugin)
229{ 226{
230 sqlite3_stmt *stmt; 227 sqlite3_stmt *stmt;
231 char *afsdir; 228 char *afsdir;
229
232#if ENULL_DEFINED 230#if ENULL_DEFINED
233 char *e; 231 char *e;
234#endif 232#endif
235 233
236 if (GNUNET_OK != 234 if (GNUNET_OK !=
237 GNUNET_CONFIGURATION_get_value_filename (cfg, 235 GNUNET_CONFIGURATION_get_value_filename (cfg,
238 "datastore-sqlite", 236 "datastore-sqlite",
239 "FILENAME", 237 "FILENAME", &afsdir))
240 &afsdir)) 238 {
241 { 239 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
242 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 240 "sqlite",
243 "sqlite", 241 _
244 _("Option `%s' in section `%s' missing in configuration!\n"), 242 ("Option `%s' in section `%s' missing in configuration!\n"),
245 "FILENAME", 243 "FILENAME", "datastore-sqlite");
246 "datastore-sqlite"); 244 return GNUNET_SYSERR;
247 return GNUNET_SYSERR; 245 }
248 }
249 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir)) 246 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
247 {
248 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
250 { 249 {
251 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir)) 250 GNUNET_break (0);
252 { 251 GNUNET_free (afsdir);
253 GNUNET_break (0); 252 return GNUNET_SYSERR;
254 GNUNET_free (afsdir);
255 return GNUNET_SYSERR;
256 }
257 /* database is new or got deleted, reset payload to zero! */
258 plugin->env->duc (plugin->env->cls, 0);
259 } 253 }
254 /* database is new or got deleted, reset payload to zero! */
255 plugin->env->duc (plugin->env->cls, 0);
256 }
260#ifdef ENABLE_NLS 257#ifdef ENABLE_NLS
261 plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir), 258 plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir),
262 nl_langinfo (CODESET)); 259 nl_langinfo (CODESET));
263#else 260#else
264 plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir), 261 plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir), "UTF-8"); /* good luck */
265 "UTF-8"); /* good luck */
266#endif 262#endif
267 GNUNET_free (afsdir); 263 GNUNET_free (afsdir);
268 264
269 /* Open database and precompile statements */ 265 /* Open database and precompile statements */
270 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK) 266 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
271 { 267 {
272 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 268 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
273 "sqlite", 269 "sqlite",
274 _("Unable to initialize SQLite: %s.\n"), 270 _("Unable to initialize SQLite: %s.\n"),
275 sqlite3_errmsg (plugin->dbh)); 271 sqlite3_errmsg (plugin->dbh));
276 return GNUNET_SYSERR; 272 return GNUNET_SYSERR;
277 } 273 }
278 CHECK (SQLITE_OK == 274 CHECK (SQLITE_OK ==
279 sqlite3_exec (plugin->dbh, 275 sqlite3_exec (plugin->dbh,
280 "PRAGMA temp_store=MEMORY", NULL, NULL, ENULL)); 276 "PRAGMA temp_store=MEMORY", NULL, NULL, ENULL));
@@ -294,8 +290,8 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
294 sqlite3_exec (plugin->dbh, 290 sqlite3_exec (plugin->dbh,
295 "PRAGMA count_changes=OFF", NULL, NULL, ENULL)); 291 "PRAGMA count_changes=OFF", NULL, NULL, ENULL));
296 CHECK (SQLITE_OK == 292 CHECK (SQLITE_OK ==
297 sqlite3_exec (plugin->dbh, 293 sqlite3_exec (plugin->dbh,
298 "PRAGMA page_size=4092", NULL, NULL, ENULL)); 294 "PRAGMA page_size=4092", NULL, NULL, ENULL));
299 295
300 CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS)); 296 CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS));
301 297
@@ -305,62 +301,60 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
305 sq_prepare (plugin->dbh, 301 sq_prepare (plugin->dbh,
306 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn090'", 302 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn090'",
307 &stmt)); 303 &stmt));
308 if ( (sqlite3_step (stmt) == SQLITE_DONE) && 304 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
309 (sqlite3_exec (plugin->dbh, 305 (sqlite3_exec (plugin->dbh,
310 "CREATE TABLE gn090 (" 306 "CREATE TABLE gn090 ("
311 " repl INT4 NOT NULL DEFAULT 0," 307 " repl INT4 NOT NULL DEFAULT 0,"
312 " type INT4 NOT NULL DEFAULT 0," 308 " type INT4 NOT NULL DEFAULT 0,"
313 " prio INT4 NOT NULL DEFAULT 0," 309 " prio INT4 NOT NULL DEFAULT 0,"
314 " anonLevel INT4 NOT NULL DEFAULT 0," 310 " anonLevel INT4 NOT NULL DEFAULT 0,"
315 " expire INT8 NOT NULL DEFAULT 0," 311 " expire INT8 NOT NULL DEFAULT 0,"
316 " rvalue INT8 NOT NULL," 312 " rvalue INT8 NOT NULL,"
317 " hash TEXT NOT NULL DEFAULT ''," 313 " hash TEXT NOT NULL DEFAULT '',"
318 " vhash TEXT NOT NULL DEFAULT ''," 314 " vhash TEXT NOT NULL DEFAULT '',"
319 " value BLOB NOT NULL DEFAULT '')", NULL, NULL, 315 " value BLOB NOT NULL DEFAULT '')", NULL, NULL,
320 NULL) != SQLITE_OK) ) 316 NULL) != SQLITE_OK))
321 { 317 {
322 LOG_SQLITE (plugin, NULL, 318 LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
323 GNUNET_ERROR_TYPE_ERROR, 319 sqlite3_finalize (stmt);
324 "sqlite3_exec"); 320 return GNUNET_SYSERR;
325 sqlite3_finalize (stmt); 321 }
326 return GNUNET_SYSERR;
327 }
328 sqlite3_finalize (stmt); 322 sqlite3_finalize (stmt);
329 create_indices (plugin->dbh); 323 create_indices (plugin->dbh);
330 324
331 if ((sq_prepare (plugin->dbh, 325 if ((sq_prepare (plugin->dbh,
332 "UPDATE gn090 " 326 "UPDATE gn090 "
333 "SET prio = prio + ?, expire = MAX(expire,?) WHERE _ROWID_ = ?", 327 "SET prio = prio + ?, expire = MAX(expire,?) WHERE _ROWID_ = ?",
334 &plugin->updPrio) != SQLITE_OK) || 328 &plugin->updPrio) != SQLITE_OK) ||
335 (sq_prepare (plugin->dbh, 329 (sq_prepare (plugin->dbh,
336 "UPDATE gn090 " 330 "UPDATE gn090 "
337 "SET repl = MAX (0, repl - 1) WHERE _ROWID_ = ?", 331 "SET repl = MAX (0, repl - 1) WHERE _ROWID_ = ?",
338 &plugin->updRepl) != SQLITE_OK) || 332 &plugin->updRepl) != SQLITE_OK) ||
339 (sq_prepare (plugin->dbh, 333 (sq_prepare (plugin->dbh,
340 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ " 334 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ "
341 "FROM gn090 INDEXED BY idx_repl_rvalue " 335 "FROM gn090 INDEXED BY idx_repl_rvalue "
342 "WHERE repl=?2 AND" 336 "WHERE repl=?2 AND"
343 " (rvalue>=?1 OR" 337 " (rvalue>=?1 OR"
344 " NOT EXISTS (SELECT 1 FROM gn090 INDEXED BY idx_repl_rvalue WHERE repl=?2 AND rvalue>=?1 LIMIT 1))" 338 " NOT EXISTS (SELECT 1 FROM gn090 INDEXED BY idx_repl_rvalue WHERE repl=?2 AND rvalue>=?1 LIMIT 1))"
345 " ORDER BY rvalue ASC" 339 " ORDER BY rvalue ASC"
346 " LIMIT 1", 340 " LIMIT 1",
347 &plugin->selRepl) != SQLITE_OK) || 341 &plugin->selRepl) != SQLITE_OK) ||
348 (sq_prepare (plugin->dbh, 342 (sq_prepare (plugin->dbh,
349 "SELECT MAX(repl) " 343 "SELECT MAX(repl) "
350 "FROM gn090 INDEXED BY idx_repl_rvalue", 344 "FROM gn090 INDEXED BY idx_repl_rvalue",
351 &plugin->maxRepl) != SQLITE_OK) || 345 &plugin->maxRepl) != SQLITE_OK) ||
352 (sq_prepare (plugin->dbh, 346 (sq_prepare (plugin->dbh,
353 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ " 347 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ "
354 "FROM gn090 INDEXED BY idx_expire" 348 "FROM gn090 INDEXED BY idx_expire"
355 " WHERE NOT EXISTS (SELECT 1 FROM gn090 WHERE expire < ?1 LIMIT 1) OR (expire < ?1) " 349 " WHERE NOT EXISTS (SELECT 1 FROM gn090 WHERE expire < ?1 LIMIT 1) OR (expire < ?1) "
356 " ORDER BY expire ASC LIMIT 1", 350 " ORDER BY expire ASC LIMIT 1",
357 &plugin->selExpi) != SQLITE_OK) || 351 &plugin->selExpi) != SQLITE_OK) ||
358 (sq_prepare (plugin->dbh, 352 (sq_prepare (plugin->dbh,
359 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ " 353 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ "
360 "FROM gn090 INDEXED BY idx_anon_type_hash " 354 "FROM gn090 INDEXED BY idx_anon_type_hash "
361 "WHERE (anonLevel = 0 AND type=?1) " 355 "WHERE (anonLevel = 0 AND type=?1) "
362 "ORDER BY hash DESC LIMIT 1 OFFSET ?2", 356 "ORDER BY hash DESC LIMIT 1 OFFSET ?2",
363 &plugin->selZeroAnon) != SQLITE_OK) || 357 &plugin->selZeroAnon) != SQLITE_OK) ||
364 (sq_prepare (plugin->dbh, 358 (sq_prepare (plugin->dbh,
365 "INSERT INTO gn090 (repl, type, prio, " 359 "INSERT INTO gn090 (repl, type, prio, "
366 "anonLevel, expire, rvalue, hash, vhash, value) " 360 "anonLevel, expire, rvalue, hash, vhash, value) "
@@ -369,11 +363,10 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
369 (sq_prepare (plugin->dbh, 363 (sq_prepare (plugin->dbh,
370 "DELETE FROM gn090 WHERE _ROWID_ = ?", 364 "DELETE FROM gn090 WHERE _ROWID_ = ?",
371 &plugin->delRow) != SQLITE_OK)) 365 &plugin->delRow) != SQLITE_OK))
372 { 366 {
373 LOG_SQLITE (plugin, NULL, 367 LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR, "precompiling");
374 GNUNET_ERROR_TYPE_ERROR, "precompiling"); 368 return GNUNET_SYSERR;
375 return GNUNET_SYSERR; 369 }
376 }
377 370
378 return GNUNET_OK; 371 return GNUNET_OK;
379} 372}
@@ -388,6 +381,7 @@ static void
388database_shutdown (struct Plugin *plugin) 381database_shutdown (struct Plugin *plugin)
389{ 382{
390 int result; 383 int result;
384
391#if SQLITE_VERSION_NUMBER >= 3007000 385#if SQLITE_VERSION_NUMBER >= 3007000
392 sqlite3_stmt *stmt; 386 sqlite3_stmt *stmt;
393#endif 387#endif
@@ -408,36 +402,35 @@ database_shutdown (struct Plugin *plugin)
408 sqlite3_finalize (plugin->selZeroAnon); 402 sqlite3_finalize (plugin->selZeroAnon);
409 if (plugin->insertContent != NULL) 403 if (plugin->insertContent != NULL)
410 sqlite3_finalize (plugin->insertContent); 404 sqlite3_finalize (plugin->insertContent);
411 result = sqlite3_close(plugin->dbh); 405 result = sqlite3_close (plugin->dbh);
412#if SQLITE_VERSION_NUMBER >= 3007000 406#if SQLITE_VERSION_NUMBER >= 3007000
413 if (result == SQLITE_BUSY) 407 if (result == SQLITE_BUSY)
408 {
409 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
410 "sqlite",
411 _
412 ("Tried to close sqlite without finalizing all prepared statements.\n"));
413 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
414 while (stmt != NULL)
414 { 415 {
415 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
416 "sqlite",
417 _("Tried to close sqlite without finalizing all prepared statements.\n"));
418 stmt = sqlite3_next_stmt(plugin->dbh, NULL);
419 while (stmt != NULL)
420 {
421#if DEBUG_SQLITE 416#if DEBUG_SQLITE
422 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 417 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
423 "sqlite", "Closing statement %p\n", stmt); 418 "sqlite", "Closing statement %p\n", stmt);
424#endif 419#endif
425 result = sqlite3_finalize(stmt); 420 result = sqlite3_finalize (stmt);
426#if DEBUG_SQLITE 421#if DEBUG_SQLITE
427 if (result != SQLITE_OK) 422 if (result != SQLITE_OK)
428 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 423 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
429 "sqlite", 424 "sqlite",
430 "Failed to close statement %p: %d\n", stmt, result); 425 "Failed to close statement %p: %d\n", stmt, result);
431#endif 426#endif
432 stmt = sqlite3_next_stmt(plugin->dbh, NULL); 427 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
433 }
434 result = sqlite3_close(plugin->dbh);
435 } 428 }
429 result = sqlite3_close (plugin->dbh);
430 }
436#endif 431#endif
437 if (SQLITE_OK != result) 432 if (SQLITE_OK != result)
438 LOG_SQLITE (plugin, NULL, 433 LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
439 GNUNET_ERROR_TYPE_ERROR,
440 "sqlite3_close");
441 434
442 GNUNET_free_non_null (plugin->fn); 435 GNUNET_free_non_null (plugin->fn);
443} 436}
@@ -451,34 +444,34 @@ database_shutdown (struct Plugin *plugin)
451 * @param rid the ID of the row to delete 444 * @param rid the ID of the row to delete
452 */ 445 */
453static int 446static int
454delete_by_rowid (struct Plugin* plugin, 447delete_by_rowid (struct Plugin *plugin, unsigned long long rid)
455 unsigned long long rid)
456{ 448{
457 if (SQLITE_OK != 449 if (SQLITE_OK != sqlite3_bind_int64 (plugin->delRow, 1, rid))
458 sqlite3_bind_int64 (plugin->delRow, 1, rid)) 450 {
459 { 451 LOG_SQLITE (plugin, NULL,
452 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
453 "sqlite3_bind_XXXX");
454 if (SQLITE_OK != sqlite3_reset (plugin->delRow))
460 LOG_SQLITE (plugin, NULL, 455 LOG_SQLITE (plugin, NULL,
461 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX"); 456 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
462 if (SQLITE_OK != sqlite3_reset (plugin->delRow)) 457 "sqlite3_reset");
463 LOG_SQLITE (plugin, NULL, 458 return GNUNET_SYSERR;
464 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 459 }
465 return GNUNET_SYSERR;
466 }
467 if (SQLITE_DONE != sqlite3_step (plugin->delRow)) 460 if (SQLITE_DONE != sqlite3_step (plugin->delRow))
468 { 461 {
462 LOG_SQLITE (plugin, NULL,
463 GNUNET_ERROR_TYPE_ERROR |
464 GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
465 if (SQLITE_OK != sqlite3_reset (plugin->delRow))
469 LOG_SQLITE (plugin, NULL, 466 LOG_SQLITE (plugin, NULL,
470 GNUNET_ERROR_TYPE_ERROR | 467 GNUNET_ERROR_TYPE_ERROR |
471 GNUNET_ERROR_TYPE_BULK, "sqlite3_step"); 468 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
472 if (SQLITE_OK != sqlite3_reset (plugin->delRow)) 469 return GNUNET_SYSERR;
473 LOG_SQLITE (plugin, NULL, 470 }
474 GNUNET_ERROR_TYPE_ERROR |
475 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
476 return GNUNET_SYSERR;
477 }
478 if (SQLITE_OK != sqlite3_reset (plugin->delRow)) 471 if (SQLITE_OK != sqlite3_reset (plugin->delRow))
479 LOG_SQLITE (plugin, NULL, 472 LOG_SQLITE (plugin, NULL,
480 GNUNET_ERROR_TYPE_ERROR | 473 GNUNET_ERROR_TYPE_ERROR |
481 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 474 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
482 return GNUNET_OK; 475 return GNUNET_OK;
483} 476}
484 477
@@ -500,15 +493,14 @@ delete_by_rowid (struct Plugin* plugin,
500 */ 493 */
501static int 494static int
502sqlite_plugin_put (void *cls, 495sqlite_plugin_put (void *cls,
503 const GNUNET_HashCode *key, 496 const GNUNET_HashCode * key,
504 uint32_t size, 497 uint32_t size,
505 const void *data, 498 const void *data,
506 enum GNUNET_BLOCK_Type type, 499 enum GNUNET_BLOCK_Type type,
507 uint32_t priority, 500 uint32_t priority,
508 uint32_t anonymity, 501 uint32_t anonymity,
509 uint32_t replication, 502 uint32_t replication,
510 struct GNUNET_TIME_Absolute expiration, 503 struct GNUNET_TIME_Absolute expiration, char **msg)
511 char ** msg)
512{ 504{
513 struct Plugin *plugin = cls; 505 struct Plugin *plugin = cls;
514 int n; 506 int n;
@@ -521,13 +513,14 @@ sqlite_plugin_put (void *cls,
521 return GNUNET_SYSERR; 513 return GNUNET_SYSERR;
522#if DEBUG_SQLITE 514#if DEBUG_SQLITE
523 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 515 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
524 "sqlite", 516 "sqlite",
525 "Storing in database block with type %u/key `%s'/priority %u/expiration in %llu ms (%lld).\n", 517 "Storing in database block with type %u/key `%s'/priority %u/expiration in %llu ms (%lld).\n",
526 type, 518 type,
527 GNUNET_h2s(key), 519 GNUNET_h2s (key),
528 priority, 520 priority,
529 (unsigned long long) GNUNET_TIME_absolute_get_remaining (expiration).rel_value, 521 (unsigned long long)
530 (long long) expiration.abs_value); 522 GNUNET_TIME_absolute_get_remaining (expiration).rel_value,
523 (long long) expiration.abs_value);
531#endif 524#endif
532 GNUNET_CRYPTO_hash (data, size, &vhash); 525 GNUNET_CRYPTO_hash (data, size, &vhash);
533 stmt = plugin->insertContent; 526 stmt = plugin->insertContent;
@@ -545,55 +538,54 @@ sqlite_plugin_put (void *cls,
545 sqlite3_bind_blob (stmt, 8, &vhash, sizeof (GNUNET_HashCode), 538 sqlite3_bind_blob (stmt, 8, &vhash, sizeof (GNUNET_HashCode),
546 SQLITE_TRANSIENT)) 539 SQLITE_TRANSIENT))
547 || (SQLITE_OK != 540 || (SQLITE_OK !=
548 sqlite3_bind_blob (stmt, 9, data, size, 541 sqlite3_bind_blob (stmt, 9, data, size, SQLITE_TRANSIENT)))
549 SQLITE_TRANSIENT))) 542 {
550 { 543 LOG_SQLITE (plugin,
551 LOG_SQLITE (plugin, 544 msg,
552 msg, 545 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
553 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX"); 546 "sqlite3_bind_XXXX");
554 if (SQLITE_OK != sqlite3_reset (stmt)) 547 if (SQLITE_OK != sqlite3_reset (stmt))
555 LOG_SQLITE (plugin, NULL, 548 LOG_SQLITE (plugin, NULL,
556 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 549 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
557 return GNUNET_SYSERR; 550 "sqlite3_reset");
558 } 551 return GNUNET_SYSERR;
552 }
559 n = sqlite3_step (stmt); 553 n = sqlite3_step (stmt);
560 switch (n) 554 switch (n)
561 { 555 {
562 case SQLITE_DONE: 556 case SQLITE_DONE:
563 plugin->env->duc (plugin->env->cls, 557 plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
564 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
565#if DEBUG_SQLITE 558#if DEBUG_SQLITE
566 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 559 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
567 "sqlite", 560 "sqlite",
568 "Stored new entry (%u bytes)\n", 561 "Stored new entry (%u bytes)\n",
569 size + GNUNET_DATASTORE_ENTRY_OVERHEAD); 562 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
570#endif 563#endif
571 ret = GNUNET_OK; 564 ret = GNUNET_OK;
572 break; 565 break;
573 case SQLITE_BUSY: 566 case SQLITE_BUSY:
574 GNUNET_break (0); 567 GNUNET_break (0);
575 LOG_SQLITE (plugin, msg, 568 LOG_SQLITE (plugin, msg,
576 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 569 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
577 "sqlite3_step"); 570 "sqlite3_step");
578 ret = GNUNET_SYSERR; 571 ret = GNUNET_SYSERR;
579 break; 572 break;
580 default: 573 default:
581 LOG_SQLITE (plugin, msg, 574 LOG_SQLITE (plugin, msg,
582 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 575 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
583 "sqlite3_step"); 576 "sqlite3_step");
584 if (SQLITE_OK != sqlite3_reset (stmt)) 577 if (SQLITE_OK != sqlite3_reset (stmt))
585 LOG_SQLITE (plugin, NULL, 578 LOG_SQLITE (plugin, NULL,
586 GNUNET_ERROR_TYPE_ERROR | 579 GNUNET_ERROR_TYPE_ERROR |
587 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 580 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
588 database_shutdown (plugin); 581 database_shutdown (plugin);
589 database_setup (plugin->env->cfg, 582 database_setup (plugin->env->cfg, plugin);
590 plugin); 583 return GNUNET_SYSERR;
591 return GNUNET_SYSERR; 584 }
592 }
593 if (SQLITE_OK != sqlite3_reset (stmt)) 585 if (SQLITE_OK != sqlite3_reset (stmt))
594 LOG_SQLITE (plugin, NULL, 586 LOG_SQLITE (plugin, NULL,
595 GNUNET_ERROR_TYPE_ERROR | 587 GNUNET_ERROR_TYPE_ERROR |
596 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 588 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
597 return ret; 589 return ret;
598} 590}
599 591
@@ -623,52 +615,51 @@ sqlite_plugin_put (void *cls,
623 */ 615 */
624static int 616static int
625sqlite_plugin_update (void *cls, 617sqlite_plugin_update (void *cls,
626 uint64_t uid, 618 uint64_t uid,
627 int delta, struct GNUNET_TIME_Absolute expire, 619 int delta, struct GNUNET_TIME_Absolute expire, char **msg)
628 char **msg)
629{ 620{
630 struct Plugin *plugin = cls; 621 struct Plugin *plugin = cls;
631 int n; 622 int n;
632 623
633 if ( (SQLITE_OK != 624 if ((SQLITE_OK !=
634 sqlite3_bind_int (plugin->updPrio, 1, delta)) || 625 sqlite3_bind_int (plugin->updPrio, 1, delta)) ||
635 (SQLITE_OK != 626 (SQLITE_OK !=
636 sqlite3_bind_int64 (plugin->updPrio, 2, expire.abs_value)) || 627 sqlite3_bind_int64 (plugin->updPrio, 2, expire.abs_value)) ||
637 (SQLITE_OK != 628 (SQLITE_OK != sqlite3_bind_int64 (plugin->updPrio, 3, uid)))
638 sqlite3_bind_int64 (plugin->updPrio, 3, uid)) ) 629 {
639 { 630 LOG_SQLITE (plugin, msg,
640 LOG_SQLITE (plugin, msg, 631 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
641 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX"); 632 "sqlite3_bind_XXXX");
642 if (SQLITE_OK != sqlite3_reset (plugin->updPrio)) 633 if (SQLITE_OK != sqlite3_reset (plugin->updPrio))
643 LOG_SQLITE (plugin, NULL, 634 LOG_SQLITE (plugin, NULL,
644 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 635 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
645 return GNUNET_SYSERR; 636 "sqlite3_reset");
637 return GNUNET_SYSERR;
646 638
647 } 639 }
648 n = sqlite3_step (plugin->updPrio); 640 n = sqlite3_step (plugin->updPrio);
649 if (SQLITE_OK != sqlite3_reset (plugin->updPrio)) 641 if (SQLITE_OK != sqlite3_reset (plugin->updPrio))
650 LOG_SQLITE (plugin, NULL, 642 LOG_SQLITE (plugin, NULL,
651 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 643 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
652 switch (n) 644 "sqlite3_reset");
653 { 645 switch (n)
654 case SQLITE_DONE: 646 {
647 case SQLITE_DONE:
655#if DEBUG_SQLITE 648#if DEBUG_SQLITE
656 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 649 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Block updated\n");
657 "sqlite",
658 "Block updated\n");
659#endif 650#endif
660 return GNUNET_OK; 651 return GNUNET_OK;
661 case SQLITE_BUSY: 652 case SQLITE_BUSY:
662 LOG_SQLITE (plugin, msg, 653 LOG_SQLITE (plugin, msg,
663 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 654 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
664 "sqlite3_step"); 655 "sqlite3_step");
665 return GNUNET_NO; 656 return GNUNET_NO;
666 default: 657 default:
667 LOG_SQLITE (plugin, msg, 658 LOG_SQLITE (plugin, msg,
668 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 659 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
669 "sqlite3_step"); 660 "sqlite3_step");
670 return GNUNET_SYSERR; 661 return GNUNET_SYSERR;
671 } 662 }
672} 663}
673 664
674 665
@@ -683,8 +674,7 @@ sqlite_plugin_update (void *cls,
683 */ 674 */
684static void 675static void
685execute_get (struct Plugin *plugin, 676execute_get (struct Plugin *plugin,
686 sqlite3_stmt *stmt, 677 sqlite3_stmt * stmt, PluginDatumProcessor proc, void *proc_cls)
687 PluginDatumProcessor proc, void *proc_cls)
688{ 678{
689 int n; 679 int n;
690 struct GNUNET_TIME_Absolute expiration; 680 struct GNUNET_TIME_Absolute expiration;
@@ -694,80 +684,74 @@ execute_get (struct Plugin *plugin,
694 684
695 n = sqlite3_step (stmt); 685 n = sqlite3_step (stmt);
696 switch (n) 686 switch (n)
687 {
688 case SQLITE_ROW:
689 size = sqlite3_column_bytes (stmt, 5);
690 rowid = sqlite3_column_int64 (stmt, 6);
691 if (sqlite3_column_bytes (stmt, 4) != sizeof (GNUNET_HashCode))
697 { 692 {
698 case SQLITE_ROW: 693 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
699 size = sqlite3_column_bytes (stmt, 5); 694 "sqlite",
700 rowid = sqlite3_column_int64 (stmt, 6); 695 _
701 if (sqlite3_column_bytes (stmt, 4) != sizeof (GNUNET_HashCode)) 696 ("Invalid data in database. Trying to fix (by deletion).\n"));
702 {
703 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
704 "sqlite",
705 _("Invalid data in database. Trying to fix (by deletion).\n"));
706 if (SQLITE_OK != sqlite3_reset (stmt))
707 LOG_SQLITE (plugin, NULL,
708 GNUNET_ERROR_TYPE_ERROR |
709 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
710 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
711 plugin->env->duc (plugin->env->cls,
712 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
713 break;
714 }
715 expiration.abs_value = sqlite3_column_int64 (stmt, 3);
716#if DEBUG_SQLITE
717 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
718 "sqlite",
719 "Found reply in database with expiration %llu\n",
720 (unsigned long long) expiration.abs_value);
721#endif
722 ret = proc (proc_cls,
723 sqlite3_column_blob (stmt, 4) /* key */,
724 size,
725 sqlite3_column_blob (stmt, 5) /* data */,
726 sqlite3_column_int (stmt, 0) /* type */,
727 sqlite3_column_int (stmt, 1) /* priority */,
728 sqlite3_column_int (stmt, 2) /* anonymity */,
729 expiration,
730 rowid);
731 if (SQLITE_OK != sqlite3_reset (stmt))
732 LOG_SQLITE (plugin, NULL,
733 GNUNET_ERROR_TYPE_ERROR |
734 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
735 if ( (GNUNET_NO == ret) &&
736 (GNUNET_OK == delete_by_rowid (plugin, rowid)) )
737 plugin->env->duc (plugin->env->cls,
738 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
739 return;
740 case SQLITE_DONE:
741 /* database must be empty */
742 if (SQLITE_OK != sqlite3_reset (stmt))
743 LOG_SQLITE (plugin, NULL,
744 GNUNET_ERROR_TYPE_ERROR |
745 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
746 break;
747 case SQLITE_BUSY:
748 case SQLITE_ERROR:
749 case SQLITE_MISUSE:
750 default:
751 LOG_SQLITE (plugin, NULL,
752 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
753 "sqlite3_step");
754 if (SQLITE_OK != sqlite3_reset (stmt)) 697 if (SQLITE_OK != sqlite3_reset (stmt))
755 LOG_SQLITE (plugin, NULL, 698 LOG_SQLITE (plugin, NULL,
756 GNUNET_ERROR_TYPE_ERROR | 699 GNUNET_ERROR_TYPE_ERROR |
757 GNUNET_ERROR_TYPE_BULK, 700 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
758 "sqlite3_reset"); 701 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
759 GNUNET_break (0); 702 plugin->env->duc (plugin->env->cls,
760 database_shutdown (plugin); 703 -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
761 database_setup (plugin->env->cfg,
762 plugin);
763 break; 704 break;
764 } 705 }
706 expiration.abs_value = sqlite3_column_int64 (stmt, 3);
707#if DEBUG_SQLITE
708 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
709 "sqlite",
710 "Found reply in database with expiration %llu\n",
711 (unsigned long long) expiration.abs_value);
712#endif
713 ret = proc (proc_cls, sqlite3_column_blob (stmt, 4) /* key */ ,
714 size, sqlite3_column_blob (stmt, 5) /* data */ ,
715 sqlite3_column_int (stmt, 0) /* type */ ,
716 sqlite3_column_int (stmt, 1) /* priority */ ,
717 sqlite3_column_int (stmt, 2) /* anonymity */ ,
718 expiration, rowid);
719 if (SQLITE_OK != sqlite3_reset (stmt))
720 LOG_SQLITE (plugin, NULL,
721 GNUNET_ERROR_TYPE_ERROR |
722 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
723 if ((GNUNET_NO == ret) && (GNUNET_OK == delete_by_rowid (plugin, rowid)))
724 plugin->env->duc (plugin->env->cls,
725 -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
726 return;
727 case SQLITE_DONE:
728 /* database must be empty */
729 if (SQLITE_OK != sqlite3_reset (stmt))
730 LOG_SQLITE (plugin, NULL,
731 GNUNET_ERROR_TYPE_ERROR |
732 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
733 break;
734 case SQLITE_BUSY:
735 case SQLITE_ERROR:
736 case SQLITE_MISUSE:
737 default:
738 LOG_SQLITE (plugin, NULL,
739 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
740 "sqlite3_step");
741 if (SQLITE_OK != sqlite3_reset (stmt))
742 LOG_SQLITE (plugin, NULL,
743 GNUNET_ERROR_TYPE_ERROR |
744 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
745 GNUNET_break (0);
746 database_shutdown (plugin);
747 database_setup (plugin->env->cfg, plugin);
748 break;
749 }
765 if (SQLITE_OK != sqlite3_reset (stmt)) 750 if (SQLITE_OK != sqlite3_reset (stmt))
766 LOG_SQLITE (plugin, NULL, 751 LOG_SQLITE (plugin, NULL,
767 GNUNET_ERROR_TYPE_ERROR | 752 GNUNET_ERROR_TYPE_ERROR |
768 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 753 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
769 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 754 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
770 GNUNET_TIME_UNIT_ZERO_ABS, 0);
771} 755}
772 756
773 757
@@ -787,31 +771,28 @@ execute_get (struct Plugin *plugin,
787 */ 771 */
788static void 772static void
789sqlite_plugin_get_zero_anonymity (void *cls, 773sqlite_plugin_get_zero_anonymity (void *cls,
790 uint64_t offset, 774 uint64_t offset,
791 enum GNUNET_BLOCK_Type type, 775 enum GNUNET_BLOCK_Type type,
792 PluginDatumProcessor proc, 776 PluginDatumProcessor proc, void *proc_cls)
793 void *proc_cls)
794{ 777{
795 struct Plugin *plugin = cls; 778 struct Plugin *plugin = cls;
796 sqlite3_stmt *stmt; 779 sqlite3_stmt *stmt;
797 780
798 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY); 781 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
799 stmt = plugin->selZeroAnon; 782 stmt = plugin->selZeroAnon;
800 if ( (SQLITE_OK != sqlite3_bind_int (stmt, 1, type)) || 783 if ((SQLITE_OK != sqlite3_bind_int (stmt, 1, type)) ||
801 (SQLITE_OK != sqlite3_bind_int64 (stmt, 2, offset)) ) 784 (SQLITE_OK != sqlite3_bind_int64 (stmt, 2, offset)))
802 { 785 {
786 LOG_SQLITE (plugin, NULL,
787 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
788 "sqlite3_bind_XXXX");
789 if (SQLITE_OK != sqlite3_reset (stmt))
803 LOG_SQLITE (plugin, NULL, 790 LOG_SQLITE (plugin, NULL,
804 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 791 GNUNET_ERROR_TYPE_ERROR |
805 "sqlite3_bind_XXXX"); 792 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
806 if (SQLITE_OK != sqlite3_reset (stmt)) 793 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
807 LOG_SQLITE (plugin, NULL, 794 return;
808 GNUNET_ERROR_TYPE_ERROR | 795 }
809 GNUNET_ERROR_TYPE_BULK,
810 "sqlite3_reset");
811 proc (proc_cls, NULL, 0, NULL, 0, 0, 0,
812 GNUNET_TIME_UNIT_ZERO_ABS, 0);
813 return;
814 }
815 execute_get (plugin, stmt, proc, proc_cls); 796 execute_get (plugin, stmt, proc, proc_cls);
816} 797}
817 798
@@ -836,11 +817,11 @@ sqlite_plugin_get_zero_anonymity (void *cls,
836 */ 817 */
837static void 818static void
838sqlite_plugin_get_key (void *cls, 819sqlite_plugin_get_key (void *cls,
839 uint64_t offset, 820 uint64_t offset,
840 const GNUNET_HashCode *key, 821 const GNUNET_HashCode * key,
841 const GNUNET_HashCode *vhash, 822 const GNUNET_HashCode * vhash,
842 enum GNUNET_BLOCK_Type type, 823 enum GNUNET_BLOCK_Type type,
843 PluginDatumProcessor proc, void *proc_cls) 824 PluginDatumProcessor proc, void *proc_cls)
844{ 825{
845 struct Plugin *plugin = cls; 826 struct Plugin *plugin = cls;
846 int ret; 827 int ret;
@@ -855,48 +836,47 @@ sqlite_plugin_get_key (void *cls,
855 GNUNET_snprintf (scratch, sizeof (scratch), 836 GNUNET_snprintf (scratch, sizeof (scratch),
856 "SELECT count(*) FROM gn090 WHERE hash=?%s%s", 837 "SELECT count(*) FROM gn090 WHERE hash=?%s%s",
857 vhash == NULL ? "" : " AND vhash=?", 838 vhash == NULL ? "" : " AND vhash=?",
858 type == 0 ? "" : " AND type=?"); 839 type == 0 ? "" : " AND type=?");
859 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK) 840 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
860 { 841 {
861 LOG_SQLITE (plugin, NULL, 842 LOG_SQLITE (plugin, NULL,
862 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite_prepare"); 843 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
863 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 844 "sqlite_prepare");
864 return; 845 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
865 } 846 return;
847 }
866 sqoff = 1; 848 sqoff = 1;
867 ret = sqlite3_bind_blob (stmt, sqoff++, 849 ret = sqlite3_bind_blob (stmt, sqoff++,
868 key, sizeof (GNUNET_HashCode), SQLITE_TRANSIENT); 850 key, sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
869 if ((vhash != NULL) && (ret == SQLITE_OK)) 851 if ((vhash != NULL) && (ret == SQLITE_OK))
870 ret = sqlite3_bind_blob (stmt, sqoff++, 852 ret = sqlite3_bind_blob (stmt, sqoff++,
871 vhash, 853 vhash, sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
872 sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
873 if ((type != 0) && (ret == SQLITE_OK)) 854 if ((type != 0) && (ret == SQLITE_OK))
874 ret = sqlite3_bind_int (stmt, sqoff++, type); 855 ret = sqlite3_bind_int (stmt, sqoff++, type);
875 if (SQLITE_OK != ret) 856 if (SQLITE_OK != ret)
876 { 857 {
877 LOG_SQLITE (plugin, NULL, 858 LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_bind");
878 GNUNET_ERROR_TYPE_ERROR, "sqlite_bind"); 859 sqlite3_finalize (stmt);
879 sqlite3_finalize (stmt); 860 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
880 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 861 return;
881 return; 862 }
882 }
883 ret = sqlite3_step (stmt); 863 ret = sqlite3_step (stmt);
884 if (ret != SQLITE_ROW) 864 if (ret != SQLITE_ROW)
885 { 865 {
886 LOG_SQLITE (plugin, NULL, 866 LOG_SQLITE (plugin, NULL,
887 GNUNET_ERROR_TYPE_ERROR| GNUNET_ERROR_TYPE_BULK, 867 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
888 "sqlite_step"); 868 "sqlite_step");
889 sqlite3_finalize (stmt); 869 sqlite3_finalize (stmt);
890 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 870 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
891 return; 871 return;
892 } 872 }
893 total = sqlite3_column_int (stmt, 0); 873 total = sqlite3_column_int (stmt, 0);
894 sqlite3_finalize (stmt); 874 sqlite3_finalize (stmt);
895 if (0 == total) 875 if (0 == total)
896 { 876 {
897 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 877 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
898 return; 878 return;
899 } 879 }
900 limit_off = (int) (offset % total); 880 limit_off = (int) (offset % total);
901 if (limit_off < 0) 881 if (limit_off < 0)
902 limit_off += total; 882 limit_off += total;
@@ -907,36 +887,33 @@ sqlite_plugin_get_key (void *cls,
907 vhash == NULL ? "" : " AND vhash=?", 887 vhash == NULL ? "" : " AND vhash=?",
908 type == 0 ? "" : " AND type=?"); 888 type == 0 ? "" : " AND type=?");
909 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK) 889 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
910 { 890 {
911 LOG_SQLITE (plugin, NULL, 891 LOG_SQLITE (plugin, NULL,
912 GNUNET_ERROR_TYPE_ERROR | 892 GNUNET_ERROR_TYPE_ERROR |
913 GNUNET_ERROR_TYPE_BULK, "sqlite_prepare"); 893 GNUNET_ERROR_TYPE_BULK, "sqlite_prepare");
914 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 894 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
915 return; 895 return;
916 } 896 }
917 sqoff = 1; 897 sqoff = 1;
918 ret = sqlite3_bind_blob (stmt, 898 ret = sqlite3_bind_blob (stmt,
919 sqoff++, 899 sqoff++,
920 key, 900 key, sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
921 sizeof (GNUNET_HashCode),
922 SQLITE_TRANSIENT);
923 if ((vhash != NULL) && (ret == SQLITE_OK)) 901 if ((vhash != NULL) && (ret == SQLITE_OK))
924 ret = sqlite3_bind_blob (stmt, 902 ret = sqlite3_bind_blob (stmt,
925 sqoff++, 903 sqoff++,
926 vhash, 904 vhash, sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
927 sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
928 if ((type != 0) && (ret == SQLITE_OK)) 905 if ((type != 0) && (ret == SQLITE_OK))
929 ret = sqlite3_bind_int (stmt, sqoff++, type); 906 ret = sqlite3_bind_int (stmt, sqoff++, type);
930 if (ret == SQLITE_OK) 907 if (ret == SQLITE_OK)
931 ret = sqlite3_bind_int64 (stmt, sqoff++, limit_off); 908 ret = sqlite3_bind_int64 (stmt, sqoff++, limit_off);
932 if (ret != SQLITE_OK) 909 if (ret != SQLITE_OK)
933 { 910 {
934 LOG_SQLITE (plugin, NULL, 911 LOG_SQLITE (plugin, NULL,
935 GNUNET_ERROR_TYPE_ERROR | 912 GNUNET_ERROR_TYPE_ERROR |
936 GNUNET_ERROR_TYPE_BULK, "sqlite_bind"); 913 GNUNET_ERROR_TYPE_BULK, "sqlite_bind");
937 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 914 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
938 return; 915 return;
939 } 916 }
940 execute_get (plugin, stmt, proc, proc_cls); 917 execute_get (plugin, stmt, proc, proc_cls);
941 sqlite3_finalize (stmt); 918 sqlite3_finalize (stmt);
942} 919}
@@ -948,12 +925,12 @@ sqlite_plugin_get_key (void *cls,
948 */ 925 */
949struct ReplCtx 926struct ReplCtx
950{ 927{
951 928
952 /** 929 /**
953 * Function to call for the result (or the NULL). 930 * Function to call for the result (or the NULL).
954 */ 931 */
955 PluginDatumProcessor proc; 932 PluginDatumProcessor proc;
956 933
957 /** 934 /**
958 * Closure for proc. 935 * Closure for proc.
959 */ 936 */
@@ -992,28 +969,24 @@ struct ReplCtx
992 */ 969 */
993static int 970static int
994repl_proc (void *cls, 971repl_proc (void *cls,
995 const GNUNET_HashCode *key, 972 const GNUNET_HashCode * key,
996 uint32_t size, 973 uint32_t size,
997 const void *data, 974 const void *data,
998 enum GNUNET_BLOCK_Type type, 975 enum GNUNET_BLOCK_Type type,
999 uint32_t priority, 976 uint32_t priority,
1000 uint32_t anonymity, 977 uint32_t anonymity,
1001 struct GNUNET_TIME_Absolute expiration, 978 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
1002 uint64_t uid)
1003{ 979{
1004 struct ReplCtx *rc = cls; 980 struct ReplCtx *rc = cls;
1005 int ret; 981 int ret;
1006 982
1007 ret = rc->proc (rc->proc_cls, 983 ret = rc->proc (rc->proc_cls,
1008 key, 984 key, size, data, type, priority, anonymity, expiration, uid);
1009 size, data,
1010 type, priority, anonymity, expiration,
1011 uid);
1012 if (key != NULL) 985 if (key != NULL)
1013 { 986 {
1014 rc->uid = uid; 987 rc->uid = uid;
1015 rc->have_uid = GNUNET_YES; 988 rc->have_uid = GNUNET_YES;
1016 } 989 }
1017 return ret; 990 return ret;
1018} 991}
1019 992
@@ -1030,7 +1003,7 @@ repl_proc (void *cls,
1030 */ 1003 */
1031static void 1004static void
1032sqlite_plugin_get_replication (void *cls, 1005sqlite_plugin_get_replication (void *cls,
1033 PluginDatumProcessor proc, void *proc_cls) 1006 PluginDatumProcessor proc, void *proc_cls)
1034{ 1007{
1035 struct Plugin *plugin = cls; 1008 struct Plugin *plugin = cls;
1036 struct ReplCtx rc; 1009 struct ReplCtx rc;
@@ -1040,76 +1013,77 @@ sqlite_plugin_get_replication (void *cls,
1040 1013
1041#if DEBUG_SQLITE 1014#if DEBUG_SQLITE
1042 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1015 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1043 "sqlite", 1016 "sqlite",
1044 "Getting random block based on replication order.\n"); 1017 "Getting random block based on replication order.\n");
1045#endif 1018#endif
1046 rc.have_uid = GNUNET_NO; 1019 rc.have_uid = GNUNET_NO;
1047 rc.proc = proc; 1020 rc.proc = proc;
1048 rc.proc_cls = proc_cls; 1021 rc.proc_cls = proc_cls;
1049 stmt = plugin->maxRepl; 1022 stmt = plugin->maxRepl;
1050 if (SQLITE_ROW != sqlite3_step (stmt)) 1023 if (SQLITE_ROW != sqlite3_step (stmt))
1051 { 1024 {
1052 if (SQLITE_OK != sqlite3_reset (stmt)) 1025 if (SQLITE_OK != sqlite3_reset (stmt))
1053 LOG_SQLITE (plugin, NULL, 1026 LOG_SQLITE (plugin, NULL,
1054 GNUNET_ERROR_TYPE_ERROR | 1027 GNUNET_ERROR_TYPE_ERROR |
1055 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 1028 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1056 /* DB empty */ 1029 /* DB empty */
1057 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 1030 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1058 GNUNET_TIME_UNIT_ZERO_ABS, 0); 1031 return;
1059 return; 1032 }
1060 }
1061 repl = sqlite3_column_int (stmt, 0); 1033 repl = sqlite3_column_int (stmt, 0);
1062 if (SQLITE_OK != sqlite3_reset (stmt)) 1034 if (SQLITE_OK != sqlite3_reset (stmt))
1063 LOG_SQLITE (plugin, NULL, 1035 LOG_SQLITE (plugin, NULL,
1064 GNUNET_ERROR_TYPE_ERROR | 1036 GNUNET_ERROR_TYPE_ERROR |
1065 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 1037 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1066 stmt = plugin->selRepl; 1038 stmt = plugin->selRepl;
1067 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX); 1039 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
1068 if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, rvalue)) 1040 if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, rvalue))
1069 { 1041 {
1070 LOG_SQLITE (plugin, NULL, 1042 LOG_SQLITE (plugin, NULL,
1071 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX"); 1043 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1072 if (SQLITE_OK != sqlite3_reset (stmt)) 1044 "sqlite3_bind_XXXX");
1073 LOG_SQLITE (plugin, NULL, 1045 if (SQLITE_OK != sqlite3_reset (stmt))
1074 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 1046 LOG_SQLITE (plugin, NULL,
1075 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 1047 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1076 GNUNET_TIME_UNIT_ZERO_ABS, 0); 1048 "sqlite3_reset");
1077 return; 1049 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1078 } 1050 return;
1051 }
1079 if (SQLITE_OK != sqlite3_bind_int (stmt, 2, repl)) 1052 if (SQLITE_OK != sqlite3_bind_int (stmt, 2, repl))
1080 { 1053 {
1081 LOG_SQLITE (plugin, NULL, 1054 LOG_SQLITE (plugin, NULL,
1082 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX"); 1055 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1083 if (SQLITE_OK != sqlite3_reset (stmt)) 1056 "sqlite3_bind_XXXX");
1084 LOG_SQLITE (plugin, NULL, 1057 if (SQLITE_OK != sqlite3_reset (stmt))
1085 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 1058 LOG_SQLITE (plugin, NULL,
1086 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 1059 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1087 GNUNET_TIME_UNIT_ZERO_ABS, 0); 1060 "sqlite3_reset");
1088 return; 1061 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1089 } 1062 return;
1090 execute_get (plugin, stmt, &repl_proc, &rc); 1063 }
1064 execute_get (plugin, stmt, &repl_proc, &rc);
1091 if (GNUNET_YES == rc.have_uid) 1065 if (GNUNET_YES == rc.have_uid)
1066 {
1067 if (SQLITE_OK != sqlite3_bind_int64 (plugin->updRepl, 1, rc.uid))
1092 { 1068 {
1093 if (SQLITE_OK != 1069 LOG_SQLITE (plugin, NULL,
1094 sqlite3_bind_int64 (plugin->updRepl, 1, rc.uid)) 1070 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1095 { 1071 "sqlite3_bind_XXXX");
1096 LOG_SQLITE (plugin, NULL,
1097 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
1098 if (SQLITE_OK != sqlite3_reset (plugin->updRepl))
1099 LOG_SQLITE (plugin, NULL,
1100 GNUNET_ERROR_TYPE_ERROR |
1101 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1102 return;
1103 }
1104 if (SQLITE_DONE != sqlite3_step (plugin->updRepl))
1105 LOG_SQLITE (plugin, NULL,
1106 GNUNET_ERROR_TYPE_ERROR |
1107 GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
1108 if (SQLITE_OK != sqlite3_reset (plugin->updRepl)) 1072 if (SQLITE_OK != sqlite3_reset (plugin->updRepl))
1109 LOG_SQLITE (plugin, NULL, 1073 LOG_SQLITE (plugin, NULL,
1110 GNUNET_ERROR_TYPE_ERROR | 1074 GNUNET_ERROR_TYPE_ERROR |
1111 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 1075 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1076 return;
1112 } 1077 }
1078 if (SQLITE_DONE != sqlite3_step (plugin->updRepl))
1079 LOG_SQLITE (plugin, NULL,
1080 GNUNET_ERROR_TYPE_ERROR |
1081 GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
1082 if (SQLITE_OK != sqlite3_reset (plugin->updRepl))
1083 LOG_SQLITE (plugin, NULL,
1084 GNUNET_ERROR_TYPE_ERROR |
1085 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1086 }
1113} 1087}
1114 1088
1115 1089
@@ -1124,7 +1098,7 @@ sqlite_plugin_get_replication (void *cls,
1124 */ 1098 */
1125static void 1099static void
1126sqlite_plugin_get_expiration (void *cls, 1100sqlite_plugin_get_expiration (void *cls,
1127 PluginDatumProcessor proc, void *proc_cls) 1101 PluginDatumProcessor proc, void *proc_cls)
1128{ 1102{
1129 struct Plugin *plugin = cls; 1103 struct Plugin *plugin = cls;
1130 sqlite3_stmt *stmt; 1104 sqlite3_stmt *stmt;
@@ -1132,22 +1106,23 @@ sqlite_plugin_get_expiration (void *cls,
1132 1106
1133#if DEBUG_SQLITE 1107#if DEBUG_SQLITE
1134 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1108 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1135 "sqlite", 1109 "sqlite",
1136 "Getting random block based on expiration and priority order.\n"); 1110 "Getting random block based on expiration and priority order.\n");
1137#endif 1111#endif
1138 now = GNUNET_TIME_absolute_get (); 1112 now = GNUNET_TIME_absolute_get ();
1139 stmt = plugin->selExpi; 1113 stmt = plugin->selExpi;
1140 if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, now.abs_value)) 1114 if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, now.abs_value))
1141 { 1115 {
1142 LOG_SQLITE (plugin, NULL, 1116 LOG_SQLITE (plugin, NULL,
1143 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX"); 1117 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1144 if (SQLITE_OK != sqlite3_reset (stmt)) 1118 "sqlite3_bind_XXXX");
1145 LOG_SQLITE (plugin, NULL, 1119 if (SQLITE_OK != sqlite3_reset (stmt))
1146 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 1120 LOG_SQLITE (plugin, NULL,
1147 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 1121 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1148 GNUNET_TIME_UNIT_ZERO_ABS, 0); 1122 "sqlite3_reset");
1149 return; 1123 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1150 } 1124 return;
1125 }
1151 execute_get (plugin, stmt, proc, proc_cls); 1126 execute_get (plugin, stmt, proc, proc_cls);
1152} 1127}
1153 1128
@@ -1157,10 +1132,11 @@ sqlite_plugin_get_expiration (void *cls,
1157 * 1132 *
1158 * @param cls our plugin context 1133 * @param cls our plugin context
1159 */ 1134 */
1160static void 1135static void
1161sqlite_plugin_drop (void *cls) 1136sqlite_plugin_drop (void *cls)
1162{ 1137{
1163 struct Plugin *plugin = cls; 1138 struct Plugin *plugin = cls;
1139
1164 plugin->drop_on_shutdown = GNUNET_YES; 1140 plugin->drop_on_shutdown = GNUNET_YES;
1165} 1141}
1166 1142
@@ -1179,48 +1155,40 @@ sqlite_plugin_estimate_size (void *cls)
1179 sqlite3_stmt *stmt; 1155 sqlite3_stmt *stmt;
1180 uint64_t pages; 1156 uint64_t pages;
1181 uint64_t page_size; 1157 uint64_t page_size;
1158
1182#if ENULL_DEFINED 1159#if ENULL_DEFINED
1183 char *e; 1160 char *e;
1184#endif 1161#endif
1185 1162
1186 if (SQLITE_VERSION_NUMBER < 3006000) 1163 if (SQLITE_VERSION_NUMBER < 3006000)
1187 { 1164 {
1188 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, 1165 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1189 "datastore-sqlite", 1166 "datastore-sqlite",
1190 _("sqlite version to old to determine size, assuming zero\n")); 1167 _
1191 return 0; 1168 ("sqlite version to old to determine size, assuming zero\n"));
1192 } 1169 return 0;
1193 CHECK (SQLITE_OK == 1170 }
1194 sqlite3_exec (plugin->dbh, 1171 CHECK (SQLITE_OK == sqlite3_exec (plugin->dbh, "VACUUM", NULL, NULL, ENULL));
1195 "VACUUM", NULL, NULL, ENULL));
1196 CHECK (SQLITE_OK ==
1197 sqlite3_exec (plugin->dbh,
1198 "PRAGMA auto_vacuum=INCREMENTAL", NULL, NULL, ENULL));
1199 CHECK (SQLITE_OK == 1172 CHECK (SQLITE_OK ==
1200 sq_prepare (plugin->dbh, 1173 sqlite3_exec (plugin->dbh,
1201 "PRAGMA page_count", 1174 "PRAGMA auto_vacuum=INCREMENTAL", NULL, NULL, ENULL));
1202 &stmt)); 1175 CHECK (SQLITE_OK == sq_prepare (plugin->dbh, "PRAGMA page_count", &stmt));
1203 if (SQLITE_ROW == 1176 if (SQLITE_ROW == sqlite3_step (stmt))
1204 sqlite3_step (stmt))
1205 pages = sqlite3_column_int64 (stmt, 0); 1177 pages = sqlite3_column_int64 (stmt, 0);
1206 else 1178 else
1207 pages = 0; 1179 pages = 0;
1208 sqlite3_finalize (stmt); 1180 sqlite3_finalize (stmt);
1209 CHECK (SQLITE_OK == 1181 CHECK (SQLITE_OK == sq_prepare (plugin->dbh, "PRAGMA page_size", &stmt));
1210 sq_prepare (plugin->dbh, 1182 CHECK (SQLITE_ROW == sqlite3_step (stmt));
1211 "PRAGMA page_size",
1212 &stmt));
1213 CHECK (SQLITE_ROW ==
1214 sqlite3_step (stmt));
1215 page_size = sqlite3_column_int64 (stmt, 0); 1183 page_size = sqlite3_column_int64 (stmt, 0);
1216 sqlite3_finalize (stmt); 1184 sqlite3_finalize (stmt);
1217 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1185 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1218 _("Using sqlite page utilization to estimate payload (%llu pages of size %llu bytes)\n"), 1186 _
1219 (unsigned long long) pages, 1187 ("Using sqlite page utilization to estimate payload (%llu pages of size %llu bytes)\n"),
1220 (unsigned long long) page_size); 1188 (unsigned long long) pages, (unsigned long long) page_size);
1221 return pages * page_size; 1189 return pages * page_size;
1222} 1190}
1223 1191
1224 1192
1225/** 1193/**
1226 * Entry point for the plugin. 1194 * Entry point for the plugin.
@@ -1236,15 +1204,14 @@ libgnunet_plugin_datastore_sqlite_init (void *cls)
1236 struct GNUNET_DATASTORE_PluginFunctions *api; 1204 struct GNUNET_DATASTORE_PluginFunctions *api;
1237 1205
1238 if (plugin.env != NULL) 1206 if (plugin.env != NULL)
1239 return NULL; /* can only initialize once! */ 1207 return NULL; /* can only initialize once! */
1240 memset (&plugin, 0, sizeof(struct Plugin)); 1208 memset (&plugin, 0, sizeof (struct Plugin));
1241 plugin.env = env; 1209 plugin.env = env;
1242 if (GNUNET_OK != 1210 if (GNUNET_OK != database_setup (env->cfg, &plugin))
1243 database_setup (env->cfg, &plugin)) 1211 {
1244 { 1212 database_shutdown (&plugin);
1245 database_shutdown (&plugin); 1213 return NULL;
1246 return NULL; 1214 }
1247 }
1248 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions)); 1215 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
1249 api->cls = &plugin; 1216 api->cls = &plugin;
1250 api->estimate_size = &sqlite_plugin_estimate_size; 1217 api->estimate_size = &sqlite_plugin_estimate_size;
@@ -1276,8 +1243,7 @@ libgnunet_plugin_datastore_sqlite_done (void *cls)
1276 1243
1277#if DEBUG_SQLITE 1244#if DEBUG_SQLITE
1278 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1245 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1279 "sqlite", 1246 "sqlite", "sqlite plugin is done\n");
1280 "sqlite plugin is done\n");
1281#endif 1247#endif
1282 1248
1283 fn = NULL; 1249 fn = NULL;
@@ -1285,24 +1251,20 @@ libgnunet_plugin_datastore_sqlite_done (void *cls)
1285 fn = GNUNET_strdup (plugin->fn); 1251 fn = GNUNET_strdup (plugin->fn);
1286#if DEBUG_SQLITE 1252#if DEBUG_SQLITE
1287 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1253 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1288 "sqlite", 1254 "sqlite", "Shutting down database\n");
1289 "Shutting down database\n");
1290#endif 1255#endif
1291 database_shutdown (plugin); 1256 database_shutdown (plugin);
1292 plugin->env = NULL; 1257 plugin->env = NULL;
1293 GNUNET_free (api); 1258 GNUNET_free (api);
1294 if (fn != NULL) 1259 if (fn != NULL)
1295 { 1260 {
1296 if (0 != UNLINK(fn)) 1261 if (0 != UNLINK (fn))
1297 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 1262 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1298 "unlink", 1263 GNUNET_free (fn);
1299 fn); 1264 }
1300 GNUNET_free (fn);
1301 }
1302#if DEBUG_SQLITE 1265#if DEBUG_SQLITE
1303 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1266 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1304 "sqlite", 1267 "sqlite", "sqlite plugin is finished\n");
1305 "sqlite plugin is finished\n");
1306#endif 1268#endif
1307 return NULL; 1269 return NULL;
1308} 1270}
diff --git a/src/datastore/plugin_datastore_template.c b/src/datastore/plugin_datastore_template.c
index 8975c0a70..7ab469140 100644
--- a/src/datastore/plugin_datastore_template.c
+++ b/src/datastore/plugin_datastore_template.c
@@ -31,7 +31,7 @@
31/** 31/**
32 * Context for all functions in this plugin. 32 * Context for all functions in this plugin.
33 */ 33 */
34struct Plugin 34struct Plugin
35{ 35{
36 /** 36 /**
37 * Our execution environment. 37 * Our execution environment.
@@ -47,7 +47,7 @@ struct Plugin
47 * @param cls our "struct Plugin*" 47 * @param cls our "struct Plugin*"
48 * @return number of bytes used on disk 48 * @return number of bytes used on disk
49 */ 49 */
50static unsigned long long 50static unsigned long long
51template_plugin_estimate_size (void *cls) 51template_plugin_estimate_size (void *cls)
52{ 52{
53 GNUNET_break (0); 53 GNUNET_break (0);
@@ -72,15 +72,14 @@ template_plugin_estimate_size (void *cls)
72 */ 72 */
73static int 73static int
74template_plugin_put (void *cls, 74template_plugin_put (void *cls,
75 const GNUNET_HashCode * key, 75 const GNUNET_HashCode * key,
76 uint32_t size, 76 uint32_t size,
77 const void *data, 77 const void *data,
78 enum GNUNET_BLOCK_Type type, 78 enum GNUNET_BLOCK_Type type,
79 uint32_t priority, 79 uint32_t priority,
80 uint32_t anonymity, 80 uint32_t anonymity,
81 uint32_t replication, 81 uint32_t replication,
82 struct GNUNET_TIME_Absolute expiration, 82 struct GNUNET_TIME_Absolute expiration, char **msg)
83 char **msg)
84{ 83{
85 GNUNET_break (0); 84 GNUNET_break (0);
86 *msg = GNUNET_strdup ("not implemented"); 85 *msg = GNUNET_strdup ("not implemented");
@@ -108,11 +107,11 @@ template_plugin_put (void *cls,
108 */ 107 */
109static void 108static void
110template_plugin_get_key (void *cls, 109template_plugin_get_key (void *cls,
111 uint64_t offset, 110 uint64_t offset,
112 const GNUNET_HashCode * key, 111 const GNUNET_HashCode * key,
113 const GNUNET_HashCode * vhash, 112 const GNUNET_HashCode * vhash,
114 enum GNUNET_BLOCK_Type type, 113 enum GNUNET_BLOCK_Type type,
115 PluginDatumProcessor proc, void *proc_cls) 114 PluginDatumProcessor proc, void *proc_cls)
116{ 115{
117 GNUNET_break (0); 116 GNUNET_break (0);
118} 117}
@@ -132,7 +131,7 @@ template_plugin_get_key (void *cls,
132 */ 131 */
133static void 132static void
134template_plugin_get_replication (void *cls, 133template_plugin_get_replication (void *cls,
135 PluginDatumProcessor proc, void *proc_cls) 134 PluginDatumProcessor proc, void *proc_cls)
136{ 135{
137 GNUNET_break (0); 136 GNUNET_break (0);
138} 137}
@@ -148,7 +147,7 @@ template_plugin_get_replication (void *cls,
148 */ 147 */
149static void 148static void
150template_plugin_get_expiration (void *cls, 149template_plugin_get_expiration (void *cls,
151 PluginDatumProcessor proc, void *proc_cls) 150 PluginDatumProcessor proc, void *proc_cls)
152{ 151{
153 GNUNET_break (0); 152 GNUNET_break (0);
154} 153}
@@ -179,10 +178,9 @@ template_plugin_get_expiration (void *cls,
179 */ 178 */
180static int 179static int
181template_plugin_update (void *cls, 180template_plugin_update (void *cls,
182 uint64_t uid, 181 uint64_t uid,
183 int delta, 182 int delta,
184 struct GNUNET_TIME_Absolute expire, 183 struct GNUNET_TIME_Absolute expire, char **msg)
185 char **msg)
186{ 184{
187 GNUNET_break (0); 185 GNUNET_break (0);
188 *msg = GNUNET_strdup ("not implemented"); 186 *msg = GNUNET_strdup ("not implemented");
@@ -204,10 +202,9 @@ template_plugin_update (void *cls,
204 */ 202 */
205static void 203static void
206template_plugin_get_zero_anonymity (void *cls, 204template_plugin_get_zero_anonymity (void *cls,
207 uint64_t offset, 205 uint64_t offset,
208 enum GNUNET_BLOCK_Type type, 206 enum GNUNET_BLOCK_Type type,
209 PluginDatumProcessor proc, 207 PluginDatumProcessor proc, void *proc_cls)
210 void *proc_cls)
211{ 208{
212 GNUNET_break (0); 209 GNUNET_break (0);
213} 210}
@@ -216,7 +213,7 @@ template_plugin_get_zero_anonymity (void *cls,
216/** 213/**
217 * Drop database. 214 * Drop database.
218 */ 215 */
219static void 216static void
220template_plugin_drop (void *cls) 217template_plugin_drop (void *cls)
221{ 218{
222 GNUNET_break (0); 219 GNUNET_break (0);
diff --git a/src/datastore/test_datastore_api.c b/src/datastore/test_datastore_api.c
index f25ee08c2..39b7a1ae2 100644
--- a/src/datastore/test_datastore_api.c
+++ b/src/datastore/test_datastore_api.c
@@ -63,34 +63,35 @@ get_size (int i)
63static const void * 63static const void *
64get_data (int i) 64get_data (int i)
65{ 65{
66 static char buf[60000]; 66 static char buf[60000];
67
67 memset (buf, i, 8 * i); 68 memset (buf, i, 8 * i);
68 return buf; 69 return buf;
69} 70}
70 71
71 72
72static int 73static int
73get_type(int i) 74get_type (int i)
74{ 75{
75 return i+1; 76 return i + 1;
76} 77}
77 78
78 79
79static int 80static int
80get_priority (int i) 81get_priority (int i)
81{ 82{
82 return i+1; 83 return i + 1;
83} 84}
84 85
85 86
86static int 87static int
87get_anonymity(int i) 88get_anonymity (int i)
88{ 89{
89 return i; 90 return i;
90} 91}
91 92
92 93
93static struct GNUNET_TIME_Absolute 94static struct GNUNET_TIME_Absolute
94get_expiration (int i) 95get_expiration (int i)
95{ 96{
96 struct GNUNET_TIME_Absolute av; 97 struct GNUNET_TIME_Absolute av;
@@ -100,22 +101,22 @@ get_expiration (int i)
100} 101}
101 102
102enum RunPhase 103enum RunPhase
103 { 104{
104 RP_DONE = 0, 105 RP_DONE = 0,
105 RP_PUT = 1, 106 RP_PUT = 1,
106 RP_GET = 2, 107 RP_GET = 2,
107 RP_DEL = 3, 108 RP_DEL = 3,
108 RP_DO_DEL = 4, 109 RP_DO_DEL = 4,
109 RP_DELVALIDATE = 5, 110 RP_DELVALIDATE = 5,
110 RP_RESERVE = 6, 111 RP_RESERVE = 6,
111 RP_PUT_MULTIPLE = 7, 112 RP_PUT_MULTIPLE = 7,
112 RP_PUT_MULTIPLE_NEXT = 8, 113 RP_PUT_MULTIPLE_NEXT = 8,
113 RP_GET_MULTIPLE = 9, 114 RP_GET_MULTIPLE = 9,
114 RP_GET_MULTIPLE_NEXT = 10, 115 RP_GET_MULTIPLE_NEXT = 10,
115 RP_UPDATE = 11, 116 RP_UPDATE = 11,
116 RP_UPDATE_VALIDATE = 12, 117 RP_UPDATE_VALIDATE = 12,
117 RP_ERROR 118 RP_ERROR
118 }; 119};
119 120
120 121
121struct CpsRunContext 122struct CpsRunContext
@@ -134,62 +135,52 @@ struct CpsRunContext
134 135
135 136
136static void 137static void
137run_continuation (void *cls, 138run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
138 const struct GNUNET_SCHEDULER_TaskContext *tc);
139 139
140 140
141static void 141static void
142check_success (void *cls, 142check_success (void *cls, int success, const char *msg)
143 int success,
144 const char *msg)
145{ 143{
146 struct CpsRunContext *crc = cls; 144 struct CpsRunContext *crc = cls;
147 145
148 if (GNUNET_OK != success) 146 if (GNUNET_OK != success)
149 { 147 {
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
151 "Operation %d/%d not successfull: `%s'\n", 149 "Operation %d/%d not successfull: `%s'\n",
152 crc->phase, 150 crc->phase, crc->i, msg);
153 crc->i, 151 crc->phase = RP_ERROR;
154 msg); 152 }
155 crc->phase = RP_ERROR;
156 }
157 GNUNET_free_non_null (crc->data); 153 GNUNET_free_non_null (crc->data);
158 crc->data = NULL; 154 crc->data = NULL;
159 GNUNET_SCHEDULER_add_continuation (&run_continuation, 155 GNUNET_SCHEDULER_add_continuation (&run_continuation,
160 crc, 156 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
161 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
162} 157}
163 158
164 159
165static void 160static void
166get_reserved (void *cls, 161get_reserved (void *cls, int success, const char *msg)
167 int success,
168 const char *msg)
169{ 162{
170 struct CpsRunContext *crc = cls; 163 struct CpsRunContext *crc = cls;
164
171 if (0 >= success) 165 if (0 >= success)
172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
173 "Error obtaining reservation: `%s'\n", 167 "Error obtaining reservation: `%s'\n", msg);
174 msg);
175 GNUNET_assert (0 < success); 168 GNUNET_assert (0 < success);
176 crc->rid = success; 169 crc->rid = success;
177 GNUNET_SCHEDULER_add_continuation (&run_continuation, 170 GNUNET_SCHEDULER_add_continuation (&run_continuation,
178 crc, 171 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
179 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
180} 172}
181 173
182 174
183static void 175static void
184check_value (void *cls, 176check_value (void *cls,
185 const GNUNET_HashCode * key, 177 const GNUNET_HashCode * key,
186 size_t size, 178 size_t size,
187 const void *data, 179 const void *data,
188 enum GNUNET_BLOCK_Type type, 180 enum GNUNET_BLOCK_Type type,
189 uint32_t priority, 181 uint32_t priority,
190 uint32_t anonymity, 182 uint32_t anonymity,
191 struct GNUNET_TIME_Absolute expiration, 183 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
192 uint64_t uid)
193{ 184{
194 struct CpsRunContext *crc = cls; 185 struct CpsRunContext *crc = cls;
195 int i; 186 int i;
@@ -197,46 +188,42 @@ check_value (void *cls,
197 i = crc->i; 188 i = crc->i;
198#if 0 189#if 0
199 fprintf (stderr, 190 fprintf (stderr,
200 "Check value got `%s' of size %u, type %d, expire %llu\n", 191 "Check value got `%s' of size %u, type %d, expire %llu\n",
201 GNUNET_h2s (key), 192 GNUNET_h2s (key),
202 (unsigned int) size, 193 (unsigned int) size,
203 type, 194 type, (unsigned long long) expiration.abs_value);
204 (unsigned long long) expiration.abs_value);
205 fprintf (stderr, 195 fprintf (stderr,
206 "Check value iteration %d wants size %u, type %d, expire %llu\n", 196 "Check value iteration %d wants size %u, type %d, expire %llu\n",
207 i, 197 i,
208 (unsigned int) get_size (i), 198 (unsigned int) get_size (i),
209 get_type (i), 199 get_type (i), (unsigned long long) get_expiration (i).abs_value);
210 (unsigned long long) get_expiration(i).abs_value);
211#endif 200#endif
212 GNUNET_assert (size == get_size (i)); 201 GNUNET_assert (size == get_size (i));
213 GNUNET_assert (0 == memcmp (data, get_data(i), size)); 202 GNUNET_assert (0 == memcmp (data, get_data (i), size));
214 GNUNET_assert (type == get_type (i)); 203 GNUNET_assert (type == get_type (i));
215 GNUNET_assert (priority == get_priority (i)); 204 GNUNET_assert (priority == get_priority (i));
216 GNUNET_assert (anonymity == get_anonymity(i)); 205 GNUNET_assert (anonymity == get_anonymity (i));
217 GNUNET_assert (expiration.abs_value == get_expiration(i).abs_value); 206 GNUNET_assert (expiration.abs_value == get_expiration (i).abs_value);
218 crc->offset++; 207 crc->offset++;
219 if (crc->i == 0) 208 if (crc->i == 0)
220 { 209 {
221 crc->phase = RP_DEL; 210 crc->phase = RP_DEL;
222 crc->i = ITERATIONS; 211 crc->i = ITERATIONS;
223 } 212 }
224 GNUNET_SCHEDULER_add_continuation (&run_continuation, 213 GNUNET_SCHEDULER_add_continuation (&run_continuation,
225 crc, 214 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
226 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
227} 215}
228 216
229 217
230static void 218static void
231delete_value (void *cls, 219delete_value (void *cls,
232 const GNUNET_HashCode *key, 220 const GNUNET_HashCode * key,
233 size_t size, 221 size_t size,
234 const void *data, 222 const void *data,
235 enum GNUNET_BLOCK_Type type, 223 enum GNUNET_BLOCK_Type type,
236 uint32_t priority, 224 uint32_t priority,
237 uint32_t anonymity, 225 uint32_t anonymity,
238 struct GNUNET_TIME_Absolute 226 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
239 expiration, uint64_t uid)
240{ 227{
241 struct CpsRunContext *crc = cls; 228 struct CpsRunContext *crc = cls;
242 229
@@ -248,21 +235,19 @@ delete_value (void *cls,
248 memcpy (crc->data, data, size); 235 memcpy (crc->data, data, size);
249 crc->phase = RP_DO_DEL; 236 crc->phase = RP_DO_DEL;
250 GNUNET_SCHEDULER_add_continuation (&run_continuation, 237 GNUNET_SCHEDULER_add_continuation (&run_continuation,
251 crc, 238 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
252 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
253} 239}
254 240
255 241
256static void 242static void
257check_nothing (void *cls, 243check_nothing (void *cls,
258 const GNUNET_HashCode *key, 244 const GNUNET_HashCode * key,
259 size_t size, 245 size_t size,
260 const void *data, 246 const void *data,
261 enum GNUNET_BLOCK_Type type, 247 enum GNUNET_BLOCK_Type type,
262 uint32_t priority, 248 uint32_t priority,
263 uint32_t anonymity, 249 uint32_t anonymity,
264 struct GNUNET_TIME_Absolute 250 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
265 expiration, uint64_t uid)
266{ 251{
267 struct CpsRunContext *crc = cls; 252 struct CpsRunContext *crc = cls;
268 253
@@ -270,352 +255,310 @@ check_nothing (void *cls,
270 if (crc->i == 0) 255 if (crc->i == 0)
271 crc->phase = RP_RESERVE; 256 crc->phase = RP_RESERVE;
272 GNUNET_SCHEDULER_add_continuation (&run_continuation, 257 GNUNET_SCHEDULER_add_continuation (&run_continuation,
273 crc, 258 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
274 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
275} 259}
276 260
277 261
278static void 262static void
279check_multiple (void *cls, 263check_multiple (void *cls,
280 const GNUNET_HashCode * key, 264 const GNUNET_HashCode * key,
281 size_t size, 265 size_t size,
282 const void *data, 266 const void *data,
283 enum GNUNET_BLOCK_Type type, 267 enum GNUNET_BLOCK_Type type,
284 uint32_t priority, 268 uint32_t priority,
285 uint32_t anonymity, 269 uint32_t anonymity,
286 struct GNUNET_TIME_Absolute 270 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
287 expiration, uint64_t uid)
288{ 271{
289 struct CpsRunContext *crc = cls; 272 struct CpsRunContext *crc = cls;
290 273
291 GNUNET_assert (key != NULL); 274 GNUNET_assert (key != NULL);
292 switch (crc->phase) 275 switch (crc->phase)
293 { 276 {
294 case RP_GET_MULTIPLE: 277 case RP_GET_MULTIPLE:
295 crc->phase = RP_GET_MULTIPLE_NEXT; 278 crc->phase = RP_GET_MULTIPLE_NEXT;
296 crc->first_uid = uid; 279 crc->first_uid = uid;
297 crc->offset++; 280 crc->offset++;
298 break; 281 break;
299 case RP_GET_MULTIPLE_NEXT: 282 case RP_GET_MULTIPLE_NEXT:
300 GNUNET_assert (uid != crc->first_uid); 283 GNUNET_assert (uid != crc->first_uid);
301 crc->phase = RP_UPDATE; 284 crc->phase = RP_UPDATE;
302 break; 285 break;
303 default: 286 default:
304 GNUNET_break (0); 287 GNUNET_break (0);
305 crc->phase = RP_ERROR; 288 crc->phase = RP_ERROR;
306 break; 289 break;
307 } 290 }
308 if (priority == get_priority (42)) 291 if (priority == get_priority (42))
309 crc->uid = uid; 292 crc->uid = uid;
310 GNUNET_SCHEDULER_add_continuation (&run_continuation, 293 GNUNET_SCHEDULER_add_continuation (&run_continuation,
311 crc, 294 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
312 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
313} 295}
314 296
315 297
316static void 298static void
317check_update (void *cls, 299check_update (void *cls,
318 const GNUNET_HashCode * key, 300 const GNUNET_HashCode * key,
319 size_t size, 301 size_t size,
320 const void *data, 302 const void *data,
321 enum GNUNET_BLOCK_Type type, 303 enum GNUNET_BLOCK_Type type,
322 uint32_t priority, 304 uint32_t priority,
323 uint32_t anonymity, 305 uint32_t anonymity,
324 struct GNUNET_TIME_Absolute 306 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
325 expiration, uint64_t uid)
326{ 307{
327 struct CpsRunContext *crc = cls; 308 struct CpsRunContext *crc = cls;
328 309
329 GNUNET_assert (key != NULL); 310 GNUNET_assert (key != NULL);
330 if ( (anonymity == get_anonymity (42)) && 311 if ((anonymity == get_anonymity (42)) &&
331 (size == get_size (42)) && 312 (size == get_size (42)) && (priority == get_priority (42) + 100))
332 (priority == get_priority (42) + 100) ) 313 crc->phase = RP_DONE;
333 crc->phase = RP_DONE;
334 else 314 else
335 { 315 {
336 GNUNET_assert (size == get_size (43)); 316 GNUNET_assert (size == get_size (43));
337 crc->offset++; 317 crc->offset++;
338 } 318 }
339 GNUNET_SCHEDULER_add_continuation (&run_continuation, 319 GNUNET_SCHEDULER_add_continuation (&run_continuation,
340 crc, 320 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
341 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
342} 321}
343 322
344 323
345static void 324static void
346run_continuation (void *cls, 325run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
347 const struct GNUNET_SCHEDULER_TaskContext *tc)
348{ 326{
349 struct CpsRunContext *crc = cls; 327 struct CpsRunContext *crc = cls;
328
350 ok = (int) crc->phase; 329 ok = (int) crc->phase;
351#if VERBOSE 330#if VERBOSE
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test in phase %u\n", crc->phase);
353 "Test in phase %u\n", crc->phase);
354#endif 332#endif
355 switch (crc->phase) 333 switch (crc->phase)
356 { 334 {
357 case RP_PUT: 335 case RP_PUT:
358#if VERBOSE 336#if VERBOSE
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360 "Executing `%s' number %u\n", 338 "Executing `%s' number %u\n", "PUT", crc->i);
361 "PUT",
362 crc->i);
363#endif 339#endif
364 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key); 340 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
365 GNUNET_DATASTORE_put (datastore, 341 GNUNET_DATASTORE_put (datastore,
366 0, 342 0,
367 &crc->key, 343 &crc->key,
368 get_size (crc->i), 344 get_size (crc->i),
369 get_data (crc->i), 345 get_data (crc->i),
370 get_type (crc->i), 346 get_type (crc->i),
371 get_priority (crc->i), 347 get_priority (crc->i),
372 get_anonymity (crc->i), 348 get_anonymity (crc->i),
373 0, 349 0,
374 get_expiration (crc->i), 350 get_expiration (crc->i),
375 1, 1, TIMEOUT, 351 1, 1, TIMEOUT, &check_success, crc);
376 &check_success, 352 crc->i++;
377 crc); 353 if (crc->i == ITERATIONS)
378 crc->i++; 354 crc->phase = RP_GET;
379 if (crc->i == ITERATIONS) 355 break;
380 crc->phase = RP_GET; 356 case RP_GET:
381 break; 357 crc->i--;
382 case RP_GET:
383 crc->i--;
384#if VERBOSE 358#if VERBOSE
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386 "Executing `%s' number %u\n", 360 "Executing `%s' number %u\n", "GET", crc->i);
387 "GET",
388 crc->i);
389#endif 361#endif
390 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key); 362 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
391 GNUNET_DATASTORE_get_key (datastore, 363 GNUNET_DATASTORE_get_key (datastore,
392 crc->offset, 364 crc->offset,
393 &crc->key, 365 &crc->key,
394 get_type (crc->i), 366 get_type (crc->i),
395 1, 1, TIMEOUT, 367 1, 1, TIMEOUT, &check_value, crc);
396 &check_value, 368 break;
397 crc); 369 case RP_DEL:
398 break; 370 crc->i--;
399 case RP_DEL:
400 crc->i--;
401#if VERBOSE 371#if VERBOSE
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403 "Executing `%s' number %u\n", 373 "Executing `%s' number %u\n", "DEL", crc->i);
404 "DEL",
405 crc->i);
406#endif 374#endif
407 crc->data = NULL; 375 crc->data = NULL;
408 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key); 376 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
409 GNUNET_assert (NULL != 377 GNUNET_assert (NULL !=
410 GNUNET_DATASTORE_get_key (datastore, 378 GNUNET_DATASTORE_get_key (datastore,
411 crc->offset, 379 crc->offset,
412 &crc->key, 380 &crc->key,
413 get_type (crc->i), 381 get_type (crc->i),
414 1, 1, TIMEOUT, 382 1, 1, TIMEOUT,
415 &delete_value, 383 &delete_value, crc));
416 crc)); 384 break;
417 break; 385 case RP_DO_DEL:
418 case RP_DO_DEL:
419#if VERBOSE 386#if VERBOSE
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "Executing `%s' number %u\n", 388 "Executing `%s' number %u\n", "DO_DEL", crc->i);
422 "DO_DEL",
423 crc->i);
424#endif 389#endif
425 if (crc->i == 0) 390 if (crc->i == 0)
426 { 391 {
427 crc->i = ITERATIONS; 392 crc->i = ITERATIONS;
428 crc->phase = RP_DELVALIDATE; 393 crc->phase = RP_DELVALIDATE;
429 } 394 }
430 else 395 else
431 { 396 {
432 crc->phase = RP_DEL; 397 crc->phase = RP_DEL;
433 } 398 }
434 GNUNET_assert (NULL != 399 GNUNET_assert (NULL !=
435 GNUNET_DATASTORE_remove (datastore, 400 GNUNET_DATASTORE_remove (datastore,
436 &crc->key, 401 &crc->key,
437 crc->size, 402 crc->size,
438 crc->data, 403 crc->data,
439 1, 1, TIMEOUT, 404 1, 1, TIMEOUT,
440 &check_success, 405 &check_success, crc));
441 crc)); 406 break;
442 break; 407 case RP_DELVALIDATE:
443 case RP_DELVALIDATE: 408 crc->i--;
444 crc->i--;
445#if VERBOSE 409#if VERBOSE
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "Executing `%s' number %u\n", 411 "Executing `%s' number %u\n", "DEL-VALIDATE", crc->i);
448 "DEL-VALIDATE",
449 crc->i);
450#endif 412#endif
451 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key); 413 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
452 GNUNET_assert (NULL != 414 GNUNET_assert (NULL !=
453 GNUNET_DATASTORE_get_key (datastore, 415 GNUNET_DATASTORE_get_key (datastore,
454 crc->offset, 416 crc->offset,
455 &crc->key, 417 &crc->key,
456 get_type (crc->i), 418 get_type (crc->i),
457 1, 1, TIMEOUT, 419 1, 1, TIMEOUT,
458 &check_nothing, 420 &check_nothing, crc));
459 crc)); 421 break;
460 break; 422 case RP_RESERVE:
461 case RP_RESERVE: 423 crc->phase = RP_PUT_MULTIPLE;
462 crc->phase = RP_PUT_MULTIPLE; 424 GNUNET_DATASTORE_reserve (datastore,
463 GNUNET_DATASTORE_reserve (datastore, 425 128 * 1024, 2, 1, 1, TIMEOUT, &get_reserved, crc);
464 128*1024, 426 break;
465 2, 427 case RP_PUT_MULTIPLE:
466 1, 1, TIMEOUT, 428 crc->phase = RP_PUT_MULTIPLE_NEXT;
467 &get_reserved, 429 GNUNET_DATASTORE_put (datastore,
468 crc); 430 crc->rid,
469 break; 431 &crc->key,
470 case RP_PUT_MULTIPLE: 432 get_size (42),
471 crc->phase = RP_PUT_MULTIPLE_NEXT; 433 get_data (42),
472 GNUNET_DATASTORE_put (datastore, 434 get_type (42),
473 crc->rid, 435 get_priority (42),
474 &crc->key, 436 get_anonymity (42),
475 get_size (42), 437 0,
476 get_data (42), 438 get_expiration (42),
477 get_type (42), 439 1, 1, TIMEOUT, &check_success, crc);
478 get_priority (42), 440 break;
479 get_anonymity (42), 441 case RP_PUT_MULTIPLE_NEXT:
480 0, 442 crc->phase = RP_GET_MULTIPLE;
481 get_expiration (42), 443 GNUNET_DATASTORE_put (datastore,
482 1, 1, TIMEOUT, 444 crc->rid,
483 &check_success, 445 &crc->key,
484 crc); 446 get_size (43),
485 break; 447 get_data (43),
486 case RP_PUT_MULTIPLE_NEXT: 448 get_type (42),
487 crc->phase = RP_GET_MULTIPLE; 449 get_priority (43),
488 GNUNET_DATASTORE_put (datastore, 450 get_anonymity (43),
489 crc->rid, 451 0,
490 &crc->key, 452 get_expiration (43),
491 get_size (43), 453 1, 1, TIMEOUT, &check_success, crc);
492 get_data (43), 454 break;
493 get_type (42), 455 case RP_GET_MULTIPLE:
494 get_priority (43), 456 GNUNET_assert (NULL !=
495 get_anonymity (43), 457 GNUNET_DATASTORE_get_key (datastore,
496 0, 458 crc->offset,
497 get_expiration (43), 459 &crc->key,
498 1, 1, TIMEOUT, 460 get_type (42),
499 &check_success, 461 1, 1, TIMEOUT,
500 crc); 462 &check_multiple, crc));
501 break; 463 break;
502 case RP_GET_MULTIPLE: 464 case RP_GET_MULTIPLE_NEXT:
503 GNUNET_assert (NULL != 465 GNUNET_assert (NULL !=
504 GNUNET_DATASTORE_get_key (datastore, 466 GNUNET_DATASTORE_get_key (datastore,
505 crc->offset, 467 crc->offset,
506 &crc->key, 468 &crc->key,
507 get_type (42), 469 get_type (42),
508 1, 1, TIMEOUT, 470 1, 1, TIMEOUT,
509 &check_multiple, 471 &check_multiple, crc));
510 crc)); 472 break;
511 break; 473 case RP_UPDATE:
512 case RP_GET_MULTIPLE_NEXT: 474 GNUNET_assert (crc->uid > 0);
513 GNUNET_assert (NULL != 475 crc->phase = RP_UPDATE_VALIDATE;
514 GNUNET_DATASTORE_get_key (datastore, 476 GNUNET_DATASTORE_update (datastore,
515 crc->offset, 477 crc->uid,
516 &crc->key, 478 100,
517 get_type (42), 479 get_expiration (42),
518 1, 1, TIMEOUT, 480 1, 1, TIMEOUT, &check_success, crc);
519 &check_multiple, 481 break;
520 crc)); 482 case RP_UPDATE_VALIDATE:
521 break; 483 GNUNET_assert (NULL !=
522 case RP_UPDATE: 484 GNUNET_DATASTORE_get_key (datastore,
523 GNUNET_assert (crc->uid > 0); 485 crc->offset,
524 crc->phase = RP_UPDATE_VALIDATE; 486 &crc->key,
525 GNUNET_DATASTORE_update (datastore, 487 get_type (42),
526 crc->uid, 488 1, 1, TIMEOUT,
527 100, 489 &check_update, crc));
528 get_expiration (42), 490 break;
529 1, 1, TIMEOUT, 491 case RP_DONE:
530 &check_success,
531 crc);
532 break;
533 case RP_UPDATE_VALIDATE:
534 GNUNET_assert (NULL !=
535 GNUNET_DATASTORE_get_key (datastore,
536 crc->offset,
537 &crc->key,
538 get_type (42),
539 1, 1, TIMEOUT,
540 &check_update,
541 crc));
542 break;
543 case RP_DONE:
544#if VERBOSE 492#if VERBOSE
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n");
546 "Finished, disconnecting\n");
547#endif 494#endif
548 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES); 495 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
549 GNUNET_free (crc); 496 GNUNET_free (crc);
550 ok = 0; 497 ok = 0;
551 break; 498 break;
552 case RP_ERROR: 499 case RP_ERROR:
553 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES); 500 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
554 GNUNET_free (crc); 501 GNUNET_free (crc);
555 ok = 43; 502 ok = 43;
556 break; 503 break;
557 } 504 }
558} 505}
559 506
560 507
561static void 508static void
562run_tests (void *cls, 509run_tests (void *cls, int32_t success, const char *msg)
563 int32_t success,
564 const char *msg)
565{ 510{
566 struct CpsRunContext *crc = cls; 511 struct CpsRunContext *crc = cls;
567 512
568 switch (success) 513 switch (success)
569 { 514 {
570 case GNUNET_YES: 515 case GNUNET_YES:
571 GNUNET_SCHEDULER_add_continuation (&run_continuation, 516 GNUNET_SCHEDULER_add_continuation (&run_continuation,
572 crc, 517 crc,
573 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 518 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
574 return; 519 return;
575 case GNUNET_NO: 520 case GNUNET_NO:
576 fprintf (stderr, 521 fprintf (stderr, "Test 'put' operation failed, key already exists (!?)\n");
577 "Test 'put' operation failed, key already exists (!?)\n"); 522 GNUNET_free (crc);
578 GNUNET_free (crc); 523 return;
579 return; 524 case GNUNET_SYSERR:
580 case GNUNET_SYSERR: 525 fprintf (stderr,
581 fprintf (stderr, 526 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
582 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n", 527 msg);
583 msg); 528 GNUNET_free (crc);
584 GNUNET_free (crc); 529 return;
585 return; 530 default:
586 default: 531 GNUNET_assert (0);
587 GNUNET_assert (0); 532 }
588 }
589} 533}
590 534
591 535
592static void 536static void
593run (void *cls, 537run (void *cls,
594 char *const *args, 538 char *const *args,
595 const char *cfgfile, 539 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
596 const struct GNUNET_CONFIGURATION_Handle *cfg)
597{ 540{
598 struct CpsRunContext *crc; 541 struct CpsRunContext *crc;
599 static GNUNET_HashCode zkey; 542 static GNUNET_HashCode zkey;
600 543
601 crc = GNUNET_malloc(sizeof(struct CpsRunContext)); 544 crc = GNUNET_malloc (sizeof (struct CpsRunContext));
602 crc->cfg = cfg; 545 crc->cfg = cfg;
603 crc->phase = RP_PUT; 546 crc->phase = RP_PUT;
604 now = GNUNET_TIME_absolute_get (); 547 now = GNUNET_TIME_absolute_get ();
605 datastore = GNUNET_DATASTORE_connect (cfg); 548 datastore = GNUNET_DATASTORE_connect (cfg);
606 if (NULL == 549 if (NULL ==
607 GNUNET_DATASTORE_put (datastore, 0, 550 GNUNET_DATASTORE_put (datastore, 0,
608 &zkey, 4, "TEST", 551 &zkey, 4, "TEST",
609 GNUNET_BLOCK_TYPE_TEST, 552 GNUNET_BLOCK_TYPE_TEST,
610 0, 0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS), 553 0, 0, 0,
611 0, 1, GNUNET_TIME_UNIT_MINUTES, 554 GNUNET_TIME_relative_to_absolute
612 &run_tests, crc)) 555 (GNUNET_TIME_UNIT_SECONDS), 0, 1,
613 { 556 GNUNET_TIME_UNIT_MINUTES, &run_tests, crc))
614 fprintf (stderr, 557 {
615 "Test 'put' operation failed.\n"); 558 fprintf (stderr, "Test 'put' operation failed.\n");
616 ok = 1; 559 ok = 1;
617 GNUNET_free (crc); 560 GNUNET_free (crc);
618 } 561 }
619} 562}
620 563
621 564
@@ -623,6 +566,7 @@ static int
623check () 566check ()
624{ 567{
625 char cfg_name[128]; 568 char cfg_name[128];
569
626#if START_DATASTORE 570#if START_DATASTORE
627 struct GNUNET_OS_Process *proc; 571 struct GNUNET_OS_Process *proc;
628#endif 572#endif
@@ -639,28 +583,27 @@ check ()
639 GNUNET_GETOPT_OPTION_END 583 GNUNET_GETOPT_OPTION_END
640 }; 584 };
641 GNUNET_snprintf (cfg_name, 585 GNUNET_snprintf (cfg_name,
642 sizeof (cfg_name), 586 sizeof (cfg_name),
643 "test_datastore_api_data_%s.conf", 587 "test_datastore_api_data_%s.conf", plugin_name);
644 plugin_name);
645#if START_DATASTORE 588#if START_DATASTORE
646 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm", 589 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
647 "gnunet-service-arm", 590 "gnunet-service-arm",
648#if VERBOSE 591#if VERBOSE
649 "-L", "DEBUG", 592 "-L", "DEBUG",
650#endif 593#endif
651 "-c", cfg_name, NULL); 594 "-c", cfg_name, NULL);
652#endif 595#endif
653 GNUNET_assert (NULL != proc); 596 GNUNET_assert (NULL != proc);
654 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, 597 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
655 argv, "test-datastore-api", "nohelp", 598 argv, "test-datastore-api", "nohelp",
656 options, &run, NULL); 599 options, &run, NULL);
657#if START_DATASTORE 600#if START_DATASTORE
658 sleep (1); /* give datastore chance to receive 'DROP' request */ 601 sleep (1); /* give datastore chance to receive 'DROP' request */
659 if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) 602 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
660 { 603 {
661 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 604 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
662 ok = 1; 605 ok = 1;
663 } 606 }
664 GNUNET_OS_process_wait (proc); 607 GNUNET_OS_process_wait (proc);
665 GNUNET_OS_process_close (proc); 608 GNUNET_OS_process_close (proc);
666 proc = NULL; 609 proc = NULL;
@@ -680,17 +623,16 @@ main (int argc, char *argv[])
680 sleep (1); 623 sleep (1);
681 /* determine name of plugin to use */ 624 /* determine name of plugin to use */
682 plugin_name = argv[0]; 625 plugin_name = argv[0];
683 while (NULL != (pos = strstr(plugin_name, "_"))) 626 while (NULL != (pos = strstr (plugin_name, "_")))
684 plugin_name = pos+1; 627 plugin_name = pos + 1;
685 if (NULL != (pos = strstr(plugin_name, "."))) 628 if (NULL != (pos = strstr (plugin_name, ".")))
686 pos[0] = 0; 629 pos[0] = 0;
687 else 630 else
688 pos = (char *) plugin_name; 631 pos = (char *) plugin_name;
689 632
690 GNUNET_snprintf (dir_name, 633 GNUNET_snprintf (dir_name,
691 sizeof (dir_name), 634 sizeof (dir_name),
692 "/tmp/test-gnunet-datastore-%s", 635 "/tmp/test-gnunet-datastore-%s", plugin_name);
693 plugin_name);
694 GNUNET_DISK_directory_remove (dir_name); 636 GNUNET_DISK_directory_remove (dir_name);
695 GNUNET_log_setup ("test-datastore-api", 637 GNUNET_log_setup ("test-datastore-api",
696#if VERBOSE 638#if VERBOSE
diff --git a/src/datastore/test_datastore_api_management.c b/src/datastore/test_datastore_api_management.c
index ca1df2cf9..b2146a915 100644
--- a/src/datastore/test_datastore_api_management.c
+++ b/src/datastore/test_datastore_api_management.c
@@ -47,7 +47,7 @@ static struct GNUNET_TIME_Absolute now;
47 47
48static int ok; 48static int ok;
49 49
50static const char* plugin_name; 50static const char *plugin_name;
51 51
52static size_t 52static size_t
53get_size (int i) 53get_size (int i)
@@ -59,34 +59,35 @@ get_size (int i)
59static const void * 59static const void *
60get_data (int i) 60get_data (int i)
61{ 61{
62 static char buf[60000]; 62 static char buf[60000];
63
63 memset (buf, i, 8 + 8 * (i % 256)); 64 memset (buf, i, 8 + 8 * (i % 256));
64 return buf; 65 return buf;
65} 66}
66 67
67 68
68static int 69static int
69get_type(int i) 70get_type (int i)
70{ 71{
71 return 1; 72 return 1;
72} 73}
73 74
74 75
75static int 76static int
76get_priority (int i) 77get_priority (int i)
77{ 78{
78 return i+1; 79 return i + 1;
79} 80}
80 81
81 82
82static int 83static int
83get_anonymity(int i) 84get_anonymity (int i)
84{ 85{
85 return i; 86 return i;
86} 87}
87 88
88 89
89static struct GNUNET_TIME_Absolute 90static struct GNUNET_TIME_Absolute
90get_expiration (int i) 91get_expiration (int i)
91{ 92{
92 struct GNUNET_TIME_Absolute av; 93 struct GNUNET_TIME_Absolute av;
@@ -96,12 +97,12 @@ get_expiration (int i)
96} 97}
97 98
98enum RunPhase 99enum RunPhase
99 { 100{
100 RP_PUT, 101 RP_PUT,
101 RP_GET, 102 RP_GET,
102 RP_DONE, 103 RP_DONE,
103 RP_GET_FAIL 104 RP_GET_FAIL
104 }; 105};
105 106
106 107
107struct CpsRunContext 108struct CpsRunContext
@@ -117,77 +118,70 @@ struct CpsRunContext
117 118
118 119
119static void 120static void
120run_continuation (void *cls, 121run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
121 const struct GNUNET_SCHEDULER_TaskContext *tc);
122 122
123 123
124static void 124static void
125check_success (void *cls, 125check_success (void *cls, int success, const char *msg)
126 int success,
127 const char *msg)
128{ 126{
129 struct CpsRunContext *crc = cls; 127 struct CpsRunContext *crc = cls;
128
130 if (GNUNET_OK != success) 129 if (GNUNET_OK != success)
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", msg);
132 "%s\n", msg);
133 GNUNET_assert (GNUNET_OK == success); 131 GNUNET_assert (GNUNET_OK == success);
134 GNUNET_free_non_null (crc->data); 132 GNUNET_free_non_null (crc->data);
135 crc->data = NULL; 133 crc->data = NULL;
136 GNUNET_SCHEDULER_add_continuation (&run_continuation, 134 GNUNET_SCHEDULER_add_continuation (&run_continuation,
137 crc, 135 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
138 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
139} 136}
140 137
141 138
142static void 139static void
143check_value (void *cls, 140check_value (void *cls,
144 const GNUNET_HashCode * key, 141 const GNUNET_HashCode * key,
145 size_t size, 142 size_t size,
146 const void *data, 143 const void *data,
147 enum GNUNET_BLOCK_Type type, 144 enum GNUNET_BLOCK_Type type,
148 uint32_t priority, 145 uint32_t priority,
149 uint32_t anonymity, 146 uint32_t anonymity,
150 struct GNUNET_TIME_Absolute expiration, 147 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
151 uint64_t uid)
152{ 148{
153 struct CpsRunContext *crc = cls; 149 struct CpsRunContext *crc = cls;
154 int i; 150 int i;
155 151
156 if (NULL == key) 152 if (NULL == key)
157 { 153 {
158 crc->phase = RP_GET_FAIL; 154 crc->phase = RP_GET_FAIL;
159 GNUNET_SCHEDULER_add_continuation (&run_continuation, 155 GNUNET_SCHEDULER_add_continuation (&run_continuation,
160 crc, 156 crc,
161 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 157 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
162 return; 158 return;
163 } 159 }
164 i = crc->i; 160 i = crc->i;
165 GNUNET_assert (size == get_size (i)); 161 GNUNET_assert (size == get_size (i));
166 GNUNET_assert (0 == memcmp (data, get_data(i), size)); 162 GNUNET_assert (0 == memcmp (data, get_data (i), size));
167 GNUNET_assert (type == get_type (i)); 163 GNUNET_assert (type == get_type (i));
168 GNUNET_assert (priority == get_priority (i)); 164 GNUNET_assert (priority == get_priority (i));
169 GNUNET_assert (anonymity == get_anonymity(i)); 165 GNUNET_assert (anonymity == get_anonymity (i));
170 GNUNET_assert (expiration.abs_value == get_expiration(i).abs_value); 166 GNUNET_assert (expiration.abs_value == get_expiration (i).abs_value);
171 crc->offset++; 167 crc->offset++;
172 crc->i--; 168 crc->i--;
173 if (crc->i == 0) 169 if (crc->i == 0)
174 crc->phase = RP_DONE; 170 crc->phase = RP_DONE;
175 GNUNET_SCHEDULER_add_continuation (&run_continuation, 171 GNUNET_SCHEDULER_add_continuation (&run_continuation,
176 crc, 172 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
177 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
178} 173}
179 174
180 175
181static void 176static void
182check_nothing (void *cls, 177check_nothing (void *cls,
183 const GNUNET_HashCode * key, 178 const GNUNET_HashCode * key,
184 size_t size, 179 size_t size,
185 const void *data, 180 const void *data,
186 enum GNUNET_BLOCK_Type type, 181 enum GNUNET_BLOCK_Type type,
187 uint32_t priority, 182 uint32_t priority,
188 uint32_t anonymity, 183 uint32_t anonymity,
189 struct GNUNET_TIME_Absolute 184 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
190 expiration, uint64_t uid)
191{ 185{
192 struct CpsRunContext *crc = cls; 186 struct CpsRunContext *crc = cls;
193 187
@@ -195,143 +189,125 @@ check_nothing (void *cls,
195 if (0 == --crc->i) 189 if (0 == --crc->i)
196 crc->phase = RP_DONE; 190 crc->phase = RP_DONE;
197 GNUNET_SCHEDULER_add_continuation (&run_continuation, 191 GNUNET_SCHEDULER_add_continuation (&run_continuation,
198 crc, 192 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
199 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
200} 193}
201 194
202 195
203static void 196static void
204run_continuation (void *cls, 197run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
205 const struct GNUNET_SCHEDULER_TaskContext *tc)
206{ 198{
207 struct CpsRunContext *crc = cls; 199 struct CpsRunContext *crc = cls;
200
208 ok = (int) crc->phase; 201 ok = (int) crc->phase;
209 switch (crc->phase) 202 switch (crc->phase)
210 { 203 {
211 case RP_PUT: 204 case RP_PUT:
212#if VERBOSE 205#if VERBOSE
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
214 "Executing `%s' number %u\n", 207 "Executing `%s' number %u\n", "PUT", crc->i);
215 "PUT",
216 crc->i);
217#endif 208#endif
218 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key); 209 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
219 GNUNET_DATASTORE_put (datastore, 210 GNUNET_DATASTORE_put (datastore,
220 0, 211 0,
221 &crc->key, 212 &crc->key,
222 get_size (crc->i), 213 get_size (crc->i),
223 get_data (crc->i), 214 get_data (crc->i),
224 get_type (crc->i), 215 get_type (crc->i),
225 get_priority (crc->i), 216 get_priority (crc->i),
226 get_anonymity (crc->i), 217 get_anonymity (crc->i),
227 0, 218 0,
228 get_expiration (crc->i), 219 get_expiration (crc->i),
229 1, 1, TIMEOUT, 220 1, 1, TIMEOUT, &check_success, crc);
230 &check_success, 221 crc->i++;
231 crc); 222 if (crc->i == ITERATIONS)
232 crc->i++; 223 {
233 if (crc->i == ITERATIONS) 224 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
234 { 225 "Sleeping to give datastore time to clean up\n");
235 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 226 sleep (1);
236 "Sleeping to give datastore time to clean up\n"); 227 crc->phase = RP_GET;
237 sleep (1); 228 crc->i--;
238 crc->phase = RP_GET; 229 }
239 crc->i--; 230 break;
240 } 231 case RP_GET:
241 break;
242 case RP_GET:
243#if VERBOSE 232#if VERBOSE
244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
245 "Executing `%s' number %u\n", 234 "Executing `%s' number %u\n", "GET", crc->i);
246 "GET",
247 crc->i);
248#endif 235#endif
249 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key); 236 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
250 GNUNET_DATASTORE_get_key (datastore, 237 GNUNET_DATASTORE_get_key (datastore,
251 crc->offset++, 238 crc->offset++,
252 &crc->key, 239 &crc->key,
253 get_type (crc->i), 240 get_type (crc->i),
254 1, 1, TIMEOUT, 241 1, 1, TIMEOUT, &check_value, crc);
255 &check_value, 242 break;
256 crc); 243 case RP_GET_FAIL:
257 break;
258 case RP_GET_FAIL:
259#if VERBOSE 244#if VERBOSE
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Executing `%s' number %u\n", 246 "Executing `%s' number %u\n", "GET(f)", crc->i);
262 "GET(f)",
263 crc->i);
264#endif 247#endif
265 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key); 248 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
266 GNUNET_DATASTORE_get_key (datastore, 249 GNUNET_DATASTORE_get_key (datastore,
267 crc->offset++, 250 crc->offset++,
268 &crc->key, 251 &crc->key,
269 get_type (crc->i), 252 get_type (crc->i),
270 1, 1, TIMEOUT, 253 1, 1, TIMEOUT, &check_nothing, crc);
271 &check_nothing, 254 break;
272 crc); 255 case RP_DONE:
273 break; 256 GNUNET_assert (0 == crc->i);
274 case RP_DONE:
275 GNUNET_assert (0 == crc->i);
276#if VERBOSE 257#if VERBOSE
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n");
278 "Finished, disconnecting\n");
279#endif 259#endif
280 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES); 260 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
281 GNUNET_free (crc); 261 GNUNET_free (crc);
282 ok = 0; 262 ok = 0;
283 } 263 }
284} 264}
285 265
286 266
287static void 267static void
288run_tests (void *cls, 268run_tests (void *cls, int success, const char *msg)
289 int success,
290 const char *msg)
291{ 269{
292 struct CpsRunContext *crc = cls; 270 struct CpsRunContext *crc = cls;
293 271
294 if (success != GNUNET_YES) 272 if (success != GNUNET_YES)
295 { 273 {
296 fprintf (stderr, 274 fprintf (stderr,
297 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.", 275 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.",
298 msg); 276 msg);
299 GNUNET_free (crc); 277 GNUNET_free (crc);
300 return; 278 return;
301 } 279 }
302 GNUNET_SCHEDULER_add_continuation (&run_continuation, 280 GNUNET_SCHEDULER_add_continuation (&run_continuation,
303 crc, 281 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
304 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
305} 282}
306 283
307 284
308static void 285static void
309run (void *cls, 286run (void *cls,
310 char *const *args, 287 char *const *args,
311 const char *cfgfile, 288 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
312 const struct GNUNET_CONFIGURATION_Handle *cfg)
313{ 289{
314 struct CpsRunContext *crc; 290 struct CpsRunContext *crc;
315 static GNUNET_HashCode zkey; 291 static GNUNET_HashCode zkey;
316 292
317 crc = GNUNET_malloc(sizeof(struct CpsRunContext)); 293 crc = GNUNET_malloc (sizeof (struct CpsRunContext));
318 crc->cfg = cfg; 294 crc->cfg = cfg;
319 crc->phase = RP_PUT; 295 crc->phase = RP_PUT;
320 now = GNUNET_TIME_absolute_get (); 296 now = GNUNET_TIME_absolute_get ();
321 datastore = GNUNET_DATASTORE_connect (cfg); 297 datastore = GNUNET_DATASTORE_connect (cfg);
322 if (NULL == 298 if (NULL ==
323 GNUNET_DATASTORE_put (datastore, 0, 299 GNUNET_DATASTORE_put (datastore, 0,
324 &zkey, 4, "TEST", 300 &zkey, 4, "TEST",
325 GNUNET_BLOCK_TYPE_TEST, 301 GNUNET_BLOCK_TYPE_TEST,
326 0, 0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS), 302 0, 0, 0,
327 0, 1, GNUNET_TIME_UNIT_MINUTES, 303 GNUNET_TIME_relative_to_absolute
328 &run_tests, crc)) 304 (GNUNET_TIME_UNIT_SECONDS), 0, 1,
329 { 305 GNUNET_TIME_UNIT_MINUTES, &run_tests, crc))
330 fprintf (stderr, 306 {
331 "Test 'put' operation failed.\n"); 307 fprintf (stderr, "Test 'put' operation failed.\n");
332 GNUNET_free (crc); 308 GNUNET_free (crc);
333 ok = 1; 309 ok = 1;
334 } 310 }
335} 311}
336 312
337 313
@@ -341,7 +317,8 @@ check ()
341{ 317{
342 struct GNUNET_OS_Process *proc; 318 struct GNUNET_OS_Process *proc;
343 char cfg_name[128]; 319 char cfg_name[128];
344 char *const argv[] = { 320
321 char *const argv[] = {
345 "test-datastore-api-management", 322 "test-datastore-api-management",
346 "-c", 323 "-c",
347 cfg_name, 324 cfg_name,
@@ -354,25 +331,24 @@ check ()
354 GNUNET_GETOPT_OPTION_END 331 GNUNET_GETOPT_OPTION_END
355 }; 332 };
356 GNUNET_snprintf (cfg_name, 333 GNUNET_snprintf (cfg_name,
357 sizeof (cfg_name), 334 sizeof (cfg_name),
358 "test_datastore_api_data_%s.conf", 335 "test_datastore_api_data_%s.conf", plugin_name);
359 plugin_name);
360 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm", 336 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
361 "gnunet-service-arm", 337 "gnunet-service-arm",
362#if VERBOSE 338#if VERBOSE
363 "-L", "DEBUG", 339 "-L", "DEBUG",
364#endif 340#endif
365 "-c", cfg_name, NULL); 341 "-c", cfg_name, NULL);
366 GNUNET_assert (NULL != proc); 342 GNUNET_assert (NULL != proc);
367 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, 343 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
368 argv, "test-datastore-api-management", "nohelp", 344 argv, "test-datastore-api-management", "nohelp",
369 options, &run, NULL); 345 options, &run, NULL);
370 sleep (1); /* give datastore chance to process 'DROP' request */ 346 sleep (1); /* give datastore chance to process 'DROP' request */
371 if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) 347 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
372 { 348 {
373 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 349 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
374 ok = 1; 350 ok = 1;
375 } 351 }
376 GNUNET_OS_process_wait (proc); 352 GNUNET_OS_process_wait (proc);
377 GNUNET_OS_process_close (proc); 353 GNUNET_OS_process_close (proc);
378 proc = NULL; 354 proc = NULL;
@@ -385,24 +361,23 @@ int
385main (int argc, char *argv[]) 361main (int argc, char *argv[])
386{ 362{
387 int ret; 363 int ret;
388 364
389 char *pos; 365 char *pos;
390 char dir_name[128]; 366 char dir_name[128];
391 367
392 sleep (1); 368 sleep (1);
393 /* determine name of plugin to use */ 369 /* determine name of plugin to use */
394 plugin_name = argv[0]; 370 plugin_name = argv[0];
395 while (NULL != (pos = strstr(plugin_name, "_"))) 371 while (NULL != (pos = strstr (plugin_name, "_")))
396 plugin_name = pos+1; 372 plugin_name = pos + 1;
397 if (NULL != (pos = strstr(plugin_name, "."))) 373 if (NULL != (pos = strstr (plugin_name, ".")))
398 pos[0] = 0; 374 pos[0] = 0;
399 else 375 else
400 pos = (char *) plugin_name; 376 pos = (char *) plugin_name;
401 377
402 GNUNET_snprintf (dir_name, 378 GNUNET_snprintf (dir_name,
403 sizeof (dir_name), 379 sizeof (dir_name),
404 "/tmp/test-gnunet-datastore-%s", 380 "/tmp/test-gnunet-datastore-%s", plugin_name);
405 plugin_name);
406 GNUNET_DISK_directory_remove (dir_name); 381 GNUNET_DISK_directory_remove (dir_name);
407 GNUNET_log_setup ("test-datastore-api-management", 382 GNUNET_log_setup ("test-datastore-api-management",
408#if VERBOSE 383#if VERBOSE
diff --git a/src/datastore/test_plugin_datastore.c b/src/datastore/test_plugin_datastore.c
index 6ac2d7a6e..15cca61f1 100644
--- a/src/datastore/test_plugin_datastore.c
+++ b/src/datastore/test_plugin_datastore.c
@@ -46,22 +46,22 @@ static const char *plugin_name;
46static int ok; 46static int ok;
47 47
48enum RunPhase 48enum RunPhase
49 { 49{
50 RP_ERROR = 0, 50 RP_ERROR = 0,
51 RP_PUT, 51 RP_PUT,
52 RP_GET, 52 RP_GET,
53 RP_UPDATE, 53 RP_UPDATE,
54 RP_ITER_ZERO, 54 RP_ITER_ZERO,
55 RP_REPL_GET, 55 RP_REPL_GET,
56 RP_EXPI_GET, 56 RP_EXPI_GET,
57 RP_DROP 57 RP_DROP
58 }; 58};
59 59
60 60
61struct CpsRunContext 61struct CpsRunContext
62{ 62{
63 const struct GNUNET_CONFIGURATION_Handle *cfg; 63 const struct GNUNET_CONFIGURATION_Handle *cfg;
64 struct GNUNET_DATASTORE_PluginFunctions * api; 64 struct GNUNET_DATASTORE_PluginFunctions *api;
65 enum RunPhase phase; 65 enum RunPhase phase;
66 unsigned int cnt; 66 unsigned int cnt;
67 unsigned int i; 67 unsigned int i;
@@ -78,27 +78,23 @@ struct CpsRunContext
78 * 0 for "reset to empty" 78 * 0 for "reset to empty"
79 */ 79 */
80static void 80static void
81disk_utilization_change_cb (void *cls, 81disk_utilization_change_cb (void *cls, int delta)
82 int delta)
83{ 82{
84 /* do nothing */ 83 /* do nothing */
85} 84}
86 85
87 86
88static void 87static void
89gen_key (int i, 88gen_key (int i, GNUNET_HashCode * key)
90 GNUNET_HashCode *key)
91{ 89{
92 memset (key, 0, sizeof (GNUNET_HashCode)); 90 memset (key, 0, sizeof (GNUNET_HashCode));
93 key->bits[0] = (unsigned int) i; 91 key->bits[0] = (unsigned int) i;
94 GNUNET_CRYPTO_hash (key, sizeof (GNUNET_HashCode), key); 92 GNUNET_CRYPTO_hash (key, sizeof (GNUNET_HashCode), key);
95} 93}
96 94
97 95
98static void 96static void
99put_value (struct GNUNET_DATASTORE_PluginFunctions * api, 97put_value (struct GNUNET_DATASTORE_PluginFunctions *api, int i, int k)
100 int i,
101 int k)
102{ 98{
103 char value[65536]; 99 char value[65536];
104 size_t size; 100 size_t size;
@@ -109,7 +105,7 @@ put_value (struct GNUNET_DATASTORE_PluginFunctions * api,
109 /* most content is 32k */ 105 /* most content is 32k */
110 size = 32 * 1024; 106 size = 32 * 1024;
111 107
112 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */ 108 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
113 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024); 109 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
114 size = size - (size & 7); /* always multiple of 8 */ 110 size = size - (size & 7); /* always multiple of 8 */
115 111
@@ -122,37 +118,31 @@ put_value (struct GNUNET_DATASTORE_PluginFunctions * api,
122 msg = NULL; 118 msg = NULL;
123 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100); 119 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
124#if VERBOSE 120#if VERBOSE
125 fprintf (stderr, 121 fprintf (stderr,
126 "putting type %u, anon %u under key %s\n", 122 "putting type %u, anon %u under key %s\n",
127 i+1, i, GNUNET_h2s (&key)); 123 i + 1, i, GNUNET_h2s (&key));
128#endif 124#endif
129 if (GNUNET_OK != api->put (api->cls, 125 if (GNUNET_OK != api->put (api->cls, &key, size, value, i + 1 /* type */ ,
130 &key, 126 prio, i /* anonymity */ ,
131 size, 127 0 /* replication */ ,
132 value, 128 GNUNET_TIME_relative_to_absolute
133 i + 1 /* type */, 129 (GNUNET_TIME_relative_multiply
134 prio, 130 (GNUNET_TIME_UNIT_MILLISECONDS,
135 i /* anonymity */, 131 60 * 60 * 60 * 1000 +
136 0 /* replication */, 132 GNUNET_CRYPTO_random_u32
137 GNUNET_TIME_relative_to_absolute 133 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))), &msg))
138 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 134 {
139 60 * 60 * 60 * 1000 + 135 fprintf (stderr, "ERROR: `%s'\n", msg);
140 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))), 136 GNUNET_free_non_null (msg);
141 &msg)) 137 return;
142 { 138 }
143 fprintf (stderr, "ERROR: `%s'\n", msg);
144 GNUNET_free_non_null (msg);
145 return;
146 }
147 stored_bytes += size; 139 stored_bytes += size;
148 stored_ops++; 140 stored_ops++;
149 stored_entries++; 141 stored_entries++;
150} 142}
151 143
152 144
153static void 145static void test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
154test (void *cls,
155 const struct GNUNET_SCHEDULER_TaskContext *tc);
156 146
157 147
158static uint64_t guid; 148static uint64_t guid;
@@ -160,28 +150,25 @@ static uint64_t guid;
160 150
161static int 151static int
162iterate_one_shot (void *cls, 152iterate_one_shot (void *cls,
163 const GNUNET_HashCode * key, 153 const GNUNET_HashCode * key,
164 uint32_t size, 154 uint32_t size,
165 const void *data, 155 const void *data,
166 enum GNUNET_BLOCK_Type type, 156 enum GNUNET_BLOCK_Type type,
167 uint32_t priority, 157 uint32_t priority,
168 uint32_t anonymity, 158 uint32_t anonymity,
169 struct GNUNET_TIME_Absolute 159 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
170 expiration,
171 uint64_t uid)
172{ 160{
173 struct CpsRunContext *crc = cls; 161 struct CpsRunContext *crc = cls;
174 162
175 GNUNET_assert (key != NULL); 163 GNUNET_assert (key != NULL);
176 guid = uid; 164 guid = uid;
177 crc->phase++; 165 crc->phase++;
178#if VERBOSE 166#if VERBOSE
179 fprintf (stderr, 167 fprintf (stderr,
180 "Found result type=%u, priority=%u, size=%u, expire=%llu, key %s\n", 168 "Found result type=%u, priority=%u, size=%u, expire=%llu, key %s\n",
181 type, priority, size, 169 type, priority, size,
182 (unsigned long long) expiration.abs_value, 170 (unsigned long long) expiration.abs_value, GNUNET_h2s (key));
183 GNUNET_h2s (key)); 171#endif
184#endif
185 GNUNET_SCHEDULER_add_now (&test, crc); 172 GNUNET_SCHEDULER_add_now (&test, crc);
186 return GNUNET_OK; 173 return GNUNET_OK;
187} 174}
@@ -195,8 +182,8 @@ iterate_one_shot (void *cls,
195 * @param cfg configuration to use 182 * @param cfg configuration to use
196 */ 183 */
197static void 184static void
198unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api, 185unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
199 const struct GNUNET_CONFIGURATION_Handle *cfg) 186 const struct GNUNET_CONFIGURATION_Handle *cfg)
200{ 187{
201 char *name; 188 char *name;
202 char *libname; 189 char *libname;
@@ -204,13 +191,12 @@ unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api,
204 if (GNUNET_OK != 191 if (GNUNET_OK !=
205 GNUNET_CONFIGURATION_get_value_string (cfg, 192 GNUNET_CONFIGURATION_get_value_string (cfg,
206 "DATASTORE", "DATABASE", &name)) 193 "DATASTORE", "DATABASE", &name))
207 { 194 {
208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
209 _("No `%s' specified for `%s' in configuration!\n"), 196 _("No `%s' specified for `%s' in configuration!\n"),
210 "DATABASE", 197 "DATABASE", "DATASTORE");
211 "DATASTORE"); 198 return;
212 return; 199 }
213 }
214 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name); 200 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
215 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api)); 201 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
216 GNUNET_free (libname); 202 GNUNET_free (libname);
@@ -224,8 +210,7 @@ unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api,
224 * the transport and core. 210 * the transport and core.
225 */ 211 */
226static void 212static void
227cleaning_task (void *cls, 213cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
228 const struct GNUNET_SCHEDULER_TaskContext *tc)
229{ 214{
230 struct CpsRunContext *crc = cls; 215 struct CpsRunContext *crc = cls;
231 216
@@ -235,9 +220,8 @@ cleaning_task (void *cls,
235 220
236 221
237static void 222static void
238test (void *cls, 223test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
239 const struct GNUNET_SCHEDULER_TaskContext *tc) 224{
240{
241 struct CpsRunContext *crc = cls; 225 struct CpsRunContext *crc = cls;
242 int j; 226 int j;
243 unsigned long long os; 227 unsigned long long os;
@@ -245,91 +229,76 @@ test (void *cls,
245 GNUNET_HashCode key; 229 GNUNET_HashCode key;
246 230
247 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 231 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
248 { 232 {
249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 233 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test aborted.\n");
250 "Test aborted.\n"); 234 crc->phase = RP_ERROR;
251 crc->phase = RP_ERROR; 235 }
252 }
253#if VERBOSE 236#if VERBOSE
254 fprintf (stderr, "In phase %d, iteration %u\n", 237 fprintf (stderr, "In phase %d, iteration %u\n", crc->phase, crc->cnt);
255 crc->phase,
256 crc->cnt);
257#endif 238#endif
258 switch (crc->phase) 239 switch (crc->phase)
240 {
241 case RP_ERROR:
242 ok = 1;
243 GNUNET_break (0);
244 crc->api->drop (crc->api->cls);
245 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
246 break;
247 case RP_PUT:
248 os = 0;
249 for (j = 0; j < PUT_10; j++)
259 { 250 {
260 case RP_ERROR: 251 put_value (crc->api, j, crc->i);
261 ok = 1; 252 cs = crc->api->estimate_size (crc->api->cls);
262 GNUNET_break (0); 253 GNUNET_assert (os <= cs);
263 crc->api->drop (crc->api->cls); 254 os = cs;
264 GNUNET_SCHEDULER_add_now (&cleaning_task, crc); 255 }
265 break; 256 crc->phase++;
266 case RP_PUT: 257 GNUNET_SCHEDULER_add_now (&test, crc);
267 os = 0; 258 break;
268 for (j=0;j<PUT_10;j++) 259 case RP_GET:
269 { 260 if (crc->cnt == 1)
270 put_value (crc->api, j, crc->i); 261 {
271 cs = crc->api->estimate_size (crc->api->cls); 262 crc->cnt = 0;
272 GNUNET_assert (os <= cs);
273 os = cs;
274 }
275 crc->phase++; 263 crc->phase++;
276 GNUNET_SCHEDULER_add_now (&test, crc); 264 GNUNET_SCHEDULER_add_now (&test, crc);
277 break; 265 break;
278 case RP_GET: 266 }
279 if (crc->cnt == 1) 267 gen_key (5, &key);
280 { 268 crc->api->get_key (crc->api->cls,
281 crc->cnt = 0; 269 crc->offset++,
282 crc->phase++; 270 &key, NULL,
283 GNUNET_SCHEDULER_add_now (&test, crc); 271 GNUNET_BLOCK_TYPE_ANY, &iterate_one_shot, crc);
284 break; 272 break;
285 } 273 case RP_UPDATE:
286 gen_key (5, &key); 274 GNUNET_assert (GNUNET_OK ==
287 crc->api->get_key (crc->api->cls, 275 crc->api->update (crc->api->cls,
288 crc->offset++, 276 guid, 1, GNUNET_TIME_UNIT_ZERO_ABS, NULL));
289 &key, NULL, 277 crc->phase++;
290 GNUNET_BLOCK_TYPE_ANY, 278 GNUNET_SCHEDULER_add_now (&test, crc);
291 &iterate_one_shot, 279 break;
292 crc); 280
293 break; 281 case RP_ITER_ZERO:
294 case RP_UPDATE: 282 if (crc->cnt == 1)
295 GNUNET_assert (GNUNET_OK == 283 {
296 crc->api->update (crc->api->cls, 284 crc->cnt = 0;
297 guid, 1,
298 GNUNET_TIME_UNIT_ZERO_ABS,
299 NULL));
300 crc->phase++; 285 crc->phase++;
301 GNUNET_SCHEDULER_add_now (&test, crc); 286 GNUNET_SCHEDULER_add_now (&test, crc);
302 break; 287 break;
303
304 case RP_ITER_ZERO:
305 if (crc->cnt == 1)
306 {
307 crc->cnt = 0;
308 crc->phase++;
309 GNUNET_SCHEDULER_add_now (&test, crc);
310 break;
311 }
312 crc->api->get_zero_anonymity (crc->api->cls,
313 0,
314 1,
315 &iterate_one_shot,
316 crc);
317 break;
318 case RP_REPL_GET:
319 crc->api->get_replication (crc->api->cls,
320 &iterate_one_shot,
321 crc);
322 break;
323 case RP_EXPI_GET:
324 crc->api->get_expiration (crc->api->cls,
325 &iterate_one_shot,
326 crc);
327 break;
328 case RP_DROP:
329 crc->api->drop (crc->api->cls);
330 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
331 break;
332 } 288 }
289 crc->api->get_zero_anonymity (crc->api->cls, 0, 1, &iterate_one_shot, crc);
290 break;
291 case RP_REPL_GET:
292 crc->api->get_replication (crc->api->cls, &iterate_one_shot, crc);
293 break;
294 case RP_EXPI_GET:
295 crc->api->get_expiration (crc->api->cls, &iterate_one_shot, crc);
296 break;
297 case RP_DROP:
298 crc->api->drop (crc->api->cls);
299 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
300 break;
301 }
333} 302}
334 303
335 304
@@ -340,20 +309,19 @@ static struct GNUNET_DATASTORE_PluginFunctions *
340load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg) 309load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
341{ 310{
342 static struct GNUNET_DATASTORE_PluginEnvironment env; 311 static struct GNUNET_DATASTORE_PluginEnvironment env;
343 struct GNUNET_DATASTORE_PluginFunctions * ret; 312 struct GNUNET_DATASTORE_PluginFunctions *ret;
344 char *name; 313 char *name;
345 char *libname; 314 char *libname;
346 315
347 if (GNUNET_OK != 316 if (GNUNET_OK !=
348 GNUNET_CONFIGURATION_get_value_string (cfg, 317 GNUNET_CONFIGURATION_get_value_string (cfg,
349 "DATASTORE", "DATABASE", &name)) 318 "DATASTORE", "DATABASE", &name))
350 { 319 {
351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
352 _("No `%s' specified for `%s' in configuration!\n"), 321 _("No `%s' specified for `%s' in configuration!\n"),
353 "DATABASE", 322 "DATABASE", "DATASTORE");
354 "DATASTORE"); 323 return NULL;
355 return NULL; 324 }
356 }
357 env.cfg = cfg; 325 env.cfg = cfg;
358 env.duc = &disk_utilization_change_cb; 326 env.duc = &disk_utilization_change_cb;
359 env.cls = NULL; 327 env.cls = NULL;
@@ -361,12 +329,10 @@ load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
361 _("Loading `%s' datastore plugin\n"), name); 329 _("Loading `%s' datastore plugin\n"), name);
362 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name); 330 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
363 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env))) 331 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
364 { 332 {
365 fprintf (stderr, 333 fprintf (stderr, "Failed to load plugin `%s'!\n", name);
366 "Failed to load plugin `%s'!\n", 334 return NULL;
367 name); 335 }
368 return NULL;
369 }
370 GNUNET_free (libname); 336 GNUNET_free (libname);
371 GNUNET_free (name); 337 GNUNET_free (name);
372 return ret; 338 return ret;
@@ -376,20 +342,19 @@ load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
376static void 342static void
377run (void *cls, 343run (void *cls,
378 char *const *args, 344 char *const *args,
379 const char *cfgfile, 345 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
380 const struct GNUNET_CONFIGURATION_Handle *c)
381{ 346{
382 struct GNUNET_DATASTORE_PluginFunctions *api; 347 struct GNUNET_DATASTORE_PluginFunctions *api;
383 struct CpsRunContext *crc; 348 struct CpsRunContext *crc;
384 349
385 api = load_plugin (c); 350 api = load_plugin (c);
386 if (api == NULL) 351 if (api == NULL)
387 { 352 {
388 fprintf (stderr, 353 fprintf (stderr,
389 "Could not initialize plugin, assuming database not configured. Test not run!\n"); 354 "Could not initialize plugin, assuming database not configured. Test not run!\n");
390 return; 355 return;
391 } 356 }
392 crc = GNUNET_malloc(sizeof(struct CpsRunContext)); 357 crc = GNUNET_malloc (sizeof (struct CpsRunContext));
393 crc->api = api; 358 crc->api = api;
394 crc->cfg = c; 359 crc->cfg = c;
395 crc->phase = RP_PUT; 360 crc->phase = RP_PUT;
@@ -401,7 +366,8 @@ static int
401check () 366check ()
402{ 367{
403 char cfg_name[128]; 368 char cfg_name[128];
404 char *const argv[] = { 369
370 char *const argv[] = {
405 "test-plugin-datastore", 371 "test-plugin-datastore",
406 "-c", 372 "-c",
407 cfg_name, 373 cfg_name,
@@ -415,9 +381,8 @@ check ()
415 }; 381 };
416 382
417 GNUNET_snprintf (cfg_name, 383 GNUNET_snprintf (cfg_name,
418 sizeof (cfg_name), 384 sizeof (cfg_name),
419 "test_plugin_datastore_data_%s.conf", 385 "test_plugin_datastore_data_%s.conf", plugin_name);
420 plugin_name);
421 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, 386 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
422 argv, "test-plugin-datastore", "nohelp", 387 argv, "test-plugin-datastore", "nohelp",
423 options, &run, NULL); 388 options, &run, NULL);
@@ -437,17 +402,16 @@ main (int argc, char *argv[])
437 sleep (1); 402 sleep (1);
438 /* determine name of plugin to use */ 403 /* determine name of plugin to use */
439 plugin_name = argv[0]; 404 plugin_name = argv[0];
440 while (NULL != (pos = strstr(plugin_name, "_"))) 405 while (NULL != (pos = strstr (plugin_name, "_")))
441 plugin_name = pos+1; 406 plugin_name = pos + 1;
442 if (NULL != (pos = strstr(plugin_name, "."))) 407 if (NULL != (pos = strstr (plugin_name, ".")))
443 pos[0] = 0; 408 pos[0] = 0;
444 else 409 else
445 pos = (char *) plugin_name; 410 pos = (char *) plugin_name;
446 411
447 GNUNET_snprintf (dir_name, 412 GNUNET_snprintf (dir_name,
448 sizeof (dir_name), 413 sizeof (dir_name),
449 "/tmp/test-gnunet-datastore-plugin-%s", 414 "/tmp/test-gnunet-datastore-plugin-%s", plugin_name);
450 plugin_name);
451 GNUNET_DISK_directory_remove (dir_name); 415 GNUNET_DISK_directory_remove (dir_name);
452 GNUNET_log_setup ("test-plugin-datastore", 416 GNUNET_log_setup ("test-plugin-datastore",
453#if VERBOSE 417#if VERBOSE
@@ -465,5 +429,3 @@ main (int argc, char *argv[])
465} 429}
466 430
467/* end of test_plugin_datastore.c */ 431/* end of test_plugin_datastore.c */
468
469