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"
35 * Context for processing status messages.
40 * Continuation to call with the status.
42 GNUNET_DATASTORE_ContinuationWithStatus cont;
53 * Context for processing result messages.
58 * Iterator to call with the result.
60 GNUNET_DATASTORE_Iterator iter;
71 * Context for a queue operation.
76 struct StatusContext sc;
78 struct ResultContext rc;
85 * Entry in our priority queue.
87 struct GNUNET_DATASTORE_QueueEntry
91 * This is a linked list.
93 struct GNUNET_DATASTORE_QueueEntry *next;
96 * This is a linked list.
98 struct GNUNET_DATASTORE_QueueEntry *prev;
101 * Handle to the master context.
103 struct GNUNET_DATASTORE_Handle *h;
106 * Response processor (NULL if we are not waiting for a response).
107 * This struct should be used for the closure, function-specific
108 * arguments can be passed via 'qc'.
110 GNUNET_CLIENT_MessageHandler response_proc;
113 * Function to call after transmission of the request.
115 GNUNET_DATASTORE_ContinuationWithStatus cont;
118 * Closure for 'cont'.
123 * Context for the operation.
125 union QueueContext qc;
128 * Task for timeout signalling.
130 GNUNET_SCHEDULER_TaskIdentifier task;
133 * Timeout for the current operation.
135 struct GNUNET_TIME_Absolute timeout;
138 * Priority in the queue.
140 unsigned int priority;
143 * Maximum allowed length of queue (otherwise
144 * this request should be discarded).
146 unsigned int max_queue;
149 * Number of bytes in the request message following
150 * this struct. 32-bit value for nicer memory
151 * access (and overall struct alignment).
153 uint32_t message_size;
156 * Has this message been transmitted to the service?
157 * Only ever GNUNET_YES for the head of the queue.
158 * Note that the overall struct should end at a
159 * multiple of 64 bits.
161 int32_t was_transmitted;
166 * Handle to the datastore service.
168 struct GNUNET_DATASTORE_Handle
174 const struct GNUNET_CONFIGURATION_Handle *cfg;
179 struct GNUNET_SCHEDULER_Handle *sched;
182 * Current connection to the datastore service.
184 struct GNUNET_CLIENT_Connection *client;
187 * Current transmit handle.
189 struct GNUNET_CLIENT_TransmitHandle *th;
192 * Current head of priority queue.
194 struct GNUNET_DATASTORE_QueueEntry *queue_head;
197 * Current tail of priority queue.
199 struct GNUNET_DATASTORE_QueueEntry *queue_tail;
202 * Task for trying to reconnect.
204 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
207 * How quickly should we retry? Used for exponential back-off on
210 struct GNUNET_TIME_Relative retry_time;
213 * Number of entries in the queue.
215 unsigned int queue_size;
222 * Connect to the datastore service.
224 * @param cfg configuration to use
225 * @param sched scheduler to use
226 * @return handle to use to access the service
228 struct GNUNET_DATASTORE_Handle *
229 GNUNET_DATASTORE_connect (const struct
230 GNUNET_CONFIGURATION_Handle
233 GNUNET_SCHEDULER_Handle
236 struct GNUNET_CLIENT_Connection *c;
237 struct GNUNET_DATASTORE_Handle *h;
239 c = GNUNET_CLIENT_connect (sched, "datastore", cfg);
241 return NULL; /* oops */
242 h = GNUNET_malloc (sizeof(struct GNUNET_DATASTORE_Handle) +
243 GNUNET_SERVER_MAX_MESSAGE_SIZE);
252 * Transmit DROP message to datastore service.
254 * @param cls the 'struct GNUNET_DATASTORE_Handle'
255 * @param size number of bytes that can be copied to buf
256 * @param buf where to copy the drop message
257 * @return number of bytes written to buf
260 transmit_drop (void *cls,
264 struct GNUNET_DATASTORE_Handle *h = cls;
265 struct GNUNET_MessageHeader *hdr;
269 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
270 _("Failed to transmit request to drop database.\n"));
271 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
274 GNUNET_assert (size >= sizeof(struct GNUNET_MessageHeader));
276 hdr->size = htons(sizeof(struct GNUNET_MessageHeader));
277 hdr->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
278 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
279 return sizeof(struct GNUNET_MessageHeader);
284 * Disconnect from the datastore service (and free
285 * associated resources).
287 * @param h handle to the datastore
288 * @param drop set to GNUNET_YES to delete all data in datastore (!)
290 void GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
293 struct GNUNET_DATASTORE_QueueEntry *qe;
295 if (h->client != NULL)
297 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
300 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
302 GNUNET_SCHEDULER_cancel (h->sched,
304 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
306 while (NULL != (qe = h->queue_head))
308 GNUNET_assert (NULL != qe->response_proc);
309 qe->response_proc (qe, NULL);
311 if (GNUNET_YES == drop)
313 h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
314 if (h->client != NULL)
317 GNUNET_CLIENT_notify_transmit_ready (h->client,
318 sizeof(struct GNUNET_MessageHeader),
319 GNUNET_TIME_UNIT_MINUTES,
324 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
333 * A request has timed out (before being transmitted to the service).
335 * @param cls the 'struct GNUNET_DATASTORE_QueueEntry'
336 * @param tc scheduler context
339 timeout_queue_entry (void *cls,
340 const struct GNUNET_SCHEDULER_TaskContext *tc)
342 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
344 qe->task = GNUNET_SCHEDULER_NO_TASK;
345 GNUNET_assert (qe->was_transmitted == GNUNET_NO);
346 qe->response_proc (qe, NULL);
351 * Create a new entry for our priority queue (and possibly discard other entires if
352 * the queue is getting too long).
354 * @param h handle to the datastore
355 * @param msize size of the message to queue
356 * @param queue_priority priority of the entry
357 * @param max_queue_size at what queue size should this request be dropped
358 * (if other requests of higher priority are in the queue)
359 * @param timeout timeout for the operation
360 * @param response_proc function to call with replies (can be NULL)
361 * @param qc client context (NOT a closure for response_proc)
362 * @return NULL if the queue is full (and this entry was dropped)
364 static struct GNUNET_DATASTORE_QueueEntry *
365 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
367 unsigned int queue_priority,
368 unsigned int max_queue_size,
369 struct GNUNET_TIME_Relative timeout,
370 GNUNET_CLIENT_MessageHandler response_proc,
371 const union QueueContext *qc)
373 struct GNUNET_DATASTORE_QueueEntry *ret;
374 struct GNUNET_DATASTORE_QueueEntry *pos;
379 while ( (pos != NULL) &&
380 (c < max_queue_size) &&
381 (pos->priority >= queue_priority) )
386 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
388 ret->response_proc = response_proc;
390 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
391 ret->priority = queue_priority;
392 ret->max_queue = max_queue_size;
393 ret->message_size = msize;
394 ret->was_transmitted = GNUNET_NO;
397 /* append at the tail */
403 /* do not insert at HEAD if HEAD query was already
404 transmitted and we are still receiving replies! */
405 if ( (pos == NULL) &&
406 (h->queue_head->was_transmitted) )
410 GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
415 if (c > max_queue_size)
417 response_proc (ret, NULL);
420 ret->task = GNUNET_SCHEDULER_add_delayed (h->sched,
422 &timeout_queue_entry,
427 if (pos->max_queue < h->queue_size)
429 GNUNET_assert (pos->response_proc != NULL);
430 pos->response_proc (pos, NULL);
440 * Process entries in the queue (or do nothing if we are already
443 * @param h handle to the datastore
446 process_queue (struct GNUNET_DATASTORE_Handle *h);
450 * Try reconnecting to the datastore service.
452 * @param cls the 'struct GNUNET_DATASTORE_Handle'
453 * @param tc scheduler context
456 try_reconnect (void *cls,
457 const struct GNUNET_SCHEDULER_TaskContext *tc)
459 struct GNUNET_DATASTORE_Handle *h = cls;
461 if (h->retry_time.value < GNUNET_CONSTANTS_SERVICE_RETRY.value)
462 h->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
464 h->retry_time = GNUNET_TIME_relative_multiply (h->retry_time, 2);
465 if (h->retry_time.value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.value)
466 h->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
467 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
468 h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
469 if (h->client == NULL)
476 * Disconnect from the service and then try reconnecting to the datastore service
479 * @param h handle to datastore to disconnect and reconnect
482 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
484 if (h->client == NULL)
487 GNUNET_STATISTICS_update (stats,
488 gettext_noop ("# reconnected to datastore"),
492 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
494 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->sched,
502 * Transmit request from queue to datastore service.
504 * @param cls the 'struct GNUNET_DATASTORE_Handle'
505 * @param size number of bytes that can be copied to buf
506 * @param buf where to copy the drop message
507 * @return number of bytes written to buf
510 transmit_request (void *cls,
514 struct GNUNET_DATASTORE_Handle *h = cls;
515 struct GNUNET_DATASTORE_QueueEntry *qe;
519 if (NULL == (qe = h->queue_head))
520 return 0; /* no entry in queue */
523 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
524 _("Failed to transmit request to database.\n"));
528 if (size < (msize = qe->message_size))
533 memcpy (buf, &qe[1], msize);
534 qe->was_transmitted = GNUNET_YES;
535 GNUNET_SCHEDULER_cancel (h->sched,
537 qe->task = GNUNET_SCHEDULER_NO_TASK;
538 GNUNET_CLIENT_receive (h->client,
541 GNUNET_TIME_absolute_get_remaining (qe->timeout));
547 * Process entries in the queue (or do nothing if we are already
550 * @param h handle to the datastore
553 process_queue (struct GNUNET_DATASTORE_Handle *h)
555 struct GNUNET_DATASTORE_QueueEntry *qe;
557 if (NULL == (qe = h->queue_head))
558 return; /* no entry in queue */
559 if (qe->was_transmitted == GNUNET_YES)
560 return; /* waiting for replies */
562 return; /* request pending */
563 if (h->client == NULL)
564 return; /* waiting for reconnect */
565 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
567 GNUNET_TIME_absolute_get_remaining (qe->timeout),
575 * Dummy continuation used to do nothing (but be non-zero).
578 * @param result result
579 * @param emsg error message
582 drop_status_cont (void *cls, int result, const char *emsg)
589 free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
591 struct GNUNET_DATASTORE_Handle *h = qe->h;
593 GNUNET_CONTAINER_DLL_remove (h->queue_head,
596 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
598 GNUNET_SCHEDULER_cancel (h->sched,
600 qe->task = GNUNET_SCHEDULER_NO_TASK;
607 * Type of a function to call when we receive a message
611 * @param msg message received, NULL on timeout or fatal error
614 process_status_message (void *cls,
616 GNUNET_MessageHeader * msg)
618 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
619 struct GNUNET_DATASTORE_Handle *h = qe->h;
620 struct StatusContext rc = qe->qc.sc;
621 const struct StatusMessage *sm;
626 was_transmitted = qe->was_transmitted;
629 free_queue_entry (qe);
630 if (NULL == h->client)
631 return; /* forced disconnect */
632 if (was_transmitted == GNUNET_YES)
634 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
635 _("Failed to receive response from database.\n"));
640 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
641 GNUNET_assert (h->queue_head == qe);
642 free_queue_entry (qe);
643 if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
644 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) )
647 h->retry_time = GNUNET_TIME_UNIT_ZERO;
649 rc.cont (rc.cont_cls,
651 _("Error reading response from datastore service"));
654 sm = (const struct StatusMessage*) msg;
655 status = ntohl(sm->status);
657 if (ntohs(msg->size) > sizeof(struct StatusMessage))
659 emsg = (const char*) &sm[1];
660 if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
663 emsg = _("Invalid error message received from datastore service");
666 if ( (status == GNUNET_SYSERR) &&
670 emsg = _("Invalid error message received from datastore service");
673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
674 "Received status %d/%s\n",
678 rc.cont (rc.cont_cls,
686 * Store an item in the datastore. If the item is already present,
687 * the priorities are summed up and the higher expiration time and
688 * lower anonymity level is used.
690 * @param h handle to the datastore
691 * @param rid reservation ID to use (from "reserve"); use 0 if no
692 * prior reservation was made
693 * @param key key for the value
694 * @param size number of bytes in data
695 * @param data content stored
696 * @param type type of the content
697 * @param priority priority of the content
698 * @param anonymity anonymity-level for the content
699 * @param expiration expiration time for the content
700 * @param queue_priority ranking of this request in the priority queue
701 * @param max_queue_size at what queue size should this request be dropped
702 * (if other requests of higher priority are in the queue)
703 * @param timeout timeout for the operation
704 * @param cont continuation to call when done
705 * @param cont_cls closure for cont
706 * @return NULL if the entry was not queued, otherwise a handle that can be used to
707 * cancel; note that even if NULL is returned, the callback will be invoked
708 * (or rather, will already have been invoked)
710 struct GNUNET_DATASTORE_QueueEntry *
711 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
713 const GNUNET_HashCode * key,
716 enum GNUNET_BLOCK_Type type,
719 struct GNUNET_TIME_Absolute expiration,
720 unsigned int queue_priority,
721 unsigned int max_queue_size,
722 struct GNUNET_TIME_Relative timeout,
723 GNUNET_DATASTORE_ContinuationWithStatus cont,
726 struct GNUNET_DATASTORE_QueueEntry *qe;
727 struct DataMessage *dm;
729 union QueueContext qc;
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733 "Asked to put %u bytes of data under key `%s'\n",
737 msize = sizeof(struct DataMessage) + size;
738 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
740 qc.sc.cont_cls = cont_cls;
741 qe = make_queue_entry (h, msize,
742 queue_priority, max_queue_size, timeout,
743 &process_status_message, &qc);
746 dm = (struct DataMessage* ) &qe[1];
747 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
748 dm->header.size = htons(msize);
749 dm->rid = htonl(rid);
750 dm->size = htonl(size);
751 dm->type = htonl(type);
752 dm->priority = htonl(priority);
753 dm->anonymity = htonl(anonymity);
754 dm->uid = GNUNET_htonll(0);
755 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
757 memcpy (&dm[1], data, size);
764 * Reserve space in the datastore. This function should be used
765 * to avoid "out of space" failures during a longer sequence of "put"
766 * operations (for example, when a file is being inserted).
768 * @param h handle to the datastore
769 * @param amount how much space (in bytes) should be reserved (for content only)
770 * @param entries how many entries will be created (to calculate per-entry overhead)
771 * @param queue_priority ranking of this request in the priority queue
772 * @param max_queue_size at what queue size should this request be dropped
773 * (if other requests of higher priority are in the queue)
774 * @param timeout how long to wait at most for a response (or before dying in queue)
775 * @param cont continuation to call when done; "success" will be set to
776 * a positive reservation value if space could be reserved.
777 * @param cont_cls closure for cont
778 * @return NULL if the entry was not queued, otherwise a handle that can be used to
779 * cancel; note that even if NULL is returned, the callback will be invoked
780 * (or rather, will already have been invoked)
782 struct GNUNET_DATASTORE_QueueEntry *
783 GNUNET_DATASTORE_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 GNUNET_DATASTORE_QueueEntry *qe;
793 struct ReserveMessage *rm;
794 union QueueContext qc;
797 cont = &drop_status_cont;
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 "Asked to reserve %llu bytes of data and %u entries'\n",
801 (unsigned long long) amount,
802 (unsigned int) entries);
805 qc.sc.cont_cls = cont_cls;
806 qe = make_queue_entry (h, sizeof(struct ReserveMessage),
807 queue_priority, max_queue_size, timeout,
808 &process_status_message, &qc);
811 rm = (struct ReserveMessage*) &qe[1];
812 rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
813 rm->header.size = htons(sizeof (struct ReserveMessage));
814 rm->entries = htonl(entries);
815 rm->amount = GNUNET_htonll(amount);
822 * Signal that all of the data for which a reservation was made has
823 * been stored and that whatever excess space might have been reserved
824 * can now be released.
826 * @param h handle to the datastore
827 * @param rid reservation ID (value of "success" in original continuation
828 * from the "reserve" function).
829 * @param queue_priority ranking of this request in the priority queue
830 * @param max_queue_size at what queue size should this request be dropped
831 * (if other requests of higher priority are in the queue)
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_release_reserve (struct GNUNET_DATASTORE_Handle *h,
845 unsigned int queue_priority,
846 unsigned int max_queue_size,
847 struct GNUNET_TIME_Relative timeout,
848 GNUNET_DATASTORE_ContinuationWithStatus cont,
851 struct GNUNET_DATASTORE_QueueEntry *qe;
852 struct ReleaseReserveMessage *rrm;
853 union QueueContext qc;
856 cont = &drop_status_cont;
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Asked to release reserve %d\n",
863 qc.sc.cont_cls = cont_cls;
864 qe = make_queue_entry (h, sizeof(struct ReleaseReserveMessage),
865 queue_priority, max_queue_size, timeout,
866 &process_status_message, &qc);
869 rrm = (struct ReleaseReserveMessage*) &qe[1];
870 rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
871 rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
872 rrm->rid = htonl(rid);
879 * Update a value in the datastore.
881 * @param h handle to the datastore
882 * @param uid identifier for the value
883 * @param priority how much to increase the priority of the value
884 * @param expiration new expiration value should be MAX of existing and this argument
885 * @param queue_priority ranking of this request in the priority queue
886 * @param max_queue_size at what queue size should this request be dropped
887 * (if other requests of higher priority are in the queue)
888 * @param timeout how long to wait at most for a response
889 * @param cont continuation to call when done
890 * @param cont_cls closure for cont
891 * @return NULL if the entry was not queued, otherwise a handle that can be used to
892 * cancel; note that even if NULL is returned, the callback will be invoked
893 * (or rather, will already have been invoked)
895 struct GNUNET_DATASTORE_QueueEntry *
896 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
897 unsigned long long uid,
899 struct GNUNET_TIME_Absolute expiration,
900 unsigned int queue_priority,
901 unsigned int max_queue_size,
902 struct GNUNET_TIME_Relative timeout,
903 GNUNET_DATASTORE_ContinuationWithStatus cont,
906 struct GNUNET_DATASTORE_QueueEntry *qe;
907 struct UpdateMessage *um;
908 union QueueContext qc;
911 cont = &drop_status_cont;
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
914 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
916 (unsigned int) priority,
917 (unsigned long long) expiration.value);
920 qc.sc.cont_cls = cont_cls;
921 qe = make_queue_entry (h, sizeof(struct UpdateMessage),
922 queue_priority, max_queue_size, timeout,
923 &process_status_message, &qc);
926 um = (struct UpdateMessage*) &qe[1];
927 um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
928 um->header.size = htons(sizeof (struct UpdateMessage));
929 um->priority = htonl(priority);
930 um->expiration = GNUNET_TIME_absolute_hton(expiration);
931 um->uid = GNUNET_htonll(uid);
938 * Explicitly remove some content from the database.
939 * The "cont"inuation will be called with status
940 * "GNUNET_OK" if content was removed, "GNUNET_NO"
941 * if no matching entry was found and "GNUNET_SYSERR"
942 * on all other types of errors.
944 * @param h handle to the datastore
945 * @param key key for the value
946 * @param size number of bytes in data
947 * @param data content stored
948 * @param queue_priority ranking of this request in the priority queue
949 * @param max_queue_size at what queue size should this request be dropped
950 * (if other requests of higher priority are in the queue)
951 * @param timeout how long to wait at most for a response
952 * @param cont continuation to call when done
953 * @param cont_cls closure for cont
954 * @return NULL if the entry was not queued, otherwise a handle that can be used to
955 * cancel; note that even if NULL is returned, the callback will be invoked
956 * (or rather, will already have been invoked)
958 struct GNUNET_DATASTORE_QueueEntry *
959 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
960 const GNUNET_HashCode *key,
963 unsigned int queue_priority,
964 unsigned int max_queue_size,
965 struct GNUNET_TIME_Relative timeout,
966 GNUNET_DATASTORE_ContinuationWithStatus cont,
969 struct GNUNET_DATASTORE_QueueEntry *qe;
970 struct DataMessage *dm;
972 union QueueContext qc;
975 cont = &drop_status_cont;
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
978 "Asked to remove %u bytes under key `%s'\n",
983 qc.sc.cont_cls = cont_cls;
984 msize = sizeof(struct DataMessage) + size;
985 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
986 qe = make_queue_entry (h, msize,
987 queue_priority, max_queue_size, timeout,
988 &process_status_message, &qc);
991 dm = (struct DataMessage*) &qe[1];
992 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
993 dm->header.size = htons(msize);
995 dm->size = htonl(size);
997 dm->priority = htonl(0);
998 dm->anonymity = htonl(0);
999 dm->uid = GNUNET_htonll(0);
1000 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
1002 memcpy (&dm[1], data, size);
1009 * Type of a function to call when we receive a message
1012 * @param cls closure
1013 * @param msg message received, NULL on timeout or fatal error
1016 process_result_message (void *cls,
1017 const struct GNUNET_MessageHeader * msg)
1019 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
1020 struct GNUNET_DATASTORE_Handle *h = qe->h;
1021 struct ResultContext rc = qe->qc.rc;
1022 const struct DataMessage *dm;
1023 int was_transmitted;
1027 was_transmitted = qe->was_transmitted;
1028 free_queue_entry (qe);
1029 if (was_transmitted == GNUNET_YES)
1031 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1032 _("Failed to receive response from database.\n"));
1035 if (rc.iter != NULL)
1036 rc.iter (rc.iter_cls,
1037 NULL, 0, NULL, 0, 0, 0,
1038 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1041 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1042 GNUNET_assert (h->queue_head == qe);
1043 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1045 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1048 "Received end of result set\n");
1050 free_queue_entry (qe);
1051 if (rc.iter != NULL)
1052 rc.iter (rc.iter_cls,
1053 NULL, 0, NULL, 0, 0, 0,
1054 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1058 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
1059 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1060 (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
1063 free_queue_entry (qe);
1064 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1066 if (rc.iter != NULL)
1067 rc.iter (rc.iter_cls,
1068 NULL, 0, NULL, 0, 0, 0,
1069 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1072 if (rc.iter == NULL)
1074 /* abort iteration */
1078 dm = (const struct DataMessage*) msg;
1080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1081 "Received result %llu with type %u and size %u with key %s\n",
1082 (unsigned long long) GNUNET_ntohll(dm->uid),
1085 GNUNET_h2s(&dm->key));
1087 rc.iter (rc.iter_cls,
1092 ntohl(dm->priority),
1093 ntohl(dm->anonymity),
1094 GNUNET_TIME_absolute_ntoh(dm->expiration),
1095 GNUNET_ntohll(dm->uid));
1100 * Get a random value from the datastore.
1102 * @param h handle to the datastore
1103 * @param queue_priority ranking of this request in the priority queue
1104 * @param max_queue_size at what queue size should this request be dropped
1105 * (if other requests of higher priority are in the queue)
1106 * @param timeout how long to wait at most for a response
1107 * @param iter function to call on a random value; it
1108 * will be called once with a value (if available)
1109 * and always once with a value of NULL.
1110 * @param iter_cls closure for iter
1111 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1112 * cancel; note that even if NULL is returned, the callback will be invoked
1113 * (or rather, will already have been invoked)
1115 struct GNUNET_DATASTORE_QueueEntry *
1116 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
1117 unsigned int queue_priority,
1118 unsigned int max_queue_size,
1119 struct GNUNET_TIME_Relative timeout,
1120 GNUNET_DATASTORE_Iterator iter,
1123 struct GNUNET_DATASTORE_QueueEntry *qe;
1124 struct GNUNET_MessageHeader *m;
1125 union QueueContext qc;
1128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1129 "Asked to get random entry in %llu ms\n",
1130 (unsigned long long) timeout.value);
1133 qc.rc.iter_cls = iter_cls;
1134 qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader),
1135 queue_priority, max_queue_size, timeout,
1136 &process_result_message, &qc);
1139 m = (struct GNUNET_MessageHeader*) &qe[1];
1140 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
1141 m->size = htons(sizeof (struct GNUNET_MessageHeader));
1149 * Iterate over the results for a particular key
1150 * in the datastore. The iterator will only be called
1151 * once initially; if the first call did contain a
1152 * result, further results can be obtained by calling
1153 * "GNUNET_DATASTORE_get_next" with the given argument.
1155 * @param h handle to the datastore
1156 * @param key maybe NULL (to match all entries)
1157 * @param type desired type, 0 for any
1158 * @param queue_priority ranking of this request in the priority queue
1159 * @param max_queue_size at what queue size should this request be dropped
1160 * (if other requests of higher priority are in the queue)
1161 * @param timeout how long to wait at most for a response
1162 * @param iter function to call on each matching value;
1163 * will be called once with a NULL value at the end
1164 * @param iter_cls closure for iter
1165 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1166 * cancel; note that even if NULL is returned, the callback will be invoked
1167 * (or rather, will already have been invoked)
1169 struct GNUNET_DATASTORE_QueueEntry *
1170 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
1171 const GNUNET_HashCode * key,
1172 enum GNUNET_BLOCK_Type type,
1173 unsigned int queue_priority,
1174 unsigned int max_queue_size,
1175 struct GNUNET_TIME_Relative timeout,
1176 GNUNET_DATASTORE_Iterator iter,
1179 struct GNUNET_DATASTORE_QueueEntry *qe;
1180 struct GetMessage *gm;
1181 union QueueContext qc;
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1185 "Asked to look for data of type %u under key `%s'\n",
1186 (unsigned int) type,
1190 qc.rc.iter_cls = iter_cls;
1191 qe = make_queue_entry (h, sizeof(struct GetMessage),
1192 queue_priority, max_queue_size, timeout,
1193 &process_result_message, &qc);
1196 gm = (struct GetMessage*) &qe[1];
1197 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1198 gm->type = htonl(type);
1201 gm->header.size = htons(sizeof (struct GetMessage));
1206 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
1214 * Function called to trigger obtaining the next result
1215 * from the datastore.
1217 * @param h handle to the datastore
1218 * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
1219 * iteration (with a final call to "iter" with key/data == NULL).
1222 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
1225 struct GNUNET_DATASTORE_QueueEntry *qe = h->queue_head;
1226 struct ResultContext rc = qe->qc.rc;
1228 GNUNET_assert (NULL != qe);
1229 GNUNET_assert (&process_result_message == qe->response_proc);
1230 if (GNUNET_YES == more)
1232 GNUNET_CLIENT_receive (h->client,
1235 GNUNET_TIME_absolute_get_remaining (qe->timeout));
1238 free_queue_entry (qe);
1239 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1241 rc.iter (rc.iter_cls,
1242 NULL, 0, NULL, 0, 0, 0,
1243 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1248 * Cancel a datastore operation. The final callback from the
1249 * operation must not have been done yet.
1251 * @param qe operation to cancel
1254 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1256 struct GNUNET_DATASTORE_Handle *h;
1260 reconnect = GNUNET_NO;
1261 if (GNUNET_YES == qe->was_transmitted)
1263 if (qe->response_proc == &process_result_message)
1265 qe->qc.rc.iter = NULL;
1268 reconnect = GNUNET_YES;
1270 free_queue_entry (qe);
1274 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1280 /* end of datastore_api.c */