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 2, 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 "datastore.h"
34 * Entry in our priority queue.
36 struct GNUNET_DATASTORE_QueueEntry
40 * This is a linked list.
42 struct GNUNET_DATASTORE_QueueEntry *next;
45 * This is a linked list.
47 struct GNUNET_DATASTORE_QueueEntry *prev;
50 * Handle to the master context.
52 struct GNUNET_DATASTORE_Handle *h;
55 * Response processor (NULL if we are not waiting for a response).
56 * This struct should be used for the closure, function-specific
57 * arguments can be passed via 'client_ctx'.
59 GNUNET_CLIENT_MessageHandler response_proc;
62 * Specific context (variable argument that
63 * can be used by the response processor).
68 * Function to call after transmission of the request.
70 GNUNET_DATASTORE_ContinuationWithStatus cont;
78 * Task for timeout signalling.
80 GNUNET_SCHEDULER_TaskIdentifier task;
83 * Timeout for the current operation.
85 struct GNUNET_TIME_Absolute timeout;
88 * Priority in the queue.
90 unsigned int priority;
93 * Maximum allowed length of queue (otherwise
94 * this request should be discarded).
96 unsigned int max_queue;
99 * Number of bytes in the request message following
100 * this struct. 32-bit value for nicer memory
101 * access (and overall struct alignment).
103 uint32_t message_size;
106 * Has this message been transmitted to the service?
107 * Only ever GNUNET_YES for the head of the queue.
108 * Note that the overall struct should end at a
109 * multiple of 64 bits.
111 int32_t was_transmitted;
116 * Handle to the datastore service.
118 struct GNUNET_DATASTORE_Handle
124 const struct GNUNET_CONFIGURATION_Handle *cfg;
129 struct GNUNET_SCHEDULER_Handle *sched;
132 * Current connection to the datastore service.
134 struct GNUNET_CLIENT_Connection *client;
137 * Current transmit handle.
139 struct GNUNET_CLIENT_TransmitHandle *th;
142 * Current head of priority queue.
144 struct GNUNET_DATASTORE_QueueEntry *queue_head;
147 * Current tail of priority queue.
149 struct GNUNET_DATASTORE_QueueEntry *queue_tail;
152 * Task for trying to reconnect.
154 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
157 * How quickly should we retry? Used for exponential back-off on
160 struct GNUNET_TIME_Relative retry_time;
163 * Number of entries in the queue.
165 unsigned int queue_size;
172 * Connect to the datastore service.
174 * @param cfg configuration to use
175 * @param sched scheduler to use
176 * @return handle to use to access the service
178 struct GNUNET_DATASTORE_Handle *
179 GNUNET_DATASTORE_connect (const struct
180 GNUNET_CONFIGURATION_Handle
183 GNUNET_SCHEDULER_Handle
186 struct GNUNET_CLIENT_Connection *c;
187 struct GNUNET_DATASTORE_Handle *h;
189 c = GNUNET_CLIENT_connect (sched, "datastore", cfg);
191 return NULL; /* oops */
192 h = GNUNET_malloc (sizeof(struct GNUNET_DATASTORE_Handle) +
193 GNUNET_SERVER_MAX_MESSAGE_SIZE);
202 * Transmit DROP message to datastore service.
204 * @param cls the 'struct GNUNET_DATASTORE_Handle'
205 * @param size number of bytes that can be copied to buf
206 * @param buf where to copy the drop message
207 * @return number of bytes written to buf
210 transmit_drop (void *cls,
214 struct GNUNET_DATASTORE_Handle *h = cls;
215 struct GNUNET_MessageHeader *hdr;
219 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
220 _("Failed to transmit request to drop database.\n"));
221 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
224 GNUNET_assert (size >= sizeof(struct GNUNET_MessageHeader));
226 hdr->size = htons(sizeof(struct GNUNET_MessageHeader));
227 hdr->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
228 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
229 return sizeof(struct GNUNET_MessageHeader);
234 * Disconnect from the datastore service (and free
235 * associated resources).
237 * @param h handle to the datastore
238 * @param drop set to GNUNET_YES to delete all data in datastore (!)
240 void GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
243 struct GNUNET_DATASTORE_QueueEntry *qe;
245 if (h->client != NULL)
247 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
250 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
252 GNUNET_SCHEDULER_cancel (h->sched,
254 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
256 while (NULL != (qe = h->queue_head))
258 if (NULL != qe->response_proc)
260 qe->response_proc (qe, NULL);
264 GNUNET_CONTAINER_DLL_remove (h->queue_head,
267 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
268 GNUNET_SCHEDULER_cancel (h->sched,
273 if (GNUNET_YES == drop)
275 h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
276 if (h->client != NULL)
279 GNUNET_CLIENT_notify_transmit_ready (h->client,
280 sizeof(struct GNUNET_MessageHeader),
281 GNUNET_TIME_UNIT_MINUTES,
286 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
295 * A request has timed out (before being transmitted to the service).
297 * @param cls the 'struct GNUNET_DATASTORE_QueueEntry'
298 * @param tc scheduler context
301 timeout_queue_entry (void *cls,
302 const struct GNUNET_SCHEDULER_TaskContext *tc)
304 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
306 qe->task = GNUNET_SCHEDULER_NO_TASK;
307 GNUNET_assert (qe->was_transmitted == GNUNET_NO);
308 qe->response_proc (qe, NULL);
313 * Create a new entry for our priority queue (and possibly discard other entires if
314 * the queue is getting too long).
316 * @param h handle to the datastore
317 * @param msize size of the message to queue
318 * @param queue_priority priority of the entry
319 * @param max_queue_size at what queue size should this request be dropped
320 * (if other requests of higher priority are in the queue)
321 * @param timeout timeout for the operation
322 * @param response_proc function to call with replies (can be NULL)
323 * @param client_ctx client context (NOT a closure for response_proc)
324 * @return NULL if the queue is full (and this entry was dropped)
326 static struct GNUNET_DATASTORE_QueueEntry *
327 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
329 unsigned int queue_priority,
330 unsigned int max_queue_size,
331 struct GNUNET_TIME_Relative timeout,
332 GNUNET_CLIENT_MessageHandler response_proc,
335 struct GNUNET_DATASTORE_QueueEntry *ret;
336 struct GNUNET_DATASTORE_QueueEntry *pos;
341 while ( (pos != NULL) &&
342 (c < max_queue_size) &&
343 (pos->priority >= queue_priority) )
348 if (c >= max_queue_size)
352 /* append at the tail */
358 /* do not insert at HEAD if HEAD query was already
359 transmitted and we are still receiving replies! */
360 if ( (pos == NULL) &&
361 (h->queue_head->was_transmitted) )
364 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
365 GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
370 ret->response_proc = response_proc;
371 ret->client_ctx = client_ctx;
372 ret->task = GNUNET_SCHEDULER_add_delayed (h->sched,
374 &timeout_queue_entry,
376 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
377 ret->priority = queue_priority;
378 ret->max_queue = max_queue_size;
379 ret->message_size = msize;
380 ret->was_transmitted = GNUNET_NO;
386 if (pos->max_queue < h->queue_size)
388 GNUNET_CONTAINER_DLL_remove (h->queue_head,
391 GNUNET_SCHEDULER_cancel (h->sched,
393 if (pos->response_proc != NULL)
394 pos->response_proc (pos, NULL);
406 * Process entries in the queue (or do nothing if we are already
409 * @param h handle to the datastore
412 process_queue (struct GNUNET_DATASTORE_Handle *h);
416 * Try reconnecting to the datastore service.
418 * @param cls the 'struct GNUNET_DATASTORE_Handle'
419 * @param tc scheduler context
422 try_reconnect (void *cls,
423 const struct GNUNET_SCHEDULER_TaskContext *tc)
425 struct GNUNET_DATASTORE_Handle *h = cls;
427 if (h->retry_time.value < GNUNET_CONSTANTS_SERVICE_RETRY.value)
428 h->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
430 h->retry_time = GNUNET_TIME_relative_multiply (h->retry_time, 2);
431 if (h->retry_time.value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.value)
432 h->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
433 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
434 h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
435 if (h->client == NULL)
442 * Disconnect from the service and then try reconnecting to the datastore service
445 * @param cls the 'struct GNUNET_DATASTORE_Handle'
446 * @param tc scheduler context
449 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
451 if (h->client == NULL)
453 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
455 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->sched,
463 * Transmit request from queue to datastore service.
465 * @param cls the 'struct GNUNET_DATASTORE_Handle'
466 * @param size number of bytes that can be copied to buf
467 * @param buf where to copy the drop message
468 * @return number of bytes written to buf
471 transmit_request (void *cls,
475 struct GNUNET_DATASTORE_Handle *h = cls;
476 struct GNUNET_DATASTORE_QueueEntry *qe;
480 if (NULL == (qe = h->queue_head))
481 return 0; /* no entry in queue */
484 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
485 _("Failed to transmit request to database.\n"));
489 if (size < (msize = qe->message_size))
494 memcpy (buf, &qe[1], msize);
495 qe->was_transmitted = GNUNET_YES;
496 GNUNET_SCHEDULER_cancel (h->sched,
498 qe->task = GNUNET_SCHEDULER_NO_TASK;
499 GNUNET_CLIENT_receive (h->client,
502 GNUNET_TIME_absolute_get_remaining (qe->timeout));
508 * Process entries in the queue (or do nothing if we are already
511 * @param h handle to the datastore
514 process_queue (struct GNUNET_DATASTORE_Handle *h)
516 struct GNUNET_DATASTORE_QueueEntry *qe;
518 if (NULL == (qe = h->queue_head))
519 return; /* no entry in queue */
520 if (qe->was_transmitted == GNUNET_YES)
521 return; /* waiting for replies */
523 return; /* request pending */
524 if (h->client == NULL)
525 return; /* waiting for reconnect */
526 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
528 GNUNET_TIME_absolute_get_remaining (qe->timeout),
538 * Context for processing status messages.
543 * Continuation to call with the status.
545 GNUNET_DATASTORE_ContinuationWithStatus cont;
556 * Dummy continuation used to do nothing (but be non-zero).
559 * @param result result
560 * @param emsg error message
563 drop_status_cont (void *cls, int result, const char *emsg)
570 * Type of a function to call when we receive a message
574 * @param msg message received, NULL on timeout or fatal error
577 process_status_message (void *cls,
579 GNUNET_MessageHeader * msg)
581 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
582 struct GNUNET_DATASTORE_Handle *h = qe->h;
583 struct StatusContext *rc = qe->client_ctx;
584 const struct StatusMessage *sm;
588 GNUNET_CONTAINER_DLL_remove (h->queue_head,
591 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
593 GNUNET_SCHEDULER_cancel (h->sched,
595 qe->task = GNUNET_SCHEDULER_NO_TASK;
600 if (NULL == h->client)
601 return; /* forced disconnect */
602 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
603 _("Failed to receive response from database.\n"));
608 if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
609 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) )
612 h->retry_time = GNUNET_TIME_UNIT_ZERO;
614 rc->cont (rc->cont_cls,
616 _("Error reading response from datastore service"));
620 sm = (const struct StatusMessage*) msg;
621 status = ntohl(sm->status);
623 if (ntohs(msg->size) > sizeof(struct StatusMessage))
625 emsg = (const char*) &sm[1];
626 if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
629 emsg = _("Invalid error message received from datastore service");
632 if ( (status == GNUNET_SYSERR) &&
636 emsg = _("Invalid error message received from datastore service");
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640 "Received status %d/%s\n",
644 rc->cont (rc->cont_cls,
653 * Store an item in the datastore. If the item is already present,
654 * the priorities are summed up and the higher expiration time and
655 * lower anonymity level is used.
657 * @param h handle to the datastore
658 * @param rid reservation ID to use (from "reserve"); use 0 if no
659 * prior reservation was made
660 * @param key key for the value
661 * @param size number of bytes in data
662 * @param data content stored
663 * @param type type of the content
664 * @param priority priority of the content
665 * @param anonymity anonymity-level for the content
666 * @param expiration expiration time for the content
667 * @param queue_priority ranking of this request in the priority queue
668 * @param max_queue_size at what queue size should this request be dropped
669 * (if other requests of higher priority are in the queue)
670 * @param timeout timeout for the operation
671 * @param cont continuation to call when done
672 * @param cont_cls closure for cont
673 * @return NULL if the entry was not queued, otherwise a handle that can be used to
674 * cancel; note that even if NULL is returned, the callback will be invoked
675 * (or rather, will already have been invoked)
677 struct GNUNET_DATASTORE_QueueEntry *
678 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
680 const GNUNET_HashCode * key,
683 enum GNUNET_BLOCK_Type type,
686 struct GNUNET_TIME_Absolute expiration,
687 unsigned int queue_priority,
688 unsigned int max_queue_size,
689 struct GNUNET_TIME_Relative timeout,
690 GNUNET_DATASTORE_ContinuationWithStatus cont,
693 struct StatusContext *scont;
694 struct GNUNET_DATASTORE_QueueEntry *qe;
695 struct DataMessage *dm;
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
700 "Asked to put %u bytes of data under key `%s'\n",
704 msize = sizeof(struct DataMessage) + size;
705 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
706 scont = GNUNET_malloc (sizeof (struct StatusContext));
708 scont->cont_cls = cont_cls;
709 qe = make_queue_entry (h, msize,
710 queue_priority, max_queue_size, timeout,
711 &process_status_message, scont);
714 dm = (struct DataMessage* ) &qe[1];
715 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
716 dm->header.size = htons(msize);
717 dm->rid = htonl(rid);
718 dm->size = htonl(size);
719 dm->type = htonl(type);
720 dm->priority = htonl(priority);
721 dm->anonymity = htonl(anonymity);
722 dm->uid = GNUNET_htonll(0);
723 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
725 memcpy (&dm[1], data, size);
732 * Reserve space in the datastore. This function should be used
733 * to avoid "out of space" failures during a longer sequence of "put"
734 * operations (for example, when a file is being inserted).
736 * @param h handle to the datastore
737 * @param amount how much space (in bytes) should be reserved (for content only)
738 * @param entries how many entries will be created (to calculate per-entry overhead)
739 * @param queue_priority ranking of this request in the priority queue
740 * @param max_queue_size at what queue size should this request be dropped
741 * (if other requests of higher priority are in the queue)
742 * @param timeout how long to wait at most for a response (or before dying in queue)
743 * @param cont continuation to call when done; "success" will be set to
744 * a positive reservation value if space could be reserved.
745 * @param cont_cls closure for cont
746 * @return NULL if the entry was not queued, otherwise a handle that can be used to
747 * cancel; note that even if NULL is returned, the callback will be invoked
748 * (or rather, will already have been invoked)
750 struct GNUNET_DATASTORE_QueueEntry *
751 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
754 unsigned int queue_priority,
755 unsigned int max_queue_size,
756 struct GNUNET_TIME_Relative timeout,
757 GNUNET_DATASTORE_ContinuationWithStatus cont,
760 struct GNUNET_DATASTORE_QueueEntry *qe;
761 struct ReserveMessage *rm;
762 struct StatusContext *scont;
765 cont = &drop_status_cont;
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768 "Asked to reserve %llu bytes of data and %u entries'\n",
769 (unsigned long long) amount,
770 (unsigned int) entries);
772 scont = GNUNET_malloc (sizeof (struct StatusContext));
774 scont->cont_cls = cont_cls;
775 qe = make_queue_entry (h, sizeof(struct ReserveMessage),
776 queue_priority, max_queue_size, timeout,
777 &process_status_message, scont);
780 rm = (struct ReserveMessage*) &qe[1];
781 rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
782 rm->header.size = htons(sizeof (struct ReserveMessage));
783 rm->entries = htonl(entries);
784 rm->amount = GNUNET_htonll(amount);
791 * Signal that all of the data for which a reservation was made has
792 * been stored and that whatever excess space might have been reserved
793 * can now be released.
795 * @param h handle to the datastore
796 * @param rid reservation ID (value of "success" in original continuation
797 * from the "reserve" function).
798 * @param queue_priority ranking of this request in the priority queue
799 * @param max_queue_size at what queue size should this request be dropped
800 * (if other requests of higher priority are in the queue)
801 * @param queue_priority ranking of this request in the priority queue
802 * @param max_queue_size at what queue size should this request be dropped
803 * (if other requests of higher priority are in the queue)
804 * @param timeout how long to wait at most for a response
805 * @param cont continuation to call when done
806 * @param cont_cls closure for cont
807 * @return NULL if the entry was not queued, otherwise a handle that can be used to
808 * cancel; note that even if NULL is returned, the callback will be invoked
809 * (or rather, will already have been invoked)
811 struct GNUNET_DATASTORE_QueueEntry *
812 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
814 unsigned int queue_priority,
815 unsigned int max_queue_size,
816 struct GNUNET_TIME_Relative timeout,
817 GNUNET_DATASTORE_ContinuationWithStatus cont,
820 struct GNUNET_DATASTORE_QueueEntry *qe;
821 struct ReleaseReserveMessage *rrm;
822 struct StatusContext *scont;
825 cont = &drop_status_cont;
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
828 "Asked to release reserve %d\n",
831 scont = GNUNET_malloc (sizeof (struct StatusContext));
833 scont->cont_cls = cont_cls;
834 qe = make_queue_entry (h, sizeof(struct ReleaseReserveMessage),
835 queue_priority, max_queue_size, timeout,
836 &process_status_message, scont);
839 rrm = (struct ReleaseReserveMessage*) &qe[1];
840 rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
841 rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
842 rrm->rid = htonl(rid);
849 * Update a value in the datastore.
851 * @param h handle to the datastore
852 * @param uid identifier for the value
853 * @param priority how much to increase the priority of the value
854 * @param expiration new expiration value should be MAX of existing and this argument
855 * @param queue_priority ranking of this request in the priority queue
856 * @param max_queue_size at what queue size should this request be dropped
857 * (if other requests of higher priority are in the queue)
858 * @param timeout how long to wait at most for a response
859 * @param cont continuation to call when done
860 * @param cont_cls closure for cont
861 * @return NULL if the entry was not queued, otherwise a handle that can be used to
862 * cancel; note that even if NULL is returned, the callback will be invoked
863 * (or rather, will already have been invoked)
865 struct GNUNET_DATASTORE_QueueEntry *
866 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
867 unsigned long long uid,
869 struct GNUNET_TIME_Absolute expiration,
870 unsigned int queue_priority,
871 unsigned int max_queue_size,
872 struct GNUNET_TIME_Relative timeout,
873 GNUNET_DATASTORE_ContinuationWithStatus cont,
876 struct GNUNET_DATASTORE_QueueEntry *qe;
877 struct UpdateMessage *um;
878 struct StatusContext *scont;
881 cont = &drop_status_cont;
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
884 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
886 (unsigned int) priority,
887 (unsigned long long) expiration.value);
889 scont = GNUNET_malloc (sizeof (struct StatusContext));
891 scont->cont_cls = cont_cls;
892 qe = make_queue_entry (h, sizeof(struct UpdateMessage),
893 queue_priority, max_queue_size, timeout,
894 &process_status_message, scont);
897 um = (struct UpdateMessage*) &qe[1];
898 um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
899 um->header.size = htons(sizeof (struct UpdateMessage));
900 um->priority = htonl(priority);
901 um->expiration = GNUNET_TIME_absolute_hton(expiration);
902 um->uid = GNUNET_htonll(uid);
909 * Explicitly remove some content from the database.
910 * The "cont"inuation will be called with status
911 * "GNUNET_OK" if content was removed, "GNUNET_NO"
912 * if no matching entry was found and "GNUNET_SYSERR"
913 * on all other types of errors.
915 * @param h handle to the datastore
916 * @param key key for the value
917 * @param size number of bytes in data
918 * @param data content stored
919 * @param queue_priority ranking of this request in the priority queue
920 * @param max_queue_size at what queue size should this request be dropped
921 * (if other requests of higher priority are in the queue)
922 * @param timeout how long to wait at most for a response
923 * @param cont continuation to call when done
924 * @param cont_cls closure for cont
925 * @return NULL if the entry was not queued, otherwise a handle that can be used to
926 * cancel; note that even if NULL is returned, the callback will be invoked
927 * (or rather, will already have been invoked)
929 struct GNUNET_DATASTORE_QueueEntry *
930 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
931 const GNUNET_HashCode *key,
934 unsigned int queue_priority,
935 unsigned int max_queue_size,
936 struct GNUNET_TIME_Relative timeout,
937 GNUNET_DATASTORE_ContinuationWithStatus cont,
940 struct GNUNET_DATASTORE_QueueEntry *qe;
941 struct DataMessage *dm;
943 struct StatusContext *scont;
946 cont = &drop_status_cont;
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
949 "Asked to remove %u bytes under key `%s'\n",
953 scont = GNUNET_malloc (sizeof (struct StatusContext));
955 scont->cont_cls = cont_cls;
956 msize = sizeof(struct DataMessage) + size;
957 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
958 qe = make_queue_entry (h, msize,
959 queue_priority, max_queue_size, timeout,
960 &process_status_message, scont);
963 dm = (struct DataMessage*) &qe[1];
964 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
965 dm->header.size = htons(msize);
967 dm->size = htonl(size);
969 dm->priority = htonl(0);
970 dm->anonymity = htonl(0);
971 dm->uid = GNUNET_htonll(0);
972 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
974 memcpy (&dm[1], data, size);
982 * Context for processing result messages.
987 * Iterator to call with the result.
989 GNUNET_DATASTORE_Iterator iter;
1000 * Type of a function to call when we receive a message
1003 * @param cls closure
1004 * @param msg message received, NULL on timeout or fatal error
1007 process_result_message (void *cls,
1008 const struct GNUNET_MessageHeader * msg)
1010 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
1011 struct GNUNET_DATASTORE_Handle *h = qe->h;
1012 struct ResultContext *rc = qe->client_ctx;
1013 const struct DataMessage *dm;
1015 GNUNET_assert (h->queue_head == qe);
1019 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1020 _("Failed to receive response from datastore\n"));
1022 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1027 rc->iter (rc->iter_cls,
1028 NULL, 0, NULL, 0, 0, 0,
1029 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1033 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1035 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1038 "Received end of result set\n");
1040 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1044 rc->iter (rc->iter_cls,
1045 NULL, 0, NULL, 0, 0, 0,
1046 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1051 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
1052 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1053 (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
1056 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1060 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1062 rc->iter (rc->iter_cls,
1063 NULL, 0, NULL, 0, 0, 0,
1064 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1068 dm = (const struct DataMessage*) msg;
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071 "Received result %llu with type %u and size %u with key %s\n",
1072 (unsigned long long) GNUNET_ntohll(dm->uid),
1075 GNUNET_h2s(&dm->key));
1077 rc->iter (rc->iter_cls,
1082 ntohl(dm->priority),
1083 ntohl(dm->anonymity),
1084 GNUNET_TIME_absolute_ntoh(dm->expiration),
1085 GNUNET_ntohll(dm->uid));
1090 * Get a random value from the datastore.
1092 * @param h handle to the datastore
1093 * @param queue_priority ranking of this request in the priority queue
1094 * @param max_queue_size at what queue size should this request be dropped
1095 * (if other requests of higher priority are in the queue)
1096 * @param timeout how long to wait at most for a response
1097 * @param iter function to call on a random value; it
1098 * will be called once with a value (if available)
1099 * and always once with a value of NULL.
1100 * @param iter_cls closure for iter
1101 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1102 * cancel; note that even if NULL is returned, the callback will be invoked
1103 * (or rather, will already have been invoked)
1105 struct GNUNET_DATASTORE_QueueEntry *
1106 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
1107 unsigned int queue_priority,
1108 unsigned int max_queue_size,
1109 struct GNUNET_TIME_Relative timeout,
1110 GNUNET_DATASTORE_Iterator iter,
1113 struct GNUNET_DATASTORE_QueueEntry *qe;
1114 struct GNUNET_MessageHeader *m;
1115 struct ResultContext *rcont;
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1119 "Asked to get random entry in %llu ms\n",
1120 (unsigned long long) timeout.value);
1122 rcont = GNUNET_malloc (sizeof (struct ResultContext));
1124 rcont->iter_cls = iter_cls;
1125 qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader),
1126 queue_priority, max_queue_size, timeout,
1127 &process_result_message, rcont);
1130 m = (struct GNUNET_MessageHeader*) &qe[1];
1131 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
1132 m->size = htons(sizeof (struct GNUNET_MessageHeader));
1140 * Iterate over the results for a particular key
1141 * in the datastore. The iterator will only be called
1142 * once initially; if the first call did contain a
1143 * result, further results can be obtained by calling
1144 * "GNUNET_DATASTORE_get_next" with the given argument.
1146 * @param h handle to the datastore
1147 * @param key maybe NULL (to match all entries)
1148 * @param type desired type, 0 for any
1149 * @param queue_priority ranking of this request in the priority queue
1150 * @param max_queue_size at what queue size should this request be dropped
1151 * (if other requests of higher priority are in the queue)
1152 * @param timeout how long to wait at most for a response
1153 * @param iter function to call on each matching value;
1154 * will be called once with a NULL value at the end
1155 * @param iter_cls closure for iter
1156 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1157 * cancel; note that even if NULL is returned, the callback will be invoked
1158 * (or rather, will already have been invoked)
1160 struct GNUNET_DATASTORE_QueueEntry *
1161 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
1162 const GNUNET_HashCode * key,
1163 enum GNUNET_BLOCK_Type type,
1164 unsigned int queue_priority,
1165 unsigned int max_queue_size,
1166 struct GNUNET_TIME_Relative timeout,
1167 GNUNET_DATASTORE_Iterator iter,
1170 struct GNUNET_DATASTORE_QueueEntry *qe;
1171 struct GetMessage *gm;
1172 struct ResultContext *rcont;
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176 "Asked to look for data of type %u under key `%s'\n",
1177 (unsigned int) type,
1180 rcont = GNUNET_malloc (sizeof (struct ResultContext));
1182 rcont->iter_cls = iter_cls;
1183 qe = make_queue_entry (h, sizeof(struct GetMessage),
1184 queue_priority, max_queue_size, timeout,
1185 &process_result_message, rcont);
1188 gm = (struct GetMessage*) &qe[1];
1189 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1190 gm->type = htonl(type);
1193 gm->header.size = htons(sizeof (struct GetMessage));
1198 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
1206 * Function called to trigger obtaining the next result
1207 * from the datastore.
1209 * @param h handle to the datastore
1210 * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
1211 * iteration (with a final call to "iter" with key/data == NULL).
1214 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
1217 struct GNUNET_DATASTORE_QueueEntry *qe = h->queue_head;
1218 struct ResultContext *rc = qe->client_ctx;
1220 GNUNET_assert (NULL != qe);
1221 GNUNET_assert (&process_result_message == qe->response_proc);
1222 if (GNUNET_YES == more)
1224 GNUNET_CLIENT_receive (h->client,
1227 GNUNET_TIME_absolute_get_remaining (qe->timeout));
1230 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1234 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1236 rc->iter (rc->iter_cls,
1237 NULL, 0, NULL, 0, 0, 0,
1238 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1244 * Cancel a datastore operation. The final callback from the
1245 * operation must not have been done yet.
1247 * @param qe operation to cancel
1250 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1252 struct GNUNET_DATASTORE_Handle *h;
1256 reconnect = qe->was_transmitted;
1257 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1260 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
1261 GNUNET_SCHEDULER_cancel (h->sched,
1266 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1272 /* end of datastore_api.c */