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 h handle to datastore to disconnect and reconnect
448 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
450 if (h->client == NULL)
452 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
454 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->sched,
462 * Transmit request from queue to datastore service.
464 * @param cls the 'struct GNUNET_DATASTORE_Handle'
465 * @param size number of bytes that can be copied to buf
466 * @param buf where to copy the drop message
467 * @return number of bytes written to buf
470 transmit_request (void *cls,
474 struct GNUNET_DATASTORE_Handle *h = cls;
475 struct GNUNET_DATASTORE_QueueEntry *qe;
479 if (NULL == (qe = h->queue_head))
480 return 0; /* no entry in queue */
483 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
484 _("Failed to transmit request to database.\n"));
488 if (size < (msize = qe->message_size))
493 memcpy (buf, &qe[1], msize);
494 qe->was_transmitted = GNUNET_YES;
495 GNUNET_SCHEDULER_cancel (h->sched,
497 qe->task = GNUNET_SCHEDULER_NO_TASK;
498 GNUNET_CLIENT_receive (h->client,
501 GNUNET_TIME_absolute_get_remaining (qe->timeout));
507 * Process entries in the queue (or do nothing if we are already
510 * @param h handle to the datastore
513 process_queue (struct GNUNET_DATASTORE_Handle *h)
515 struct GNUNET_DATASTORE_QueueEntry *qe;
517 if (NULL == (qe = h->queue_head))
518 return; /* no entry in queue */
519 if (qe->was_transmitted == GNUNET_YES)
520 return; /* waiting for replies */
522 return; /* request pending */
523 if (h->client == NULL)
524 return; /* waiting for reconnect */
525 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
527 GNUNET_TIME_absolute_get_remaining (qe->timeout),
537 * Context for processing status messages.
542 * Continuation to call with the status.
544 GNUNET_DATASTORE_ContinuationWithStatus cont;
555 * Dummy continuation used to do nothing (but be non-zero).
558 * @param result result
559 * @param emsg error message
562 drop_status_cont (void *cls, int result, const char *emsg)
569 * Type of a function to call when we receive a message
573 * @param msg message received, NULL on timeout or fatal error
576 process_status_message (void *cls,
578 GNUNET_MessageHeader * msg)
580 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
581 struct GNUNET_DATASTORE_Handle *h = qe->h;
582 struct StatusContext *rc = qe->client_ctx;
583 const struct StatusMessage *sm;
587 GNUNET_CONTAINER_DLL_remove (h->queue_head,
590 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
592 GNUNET_SCHEDULER_cancel (h->sched,
594 qe->task = GNUNET_SCHEDULER_NO_TASK;
599 if (NULL == h->client)
600 return; /* forced disconnect */
601 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
602 _("Failed to receive response from database.\n"));
607 if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
608 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) )
611 h->retry_time = GNUNET_TIME_UNIT_ZERO;
613 rc->cont (rc->cont_cls,
615 _("Error reading response from datastore service"));
619 sm = (const struct StatusMessage*) msg;
620 status = ntohl(sm->status);
622 if (ntohs(msg->size) > sizeof(struct StatusMessage))
624 emsg = (const char*) &sm[1];
625 if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
628 emsg = _("Invalid error message received from datastore service");
631 if ( (status == GNUNET_SYSERR) &&
635 emsg = _("Invalid error message received from datastore service");
638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
639 "Received status %d/%s\n",
643 rc->cont (rc->cont_cls,
652 * Store an item in the datastore. If the item is already present,
653 * the priorities are summed up and the higher expiration time and
654 * lower anonymity level is used.
656 * @param h handle to the datastore
657 * @param rid reservation ID to use (from "reserve"); use 0 if no
658 * prior reservation was made
659 * @param key key for the value
660 * @param size number of bytes in data
661 * @param data content stored
662 * @param type type of the content
663 * @param priority priority of the content
664 * @param anonymity anonymity-level for the content
665 * @param expiration expiration time for the content
666 * @param queue_priority ranking of this request in the priority queue
667 * @param max_queue_size at what queue size should this request be dropped
668 * (if other requests of higher priority are in the queue)
669 * @param timeout timeout for the operation
670 * @param cont continuation to call when done
671 * @param cont_cls closure for cont
672 * @return NULL if the entry was not queued, otherwise a handle that can be used to
673 * cancel; note that even if NULL is returned, the callback will be invoked
674 * (or rather, will already have been invoked)
676 struct GNUNET_DATASTORE_QueueEntry *
677 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
679 const GNUNET_HashCode * key,
682 enum GNUNET_BLOCK_Type type,
685 struct GNUNET_TIME_Absolute expiration,
686 unsigned int queue_priority,
687 unsigned int max_queue_size,
688 struct GNUNET_TIME_Relative timeout,
689 GNUNET_DATASTORE_ContinuationWithStatus cont,
692 struct StatusContext *scont;
693 struct GNUNET_DATASTORE_QueueEntry *qe;
694 struct DataMessage *dm;
698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
699 "Asked to put %u bytes of data under key `%s'\n",
703 msize = sizeof(struct DataMessage) + size;
704 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
705 scont = GNUNET_malloc (sizeof (struct StatusContext));
707 scont->cont_cls = cont_cls;
708 qe = make_queue_entry (h, msize,
709 queue_priority, max_queue_size, timeout,
710 &process_status_message, scont);
713 dm = (struct DataMessage* ) &qe[1];
714 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
715 dm->header.size = htons(msize);
716 dm->rid = htonl(rid);
717 dm->size = htonl(size);
718 dm->type = htonl(type);
719 dm->priority = htonl(priority);
720 dm->anonymity = htonl(anonymity);
721 dm->uid = GNUNET_htonll(0);
722 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
724 memcpy (&dm[1], data, size);
731 * Reserve space in the datastore. This function should be used
732 * to avoid "out of space" failures during a longer sequence of "put"
733 * operations (for example, when a file is being inserted).
735 * @param h handle to the datastore
736 * @param amount how much space (in bytes) should be reserved (for content only)
737 * @param entries how many entries will be created (to calculate per-entry overhead)
738 * @param queue_priority ranking of this request in the priority queue
739 * @param max_queue_size at what queue size should this request be dropped
740 * (if other requests of higher priority are in the queue)
741 * @param timeout how long to wait at most for a response (or before dying in queue)
742 * @param cont continuation to call when done; "success" will be set to
743 * a positive reservation value if space could be reserved.
744 * @param cont_cls closure for cont
745 * @return NULL if the entry was not queued, otherwise a handle that can be used to
746 * cancel; note that even if NULL is returned, the callback will be invoked
747 * (or rather, will already have been invoked)
749 struct GNUNET_DATASTORE_QueueEntry *
750 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
753 unsigned int queue_priority,
754 unsigned int max_queue_size,
755 struct GNUNET_TIME_Relative timeout,
756 GNUNET_DATASTORE_ContinuationWithStatus cont,
759 struct GNUNET_DATASTORE_QueueEntry *qe;
760 struct ReserveMessage *rm;
761 struct StatusContext *scont;
764 cont = &drop_status_cont;
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
767 "Asked to reserve %llu bytes of data and %u entries'\n",
768 (unsigned long long) amount,
769 (unsigned int) entries);
771 scont = GNUNET_malloc (sizeof (struct StatusContext));
773 scont->cont_cls = cont_cls;
774 qe = make_queue_entry (h, sizeof(struct ReserveMessage),
775 queue_priority, max_queue_size, timeout,
776 &process_status_message, scont);
779 rm = (struct ReserveMessage*) &qe[1];
780 rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
781 rm->header.size = htons(sizeof (struct ReserveMessage));
782 rm->entries = htonl(entries);
783 rm->amount = GNUNET_htonll(amount);
790 * Signal that all of the data for which a reservation was made has
791 * been stored and that whatever excess space might have been reserved
792 * can now be released.
794 * @param h handle to the datastore
795 * @param rid reservation ID (value of "success" in original continuation
796 * from the "reserve" function).
797 * @param queue_priority ranking of this request in the priority queue
798 * @param max_queue_size at what queue size should this request be dropped
799 * (if other requests of higher priority are in the queue)
800 * @param queue_priority ranking of this request in the priority queue
801 * @param max_queue_size at what queue size should this request be dropped
802 * (if other requests of higher priority are in the queue)
803 * @param timeout how long to wait at most for a response
804 * @param cont continuation to call when done
805 * @param cont_cls closure for cont
806 * @return NULL if the entry was not queued, otherwise a handle that can be used to
807 * cancel; note that even if NULL is returned, the callback will be invoked
808 * (or rather, will already have been invoked)
810 struct GNUNET_DATASTORE_QueueEntry *
811 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
813 unsigned int queue_priority,
814 unsigned int max_queue_size,
815 struct GNUNET_TIME_Relative timeout,
816 GNUNET_DATASTORE_ContinuationWithStatus cont,
819 struct GNUNET_DATASTORE_QueueEntry *qe;
820 struct ReleaseReserveMessage *rrm;
821 struct StatusContext *scont;
824 cont = &drop_status_cont;
826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
827 "Asked to release reserve %d\n",
830 scont = GNUNET_malloc (sizeof (struct StatusContext));
832 scont->cont_cls = cont_cls;
833 qe = make_queue_entry (h, sizeof(struct ReleaseReserveMessage),
834 queue_priority, max_queue_size, timeout,
835 &process_status_message, scont);
838 rrm = (struct ReleaseReserveMessage*) &qe[1];
839 rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
840 rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
841 rrm->rid = htonl(rid);
848 * Update a value in the datastore.
850 * @param h handle to the datastore
851 * @param uid identifier for the value
852 * @param priority how much to increase the priority of the value
853 * @param expiration new expiration value should be MAX of existing and this argument
854 * @param queue_priority ranking of this request in the priority queue
855 * @param max_queue_size at what queue size should this request be dropped
856 * (if other requests of higher priority are in the queue)
857 * @param timeout how long to wait at most for a response
858 * @param cont continuation to call when done
859 * @param cont_cls closure for cont
860 * @return NULL if the entry was not queued, otherwise a handle that can be used to
861 * cancel; note that even if NULL is returned, the callback will be invoked
862 * (or rather, will already have been invoked)
864 struct GNUNET_DATASTORE_QueueEntry *
865 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
866 unsigned long long uid,
868 struct GNUNET_TIME_Absolute expiration,
869 unsigned int queue_priority,
870 unsigned int max_queue_size,
871 struct GNUNET_TIME_Relative timeout,
872 GNUNET_DATASTORE_ContinuationWithStatus cont,
875 struct GNUNET_DATASTORE_QueueEntry *qe;
876 struct UpdateMessage *um;
877 struct StatusContext *scont;
880 cont = &drop_status_cont;
882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
883 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
885 (unsigned int) priority,
886 (unsigned long long) expiration.value);
888 scont = GNUNET_malloc (sizeof (struct StatusContext));
890 scont->cont_cls = cont_cls;
891 qe = make_queue_entry (h, sizeof(struct UpdateMessage),
892 queue_priority, max_queue_size, timeout,
893 &process_status_message, scont);
896 um = (struct UpdateMessage*) &qe[1];
897 um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
898 um->header.size = htons(sizeof (struct UpdateMessage));
899 um->priority = htonl(priority);
900 um->expiration = GNUNET_TIME_absolute_hton(expiration);
901 um->uid = GNUNET_htonll(uid);
908 * Explicitly remove some content from the database.
909 * The "cont"inuation will be called with status
910 * "GNUNET_OK" if content was removed, "GNUNET_NO"
911 * if no matching entry was found and "GNUNET_SYSERR"
912 * on all other types of errors.
914 * @param h handle to the datastore
915 * @param key key for the value
916 * @param size number of bytes in data
917 * @param data content stored
918 * @param queue_priority ranking of this request in the priority queue
919 * @param max_queue_size at what queue size should this request be dropped
920 * (if other requests of higher priority are in the queue)
921 * @param timeout how long to wait at most for a response
922 * @param cont continuation to call when done
923 * @param cont_cls closure for cont
924 * @return NULL if the entry was not queued, otherwise a handle that can be used to
925 * cancel; note that even if NULL is returned, the callback will be invoked
926 * (or rather, will already have been invoked)
928 struct GNUNET_DATASTORE_QueueEntry *
929 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
930 const GNUNET_HashCode *key,
933 unsigned int queue_priority,
934 unsigned int max_queue_size,
935 struct GNUNET_TIME_Relative timeout,
936 GNUNET_DATASTORE_ContinuationWithStatus cont,
939 struct GNUNET_DATASTORE_QueueEntry *qe;
940 struct DataMessage *dm;
942 struct StatusContext *scont;
945 cont = &drop_status_cont;
947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
948 "Asked to remove %u bytes under key `%s'\n",
952 scont = GNUNET_malloc (sizeof (struct StatusContext));
954 scont->cont_cls = cont_cls;
955 msize = sizeof(struct DataMessage) + size;
956 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
957 qe = make_queue_entry (h, msize,
958 queue_priority, max_queue_size, timeout,
959 &process_status_message, scont);
962 dm = (struct DataMessage*) &qe[1];
963 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
964 dm->header.size = htons(msize);
966 dm->size = htonl(size);
968 dm->priority = htonl(0);
969 dm->anonymity = htonl(0);
970 dm->uid = GNUNET_htonll(0);
971 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
973 memcpy (&dm[1], data, size);
981 * Context for processing result messages.
986 * Iterator to call with the result.
988 GNUNET_DATASTORE_Iterator iter;
999 * Type of a function to call when we receive a message
1002 * @param cls closure
1003 * @param msg message received, NULL on timeout or fatal error
1006 process_result_message (void *cls,
1007 const struct GNUNET_MessageHeader * msg)
1009 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
1010 struct GNUNET_DATASTORE_Handle *h = qe->h;
1011 struct ResultContext *rc = qe->client_ctx;
1012 const struct DataMessage *dm;
1014 GNUNET_assert (h->queue_head == qe);
1018 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1019 _("Failed to receive response from datastore\n"));
1021 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1026 rc->iter (rc->iter_cls,
1027 NULL, 0, NULL, 0, 0, 0,
1028 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1032 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1034 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1037 "Received end of result set\n");
1039 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1043 rc->iter (rc->iter_cls,
1044 NULL, 0, NULL, 0, 0, 0,
1045 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1050 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
1051 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1052 (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
1055 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1059 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1061 rc->iter (rc->iter_cls,
1062 NULL, 0, NULL, 0, 0, 0,
1063 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1067 dm = (const struct DataMessage*) msg;
1069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1070 "Received result %llu with type %u and size %u with key %s\n",
1071 (unsigned long long) GNUNET_ntohll(dm->uid),
1074 GNUNET_h2s(&dm->key));
1076 rc->iter (rc->iter_cls,
1081 ntohl(dm->priority),
1082 ntohl(dm->anonymity),
1083 GNUNET_TIME_absolute_ntoh(dm->expiration),
1084 GNUNET_ntohll(dm->uid));
1089 * Get a random value from the datastore.
1091 * @param h handle to the datastore
1092 * @param queue_priority ranking of this request in the priority queue
1093 * @param max_queue_size at what queue size should this request be dropped
1094 * (if other requests of higher priority are in the queue)
1095 * @param timeout how long to wait at most for a response
1096 * @param iter function to call on a random value; it
1097 * will be called once with a value (if available)
1098 * and always once with a value of NULL.
1099 * @param iter_cls closure for iter
1100 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1101 * cancel; note that even if NULL is returned, the callback will be invoked
1102 * (or rather, will already have been invoked)
1104 struct GNUNET_DATASTORE_QueueEntry *
1105 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
1106 unsigned int queue_priority,
1107 unsigned int max_queue_size,
1108 struct GNUNET_TIME_Relative timeout,
1109 GNUNET_DATASTORE_Iterator iter,
1112 struct GNUNET_DATASTORE_QueueEntry *qe;
1113 struct GNUNET_MessageHeader *m;
1114 struct ResultContext *rcont;
1117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1118 "Asked to get random entry in %llu ms\n",
1119 (unsigned long long) timeout.value);
1121 rcont = GNUNET_malloc (sizeof (struct ResultContext));
1123 rcont->iter_cls = iter_cls;
1124 qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader),
1125 queue_priority, max_queue_size, timeout,
1126 &process_result_message, rcont);
1129 m = (struct GNUNET_MessageHeader*) &qe[1];
1130 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
1131 m->size = htons(sizeof (struct GNUNET_MessageHeader));
1139 * Iterate over the results for a particular key
1140 * in the datastore. The iterator will only be called
1141 * once initially; if the first call did contain a
1142 * result, further results can be obtained by calling
1143 * "GNUNET_DATASTORE_get_next" with the given argument.
1145 * @param h handle to the datastore
1146 * @param key maybe NULL (to match all entries)
1147 * @param type desired type, 0 for any
1148 * @param queue_priority ranking of this request in the priority queue
1149 * @param max_queue_size at what queue size should this request be dropped
1150 * (if other requests of higher priority are in the queue)
1151 * @param timeout how long to wait at most for a response
1152 * @param iter function to call on each matching value;
1153 * will be called once with a NULL value at the end
1154 * @param iter_cls closure for iter
1155 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1156 * cancel; note that even if NULL is returned, the callback will be invoked
1157 * (or rather, will already have been invoked)
1159 struct GNUNET_DATASTORE_QueueEntry *
1160 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
1161 const GNUNET_HashCode * key,
1162 enum GNUNET_BLOCK_Type type,
1163 unsigned int queue_priority,
1164 unsigned int max_queue_size,
1165 struct GNUNET_TIME_Relative timeout,
1166 GNUNET_DATASTORE_Iterator iter,
1169 struct GNUNET_DATASTORE_QueueEntry *qe;
1170 struct GetMessage *gm;
1171 struct ResultContext *rcont;
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1175 "Asked to look for data of type %u under key `%s'\n",
1176 (unsigned int) type,
1179 rcont = GNUNET_malloc (sizeof (struct ResultContext));
1181 rcont->iter_cls = iter_cls;
1182 qe = make_queue_entry (h, sizeof(struct GetMessage),
1183 queue_priority, max_queue_size, timeout,
1184 &process_result_message, rcont);
1187 gm = (struct GetMessage*) &qe[1];
1188 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1189 gm->type = htonl(type);
1192 gm->header.size = htons(sizeof (struct GetMessage));
1197 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
1205 * Function called to trigger obtaining the next result
1206 * from the datastore.
1208 * @param h handle to the datastore
1209 * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
1210 * iteration (with a final call to "iter" with key/data == NULL).
1213 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
1216 struct GNUNET_DATASTORE_QueueEntry *qe = h->queue_head;
1217 struct ResultContext *rc = qe->client_ctx;
1219 GNUNET_assert (NULL != qe);
1220 GNUNET_assert (&process_result_message == qe->response_proc);
1221 if (GNUNET_YES == more)
1223 GNUNET_CLIENT_receive (h->client,
1226 GNUNET_TIME_absolute_get_remaining (qe->timeout));
1229 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1233 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1235 rc->iter (rc->iter_cls,
1236 NULL, 0, NULL, 0, 0, 0,
1237 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1243 * Cancel a datastore operation. The final callback from the
1244 * operation must not have been done yet.
1246 * @param qe operation to cancel
1249 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1251 struct GNUNET_DATASTORE_Handle *h;
1255 reconnect = qe->was_transmitted;
1256 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1259 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
1260 GNUNET_SCHEDULER_cancel (h->sched,
1265 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1271 /* end of datastore_api.c */