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 3, 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;
218 * Are we currently trying to receive from the service?
227 * Connect to the datastore service.
229 * @param cfg configuration to use
230 * @param sched scheduler to use
231 * @return handle to use to access the service
233 struct GNUNET_DATASTORE_Handle *
234 GNUNET_DATASTORE_connect (const struct
235 GNUNET_CONFIGURATION_Handle
238 GNUNET_SCHEDULER_Handle
241 struct GNUNET_CLIENT_Connection *c;
242 struct GNUNET_DATASTORE_Handle *h;
244 c = GNUNET_CLIENT_connect (sched, "datastore", cfg);
246 return NULL; /* oops */
247 h = GNUNET_malloc (sizeof(struct GNUNET_DATASTORE_Handle) +
248 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1);
257 * Transmit DROP message to datastore service.
259 * @param cls the 'struct GNUNET_DATASTORE_Handle'
260 * @param size number of bytes that can be copied to buf
261 * @param buf where to copy the drop message
262 * @return number of bytes written to buf
265 transmit_drop (void *cls,
269 struct GNUNET_DATASTORE_Handle *h = cls;
270 struct GNUNET_MessageHeader *hdr;
274 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
275 _("Failed to transmit request to drop database.\n"));
276 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
279 GNUNET_assert (size >= sizeof(struct GNUNET_MessageHeader));
281 hdr->size = htons(sizeof(struct GNUNET_MessageHeader));
282 hdr->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
283 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
284 return sizeof(struct GNUNET_MessageHeader);
289 * Disconnect from the datastore service (and free
290 * associated resources).
292 * @param h handle to the datastore
293 * @param drop set to GNUNET_YES to delete all data in datastore (!)
295 void GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
298 struct GNUNET_DATASTORE_QueueEntry *qe;
300 if (h->client != NULL)
302 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
305 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
307 GNUNET_SCHEDULER_cancel (h->sched,
309 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
311 while (NULL != (qe = h->queue_head))
313 GNUNET_assert (NULL != qe->response_proc);
314 qe->response_proc (qe, NULL);
316 if (GNUNET_YES == drop)
318 h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
319 if (h->client != NULL)
322 GNUNET_CLIENT_notify_transmit_ready (h->client,
323 sizeof(struct GNUNET_MessageHeader),
324 GNUNET_TIME_UNIT_MINUTES,
329 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
338 * A request has timed out (before being transmitted to the service).
340 * @param cls the 'struct GNUNET_DATASTORE_QueueEntry'
341 * @param tc scheduler context
344 timeout_queue_entry (void *cls,
345 const struct GNUNET_SCHEDULER_TaskContext *tc)
347 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
349 qe->task = GNUNET_SCHEDULER_NO_TASK;
350 GNUNET_assert (qe->was_transmitted == GNUNET_NO);
351 qe->response_proc (qe, NULL);
356 * Create a new entry for our priority queue (and possibly discard other entires if
357 * the queue is getting too long).
359 * @param h handle to the datastore
360 * @param msize size of the message to queue
361 * @param queue_priority priority of the entry
362 * @param max_queue_size at what queue size should this request be dropped
363 * (if other requests of higher priority are in the queue)
364 * @param timeout timeout for the operation
365 * @param response_proc function to call with replies (can be NULL)
366 * @param qc client context (NOT a closure for response_proc)
367 * @return NULL if the queue is full (and this entry was dropped)
369 static struct GNUNET_DATASTORE_QueueEntry *
370 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
372 unsigned int queue_priority,
373 unsigned int max_queue_size,
374 struct GNUNET_TIME_Relative timeout,
375 GNUNET_CLIENT_MessageHandler response_proc,
376 const union QueueContext *qc)
378 struct GNUNET_DATASTORE_QueueEntry *ret;
379 struct GNUNET_DATASTORE_QueueEntry *pos;
384 while ( (pos != NULL) &&
385 (c < max_queue_size) &&
386 (pos->priority >= queue_priority) )
391 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
393 ret->response_proc = response_proc;
395 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
396 ret->priority = queue_priority;
397 ret->max_queue = max_queue_size;
398 ret->message_size = msize;
399 ret->was_transmitted = GNUNET_NO;
402 /* append at the tail */
408 /* do not insert at HEAD if HEAD query was already
409 transmitted and we are still receiving replies! */
410 if ( (pos == NULL) &&
411 (h->queue_head->was_transmitted) )
415 GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
420 if (c > max_queue_size)
422 response_proc (ret, NULL);
425 ret->task = GNUNET_SCHEDULER_add_delayed (h->sched,
427 &timeout_queue_entry,
432 if (pos->max_queue < h->queue_size)
434 GNUNET_assert (pos->response_proc != NULL);
435 pos->response_proc (pos, NULL);
445 * Process entries in the queue (or do nothing if we are already
448 * @param h handle to the datastore
451 process_queue (struct GNUNET_DATASTORE_Handle *h);
455 * Try reconnecting to the datastore service.
457 * @param cls the 'struct GNUNET_DATASTORE_Handle'
458 * @param tc scheduler context
461 try_reconnect (void *cls,
462 const struct GNUNET_SCHEDULER_TaskContext *tc)
464 struct GNUNET_DATASTORE_Handle *h = cls;
466 if (h->retry_time.value < GNUNET_CONSTANTS_SERVICE_RETRY.value)
467 h->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
469 h->retry_time = GNUNET_TIME_relative_multiply (h->retry_time, 2);
470 if (h->retry_time.value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.value)
471 h->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
472 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
473 h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
474 if (h->client == NULL)
476 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
477 "DATASTORE reconnect failed (fatally)\n");
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "Reconnected to DATASTORE\n");
489 * Disconnect from the service and then try reconnecting to the datastore service
492 * @param h handle to datastore to disconnect and reconnect
495 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
497 if (h->client == NULL)
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "client NULL in disconnect, will not try to reconnect\n");
506 GNUNET_STATISTICS_update (stats,
507 gettext_noop ("# reconnected to DATASTORE"),
511 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
513 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->sched,
521 * Transmit request from queue to datastore service.
523 * @param cls the 'struct GNUNET_DATASTORE_Handle'
524 * @param size number of bytes that can be copied to buf
525 * @param buf where to copy the drop message
526 * @return number of bytes written to buf
529 transmit_request (void *cls,
533 struct GNUNET_DATASTORE_Handle *h = cls;
534 struct GNUNET_DATASTORE_QueueEntry *qe;
538 if (NULL == (qe = h->queue_head))
539 return 0; /* no entry in queue */
542 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
543 _("Failed to transmit request to DATASTORE.\n"));
547 if (size < (msize = qe->message_size))
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
554 "Transmitting %u byte request to DATASTORE\n",
557 memcpy (buf, &qe[1], msize);
558 qe->was_transmitted = GNUNET_YES;
559 GNUNET_SCHEDULER_cancel (h->sched,
561 qe->task = GNUNET_SCHEDULER_NO_TASK;
562 h->in_receive = GNUNET_YES;
563 GNUNET_CLIENT_receive (h->client,
566 GNUNET_TIME_absolute_get_remaining (qe->timeout));
572 * Process entries in the queue (or do nothing if we are already
575 * @param h handle to the datastore
578 process_queue (struct GNUNET_DATASTORE_Handle *h)
580 struct GNUNET_DATASTORE_QueueEntry *qe;
582 if (NULL == (qe = h->queue_head))
584 #if DEBUG_DATASTORE > 1
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 return; /* no entry in queue */
590 if (qe->was_transmitted == GNUNET_YES)
592 #if DEBUG_DATASTORE > 1
593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
594 "Head request already transmitted\n");
596 return; /* waiting for replies */
600 #if DEBUG_DATASTORE > 1
601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
602 "Pending transmission request\n");
604 return; /* request pending */
606 if (h->client == NULL)
608 #if DEBUG_DATASTORE > 1
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
612 return; /* waiting for reconnect */
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616 "Queueing %u byte request to DATASTORE\n",
619 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
621 GNUNET_TIME_absolute_get_remaining (qe->timeout),
629 * Dummy continuation used to do nothing (but be non-zero).
632 * @param result result
633 * @param emsg error message
636 drop_status_cont (void *cls, int result, const char *emsg)
643 free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
645 struct GNUNET_DATASTORE_Handle *h = qe->h;
647 GNUNET_CONTAINER_DLL_remove (h->queue_head,
650 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
652 GNUNET_SCHEDULER_cancel (h->sched,
654 qe->task = GNUNET_SCHEDULER_NO_TASK;
661 * Type of a function to call when we receive a message
665 * @param msg message received, NULL on timeout or fatal error
668 process_status_message (void *cls,
670 GNUNET_MessageHeader * msg)
672 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
673 struct GNUNET_DATASTORE_Handle *h = qe->h;
674 struct StatusContext rc = qe->qc.sc;
675 const struct StatusMessage *sm;
680 h->in_receive = GNUNET_NO;
681 was_transmitted = qe->was_transmitted;
684 free_queue_entry (qe);
685 if (NULL == h->client)
686 return; /* forced disconnect */
687 rc.cont (rc.cont_cls,
689 _("Failed to receive response from database."));
690 if (was_transmitted == GNUNET_YES)
694 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
695 GNUNET_assert (h->queue_head == qe);
696 free_queue_entry (qe);
697 if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
698 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) )
701 h->retry_time = GNUNET_TIME_UNIT_ZERO;
703 rc.cont (rc.cont_cls,
705 _("Error reading response from datastore service"));
708 sm = (const struct StatusMessage*) msg;
709 status = ntohl(sm->status);
711 if (ntohs(msg->size) > sizeof(struct StatusMessage))
713 emsg = (const char*) &sm[1];
714 if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
717 emsg = _("Invalid error message received from datastore service");
720 if ( (status == GNUNET_SYSERR) &&
724 emsg = _("Invalid error message received from datastore service");
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728 "Received status %d/%s\n",
732 rc.cont (rc.cont_cls,
740 * Store an item in the datastore. If the item is already present,
741 * the priorities are summed up and the higher expiration time and
742 * lower anonymity level is used.
744 * @param h handle to the datastore
745 * @param rid reservation ID to use (from "reserve"); use 0 if no
746 * prior reservation was made
747 * @param key key for the value
748 * @param size number of bytes in data
749 * @param data content stored
750 * @param type type of the content
751 * @param priority priority of the content
752 * @param anonymity anonymity-level for the content
753 * @param expiration expiration time for the content
754 * @param queue_priority ranking of this request in the priority queue
755 * @param max_queue_size at what queue size should this request be dropped
756 * (if other requests of higher priority are in the queue)
757 * @param timeout timeout for the operation
758 * @param cont continuation to call when done
759 * @param cont_cls closure for cont
760 * @return NULL if the entry was not queued, otherwise a handle that can be used to
761 * cancel; note that even if NULL is returned, the callback will be invoked
762 * (or rather, will already have been invoked)
764 struct GNUNET_DATASTORE_QueueEntry *
765 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
767 const GNUNET_HashCode * key,
770 enum GNUNET_BLOCK_Type type,
773 struct GNUNET_TIME_Absolute expiration,
774 unsigned int queue_priority,
775 unsigned int max_queue_size,
776 struct GNUNET_TIME_Relative timeout,
777 GNUNET_DATASTORE_ContinuationWithStatus cont,
780 struct GNUNET_DATASTORE_QueueEntry *qe;
781 struct DataMessage *dm;
783 union QueueContext qc;
786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
787 "Asked to put %u bytes of data under key `%s'\n",
791 msize = sizeof(struct DataMessage) + size;
792 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
794 qc.sc.cont_cls = cont_cls;
795 qe = make_queue_entry (h, msize,
796 queue_priority, max_queue_size, timeout,
797 &process_status_message, &qc);
800 dm = (struct DataMessage* ) &qe[1];
801 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
802 dm->header.size = htons(msize);
803 dm->rid = htonl(rid);
804 dm->size = htonl(size);
805 dm->type = htonl(type);
806 dm->priority = htonl(priority);
807 dm->anonymity = htonl(anonymity);
808 dm->uid = GNUNET_htonll(0);
809 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
811 memcpy (&dm[1], data, size);
818 * Reserve space in the datastore. This function should be used
819 * to avoid "out of space" failures during a longer sequence of "put"
820 * operations (for example, when a file is being inserted).
822 * @param h handle to the datastore
823 * @param amount how much space (in bytes) should be reserved (for content only)
824 * @param entries how many entries will be created (to calculate per-entry overhead)
825 * @param queue_priority ranking of this request in the priority queue
826 * @param max_queue_size at what queue size should this request be dropped
827 * (if other requests of higher priority are in the queue)
828 * @param timeout how long to wait at most for a response (or before dying in queue)
829 * @param cont continuation to call when done; "success" will be set to
830 * a positive reservation value if space could be reserved.
831 * @param cont_cls closure for cont
832 * @return NULL if the entry was not queued, otherwise a handle that can be used to
833 * cancel; note that even if NULL is returned, the callback will be invoked
834 * (or rather, will already have been invoked)
836 struct GNUNET_DATASTORE_QueueEntry *
837 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
840 unsigned int queue_priority,
841 unsigned int max_queue_size,
842 struct GNUNET_TIME_Relative timeout,
843 GNUNET_DATASTORE_ContinuationWithStatus cont,
846 struct GNUNET_DATASTORE_QueueEntry *qe;
847 struct ReserveMessage *rm;
848 union QueueContext qc;
851 cont = &drop_status_cont;
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
854 "Asked to reserve %llu bytes of data and %u entries'\n",
855 (unsigned long long) amount,
856 (unsigned int) entries);
859 qc.sc.cont_cls = cont_cls;
860 qe = make_queue_entry (h, sizeof(struct ReserveMessage),
861 queue_priority, max_queue_size, timeout,
862 &process_status_message, &qc);
865 rm = (struct ReserveMessage*) &qe[1];
866 rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
867 rm->header.size = htons(sizeof (struct ReserveMessage));
868 rm->entries = htonl(entries);
869 rm->amount = GNUNET_htonll(amount);
876 * Signal that all of the data for which a reservation was made has
877 * been stored and that whatever excess space might have been reserved
878 * can now be released.
880 * @param h handle to the datastore
881 * @param rid reservation ID (value of "success" in original continuation
882 * from the "reserve" function).
883 * @param queue_priority ranking of this request in the priority queue
884 * @param max_queue_size at what queue size should this request be dropped
885 * (if other requests of higher priority are in the queue)
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
892 * @return NULL if the entry was not queued, otherwise a handle that can be used to
893 * cancel; note that even if NULL is returned, the callback will be invoked
894 * (or rather, will already have been invoked)
896 struct GNUNET_DATASTORE_QueueEntry *
897 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
899 unsigned int queue_priority,
900 unsigned int max_queue_size,
901 struct GNUNET_TIME_Relative timeout,
902 GNUNET_DATASTORE_ContinuationWithStatus cont,
905 struct GNUNET_DATASTORE_QueueEntry *qe;
906 struct ReleaseReserveMessage *rrm;
907 union QueueContext qc;
910 cont = &drop_status_cont;
912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
913 "Asked to release reserve %d\n",
917 qc.sc.cont_cls = cont_cls;
918 qe = make_queue_entry (h, sizeof(struct ReleaseReserveMessage),
919 queue_priority, max_queue_size, timeout,
920 &process_status_message, &qc);
923 rrm = (struct ReleaseReserveMessage*) &qe[1];
924 rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
925 rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
926 rrm->rid = htonl(rid);
933 * Update a value in the datastore.
935 * @param h handle to the datastore
936 * @param uid identifier for the value
937 * @param priority how much to increase the priority of the value
938 * @param expiration new expiration value should be MAX of existing and this argument
939 * @param queue_priority ranking of this request in the priority queue
940 * @param max_queue_size at what queue size should this request be dropped
941 * (if other requests of higher priority are in the queue)
942 * @param timeout how long to wait at most for a response
943 * @param cont continuation to call when done
944 * @param cont_cls closure for cont
945 * @return NULL if the entry was not queued, otherwise a handle that can be used to
946 * cancel; note that even if NULL is returned, the callback will be invoked
947 * (or rather, will already have been invoked)
949 struct GNUNET_DATASTORE_QueueEntry *
950 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
951 unsigned long long uid,
953 struct GNUNET_TIME_Absolute expiration,
954 unsigned int queue_priority,
955 unsigned int max_queue_size,
956 struct GNUNET_TIME_Relative timeout,
957 GNUNET_DATASTORE_ContinuationWithStatus cont,
960 struct GNUNET_DATASTORE_QueueEntry *qe;
961 struct UpdateMessage *um;
962 union QueueContext qc;
965 cont = &drop_status_cont;
967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
968 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
970 (unsigned int) priority,
971 (unsigned long long) expiration.value);
974 qc.sc.cont_cls = cont_cls;
975 qe = make_queue_entry (h, sizeof(struct UpdateMessage),
976 queue_priority, max_queue_size, timeout,
977 &process_status_message, &qc);
980 um = (struct UpdateMessage*) &qe[1];
981 um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
982 um->header.size = htons(sizeof (struct UpdateMessage));
983 um->priority = htonl(priority);
984 um->expiration = GNUNET_TIME_absolute_hton(expiration);
985 um->uid = GNUNET_htonll(uid);
992 * Explicitly remove some content from the database.
993 * The "cont"inuation will be called with status
994 * "GNUNET_OK" if content was removed, "GNUNET_NO"
995 * if no matching entry was found and "GNUNET_SYSERR"
996 * on all other types of errors.
998 * @param h handle to the datastore
999 * @param key key for the value
1000 * @param size number of bytes in data
1001 * @param data content stored
1002 * @param queue_priority ranking of this request in the priority queue
1003 * @param max_queue_size at what queue size should this request be dropped
1004 * (if other requests of higher priority are in the queue)
1005 * @param timeout how long to wait at most for a response
1006 * @param cont continuation to call when done
1007 * @param cont_cls closure for cont
1008 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1009 * cancel; note that even if NULL is returned, the callback will be invoked
1010 * (or rather, will already have been invoked)
1012 struct GNUNET_DATASTORE_QueueEntry *
1013 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
1014 const GNUNET_HashCode *key,
1017 unsigned int queue_priority,
1018 unsigned int max_queue_size,
1019 struct GNUNET_TIME_Relative timeout,
1020 GNUNET_DATASTORE_ContinuationWithStatus cont,
1023 struct GNUNET_DATASTORE_QueueEntry *qe;
1024 struct DataMessage *dm;
1026 union QueueContext qc;
1029 cont = &drop_status_cont;
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032 "Asked to remove %u bytes under key `%s'\n",
1037 qc.sc.cont_cls = cont_cls;
1038 msize = sizeof(struct DataMessage) + size;
1039 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1040 qe = make_queue_entry (h, msize,
1041 queue_priority, max_queue_size, timeout,
1042 &process_status_message, &qc);
1045 dm = (struct DataMessage*) &qe[1];
1046 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
1047 dm->header.size = htons(msize);
1049 dm->size = htonl(size);
1050 dm->type = htonl(0);
1051 dm->priority = htonl(0);
1052 dm->anonymity = htonl(0);
1053 dm->uid = GNUNET_htonll(0);
1054 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
1056 memcpy (&dm[1], data, size);
1063 * Type of a function to call when we receive a message
1066 * @param cls closure
1067 * @param msg message received, NULL on timeout or fatal error
1070 process_result_message (void *cls,
1071 const struct GNUNET_MessageHeader * msg)
1073 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
1074 struct GNUNET_DATASTORE_Handle *h = qe->h;
1075 struct ResultContext rc = qe->qc.rc;
1076 const struct DataMessage *dm;
1077 int was_transmitted;
1079 h->in_receive = GNUNET_NO;
1082 was_transmitted = qe->was_transmitted;
1083 free_queue_entry (qe);
1084 if (was_transmitted == GNUNET_YES)
1086 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1087 _("Failed to receive response from database.\n"));
1090 if (rc.iter != NULL)
1091 rc.iter (rc.iter_cls,
1092 NULL, 0, NULL, 0, 0, 0,
1093 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1096 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1097 GNUNET_assert (h->queue_head == qe);
1098 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1100 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
1102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1103 "Received end of result set\n");
1105 free_queue_entry (qe);
1106 if (rc.iter != NULL)
1107 rc.iter (rc.iter_cls,
1108 NULL, 0, NULL, 0, 0, 0,
1109 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1113 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
1114 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1115 (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
1118 free_queue_entry (qe);
1119 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1121 if (rc.iter != NULL)
1122 rc.iter (rc.iter_cls,
1123 NULL, 0, NULL, 0, 0, 0,
1124 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1127 if (rc.iter == NULL)
1129 /* abort iteration */
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 "Aborting iteration via disconnect (client has cancelled)\n");
1134 free_queue_entry (qe);
1135 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1139 dm = (const struct DataMessage*) msg;
1141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1142 "Received result %llu with type %u and size %u with key %s\n",
1143 (unsigned long long) GNUNET_ntohll(dm->uid),
1146 GNUNET_h2s(&dm->key));
1148 rc.iter (rc.iter_cls,
1153 ntohl(dm->priority),
1154 ntohl(dm->anonymity),
1155 GNUNET_TIME_absolute_ntoh(dm->expiration),
1156 GNUNET_ntohll(dm->uid));
1161 * Get a random value from the datastore.
1163 * @param h handle to the datastore
1164 * @param queue_priority ranking of this request in the priority queue
1165 * @param max_queue_size at what queue size should this request be dropped
1166 * (if other requests of higher priority are in the queue)
1167 * @param timeout how long to wait at most for a response
1168 * @param iter function to call on a random value; it
1169 * will be called once with a value (if available)
1170 * and always once with a value of NULL.
1171 * @param iter_cls closure for iter
1172 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1173 * cancel; note that even if NULL is returned, the callback will be invoked
1174 * (or rather, will already have been invoked)
1176 struct GNUNET_DATASTORE_QueueEntry *
1177 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
1178 unsigned int queue_priority,
1179 unsigned int max_queue_size,
1180 struct GNUNET_TIME_Relative timeout,
1181 GNUNET_DATASTORE_Iterator iter,
1184 struct GNUNET_DATASTORE_QueueEntry *qe;
1185 struct GNUNET_MessageHeader *m;
1186 union QueueContext qc;
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190 "Asked to get random entry in %llu ms\n",
1191 (unsigned long long) timeout.value);
1194 qc.rc.iter_cls = iter_cls;
1195 qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader),
1196 queue_priority, max_queue_size, timeout,
1197 &process_result_message, &qc);
1200 m = (struct GNUNET_MessageHeader*) &qe[1];
1201 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
1202 m->size = htons(sizeof (struct GNUNET_MessageHeader));
1210 * Iterate over the results for a particular key
1211 * in the datastore. The iterator will only be called
1212 * once initially; if the first call did contain a
1213 * result, further results can be obtained by calling
1214 * "GNUNET_DATASTORE_get_next" with the given argument.
1216 * @param h handle to the datastore
1217 * @param key maybe NULL (to match all entries)
1218 * @param type desired type, 0 for any
1219 * @param queue_priority ranking of this request in the priority queue
1220 * @param max_queue_size at what queue size should this request be dropped
1221 * (if other requests of higher priority are in the queue)
1222 * @param timeout how long to wait at most for a response
1223 * @param iter function to call on each matching value;
1224 * will be called once with a NULL value at the end
1225 * @param iter_cls closure for iter
1226 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1227 * cancel; note that even if NULL is returned, the callback will be invoked
1228 * (or rather, will already have been invoked)
1230 struct GNUNET_DATASTORE_QueueEntry *
1231 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
1232 const GNUNET_HashCode * key,
1233 enum GNUNET_BLOCK_Type type,
1234 unsigned int queue_priority,
1235 unsigned int max_queue_size,
1236 struct GNUNET_TIME_Relative timeout,
1237 GNUNET_DATASTORE_Iterator iter,
1240 struct GNUNET_DATASTORE_QueueEntry *qe;
1241 struct GetMessage *gm;
1242 union QueueContext qc;
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1246 "Asked to look for data of type %u under key `%s'\n",
1247 (unsigned int) type,
1251 qc.rc.iter_cls = iter_cls;
1252 qe = make_queue_entry (h, sizeof(struct GetMessage),
1253 queue_priority, max_queue_size, timeout,
1254 &process_result_message, &qc);
1257 gm = (struct GetMessage*) &qe[1];
1258 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1259 gm->type = htonl(type);
1262 gm->header.size = htons(sizeof (struct GetMessage));
1267 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
1275 * Function called to trigger obtaining the next result
1276 * from the datastore.
1278 * @param h handle to the datastore
1279 * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
1280 * iteration (with a final call to "iter" with key/data == NULL).
1283 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
1286 struct GNUNET_DATASTORE_QueueEntry *qe = h->queue_head;
1287 struct ResultContext rc = qe->qc.rc;
1289 GNUNET_assert (&process_result_message == qe->response_proc);
1290 if (GNUNET_YES == more)
1292 h->in_receive = GNUNET_YES;
1293 GNUNET_CLIENT_receive (h->client,
1296 GNUNET_TIME_absolute_get_remaining (qe->timeout));
1299 free_queue_entry (qe);
1300 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1302 rc.iter (rc.iter_cls,
1303 NULL, 0, NULL, 0, 0, 0,
1304 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1309 * Cancel a datastore operation. The final callback from the
1310 * operation must not have been done yet.
1312 * @param qe operation to cancel
1315 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1317 struct GNUNET_DATASTORE_Handle *h;
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1323 "Pending DATASTORE request %p cancelled (%d, %d)\n",
1325 qe->was_transmitted,
1326 h->queue_head == qe);
1328 reconnect = GNUNET_NO;
1329 if (GNUNET_YES == qe->was_transmitted)
1331 if (qe->response_proc == &process_result_message)
1333 qe->qc.rc.iter = NULL;
1334 if (GNUNET_YES != h->in_receive)
1335 GNUNET_DATASTORE_get_next (h, GNUNET_YES);
1338 reconnect = GNUNET_YES;
1340 free_queue_entry (qe);
1343 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1353 /* end of datastore_api.c */