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 GNUNET_assert (NULL != qe->response_proc);
259 qe->response_proc (qe, NULL);
261 if (GNUNET_YES == drop)
263 h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
264 if (h->client != NULL)
267 GNUNET_CLIENT_notify_transmit_ready (h->client,
268 sizeof(struct GNUNET_MessageHeader),
269 GNUNET_TIME_UNIT_MINUTES,
274 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
283 * A request has timed out (before being transmitted to the service).
285 * @param cls the 'struct GNUNET_DATASTORE_QueueEntry'
286 * @param tc scheduler context
289 timeout_queue_entry (void *cls,
290 const struct GNUNET_SCHEDULER_TaskContext *tc)
292 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
294 qe->task = GNUNET_SCHEDULER_NO_TASK;
295 GNUNET_assert (qe->was_transmitted == GNUNET_NO);
296 qe->response_proc (qe, NULL);
301 * Create a new entry for our priority queue (and possibly discard other entires if
302 * the queue is getting too long).
304 * @param h handle to the datastore
305 * @param msize size of the message to queue
306 * @param queue_priority priority of the entry
307 * @param max_queue_size at what queue size should this request be dropped
308 * (if other requests of higher priority are in the queue)
309 * @param timeout timeout for the operation
310 * @param response_proc function to call with replies (can be NULL)
311 * @param client_ctx client context (NOT a closure for response_proc)
312 * @return NULL if the queue is full (and this entry was dropped)
314 static struct GNUNET_DATASTORE_QueueEntry *
315 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
317 unsigned int queue_priority,
318 unsigned int max_queue_size,
319 struct GNUNET_TIME_Relative timeout,
320 GNUNET_CLIENT_MessageHandler response_proc,
323 struct GNUNET_DATASTORE_QueueEntry *ret;
324 struct GNUNET_DATASTORE_QueueEntry *pos;
329 while ( (pos != NULL) &&
330 (c < max_queue_size) &&
331 (pos->priority >= queue_priority) )
336 if (c >= max_queue_size)
340 /* append at the tail */
346 /* do not insert at HEAD if HEAD query was already
347 transmitted and we are still receiving replies! */
348 if ( (pos == NULL) &&
349 (h->queue_head->was_transmitted) )
352 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
353 GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
358 ret->response_proc = response_proc;
359 ret->client_ctx = client_ctx;
360 ret->task = GNUNET_SCHEDULER_add_delayed (h->sched,
362 &timeout_queue_entry,
364 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
365 ret->priority = queue_priority;
366 ret->max_queue = max_queue_size;
367 ret->message_size = msize;
368 ret->was_transmitted = GNUNET_NO;
374 if (pos->max_queue < h->queue_size)
376 GNUNET_assert (pos->response_proc != NULL);
377 pos->response_proc (pos, NULL);
387 * Process entries in the queue (or do nothing if we are already
390 * @param h handle to the datastore
393 process_queue (struct GNUNET_DATASTORE_Handle *h);
397 * Try reconnecting to the datastore service.
399 * @param cls the 'struct GNUNET_DATASTORE_Handle'
400 * @param tc scheduler context
403 try_reconnect (void *cls,
404 const struct GNUNET_SCHEDULER_TaskContext *tc)
406 struct GNUNET_DATASTORE_Handle *h = cls;
408 if (h->retry_time.value < GNUNET_CONSTANTS_SERVICE_RETRY.value)
409 h->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
411 h->retry_time = GNUNET_TIME_relative_multiply (h->retry_time, 2);
412 if (h->retry_time.value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.value)
413 h->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
414 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
415 h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
416 if (h->client == NULL)
423 * Disconnect from the service and then try reconnecting to the datastore service
426 * @param h handle to datastore to disconnect and reconnect
429 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
431 if (h->client == NULL)
433 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
435 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->sched,
443 * Transmit request from queue to datastore service.
445 * @param cls the 'struct GNUNET_DATASTORE_Handle'
446 * @param size number of bytes that can be copied to buf
447 * @param buf where to copy the drop message
448 * @return number of bytes written to buf
451 transmit_request (void *cls,
455 struct GNUNET_DATASTORE_Handle *h = cls;
456 struct GNUNET_DATASTORE_QueueEntry *qe;
460 if (NULL == (qe = h->queue_head))
461 return 0; /* no entry in queue */
464 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
465 _("Failed to transmit request to database.\n"));
469 if (size < (msize = qe->message_size))
474 memcpy (buf, &qe[1], msize);
475 qe->was_transmitted = GNUNET_YES;
476 GNUNET_SCHEDULER_cancel (h->sched,
478 qe->task = GNUNET_SCHEDULER_NO_TASK;
479 GNUNET_CLIENT_receive (h->client,
482 GNUNET_TIME_absolute_get_remaining (qe->timeout));
488 * Process entries in the queue (or do nothing if we are already
491 * @param h handle to the datastore
494 process_queue (struct GNUNET_DATASTORE_Handle *h)
496 struct GNUNET_DATASTORE_QueueEntry *qe;
498 if (NULL == (qe = h->queue_head))
499 return; /* no entry in queue */
500 if (qe->was_transmitted == GNUNET_YES)
501 return; /* waiting for replies */
503 return; /* request pending */
504 if (h->client == NULL)
505 return; /* waiting for reconnect */
506 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
508 GNUNET_TIME_absolute_get_remaining (qe->timeout),
518 * Context for processing status messages.
523 * Continuation to call with the status.
525 GNUNET_DATASTORE_ContinuationWithStatus cont;
536 * Dummy continuation used to do nothing (but be non-zero).
539 * @param result result
540 * @param emsg error message
543 drop_status_cont (void *cls, int result, const char *emsg)
550 free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
552 struct GNUNET_DATASTORE_Handle *h = qe->h;
554 GNUNET_CONTAINER_DLL_remove (h->queue_head,
557 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
559 GNUNET_SCHEDULER_cancel (h->sched,
561 qe->task = GNUNET_SCHEDULER_NO_TASK;
568 * Type of a function to call when we receive a message
572 * @param msg message received, NULL on timeout or fatal error
575 process_status_message (void *cls,
577 GNUNET_MessageHeader * msg)
579 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
580 struct GNUNET_DATASTORE_Handle *h = qe->h;
581 struct StatusContext *rc = qe->client_ctx;
582 const struct StatusMessage *sm;
586 free_queue_entry (qe);
589 if (NULL == h->client)
590 return; /* forced disconnect */
591 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
592 _("Failed to receive response from database.\n"));
597 if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
598 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) )
601 h->retry_time = GNUNET_TIME_UNIT_ZERO;
603 rc->cont (rc->cont_cls,
605 _("Error reading response from datastore service"));
609 sm = (const struct StatusMessage*) msg;
610 status = ntohl(sm->status);
612 if (ntohs(msg->size) > sizeof(struct StatusMessage))
614 emsg = (const char*) &sm[1];
615 if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
618 emsg = _("Invalid error message received from datastore service");
621 if ( (status == GNUNET_SYSERR) &&
625 emsg = _("Invalid error message received from datastore service");
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
629 "Received status %d/%s\n",
633 rc->cont (rc->cont_cls,
642 * Store an item in the datastore. If the item is already present,
643 * the priorities are summed up and the higher expiration time and
644 * lower anonymity level is used.
646 * @param h handle to the datastore
647 * @param rid reservation ID to use (from "reserve"); use 0 if no
648 * prior reservation was made
649 * @param key key for the value
650 * @param size number of bytes in data
651 * @param data content stored
652 * @param type type of the content
653 * @param priority priority of the content
654 * @param anonymity anonymity-level for the content
655 * @param expiration expiration time for the content
656 * @param queue_priority ranking of this request in the priority queue
657 * @param max_queue_size at what queue size should this request be dropped
658 * (if other requests of higher priority are in the queue)
659 * @param timeout timeout for the operation
660 * @param cont continuation to call when done
661 * @param cont_cls closure for cont
662 * @return NULL if the entry was not queued, otherwise a handle that can be used to
663 * cancel; note that even if NULL is returned, the callback will be invoked
664 * (or rather, will already have been invoked)
666 struct GNUNET_DATASTORE_QueueEntry *
667 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
669 const GNUNET_HashCode * key,
672 enum GNUNET_BLOCK_Type type,
675 struct GNUNET_TIME_Absolute expiration,
676 unsigned int queue_priority,
677 unsigned int max_queue_size,
678 struct GNUNET_TIME_Relative timeout,
679 GNUNET_DATASTORE_ContinuationWithStatus cont,
682 struct StatusContext *scont;
683 struct GNUNET_DATASTORE_QueueEntry *qe;
684 struct DataMessage *dm;
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689 "Asked to put %u bytes of data under key `%s'\n",
693 msize = sizeof(struct DataMessage) + size;
694 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
695 scont = GNUNET_malloc (sizeof (struct StatusContext));
697 scont->cont_cls = cont_cls;
698 qe = make_queue_entry (h, msize,
699 queue_priority, max_queue_size, timeout,
700 &process_status_message, scont);
703 dm = (struct DataMessage* ) &qe[1];
704 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
705 dm->header.size = htons(msize);
706 dm->rid = htonl(rid);
707 dm->size = htonl(size);
708 dm->type = htonl(type);
709 dm->priority = htonl(priority);
710 dm->anonymity = htonl(anonymity);
711 dm->uid = GNUNET_htonll(0);
712 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
714 memcpy (&dm[1], data, size);
721 * Reserve space in the datastore. This function should be used
722 * to avoid "out of space" failures during a longer sequence of "put"
723 * operations (for example, when a file is being inserted).
725 * @param h handle to the datastore
726 * @param amount how much space (in bytes) should be reserved (for content only)
727 * @param entries how many entries will be created (to calculate per-entry overhead)
728 * @param queue_priority ranking of this request in the priority queue
729 * @param max_queue_size at what queue size should this request be dropped
730 * (if other requests of higher priority are in the queue)
731 * @param timeout how long to wait at most for a response (or before dying in queue)
732 * @param cont continuation to call when done; "success" will be set to
733 * a positive reservation value if space could be reserved.
734 * @param cont_cls closure for cont
735 * @return NULL if the entry was not queued, otherwise a handle that can be used to
736 * cancel; note that even if NULL is returned, the callback will be invoked
737 * (or rather, will already have been invoked)
739 struct GNUNET_DATASTORE_QueueEntry *
740 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
743 unsigned int queue_priority,
744 unsigned int max_queue_size,
745 struct GNUNET_TIME_Relative timeout,
746 GNUNET_DATASTORE_ContinuationWithStatus cont,
749 struct GNUNET_DATASTORE_QueueEntry *qe;
750 struct ReserveMessage *rm;
751 struct StatusContext *scont;
754 cont = &drop_status_cont;
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
757 "Asked to reserve %llu bytes of data and %u entries'\n",
758 (unsigned long long) amount,
759 (unsigned int) entries);
761 scont = GNUNET_malloc (sizeof (struct StatusContext));
763 scont->cont_cls = cont_cls;
764 qe = make_queue_entry (h, sizeof(struct ReserveMessage),
765 queue_priority, max_queue_size, timeout,
766 &process_status_message, scont);
769 rm = (struct ReserveMessage*) &qe[1];
770 rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
771 rm->header.size = htons(sizeof (struct ReserveMessage));
772 rm->entries = htonl(entries);
773 rm->amount = GNUNET_htonll(amount);
780 * Signal that all of the data for which a reservation was made has
781 * been stored and that whatever excess space might have been reserved
782 * can now be released.
784 * @param h handle to the datastore
785 * @param rid reservation ID (value of "success" in original continuation
786 * from the "reserve" function).
787 * @param queue_priority ranking of this request in the priority queue
788 * @param max_queue_size at what queue size should this request be dropped
789 * (if other requests of higher priority are in the queue)
790 * @param queue_priority ranking of this request in the priority queue
791 * @param max_queue_size at what queue size should this request be dropped
792 * (if other requests of higher priority are in the queue)
793 * @param timeout how long to wait at most for a response
794 * @param cont continuation to call when done
795 * @param cont_cls closure for cont
796 * @return NULL if the entry was not queued, otherwise a handle that can be used to
797 * cancel; note that even if NULL is returned, the callback will be invoked
798 * (or rather, will already have been invoked)
800 struct GNUNET_DATASTORE_QueueEntry *
801 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
803 unsigned int queue_priority,
804 unsigned int max_queue_size,
805 struct GNUNET_TIME_Relative timeout,
806 GNUNET_DATASTORE_ContinuationWithStatus cont,
809 struct GNUNET_DATASTORE_QueueEntry *qe;
810 struct ReleaseReserveMessage *rrm;
811 struct StatusContext *scont;
814 cont = &drop_status_cont;
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
817 "Asked to release reserve %d\n",
820 scont = GNUNET_malloc (sizeof (struct StatusContext));
822 scont->cont_cls = cont_cls;
823 qe = make_queue_entry (h, sizeof(struct ReleaseReserveMessage),
824 queue_priority, max_queue_size, timeout,
825 &process_status_message, scont);
828 rrm = (struct ReleaseReserveMessage*) &qe[1];
829 rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
830 rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
831 rrm->rid = htonl(rid);
838 * Update a value in the datastore.
840 * @param h handle to the datastore
841 * @param uid identifier for the value
842 * @param priority how much to increase the priority of the value
843 * @param expiration new expiration value should be MAX of existing and this argument
844 * @param queue_priority ranking of this request in the priority queue
845 * @param max_queue_size at what queue size should this request be dropped
846 * (if other requests of higher priority are in the queue)
847 * @param timeout how long to wait at most for a response
848 * @param cont continuation to call when done
849 * @param cont_cls closure for cont
850 * @return NULL if the entry was not queued, otherwise a handle that can be used to
851 * cancel; note that even if NULL is returned, the callback will be invoked
852 * (or rather, will already have been invoked)
854 struct GNUNET_DATASTORE_QueueEntry *
855 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
856 unsigned long long uid,
858 struct GNUNET_TIME_Absolute expiration,
859 unsigned int queue_priority,
860 unsigned int max_queue_size,
861 struct GNUNET_TIME_Relative timeout,
862 GNUNET_DATASTORE_ContinuationWithStatus cont,
865 struct GNUNET_DATASTORE_QueueEntry *qe;
866 struct UpdateMessage *um;
867 struct StatusContext *scont;
870 cont = &drop_status_cont;
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
875 (unsigned int) priority,
876 (unsigned long long) expiration.value);
878 scont = GNUNET_malloc (sizeof (struct StatusContext));
880 scont->cont_cls = cont_cls;
881 qe = make_queue_entry (h, sizeof(struct UpdateMessage),
882 queue_priority, max_queue_size, timeout,
883 &process_status_message, scont);
886 um = (struct UpdateMessage*) &qe[1];
887 um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
888 um->header.size = htons(sizeof (struct UpdateMessage));
889 um->priority = htonl(priority);
890 um->expiration = GNUNET_TIME_absolute_hton(expiration);
891 um->uid = GNUNET_htonll(uid);
898 * Explicitly remove some content from the database.
899 * The "cont"inuation will be called with status
900 * "GNUNET_OK" if content was removed, "GNUNET_NO"
901 * if no matching entry was found and "GNUNET_SYSERR"
902 * on all other types of errors.
904 * @param h handle to the datastore
905 * @param key key for the value
906 * @param size number of bytes in data
907 * @param data content stored
908 * @param queue_priority ranking of this request in the priority queue
909 * @param max_queue_size at what queue size should this request be dropped
910 * (if other requests of higher priority are in the queue)
911 * @param timeout how long to wait at most for a response
912 * @param cont continuation to call when done
913 * @param cont_cls closure for cont
914 * @return NULL if the entry was not queued, otherwise a handle that can be used to
915 * cancel; note that even if NULL is returned, the callback will be invoked
916 * (or rather, will already have been invoked)
918 struct GNUNET_DATASTORE_QueueEntry *
919 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
920 const GNUNET_HashCode *key,
923 unsigned int queue_priority,
924 unsigned int max_queue_size,
925 struct GNUNET_TIME_Relative timeout,
926 GNUNET_DATASTORE_ContinuationWithStatus cont,
929 struct GNUNET_DATASTORE_QueueEntry *qe;
930 struct DataMessage *dm;
932 struct StatusContext *scont;
935 cont = &drop_status_cont;
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938 "Asked to remove %u bytes under key `%s'\n",
942 scont = GNUNET_malloc (sizeof (struct StatusContext));
944 scont->cont_cls = cont_cls;
945 msize = sizeof(struct DataMessage) + size;
946 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
947 qe = make_queue_entry (h, msize,
948 queue_priority, max_queue_size, timeout,
949 &process_status_message, scont);
952 dm = (struct DataMessage*) &qe[1];
953 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
954 dm->header.size = htons(msize);
956 dm->size = htonl(size);
958 dm->priority = htonl(0);
959 dm->anonymity = htonl(0);
960 dm->uid = GNUNET_htonll(0);
961 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
963 memcpy (&dm[1], data, size);
971 * Context for processing result messages.
976 * Iterator to call with the result.
978 GNUNET_DATASTORE_Iterator iter;
989 * Type of a function to call when we receive a message
993 * @param msg message received, NULL on timeout or fatal error
996 process_result_message (void *cls,
997 const struct GNUNET_MessageHeader * msg)
999 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
1000 struct GNUNET_DATASTORE_Handle *h = qe->h;
1001 struct ResultContext *rc = qe->client_ctx;
1002 const struct DataMessage *dm;
1004 GNUNET_assert (h->queue_head == qe);
1008 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1009 _("Failed to receive response from datastore\n"));
1011 free_queue_entry (qe);
1013 rc->iter (rc->iter_cls,
1014 NULL, 0, NULL, 0, 0, 0,
1015 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1019 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1021 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1024 "Received end of result set\n");
1026 free_queue_entry (qe);
1027 rc->iter (rc->iter_cls,
1028 NULL, 0, NULL, 0, 0, 0,
1029 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1034 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
1035 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1036 (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
1039 free_queue_entry (qe);
1040 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1042 rc->iter (rc->iter_cls,
1043 NULL, 0, NULL, 0, 0, 0,
1044 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1048 dm = (const struct DataMessage*) msg;
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051 "Received result %llu with type %u and size %u with key %s\n",
1052 (unsigned long long) GNUNET_ntohll(dm->uid),
1055 GNUNET_h2s(&dm->key));
1057 rc->iter (rc->iter_cls,
1062 ntohl(dm->priority),
1063 ntohl(dm->anonymity),
1064 GNUNET_TIME_absolute_ntoh(dm->expiration),
1065 GNUNET_ntohll(dm->uid));
1070 * Get a random value from the datastore.
1072 * @param h handle to the datastore
1073 * @param queue_priority ranking of this request in the priority queue
1074 * @param max_queue_size at what queue size should this request be dropped
1075 * (if other requests of higher priority are in the queue)
1076 * @param timeout how long to wait at most for a response
1077 * @param iter function to call on a random value; it
1078 * will be called once with a value (if available)
1079 * and always once with a value of NULL.
1080 * @param iter_cls closure for iter
1081 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1082 * cancel; note that even if NULL is returned, the callback will be invoked
1083 * (or rather, will already have been invoked)
1085 struct GNUNET_DATASTORE_QueueEntry *
1086 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
1087 unsigned int queue_priority,
1088 unsigned int max_queue_size,
1089 struct GNUNET_TIME_Relative timeout,
1090 GNUNET_DATASTORE_Iterator iter,
1093 struct GNUNET_DATASTORE_QueueEntry *qe;
1094 struct GNUNET_MessageHeader *m;
1095 struct ResultContext *rcont;
1098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1099 "Asked to get random entry in %llu ms\n",
1100 (unsigned long long) timeout.value);
1102 rcont = GNUNET_malloc (sizeof (struct ResultContext));
1104 rcont->iter_cls = iter_cls;
1105 qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader),
1106 queue_priority, max_queue_size, timeout,
1107 &process_result_message, rcont);
1110 m = (struct GNUNET_MessageHeader*) &qe[1];
1111 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
1112 m->size = htons(sizeof (struct GNUNET_MessageHeader));
1120 * Iterate over the results for a particular key
1121 * in the datastore. The iterator will only be called
1122 * once initially; if the first call did contain a
1123 * result, further results can be obtained by calling
1124 * "GNUNET_DATASTORE_get_next" with the given argument.
1126 * @param h handle to the datastore
1127 * @param key maybe NULL (to match all entries)
1128 * @param type desired type, 0 for any
1129 * @param queue_priority ranking of this request in the priority queue
1130 * @param max_queue_size at what queue size should this request be dropped
1131 * (if other requests of higher priority are in the queue)
1132 * @param timeout how long to wait at most for a response
1133 * @param iter function to call on each matching value;
1134 * will be called once with a NULL value at the end
1135 * @param iter_cls closure for iter
1136 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1137 * cancel; note that even if NULL is returned, the callback will be invoked
1138 * (or rather, will already have been invoked)
1140 struct GNUNET_DATASTORE_QueueEntry *
1141 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
1142 const GNUNET_HashCode * key,
1143 enum GNUNET_BLOCK_Type type,
1144 unsigned int queue_priority,
1145 unsigned int max_queue_size,
1146 struct GNUNET_TIME_Relative timeout,
1147 GNUNET_DATASTORE_Iterator iter,
1150 struct GNUNET_DATASTORE_QueueEntry *qe;
1151 struct GetMessage *gm;
1152 struct ResultContext *rcont;
1155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1156 "Asked to look for data of type %u under key `%s'\n",
1157 (unsigned int) type,
1160 rcont = GNUNET_malloc (sizeof (struct ResultContext));
1162 rcont->iter_cls = iter_cls;
1163 qe = make_queue_entry (h, sizeof(struct GetMessage),
1164 queue_priority, max_queue_size, timeout,
1165 &process_result_message, rcont);
1168 gm = (struct GetMessage*) &qe[1];
1169 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1170 gm->type = htonl(type);
1173 gm->header.size = htons(sizeof (struct GetMessage));
1178 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
1186 * Function called to trigger obtaining the next result
1187 * from the datastore.
1189 * @param h handle to the datastore
1190 * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
1191 * iteration (with a final call to "iter" with key/data == NULL).
1194 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
1197 struct GNUNET_DATASTORE_QueueEntry *qe = h->queue_head;
1198 struct ResultContext *rc = qe->client_ctx;
1200 GNUNET_assert (NULL != qe);
1201 GNUNET_assert (&process_result_message == qe->response_proc);
1202 if (GNUNET_YES == more)
1204 GNUNET_CLIENT_receive (h->client,
1207 GNUNET_TIME_absolute_get_remaining (qe->timeout));
1210 free_queue_entry (qe);
1211 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1213 rc->iter (rc->iter_cls,
1214 NULL, 0, NULL, 0, 0, 0,
1215 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1221 * Cancel a datastore operation. The final callback from the
1222 * operation must not have been done yet.
1224 * @param qe operation to cancel
1227 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1229 struct GNUNET_DATASTORE_Handle *h;
1233 reconnect = qe->was_transmitted;
1234 free_queue_entry (qe);
1238 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1244 /* end of datastore_api.c */