2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011 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 "gnunet_statistics_service.h"
32 #include "datastore.h"
35 * If a client stopped asking for more results, how many more do
36 * we receive from the DB before killing the connection? Trade-off
37 * between re-doing TCP handshakes and (needlessly) receiving
40 #define MAX_EXCESS_RESULTS 8
43 * Context for processing status messages.
48 * Continuation to call with the status.
50 GNUNET_DATASTORE_ContinuationWithStatus cont;
61 * Context for processing result messages.
66 * Function to call with the result.
68 GNUNET_DATASTORE_DatumProcessor proc;
79 * Context for a queue operation.
84 struct StatusContext sc;
86 struct ResultContext rc;
93 * Entry in our priority queue.
95 struct GNUNET_DATASTORE_QueueEntry
99 * This is a linked list.
101 struct GNUNET_DATASTORE_QueueEntry *next;
104 * This is a linked list.
106 struct GNUNET_DATASTORE_QueueEntry *prev;
109 * Handle to the master context.
111 struct GNUNET_DATASTORE_Handle *h;
114 * Response processor (NULL if we are not waiting for a response).
115 * This struct should be used for the closure, function-specific
116 * arguments can be passed via 'qc'.
118 GNUNET_CLIENT_MessageHandler response_proc;
121 * Function to call after transmission of the request.
123 GNUNET_DATASTORE_ContinuationWithStatus cont;
126 * Closure for 'cont'.
131 * Context for the operation.
133 union QueueContext qc;
136 * Task for timeout signalling.
138 GNUNET_SCHEDULER_TaskIdentifier task;
141 * Timeout for the current operation.
143 struct GNUNET_TIME_Absolute timeout;
146 * Priority in the queue.
148 unsigned int priority;
151 * Maximum allowed length of queue (otherwise
152 * this request should be discarded).
154 unsigned int max_queue;
157 * Number of bytes in the request message following
158 * this struct. 32-bit value for nicer memory
159 * access (and overall struct alignment).
161 uint32_t message_size;
164 * Has this message been transmitted to the service?
165 * Only ever GNUNET_YES for the head of the queue.
166 * Note that the overall struct should end at a
167 * multiple of 64 bits.
174 * Handle to the datastore service.
176 struct GNUNET_DATASTORE_Handle
182 const struct GNUNET_CONFIGURATION_Handle *cfg;
185 * Current connection to the datastore service.
187 struct GNUNET_CLIENT_Connection *client;
190 * Handle for statistics.
192 struct GNUNET_STATISTICS_Handle *stats;
195 * Current transmit handle.
197 struct GNUNET_CLIENT_TransmitHandle *th;
200 * Current head of priority queue.
202 struct GNUNET_DATASTORE_QueueEntry *queue_head;
205 * Current tail of priority queue.
207 struct GNUNET_DATASTORE_QueueEntry *queue_tail;
210 * Task for trying to reconnect.
212 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
215 * How quickly should we retry? Used for exponential back-off on
218 struct GNUNET_TIME_Relative retry_time;
221 * Number of entries in the queue.
223 unsigned int queue_size;
226 * Number of results we're receiving for the current query
227 * after application stopped to care. Used to determine when
228 * to reset the connection.
230 unsigned int result_count;
233 * Are we currently trying to receive from the service?
238 * We should ignore the next message(s) from the service.
240 unsigned int skip_next_messages;
247 * Connect to the datastore service.
249 * @param cfg configuration to use
250 * @return handle to use to access the service
252 struct GNUNET_DATASTORE_Handle *
253 GNUNET_DATASTORE_connect (const struct
254 GNUNET_CONFIGURATION_Handle
257 struct GNUNET_CLIENT_Connection *c;
258 struct GNUNET_DATASTORE_Handle *h;
260 c = GNUNET_CLIENT_connect ("datastore", cfg);
262 return NULL; /* oops */
263 h = GNUNET_malloc (sizeof(struct GNUNET_DATASTORE_Handle) +
264 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1);
267 h->stats = GNUNET_STATISTICS_create ("datastore-api",
274 * Transmit DROP message to datastore service.
276 * @param cls the 'struct GNUNET_DATASTORE_Handle'
277 * @param size number of bytes that can be copied to buf
278 * @param buf where to copy the drop message
279 * @return number of bytes written to buf
282 transmit_drop (void *cls,
286 struct GNUNET_DATASTORE_Handle *h = cls;
287 struct GNUNET_MessageHeader *hdr;
291 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
292 _("Failed to transmit request to drop database.\n"));
293 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
296 GNUNET_assert (size >= sizeof(struct GNUNET_MessageHeader));
298 hdr->size = htons(sizeof(struct GNUNET_MessageHeader));
299 hdr->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
300 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
301 return sizeof(struct GNUNET_MessageHeader);
306 * Disconnect from the datastore service (and free
307 * associated resources).
309 * @param h handle to the datastore
310 * @param drop set to GNUNET_YES to delete all data in datastore (!)
313 GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
316 struct GNUNET_DATASTORE_QueueEntry *qe;
318 if (h->client != NULL)
320 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
323 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
325 GNUNET_SCHEDULER_cancel (h->reconnect_task);
326 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
328 while (NULL != (qe = h->queue_head))
330 GNUNET_assert (NULL != qe->response_proc);
331 qe->response_proc (h, NULL);
333 if (GNUNET_YES == drop)
335 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
336 if (h->client != NULL)
339 GNUNET_CLIENT_notify_transmit_ready (h->client,
340 sizeof(struct GNUNET_MessageHeader),
341 GNUNET_TIME_UNIT_MINUTES,
346 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
350 GNUNET_STATISTICS_destroy (h->stats,
357 * A request has timed out (before being transmitted to the service).
359 * @param cls the 'struct GNUNET_DATASTORE_QueueEntry'
360 * @param tc scheduler context
363 timeout_queue_entry (void *cls,
364 const struct GNUNET_SCHEDULER_TaskContext *tc)
366 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
368 GNUNET_STATISTICS_update (qe->h->stats,
369 gettext_noop ("# queue entry timeouts"),
372 qe->task = GNUNET_SCHEDULER_NO_TASK;
373 GNUNET_assert (qe->was_transmitted == GNUNET_NO);
374 qe->response_proc (qe->h, NULL);
379 * Create a new entry for our priority queue (and possibly discard other entires if
380 * the queue is getting too long).
382 * @param h handle to the datastore
383 * @param msize size of the message to queue
384 * @param queue_priority priority of the entry
385 * @param max_queue_size at what queue size should this request be dropped
386 * (if other requests of higher priority are in the queue)
387 * @param timeout timeout for the operation
388 * @param response_proc function to call with replies (can be NULL)
389 * @param qc client context (NOT a closure for response_proc)
390 * @return NULL if the queue is full
392 static struct GNUNET_DATASTORE_QueueEntry *
393 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
395 unsigned int queue_priority,
396 unsigned int max_queue_size,
397 struct GNUNET_TIME_Relative timeout,
398 GNUNET_CLIENT_MessageHandler response_proc,
399 const union QueueContext *qc)
401 struct GNUNET_DATASTORE_QueueEntry *ret;
402 struct GNUNET_DATASTORE_QueueEntry *pos;
407 while ( (pos != NULL) &&
408 (c < max_queue_size) &&
409 (pos->priority >= queue_priority) )
414 if (c >= max_queue_size)
416 GNUNET_STATISTICS_update (h->stats,
417 gettext_noop ("# queue overflows"),
422 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
424 ret->response_proc = response_proc;
426 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
427 ret->priority = queue_priority;
428 ret->max_queue = max_queue_size;
429 ret->message_size = msize;
430 ret->was_transmitted = GNUNET_NO;
433 /* append at the tail */
439 /* do not insert at HEAD if HEAD query was already
440 transmitted and we are still receiving replies! */
441 if ( (pos == NULL) &&
442 (h->queue_head->was_transmitted) )
446 GNUNET_STATISTICS_update (h->stats,
447 gettext_noop ("# queue entries created"),
450 GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
455 ret->task = GNUNET_SCHEDULER_add_delayed (timeout,
456 &timeout_queue_entry,
461 if (pos->max_queue < h->queue_size)
463 GNUNET_assert (pos->response_proc != NULL);
464 /* move 'pos' element to head so that it will be
465 killed on 'NULL' call below */
466 GNUNET_CONTAINER_DLL_remove (h->queue_head,
469 GNUNET_CONTAINER_DLL_insert (h->queue_head,
472 pos->response_proc (h, NULL);
482 * Process entries in the queue (or do nothing if we are already
485 * @param h handle to the datastore
488 process_queue (struct GNUNET_DATASTORE_Handle *h);
492 * Try reconnecting to the datastore service.
494 * @param cls the 'struct GNUNET_DATASTORE_Handle'
495 * @param tc scheduler context
498 try_reconnect (void *cls,
499 const struct GNUNET_SCHEDULER_TaskContext *tc)
501 struct GNUNET_DATASTORE_Handle *h = cls;
503 if (h->retry_time.rel_value < GNUNET_CONSTANTS_SERVICE_RETRY.rel_value)
504 h->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
506 h->retry_time = GNUNET_TIME_relative_multiply (h->retry_time, 2);
507 if (h->retry_time.rel_value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.rel_value)
508 h->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
509 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
510 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
511 if (h->client == NULL)
513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
514 "DATASTORE reconnect failed (fatally)\n");
517 GNUNET_STATISTICS_update (h->stats,
518 gettext_noop ("# datastore connections (re)created"),
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523 "Reconnected to DATASTORE\n");
530 * Disconnect from the service and then try reconnecting to the datastore service
533 * @param h handle to datastore to disconnect and reconnect
536 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
538 if (h->client == NULL)
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542 "client NULL in disconnect, will not try to reconnect\n");
547 GNUNET_STATISTICS_update (stats,
548 gettext_noop ("# reconnected to DATASTORE"),
552 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
553 h->skip_next_messages = 0;
555 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_time,
562 * Transmit request from queue to datastore service.
564 * @param cls the 'struct GNUNET_DATASTORE_Handle'
565 * @param size number of bytes that can be copied to buf
566 * @param buf where to copy the drop message
567 * @return number of bytes written to buf
570 transmit_request (void *cls,
574 struct GNUNET_DATASTORE_Handle *h = cls;
575 struct GNUNET_DATASTORE_QueueEntry *qe;
579 if (NULL == (qe = h->queue_head))
580 return 0; /* no entry in queue */
583 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
584 _("Failed to transmit request to DATASTORE.\n"));
585 GNUNET_STATISTICS_update (h->stats,
586 gettext_noop ("# transmission request failures"),
592 if (size < (msize = qe->message_size))
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
599 "Transmitting %u byte request to DATASTORE\n",
602 memcpy (buf, &qe[1], msize);
603 qe->was_transmitted = GNUNET_YES;
604 GNUNET_SCHEDULER_cancel (qe->task);
605 qe->task = GNUNET_SCHEDULER_NO_TASK;
606 h->in_receive = GNUNET_YES;
607 GNUNET_CLIENT_receive (h->client,
610 GNUNET_TIME_absolute_get_remaining (qe->timeout));
611 GNUNET_STATISTICS_update (h->stats,
612 gettext_noop ("# bytes sent to datastore"),
620 * Process entries in the queue (or do nothing if we are already
623 * @param h handle to the datastore
626 process_queue (struct GNUNET_DATASTORE_Handle *h)
628 struct GNUNET_DATASTORE_QueueEntry *qe;
630 if (NULL == (qe = h->queue_head))
632 #if DEBUG_DATASTORE > 1
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 return; /* no entry in queue */
638 if (qe->was_transmitted == GNUNET_YES)
640 #if DEBUG_DATASTORE > 1
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
642 "Head request already transmitted\n");
644 return; /* waiting for replies */
648 #if DEBUG_DATASTORE > 1
649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
650 "Pending transmission request\n");
652 return; /* request pending */
654 if (h->client == NULL)
656 #if DEBUG_DATASTORE > 1
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660 return; /* waiting for reconnect */
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "Queueing %u byte request to DATASTORE\n",
667 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
669 GNUNET_TIME_absolute_get_remaining (qe->timeout),
677 * Dummy continuation used to do nothing (but be non-zero).
680 * @param result result
681 * @param emsg error message
684 drop_status_cont (void *cls, int32_t result, const char *emsg)
691 free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
693 struct GNUNET_DATASTORE_Handle *h = qe->h;
695 GNUNET_CONTAINER_DLL_remove (h->queue_head,
698 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
700 GNUNET_SCHEDULER_cancel (qe->task);
701 qe->task = GNUNET_SCHEDULER_NO_TASK;
704 qe->was_transmitted = GNUNET_SYSERR; /* use-after-free warning */
709 * Type of a function to call when we receive a message
713 * @param msg message received, NULL on timeout or fatal error
716 process_status_message (void *cls,
718 GNUNET_MessageHeader * msg)
720 struct GNUNET_DATASTORE_Handle *h = cls;
721 struct GNUNET_DATASTORE_QueueEntry *qe;
722 struct StatusContext rc;
723 const struct StatusMessage *sm;
728 h->in_receive = GNUNET_NO;
729 if (h->skip_next_messages > 0)
731 h->skip_next_messages--;
735 if (NULL == (qe = h->queue_head))
744 was_transmitted = qe->was_transmitted;
745 free_queue_entry (qe);
746 if (NULL == h->client)
747 return; /* forced disconnect */
749 rc.cont (rc.cont_cls,
751 _("Failed to receive status response from database."));
752 if (was_transmitted == GNUNET_YES)
756 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
757 free_queue_entry (qe);
758 if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
759 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) )
762 h->retry_time = GNUNET_TIME_UNIT_ZERO;
765 rc.cont (rc.cont_cls,
767 _("Error reading response from datastore service"));
770 sm = (const struct StatusMessage*) msg;
771 status = ntohl(sm->status);
773 if (ntohs(msg->size) > sizeof(struct StatusMessage))
775 emsg = (const char*) &sm[1];
776 if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
779 emsg = _("Invalid error message received from datastore service");
782 if ( (status == GNUNET_SYSERR) &&
786 emsg = _("Invalid error message received from datastore service");
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
790 "Received status %d/%s\n",
794 GNUNET_STATISTICS_update (h->stats,
795 gettext_noop ("# status messages received"),
798 h->retry_time.rel_value = 0;
801 rc.cont (rc.cont_cls,
808 * Store an item in the datastore. If the item is already present,
809 * the priorities are summed up and the higher expiration time and
810 * lower anonymity level is used.
812 * @param h handle to the datastore
813 * @param rid reservation ID to use (from "reserve"); use 0 if no
814 * prior reservation was made
815 * @param key key for the value
816 * @param size number of bytes in data
817 * @param data content stored
818 * @param type type of the content
819 * @param priority priority of the content
820 * @param anonymity anonymity-level for the content
821 * @param replication how often should the content be replicated to other peers?
822 * @param expiration expiration time for the content
823 * @param queue_priority ranking of this request in the priority queue
824 * @param max_queue_size at what queue size should this request be dropped
825 * (if other requests of higher priority are in the queue)
826 * @param timeout timeout for the operation
827 * @param cont continuation to call when done
828 * @param cont_cls closure for cont
829 * @return NULL if the entry was not queued, otherwise a handle that can be used to
830 * cancel; note that even if NULL is returned, the callback will be invoked
831 * (or rather, will already have been invoked)
833 struct GNUNET_DATASTORE_QueueEntry *
834 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
836 const GNUNET_HashCode * key,
839 enum GNUNET_BLOCK_Type type,
842 uint32_t replication,
843 struct GNUNET_TIME_Absolute expiration,
844 unsigned int queue_priority,
845 unsigned int max_queue_size,
846 struct GNUNET_TIME_Relative timeout,
847 GNUNET_DATASTORE_ContinuationWithStatus cont,
850 struct GNUNET_DATASTORE_QueueEntry *qe;
851 struct DataMessage *dm;
853 union QueueContext qc;
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
857 "Asked to put %u bytes of data under key `%s' for %llu ms\n",
860 GNUNET_TIME_absolute_get_remaining (expiration).rel_value);
862 msize = sizeof(struct DataMessage) + size;
863 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
865 qc.sc.cont_cls = cont_cls;
866 qe = make_queue_entry (h, msize,
867 queue_priority, max_queue_size, timeout,
868 &process_status_message, &qc);
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873 "Could not create queue entry for PUT\n");
877 GNUNET_STATISTICS_update (h->stats,
878 gettext_noop ("# PUT requests executed"),
881 dm = (struct DataMessage* ) &qe[1];
882 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
883 dm->header.size = htons(msize);
884 dm->rid = htonl(rid);
885 dm->size = htonl( (uint32_t) size);
886 dm->type = htonl(type);
887 dm->priority = htonl(priority);
888 dm->anonymity = htonl(anonymity);
889 dm->replication = htonl (replication);
890 dm->reserved = htonl (0);
891 dm->uid = GNUNET_htonll(0);
892 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
894 memcpy (&dm[1], data, size);
901 * Reserve space in the datastore. This function should be used
902 * to avoid "out of space" failures during a longer sequence of "put"
903 * operations (for example, when a file is being inserted).
905 * @param h handle to the datastore
906 * @param amount how much space (in bytes) should be reserved (for content only)
907 * @param entries how many entries will be created (to calculate per-entry overhead)
908 * @param queue_priority ranking of this request in the priority queue
909 * @param max_queue_size at what queue size should this request be dropped
910 * (if other requests of higher priority are in the queue)
911 * @param timeout how long to wait at most for a response (or before dying in queue)
912 * @param cont continuation to call when done; "success" will be set to
913 * a positive reservation value if space could be reserved.
914 * @param cont_cls closure for cont
915 * @return NULL if the entry was not queued, otherwise a handle that can be used to
916 * cancel; note that even if NULL is returned, the callback will be invoked
917 * (or rather, will already have been invoked)
919 struct GNUNET_DATASTORE_QueueEntry *
920 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
923 unsigned int queue_priority,
924 unsigned int max_queue_size,
925 struct GNUNET_TIME_Relative timeout,
926 GNUNET_DATASTORE_ContinuationWithStatus cont,
929 struct GNUNET_DATASTORE_QueueEntry *qe;
930 struct ReserveMessage *rm;
931 union QueueContext qc;
934 cont = &drop_status_cont;
936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
937 "Asked to reserve %llu bytes of data and %u entries'\n",
938 (unsigned long long) amount,
939 (unsigned int) entries);
942 qc.sc.cont_cls = cont_cls;
943 qe = make_queue_entry (h, sizeof(struct ReserveMessage),
944 queue_priority, max_queue_size, timeout,
945 &process_status_message, &qc);
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950 "Could not create queue entry to reserve\n");
954 GNUNET_STATISTICS_update (h->stats,
955 gettext_noop ("# RESERVE requests executed"),
958 rm = (struct ReserveMessage*) &qe[1];
959 rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
960 rm->header.size = htons(sizeof (struct ReserveMessage));
961 rm->entries = htonl(entries);
962 rm->amount = GNUNET_htonll(amount);
969 * Signal that all of the data for which a reservation was made has
970 * been stored and that whatever excess space might have been reserved
971 * can now be released.
973 * @param h handle to the datastore
974 * @param rid reservation ID (value of "success" in original continuation
975 * from the "reserve" function).
976 * @param queue_priority ranking of this request in the priority queue
977 * @param max_queue_size at what queue size should this request be dropped
978 * (if other requests of higher priority are in the queue)
979 * @param queue_priority ranking of this request in the priority queue
980 * @param max_queue_size at what queue size should this request be dropped
981 * (if other requests of higher priority are in the queue)
982 * @param timeout how long to wait at most for a response
983 * @param cont continuation to call when done
984 * @param cont_cls closure for cont
985 * @return NULL if the entry was not queued, otherwise a handle that can be used to
986 * cancel; note that even if NULL is returned, the callback will be invoked
987 * (or rather, will already have been invoked)
989 struct GNUNET_DATASTORE_QueueEntry *
990 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
992 unsigned int queue_priority,
993 unsigned int max_queue_size,
994 struct GNUNET_TIME_Relative timeout,
995 GNUNET_DATASTORE_ContinuationWithStatus cont,
998 struct GNUNET_DATASTORE_QueueEntry *qe;
999 struct ReleaseReserveMessage *rrm;
1000 union QueueContext qc;
1003 cont = &drop_status_cont;
1005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006 "Asked to release reserve %d\n",
1010 qc.sc.cont_cls = cont_cls;
1011 qe = make_queue_entry (h, sizeof(struct ReleaseReserveMessage),
1012 queue_priority, max_queue_size, timeout,
1013 &process_status_message, &qc);
1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1018 "Could not create queue entry to release reserve\n");
1022 GNUNET_STATISTICS_update (h->stats,
1023 gettext_noop ("# RELEASE RESERVE requests executed"),
1026 rrm = (struct ReleaseReserveMessage*) &qe[1];
1027 rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
1028 rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
1029 rrm->rid = htonl(rid);
1036 * Update a value in the datastore.
1038 * @param h handle to the datastore
1039 * @param uid identifier for the value
1040 * @param priority how much to increase the priority of the value
1041 * @param expiration new expiration value should be MAX of existing and this argument
1042 * @param queue_priority ranking of this request in the priority queue
1043 * @param max_queue_size at what queue size should this request be dropped
1044 * (if other requests of higher priority are in the queue)
1045 * @param timeout how long to wait at most for a response
1046 * @param cont continuation to call when done
1047 * @param cont_cls closure for cont
1048 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1049 * cancel; note that even if NULL is returned, the callback will be invoked
1050 * (or rather, will already have been invoked)
1052 struct GNUNET_DATASTORE_QueueEntry *
1053 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
1056 struct GNUNET_TIME_Absolute expiration,
1057 unsigned int queue_priority,
1058 unsigned int max_queue_size,
1059 struct GNUNET_TIME_Relative timeout,
1060 GNUNET_DATASTORE_ContinuationWithStatus cont,
1063 struct GNUNET_DATASTORE_QueueEntry *qe;
1064 struct UpdateMessage *um;
1065 union QueueContext qc;
1068 cont = &drop_status_cont;
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
1073 (unsigned int) priority,
1074 (unsigned long long) expiration.abs_value);
1077 qc.sc.cont_cls = cont_cls;
1078 qe = make_queue_entry (h, sizeof(struct UpdateMessage),
1079 queue_priority, max_queue_size, timeout,
1080 &process_status_message, &qc);
1084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1085 "Could not create queue entry for UPDATE\n");
1089 GNUNET_STATISTICS_update (h->stats,
1090 gettext_noop ("# UPDATE requests executed"),
1093 um = (struct UpdateMessage*) &qe[1];
1094 um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
1095 um->header.size = htons(sizeof (struct UpdateMessage));
1096 um->priority = htonl(priority);
1097 um->expiration = GNUNET_TIME_absolute_hton(expiration);
1098 um->uid = GNUNET_htonll(uid);
1105 * Explicitly remove some content from the database.
1106 * The "cont"inuation will be called with status
1107 * "GNUNET_OK" if content was removed, "GNUNET_NO"
1108 * if no matching entry was found and "GNUNET_SYSERR"
1109 * on all other types of errors.
1111 * @param h handle to the datastore
1112 * @param key key for the value
1113 * @param size number of bytes in data
1114 * @param data content stored
1115 * @param queue_priority ranking of this request in the priority queue
1116 * @param max_queue_size at what queue size should this request be dropped
1117 * (if other requests of higher priority are in the queue)
1118 * @param timeout how long to wait at most for a response
1119 * @param cont continuation to call when done
1120 * @param cont_cls closure for cont
1121 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1122 * cancel; note that even if NULL is returned, the callback will be invoked
1123 * (or rather, will already have been invoked)
1125 struct GNUNET_DATASTORE_QueueEntry *
1126 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
1127 const GNUNET_HashCode *key,
1130 unsigned int queue_priority,
1131 unsigned int max_queue_size,
1132 struct GNUNET_TIME_Relative timeout,
1133 GNUNET_DATASTORE_ContinuationWithStatus cont,
1136 struct GNUNET_DATASTORE_QueueEntry *qe;
1137 struct DataMessage *dm;
1139 union QueueContext qc;
1142 cont = &drop_status_cont;
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145 "Asked to remove %u bytes under key `%s'\n",
1150 qc.sc.cont_cls = cont_cls;
1151 msize = sizeof(struct DataMessage) + size;
1152 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1153 qe = make_queue_entry (h, msize,
1154 queue_priority, max_queue_size, timeout,
1155 &process_status_message, &qc);
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1160 "Could not create queue entry for REMOVE\n");
1164 GNUNET_STATISTICS_update (h->stats,
1165 gettext_noop ("# REMOVE requests executed"),
1168 dm = (struct DataMessage*) &qe[1];
1169 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
1170 dm->header.size = htons(msize);
1172 dm->size = htonl(size);
1173 dm->type = htonl(0);
1174 dm->priority = htonl(0);
1175 dm->anonymity = htonl(0);
1176 dm->uid = GNUNET_htonll(0);
1177 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
1179 memcpy (&dm[1], data, size);
1186 * Type of a function to call when we receive a message
1189 * @param cls closure
1190 * @param msg message received, NULL on timeout or fatal error
1193 process_result_message (void *cls,
1194 const struct GNUNET_MessageHeader *msg)
1196 struct GNUNET_DATASTORE_Handle *h = cls;
1197 struct GNUNET_DATASTORE_QueueEntry *qe;
1198 struct ResultContext rc;
1199 const struct DataMessage *dm;
1201 h->in_receive = GNUNET_NO;
1202 if (h->skip_next_messages > 0)
1204 h->skip_next_messages--;
1211 GNUNET_assert (NULL != qe);
1212 if (qe->was_transmitted == GNUNET_YES)
1215 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1216 _("Failed to receive response from database.\n"));
1218 free_queue_entry (qe);
1219 if (rc.proc != NULL)
1220 rc.proc (rc.proc_cls,
1221 NULL, 0, NULL, 0, 0, 0,
1222 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1226 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1228 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
1231 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1232 free_queue_entry (qe);
1234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1235 "Received end of result set, new queue size is %u\n",
1238 if (rc.proc != NULL)
1239 rc.proc (rc.proc_cls,
1240 NULL, 0, NULL, 0, 0, 0,
1241 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1242 h->retry_time.rel_value = 0;
1243 h->result_count = 0;
1249 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1250 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
1251 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1252 (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
1255 free_queue_entry (qe);
1256 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1258 if (rc.proc != NULL)
1259 rc.proc (rc.proc_cls,
1260 NULL, 0, NULL, 0, 0, 0,
1261 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1264 GNUNET_STATISTICS_update (h->stats,
1265 gettext_noop ("# Results received"),
1268 dm = (const struct DataMessage*) msg;
1270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1271 "Received result %llu with type %u and size %u with key %s\n",
1272 (unsigned long long) GNUNET_ntohll(dm->uid),
1275 GNUNET_h2s(&dm->key));
1277 free_queue_entry (qe);
1278 h->retry_time.rel_value = 0;
1279 rc.proc (rc.proc_cls,
1284 ntohl(dm->priority),
1285 ntohl(dm->anonymity),
1286 GNUNET_TIME_absolute_ntoh(dm->expiration),
1287 GNUNET_ntohll(dm->uid));
1292 * Get a random value from the datastore for content replication.
1293 * Returns a single, random value among those with the highest
1294 * replication score, lowering positive replication scores by one for
1295 * the chosen value (if only content with a replication score exists,
1296 * a random value is returned and replication scores are not changed).
1298 * @param h handle to the datastore
1299 * @param queue_priority ranking of this request in the priority queue
1300 * @param max_queue_size at what queue size should this request be dropped
1301 * (if other requests of higher priority are in the queue)
1302 * @param timeout how long to wait at most for a response
1303 * @param proc function to call on a random value; it
1304 * will be called once with a value (if available)
1305 * and always once with a value of NULL.
1306 * @param proc_cls closure for proc
1307 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1310 struct GNUNET_DATASTORE_QueueEntry *
1311 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
1312 unsigned int queue_priority,
1313 unsigned int max_queue_size,
1314 struct GNUNET_TIME_Relative timeout,
1315 GNUNET_DATASTORE_DatumProcessor proc,
1318 struct GNUNET_DATASTORE_QueueEntry *qe;
1319 struct GNUNET_MessageHeader *m;
1320 union QueueContext qc;
1322 GNUNET_assert (NULL != proc);
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1325 "Asked to get replication entry in %llu ms\n",
1326 (unsigned long long) timeout.rel_value);
1329 qc.rc.proc_cls = proc_cls;
1330 qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader),
1331 queue_priority, max_queue_size, timeout,
1332 &process_result_message, &qc);
1336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1337 "Could not create queue entry for GET REPLICATION\n");
1341 GNUNET_STATISTICS_update (h->stats,
1342 gettext_noop ("# GET REPLICATION requests executed"),
1345 m = (struct GNUNET_MessageHeader*) &qe[1];
1346 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION);
1347 m->size = htons(sizeof (struct GNUNET_MessageHeader));
1354 * Get a single zero-anonymity value from the datastore.
1356 * @param h handle to the datastore
1357 * @param offset offset of the result (mod #num-results); set to
1358 * a random 64-bit value initially; then increment by
1359 * one each time; detect that all results have been found by uid
1360 * being again the first uid ever returned.
1361 * @param queue_priority ranking of this request in the priority queue
1362 * @param max_queue_size at what queue size should this request be dropped
1363 * (if other requests of higher priority are in the queue)
1364 * @param timeout how long to wait at most for a response
1365 * @param type allowed type for the operation (never zero)
1366 * @param proc function to call on a random value; it
1367 * will be called once with a value (if available)
1368 * or with NULL if none value exists.
1369 * @param proc_cls closure for proc
1370 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1373 struct GNUNET_DATASTORE_QueueEntry *
1374 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
1376 unsigned int queue_priority,
1377 unsigned int max_queue_size,
1378 struct GNUNET_TIME_Relative timeout,
1379 enum GNUNET_BLOCK_Type type,
1380 GNUNET_DATASTORE_DatumProcessor proc,
1383 struct GNUNET_DATASTORE_QueueEntry *qe;
1384 struct GetZeroAnonymityMessage *m;
1385 union QueueContext qc;
1387 GNUNET_assert (NULL != proc);
1388 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
1390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1391 "Asked to get %llu-th zero-anonymity entry of type %d in %llu ms\n",
1392 (unsigned long long) offset,
1394 (unsigned long long) timeout.rel_value);
1397 qc.rc.proc_cls = proc_cls;
1398 qe = make_queue_entry (h, sizeof(struct GetZeroAnonymityMessage),
1399 queue_priority, max_queue_size, timeout,
1400 &process_result_message, &qc);
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1405 "Could not create queue entry for zero-anonymity procation\n");
1409 GNUNET_STATISTICS_update (h->stats,
1410 gettext_noop ("# GET ZERO ANONYMITY requests executed"),
1413 m = (struct GetZeroAnonymityMessage*) &qe[1];
1414 m->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY);
1415 m->header.size = htons(sizeof (struct GetZeroAnonymityMessage));
1416 m->type = htonl ((uint32_t) type);
1417 m->offset = GNUNET_htonll (offset);
1424 * Get a result for a particular key from the datastore. The processor
1425 * will only be called once.
1427 * @param h handle to the datastore
1428 * @param offset offset of the result (mod #num-results); set to
1429 * a random 64-bit value initially; then increment by
1430 * one each time; detect that all results have been found by uid
1431 * being again the first uid ever returned.
1432 * @param key maybe NULL (to match all entries)
1433 * @param type desired type, 0 for any
1434 * @param queue_priority ranking of this request in the priority queue
1435 * @param max_queue_size at what queue size should this request be dropped
1436 * (if other requests of higher priority are in the queue)
1437 * @param timeout how long to wait at most for a response
1438 * @param proc function to call on each matching value;
1439 * will be called once with a NULL value at the end
1440 * @param proc_cls closure for proc
1441 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1444 struct GNUNET_DATASTORE_QueueEntry *
1445 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h,
1447 const GNUNET_HashCode * key,
1448 enum GNUNET_BLOCK_Type type,
1449 unsigned int queue_priority,
1450 unsigned int max_queue_size,
1451 struct GNUNET_TIME_Relative timeout,
1452 GNUNET_DATASTORE_DatumProcessor proc,
1455 struct GNUNET_DATASTORE_QueueEntry *qe;
1456 struct GetMessage *gm;
1457 union QueueContext qc;
1459 GNUNET_assert (NULL != proc);
1461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1462 "Asked to look for data of type %u under key `%s'\n",
1463 (unsigned int) type,
1467 qc.rc.proc_cls = proc_cls;
1468 qe = make_queue_entry (h, sizeof(struct GetMessage),
1469 queue_priority, max_queue_size, timeout,
1470 &process_result_message, &qc);
1474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1475 "Could not queue request for `%s'\n",
1480 GNUNET_STATISTICS_update (h->stats,
1481 gettext_noop ("# GET requests executed"),
1484 gm = (struct GetMessage*) &qe[1];
1485 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1486 gm->type = htonl(type);
1487 gm->offset = GNUNET_htonll (offset);
1490 gm->header.size = htons(sizeof (struct GetMessage));
1495 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
1503 * Cancel a datastore operation. The final callback from the
1504 * operation must not have been done yet.
1506 * @param qe operation to cancel
1509 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1511 struct GNUNET_DATASTORE_Handle *h;
1513 GNUNET_assert (GNUNET_SYSERR != qe->was_transmitted);
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1517 "Pending DATASTORE request %p cancelled (%d, %d)\n",
1519 qe->was_transmitted,
1520 h->queue_head == qe);
1522 if (GNUNET_YES == qe->was_transmitted)
1524 free_queue_entry (qe);
1525 h->skip_next_messages++;
1528 free_queue_entry (qe);
1533 /* end of datastore_api.c */