2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2007, 2009, 2010 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 * Iterator to call with the result.
68 GNUNET_DATASTORE_Iterator iter;
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.
169 int32_t was_transmitted;
174 * Handle to the datastore service.
176 struct GNUNET_DATASTORE_Handle
182 const struct GNUNET_CONFIGURATION_Handle *cfg;
187 struct GNUNET_SCHEDULER_Handle *sched;
190 * Current connection to the datastore service.
192 struct GNUNET_CLIENT_Connection *client;
195 * Handle for statistics.
197 struct GNUNET_STATISTICS_Handle *stats;
200 * Current transmit handle.
202 struct GNUNET_CLIENT_TransmitHandle *th;
205 * Current head of priority queue.
207 struct GNUNET_DATASTORE_QueueEntry *queue_head;
210 * Current tail of priority queue.
212 struct GNUNET_DATASTORE_QueueEntry *queue_tail;
215 * Task for trying to reconnect.
217 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
220 * How quickly should we retry? Used for exponential back-off on
223 struct GNUNET_TIME_Relative retry_time;
226 * Number of entries in the queue.
228 unsigned int queue_size;
231 * Number of results we're receiving for the current query
232 * after application stopped to care. Used to determine when
233 * to reset the connection.
235 unsigned int result_count;
238 * Are we currently trying to receive from the service?
247 * Connect to the datastore service.
249 * @param cfg configuration to use
250 * @param sched scheduler to use
251 * @return handle to use to access the service
253 struct GNUNET_DATASTORE_Handle *
254 GNUNET_DATASTORE_connect (const struct
255 GNUNET_CONFIGURATION_Handle
258 GNUNET_SCHEDULER_Handle
261 struct GNUNET_CLIENT_Connection *c;
262 struct GNUNET_DATASTORE_Handle *h;
264 c = GNUNET_CLIENT_connect (sched, "datastore", cfg);
266 return NULL; /* oops */
267 h = GNUNET_malloc (sizeof(struct GNUNET_DATASTORE_Handle) +
268 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1);
272 h->stats = GNUNET_STATISTICS_create (sched,
280 * Transmit DROP message to datastore service.
282 * @param cls the 'struct GNUNET_DATASTORE_Handle'
283 * @param size number of bytes that can be copied to buf
284 * @param buf where to copy the drop message
285 * @return number of bytes written to buf
288 transmit_drop (void *cls,
292 struct GNUNET_DATASTORE_Handle *h = cls;
293 struct GNUNET_MessageHeader *hdr;
297 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
298 _("Failed to transmit request to drop database.\n"));
299 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
302 GNUNET_assert (size >= sizeof(struct GNUNET_MessageHeader));
304 hdr->size = htons(sizeof(struct GNUNET_MessageHeader));
305 hdr->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
306 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
307 return sizeof(struct GNUNET_MessageHeader);
312 * Disconnect from the datastore service (and free
313 * associated resources).
315 * @param h handle to the datastore
316 * @param drop set to GNUNET_YES to delete all data in datastore (!)
318 void GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
321 struct GNUNET_DATASTORE_QueueEntry *qe;
323 if (h->client != NULL)
325 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
328 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
330 GNUNET_SCHEDULER_cancel (h->sched,
332 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
334 while (NULL != (qe = h->queue_head))
336 GNUNET_assert (NULL != qe->response_proc);
337 qe->response_proc (qe, NULL);
339 if (GNUNET_YES == drop)
341 h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
342 if (h->client != NULL)
345 GNUNET_CLIENT_notify_transmit_ready (h->client,
346 sizeof(struct GNUNET_MessageHeader),
347 GNUNET_TIME_UNIT_MINUTES,
352 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
356 GNUNET_STATISTICS_destroy (h->stats,
363 * A request has timed out (before being transmitted to the service).
365 * @param cls the 'struct GNUNET_DATASTORE_QueueEntry'
366 * @param tc scheduler context
369 timeout_queue_entry (void *cls,
370 const struct GNUNET_SCHEDULER_TaskContext *tc)
372 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
374 GNUNET_STATISTICS_update (qe->h->stats,
375 gettext_noop ("# queue entry timeouts"),
378 qe->task = GNUNET_SCHEDULER_NO_TASK;
379 GNUNET_assert (qe->was_transmitted == GNUNET_NO);
380 qe->response_proc (qe, NULL);
385 * Create a new entry for our priority queue (and possibly discard other entires if
386 * the queue is getting too long).
388 * @param h handle to the datastore
389 * @param msize size of the message to queue
390 * @param queue_priority priority of the entry
391 * @param max_queue_size at what queue size should this request be dropped
392 * (if other requests of higher priority are in the queue)
393 * @param timeout timeout for the operation
394 * @param response_proc function to call with replies (can be NULL)
395 * @param qc client context (NOT a closure for response_proc)
396 * @return NULL if the queue is full (and this entry was dropped)
398 static struct GNUNET_DATASTORE_QueueEntry *
399 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
401 unsigned int queue_priority,
402 unsigned int max_queue_size,
403 struct GNUNET_TIME_Relative timeout,
404 GNUNET_CLIENT_MessageHandler response_proc,
405 const union QueueContext *qc)
407 struct GNUNET_DATASTORE_QueueEntry *ret;
408 struct GNUNET_DATASTORE_QueueEntry *pos;
413 while ( (pos != NULL) &&
414 (c < max_queue_size) &&
415 (pos->priority >= queue_priority) )
420 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
422 ret->response_proc = response_proc;
424 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
425 ret->priority = queue_priority;
426 ret->max_queue = max_queue_size;
427 ret->message_size = msize;
428 ret->was_transmitted = GNUNET_NO;
431 /* append at the tail */
437 /* do not insert at HEAD if HEAD query was already
438 transmitted and we are still receiving replies! */
439 if ( (pos == NULL) &&
440 (h->queue_head->was_transmitted) )
444 GNUNET_STATISTICS_update (h->stats,
445 gettext_noop ("# queue entries created"),
448 GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
453 if (c > max_queue_size)
455 GNUNET_STATISTICS_update (h->stats,
456 gettext_noop ("# queue overflows"),
459 response_proc (ret, NULL);
462 ret->task = GNUNET_SCHEDULER_add_delayed (h->sched,
464 &timeout_queue_entry,
469 if (pos->max_queue < h->queue_size)
471 GNUNET_assert (pos->response_proc != NULL);
472 pos->response_proc (pos, 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.value < GNUNET_CONSTANTS_SERVICE_RETRY.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.value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.value)
508 h->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
509 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
510 h->client = GNUNET_CLIENT_connect (h->sched, "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);
554 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->sched,
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 (h->sched,
606 qe->task = GNUNET_SCHEDULER_NO_TASK;
607 h->in_receive = GNUNET_YES;
608 GNUNET_CLIENT_receive (h->client,
611 GNUNET_TIME_absolute_get_remaining (qe->timeout));
612 GNUNET_STATISTICS_update (h->stats,
613 gettext_noop ("# bytes sent to datastore"),
621 * Process entries in the queue (or do nothing if we are already
624 * @param h handle to the datastore
627 process_queue (struct GNUNET_DATASTORE_Handle *h)
629 struct GNUNET_DATASTORE_QueueEntry *qe;
631 if (NULL == (qe = h->queue_head))
633 #if DEBUG_DATASTORE > 1
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
637 return; /* no entry in queue */
639 if (qe->was_transmitted == GNUNET_YES)
641 #if DEBUG_DATASTORE > 1
642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
643 "Head request already transmitted\n");
645 return; /* waiting for replies */
649 #if DEBUG_DATASTORE > 1
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
651 "Pending transmission request\n");
653 return; /* request pending */
655 if (h->client == NULL)
657 #if DEBUG_DATASTORE > 1
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
661 return; /* waiting for reconnect */
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
665 "Queueing %u byte request to DATASTORE\n",
668 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
670 GNUNET_TIME_absolute_get_remaining (qe->timeout),
678 * Dummy continuation used to do nothing (but be non-zero).
681 * @param result result
682 * @param emsg error message
685 drop_status_cont (void *cls, int result, const char *emsg)
692 free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
694 struct GNUNET_DATASTORE_Handle *h = qe->h;
696 GNUNET_CONTAINER_DLL_remove (h->queue_head,
699 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
701 GNUNET_SCHEDULER_cancel (h->sched,
703 qe->task = GNUNET_SCHEDULER_NO_TASK;
710 * Type of a function to call when we receive a message
714 * @param msg message received, NULL on timeout or fatal error
717 process_status_message (void *cls,
719 GNUNET_MessageHeader * msg)
721 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
722 struct GNUNET_DATASTORE_Handle *h = qe->h;
723 struct StatusContext rc = qe->qc.sc;
724 const struct StatusMessage *sm;
729 h->in_receive = GNUNET_NO;
730 was_transmitted = qe->was_transmitted;
733 free_queue_entry (qe);
734 if (NULL == h->client)
735 return; /* forced disconnect */
737 rc.cont (rc.cont_cls,
739 _("Failed to receive status response from database."));
740 if (was_transmitted == GNUNET_YES)
744 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
745 GNUNET_assert (h->queue_head == qe);
746 free_queue_entry (qe);
747 if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
748 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) )
751 h->retry_time = GNUNET_TIME_UNIT_ZERO;
754 rc.cont (rc.cont_cls,
756 _("Error reading response from datastore service"));
759 sm = (const struct StatusMessage*) msg;
760 status = ntohl(sm->status);
762 if (ntohs(msg->size) > sizeof(struct StatusMessage))
764 emsg = (const char*) &sm[1];
765 if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
768 emsg = _("Invalid error message received from datastore service");
771 if ( (status == GNUNET_SYSERR) &&
775 emsg = _("Invalid error message received from datastore service");
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
779 "Received status %d/%s\n",
783 GNUNET_STATISTICS_update (h->stats,
784 gettext_noop ("# status messages received"),
787 h->retry_time.value = 0;
790 rc.cont (rc.cont_cls,
797 * Store an item in the datastore. If the item is already present,
798 * the priorities are summed up and the higher expiration time and
799 * lower anonymity level is used.
801 * @param h handle to the datastore
802 * @param rid reservation ID to use (from "reserve"); use 0 if no
803 * prior reservation was made
804 * @param key key for the value
805 * @param size number of bytes in data
806 * @param data content stored
807 * @param type type of the content
808 * @param priority priority of the content
809 * @param anonymity anonymity-level for the content
810 * @param expiration expiration time for the content
811 * @param queue_priority ranking of this request in the priority queue
812 * @param max_queue_size at what queue size should this request be dropped
813 * (if other requests of higher priority are in the queue)
814 * @param timeout timeout for the operation
815 * @param cont continuation to call when done
816 * @param cont_cls closure for cont
817 * @return NULL if the entry was not queued, otherwise a handle that can be used to
818 * cancel; note that even if NULL is returned, the callback will be invoked
819 * (or rather, will already have been invoked)
821 struct GNUNET_DATASTORE_QueueEntry *
822 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
824 const GNUNET_HashCode * key,
827 enum GNUNET_BLOCK_Type type,
830 struct GNUNET_TIME_Absolute expiration,
831 unsigned int queue_priority,
832 unsigned int max_queue_size,
833 struct GNUNET_TIME_Relative timeout,
834 GNUNET_DATASTORE_ContinuationWithStatus cont,
837 struct GNUNET_DATASTORE_QueueEntry *qe;
838 struct DataMessage *dm;
840 union QueueContext qc;
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844 "Asked to put %u bytes of data under key `%s'\n",
848 msize = sizeof(struct DataMessage) + size;
849 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
851 qc.sc.cont_cls = cont_cls;
852 qe = make_queue_entry (h, msize,
853 queue_priority, max_queue_size, timeout,
854 &process_status_message, &qc);
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Could not create queue entry for PUT\n");
863 GNUNET_STATISTICS_update (h->stats,
864 gettext_noop ("# PUT requests executed"),
867 dm = (struct DataMessage* ) &qe[1];
868 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
869 dm->header.size = htons(msize);
870 dm->rid = htonl(rid);
871 dm->size = htonl( (uint32_t) size);
872 dm->type = htonl(type);
873 dm->priority = htonl(priority);
874 dm->anonymity = htonl(anonymity);
875 dm->uid = GNUNET_htonll(0);
876 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
878 memcpy (&dm[1], data, size);
885 * Reserve space in the datastore. This function should be used
886 * to avoid "out of space" failures during a longer sequence of "put"
887 * operations (for example, when a file is being inserted).
889 * @param h handle to the datastore
890 * @param amount how much space (in bytes) should be reserved (for content only)
891 * @param entries how many entries will be created (to calculate per-entry overhead)
892 * @param queue_priority ranking of this request in the priority queue
893 * @param max_queue_size at what queue size should this request be dropped
894 * (if other requests of higher priority are in the queue)
895 * @param timeout how long to wait at most for a response (or before dying in queue)
896 * @param cont continuation to call when done; "success" will be set to
897 * a positive reservation value if space could be reserved.
898 * @param cont_cls closure for cont
899 * @return NULL if the entry was not queued, otherwise a handle that can be used to
900 * cancel; note that even if NULL is returned, the callback will be invoked
901 * (or rather, will already have been invoked)
903 struct GNUNET_DATASTORE_QueueEntry *
904 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
907 unsigned int queue_priority,
908 unsigned int max_queue_size,
909 struct GNUNET_TIME_Relative timeout,
910 GNUNET_DATASTORE_ContinuationWithStatus cont,
913 struct GNUNET_DATASTORE_QueueEntry *qe;
914 struct ReserveMessage *rm;
915 union QueueContext qc;
918 cont = &drop_status_cont;
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921 "Asked to reserve %llu bytes of data and %u entries'\n",
922 (unsigned long long) amount,
923 (unsigned int) entries);
926 qc.sc.cont_cls = cont_cls;
927 qe = make_queue_entry (h, sizeof(struct ReserveMessage),
928 queue_priority, max_queue_size, timeout,
929 &process_status_message, &qc);
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
934 "Could not create queue entry to reserve\n");
938 GNUNET_STATISTICS_update (h->stats,
939 gettext_noop ("# RESERVE requests executed"),
942 rm = (struct ReserveMessage*) &qe[1];
943 rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
944 rm->header.size = htons(sizeof (struct ReserveMessage));
945 rm->entries = htonl(entries);
946 rm->amount = GNUNET_htonll(amount);
953 * Signal that all of the data for which a reservation was made has
954 * been stored and that whatever excess space might have been reserved
955 * can now be released.
957 * @param h handle to the datastore
958 * @param rid reservation ID (value of "success" in original continuation
959 * from the "reserve" function).
960 * @param queue_priority ranking of this request in the priority queue
961 * @param max_queue_size at what queue size should this request be dropped
962 * (if other requests of higher priority are in the queue)
963 * @param queue_priority ranking of this request in the priority queue
964 * @param max_queue_size at what queue size should this request be dropped
965 * (if other requests of higher priority are in the queue)
966 * @param timeout how long to wait at most for a response
967 * @param cont continuation to call when done
968 * @param cont_cls closure for cont
969 * @return NULL if the entry was not queued, otherwise a handle that can be used to
970 * cancel; note that even if NULL is returned, the callback will be invoked
971 * (or rather, will already have been invoked)
973 struct GNUNET_DATASTORE_QueueEntry *
974 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
976 unsigned int queue_priority,
977 unsigned int max_queue_size,
978 struct GNUNET_TIME_Relative timeout,
979 GNUNET_DATASTORE_ContinuationWithStatus cont,
982 struct GNUNET_DATASTORE_QueueEntry *qe;
983 struct ReleaseReserveMessage *rrm;
984 union QueueContext qc;
987 cont = &drop_status_cont;
989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
990 "Asked to release reserve %d\n",
994 qc.sc.cont_cls = cont_cls;
995 qe = make_queue_entry (h, sizeof(struct ReleaseReserveMessage),
996 queue_priority, max_queue_size, timeout,
997 &process_status_message, &qc);
1001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1002 "Could not create queue entry to release reserve\n");
1006 GNUNET_STATISTICS_update (h->stats,
1007 gettext_noop ("# RELEASE RESERVE requests executed"),
1010 rrm = (struct ReleaseReserveMessage*) &qe[1];
1011 rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
1012 rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
1013 rrm->rid = htonl(rid);
1020 * Update a value in the datastore.
1022 * @param h handle to the datastore
1023 * @param uid identifier for the value
1024 * @param priority how much to increase the priority of the value
1025 * @param expiration new expiration value should be MAX of existing and this argument
1026 * @param queue_priority ranking of this request in the priority queue
1027 * @param max_queue_size at what queue size should this request be dropped
1028 * (if other requests of higher priority are in the queue)
1029 * @param timeout how long to wait at most for a response
1030 * @param cont continuation to call when done
1031 * @param cont_cls closure for cont
1032 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1033 * cancel; note that even if NULL is returned, the callback will be invoked
1034 * (or rather, will already have been invoked)
1036 struct GNUNET_DATASTORE_QueueEntry *
1037 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
1038 unsigned long long uid,
1040 struct GNUNET_TIME_Absolute expiration,
1041 unsigned int queue_priority,
1042 unsigned int max_queue_size,
1043 struct GNUNET_TIME_Relative timeout,
1044 GNUNET_DATASTORE_ContinuationWithStatus cont,
1047 struct GNUNET_DATASTORE_QueueEntry *qe;
1048 struct UpdateMessage *um;
1049 union QueueContext qc;
1052 cont = &drop_status_cont;
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1055 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
1057 (unsigned int) priority,
1058 (unsigned long long) expiration.value);
1061 qc.sc.cont_cls = cont_cls;
1062 qe = make_queue_entry (h, sizeof(struct UpdateMessage),
1063 queue_priority, max_queue_size, timeout,
1064 &process_status_message, &qc);
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1069 "Could not create queue entry for UPDATE\n");
1073 GNUNET_STATISTICS_update (h->stats,
1074 gettext_noop ("# UPDATE requests executed"),
1077 um = (struct UpdateMessage*) &qe[1];
1078 um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
1079 um->header.size = htons(sizeof (struct UpdateMessage));
1080 um->priority = htonl(priority);
1081 um->expiration = GNUNET_TIME_absolute_hton(expiration);
1082 um->uid = GNUNET_htonll(uid);
1089 * Explicitly remove some content from the database.
1090 * The "cont"inuation will be called with status
1091 * "GNUNET_OK" if content was removed, "GNUNET_NO"
1092 * if no matching entry was found and "GNUNET_SYSERR"
1093 * on all other types of errors.
1095 * @param h handle to the datastore
1096 * @param key key for the value
1097 * @param size number of bytes in data
1098 * @param data content stored
1099 * @param queue_priority ranking of this request in the priority queue
1100 * @param max_queue_size at what queue size should this request be dropped
1101 * (if other requests of higher priority are in the queue)
1102 * @param timeout how long to wait at most for a response
1103 * @param cont continuation to call when done
1104 * @param cont_cls closure for cont
1105 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1106 * cancel; note that even if NULL is returned, the callback will be invoked
1107 * (or rather, will already have been invoked)
1109 struct GNUNET_DATASTORE_QueueEntry *
1110 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
1111 const GNUNET_HashCode *key,
1114 unsigned int queue_priority,
1115 unsigned int max_queue_size,
1116 struct GNUNET_TIME_Relative timeout,
1117 GNUNET_DATASTORE_ContinuationWithStatus cont,
1120 struct GNUNET_DATASTORE_QueueEntry *qe;
1121 struct DataMessage *dm;
1123 union QueueContext qc;
1126 cont = &drop_status_cont;
1128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1129 "Asked to remove %u bytes under key `%s'\n",
1134 qc.sc.cont_cls = cont_cls;
1135 msize = sizeof(struct DataMessage) + size;
1136 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1137 qe = make_queue_entry (h, msize,
1138 queue_priority, max_queue_size, timeout,
1139 &process_status_message, &qc);
1143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1144 "Could not create queue entry for REMOVE\n");
1148 GNUNET_STATISTICS_update (h->stats,
1149 gettext_noop ("# REMOVE requests executed"),
1152 dm = (struct DataMessage*) &qe[1];
1153 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
1154 dm->header.size = htons(msize);
1156 dm->size = htonl(size);
1157 dm->type = htonl(0);
1158 dm->priority = htonl(0);
1159 dm->anonymity = htonl(0);
1160 dm->uid = GNUNET_htonll(0);
1161 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
1163 memcpy (&dm[1], data, size);
1170 * Type of a function to call when we receive a message
1173 * @param cls closure
1174 * @param msg message received, NULL on timeout or fatal error
1177 process_result_message (void *cls,
1178 const struct GNUNET_MessageHeader * msg)
1180 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
1181 struct GNUNET_DATASTORE_Handle *h = qe->h;
1182 struct ResultContext rc = qe->qc.rc;
1183 const struct DataMessage *dm;
1184 int was_transmitted;
1186 h->in_receive = GNUNET_NO;
1189 was_transmitted = qe->was_transmitted;
1190 free_queue_entry (qe);
1191 if (was_transmitted == GNUNET_YES)
1193 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1194 _("Failed to receive response from database.\n"));
1200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1201 "Request dropped due to finite datastore queue length.\n");
1204 if (rc.iter != NULL)
1205 rc.iter (rc.iter_cls,
1206 NULL, 0, NULL, 0, 0, 0,
1207 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1210 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1211 GNUNET_assert (h->queue_head == qe);
1212 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1214 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
1216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1217 "Received end of result set\n");
1219 free_queue_entry (qe);
1220 if (rc.iter != NULL)
1221 rc.iter (rc.iter_cls,
1222 NULL, 0, NULL, 0, 0, 0,
1223 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1224 h->retry_time.value = 0;
1225 h->result_count = 0;
1229 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
1230 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1231 (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
1234 free_queue_entry (qe);
1235 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1237 if (rc.iter != NULL)
1238 rc.iter (rc.iter_cls,
1239 NULL, 0, NULL, 0, 0, 0,
1240 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1243 GNUNET_STATISTICS_update (h->stats,
1244 gettext_noop ("# Results received"),
1247 if (rc.iter == NULL)
1250 GNUNET_STATISTICS_update (h->stats,
1251 gettext_noop ("# Excess results received"),
1254 if (h->result_count > MAX_EXCESS_RESULTS)
1256 free_queue_entry (qe);
1257 GNUNET_STATISTICS_update (h->stats,
1258 gettext_noop ("# Forced database connection resets"),
1261 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1265 GNUNET_DATASTORE_get_next (h, GNUNET_NO);
1268 dm = (const struct DataMessage*) msg;
1270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1271 "Received result %llu with type %u and size %u with key %s\n",
1272 (unsigned long long) GNUNET_ntohll(dm->uid),
1275 GNUNET_h2s(&dm->key));
1277 h->retry_time.value = 0;
1278 rc.iter (rc.iter_cls,
1283 ntohl(dm->priority),
1284 ntohl(dm->anonymity),
1285 GNUNET_TIME_absolute_ntoh(dm->expiration),
1286 GNUNET_ntohll(dm->uid));
1291 * Get a random value from the datastore.
1293 * @param h handle to the datastore
1294 * @param queue_priority ranking of this request in the priority queue
1295 * @param max_queue_size at what queue size should this request be dropped
1296 * (if other requests of higher priority are in the queue)
1297 * @param timeout how long to wait at most for a response
1298 * @param iter function to call on a random value; it
1299 * will be called once with a value (if available)
1300 * and always once with a value of NULL.
1301 * @param iter_cls closure for iter
1302 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1303 * cancel; note that even if NULL is returned, the callback will be invoked
1304 * (or rather, will already have been invoked)
1306 struct GNUNET_DATASTORE_QueueEntry *
1307 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
1308 unsigned int queue_priority,
1309 unsigned int max_queue_size,
1310 struct GNUNET_TIME_Relative timeout,
1311 GNUNET_DATASTORE_Iterator iter,
1314 struct GNUNET_DATASTORE_QueueEntry *qe;
1315 struct GNUNET_MessageHeader *m;
1316 union QueueContext qc;
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1320 "Asked to get random entry in %llu ms\n",
1321 (unsigned long long) timeout.value);
1324 qc.rc.iter_cls = iter_cls;
1325 qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader),
1326 queue_priority, max_queue_size, timeout,
1327 &process_result_message, &qc);
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1332 "Could not create queue entry for GET RANDOM\n");
1336 GNUNET_STATISTICS_update (h->stats,
1337 gettext_noop ("# GET RANDOM requests executed"),
1340 m = (struct GNUNET_MessageHeader*) &qe[1];
1341 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
1342 m->size = htons(sizeof (struct GNUNET_MessageHeader));
1349 * Get a zero-anonymity value from the datastore.
1351 * @param h handle to the datastore
1352 * @param queue_priority ranking of this request in the priority queue
1353 * @param max_queue_size at what queue size should this request be dropped
1354 * (if other requests of higher priority are in the queue)
1355 * @param timeout how long to wait at most for a response
1356 * @param type allowed type for the operation
1357 * @param iter function to call on a random value; it
1358 * will be called once with a value (if available)
1359 * and always once with a value of NULL.
1360 * @param iter_cls closure for iter
1361 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1362 * cancel; note that even if NULL is returned, the callback will be invoked
1363 * (or rather, will already have been invoked)
1365 struct GNUNET_DATASTORE_QueueEntry *
1366 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
1367 unsigned int queue_priority,
1368 unsigned int max_queue_size,
1369 struct GNUNET_TIME_Relative timeout,
1370 enum GNUNET_BLOCK_Type type,
1371 GNUNET_DATASTORE_Iterator iter,
1374 struct GNUNET_DATASTORE_QueueEntry *qe;
1375 struct GetZeroAnonymityMessage *m;
1376 union QueueContext qc;
1379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1380 "Asked to get zero-anonymity entry in %llu ms\n",
1381 (unsigned long long) timeout.value);
1384 qc.rc.iter_cls = iter_cls;
1385 qe = make_queue_entry (h, sizeof(struct GetZeroAnonymityMessage),
1386 queue_priority, max_queue_size, timeout,
1387 &process_result_message, &qc);
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1392 "Could not create queue entry for zero-anonymity iteration\n");
1396 GNUNET_STATISTICS_update (h->stats,
1397 gettext_noop ("# GET ZERO ANONYMITY requests executed"),
1400 m = (struct GetZeroAnonymityMessage*) &qe[1];
1401 m->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY);
1402 m->header.size = htons(sizeof (struct GetZeroAnonymityMessage));
1403 m->type = htonl ((uint32_t) type);
1411 * Iterate over the results for a particular key
1412 * in the datastore. The iterator will only be called
1413 * once initially; if the first call did contain a
1414 * result, further results can be obtained by calling
1415 * "GNUNET_DATASTORE_get_next" with the given argument.
1417 * @param h handle to the datastore
1418 * @param key maybe NULL (to match all entries)
1419 * @param type desired type, 0 for any
1420 * @param queue_priority ranking of this request in the priority queue
1421 * @param max_queue_size at what queue size should this request be dropped
1422 * (if other requests of higher priority are in the queue)
1423 * @param timeout how long to wait at most for a response
1424 * @param iter function to call on each matching value;
1425 * will be called once with a NULL value at the end
1426 * @param iter_cls closure for iter
1427 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1428 * cancel; note that even if NULL is returned, the callback will be invoked
1429 * (or rather, will already have been invoked)
1431 struct GNUNET_DATASTORE_QueueEntry *
1432 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
1433 const GNUNET_HashCode * key,
1434 enum GNUNET_BLOCK_Type type,
1435 unsigned int queue_priority,
1436 unsigned int max_queue_size,
1437 struct GNUNET_TIME_Relative timeout,
1438 GNUNET_DATASTORE_Iterator iter,
1441 struct GNUNET_DATASTORE_QueueEntry *qe;
1442 struct GetMessage *gm;
1443 union QueueContext qc;
1446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1447 "Asked to look for data of type %u under key `%s'\n",
1448 (unsigned int) type,
1452 qc.rc.iter_cls = iter_cls;
1453 qe = make_queue_entry (h, sizeof(struct GetMessage),
1454 queue_priority, max_queue_size, timeout,
1455 &process_result_message, &qc);
1459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1460 "Could not queue request for `%s'\n",
1465 GNUNET_STATISTICS_update (h->stats,
1466 gettext_noop ("# GET requests executed"),
1469 gm = (struct GetMessage*) &qe[1];
1470 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1471 gm->type = htonl(type);
1474 gm->header.size = htons(sizeof (struct GetMessage));
1479 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
1487 * Function called to trigger obtaining the next result
1488 * from the datastore.
1490 * @param h handle to the datastore
1491 * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
1492 * iteration (with a final call to "iter" with key/data == NULL).
1495 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
1498 struct GNUNET_DATASTORE_QueueEntry *qe = h->queue_head;
1499 struct ResultContext rc = qe->qc.rc;
1501 GNUNET_assert (&process_result_message == qe->response_proc);
1502 if (GNUNET_YES != more)
1504 qe->qc.rc.iter = NULL;
1505 qe->qc.rc.iter_cls = NULL;
1506 if (rc.iter != NULL)
1507 rc.iter (rc.iter_cls,
1508 NULL, 0, NULL, 0, 0, 0,
1509 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1511 h->in_receive = GNUNET_YES;
1512 GNUNET_CLIENT_receive (h->client,
1515 GNUNET_TIME_absolute_get_remaining (qe->timeout));
1520 * Cancel a datastore operation. The final callback from the
1521 * operation must not have been done yet.
1523 * @param qe operation to cancel
1526 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1528 struct GNUNET_DATASTORE_Handle *h;
1533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1534 "Pending DATASTORE request %p cancelled (%d, %d)\n",
1536 qe->was_transmitted,
1537 h->queue_head == qe);
1539 reconnect = GNUNET_NO;
1540 if (GNUNET_YES == qe->was_transmitted)
1542 if (qe->response_proc == &process_result_message)
1544 qe->qc.rc.iter = NULL;
1545 if (GNUNET_YES != h->in_receive)
1546 GNUNET_DATASTORE_get_next (h, GNUNET_YES);
1550 qe->qc.sc.cont = NULL;
1554 free_queue_entry (qe);
1559 /* end of datastore_api.c */