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 contX;
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->response_proc != NULL)
300 qe->response_proc (qe, NULL);
306 * Create a new entry for our priority queue (and possibly discard other entires if
307 * the queue is getting too long).
309 * @param h handle to the datastore
310 * @param msize size of the message to queue
311 * @param queue_priority priority of the entry
312 * @param max_queue_size at what queue size should this request be dropped
313 * (if other requests of higher priority are in the queue)
314 * @param timeout timeout for the operation
315 * @param response_proc function to call with replies (can be NULL)
316 * @param client_ctx client context (NOT a closure for response_proc)
317 * @return NULL if the queue is full (and this entry was dropped)
319 static struct QueueEntry *
320 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
322 unsigned int queue_priority,
323 unsigned int max_queue_size,
324 struct GNUNET_TIME_Relative timeout,
325 GNUNET_CLIENT_MessageHandler response_proc,
328 struct QueueEntry *ret;
329 struct QueueEntry *pos;
334 while ( (pos != NULL) &&
335 (c < max_queue_size) &&
336 (pos->priority >= queue_priority) )
341 if (c >= max_queue_size)
345 /* append at the tail */
351 /* do not insert at HEAD if HEAD query was already
352 transmitted and we are still receiving replies! */
353 if ( (pos == NULL) &&
354 (h->queue_head->was_transmitted) )
357 ret = GNUNET_malloc (sizeof (struct QueueEntry) + msize);
358 GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
363 ret->response_proc = response_proc;
364 ret->client_ctx = client_ctx;
365 ret->task = GNUNET_SCHEDULER_add_delayed (h->sched,
367 &timeout_queue_entry,
369 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
370 ret->priority = queue_priority;
371 ret->max_queue = max_queue_size;
372 ret->message_size = msize;
373 ret->was_transmitted = GNUNET_NO;
379 if (pos->max_queue < h->queue_size)
381 GNUNET_CONTAINER_DLL_remove (h->queue_head,
384 GNUNET_SCHEDULER_cancel (h->sched,
386 if (pos->response_proc != NULL)
387 pos->response_proc (pos, NULL);
399 * Process entries in the queue (or do nothing if we are already
402 * @param h handle to the datastore
405 process_queue (struct GNUNET_DATASTORE_Handle *h);
409 * Try reconnecting to the datastore service.
411 * @param cls the 'struct GNUNET_DATASTORE_Handle'
412 * @param tc scheduler context
415 try_reconnect (void *cls,
416 const struct GNUNET_SCHEDULER_TaskContext *tc)
418 struct GNUNET_DATASTORE_Handle *h = cls;
420 if (h->retry_time.value < GNUNET_CONSTANTS_SERVICE_RETRY.value)
421 h->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
423 h->retry_time = GNUNET_TIME_relative_multiply (h->retry_time, 2);
424 if (h->retry_time.value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.value)
425 h->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
426 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
427 h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
428 if (h->client == NULL)
435 * Disconnect from the service and then try reconnecting to the datastore service
438 * @param cls the 'struct GNUNET_DATASTORE_Handle'
439 * @param tc scheduler context
442 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
444 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
446 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->sched,
454 * Transmit request from queue to datastore service.
456 * @param cls the 'struct GNUNET_DATASTORE_Handle'
457 * @param size number of bytes that can be copied to buf
458 * @param buf where to copy the drop message
459 * @return number of bytes written to buf
462 transmit_request (void *cls,
466 struct GNUNET_DATASTORE_Handle *h = cls;
467 struct QueueEntry *qe;
471 if (NULL == (qe = h->queue_head))
472 return 0; /* no entry in queue */
475 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
476 _("Failed to transmit request to database.\n"));
480 if (size < (msize = qe->message_size))
485 memcpy (buf, &qe[1], msize);
486 qe->was_transmitted = GNUNET_YES;
487 GNUNET_SCHEDULER_cancel (h->sched,
489 qe->task = GNUNET_SCHEDULER_NO_TASK;
490 GNUNET_CLIENT_receive (h->client,
493 GNUNET_TIME_absolute_get_remaining (qe->timeout));
499 * Process entries in the queue (or do nothing if we are already
502 * @param h handle to the datastore
505 process_queue (struct GNUNET_DATASTORE_Handle *h)
507 struct QueueEntry *qe;
509 if (NULL == (qe = h->queue_head))
510 return; /* no entry in queue */
511 if (qe->was_transmitted == GNUNET_YES)
512 return; /* waiting for replies */
514 return; /* request pending */
515 if (h->client == NULL)
516 return; /* waiting for reconnect */
517 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
519 GNUNET_TIME_absolute_get_remaining (qe->timeout),
529 * Context for processing status messages.
534 * Continuation to call with the status.
536 GNUNET_DATASTORE_ContinuationWithStatus cont;
547 * Dummy continuation used to do nothing (but be non-zero).
550 * @param result result
551 * @param emsg error message
554 drop_status_cont (void *cls, int result, const char *emsg)
561 * Type of a function to call when we receive a message
565 * @param msg message received, NULL on timeout or fatal error
568 process_status_message (void *cls,
570 GNUNET_MessageHeader * msg)
572 struct QueueEntry *qe = cls;
573 struct GNUNET_DATASTORE_Handle *h = qe->h;
574 struct StatusContext *rc = qe->client_ctx;
575 const struct StatusMessage *sm;
579 GNUNET_CONTAINER_DLL_remove (h->queue_head,
585 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
586 _("Failed to receive response from database.\n"));
591 if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
592 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) )
595 h->retry_time = GNUNET_TIME_UNIT_ZERO;
597 rc->cont (rc->cont_cls,
599 _("Error reading response from datastore service"));
603 sm = (const struct StatusMessage*) msg;
604 status = ntohl(sm->status);
606 if (ntohs(msg->size) > sizeof(struct StatusMessage))
608 emsg = (const char*) &sm[1];
609 if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
612 emsg = _("Invalid error message received from datastore service");
615 if ( (status == GNUNET_SYSERR) &&
619 emsg = _("Invalid error message received from datastore service");
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
623 "Received status %d/%s\n",
627 rc->cont (rc->cont_cls,
636 * Store an item in the datastore. If the item is already present,
637 * the priorities are summed up and the higher expiration time and
638 * lower anonymity level is used.
640 * @param h handle to the datastore
641 * @param rid reservation ID to use (from "reserve"); use 0 if no
642 * prior reservation was made
643 * @param key key for the value
644 * @param size number of bytes in data
645 * @param data content stored
646 * @param type type of the content
647 * @param priority priority of the content
648 * @param anonymity anonymity-level for the content
649 * @param expiration expiration time for the content
650 * @param queue_priority ranking of this request in the priority queue
651 * @param max_queue_size at what queue size should this request be dropped
652 * (if other requests of higher priority are in the queue)
653 * @param timeout timeout for the operation
654 * @param cont continuation to call when done
655 * @param cont_cls closure for cont
658 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
660 const GNUNET_HashCode * key,
663 enum GNUNET_BLOCK_Type type,
666 struct GNUNET_TIME_Absolute expiration,
667 unsigned int queue_priority,
668 unsigned int max_queue_size,
669 struct GNUNET_TIME_Relative timeout,
670 GNUNET_DATASTORE_ContinuationWithStatus cont,
673 struct StatusContext *scont;
674 struct QueueEntry *qe;
675 struct DataMessage *dm;
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680 "Asked to put %u bytes of data under key `%s'\n",
684 msize = sizeof(struct DataMessage) + size;
685 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
686 scont = GNUNET_malloc (sizeof (struct StatusContext));
688 scont->cont_cls = cont_cls;
689 qe = make_queue_entry (h, msize,
690 queue_priority, max_queue_size, timeout,
691 &process_status_message, scont);
694 dm = (struct DataMessage* ) &qe[1];
695 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
696 dm->header.size = htons(msize);
697 dm->rid = htonl(rid);
698 dm->size = htonl(size);
699 dm->type = htonl(type);
700 dm->priority = htonl(priority);
701 dm->anonymity = htonl(anonymity);
702 dm->uid = GNUNET_htonll(0);
703 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
705 memcpy (&dm[1], data, size);
711 * Reserve space in the datastore. This function should be used
712 * to avoid "out of space" failures during a longer sequence of "put"
713 * operations (for example, when a file is being inserted).
715 * @param h handle to the datastore
716 * @param amount how much space (in bytes) should be reserved (for content only)
717 * @param entries how many entries will be created (to calculate per-entry overhead)
718 * @param queue_priority ranking of this request in the priority queue
719 * @param max_queue_size at what queue size should this request be dropped
720 * (if other requests of higher priority are in the queue)
721 * @param timeout how long to wait at most for a response (or before dying in queue)
722 * @param cont continuation to call when done; "success" will be set to
723 * a positive reservation value if space could be reserved.
724 * @param cont_cls closure for cont
727 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
730 unsigned int queue_priority,
731 unsigned int max_queue_size,
732 struct GNUNET_TIME_Relative timeout,
733 GNUNET_DATASTORE_ContinuationWithStatus cont,
736 struct QueueEntry *qe;
737 struct ReserveMessage *rm;
738 struct StatusContext *scont;
741 cont = &drop_status_cont;
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
744 "Asked to reserve %llu bytes of data and %u entries'\n",
745 (unsigned long long) amount,
746 (unsigned int) entries);
748 scont = GNUNET_malloc (sizeof (struct StatusContext));
750 scont->cont_cls = cont_cls;
751 qe = make_queue_entry (h, sizeof(struct ReserveMessage),
752 queue_priority, max_queue_size, timeout,
753 &process_status_message, scont);
756 rm = (struct ReserveMessage*) &qe[1];
757 rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
758 rm->header.size = htons(sizeof (struct ReserveMessage));
759 rm->entries = htonl(entries);
760 rm->amount = GNUNET_htonll(amount);
766 * Signal that all of the data for which a reservation was made has
767 * been stored and that whatever excess space might have been reserved
768 * can now be released.
770 * @param h handle to the datastore
771 * @param rid reservation ID (value of "success" in original continuation
772 * from the "reserve" function).
773 * @param queue_priority ranking of this request in the priority queue
774 * @param max_queue_size at what queue size should this request be dropped
775 * (if other requests of higher priority are in the queue)
776 * @param queue_priority ranking of this request in the priority queue
777 * @param max_queue_size at what queue size should this request be dropped
778 * (if other requests of higher priority are in the queue)
779 * @param timeout how long to wait at most for a response
780 * @param cont continuation to call when done
781 * @param cont_cls closure for cont
784 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
786 unsigned int queue_priority,
787 unsigned int max_queue_size,
788 struct GNUNET_TIME_Relative timeout,
789 GNUNET_DATASTORE_ContinuationWithStatus cont,
792 struct QueueEntry *qe;
793 struct ReleaseReserveMessage *rrm;
794 struct StatusContext *scont;
797 cont = &drop_status_cont;
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 "Asked to release reserve %d\n",
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 &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 cont = &drop_status_cont;
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
852 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
854 (unsigned int) priority,
855 (unsigned long long) expiration.value);
857 scont = GNUNET_malloc (sizeof (struct StatusContext));
859 scont->cont_cls = cont_cls;
860 qe = make_queue_entry (h, sizeof(struct UpdateMessage),
861 queue_priority, max_queue_size, timeout,
862 &process_status_message, scont);
865 um = (struct UpdateMessage*) &qe[1];
866 um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
867 um->header.size = htons(sizeof (struct UpdateMessage));
868 um->priority = htonl(priority);
869 um->expiration = GNUNET_TIME_absolute_hton(expiration);
870 um->uid = GNUNET_htonll(uid);
876 * Explicitly remove some content from the database.
877 * The "cont"inuation will be called with status
878 * "GNUNET_OK" if content was removed, "GNUNET_NO"
879 * if no matching entry was found and "GNUNET_SYSERR"
880 * on all other types of errors.
882 * @param h handle to the datastore
883 * @param key key for the value
884 * @param size number of bytes in data
885 * @param data content stored
886 * @param queue_priority ranking of this request in the priority queue
887 * @param max_queue_size at what queue size should this request be dropped
888 * (if other requests of higher priority are in the queue)
889 * @param timeout how long to wait at most for a response
890 * @param cont continuation to call when done
891 * @param cont_cls closure for cont
894 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
895 const GNUNET_HashCode *key,
898 unsigned int queue_priority,
899 unsigned int max_queue_size,
900 struct GNUNET_TIME_Relative timeout,
901 GNUNET_DATASTORE_ContinuationWithStatus cont,
904 struct QueueEntry *qe;
905 struct DataMessage *dm;
907 struct StatusContext *scont;
910 cont = &drop_status_cont;
912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
913 "Asked to remove %u bytes under key `%s'\n",
917 scont = GNUNET_malloc (sizeof (struct StatusContext));
919 scont->cont_cls = cont_cls;
920 msize = sizeof(struct DataMessage) + size;
921 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
922 qe = make_queue_entry (h, msize,
923 queue_priority, max_queue_size, timeout,
924 &process_status_message, scont);
927 dm = (struct DataMessage*) &qe[1];
928 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
929 dm->header.size = htons(msize);
931 dm->size = htonl(size);
933 dm->priority = htonl(0);
934 dm->anonymity = htonl(0);
935 dm->uid = GNUNET_htonll(0);
936 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
938 memcpy (&dm[1], data, size);
945 * Context for processing result messages.
950 * Iterator to call with the result.
952 GNUNET_DATASTORE_Iterator iter;
963 * Type of a function to call when we receive a message
967 * @param msg message received, NULL on timeout or fatal error
970 process_result_message (void *cls,
971 const struct GNUNET_MessageHeader * msg)
973 struct QueueEntry *qe = cls;
974 struct GNUNET_DATASTORE_Handle *h = qe->h;
975 struct ResultContext *rc = qe->client_ctx;
976 const struct DataMessage *dm;
981 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
982 _("Failed to receive response from datastore\n"));
984 GNUNET_CONTAINER_DLL_remove (h->queue_head,
989 rc->iter (rc->iter_cls,
990 NULL, 0, NULL, 0, 0, 0,
991 GNUNET_TIME_UNIT_ZERO_ABS, 0);
995 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
997 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1000 "Received end of result set\n");
1002 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1006 rc->iter (rc->iter_cls,
1007 NULL, 0, NULL, 0, 0, 0,
1008 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1013 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
1014 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1015 (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
1018 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1022 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1024 rc->iter (rc->iter_cls,
1025 NULL, 0, NULL, 0, 0, 0,
1026 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1030 dm = (const struct DataMessage*) msg;
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "Received result %llu with type %u and size %u with key %s\n",
1034 (unsigned long long) GNUNET_ntohll(dm->uid),
1037 GNUNET_h2s(&dm->key));
1039 rc->iter (rc->iter_cls,
1044 ntohl(dm->priority),
1045 ntohl(dm->anonymity),
1046 GNUNET_TIME_absolute_ntoh(dm->expiration),
1047 GNUNET_ntohll(dm->uid));
1052 * Get a random value from the datastore.
1054 * @param h handle to the datastore
1055 * @param queue_priority ranking of this request in the priority queue
1056 * @param max_queue_size at what queue size should this request be dropped
1057 * (if other requests of higher priority are in the queue)
1058 * @param timeout how long to wait at most for a response
1059 * @param iter function to call on a random value; it
1060 * will be called once with a value (if available)
1061 * and always once with a value of NULL.
1062 * @param iter_cls closure for iter
1065 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
1066 unsigned int queue_priority,
1067 unsigned int max_queue_size,
1068 struct GNUNET_TIME_Relative timeout,
1069 GNUNET_DATASTORE_Iterator iter,
1072 struct QueueEntry *qe;
1073 struct GNUNET_MessageHeader *m;
1074 struct ResultContext *rcont;
1077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1078 "Asked to get random entry in %llu ms\n",
1079 (unsigned long long) timeout.value);
1081 rcont = GNUNET_malloc (sizeof (struct ResultContext));
1083 rcont->iter_cls = iter_cls;
1084 qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader),
1085 queue_priority, max_queue_size, timeout,
1086 &process_result_message, rcont);
1089 m = (struct GNUNET_MessageHeader*) &qe[1];
1090 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
1091 m->size = htons(sizeof (struct GNUNET_MessageHeader));
1098 * Iterate over the results for a particular key
1099 * in the datastore. The iterator will only be called
1100 * once initially; if the first call did contain a
1101 * result, further results can be obtained by calling
1102 * "GNUNET_DATASTORE_get_next" with the given argument.
1104 * @param h handle to the datastore
1105 * @param key maybe NULL (to match all entries)
1106 * @param type desired type, 0 for any
1107 * @param queue_priority ranking of this request in the priority queue
1108 * @param max_queue_size at what queue size should this request be dropped
1109 * (if other requests of higher priority are in the queue)
1110 * @param timeout how long to wait at most for a response
1111 * @param iter function to call on each matching value;
1112 * will be called once with a NULL value at the end
1113 * @param iter_cls closure for iter
1116 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
1117 const GNUNET_HashCode * key,
1118 enum GNUNET_BLOCK_Type type,
1119 unsigned int queue_priority,
1120 unsigned int max_queue_size,
1121 struct GNUNET_TIME_Relative timeout,
1122 GNUNET_DATASTORE_Iterator iter,
1125 struct QueueEntry *qe;
1126 struct GetMessage *gm;
1127 struct ResultContext *rcont;
1130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1131 "Asked to look for data of type %u under key `%s'\n",
1132 (unsigned int) type,
1135 rcont = GNUNET_malloc (sizeof (struct ResultContext));
1137 rcont->iter_cls = iter_cls;
1138 qe = make_queue_entry (h, sizeof(struct GetMessage),
1139 queue_priority, max_queue_size, timeout,
1140 &process_result_message, rcont);
1143 gm = (struct GetMessage*) &qe[1];
1144 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1145 gm->type = htonl(type);
1148 gm->header.size = htons(sizeof (struct GetMessage));
1153 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
1160 * Function called to trigger obtaining the next result
1161 * from the datastore.
1163 * @param h handle to the datastore
1164 * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
1165 * iteration (with a final call to "iter" with key/data == NULL).
1168 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
1171 struct QueueEntry *qe = h->queue_head;
1172 struct ResultContext *rc = qe->client_ctx;
1174 GNUNET_assert (NULL != qe);
1175 GNUNET_assert (&process_result_message == qe->response_proc);
1176 if (GNUNET_YES == more)
1178 GNUNET_CLIENT_receive (h->client,
1181 GNUNET_TIME_absolute_get_remaining (qe->timeout));
1184 GNUNET_CONTAINER_DLL_remove (h->queue_head,
1188 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1190 rc->iter (rc->iter_cls,
1191 NULL, 0, NULL, 0, 0, 0,
1192 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1197 /* end of datastore_api.c */