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'\n",
861 msize = sizeof(struct DataMessage) + size;
862 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
864 qc.sc.cont_cls = cont_cls;
865 qe = make_queue_entry (h, msize,
866 queue_priority, max_queue_size, timeout,
867 &process_status_message, &qc);
871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
872 "Could not create queue entry for PUT\n");
876 GNUNET_STATISTICS_update (h->stats,
877 gettext_noop ("# PUT requests executed"),
880 dm = (struct DataMessage* ) &qe[1];
881 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
882 dm->header.size = htons(msize);
883 dm->rid = htonl(rid);
884 dm->size = htonl( (uint32_t) size);
885 dm->type = htonl(type);
886 dm->priority = htonl(priority);
887 dm->anonymity = htonl(anonymity);
888 dm->uid = GNUNET_htonll(0);
889 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
891 memcpy (&dm[1], data, size);
898 * Reserve space in the datastore. This function should be used
899 * to avoid "out of space" failures during a longer sequence of "put"
900 * operations (for example, when a file is being inserted).
902 * @param h handle to the datastore
903 * @param amount how much space (in bytes) should be reserved (for content only)
904 * @param entries how many entries will be created (to calculate per-entry overhead)
905 * @param queue_priority ranking of this request in the priority queue
906 * @param max_queue_size at what queue size should this request be dropped
907 * (if other requests of higher priority are in the queue)
908 * @param timeout how long to wait at most for a response (or before dying in queue)
909 * @param cont continuation to call when done; "success" will be set to
910 * a positive reservation value if space could be reserved.
911 * @param cont_cls closure for cont
912 * @return NULL if the entry was not queued, otherwise a handle that can be used to
913 * cancel; note that even if NULL is returned, the callback will be invoked
914 * (or rather, will already have been invoked)
916 struct GNUNET_DATASTORE_QueueEntry *
917 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
920 unsigned int queue_priority,
921 unsigned int max_queue_size,
922 struct GNUNET_TIME_Relative timeout,
923 GNUNET_DATASTORE_ContinuationWithStatus cont,
926 struct GNUNET_DATASTORE_QueueEntry *qe;
927 struct ReserveMessage *rm;
928 union QueueContext qc;
931 cont = &drop_status_cont;
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
934 "Asked to reserve %llu bytes of data and %u entries'\n",
935 (unsigned long long) amount,
936 (unsigned int) entries);
939 qc.sc.cont_cls = cont_cls;
940 qe = make_queue_entry (h, sizeof(struct ReserveMessage),
941 queue_priority, max_queue_size, timeout,
942 &process_status_message, &qc);
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
947 "Could not create queue entry to reserve\n");
951 GNUNET_STATISTICS_update (h->stats,
952 gettext_noop ("# RESERVE requests executed"),
955 rm = (struct ReserveMessage*) &qe[1];
956 rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
957 rm->header.size = htons(sizeof (struct ReserveMessage));
958 rm->entries = htonl(entries);
959 rm->amount = GNUNET_htonll(amount);
966 * Signal that all of the data for which a reservation was made has
967 * been stored and that whatever excess space might have been reserved
968 * can now be released.
970 * @param h handle to the datastore
971 * @param rid reservation ID (value of "success" in original continuation
972 * from the "reserve" function).
973 * @param queue_priority ranking of this request in the priority queue
974 * @param max_queue_size at what queue size should this request be dropped
975 * (if other requests of higher priority are in the queue)
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 timeout how long to wait at most for a response
980 * @param cont continuation to call when done
981 * @param cont_cls closure for cont
982 * @return NULL if the entry was not queued, otherwise a handle that can be used to
983 * cancel; note that even if NULL is returned, the callback will be invoked
984 * (or rather, will already have been invoked)
986 struct GNUNET_DATASTORE_QueueEntry *
987 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
989 unsigned int queue_priority,
990 unsigned int max_queue_size,
991 struct GNUNET_TIME_Relative timeout,
992 GNUNET_DATASTORE_ContinuationWithStatus cont,
995 struct GNUNET_DATASTORE_QueueEntry *qe;
996 struct ReleaseReserveMessage *rrm;
997 union QueueContext qc;
1000 cont = &drop_status_cont;
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1003 "Asked to release reserve %d\n",
1007 qc.sc.cont_cls = cont_cls;
1008 qe = make_queue_entry (h, sizeof(struct ReleaseReserveMessage),
1009 queue_priority, max_queue_size, timeout,
1010 &process_status_message, &qc);
1014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1015 "Could not create queue entry to release reserve\n");
1019 GNUNET_STATISTICS_update (h->stats,
1020 gettext_noop ("# RELEASE RESERVE requests executed"),
1023 rrm = (struct ReleaseReserveMessage*) &qe[1];
1024 rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
1025 rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
1026 rrm->rid = htonl(rid);
1033 * Update a value in the datastore.
1035 * @param h handle to the datastore
1036 * @param uid identifier for the value
1037 * @param priority how much to increase the priority of the value
1038 * @param expiration new expiration value should be MAX of existing and this argument
1039 * @param queue_priority ranking of this request in the priority queue
1040 * @param max_queue_size at what queue size should this request be dropped
1041 * (if other requests of higher priority are in the queue)
1042 * @param timeout how long to wait at most for a response
1043 * @param cont continuation to call when done
1044 * @param cont_cls closure for cont
1045 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1046 * cancel; note that even if NULL is returned, the callback will be invoked
1047 * (or rather, will already have been invoked)
1049 struct GNUNET_DATASTORE_QueueEntry *
1050 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
1053 struct GNUNET_TIME_Absolute expiration,
1054 unsigned int queue_priority,
1055 unsigned int max_queue_size,
1056 struct GNUNET_TIME_Relative timeout,
1057 GNUNET_DATASTORE_ContinuationWithStatus cont,
1060 struct GNUNET_DATASTORE_QueueEntry *qe;
1061 struct UpdateMessage *um;
1062 union QueueContext qc;
1065 cont = &drop_status_cont;
1067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1068 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
1070 (unsigned int) priority,
1071 (unsigned long long) expiration.abs_value);
1074 qc.sc.cont_cls = cont_cls;
1075 qe = make_queue_entry (h, sizeof(struct UpdateMessage),
1076 queue_priority, max_queue_size, timeout,
1077 &process_status_message, &qc);
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1082 "Could not create queue entry for UPDATE\n");
1086 GNUNET_STATISTICS_update (h->stats,
1087 gettext_noop ("# UPDATE requests executed"),
1090 um = (struct UpdateMessage*) &qe[1];
1091 um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
1092 um->header.size = htons(sizeof (struct UpdateMessage));
1093 um->priority = htonl(priority);
1094 um->expiration = GNUNET_TIME_absolute_hton(expiration);
1095 um->uid = GNUNET_htonll(uid);
1102 * Explicitly remove some content from the database.
1103 * The "cont"inuation will be called with status
1104 * "GNUNET_OK" if content was removed, "GNUNET_NO"
1105 * if no matching entry was found and "GNUNET_SYSERR"
1106 * on all other types of errors.
1108 * @param h handle to the datastore
1109 * @param key key for the value
1110 * @param size number of bytes in data
1111 * @param data content stored
1112 * @param queue_priority ranking of this request in the priority queue
1113 * @param max_queue_size at what queue size should this request be dropped
1114 * (if other requests of higher priority are in the queue)
1115 * @param timeout how long to wait at most for a response
1116 * @param cont continuation to call when done
1117 * @param cont_cls closure for cont
1118 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1119 * cancel; note that even if NULL is returned, the callback will be invoked
1120 * (or rather, will already have been invoked)
1122 struct GNUNET_DATASTORE_QueueEntry *
1123 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
1124 const GNUNET_HashCode *key,
1127 unsigned int queue_priority,
1128 unsigned int max_queue_size,
1129 struct GNUNET_TIME_Relative timeout,
1130 GNUNET_DATASTORE_ContinuationWithStatus cont,
1133 struct GNUNET_DATASTORE_QueueEntry *qe;
1134 struct DataMessage *dm;
1136 union QueueContext qc;
1139 cont = &drop_status_cont;
1141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1142 "Asked to remove %u bytes under key `%s'\n",
1147 qc.sc.cont_cls = cont_cls;
1148 msize = sizeof(struct DataMessage) + size;
1149 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1150 qe = make_queue_entry (h, msize,
1151 queue_priority, max_queue_size, timeout,
1152 &process_status_message, &qc);
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1157 "Could not create queue entry for REMOVE\n");
1161 GNUNET_STATISTICS_update (h->stats,
1162 gettext_noop ("# REMOVE requests executed"),
1165 dm = (struct DataMessage*) &qe[1];
1166 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
1167 dm->header.size = htons(msize);
1169 dm->size = htonl(size);
1170 dm->type = htonl(0);
1171 dm->priority = htonl(0);
1172 dm->anonymity = htonl(0);
1173 dm->uid = GNUNET_htonll(0);
1174 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
1176 memcpy (&dm[1], data, size);
1183 * Type of a function to call when we receive a message
1186 * @param cls closure
1187 * @param msg message received, NULL on timeout or fatal error
1190 process_result_message (void *cls,
1191 const struct GNUNET_MessageHeader *msg)
1193 struct GNUNET_DATASTORE_Handle *h = cls;
1194 struct GNUNET_DATASTORE_QueueEntry *qe;
1195 struct ResultContext rc;
1196 const struct DataMessage *dm;
1198 h->in_receive = GNUNET_NO;
1199 if (h->skip_next_messages > 0)
1201 h->skip_next_messages--;
1208 GNUNET_assert (NULL != qe);
1209 if (qe->was_transmitted == GNUNET_YES)
1212 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1213 _("Failed to receive response from database.\n"));
1216 free_queue_entry (qe);
1217 if (rc.proc != NULL)
1218 rc.proc (rc.proc_cls,
1219 NULL, 0, NULL, 0, 0, 0,
1220 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1223 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1225 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
1228 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1229 free_queue_entry (qe);
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1232 "Received end of result set, new queue size is %u\n",
1235 if (rc.proc != NULL)
1236 rc.proc (rc.proc_cls,
1237 NULL, 0, NULL, 0, 0, 0,
1238 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1239 h->retry_time.rel_value = 0;
1240 h->result_count = 0;
1246 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1247 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
1248 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1249 (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
1252 free_queue_entry (qe);
1253 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1255 if (rc.proc != NULL)
1256 rc.proc (rc.proc_cls,
1257 NULL, 0, NULL, 0, 0, 0,
1258 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1261 GNUNET_STATISTICS_update (h->stats,
1262 gettext_noop ("# Results received"),
1265 dm = (const struct DataMessage*) msg;
1267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1268 "Received result %llu with type %u and size %u with key %s\n",
1269 (unsigned long long) GNUNET_ntohll(dm->uid),
1272 GNUNET_h2s(&dm->key));
1274 free_queue_entry (qe);
1275 h->retry_time.rel_value = 0;
1276 rc.proc (rc.proc_cls,
1281 ntohl(dm->priority),
1282 ntohl(dm->anonymity),
1283 GNUNET_TIME_absolute_ntoh(dm->expiration),
1284 GNUNET_ntohll(dm->uid));
1289 * Get a random value from the datastore for content replication.
1290 * Returns a single, random value among those with the highest
1291 * replication score, lowering positive replication scores by one for
1292 * the chosen value (if only content with a replication score exists,
1293 * a random value is returned and replication scores are not changed).
1295 * @param h handle to the datastore
1296 * @param queue_priority ranking of this request in the priority queue
1297 * @param max_queue_size at what queue size should this request be dropped
1298 * (if other requests of higher priority are in the queue)
1299 * @param timeout how long to wait at most for a response
1300 * @param proc function to call on a random value; it
1301 * will be called once with a value (if available)
1302 * and always once with a value of NULL.
1303 * @param proc_cls closure for proc
1304 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1307 struct GNUNET_DATASTORE_QueueEntry *
1308 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
1309 unsigned int queue_priority,
1310 unsigned int max_queue_size,
1311 struct GNUNET_TIME_Relative timeout,
1312 GNUNET_DATASTORE_DatumProcessor proc,
1315 struct GNUNET_DATASTORE_QueueEntry *qe;
1316 struct GNUNET_MessageHeader *m;
1317 union QueueContext qc;
1319 GNUNET_assert (NULL != proc);
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322 "Asked to get replication entry in %llu ms\n",
1323 (unsigned long long) timeout.rel_value);
1326 qc.rc.proc_cls = proc_cls;
1327 qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader),
1328 queue_priority, max_queue_size, timeout,
1329 &process_result_message, &qc);
1333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1334 "Could not create queue entry for GET REPLICATION\n");
1338 GNUNET_STATISTICS_update (h->stats,
1339 gettext_noop ("# GET REPLICATION requests executed"),
1342 m = (struct GNUNET_MessageHeader*) &qe[1];
1343 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION);
1344 m->size = htons(sizeof (struct GNUNET_MessageHeader));
1351 * Get a single zero-anonymity value from the datastore.
1353 * @param h handle to the datastore
1354 * @param offset offset of the result (mod #num-results); set to
1355 * a random 64-bit value initially; then increment by
1356 * one each time; detect that all results have been found by uid
1357 * being again the first uid ever returned.
1358 * @param queue_priority ranking of this request in the priority queue
1359 * @param max_queue_size at what queue size should this request be dropped
1360 * (if other requests of higher priority are in the queue)
1361 * @param timeout how long to wait at most for a response
1362 * @param type allowed type for the operation (never zero)
1363 * @param proc function to call on a random value; it
1364 * will be called once with a value (if available)
1365 * or with NULL if none value exists.
1366 * @param proc_cls closure for proc
1367 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1370 struct GNUNET_DATASTORE_QueueEntry *
1371 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
1373 unsigned int queue_priority,
1374 unsigned int max_queue_size,
1375 struct GNUNET_TIME_Relative timeout,
1376 enum GNUNET_BLOCK_Type type,
1377 GNUNET_DATASTORE_DatumProcessor proc,
1380 struct GNUNET_DATASTORE_QueueEntry *qe;
1381 struct GetZeroAnonymityMessage *m;
1382 union QueueContext qc;
1384 GNUNET_assert (NULL != proc);
1385 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 "Asked to get %llu-th zero-anonymity entry of type %d in %llu ms\n",
1389 (unsigned long long) offset,
1391 (unsigned long long) timeout.rel_value);
1394 qc.rc.proc_cls = proc_cls;
1395 qe = make_queue_entry (h, sizeof(struct GetZeroAnonymityMessage),
1396 queue_priority, max_queue_size, timeout,
1397 &process_result_message, &qc);
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 "Could not create queue entry for zero-anonymity procation\n");
1406 GNUNET_STATISTICS_update (h->stats,
1407 gettext_noop ("# GET ZERO ANONYMITY requests executed"),
1410 m = (struct GetZeroAnonymityMessage*) &qe[1];
1411 m->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY);
1412 m->header.size = htons(sizeof (struct GetZeroAnonymityMessage));
1413 m->type = htonl ((uint32_t) type);
1414 m->offset = GNUNET_htonll (offset);
1421 * Get a result for a particular key from the datastore. The processor
1422 * will only be called once.
1424 * @param h handle to the datastore
1425 * @param offset offset of the result (mod #num-results); set to
1426 * a random 64-bit value initially; then increment by
1427 * one each time; detect that all results have been found by uid
1428 * being again the first uid ever returned.
1429 * @param key maybe NULL (to match all entries)
1430 * @param type desired type, 0 for any
1431 * @param queue_priority ranking of this request in the priority queue
1432 * @param max_queue_size at what queue size should this request be dropped
1433 * (if other requests of higher priority are in the queue)
1434 * @param timeout how long to wait at most for a response
1435 * @param proc function to call on each matching value;
1436 * will be called once with a NULL value at the end
1437 * @param proc_cls closure for proc
1438 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1441 struct GNUNET_DATASTORE_QueueEntry *
1442 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h,
1444 const GNUNET_HashCode * key,
1445 enum GNUNET_BLOCK_Type type,
1446 unsigned int queue_priority,
1447 unsigned int max_queue_size,
1448 struct GNUNET_TIME_Relative timeout,
1449 GNUNET_DATASTORE_DatumProcessor proc,
1452 struct GNUNET_DATASTORE_QueueEntry *qe;
1453 struct GetMessage *gm;
1454 union QueueContext qc;
1456 GNUNET_assert (NULL != proc);
1458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1459 "Asked to look for data of type %u under key `%s'\n",
1460 (unsigned int) type,
1464 qc.rc.proc_cls = proc_cls;
1465 qe = make_queue_entry (h, sizeof(struct GetMessage),
1466 queue_priority, max_queue_size, timeout,
1467 &process_result_message, &qc);
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1472 "Could not queue request for `%s'\n",
1477 GNUNET_STATISTICS_update (h->stats,
1478 gettext_noop ("# GET requests executed"),
1481 gm = (struct GetMessage*) &qe[1];
1482 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1483 gm->type = htonl(type);
1484 gm->offset = GNUNET_htonll (offset);
1487 gm->header.size = htons(sizeof (struct GetMessage));
1492 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
1500 * Cancel a datastore operation. The final callback from the
1501 * operation must not have been done yet.
1503 * @param qe operation to cancel
1506 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1508 struct GNUNET_DATASTORE_Handle *h;
1510 GNUNET_assert (GNUNET_SYSERR != qe->was_transmitted);
1513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1514 "Pending DATASTORE request %p cancelled (%d, %d)\n",
1516 qe->was_transmitted,
1517 h->queue_head == qe);
1519 if (GNUNET_YES == qe->was_transmitted)
1521 free_queue_entry (qe);
1522 h->skip_next_messages++;
1525 free_queue_entry (qe);
1530 /* end of datastore_api.c */