2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
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
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file datastore/datastore_api.c
23 * @brief Management for the datastore for files stored on a GNUnet node. Implements
24 * a priority queue for requests (with timeouts).
25 * @author Christian Grothoff
28 #include "gnunet_arm_service.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_datastore_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "datastore.h"
35 * If a client stopped asking for more results, how many more do
36 * we receive from the DB before killing the connection? Trade-off
37 * between re-doing TCP handshakes and (needlessly) receiving
40 #define MAX_EXCESS_RESULTS 8
43 * Context for processing status messages.
48 * Continuation to call with the status.
50 GNUNET_DATASTORE_ContinuationWithStatus cont;
61 * Context for processing result messages.
66 * Function to call with the result.
68 GNUNET_DATASTORE_DatumProcessor proc;
79 * Context for a queue operation.
84 struct StatusContext sc;
86 struct ResultContext rc;
93 * Entry in our priority queue.
95 struct GNUNET_DATASTORE_QueueEntry
99 * This is a linked list.
101 struct GNUNET_DATASTORE_QueueEntry *next;
104 * This is a linked list.
106 struct GNUNET_DATASTORE_QueueEntry *prev;
109 * Handle to the master context.
111 struct GNUNET_DATASTORE_Handle *h;
114 * Response processor (NULL if we are not waiting for a response).
115 * This struct should be used for the closure, function-specific
116 * arguments can be passed via 'qc'.
118 GNUNET_CLIENT_MessageHandler response_proc;
121 * Function to call after transmission of the request.
123 GNUNET_DATASTORE_ContinuationWithStatus cont;
126 * Closure for 'cont'.
131 * Context for the operation.
133 union QueueContext qc;
136 * Task for timeout signalling.
138 GNUNET_SCHEDULER_TaskIdentifier task;
141 * Timeout for the current operation.
143 struct GNUNET_TIME_Absolute timeout;
146 * Priority in the queue.
148 unsigned int priority;
151 * Maximum allowed length of queue (otherwise
152 * this request should be discarded).
154 unsigned int max_queue;
157 * Number of bytes in the request message following
158 * this struct. 32-bit value for nicer memory
159 * access (and overall struct alignment).
161 uint32_t message_size;
164 * Has this message been transmitted to the service?
165 * Only ever GNUNET_YES for the head of the queue.
166 * Note that the overall struct should end at a
167 * multiple of 64 bits.
174 * Handle to the datastore service.
176 struct GNUNET_DATASTORE_Handle
182 const struct GNUNET_CONFIGURATION_Handle *cfg;
185 * Current connection to the datastore service.
187 struct GNUNET_CLIENT_Connection *client;
190 * Handle for statistics.
192 struct GNUNET_STATISTICS_Handle *stats;
195 * Current transmit handle.
197 struct GNUNET_CLIENT_TransmitHandle *th;
200 * Current head of priority queue.
202 struct GNUNET_DATASTORE_QueueEntry *queue_head;
205 * Current tail of priority queue.
207 struct GNUNET_DATASTORE_QueueEntry *queue_tail;
210 * Task for trying to reconnect.
212 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
215 * How quickly should we retry? Used for exponential back-off on
218 struct GNUNET_TIME_Relative retry_time;
221 * Number of entries in the queue.
223 unsigned int queue_size;
226 * Number of results we're receiving for the current query
227 * after application stopped to care. Used to determine when
228 * to reset the connection.
230 unsigned int result_count;
233 * Are we currently trying to receive from the service?
238 * We should ignore the next message(s) from the service.
240 unsigned int skip_next_messages;
247 * Connect to the datastore service.
249 * @param cfg configuration to use
250 * @return handle to use to access the service
252 struct GNUNET_DATASTORE_Handle *
253 GNUNET_DATASTORE_connect (const struct
254 GNUNET_CONFIGURATION_Handle
257 struct GNUNET_CLIENT_Connection *c;
258 struct GNUNET_DATASTORE_Handle *h;
260 c = GNUNET_CLIENT_connect ("datastore", cfg);
262 return NULL; /* oops */
263 h = GNUNET_malloc (sizeof(struct GNUNET_DATASTORE_Handle) +
264 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1);
267 h->stats = GNUNET_STATISTICS_create ("datastore-api",
274 * Transmit DROP message to datastore service.
276 * @param cls the 'struct GNUNET_DATASTORE_Handle'
277 * @param size number of bytes that can be copied to buf
278 * @param buf where to copy the drop message
279 * @return number of bytes written to buf
282 transmit_drop (void *cls,
286 struct GNUNET_DATASTORE_Handle *h = cls;
287 struct GNUNET_MessageHeader *hdr;
291 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
292 _("Failed to transmit request to drop database.\n"));
293 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
296 GNUNET_assert (size >= sizeof(struct GNUNET_MessageHeader));
298 hdr->size = htons(sizeof(struct GNUNET_MessageHeader));
299 hdr->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
300 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
301 return sizeof(struct GNUNET_MessageHeader);
306 * Disconnect from the datastore service (and free
307 * associated resources).
309 * @param h handle to the datastore
310 * @param drop set to GNUNET_YES to delete all data in datastore (!)
313 GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
316 struct GNUNET_DATASTORE_QueueEntry *qe;
318 if (h->client != NULL)
320 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
323 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
325 GNUNET_SCHEDULER_cancel (h->reconnect_task);
326 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
328 while (NULL != (qe = h->queue_head))
330 GNUNET_assert (NULL != qe->response_proc);
331 qe->response_proc (h, NULL);
333 if (GNUNET_YES == drop)
335 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
336 if (h->client != NULL)
339 GNUNET_CLIENT_notify_transmit_ready (h->client,
340 sizeof(struct GNUNET_MessageHeader),
341 GNUNET_TIME_UNIT_MINUTES,
346 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
350 GNUNET_STATISTICS_destroy (h->stats,
357 * A request has timed out (before being transmitted to the service).
359 * @param cls the 'struct GNUNET_DATASTORE_QueueEntry'
360 * @param tc scheduler context
363 timeout_queue_entry (void *cls,
364 const struct GNUNET_SCHEDULER_TaskContext *tc)
366 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
368 GNUNET_STATISTICS_update (qe->h->stats,
369 gettext_noop ("# queue entry timeouts"),
372 qe->task = GNUNET_SCHEDULER_NO_TASK;
373 GNUNET_assert (qe->was_transmitted == GNUNET_NO);
374 qe->response_proc (qe->h, NULL);
379 * Create a new entry for our priority queue (and possibly discard other entires if
380 * the queue is getting too long).
382 * @param h handle to the datastore
383 * @param msize size of the message to queue
384 * @param queue_priority priority of the entry
385 * @param max_queue_size at what queue size should this request be dropped
386 * (if other requests of higher priority are in the queue)
387 * @param timeout timeout for the operation
388 * @param response_proc function to call with replies (can be NULL)
389 * @param qc client context (NOT a closure for response_proc)
390 * @return NULL if the queue is full
392 static struct GNUNET_DATASTORE_QueueEntry *
393 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
395 unsigned int queue_priority,
396 unsigned int max_queue_size,
397 struct GNUNET_TIME_Relative timeout,
398 GNUNET_CLIENT_MessageHandler response_proc,
399 const union QueueContext *qc)
401 struct GNUNET_DATASTORE_QueueEntry *ret;
402 struct GNUNET_DATASTORE_QueueEntry *pos;
407 while ( (pos != NULL) &&
408 (c < max_queue_size) &&
409 (pos->priority >= queue_priority) )
414 if (c >= max_queue_size)
416 GNUNET_STATISTICS_update (h->stats,
417 gettext_noop ("# queue overflows"),
422 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
424 ret->response_proc = response_proc;
426 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
427 ret->priority = queue_priority;
428 ret->max_queue = max_queue_size;
429 ret->message_size = msize;
430 ret->was_transmitted = GNUNET_NO;
433 /* append at the tail */
439 /* do not insert at HEAD if HEAD query was already
440 transmitted and we are still receiving replies! */
441 if ( (pos == NULL) &&
442 (h->queue_head->was_transmitted) )
446 GNUNET_STATISTICS_update (h->stats,
447 gettext_noop ("# queue entries created"),
450 GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
455 ret->task = GNUNET_SCHEDULER_add_delayed (timeout,
456 &timeout_queue_entry,
461 if (pos->max_queue < h->queue_size)
463 GNUNET_assert (pos->response_proc != NULL);
464 /* move 'pos' element to head so that it will be
465 killed on 'NULL' call below */
466 GNUNET_CONTAINER_DLL_remove (h->queue_head,
469 GNUNET_CONTAINER_DLL_insert (h->queue_head,
472 pos->response_proc (h, NULL);
482 * Process entries in the queue (or do nothing if we are already
485 * @param h handle to the datastore
488 process_queue (struct GNUNET_DATASTORE_Handle *h);
492 * Try reconnecting to the datastore service.
494 * @param cls the 'struct GNUNET_DATASTORE_Handle'
495 * @param tc scheduler context
498 try_reconnect (void *cls,
499 const struct GNUNET_SCHEDULER_TaskContext *tc)
501 struct GNUNET_DATASTORE_Handle *h = cls;
503 if (h->retry_time.rel_value < GNUNET_CONSTANTS_SERVICE_RETRY.rel_value)
504 h->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
506 h->retry_time = GNUNET_TIME_relative_multiply (h->retry_time, 2);
507 if (h->retry_time.rel_value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.rel_value)
508 h->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
509 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
510 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
511 if (h->client == NULL)
513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
514 "DATASTORE reconnect failed (fatally)\n");
517 GNUNET_STATISTICS_update (h->stats,
518 gettext_noop ("# datastore connections (re)created"),
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523 "Reconnected to DATASTORE\n");
530 * Disconnect from the service and then try reconnecting to the datastore service
533 * @param h handle to datastore to disconnect and reconnect
536 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
538 if (h->client == NULL)
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542 "client NULL in disconnect, will not try to reconnect\n");
547 GNUNET_STATISTICS_update (stats,
548 gettext_noop ("# reconnected to DATASTORE"),
552 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
553 h->skip_next_messages = 0;
555 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_time,
562 * Transmit request from queue to datastore service.
564 * @param cls the 'struct GNUNET_DATASTORE_Handle'
565 * @param size number of bytes that can be copied to buf
566 * @param buf where to copy the drop message
567 * @return number of bytes written to buf
570 transmit_request (void *cls,
574 struct GNUNET_DATASTORE_Handle *h = cls;
575 struct GNUNET_DATASTORE_QueueEntry *qe;
579 if (NULL == (qe = h->queue_head))
580 return 0; /* no entry in queue */
583 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
584 _("Failed to transmit request to DATASTORE.\n"));
585 GNUNET_STATISTICS_update (h->stats,
586 gettext_noop ("# transmission request failures"),
592 if (size < (msize = qe->message_size))
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
599 "Transmitting %u byte request to DATASTORE\n",
602 memcpy (buf, &qe[1], msize);
603 qe->was_transmitted = GNUNET_YES;
604 GNUNET_SCHEDULER_cancel (qe->task);
605 qe->task = GNUNET_SCHEDULER_NO_TASK;
606 h->in_receive = GNUNET_YES;
607 GNUNET_CLIENT_receive (h->client,
610 GNUNET_TIME_absolute_get_remaining (qe->timeout));
611 GNUNET_STATISTICS_update (h->stats,
612 gettext_noop ("# bytes sent to datastore"),
620 * Process entries in the queue (or do nothing if we are already
623 * @param h handle to the datastore
626 process_queue (struct GNUNET_DATASTORE_Handle *h)
628 struct GNUNET_DATASTORE_QueueEntry *qe;
630 if (NULL == (qe = h->queue_head))
632 #if DEBUG_DATASTORE > 1
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 return; /* no entry in queue */
638 if (qe->was_transmitted == GNUNET_YES)
640 #if DEBUG_DATASTORE > 1
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
642 "Head request already transmitted\n");
644 return; /* waiting for replies */
648 #if DEBUG_DATASTORE > 1
649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
650 "Pending transmission request\n");
652 return; /* request pending */
654 if (h->client == NULL)
656 #if DEBUG_DATASTORE > 1
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660 return; /* waiting for reconnect */
662 if (GNUNET_YES == h->in_receive)
664 /* wait for response to previous query */
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
669 "Queueing %u byte request to DATASTORE\n",
672 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
674 GNUNET_TIME_absolute_get_remaining (qe->timeout),
682 * Dummy continuation used to do nothing (but be non-zero).
685 * @param result result
686 * @param emsg error message
689 drop_status_cont (void *cls, int32_t result, const char *emsg)
696 free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
698 struct GNUNET_DATASTORE_Handle *h = qe->h;
700 GNUNET_CONTAINER_DLL_remove (h->queue_head,
703 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
705 GNUNET_SCHEDULER_cancel (qe->task);
706 qe->task = GNUNET_SCHEDULER_NO_TASK;
709 qe->was_transmitted = GNUNET_SYSERR; /* use-after-free warning */
714 * Type of a function to call when we receive a message
718 * @param msg message received, NULL on timeout or fatal error
721 process_status_message (void *cls,
723 GNUNET_MessageHeader * msg)
725 struct GNUNET_DATASTORE_Handle *h = cls;
726 struct GNUNET_DATASTORE_QueueEntry *qe;
727 struct StatusContext rc;
728 const struct StatusMessage *sm;
733 h->in_receive = GNUNET_NO;
734 if (h->skip_next_messages > 0)
736 h->skip_next_messages--;
740 if (NULL == (qe = h->queue_head))
749 was_transmitted = qe->was_transmitted;
750 free_queue_entry (qe);
751 if (NULL == h->client)
752 return; /* forced disconnect */
754 rc.cont (rc.cont_cls,
756 _("Failed to receive status response from database."));
757 if (was_transmitted == GNUNET_YES)
761 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
762 free_queue_entry (qe);
763 if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
764 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) )
767 h->retry_time = GNUNET_TIME_UNIT_ZERO;
770 rc.cont (rc.cont_cls,
772 _("Error reading response from datastore service"));
775 sm = (const struct StatusMessage*) msg;
776 status = ntohl(sm->status);
778 if (ntohs(msg->size) > sizeof(struct StatusMessage))
780 emsg = (const char*) &sm[1];
781 if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
784 emsg = _("Invalid error message received from datastore service");
787 if ( (status == GNUNET_SYSERR) &&
791 emsg = _("Invalid error message received from datastore service");
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
795 "Received status %d/%s\n",
799 GNUNET_STATISTICS_update (h->stats,
800 gettext_noop ("# status messages received"),
803 h->retry_time.rel_value = 0;
806 rc.cont (rc.cont_cls,
813 * Store an item in the datastore. If the item is already present,
814 * the priorities are summed up and the higher expiration time and
815 * lower anonymity level is used.
817 * @param h handle to the datastore
818 * @param rid reservation ID to use (from "reserve"); use 0 if no
819 * prior reservation was made
820 * @param key key for the value
821 * @param size number of bytes in data
822 * @param data content stored
823 * @param type type of the content
824 * @param priority priority of the content
825 * @param anonymity anonymity-level for the content
826 * @param replication how often should the content be replicated to other peers?
827 * @param expiration expiration time for the content
828 * @param queue_priority ranking of this request in the priority queue
829 * @param max_queue_size at what queue size should this request be dropped
830 * (if other requests of higher priority are in the queue)
831 * @param timeout timeout for the operation
832 * @param cont continuation to call when done
833 * @param cont_cls closure for cont
834 * @return NULL if the entry was not queued, otherwise a handle that can be used to
835 * cancel; note that even if NULL is returned, the callback will be invoked
836 * (or rather, will already have been invoked)
838 struct GNUNET_DATASTORE_QueueEntry *
839 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
841 const GNUNET_HashCode * key,
844 enum GNUNET_BLOCK_Type type,
847 uint32_t replication,
848 struct GNUNET_TIME_Absolute expiration,
849 unsigned int queue_priority,
850 unsigned int max_queue_size,
851 struct GNUNET_TIME_Relative timeout,
852 GNUNET_DATASTORE_ContinuationWithStatus cont,
855 struct GNUNET_DATASTORE_QueueEntry *qe;
856 struct DataMessage *dm;
858 union QueueContext qc;
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
862 "Asked to put %u bytes of data under key `%s' for %llu ms\n",
865 GNUNET_TIME_absolute_get_remaining (expiration).rel_value);
867 msize = sizeof(struct DataMessage) + size;
868 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
870 qc.sc.cont_cls = cont_cls;
871 qe = make_queue_entry (h, msize,
872 queue_priority, max_queue_size, timeout,
873 &process_status_message, &qc);
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
878 "Could not create queue entry for PUT\n");
882 GNUNET_STATISTICS_update (h->stats,
883 gettext_noop ("# PUT requests executed"),
886 dm = (struct DataMessage* ) &qe[1];
887 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
888 dm->header.size = htons(msize);
889 dm->rid = htonl(rid);
890 dm->size = htonl( (uint32_t) size);
891 dm->type = htonl(type);
892 dm->priority = htonl(priority);
893 dm->anonymity = htonl(anonymity);
894 dm->replication = htonl (replication);
895 dm->reserved = htonl (0);
896 dm->uid = GNUNET_htonll(0);
897 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
899 memcpy (&dm[1], data, size);
906 * Reserve space in the datastore. This function should be used
907 * to avoid "out of space" failures during a longer sequence of "put"
908 * operations (for example, when a file is being inserted).
910 * @param h handle to the datastore
911 * @param amount how much space (in bytes) should be reserved (for content only)
912 * @param entries how many entries will be created (to calculate per-entry overhead)
913 * @param queue_priority ranking of this request in the priority queue
914 * @param max_queue_size at what queue size should this request be dropped
915 * (if other requests of higher priority are in the queue)
916 * @param timeout how long to wait at most for a response (or before dying in queue)
917 * @param cont continuation to call when done; "success" will be set to
918 * a positive reservation value if space could be reserved.
919 * @param cont_cls closure for cont
920 * @return NULL if the entry was not queued, otherwise a handle that can be used to
921 * cancel; note that even if NULL is returned, the callback will be invoked
922 * (or rather, will already have been invoked)
924 struct GNUNET_DATASTORE_QueueEntry *
925 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
928 unsigned int queue_priority,
929 unsigned int max_queue_size,
930 struct GNUNET_TIME_Relative timeout,
931 GNUNET_DATASTORE_ContinuationWithStatus cont,
934 struct GNUNET_DATASTORE_QueueEntry *qe;
935 struct ReserveMessage *rm;
936 union QueueContext qc;
939 cont = &drop_status_cont;
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
942 "Asked to reserve %llu bytes of data and %u entries'\n",
943 (unsigned long long) amount,
944 (unsigned int) entries);
947 qc.sc.cont_cls = cont_cls;
948 qe = make_queue_entry (h, sizeof(struct ReserveMessage),
949 queue_priority, max_queue_size, timeout,
950 &process_status_message, &qc);
954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
955 "Could not create queue entry to reserve\n");
959 GNUNET_STATISTICS_update (h->stats,
960 gettext_noop ("# RESERVE requests executed"),
963 rm = (struct ReserveMessage*) &qe[1];
964 rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
965 rm->header.size = htons(sizeof (struct ReserveMessage));
966 rm->entries = htonl(entries);
967 rm->amount = GNUNET_htonll(amount);
974 * Signal that all of the data for which a reservation was made has
975 * been stored and that whatever excess space might have been reserved
976 * can now be released.
978 * @param h handle to the datastore
979 * @param rid reservation ID (value of "success" in original continuation
980 * from the "reserve" function).
981 * @param queue_priority ranking of this request in the priority queue
982 * @param max_queue_size at what queue size should this request be dropped
983 * (if other requests of higher priority are in the queue)
984 * @param queue_priority ranking of this request in the priority queue
985 * @param max_queue_size at what queue size should this request be dropped
986 * (if other requests of higher priority are in the queue)
987 * @param timeout how long to wait at most for a response
988 * @param cont continuation to call when done
989 * @param cont_cls closure for cont
990 * @return NULL if the entry was not queued, otherwise a handle that can be used to
991 * cancel; note that even if NULL is returned, the callback will be invoked
992 * (or rather, will already have been invoked)
994 struct GNUNET_DATASTORE_QueueEntry *
995 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
997 unsigned int queue_priority,
998 unsigned int max_queue_size,
999 struct GNUNET_TIME_Relative timeout,
1000 GNUNET_DATASTORE_ContinuationWithStatus cont,
1003 struct GNUNET_DATASTORE_QueueEntry *qe;
1004 struct ReleaseReserveMessage *rrm;
1005 union QueueContext qc;
1008 cont = &drop_status_cont;
1010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1011 "Asked to release reserve %d\n",
1015 qc.sc.cont_cls = cont_cls;
1016 qe = make_queue_entry (h, sizeof(struct ReleaseReserveMessage),
1017 queue_priority, max_queue_size, timeout,
1018 &process_status_message, &qc);
1022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1023 "Could not create queue entry to release reserve\n");
1027 GNUNET_STATISTICS_update (h->stats,
1028 gettext_noop ("# RELEASE RESERVE requests executed"),
1031 rrm = (struct ReleaseReserveMessage*) &qe[1];
1032 rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
1033 rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
1034 rrm->rid = htonl(rid);
1041 * Update a value in the datastore.
1043 * @param h handle to the datastore
1044 * @param uid identifier for the value
1045 * @param priority how much to increase the priority of the value
1046 * @param expiration new expiration value should be MAX of existing and this argument
1047 * @param queue_priority ranking of this request in the priority queue
1048 * @param max_queue_size at what queue size should this request be dropped
1049 * (if other requests of higher priority are in the queue)
1050 * @param timeout how long to wait at most for a response
1051 * @param cont continuation to call when done
1052 * @param cont_cls closure for cont
1053 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1054 * cancel; note that even if NULL is returned, the callback will be invoked
1055 * (or rather, will already have been invoked)
1057 struct GNUNET_DATASTORE_QueueEntry *
1058 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
1061 struct GNUNET_TIME_Absolute expiration,
1062 unsigned int queue_priority,
1063 unsigned int max_queue_size,
1064 struct GNUNET_TIME_Relative timeout,
1065 GNUNET_DATASTORE_ContinuationWithStatus cont,
1068 struct GNUNET_DATASTORE_QueueEntry *qe;
1069 struct UpdateMessage *um;
1070 union QueueContext qc;
1073 cont = &drop_status_cont;
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
1078 (unsigned int) priority,
1079 (unsigned long long) expiration.abs_value);
1082 qc.sc.cont_cls = cont_cls;
1083 qe = make_queue_entry (h, sizeof(struct UpdateMessage),
1084 queue_priority, max_queue_size, timeout,
1085 &process_status_message, &qc);
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090 "Could not create queue entry for UPDATE\n");
1094 GNUNET_STATISTICS_update (h->stats,
1095 gettext_noop ("# UPDATE requests executed"),
1098 um = (struct UpdateMessage*) &qe[1];
1099 um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
1100 um->header.size = htons(sizeof (struct UpdateMessage));
1101 um->priority = htonl(priority);
1102 um->expiration = GNUNET_TIME_absolute_hton(expiration);
1103 um->uid = GNUNET_htonll(uid);
1110 * Explicitly remove some content from the database.
1111 * The "cont"inuation will be called with status
1112 * "GNUNET_OK" if content was removed, "GNUNET_NO"
1113 * if no matching entry was found and "GNUNET_SYSERR"
1114 * on all other types of errors.
1116 * @param h handle to the datastore
1117 * @param key key for the value
1118 * @param size number of bytes in data
1119 * @param data content stored
1120 * @param queue_priority ranking of this request in the priority queue
1121 * @param max_queue_size at what queue size should this request be dropped
1122 * (if other requests of higher priority are in the queue)
1123 * @param timeout how long to wait at most for a response
1124 * @param cont continuation to call when done
1125 * @param cont_cls closure for cont
1126 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1127 * cancel; note that even if NULL is returned, the callback will be invoked
1128 * (or rather, will already have been invoked)
1130 struct GNUNET_DATASTORE_QueueEntry *
1131 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
1132 const GNUNET_HashCode *key,
1135 unsigned int queue_priority,
1136 unsigned int max_queue_size,
1137 struct GNUNET_TIME_Relative timeout,
1138 GNUNET_DATASTORE_ContinuationWithStatus cont,
1141 struct GNUNET_DATASTORE_QueueEntry *qe;
1142 struct DataMessage *dm;
1144 union QueueContext qc;
1147 cont = &drop_status_cont;
1149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1150 "Asked to remove %u bytes under key `%s'\n",
1155 qc.sc.cont_cls = cont_cls;
1156 msize = sizeof(struct DataMessage) + size;
1157 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1158 qe = make_queue_entry (h, msize,
1159 queue_priority, max_queue_size, timeout,
1160 &process_status_message, &qc);
1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1165 "Could not create queue entry for REMOVE\n");
1169 GNUNET_STATISTICS_update (h->stats,
1170 gettext_noop ("# REMOVE requests executed"),
1173 dm = (struct DataMessage*) &qe[1];
1174 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
1175 dm->header.size = htons(msize);
1177 dm->size = htonl(size);
1178 dm->type = htonl(0);
1179 dm->priority = htonl(0);
1180 dm->anonymity = htonl(0);
1181 dm->uid = GNUNET_htonll(0);
1182 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
1184 memcpy (&dm[1], data, size);
1191 * Type of a function to call when we receive a message
1194 * @param cls closure
1195 * @param msg message received, NULL on timeout or fatal error
1198 process_result_message (void *cls,
1199 const struct GNUNET_MessageHeader *msg)
1201 struct GNUNET_DATASTORE_Handle *h = cls;
1202 struct GNUNET_DATASTORE_QueueEntry *qe;
1203 struct ResultContext rc;
1204 const struct DataMessage *dm;
1206 h->in_receive = GNUNET_NO;
1207 if (h->skip_next_messages > 0)
1209 h->skip_next_messages--;
1216 GNUNET_assert (NULL != qe);
1217 if (qe->was_transmitted == GNUNET_YES)
1220 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1221 _("Failed to receive response from database.\n"));
1223 free_queue_entry (qe);
1224 if (rc.proc != NULL)
1225 rc.proc (rc.proc_cls,
1226 NULL, 0, NULL, 0, 0, 0,
1227 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1231 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1233 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
1236 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1237 free_queue_entry (qe);
1239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1240 "Received end of result set, new queue size is %u\n",
1243 if (rc.proc != NULL)
1244 rc.proc (rc.proc_cls,
1245 NULL, 0, NULL, 0, 0, 0,
1246 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1247 h->retry_time.rel_value = 0;
1248 h->result_count = 0;
1254 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1255 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
1256 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1257 (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
1260 free_queue_entry (qe);
1261 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1263 if (rc.proc != NULL)
1264 rc.proc (rc.proc_cls,
1265 NULL, 0, NULL, 0, 0, 0,
1266 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1269 GNUNET_STATISTICS_update (h->stats,
1270 gettext_noop ("# Results received"),
1273 dm = (const struct DataMessage*) msg;
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1276 "Received result %llu with type %u and size %u with key %s\n",
1277 (unsigned long long) GNUNET_ntohll(dm->uid),
1280 GNUNET_h2s(&dm->key));
1282 free_queue_entry (qe);
1283 h->retry_time.rel_value = 0;
1284 rc.proc (rc.proc_cls,
1289 ntohl(dm->priority),
1290 ntohl(dm->anonymity),
1291 GNUNET_TIME_absolute_ntoh(dm->expiration),
1292 GNUNET_ntohll(dm->uid));
1297 * Get a random value from the datastore for content replication.
1298 * Returns a single, random value among those with the highest
1299 * replication score, lowering positive replication scores by one for
1300 * the chosen value (if only content with a replication score exists,
1301 * a random value is returned and replication scores are not changed).
1303 * @param h handle to the datastore
1304 * @param queue_priority ranking of this request in the priority queue
1305 * @param max_queue_size at what queue size should this request be dropped
1306 * (if other requests of higher priority are in the queue)
1307 * @param timeout how long to wait at most for a response
1308 * @param proc function to call on a random value; it
1309 * will be called once with a value (if available)
1310 * and always once with a value of NULL.
1311 * @param proc_cls closure for proc
1312 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1315 struct GNUNET_DATASTORE_QueueEntry *
1316 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
1317 unsigned int queue_priority,
1318 unsigned int max_queue_size,
1319 struct GNUNET_TIME_Relative timeout,
1320 GNUNET_DATASTORE_DatumProcessor proc,
1323 struct GNUNET_DATASTORE_QueueEntry *qe;
1324 struct GNUNET_MessageHeader *m;
1325 union QueueContext qc;
1327 GNUNET_assert (NULL != proc);
1329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1330 "Asked to get replication entry in %llu ms\n",
1331 (unsigned long long) timeout.rel_value);
1334 qc.rc.proc_cls = proc_cls;
1335 qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader),
1336 queue_priority, max_queue_size, timeout,
1337 &process_result_message, &qc);
1341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1342 "Could not create queue entry for GET REPLICATION\n");
1346 GNUNET_STATISTICS_update (h->stats,
1347 gettext_noop ("# GET REPLICATION requests executed"),
1350 m = (struct GNUNET_MessageHeader*) &qe[1];
1351 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION);
1352 m->size = htons(sizeof (struct GNUNET_MessageHeader));
1359 * Get a single zero-anonymity value from the datastore.
1361 * @param h handle to the datastore
1362 * @param offset offset of the result (mod #num-results); set to
1363 * a random 64-bit value initially; then increment by
1364 * one each time; detect that all results have been found by uid
1365 * being again the first uid ever returned.
1366 * @param queue_priority ranking of this request in the priority queue
1367 * @param max_queue_size at what queue size should this request be dropped
1368 * (if other requests of higher priority are in the queue)
1369 * @param timeout how long to wait at most for a response
1370 * @param type allowed type for the operation (never zero)
1371 * @param proc function to call on a random value; it
1372 * will be called once with a value (if available)
1373 * or with NULL if none value exists.
1374 * @param proc_cls closure for proc
1375 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1378 struct GNUNET_DATASTORE_QueueEntry *
1379 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
1381 unsigned int queue_priority,
1382 unsigned int max_queue_size,
1383 struct GNUNET_TIME_Relative timeout,
1384 enum GNUNET_BLOCK_Type type,
1385 GNUNET_DATASTORE_DatumProcessor proc,
1388 struct GNUNET_DATASTORE_QueueEntry *qe;
1389 struct GetZeroAnonymityMessage *m;
1390 union QueueContext qc;
1392 GNUNET_assert (NULL != proc);
1393 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1396 "Asked to get %llu-th zero-anonymity entry of type %d in %llu ms\n",
1397 (unsigned long long) offset,
1399 (unsigned long long) timeout.rel_value);
1402 qc.rc.proc_cls = proc_cls;
1403 qe = make_queue_entry (h, sizeof(struct GetZeroAnonymityMessage),
1404 queue_priority, max_queue_size, timeout,
1405 &process_result_message, &qc);
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1410 "Could not create queue entry for zero-anonymity procation\n");
1414 GNUNET_STATISTICS_update (h->stats,
1415 gettext_noop ("# GET ZERO ANONYMITY requests executed"),
1418 m = (struct GetZeroAnonymityMessage*) &qe[1];
1419 m->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY);
1420 m->header.size = htons(sizeof (struct GetZeroAnonymityMessage));
1421 m->type = htonl ((uint32_t) type);
1422 m->offset = GNUNET_htonll (offset);
1429 * Get a result for a particular key from the datastore. The processor
1430 * will only be called once.
1432 * @param h handle to the datastore
1433 * @param offset offset of the result (mod #num-results); set to
1434 * a random 64-bit value initially; then increment by
1435 * one each time; detect that all results have been found by uid
1436 * being again the first uid ever returned.
1437 * @param key maybe NULL (to match all entries)
1438 * @param type desired type, 0 for any
1439 * @param queue_priority ranking of this request in the priority queue
1440 * @param max_queue_size at what queue size should this request be dropped
1441 * (if other requests of higher priority are in the queue)
1442 * @param timeout how long to wait at most for a response
1443 * @param proc function to call on each matching value;
1444 * will be called once with a NULL value at the end
1445 * @param proc_cls closure for proc
1446 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1449 struct GNUNET_DATASTORE_QueueEntry *
1450 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h,
1452 const GNUNET_HashCode * key,
1453 enum GNUNET_BLOCK_Type type,
1454 unsigned int queue_priority,
1455 unsigned int max_queue_size,
1456 struct GNUNET_TIME_Relative timeout,
1457 GNUNET_DATASTORE_DatumProcessor proc,
1460 struct GNUNET_DATASTORE_QueueEntry *qe;
1461 struct GetMessage *gm;
1462 union QueueContext qc;
1464 GNUNET_assert (NULL != proc);
1466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1467 "Asked to look for data of type %u under key `%s'\n",
1468 (unsigned int) type,
1472 qc.rc.proc_cls = proc_cls;
1473 qe = make_queue_entry (h, sizeof(struct GetMessage),
1474 queue_priority, max_queue_size, timeout,
1475 &process_result_message, &qc);
1479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1480 "Could not queue request for `%s'\n",
1485 GNUNET_STATISTICS_update (h->stats,
1486 gettext_noop ("# GET requests executed"),
1489 gm = (struct GetMessage*) &qe[1];
1490 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1491 gm->type = htonl(type);
1492 gm->offset = GNUNET_htonll (offset);
1495 gm->header.size = htons(sizeof (struct GetMessage));
1500 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
1508 * Cancel a datastore operation. The final callback from the
1509 * operation must not have been done yet.
1511 * @param qe operation to cancel
1514 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1516 struct GNUNET_DATASTORE_Handle *h;
1518 GNUNET_assert (GNUNET_SYSERR != qe->was_transmitted);
1521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1522 "Pending DATASTORE request %p cancelled (%d, %d)\n",
1524 qe->was_transmitted,
1525 h->queue_head == qe);
1527 if (GNUNET_YES == qe->was_transmitted)
1529 free_queue_entry (qe);
1530 h->skip_next_messages++;
1533 free_queue_entry (qe);
1538 /* end of datastore_api.c */