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 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
338 ret->response_proc = response_proc;
339 ret->client_ctx = client_ctx;
340 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
341 ret->priority = queue_priority;
342 ret->max_queue = max_queue_size;
343 ret->message_size = msize;
344 ret->was_transmitted = GNUNET_NO;
347 /* append at the tail */
353 /* do not insert at HEAD if HEAD query was already
354 transmitted and we are still receiving replies! */
355 if ( (pos == NULL) &&
356 (h->queue_head->was_transmitted) )
360 GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
365 if (c > max_queue_size)
367 response_proc (ret, NULL);
371 ret->task = GNUNET_SCHEDULER_add_delayed (h->sched,
373 &timeout_queue_entry,
378 if (pos->max_queue < h->queue_size)
380 GNUNET_assert (pos->response_proc != NULL);
381 pos->response_proc (pos, NULL);
391 * Process entries in the queue (or do nothing if we are already
394 * @param h handle to the datastore
397 process_queue (struct GNUNET_DATASTORE_Handle *h);
401 * Try reconnecting to the datastore service.
403 * @param cls the 'struct GNUNET_DATASTORE_Handle'
404 * @param tc scheduler context
407 try_reconnect (void *cls,
408 const struct GNUNET_SCHEDULER_TaskContext *tc)
410 struct GNUNET_DATASTORE_Handle *h = cls;
412 if (h->retry_time.value < GNUNET_CONSTANTS_SERVICE_RETRY.value)
413 h->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
415 h->retry_time = GNUNET_TIME_relative_multiply (h->retry_time, 2);
416 if (h->retry_time.value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.value)
417 h->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
418 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
419 h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
420 if (h->client == NULL)
427 * Disconnect from the service and then try reconnecting to the datastore service
430 * @param h handle to datastore to disconnect and reconnect
433 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
435 if (h->client == NULL)
437 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
439 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->sched,
447 * Transmit request from queue to datastore service.
449 * @param cls the 'struct GNUNET_DATASTORE_Handle'
450 * @param size number of bytes that can be copied to buf
451 * @param buf where to copy the drop message
452 * @return number of bytes written to buf
455 transmit_request (void *cls,
459 struct GNUNET_DATASTORE_Handle *h = cls;
460 struct GNUNET_DATASTORE_QueueEntry *qe;
464 if (NULL == (qe = h->queue_head))
465 return 0; /* no entry in queue */
468 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
469 _("Failed to transmit request to database.\n"));
473 if (size < (msize = qe->message_size))
478 memcpy (buf, &qe[1], msize);
479 qe->was_transmitted = GNUNET_YES;
480 GNUNET_SCHEDULER_cancel (h->sched,
482 qe->task = GNUNET_SCHEDULER_NO_TASK;
483 GNUNET_CLIENT_receive (h->client,
486 GNUNET_TIME_absolute_get_remaining (qe->timeout));
492 * Process entries in the queue (or do nothing if we are already
495 * @param h handle to the datastore
498 process_queue (struct GNUNET_DATASTORE_Handle *h)
500 struct GNUNET_DATASTORE_QueueEntry *qe;
502 if (NULL == (qe = h->queue_head))
503 return; /* no entry in queue */
504 if (qe->was_transmitted == GNUNET_YES)
505 return; /* waiting for replies */
507 return; /* request pending */
508 if (h->client == NULL)
509 return; /* waiting for reconnect */
510 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
512 GNUNET_TIME_absolute_get_remaining (qe->timeout),
522 * Context for processing status messages.
527 * Continuation to call with the status.
529 GNUNET_DATASTORE_ContinuationWithStatus cont;
540 * Dummy continuation used to do nothing (but be non-zero).
543 * @param result result
544 * @param emsg error message
547 drop_status_cont (void *cls, int result, const char *emsg)
554 free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
556 struct GNUNET_DATASTORE_Handle *h = qe->h;
558 GNUNET_CONTAINER_DLL_remove (h->queue_head,
561 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
563 GNUNET_SCHEDULER_cancel (h->sched,
565 qe->task = GNUNET_SCHEDULER_NO_TASK;
572 * Type of a function to call when we receive a message
576 * @param msg message received, NULL on timeout or fatal error
579 process_status_message (void *cls,
581 GNUNET_MessageHeader * msg)
583 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
584 struct GNUNET_DATASTORE_Handle *h = qe->h;
585 struct StatusContext *rc = qe->client_ctx;
586 const struct StatusMessage *sm;
590 free_queue_entry (qe);
593 if (NULL == h->client)
594 return; /* forced disconnect */
595 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
596 _("Failed to receive response from database.\n"));
601 if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
602 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) )
605 h->retry_time = GNUNET_TIME_UNIT_ZERO;
607 rc->cont (rc->cont_cls,
609 _("Error reading response from datastore service"));
613 sm = (const struct StatusMessage*) msg;
614 status = ntohl(sm->status);
616 if (ntohs(msg->size) > sizeof(struct StatusMessage))
618 emsg = (const char*) &sm[1];
619 if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
622 emsg = _("Invalid error message received from datastore service");
625 if ( (status == GNUNET_SYSERR) &&
629 emsg = _("Invalid error message received from datastore service");
632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
633 "Received status %d/%s\n",
637 rc->cont (rc->cont_cls,
646 * Store an item in the datastore. If the item is already present,
647 * the priorities are summed up and the higher expiration time and
648 * lower anonymity level is used.
650 * @param h handle to the datastore
651 * @param rid reservation ID to use (from "reserve"); use 0 if no
652 * prior reservation was made
653 * @param key key for the value
654 * @param size number of bytes in data
655 * @param data content stored
656 * @param type type of the content
657 * @param priority priority of the content
658 * @param anonymity anonymity-level for the content
659 * @param expiration expiration time for the content
660 * @param queue_priority ranking of this request in the priority queue
661 * @param max_queue_size at what queue size should this request be dropped
662 * (if other requests of higher priority are in the queue)
663 * @param timeout timeout for the operation
664 * @param cont continuation to call when done
665 * @param cont_cls closure for cont
666 * @return NULL if the entry was not queued, otherwise a handle that can be used to
667 * cancel; note that even if NULL is returned, the callback will be invoked
668 * (or rather, will already have been invoked)
670 struct GNUNET_DATASTORE_QueueEntry *
671 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
673 const GNUNET_HashCode * key,
676 enum GNUNET_BLOCK_Type type,
679 struct GNUNET_TIME_Absolute expiration,
680 unsigned int queue_priority,
681 unsigned int max_queue_size,
682 struct GNUNET_TIME_Relative timeout,
683 GNUNET_DATASTORE_ContinuationWithStatus cont,
686 struct StatusContext *scont;
687 struct GNUNET_DATASTORE_QueueEntry *qe;
688 struct DataMessage *dm;
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
693 "Asked to put %u bytes of data under key `%s'\n",
697 msize = sizeof(struct DataMessage) + size;
698 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
699 scont = GNUNET_malloc (sizeof (struct StatusContext));
701 scont->cont_cls = cont_cls;
702 qe = make_queue_entry (h, msize,
703 queue_priority, max_queue_size, timeout,
704 &process_status_message, scont);
707 dm = (struct DataMessage* ) &qe[1];
708 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
709 dm->header.size = htons(msize);
710 dm->rid = htonl(rid);
711 dm->size = htonl(size);
712 dm->type = htonl(type);
713 dm->priority = htonl(priority);
714 dm->anonymity = htonl(anonymity);
715 dm->uid = GNUNET_htonll(0);
716 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
718 memcpy (&dm[1], data, size);
725 * Reserve space in the datastore. This function should be used
726 * to avoid "out of space" failures during a longer sequence of "put"
727 * operations (for example, when a file is being inserted).
729 * @param h handle to the datastore
730 * @param amount how much space (in bytes) should be reserved (for content only)
731 * @param entries how many entries will be created (to calculate per-entry overhead)
732 * @param queue_priority ranking of this request in the priority queue
733 * @param max_queue_size at what queue size should this request be dropped
734 * (if other requests of higher priority are in the queue)
735 * @param timeout how long to wait at most for a response (or before dying in queue)
736 * @param cont continuation to call when done; "success" will be set to
737 * a positive reservation value if space could be reserved.
738 * @param cont_cls closure for cont
739 * @return NULL if the entry was not queued, otherwise a handle that can be used to
740 * cancel; note that even if NULL is returned, the callback will be invoked
741 * (or rather, will already have been invoked)
743 struct GNUNET_DATASTORE_QueueEntry *
744 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
747 unsigned int queue_priority,
748 unsigned int max_queue_size,
749 struct GNUNET_TIME_Relative timeout,
750 GNUNET_DATASTORE_ContinuationWithStatus cont,
753 struct GNUNET_DATASTORE_QueueEntry *qe;
754 struct ReserveMessage *rm;
755 struct StatusContext *scont;
758 cont = &drop_status_cont;
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
761 "Asked to reserve %llu bytes of data and %u entries'\n",
762 (unsigned long long) amount,
763 (unsigned int) entries);
765 scont = GNUNET_malloc (sizeof (struct StatusContext));
767 scont->cont_cls = cont_cls;
768 qe = make_queue_entry (h, sizeof(struct ReserveMessage),
769 queue_priority, max_queue_size, timeout,
770 &process_status_message, scont);
773 rm = (struct ReserveMessage*) &qe[1];
774 rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
775 rm->header.size = htons(sizeof (struct ReserveMessage));
776 rm->entries = htonl(entries);
777 rm->amount = GNUNET_htonll(amount);
784 * Signal that all of the data for which a reservation was made has
785 * been stored and that whatever excess space might have been reserved
786 * can now be released.
788 * @param h handle to the datastore
789 * @param rid reservation ID (value of "success" in original continuation
790 * from the "reserve" function).
791 * @param queue_priority ranking of this request in the priority queue
792 * @param max_queue_size at what queue size should this request be dropped
793 * (if other requests of higher priority are in the queue)
794 * @param queue_priority ranking of this request in the priority queue
795 * @param max_queue_size at what queue size should this request be dropped
796 * (if other requests of higher priority are in the queue)
797 * @param timeout how long to wait at most for a response
798 * @param cont continuation to call when done
799 * @param cont_cls closure for cont
800 * @return NULL if the entry was not queued, otherwise a handle that can be used to
801 * cancel; note that even if NULL is returned, the callback will be invoked
802 * (or rather, will already have been invoked)
804 struct GNUNET_DATASTORE_QueueEntry *
805 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
807 unsigned int queue_priority,
808 unsigned int max_queue_size,
809 struct GNUNET_TIME_Relative timeout,
810 GNUNET_DATASTORE_ContinuationWithStatus cont,
813 struct GNUNET_DATASTORE_QueueEntry *qe;
814 struct ReleaseReserveMessage *rrm;
815 struct StatusContext *scont;
818 cont = &drop_status_cont;
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 "Asked to release reserve %d\n",
824 scont = GNUNET_malloc (sizeof (struct StatusContext));
826 scont->cont_cls = cont_cls;
827 qe = make_queue_entry (h, sizeof(struct ReleaseReserveMessage),
828 queue_priority, max_queue_size, timeout,
829 &process_status_message, scont);
832 rrm = (struct ReleaseReserveMessage*) &qe[1];
833 rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
834 rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
835 rrm->rid = htonl(rid);
842 * Update a value in the datastore.
844 * @param h handle to the datastore
845 * @param uid identifier for the value
846 * @param priority how much to increase the priority of the value
847 * @param expiration new expiration value should be MAX of existing and this argument
848 * @param queue_priority ranking of this request in the priority queue
849 * @param max_queue_size at what queue size should this request be dropped
850 * (if other requests of higher priority are in the queue)
851 * @param timeout how long to wait at most for a response
852 * @param cont continuation to call when done
853 * @param cont_cls closure for cont
854 * @return NULL if the entry was not queued, otherwise a handle that can be used to
855 * cancel; note that even if NULL is returned, the callback will be invoked
856 * (or rather, will already have been invoked)
858 struct GNUNET_DATASTORE_QueueEntry *
859 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
860 unsigned long long uid,
862 struct GNUNET_TIME_Absolute expiration,
863 unsigned int queue_priority,
864 unsigned int max_queue_size,
865 struct GNUNET_TIME_Relative timeout,
866 GNUNET_DATASTORE_ContinuationWithStatus cont,
869 struct GNUNET_DATASTORE_QueueEntry *qe;
870 struct UpdateMessage *um;
871 struct StatusContext *scont;
874 cont = &drop_status_cont;
876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
877 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
879 (unsigned int) priority,
880 (unsigned long long) expiration.value);
882 scont = GNUNET_malloc (sizeof (struct StatusContext));
884 scont->cont_cls = cont_cls;
885 qe = make_queue_entry (h, sizeof(struct UpdateMessage),
886 queue_priority, max_queue_size, timeout,
887 &process_status_message, scont);
890 um = (struct UpdateMessage*) &qe[1];
891 um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
892 um->header.size = htons(sizeof (struct UpdateMessage));
893 um->priority = htonl(priority);
894 um->expiration = GNUNET_TIME_absolute_hton(expiration);
895 um->uid = GNUNET_htonll(uid);
902 * Explicitly remove some content from the database.
903 * The "cont"inuation will be called with status
904 * "GNUNET_OK" if content was removed, "GNUNET_NO"
905 * if no matching entry was found and "GNUNET_SYSERR"
906 * on all other types of errors.
908 * @param h handle to the datastore
909 * @param key key for the value
910 * @param size number of bytes in data
911 * @param data content stored
912 * @param queue_priority ranking of this request in the priority queue
913 * @param max_queue_size at what queue size should this request be dropped
914 * (if other requests of higher priority are in the queue)
915 * @param timeout how long to wait at most for a response
916 * @param cont continuation to call when done
917 * @param cont_cls closure for cont
918 * @return NULL if the entry was not queued, otherwise a handle that can be used to
919 * cancel; note that even if NULL is returned, the callback will be invoked
920 * (or rather, will already have been invoked)
922 struct GNUNET_DATASTORE_QueueEntry *
923 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
924 const GNUNET_HashCode *key,
927 unsigned int queue_priority,
928 unsigned int max_queue_size,
929 struct GNUNET_TIME_Relative timeout,
930 GNUNET_DATASTORE_ContinuationWithStatus cont,
933 struct GNUNET_DATASTORE_QueueEntry *qe;
934 struct DataMessage *dm;
936 struct StatusContext *scont;
939 cont = &drop_status_cont;
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
942 "Asked to remove %u bytes under key `%s'\n",
946 scont = GNUNET_malloc (sizeof (struct StatusContext));
948 scont->cont_cls = cont_cls;
949 msize = sizeof(struct DataMessage) + size;
950 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
951 qe = make_queue_entry (h, msize,
952 queue_priority, max_queue_size, timeout,
953 &process_status_message, scont);
956 dm = (struct DataMessage*) &qe[1];
957 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
958 dm->header.size = htons(msize);
960 dm->size = htonl(size);
962 dm->priority = htonl(0);
963 dm->anonymity = htonl(0);
964 dm->uid = GNUNET_htonll(0);
965 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
967 memcpy (&dm[1], data, size);
975 * Context for processing result messages.
980 * Iterator to call with the result.
982 GNUNET_DATASTORE_Iterator iter;
993 * Type of a function to call when we receive a message
997 * @param msg message received, NULL on timeout or fatal error
1000 process_result_message (void *cls,
1001 const struct GNUNET_MessageHeader * msg)
1003 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
1004 struct GNUNET_DATASTORE_Handle *h = qe->h;
1005 struct ResultContext *rc = qe->client_ctx;
1006 const struct DataMessage *dm;
1008 GNUNET_assert (h->queue_head == qe);
1012 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1013 _("Failed to receive response from datastore\n"));
1015 free_queue_entry (qe);
1017 rc->iter (rc->iter_cls,
1018 NULL, 0, NULL, 0, 0, 0,
1019 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1023 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1025 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
1027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1028 "Received end of result set\n");
1030 free_queue_entry (qe);
1031 rc->iter (rc->iter_cls,
1032 NULL, 0, NULL, 0, 0, 0,
1033 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1038 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
1039 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1040 (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
1043 free_queue_entry (qe);
1044 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1046 rc->iter (rc->iter_cls,
1047 NULL, 0, NULL, 0, 0, 0,
1048 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1052 dm = (const struct DataMessage*) msg;
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1055 "Received result %llu with type %u and size %u with key %s\n",
1056 (unsigned long long) GNUNET_ntohll(dm->uid),
1059 GNUNET_h2s(&dm->key));
1061 rc->iter (rc->iter_cls,
1066 ntohl(dm->priority),
1067 ntohl(dm->anonymity),
1068 GNUNET_TIME_absolute_ntoh(dm->expiration),
1069 GNUNET_ntohll(dm->uid));
1074 * Get a random value from the datastore.
1076 * @param h handle to the datastore
1077 * @param queue_priority ranking of this request in the priority queue
1078 * @param max_queue_size at what queue size should this request be dropped
1079 * (if other requests of higher priority are in the queue)
1080 * @param timeout how long to wait at most for a response
1081 * @param iter function to call on a random value; it
1082 * will be called once with a value (if available)
1083 * and always once with a value of NULL.
1084 * @param iter_cls closure for iter
1085 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1086 * cancel; note that even if NULL is returned, the callback will be invoked
1087 * (or rather, will already have been invoked)
1089 struct GNUNET_DATASTORE_QueueEntry *
1090 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
1091 unsigned int queue_priority,
1092 unsigned int max_queue_size,
1093 struct GNUNET_TIME_Relative timeout,
1094 GNUNET_DATASTORE_Iterator iter,
1097 struct GNUNET_DATASTORE_QueueEntry *qe;
1098 struct GNUNET_MessageHeader *m;
1099 struct ResultContext *rcont;
1102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1103 "Asked to get random entry in %llu ms\n",
1104 (unsigned long long) timeout.value);
1106 rcont = GNUNET_malloc (sizeof (struct ResultContext));
1108 rcont->iter_cls = iter_cls;
1109 qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader),
1110 queue_priority, max_queue_size, timeout,
1111 &process_result_message, rcont);
1114 m = (struct GNUNET_MessageHeader*) &qe[1];
1115 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
1116 m->size = htons(sizeof (struct GNUNET_MessageHeader));
1124 * Iterate over the results for a particular key
1125 * in the datastore. The iterator will only be called
1126 * once initially; if the first call did contain a
1127 * result, further results can be obtained by calling
1128 * "GNUNET_DATASTORE_get_next" with the given argument.
1130 * @param h handle to the datastore
1131 * @param key maybe NULL (to match all entries)
1132 * @param type desired type, 0 for any
1133 * @param queue_priority ranking of this request in the priority queue
1134 * @param max_queue_size at what queue size should this request be dropped
1135 * (if other requests of higher priority are in the queue)
1136 * @param timeout how long to wait at most for a response
1137 * @param iter function to call on each matching value;
1138 * will be called once with a NULL value at the end
1139 * @param iter_cls closure for iter
1140 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1141 * cancel; note that even if NULL is returned, the callback will be invoked
1142 * (or rather, will already have been invoked)
1144 struct GNUNET_DATASTORE_QueueEntry *
1145 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
1146 const GNUNET_HashCode * key,
1147 enum GNUNET_BLOCK_Type type,
1148 unsigned int queue_priority,
1149 unsigned int max_queue_size,
1150 struct GNUNET_TIME_Relative timeout,
1151 GNUNET_DATASTORE_Iterator iter,
1154 struct GNUNET_DATASTORE_QueueEntry *qe;
1155 struct GetMessage *gm;
1156 struct ResultContext *rcont;
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1160 "Asked to look for data of type %u under key `%s'\n",
1161 (unsigned int) type,
1164 rcont = GNUNET_malloc (sizeof (struct ResultContext));
1166 rcont->iter_cls = iter_cls;
1167 qe = make_queue_entry (h, sizeof(struct GetMessage),
1168 queue_priority, max_queue_size, timeout,
1169 &process_result_message, rcont);
1172 gm = (struct GetMessage*) &qe[1];
1173 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1174 gm->type = htonl(type);
1177 gm->header.size = htons(sizeof (struct GetMessage));
1182 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
1190 * Function called to trigger obtaining the next result
1191 * from the datastore.
1193 * @param h handle to the datastore
1194 * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
1195 * iteration (with a final call to "iter" with key/data == NULL).
1198 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
1201 struct GNUNET_DATASTORE_QueueEntry *qe = h->queue_head;
1202 struct ResultContext *rc = qe->client_ctx;
1204 GNUNET_assert (NULL != qe);
1205 GNUNET_assert (&process_result_message == qe->response_proc);
1206 if (GNUNET_YES == more)
1208 GNUNET_CLIENT_receive (h->client,
1211 GNUNET_TIME_absolute_get_remaining (qe->timeout));
1214 free_queue_entry (qe);
1215 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1217 rc->iter (rc->iter_cls,
1218 NULL, 0, NULL, 0, 0, 0,
1219 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1225 * Cancel a datastore operation. The final callback from the
1226 * operation must not have been done yet.
1228 * @param qe operation to cancel
1231 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1233 struct GNUNET_DATASTORE_Handle *h;
1237 reconnect = qe->was_transmitted;
1238 free_queue_entry (qe);
1242 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1248 /* end of datastore_api.c */