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)
246 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
247 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
248 GNUNET_SCHEDULER_cancel (h->sched,
251 while (NULL != (qe = h->queue_head))
253 GNUNET_CONTAINER_DLL_remove (h->queue_head,
256 if (NULL != qe->response_proc)
257 qe->response_proc (qe, NULL);
260 if (GNUNET_YES == drop)
262 h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
263 if (h->client != NULL)
266 GNUNET_CLIENT_notify_transmit_ready (h->client,
267 sizeof(struct GNUNET_MessageHeader),
268 GNUNET_TIME_UNIT_MINUTES,
273 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
282 * A request has timed out (before being transmitted to the service).
284 * @param cls the 'struct GNUNET_DATASTORE_QueueEntry'
285 * @param tc scheduler context
288 timeout_queue_entry (void *cls,
289 const struct GNUNET_SCHEDULER_TaskContext *tc)
291 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
293 qe->task = GNUNET_SCHEDULER_NO_TASK;
294 GNUNET_assert (qe->was_transmitted == GNUNET_NO);
295 qe->response_proc (qe, NULL);
300 * Create a new entry for our priority queue (and possibly discard other entires if
301 * the queue is getting too long).
303 * @param h handle to the datastore
304 * @param msize size of the message to queue
305 * @param queue_priority priority of the entry
306 * @param max_queue_size at what queue size should this request be dropped
307 * (if other requests of higher priority are in the queue)
308 * @param timeout timeout for the operation
309 * @param response_proc function to call with replies (can be NULL)
310 * @param client_ctx client context (NOT a closure for response_proc)
311 * @return NULL if the queue is full (and this entry was dropped)
313 static struct GNUNET_DATASTORE_QueueEntry *
314 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
316 unsigned int queue_priority,
317 unsigned int max_queue_size,
318 struct GNUNET_TIME_Relative timeout,
319 GNUNET_CLIENT_MessageHandler response_proc,
322 struct GNUNET_DATASTORE_QueueEntry *ret;
323 struct GNUNET_DATASTORE_QueueEntry *pos;
328 while ( (pos != NULL) &&
329 (c < max_queue_size) &&
330 (pos->priority >= queue_priority) )
335 if (c >= max_queue_size)
339 /* append at the tail */
345 /* do not insert at HEAD if HEAD query was already
346 transmitted and we are still receiving replies! */
347 if ( (pos == NULL) &&
348 (h->queue_head->was_transmitted) )
351 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
352 GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
357 ret->response_proc = response_proc;
358 ret->client_ctx = client_ctx;
359 ret->task = GNUNET_SCHEDULER_add_delayed (h->sched,
361 &timeout_queue_entry,
363 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
364 ret->priority = queue_priority;
365 ret->max_queue = max_queue_size;
366 ret->message_size = msize;
367 ret->was_transmitted = GNUNET_NO;
373 if (pos->max_queue < h->queue_size)
375 GNUNET_CONTAINER_DLL_remove (h->queue_head,
378 GNUNET_SCHEDULER_cancel (h->sched,
380 if (pos->response_proc != NULL)
381 pos->response_proc (pos, NULL);
393 * Process entries in the queue (or do nothing if we are already
396 * @param h handle to the datastore
399 process_queue (struct GNUNET_DATASTORE_Handle *h);
403 * Try reconnecting to the datastore service.
405 * @param cls the 'struct GNUNET_DATASTORE_Handle'
406 * @param tc scheduler context
409 try_reconnect (void *cls,
410 const struct GNUNET_SCHEDULER_TaskContext *tc)
412 struct GNUNET_DATASTORE_Handle *h = cls;
414 if (h->retry_time.value < GNUNET_CONSTANTS_SERVICE_RETRY.value)
415 h->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
417 h->retry_time = GNUNET_TIME_relative_multiply (h->retry_time, 2);
418 if (h->retry_time.value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.value)
419 h->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
420 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
421 h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
422 if (h->client == NULL)
429 * Disconnect from the service and then try reconnecting to the datastore service
432 * @param cls the 'struct GNUNET_DATASTORE_Handle'
433 * @param tc scheduler context
436 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
438 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
440 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->sched,
448 * Transmit request from queue to datastore service.
450 * @param cls the 'struct GNUNET_DATASTORE_Handle'
451 * @param size number of bytes that can be copied to buf
452 * @param buf where to copy the drop message
453 * @return number of bytes written to buf
456 transmit_request (void *cls,
460 struct GNUNET_DATASTORE_Handle *h = cls;
461 struct GNUNET_DATASTORE_QueueEntry *qe;
465 if (NULL == (qe = h->queue_head))
466 return 0; /* no entry in queue */
469 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
470 _("Failed to transmit request to database.\n"));
474 if (size < (msize = qe->message_size))
479 memcpy (buf, &qe[1], msize);
480 qe->was_transmitted = GNUNET_YES;
481 GNUNET_SCHEDULER_cancel (h->sched,
483 qe->task = GNUNET_SCHEDULER_NO_TASK;
484 GNUNET_CLIENT_receive (h->client,
487 GNUNET_TIME_absolute_get_remaining (qe->timeout));
493 * Process entries in the queue (or do nothing if we are already
496 * @param h handle to the datastore
499 process_queue (struct GNUNET_DATASTORE_Handle *h)
501 struct GNUNET_DATASTORE_QueueEntry *qe;
503 if (NULL == (qe = h->queue_head))
504 return; /* no entry in queue */
505 if (qe->was_transmitted == GNUNET_YES)
506 return; /* waiting for replies */
508 return; /* request pending */
509 if (h->client == NULL)
510 return; /* waiting for reconnect */
511 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
513 GNUNET_TIME_absolute_get_remaining (qe->timeout),
523 * Context for processing status messages.
528 * Continuation to call with the status.
530 GNUNET_DATASTORE_ContinuationWithStatus cont;
541 * Dummy continuation used to do nothing (but be non-zero).
544 * @param result result
545 * @param emsg error message
548 drop_status_cont (void *cls, int result, const char *emsg)
555 * Type of a function to call when we receive a message
559 * @param msg message received, NULL on timeout or fatal error
562 process_status_message (void *cls,
564 GNUNET_MessageHeader * msg)
566 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
567 struct GNUNET_DATASTORE_Handle *h = qe->h;
568 struct StatusContext *rc = qe->client_ctx;
569 const struct StatusMessage *sm;
573 GNUNET_CONTAINER_DLL_remove (h->queue_head,
579 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
580 _("Failed to receive response from database.\n"));
585 if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
586 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) )
589 h->retry_time = GNUNET_TIME_UNIT_ZERO;
591 rc->cont (rc->cont_cls,
593 _("Error reading response from datastore service"));
597 sm = (const struct StatusMessage*) msg;
598 status = ntohl(sm->status);
600 if (ntohs(msg->size) > sizeof(struct StatusMessage))
602 emsg = (const char*) &sm[1];
603 if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
606 emsg = _("Invalid error message received from datastore service");
609 if ( (status == GNUNET_SYSERR) &&
613 emsg = _("Invalid error message received from datastore service");
616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
617 "Received status %d/%s\n",
621 rc->cont (rc->cont_cls,
630 * Store an item in the datastore. If the item is already present,
631 * the priorities are summed up and the higher expiration time and
632 * lower anonymity level is used.
634 * @param h handle to the datastore
635 * @param rid reservation ID to use (from "reserve"); use 0 if no
636 * prior reservation was made
637 * @param key key for the value
638 * @param size number of bytes in data
639 * @param data content stored
640 * @param type type of the content
641 * @param priority priority of the content
642 * @param anonymity anonymity-level for the content
643 * @param expiration expiration time for the content
644 * @param queue_priority ranking of this request in the priority queue
645 * @param max_queue_size at what queue size should this request be dropped
646 * (if other requests of higher priority are in the queue)
647 * @param timeout timeout for the operation
648 * @param cont continuation to call when done
649 * @param cont_cls closure for cont
650 * @return NULL if the entry was not queued, otherwise a handle that can be used to
651 * cancel; note that even if NULL is returned, the callback will be invoked
652 * (or rather, will already have been invoked)
654 struct GNUNET_DATASTORE_QueueEntry *
655 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
657 const GNUNET_HashCode * key,
660 enum GNUNET_BLOCK_Type type,
663 struct GNUNET_TIME_Absolute expiration,
664 unsigned int queue_priority,
665 unsigned int max_queue_size,
666 struct GNUNET_TIME_Relative timeout,
667 GNUNET_DATASTORE_ContinuationWithStatus cont,
670 struct StatusContext *scont;
671 struct GNUNET_DATASTORE_QueueEntry *qe;
672 struct DataMessage *dm;
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
677 "Asked to put %u bytes of data under key `%s'\n",
681 msize = sizeof(struct DataMessage) + size;
682 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
683 scont = GNUNET_malloc (sizeof (struct StatusContext));
685 scont->cont_cls = cont_cls;
686 qe = make_queue_entry (h, msize,
687 queue_priority, max_queue_size, timeout,
688 &process_status_message, scont);
691 dm = (struct DataMessage* ) &qe[1];
692 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
693 dm->header.size = htons(msize);
694 dm->rid = htonl(rid);
695 dm->size = htonl(size);
696 dm->type = htonl(type);
697 dm->priority = htonl(priority);
698 dm->anonymity = htonl(anonymity);
699 dm->uid = GNUNET_htonll(0);
700 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
702 memcpy (&dm[1], data, size);
709 * Reserve space in the datastore. This function should be used
710 * to avoid "out of space" failures during a longer sequence of "put"
711 * operations (for example, when a file is being inserted).
713 * @param h handle to the datastore
714 * @param amount how much space (in bytes) should be reserved (for content only)
715 * @param entries how many entries will be created (to calculate per-entry overhead)
716 * @param queue_priority ranking of this request in the priority queue
717 * @param max_queue_size at what queue size should this request be dropped
718 * (if other requests of higher priority are in the queue)
719 * @param timeout how long to wait at most for a response (or before dying in queue)
720 * @param cont continuation to call when done; "success" will be set to
721 * a positive reservation value if space could be reserved.
722 * @param cont_cls closure for cont
723 * @return NULL if the entry was not queued, otherwise a handle that can be used to
724 * cancel; note that even if NULL is returned, the callback will be invoked
725 * (or rather, will already have been invoked)
727 struct GNUNET_DATASTORE_QueueEntry *
728 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
731 unsigned int queue_priority,
732 unsigned int max_queue_size,
733 struct GNUNET_TIME_Relative timeout,
734 GNUNET_DATASTORE_ContinuationWithStatus cont,
737 struct GNUNET_DATASTORE_QueueEntry *qe;
738 struct ReserveMessage *rm;
739 struct StatusContext *scont;
742 cont = &drop_status_cont;
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 "Asked to reserve %llu bytes of data and %u entries'\n",
746 (unsigned long long) amount,
747 (unsigned int) entries);
749 scont = GNUNET_malloc (sizeof (struct StatusContext));
751 scont->cont_cls = cont_cls;
752 qe = make_queue_entry (h, sizeof(struct ReserveMessage),
753 queue_priority, max_queue_size, timeout,
754 &process_status_message, scont);
757 rm = (struct ReserveMessage*) &qe[1];
758 rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
759 rm->header.size = htons(sizeof (struct ReserveMessage));
760 rm->entries = htonl(entries);
761 rm->amount = GNUNET_htonll(amount);
768 * Signal that all of the data for which a reservation was made has
769 * been stored and that whatever excess space might have been reserved
770 * can now be released.
772 * @param h handle to the datastore
773 * @param rid reservation ID (value of "success" in original continuation
774 * from the "reserve" function).
775 * @param queue_priority ranking of this request in the priority queue
776 * @param max_queue_size at what queue size should this request be dropped
777 * (if other requests of higher priority are in the queue)
778 * @param queue_priority ranking of this request in the priority queue
779 * @param max_queue_size at what queue size should this request be dropped
780 * (if other requests of higher priority are in the queue)
781 * @param timeout how long to wait at most for a response
782 * @param cont continuation to call when done
783 * @param cont_cls closure for cont
784 * @return NULL if the entry was not queued, otherwise a handle that can be used to
785 * cancel; note that even if NULL is returned, the callback will be invoked
786 * (or rather, will already have been invoked)
788 struct GNUNET_DATASTORE_QueueEntry *
789 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
791 unsigned int queue_priority,
792 unsigned int max_queue_size,
793 struct GNUNET_TIME_Relative timeout,
794 GNUNET_DATASTORE_ContinuationWithStatus cont,
797 struct GNUNET_DATASTORE_QueueEntry *qe;
798 struct ReleaseReserveMessage *rrm;
799 struct StatusContext *scont;
802 cont = &drop_status_cont;
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
805 "Asked to release reserve %d\n",
808 scont = GNUNET_malloc (sizeof (struct StatusContext));
810 scont->cont_cls = cont_cls;
811 qe = make_queue_entry (h, sizeof(struct ReleaseReserveMessage),
812 queue_priority, max_queue_size, timeout,
813 &process_status_message, scont);
816 rrm = (struct ReleaseReserveMessage*) &qe[1];
817 rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
818 rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
819 rrm->rid = htonl(rid);
826 * Update a value in the datastore.
828 * @param h handle to the datastore
829 * @param uid identifier for the value
830 * @param priority how much to increase the priority of the value
831 * @param expiration new expiration value should be MAX of existing and this argument
832 * @param queue_priority ranking of this request in the priority queue
833 * @param max_queue_size at what queue size should this request be dropped
834 * (if other requests of higher priority are in the queue)
835 * @param timeout how long to wait at most for a response
836 * @param cont continuation to call when done
837 * @param cont_cls closure for cont
838 * @return NULL if the entry was not queued, otherwise a handle that can be used to
839 * cancel; note that even if NULL is returned, the callback will be invoked
840 * (or rather, will already have been invoked)
842 struct GNUNET_DATASTORE_QueueEntry *
843 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
844 unsigned long long uid,
846 struct GNUNET_TIME_Absolute expiration,
847 unsigned int queue_priority,
848 unsigned int max_queue_size,
849 struct GNUNET_TIME_Relative timeout,
850 GNUNET_DATASTORE_ContinuationWithStatus cont,
853 struct GNUNET_DATASTORE_QueueEntry *qe;
854 struct UpdateMessage *um;
855 struct StatusContext *scont;
858 cont = &drop_status_cont;
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
863 (unsigned int) priority,
864 (unsigned long long) expiration.value);
866 scont = GNUNET_malloc (sizeof (struct StatusContext));
868 scont->cont_cls = cont_cls;
869 qe = make_queue_entry (h, sizeof(struct UpdateMessage),
870 queue_priority, max_queue_size, timeout,
871 &process_status_message, scont);
874 um = (struct UpdateMessage*) &qe[1];
875 um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
876 um->header.size = htons(sizeof (struct UpdateMessage));
877 um->priority = htonl(priority);
878 um->expiration = GNUNET_TIME_absolute_hton(expiration);
879 um->uid = GNUNET_htonll(uid);
886 * Explicitly remove some content from the database.
887 * The "cont"inuation will be called with status
888 * "GNUNET_OK" if content was removed, "GNUNET_NO"
889 * if no matching entry was found and "GNUNET_SYSERR"
890 * on all other types of errors.
892 * @param h handle to the datastore
893 * @param key key for the value
894 * @param size number of bytes in data
895 * @param data content stored
896 * @param queue_priority ranking of this request in the priority queue
897 * @param max_queue_size at what queue size should this request be dropped
898 * (if other requests of higher priority are in the queue)
899 * @param timeout how long to wait at most for a response
900 * @param cont continuation to call when done
901 * @param cont_cls closure for cont
902 * @return NULL if the entry was not queued, otherwise a handle that can be used to
903 * cancel; note that even if NULL is returned, the callback will be invoked
904 * (or rather, will already have been invoked)
906 struct GNUNET_DATASTORE_QueueEntry *
907 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
908 const GNUNET_HashCode *key,
911 unsigned int queue_priority,
912 unsigned int max_queue_size,
913 struct GNUNET_TIME_Relative timeout,
914 GNUNET_DATASTORE_ContinuationWithStatus cont,
917 struct GNUNET_DATASTORE_QueueEntry *qe;
918 struct DataMessage *dm;
920 struct StatusContext *scont;
923 cont = &drop_status_cont;
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926 "Asked to remove %u bytes under key `%s'\n",
930 scont = GNUNET_malloc (sizeof (struct StatusContext));
932 scont->cont_cls = cont_cls;
933 msize = sizeof(struct DataMessage) + size;
934 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
935 qe = make_queue_entry (h, msize,
936 queue_priority, max_queue_size, timeout,
937 &process_status_message, scont);
940 dm = (struct DataMessage*) &qe[1];
941 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
942 dm->header.size = htons(msize);
944 dm->size = htonl(size);
946 dm->priority = htonl(0);
947 dm->anonymity = htonl(0);
948 dm->uid = GNUNET_htonll(0);
949 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
951 memcpy (&dm[1], data, size);
959 * Context for processing result messages.
964 * Iterator to call with the result.
966 GNUNET_DATASTORE_Iterator iter;
977 * Type of a function to call when we receive a message
981 * @param msg message received, NULL on timeout or fatal error
984 process_result_message (void *cls,
985 const struct GNUNET_MessageHeader * msg)
987 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
988 struct GNUNET_DATASTORE_Handle *h = qe->h;
989 struct ResultContext *rc = qe->client_ctx;
990 const struct DataMessage *dm;
992 GNUNET_assert (h->queue_head == qe);
996 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
997 _("Failed to receive response from datastore\n"));
999 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1004 rc->iter (rc->iter_cls,
1005 NULL, 0, NULL, 0, 0, 0,
1006 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1010 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1012 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
1014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1015 "Received end of result set\n");
1017 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1021 rc->iter (rc->iter_cls,
1022 NULL, 0, NULL, 0, 0, 0,
1023 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1028 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
1029 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1030 (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
1033 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1037 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1039 rc->iter (rc->iter_cls,
1040 NULL, 0, NULL, 0, 0, 0,
1041 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1045 dm = (const struct DataMessage*) msg;
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1048 "Received result %llu with type %u and size %u with key %s\n",
1049 (unsigned long long) GNUNET_ntohll(dm->uid),
1052 GNUNET_h2s(&dm->key));
1054 rc->iter (rc->iter_cls,
1059 ntohl(dm->priority),
1060 ntohl(dm->anonymity),
1061 GNUNET_TIME_absolute_ntoh(dm->expiration),
1062 GNUNET_ntohll(dm->uid));
1067 * Get a random value from the datastore.
1069 * @param h handle to the datastore
1070 * @param queue_priority ranking of this request in the priority queue
1071 * @param max_queue_size at what queue size should this request be dropped
1072 * (if other requests of higher priority are in the queue)
1073 * @param timeout how long to wait at most for a response
1074 * @param iter function to call on a random value; it
1075 * will be called once with a value (if available)
1076 * and always once with a value of NULL.
1077 * @param iter_cls closure for iter
1078 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1079 * cancel; note that even if NULL is returned, the callback will be invoked
1080 * (or rather, will already have been invoked)
1082 struct GNUNET_DATASTORE_QueueEntry *
1083 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
1084 unsigned int queue_priority,
1085 unsigned int max_queue_size,
1086 struct GNUNET_TIME_Relative timeout,
1087 GNUNET_DATASTORE_Iterator iter,
1090 struct GNUNET_DATASTORE_QueueEntry *qe;
1091 struct GNUNET_MessageHeader *m;
1092 struct ResultContext *rcont;
1095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1096 "Asked to get random entry in %llu ms\n",
1097 (unsigned long long) timeout.value);
1099 rcont = GNUNET_malloc (sizeof (struct ResultContext));
1101 rcont->iter_cls = iter_cls;
1102 qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader),
1103 queue_priority, max_queue_size, timeout,
1104 &process_result_message, rcont);
1107 m = (struct GNUNET_MessageHeader*) &qe[1];
1108 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
1109 m->size = htons(sizeof (struct GNUNET_MessageHeader));
1117 * Iterate over the results for a particular key
1118 * in the datastore. The iterator will only be called
1119 * once initially; if the first call did contain a
1120 * result, further results can be obtained by calling
1121 * "GNUNET_DATASTORE_get_next" with the given argument.
1123 * @param h handle to the datastore
1124 * @param key maybe NULL (to match all entries)
1125 * @param type desired type, 0 for any
1126 * @param queue_priority ranking of this request in the priority queue
1127 * @param max_queue_size at what queue size should this request be dropped
1128 * (if other requests of higher priority are in the queue)
1129 * @param timeout how long to wait at most for a response
1130 * @param iter function to call on each matching value;
1131 * will be called once with a NULL value at the end
1132 * @param iter_cls closure for iter
1133 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1134 * cancel; note that even if NULL is returned, the callback will be invoked
1135 * (or rather, will already have been invoked)
1137 struct GNUNET_DATASTORE_QueueEntry *
1138 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
1139 const GNUNET_HashCode * key,
1140 enum GNUNET_BLOCK_Type type,
1141 unsigned int queue_priority,
1142 unsigned int max_queue_size,
1143 struct GNUNET_TIME_Relative timeout,
1144 GNUNET_DATASTORE_Iterator iter,
1147 struct GNUNET_DATASTORE_QueueEntry *qe;
1148 struct GetMessage *gm;
1149 struct ResultContext *rcont;
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 "Asked to look for data of type %u under key `%s'\n",
1154 (unsigned int) type,
1157 rcont = GNUNET_malloc (sizeof (struct ResultContext));
1159 rcont->iter_cls = iter_cls;
1160 qe = make_queue_entry (h, sizeof(struct GetMessage),
1161 queue_priority, max_queue_size, timeout,
1162 &process_result_message, rcont);
1165 gm = (struct GetMessage*) &qe[1];
1166 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1167 gm->type = htonl(type);
1170 gm->header.size = htons(sizeof (struct GetMessage));
1175 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
1183 * Function called to trigger obtaining the next result
1184 * from the datastore.
1186 * @param h handle to the datastore
1187 * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
1188 * iteration (with a final call to "iter" with key/data == NULL).
1191 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
1194 struct GNUNET_DATASTORE_QueueEntry *qe = h->queue_head;
1195 struct ResultContext *rc = qe->client_ctx;
1197 GNUNET_assert (NULL != qe);
1198 GNUNET_assert (&process_result_message == qe->response_proc);
1199 if (GNUNET_YES == more)
1201 GNUNET_CLIENT_receive (h->client,
1204 GNUNET_TIME_absolute_get_remaining (qe->timeout));
1207 GNUNET_CONTAINER_DLL_remove (h->queue_head,
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 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1237 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
1238 GNUNET_SCHEDULER_cancel (h->sched,
1243 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1249 /* end of datastore_api.c */