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)
486 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
488 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->sched,
496 * Transmit request from queue to datastore service.
498 * @param cls the 'struct GNUNET_DATASTORE_Handle'
499 * @param size number of bytes that can be copied to buf
500 * @param buf where to copy the drop message
501 * @return number of bytes written to buf
504 transmit_request (void *cls,
508 struct GNUNET_DATASTORE_Handle *h = cls;
509 struct GNUNET_DATASTORE_QueueEntry *qe;
513 if (NULL == (qe = h->queue_head))
514 return 0; /* no entry in queue */
517 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
518 _("Failed to transmit request to database.\n"));
522 if (size < (msize = qe->message_size))
527 memcpy (buf, &qe[1], msize);
528 qe->was_transmitted = GNUNET_YES;
529 GNUNET_SCHEDULER_cancel (h->sched,
531 qe->task = GNUNET_SCHEDULER_NO_TASK;
532 GNUNET_CLIENT_receive (h->client,
535 GNUNET_TIME_absolute_get_remaining (qe->timeout));
541 * Process entries in the queue (or do nothing if we are already
544 * @param h handle to the datastore
547 process_queue (struct GNUNET_DATASTORE_Handle *h)
549 struct GNUNET_DATASTORE_QueueEntry *qe;
551 if (NULL == (qe = h->queue_head))
552 return; /* no entry in queue */
553 if (qe->was_transmitted == GNUNET_YES)
554 return; /* waiting for replies */
556 return; /* request pending */
557 if (h->client == NULL)
558 return; /* waiting for reconnect */
559 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
561 GNUNET_TIME_absolute_get_remaining (qe->timeout),
569 * Dummy continuation used to do nothing (but be non-zero).
572 * @param result result
573 * @param emsg error message
576 drop_status_cont (void *cls, int result, const char *emsg)
583 free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
585 struct GNUNET_DATASTORE_Handle *h = qe->h;
587 GNUNET_CONTAINER_DLL_remove (h->queue_head,
590 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
592 GNUNET_SCHEDULER_cancel (h->sched,
594 qe->task = GNUNET_SCHEDULER_NO_TASK;
601 * Type of a function to call when we receive a message
605 * @param msg message received, NULL on timeout or fatal error
608 process_status_message (void *cls,
610 GNUNET_MessageHeader * msg)
612 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
613 struct GNUNET_DATASTORE_Handle *h = qe->h;
614 struct StatusContext rc = qe->qc.sc;
615 const struct StatusMessage *sm;
619 free_queue_entry (qe);
622 if (NULL == h->client)
623 return; /* forced disconnect */
624 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
625 _("Failed to receive response from database.\n"));
630 if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
631 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) )
634 h->retry_time = GNUNET_TIME_UNIT_ZERO;
636 rc.cont (rc.cont_cls,
638 _("Error reading response from datastore service"));
641 sm = (const struct StatusMessage*) msg;
642 status = ntohl(sm->status);
644 if (ntohs(msg->size) > sizeof(struct StatusMessage))
646 emsg = (const char*) &sm[1];
647 if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
650 emsg = _("Invalid error message received from datastore service");
653 if ( (status == GNUNET_SYSERR) &&
657 emsg = _("Invalid error message received from datastore service");
660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
661 "Received status %d/%s\n",
665 rc.cont (rc.cont_cls,
673 * Store an item in the datastore. If the item is already present,
674 * the priorities are summed up and the higher expiration time and
675 * lower anonymity level is used.
677 * @param h handle to the datastore
678 * @param rid reservation ID to use (from "reserve"); use 0 if no
679 * prior reservation was made
680 * @param key key for the value
681 * @param size number of bytes in data
682 * @param data content stored
683 * @param type type of the content
684 * @param priority priority of the content
685 * @param anonymity anonymity-level for the content
686 * @param expiration expiration time for the content
687 * @param queue_priority ranking of this request in the priority queue
688 * @param max_queue_size at what queue size should this request be dropped
689 * (if other requests of higher priority are in the queue)
690 * @param timeout timeout for the operation
691 * @param cont continuation to call when done
692 * @param cont_cls closure for cont
693 * @return NULL if the entry was not queued, otherwise a handle that can be used to
694 * cancel; note that even if NULL is returned, the callback will be invoked
695 * (or rather, will already have been invoked)
697 struct GNUNET_DATASTORE_QueueEntry *
698 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
700 const GNUNET_HashCode * key,
703 enum GNUNET_BLOCK_Type type,
706 struct GNUNET_TIME_Absolute expiration,
707 unsigned int queue_priority,
708 unsigned int max_queue_size,
709 struct GNUNET_TIME_Relative timeout,
710 GNUNET_DATASTORE_ContinuationWithStatus cont,
713 struct GNUNET_DATASTORE_QueueEntry *qe;
714 struct DataMessage *dm;
716 union QueueContext qc;
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
720 "Asked to put %u bytes of data under key `%s'\n",
724 msize = sizeof(struct DataMessage) + size;
725 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
727 qc.sc.cont_cls = cont_cls;
728 qe = make_queue_entry (h, msize,
729 queue_priority, max_queue_size, timeout,
730 &process_status_message, &qc);
733 dm = (struct DataMessage* ) &qe[1];
734 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
735 dm->header.size = htons(msize);
736 dm->rid = htonl(rid);
737 dm->size = htonl(size);
738 dm->type = htonl(type);
739 dm->priority = htonl(priority);
740 dm->anonymity = htonl(anonymity);
741 dm->uid = GNUNET_htonll(0);
742 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
744 memcpy (&dm[1], data, size);
751 * Reserve space in the datastore. This function should be used
752 * to avoid "out of space" failures during a longer sequence of "put"
753 * operations (for example, when a file is being inserted).
755 * @param h handle to the datastore
756 * @param amount how much space (in bytes) should be reserved (for content only)
757 * @param entries how many entries will be created (to calculate per-entry overhead)
758 * @param queue_priority ranking of this request in the priority queue
759 * @param max_queue_size at what queue size should this request be dropped
760 * (if other requests of higher priority are in the queue)
761 * @param timeout how long to wait at most for a response (or before dying in queue)
762 * @param cont continuation to call when done; "success" will be set to
763 * a positive reservation value if space could be reserved.
764 * @param cont_cls closure for cont
765 * @return NULL if the entry was not queued, otherwise a handle that can be used to
766 * cancel; note that even if NULL is returned, the callback will be invoked
767 * (or rather, will already have been invoked)
769 struct GNUNET_DATASTORE_QueueEntry *
770 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
773 unsigned int queue_priority,
774 unsigned int max_queue_size,
775 struct GNUNET_TIME_Relative timeout,
776 GNUNET_DATASTORE_ContinuationWithStatus cont,
779 struct GNUNET_DATASTORE_QueueEntry *qe;
780 struct ReserveMessage *rm;
781 union QueueContext qc;
784 cont = &drop_status_cont;
786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
787 "Asked to reserve %llu bytes of data and %u entries'\n",
788 (unsigned long long) amount,
789 (unsigned int) entries);
792 qc.sc.cont_cls = cont_cls;
793 qe = make_queue_entry (h, sizeof(struct ReserveMessage),
794 queue_priority, max_queue_size, timeout,
795 &process_status_message, &qc);
798 rm = (struct ReserveMessage*) &qe[1];
799 rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
800 rm->header.size = htons(sizeof (struct ReserveMessage));
801 rm->entries = htonl(entries);
802 rm->amount = GNUNET_htonll(amount);
809 * Signal that all of the data for which a reservation was made has
810 * been stored and that whatever excess space might have been reserved
811 * can now be released.
813 * @param h handle to the datastore
814 * @param rid reservation ID (value of "success" in original continuation
815 * from the "reserve" function).
816 * @param queue_priority ranking of this request in the priority queue
817 * @param max_queue_size at what queue size should this request be dropped
818 * (if other requests of higher priority are in the queue)
819 * @param queue_priority ranking of this request in the priority queue
820 * @param max_queue_size at what queue size should this request be dropped
821 * (if other requests of higher priority are in the queue)
822 * @param timeout how long to wait at most for a response
823 * @param cont continuation to call when done
824 * @param cont_cls closure for cont
825 * @return NULL if the entry was not queued, otherwise a handle that can be used to
826 * cancel; note that even if NULL is returned, the callback will be invoked
827 * (or rather, will already have been invoked)
829 struct GNUNET_DATASTORE_QueueEntry *
830 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
832 unsigned int queue_priority,
833 unsigned int max_queue_size,
834 struct GNUNET_TIME_Relative timeout,
835 GNUNET_DATASTORE_ContinuationWithStatus cont,
838 struct GNUNET_DATASTORE_QueueEntry *qe;
839 struct ReleaseReserveMessage *rrm;
840 union QueueContext qc;
843 cont = &drop_status_cont;
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
846 "Asked to release reserve %d\n",
850 qc.sc.cont_cls = cont_cls;
851 qe = make_queue_entry (h, sizeof(struct ReleaseReserveMessage),
852 queue_priority, max_queue_size, timeout,
853 &process_status_message, &qc);
856 rrm = (struct ReleaseReserveMessage*) &qe[1];
857 rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
858 rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
859 rrm->rid = htonl(rid);
866 * Update a value in the datastore.
868 * @param h handle to the datastore
869 * @param uid identifier for the value
870 * @param priority how much to increase the priority of the value
871 * @param expiration new expiration value should be MAX of existing and this argument
872 * @param queue_priority ranking of this request in the priority queue
873 * @param max_queue_size at what queue size should this request be dropped
874 * (if other requests of higher priority are in the queue)
875 * @param timeout how long to wait at most for a response
876 * @param cont continuation to call when done
877 * @param cont_cls closure for cont
878 * @return NULL if the entry was not queued, otherwise a handle that can be used to
879 * cancel; note that even if NULL is returned, the callback will be invoked
880 * (or rather, will already have been invoked)
882 struct GNUNET_DATASTORE_QueueEntry *
883 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
884 unsigned long long uid,
886 struct GNUNET_TIME_Absolute expiration,
887 unsigned int queue_priority,
888 unsigned int max_queue_size,
889 struct GNUNET_TIME_Relative timeout,
890 GNUNET_DATASTORE_ContinuationWithStatus cont,
893 struct GNUNET_DATASTORE_QueueEntry *qe;
894 struct UpdateMessage *um;
895 union QueueContext qc;
898 cont = &drop_status_cont;
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
903 (unsigned int) priority,
904 (unsigned long long) expiration.value);
907 qc.sc.cont_cls = cont_cls;
908 qe = make_queue_entry (h, sizeof(struct UpdateMessage),
909 queue_priority, max_queue_size, timeout,
910 &process_status_message, &qc);
913 um = (struct UpdateMessage*) &qe[1];
914 um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
915 um->header.size = htons(sizeof (struct UpdateMessage));
916 um->priority = htonl(priority);
917 um->expiration = GNUNET_TIME_absolute_hton(expiration);
918 um->uid = GNUNET_htonll(uid);
925 * Explicitly remove some content from the database.
926 * The "cont"inuation will be called with status
927 * "GNUNET_OK" if content was removed, "GNUNET_NO"
928 * if no matching entry was found and "GNUNET_SYSERR"
929 * on all other types of errors.
931 * @param h handle to the datastore
932 * @param key key for the value
933 * @param size number of bytes in data
934 * @param data content stored
935 * @param queue_priority ranking of this request in the priority queue
936 * @param max_queue_size at what queue size should this request be dropped
937 * (if other requests of higher priority are in the queue)
938 * @param timeout how long to wait at most for a response
939 * @param cont continuation to call when done
940 * @param cont_cls closure for cont
941 * @return NULL if the entry was not queued, otherwise a handle that can be used to
942 * cancel; note that even if NULL is returned, the callback will be invoked
943 * (or rather, will already have been invoked)
945 struct GNUNET_DATASTORE_QueueEntry *
946 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
947 const GNUNET_HashCode *key,
950 unsigned int queue_priority,
951 unsigned int max_queue_size,
952 struct GNUNET_TIME_Relative timeout,
953 GNUNET_DATASTORE_ContinuationWithStatus cont,
956 struct GNUNET_DATASTORE_QueueEntry *qe;
957 struct DataMessage *dm;
959 union QueueContext qc;
962 cont = &drop_status_cont;
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
965 "Asked to remove %u bytes under key `%s'\n",
970 qc.sc.cont_cls = cont_cls;
971 msize = sizeof(struct DataMessage) + size;
972 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
973 qe = make_queue_entry (h, msize,
974 queue_priority, max_queue_size, timeout,
975 &process_status_message, &qc);
978 dm = (struct DataMessage*) &qe[1];
979 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
980 dm->header.size = htons(msize);
982 dm->size = htonl(size);
984 dm->priority = htonl(0);
985 dm->anonymity = htonl(0);
986 dm->uid = GNUNET_htonll(0);
987 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
989 memcpy (&dm[1], data, size);
996 * Type of a function to call when we receive a message
1000 * @param msg message received, NULL on timeout or fatal error
1003 process_result_message (void *cls,
1004 const struct GNUNET_MessageHeader * msg)
1006 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
1007 struct GNUNET_DATASTORE_Handle *h = qe->h;
1008 struct ResultContext rc = qe->qc.rc;
1009 const struct DataMessage *dm;
1010 int was_transmitted;
1015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1016 "Failed to receive response from datastore or queue full\n");
1018 was_transmitted = qe->was_transmitted;
1019 free_queue_entry (qe);
1020 if (GNUNET_YES == was_transmitted)
1022 rc.iter (rc.iter_cls,
1023 NULL, 0, NULL, 0, 0, 0,
1024 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1027 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1028 GNUNET_assert (h->queue_head == qe);
1029 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1031 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
1033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1034 "Received end of result set\n");
1036 free_queue_entry (qe);
1037 rc.iter (rc.iter_cls,
1038 NULL, 0, NULL, 0, 0, 0,
1039 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1043 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
1044 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1045 (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
1048 free_queue_entry (qe);
1049 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1051 rc.iter (rc.iter_cls,
1052 NULL, 0, NULL, 0, 0, 0,
1053 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1056 dm = (const struct DataMessage*) msg;
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1059 "Received result %llu with type %u and size %u with key %s\n",
1060 (unsigned long long) GNUNET_ntohll(dm->uid),
1063 GNUNET_h2s(&dm->key));
1065 rc.iter (rc.iter_cls,
1070 ntohl(dm->priority),
1071 ntohl(dm->anonymity),
1072 GNUNET_TIME_absolute_ntoh(dm->expiration),
1073 GNUNET_ntohll(dm->uid));
1078 * Get a random value from the datastore.
1080 * @param h handle to the datastore
1081 * @param queue_priority ranking of this request in the priority queue
1082 * @param max_queue_size at what queue size should this request be dropped
1083 * (if other requests of higher priority are in the queue)
1084 * @param timeout how long to wait at most for a response
1085 * @param iter function to call on a random value; it
1086 * will be called once with a value (if available)
1087 * and always once with a value of NULL.
1088 * @param iter_cls closure for iter
1089 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1090 * cancel; note that even if NULL is returned, the callback will be invoked
1091 * (or rather, will already have been invoked)
1093 struct GNUNET_DATASTORE_QueueEntry *
1094 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
1095 unsigned int queue_priority,
1096 unsigned int max_queue_size,
1097 struct GNUNET_TIME_Relative timeout,
1098 GNUNET_DATASTORE_Iterator iter,
1101 struct GNUNET_DATASTORE_QueueEntry *qe;
1102 struct GNUNET_MessageHeader *m;
1103 union QueueContext qc;
1106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1107 "Asked to get random entry in %llu ms\n",
1108 (unsigned long long) timeout.value);
1111 qc.rc.iter_cls = iter_cls;
1112 qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader),
1113 queue_priority, max_queue_size, timeout,
1114 &process_result_message, &qc);
1117 m = (struct GNUNET_MessageHeader*) &qe[1];
1118 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
1119 m->size = htons(sizeof (struct GNUNET_MessageHeader));
1127 * Iterate over the results for a particular key
1128 * in the datastore. The iterator will only be called
1129 * once initially; if the first call did contain a
1130 * result, further results can be obtained by calling
1131 * "GNUNET_DATASTORE_get_next" with the given argument.
1133 * @param h handle to the datastore
1134 * @param key maybe NULL (to match all entries)
1135 * @param type desired type, 0 for any
1136 * @param queue_priority ranking of this request in the priority queue
1137 * @param max_queue_size at what queue size should this request be dropped
1138 * (if other requests of higher priority are in the queue)
1139 * @param timeout how long to wait at most for a response
1140 * @param iter function to call on each matching value;
1141 * will be called once with a NULL value at the end
1142 * @param iter_cls closure for iter
1143 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1144 * cancel; note that even if NULL is returned, the callback will be invoked
1145 * (or rather, will already have been invoked)
1147 struct GNUNET_DATASTORE_QueueEntry *
1148 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
1149 const GNUNET_HashCode * key,
1150 enum GNUNET_BLOCK_Type type,
1151 unsigned int queue_priority,
1152 unsigned int max_queue_size,
1153 struct GNUNET_TIME_Relative timeout,
1154 GNUNET_DATASTORE_Iterator iter,
1157 struct GNUNET_DATASTORE_QueueEntry *qe;
1158 struct GetMessage *gm;
1159 union QueueContext qc;
1162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1163 "Asked to look for data of type %u under key `%s'\n",
1164 (unsigned int) type,
1168 qc.rc.iter_cls = iter_cls;
1169 qe = make_queue_entry (h, sizeof(struct GetMessage),
1170 queue_priority, max_queue_size, timeout,
1171 &process_result_message, &qc);
1174 gm = (struct GetMessage*) &qe[1];
1175 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1176 gm->type = htonl(type);
1179 gm->header.size = htons(sizeof (struct GetMessage));
1184 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
1192 * Function called to trigger obtaining the next result
1193 * from the datastore.
1195 * @param h handle to the datastore
1196 * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
1197 * iteration (with a final call to "iter" with key/data == NULL).
1200 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
1203 struct GNUNET_DATASTORE_QueueEntry *qe = h->queue_head;
1204 struct ResultContext rc = qe->qc.rc;
1206 GNUNET_assert (NULL != qe);
1207 GNUNET_assert (&process_result_message == qe->response_proc);
1208 if (GNUNET_YES == more)
1210 GNUNET_CLIENT_receive (h->client,
1213 GNUNET_TIME_absolute_get_remaining (qe->timeout));
1216 free_queue_entry (qe);
1217 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1219 rc.iter (rc.iter_cls,
1220 NULL, 0, NULL, 0, 0, 0,
1221 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1226 * Cancel a datastore operation. The final callback from the
1227 * operation must not have been done yet.
1229 * @param qe operation to cancel
1232 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1234 struct GNUNET_DATASTORE_Handle *h;
1238 reconnect = qe->was_transmitted;
1239 free_queue_entry (qe);
1243 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1249 /* end of datastore_api.c */