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.
40 * This is a linked list.
42 struct QueueEntry *next;
45 * This is a linked list.
47 struct 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 QueueEntry *queue_head;
147 * Current tail of priority queue.
149 struct 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 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 QueueEntry'
285 * @param tc scheduler context
288 timeout_queue_entry (void *cls,
289 const struct GNUNET_SCHEDULER_TaskContext *tc)
291 struct QueueEntry *qe = cls;
292 struct GNUNET_DATASTORE_Handle *h = qe->h;
294 qe->task = GNUNET_SCHEDULER_NO_TASK;
295 GNUNET_assert (qe->was_transmitted == GNUNET_NO);
296 GNUNET_CONTAINER_DLL_remove (h->queue_head,
299 if (qe->cont != NULL)
300 qe->cont (qe->cont_cls, GNUNET_NO, _("timeout"));
301 if (qe->response_proc != NULL)
302 qe->response_proc (qe, NULL);
308 * Create a new entry for our priority queue (and possibly discard other entires if
309 * the queue is getting too long).
311 * @param h handle to the datastore
312 * @param msize size of the message to queue
313 * @param queue_priority priority of the entry
314 * @param max_queue_size at what queue size should this request be dropped
315 * (if other requests of higher priority are in the queue)
316 * @param timeout timeout for the operation
317 * @param cont continuation to call when done with request transmission (can be NULL)
318 * @param cont_cls closure for cont
319 * @param response_proc function to call with replies (can be NULL)
320 * @param client_ctx client context (NOT a closure for response_proc)
321 * @return NULL if the queue is full (and this entry was dropped)
323 static struct QueueEntry *
324 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
326 unsigned int queue_priority,
327 unsigned int max_queue_size,
328 struct GNUNET_TIME_Relative timeout,
329 GNUNET_DATASTORE_ContinuationWithStatus cont,
331 GNUNET_CLIENT_MessageHandler response_proc,
334 struct QueueEntry *ret;
335 struct QueueEntry *pos;
340 while ( (pos != NULL) &&
341 (c < max_queue_size) &&
342 (pos->priority >= queue_priority) )
347 if (c >= max_queue_size)
351 /* append at the tail */
357 /* do not insert at HEAD if HEAD query was already
358 transmitted and we are still receiving replies! */
359 if ( (pos == NULL) &&
360 (h->queue_head->was_transmitted) )
363 ret = GNUNET_malloc (sizeof (struct QueueEntry) + msize);
364 GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
369 ret->response_proc = response_proc;
370 ret->client_ctx = client_ctx;
372 ret->cont_cls = cont_cls;
373 ret->task = GNUNET_SCHEDULER_add_delayed (h->sched,
375 &timeout_queue_entry,
377 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
378 ret->priority = queue_priority;
379 ret->max_queue = max_queue_size;
380 ret->message_size = msize;
381 ret->was_transmitted = GNUNET_NO;
387 if (pos->max_queue < h->queue_size)
389 GNUNET_CONTAINER_DLL_remove (h->queue_head,
392 GNUNET_SCHEDULER_cancel (h->sched,
394 if (pos->cont != NULL)
395 pos->cont (pos->cont_cls, GNUNET_NO, _("Message queue full"));
396 if (pos->response_proc != NULL)
397 pos->response_proc (pos, NULL);
409 * Process entries in the queue (or do nothing if we are already
412 * @param h handle to the datastore
415 process_queue (struct GNUNET_DATASTORE_Handle *h);
419 * Try reconnecting to the datastore service.
421 * @param cls the 'struct GNUNET_DATASTORE_Handle'
422 * @param tc scheduler context
425 try_reconnect (void *cls,
426 const struct GNUNET_SCHEDULER_TaskContext *tc)
428 struct GNUNET_DATASTORE_Handle *h = cls;
430 if (h->retry_time.value < GNUNET_CONSTANTS_SERVICE_RETRY.value)
431 h->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
433 h->retry_time = GNUNET_TIME_relative_multiply (h->retry_time, 2);
434 if (h->retry_time.value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.value)
435 h->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
436 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
437 h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
438 if (h->client == NULL)
445 * Disconnect from the service and then try reconnecting to the datastore service
448 * @param cls the 'struct GNUNET_DATASTORE_Handle'
449 * @param tc scheduler context
452 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
454 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
456 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->sched,
464 * Transmit request from queue to datastore service.
466 * @param cls the 'struct GNUNET_DATASTORE_Handle'
467 * @param size number of bytes that can be copied to buf
468 * @param buf where to copy the drop message
469 * @return number of bytes written to buf
472 transmit_request (void *cls,
476 struct GNUNET_DATASTORE_Handle *h = cls;
477 struct QueueEntry *qe;
481 if (NULL == (qe = h->queue_head))
482 return 0; /* no entry in queue */
485 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
486 _("Failed to transmit request to database.\n"));
490 if (size < (msize = qe->message_size))
495 memcpy (buf, &qe[1], msize);
496 qe->was_transmitted = GNUNET_YES;
497 if (qe->cont != NULL)
498 qe->cont (qe->cont_cls, GNUNET_OK, NULL);
499 GNUNET_SCHEDULER_cancel (h->sched,
501 qe->task = GNUNET_SCHEDULER_NO_TASK;
502 if (qe->response_proc == NULL)
504 GNUNET_CONTAINER_DLL_remove (h->queue_head,
512 GNUNET_CLIENT_receive (h->client,
515 GNUNET_TIME_absolute_get_remaining (qe->timeout));
522 * Process entries in the queue (or do nothing if we are already
525 * @param h handle to the datastore
528 process_queue (struct GNUNET_DATASTORE_Handle *h)
530 struct QueueEntry *qe;
532 if (NULL == (qe = h->queue_head))
533 return; /* no entry in queue */
534 if (qe->was_transmitted == GNUNET_YES)
535 return; /* waiting for replies */
537 return; /* request pending */
538 if (h->client == NULL)
539 return; /* waiting for reconnect */
540 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
542 GNUNET_TIME_absolute_get_remaining (qe->timeout),
550 * Store an item in the datastore. If the item is already present,
551 * the priorities are summed up and the higher expiration time and
552 * lower anonymity level is used.
554 * @param h handle to the datastore
555 * @param rid reservation ID to use (from "reserve"); use 0 if no
556 * prior reservation was made
557 * @param key key for the value
558 * @param size number of bytes in data
559 * @param data content stored
560 * @param type type of the content
561 * @param priority priority of the content
562 * @param anonymity anonymity-level for the content
563 * @param expiration expiration time for the content
564 * @param queue_priority ranking of this request in the priority queue
565 * @param max_queue_size at what queue size should this request be dropped
566 * (if other requests of higher priority are in the queue)
567 * @param timeout timeout for the operation
568 * @param cont continuation to call when done
569 * @param cont_cls closure for cont
572 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
574 const GNUNET_HashCode * key,
577 enum GNUNET_BLOCK_Type type,
580 struct GNUNET_TIME_Absolute expiration,
581 unsigned int queue_priority,
582 unsigned int max_queue_size,
583 struct GNUNET_TIME_Relative timeout,
584 GNUNET_DATASTORE_ContinuationWithStatus cont,
587 struct QueueEntry *qe;
588 struct DataMessage *dm;
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593 "Asked to put %u bytes of data under key `%s'\n",
597 msize = sizeof(struct DataMessage) + size;
598 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
599 qe = make_queue_entry (h, msize,
600 queue_priority, max_queue_size, timeout,
601 cont, cont_cls, NULL, NULL);
604 dm = (struct DataMessage* ) &qe[1];
605 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
606 dm->header.size = htons(msize);
607 dm->rid = htonl(rid);
608 dm->size = htonl(size);
609 dm->type = htonl(type);
610 dm->priority = htonl(priority);
611 dm->anonymity = htonl(anonymity);
612 dm->uid = GNUNET_htonll(0);
613 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
615 memcpy (&dm[1], data, size);
621 * Context for processing status messages.
626 * Continuation to call with the status.
628 GNUNET_DATASTORE_ContinuationWithStatus cont;
639 * Type of a function to call when we receive a message
643 * @param msg message received, NULL on timeout or fatal error
646 process_status_message (void *cls,
648 GNUNET_MessageHeader * msg)
650 struct QueueEntry *qe = cls;
651 struct GNUNET_DATASTORE_Handle *h = qe->h;
652 struct StatusContext *rc = qe->client_ctx;
653 const struct StatusMessage *sm;
657 GNUNET_CONTAINER_DLL_remove (h->queue_head,
663 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
664 _("Failed to receive response from database.\n"));
669 if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
670 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) )
673 h->retry_time = GNUNET_TIME_UNIT_ZERO;
675 rc->cont (rc->cont_cls,
677 _("Error reading response from datastore service"));
681 sm = (const struct StatusMessage*) msg;
682 status = ntohl(sm->status);
684 if (ntohs(msg->size) > sizeof(struct StatusMessage))
686 emsg = (const char*) &sm[1];
687 if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
690 emsg = _("Invalid error message received from datastore service");
693 if ( (status == GNUNET_SYSERR) &&
697 emsg = _("Invalid error message received from datastore service");
700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
701 "Received status %d/%s\n",
705 rc->cont (rc->cont_cls,
714 * Reserve space in the datastore. This function should be used
715 * to avoid "out of space" failures during a longer sequence of "put"
716 * operations (for example, when a file is being inserted).
718 * @param h handle to the datastore
719 * @param amount how much space (in bytes) should be reserved (for content only)
720 * @param entries how many entries will be created (to calculate per-entry overhead)
721 * @param queue_priority ranking of this request in the priority queue
722 * @param max_queue_size at what queue size should this request be dropped
723 * (if other requests of higher priority are in the queue)
724 * @param timeout how long to wait at most for a response (or before dying in queue)
725 * @param cont continuation to call when done; "success" will be set to
726 * a positive reservation value if space could be reserved.
727 * @param cont_cls closure for cont
730 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
733 unsigned int queue_priority,
734 unsigned int max_queue_size,
735 struct GNUNET_TIME_Relative timeout,
736 GNUNET_DATASTORE_ContinuationWithStatus cont,
739 struct QueueEntry *qe;
740 struct ReserveMessage *rm;
741 struct StatusContext *scont;
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 NULL, NULL, &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);
767 * Signal that all of the data for which a reservation was made has
768 * been stored and that whatever excess space might have been reserved
769 * can now be released.
771 * @param h handle to the datastore
772 * @param rid reservation ID (value of "success" in original continuation
773 * from the "reserve" function).
774 * @param queue_priority ranking of this request in the priority queue
775 * @param max_queue_size at what queue size should this request be dropped
776 * (if other requests of higher priority are in the queue)
777 * @param queue_priority ranking of this request in the priority queue
778 * @param max_queue_size at what queue size should this request be dropped
779 * (if other requests of higher priority are in the queue)
780 * @param timeout how long to wait at most for a response
781 * @param cont continuation to call when done
782 * @param cont_cls closure for cont
785 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
787 unsigned int queue_priority,
788 unsigned int max_queue_size,
789 struct GNUNET_TIME_Relative timeout,
790 GNUNET_DATASTORE_ContinuationWithStatus cont,
793 struct QueueEntry *qe;
794 struct ReleaseReserveMessage *rrm;
795 struct StatusContext *scont;
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Asked to reserve %llu bytes of data and %u entries'\n",
800 (unsigned long long) amount,
801 (unsigned int) entries);
803 scont = GNUNET_malloc (sizeof (struct StatusContext));
805 scont->cont_cls = cont_cls;
806 qe = make_queue_entry (h, sizeof(struct ReleaseReserveMessage),
807 queue_priority, max_queue_size, timeout,
808 NULL, NULL, &process_status_message, scont);
811 rrm = (struct ReleaseReserveMessage*) &qe[1];
812 rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
813 rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
814 rrm->rid = htonl(rid);
820 * Update a value in the datastore.
822 * @param h handle to the datastore
823 * @param uid identifier for the value
824 * @param priority how much to increase the priority of the value
825 * @param expiration new expiration value should be MAX of existing and this argument
826 * @param queue_priority ranking of this request in the priority queue
827 * @param max_queue_size at what queue size should this request be dropped
828 * (if other requests of higher priority are in the queue)
829 * @param timeout how long to wait at most for a response
830 * @param cont continuation to call when done
831 * @param cont_cls closure for cont
834 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
835 unsigned long long uid,
837 struct GNUNET_TIME_Absolute expiration,
838 unsigned int queue_priority,
839 unsigned int max_queue_size,
840 struct GNUNET_TIME_Relative timeout,
841 GNUNET_DATASTORE_ContinuationWithStatus cont,
844 struct QueueEntry *qe;
845 struct UpdateMessage *um;
846 struct StatusContext *scont;
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
850 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
852 (unsigned int) priority,
853 (unsigned long long) expiration.value);
855 scont = GNUNET_malloc (sizeof (struct StatusContext));
857 scont->cont_cls = cont_cls;
858 qe = make_queue_entry (h, sizeof(struct UpdateMessage),
859 queue_priority, max_queue_size, timeout,
860 NULL, NULL, &process_status_message, scont);
863 um = (struct UpdateMessage*) &qe[1];
864 um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
865 um->header.size = htons(sizeof (struct UpdateMessage));
866 um->priority = htonl(priority);
867 um->expiration = GNUNET_TIME_absolute_hton(expiration);
868 um->uid = GNUNET_htonll(uid);
874 * Explicitly remove some content from the database.
875 * The "cont"inuation will be called with status
876 * "GNUNET_OK" if content was removed, "GNUNET_NO"
877 * if no matching entry was found and "GNUNET_SYSERR"
878 * on all other types of errors.
880 * @param h handle to the datastore
881 * @param key key for the value
882 * @param size number of bytes in data
883 * @param data content stored
884 * @param queue_priority ranking of this request in the priority queue
885 * @param max_queue_size at what queue size should this request be dropped
886 * (if other requests of higher priority are in the queue)
887 * @param timeout how long to wait at most for a response
888 * @param cont continuation to call when done
889 * @param cont_cls closure for cont
892 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
893 const GNUNET_HashCode *key,
896 unsigned int queue_priority,
897 unsigned int max_queue_size,
898 struct GNUNET_TIME_Relative timeout,
899 GNUNET_DATASTORE_ContinuationWithStatus cont,
902 struct QueueEntry *qe;
903 struct DataMessage *dm;
905 struct StatusContext *scont;
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
909 "Asked to remove %u bytes under key `%s'\n",
913 scont = GNUNET_malloc (sizeof (struct StatusContext));
915 scont->cont_cls = cont_cls;
916 msize = sizeof(struct DataMessage) + size;
917 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
918 qe = make_queue_entry (h, msize,
919 queue_priority, max_queue_size, timeout,
920 NULL, NULL, &process_status_message, scont);
923 dm = (struct DataMessage*) &qe[1];
924 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
925 dm->header.size = htons(msize);
927 dm->size = htonl(size);
929 dm->priority = htonl(0);
930 dm->anonymity = htonl(0);
931 dm->uid = GNUNET_htonll(0);
932 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
934 memcpy (&dm[1], data, size);
941 * Context for processing result messages.
946 * Iterator to call with the result.
948 GNUNET_DATASTORE_Iterator iter;
956 * Automatically get the next result, or wait for a call to
957 * GNUNET_DATASTORE_get_next? GNUNET_YES means we automatically
958 * get the next one (if there are more).
966 * Type of a function to call when we receive a message
970 * @param msg message received, NULL on timeout or fatal error
973 process_result_message (void *cls,
974 const struct GNUNET_MessageHeader * msg)
976 struct QueueEntry *qe = cls;
977 struct GNUNET_DATASTORE_Handle *h = qe->h;
978 struct ResultContext *rc = qe->client_ctx;
979 const struct DataMessage *dm;
984 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
985 _("Failed to receive response from datastore\n"));
987 GNUNET_CONTAINER_DLL_remove (h->queue_head,
992 rc->iter (rc->iter_cls,
993 NULL, 0, NULL, 0, 0, 0,
994 GNUNET_TIME_UNIT_ZERO_ABS, 0);
998 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1000 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1003 "Received end of result set\n");
1005 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1009 rc->iter (rc->iter_cls,
1010 NULL, 0, NULL, 0, 0, 0,
1011 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1016 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
1017 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1018 (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
1021 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1025 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1027 rc->iter (rc->iter_cls,
1028 NULL, 0, NULL, 0, 0, 0,
1029 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1033 dm = (const struct DataMessage*) msg;
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 "Received result %llu with type %u and size %u with key %s\n",
1037 (unsigned long long) GNUNET_ntohll(dm->uid),
1040 GNUNET_h2s(&dm->key));
1042 rc->iter (rc->iter_cls,
1047 ntohl(dm->priority),
1048 ntohl(dm->anonymity),
1049 GNUNET_TIME_absolute_ntoh(dm->expiration),
1050 GNUNET_ntohll(dm->uid));
1051 if (rc->get_next == GNUNET_YES)
1052 GNUNET_CLIENT_receive (h->client,
1055 GNUNET_TIME_absolute_get_remaining (qe->timeout));
1060 * Get a random value from the datastore.
1062 * @param h handle to the datastore
1063 * @param queue_priority ranking of this request in the priority queue
1064 * @param max_queue_size at what queue size should this request be dropped
1065 * (if other requests of higher priority are in the queue)
1066 * @param timeout how long to wait at most for a response
1067 * @param iter function to call on a random value; it
1068 * will be called once with a value (if available)
1069 * and always once with a value of NULL.
1070 * @param iter_cls closure for iter
1073 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
1074 unsigned int queue_priority,
1075 unsigned int max_queue_size,
1076 struct GNUNET_TIME_Relative timeout,
1077 GNUNET_DATASTORE_Iterator iter,
1080 struct QueueEntry *qe;
1081 struct GNUNET_MessageHeader *m;
1082 struct ResultContext *rcont;
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086 "Asked to get random entry in %llu ms\n",
1087 (unsigned long long) timeout.value);
1089 rcont = GNUNET_malloc (sizeof (struct ResultContext));
1091 rcont->iter_cls = iter_cls;
1092 rcont->get_next = GNUNET_YES;
1093 qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader),
1094 queue_priority, max_queue_size, timeout,
1095 NULL, NULL, &process_result_message, rcont);
1098 m = (struct GNUNET_MessageHeader*) &qe[1];
1099 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
1100 m->size = htons(sizeof (struct GNUNET_MessageHeader));
1107 * Iterate over the results for a particular key
1108 * in the datastore. The iterator will only be called
1109 * once initially; if the first call did contain a
1110 * result, further results can be obtained by calling
1111 * "GNUNET_DATASTORE_get_next" with the given argument.
1113 * @param h handle to the datastore
1114 * @param key maybe NULL (to match all entries)
1115 * @param type desired type, 0 for any
1116 * @param queue_priority ranking of this request in the priority queue
1117 * @param max_queue_size at what queue size should this request be dropped
1118 * (if other requests of higher priority are in the queue)
1119 * @param timeout how long to wait at most for a response
1120 * @param iter function to call on each matching value;
1121 * will be called once with a NULL value at the end
1122 * @param iter_cls closure for iter
1125 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
1126 const GNUNET_HashCode * key,
1127 enum GNUNET_BLOCK_Type type,
1128 unsigned int queue_priority,
1129 unsigned int max_queue_size,
1130 struct GNUNET_TIME_Relative timeout,
1131 GNUNET_DATASTORE_Iterator iter,
1134 struct QueueEntry *qe;
1135 struct GetMessage *gm;
1136 struct ResultContext *rcont;
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 "Asked to look for data of type %u under key `%s'\n",
1141 (unsigned int) type,
1144 rcont = GNUNET_malloc (sizeof (struct ResultContext));
1146 rcont->iter_cls = iter_cls;
1147 rcont->get_next = GNUNET_NO;
1148 qe = make_queue_entry (h, sizeof(struct GetMessage),
1149 queue_priority, max_queue_size, timeout,
1150 NULL, NULL, &process_result_message, rcont);
1153 gm = (struct GetMessage*) &qe[1];
1154 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1155 gm->type = htonl(type);
1158 gm->header.size = htons(sizeof (struct GetMessage));
1163 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
1170 * Function called to trigger obtaining the next result
1171 * from the datastore.
1173 * @param h handle to the datastore
1174 * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
1175 * iteration (with a final call to "iter" with key/data == NULL).
1178 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
1181 struct QueueEntry *qe = h->queue_head;
1182 struct ResultContext *rc = qe->client_ctx;
1184 GNUNET_assert (NULL != qe);
1185 GNUNET_assert (&process_result_message == qe->response_proc);
1186 GNUNET_assert (rc->get_next == GNUNET_NO);
1187 if (GNUNET_YES == more)
1189 GNUNET_CLIENT_receive (h->client,
1192 GNUNET_TIME_absolute_get_remaining (qe->timeout));
1195 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1199 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1201 rc->iter (rc->iter_cls,
1202 NULL, 0, NULL, 0, 0, 0,
1203 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1208 /* end of datastore_api.c */