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"
34 #define LOG(kind,...) GNUNET_log_from (kind, "datastore-api",__VA_ARGS__)
37 * Collect an instane number of statistics? May cause excessive IPC.
39 #define INSANE_STATISTICS GNUNET_NO
42 * If a client stopped asking for more results, how many more do
43 * we receive from the DB before killing the connection? Trade-off
44 * between re-doing TCP handshakes and (needlessly) receiving
47 #define MAX_EXCESS_RESULTS 8
50 * Context for processing status messages.
55 * Continuation to call with the status.
57 GNUNET_DATASTORE_ContinuationWithStatus cont;
68 * Context for processing result messages.
73 * Function to call with the result.
75 GNUNET_DATASTORE_DatumProcessor proc;
86 * Context for a queue operation.
91 struct StatusContext sc;
93 struct ResultContext rc;
100 * Entry in our priority queue.
102 struct GNUNET_DATASTORE_QueueEntry
106 * This is a linked list.
108 struct GNUNET_DATASTORE_QueueEntry *next;
111 * This is a linked list.
113 struct GNUNET_DATASTORE_QueueEntry *prev;
116 * Handle to the master context.
118 struct GNUNET_DATASTORE_Handle *h;
121 * Response processor (NULL if we are not waiting for a response).
122 * This struct should be used for the closure, function-specific
123 * arguments can be passed via 'qc'.
125 GNUNET_CLIENT_MessageHandler response_proc;
128 * Function to call after transmission of the request.
130 GNUNET_DATASTORE_ContinuationWithStatus cont;
133 * Closure for 'cont'.
138 * Context for the operation.
140 union QueueContext qc;
143 * Task for timeout signalling.
145 GNUNET_SCHEDULER_TaskIdentifier task;
148 * Timeout for the current operation.
150 struct GNUNET_TIME_Absolute timeout;
153 * Priority in the queue.
155 unsigned int priority;
158 * Maximum allowed length of queue (otherwise
159 * this request should be discarded).
161 unsigned int max_queue;
164 * Number of bytes in the request message following
165 * this struct. 32-bit value for nicer memory
166 * access (and overall struct alignment).
168 uint32_t message_size;
171 * Has this message been transmitted to the service?
172 * Only ever GNUNET_YES for the head of the queue.
173 * Note that the overall struct should end at a
174 * multiple of 64 bits.
181 * Handle to the datastore service.
183 struct GNUNET_DATASTORE_Handle
189 const struct GNUNET_CONFIGURATION_Handle *cfg;
192 * Current connection to the datastore service.
194 struct GNUNET_CLIENT_Connection *client;
197 * Handle for statistics.
199 struct GNUNET_STATISTICS_Handle *stats;
202 * Current transmit handle.
204 struct GNUNET_CLIENT_TransmitHandle *th;
207 * Current head of priority queue.
209 struct GNUNET_DATASTORE_QueueEntry *queue_head;
212 * Current tail of priority queue.
214 struct GNUNET_DATASTORE_QueueEntry *queue_tail;
217 * Task for trying to reconnect.
219 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
222 * How quickly should we retry? Used for exponential back-off on
225 struct GNUNET_TIME_Relative retry_time;
228 * Number of entries in the queue.
230 unsigned int queue_size;
233 * Number of results we're receiving for the current query
234 * after application stopped to care. Used to determine when
235 * to reset the connection.
237 unsigned int result_count;
240 * Are we currently trying to receive from the service?
245 * We should ignore the next message(s) from the service.
247 unsigned int skip_next_messages;
254 * Connect to the datastore service.
256 * @param cfg configuration to use
257 * @return handle to use to access the service
259 struct GNUNET_DATASTORE_Handle *
260 GNUNET_DATASTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
262 struct GNUNET_CLIENT_Connection *c;
263 struct GNUNET_DATASTORE_Handle *h;
265 c = GNUNET_CLIENT_connect ("datastore", cfg);
267 return NULL; /* oops */
268 h = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_Handle) +
269 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1);
272 h->stats = GNUNET_STATISTICS_create ("datastore-api", cfg);
278 * Task used by 'transmit_drop' to disconnect the datastore.
280 * @param cls the datastore handle
281 * @param tc scheduler context
284 disconnect_after_drop (void *cls,
285 const struct GNUNET_SCHEDULER_TaskContext *tc)
287 struct GNUNET_DATASTORE_Handle *h = cls;
289 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
294 * Transmit DROP message to datastore service.
296 * @param cls the 'struct GNUNET_DATASTORE_Handle'
297 * @param size number of bytes that can be copied to buf
298 * @param buf where to copy the drop message
299 * @return number of bytes written to buf
302 transmit_drop (void *cls, size_t size, void *buf)
304 struct GNUNET_DATASTORE_Handle *h = cls;
305 struct GNUNET_MessageHeader *hdr;
309 LOG (GNUNET_ERROR_TYPE_WARNING,
310 _("Failed to transmit request to drop database.\n"));
311 GNUNET_SCHEDULER_add_continuation (&disconnect_after_drop, h,
312 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
315 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
317 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
318 hdr->type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
319 GNUNET_SCHEDULER_add_continuation (&disconnect_after_drop, h,
320 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
321 return sizeof (struct GNUNET_MessageHeader);
326 * Disconnect from the datastore service (and free
327 * associated resources).
329 * @param h handle to the datastore
330 * @param drop set to GNUNET_YES to delete all data in datastore (!)
333 GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h, int drop)
335 struct GNUNET_DATASTORE_QueueEntry *qe;
337 LOG (GNUNET_ERROR_TYPE_DEBUG, "Datastore disconnect\n");
340 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
343 if (h->client != NULL)
345 GNUNET_CLIENT_disconnect (h->client);
348 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
350 GNUNET_SCHEDULER_cancel (h->reconnect_task);
351 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
353 while (NULL != (qe = h->queue_head))
355 GNUNET_assert (NULL != qe->response_proc);
356 qe->response_proc (h, NULL);
358 if (GNUNET_YES == drop)
360 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
361 if (h->client != NULL)
364 GNUNET_CLIENT_notify_transmit_ready (h->client,
366 GNUNET_MessageHeader),
367 GNUNET_TIME_UNIT_MINUTES,
368 GNUNET_YES, &transmit_drop, h))
370 GNUNET_CLIENT_disconnect (h->client);
375 GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
382 * A request has timed out (before being transmitted to the service).
384 * @param cls the 'struct GNUNET_DATASTORE_QueueEntry'
385 * @param tc scheduler context
388 timeout_queue_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
390 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
392 GNUNET_STATISTICS_update (qe->h->stats,
393 gettext_noop ("# queue entry timeouts"), 1,
395 qe->task = GNUNET_SCHEDULER_NO_TASK;
396 GNUNET_assert (GNUNET_NO == qe->was_transmitted);
397 LOG (GNUNET_ERROR_TYPE_DEBUG, "Timeout of request in datastore queue\n");
398 qe->response_proc (qe->h, NULL);
403 * Create a new entry for our priority queue (and possibly discard other entires if
404 * the queue is getting too long).
406 * @param h handle to the datastore
407 * @param msize size of the message to queue
408 * @param queue_priority priority of the entry
409 * @param max_queue_size at what queue size should this request be dropped
410 * (if other requests of higher priority are in the queue)
411 * @param timeout timeout for the operation
412 * @param response_proc function to call with replies (can be NULL)
413 * @param qc client context (NOT a closure for response_proc)
414 * @return NULL if the queue is full
416 static struct GNUNET_DATASTORE_QueueEntry *
417 make_queue_entry (struct GNUNET_DATASTORE_Handle *h, size_t msize,
418 unsigned int queue_priority, unsigned int max_queue_size,
419 struct GNUNET_TIME_Relative timeout,
420 GNUNET_CLIENT_MessageHandler response_proc,
421 const union QueueContext *qc)
423 struct GNUNET_DATASTORE_QueueEntry *ret;
424 struct GNUNET_DATASTORE_QueueEntry *pos;
429 while ((pos != NULL) && (c < max_queue_size) &&
430 (pos->priority >= queue_priority))
435 if (c >= max_queue_size)
437 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# queue overflows"), 1,
441 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
443 ret->response_proc = response_proc;
445 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
446 ret->priority = queue_priority;
447 ret->max_queue = max_queue_size;
448 ret->message_size = msize;
449 ret->was_transmitted = GNUNET_NO;
452 /* append at the tail */
458 /* do not insert at HEAD if HEAD query was already
459 * transmitted and we are still receiving replies! */
460 if ((pos == NULL) && (h->queue_head->was_transmitted))
464 #if INSANE_STATISTICS
465 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# queue entries created"),
468 GNUNET_CONTAINER_DLL_insert_after (h->queue_head, h->queue_tail, pos, ret);
470 ret->task = GNUNET_SCHEDULER_add_delayed (timeout, &timeout_queue_entry, ret);
471 for (pos = ret->next; NULL != pos; pos = pos->next)
473 if ((pos->max_queue < h->queue_size) && (pos->was_transmitted == GNUNET_NO))
475 GNUNET_assert (pos->response_proc != NULL);
476 /* move 'pos' element to head so that it will be
477 * killed on 'NULL' call below */
478 LOG (GNUNET_ERROR_TYPE_DEBUG, "Dropping request from datastore queue\n");
479 GNUNET_CONTAINER_DLL_remove (h->queue_head, h->queue_tail, pos);
480 GNUNET_CONTAINER_DLL_insert (h->queue_head, h->queue_tail, pos);
481 GNUNET_STATISTICS_update (h->stats,
483 ("# Requests dropped from datastore queue"), 1,
485 GNUNET_assert (h->queue_head == pos);
486 pos->response_proc (h, NULL);
495 * Process entries in the queue (or do nothing if we are already
498 * @param h handle to the datastore
501 process_queue (struct GNUNET_DATASTORE_Handle *h);
505 * Try reconnecting to the datastore service.
507 * @param cls the 'struct GNUNET_DATASTORE_Handle'
508 * @param tc scheduler context
511 try_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
513 struct GNUNET_DATASTORE_Handle *h = cls;
515 h->retry_time = GNUNET_TIME_STD_BACKOFF (h->retry_time);
516 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
517 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
518 if (h->client == NULL)
520 LOG (GNUNET_ERROR_TYPE_ERROR, "DATASTORE reconnect failed (fatally)\n");
523 GNUNET_STATISTICS_update (h->stats,
525 ("# datastore connections (re)created"), 1,
527 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnected to DATASTORE\n");
533 * Disconnect from the service and then try reconnecting to the datastore service
536 * @param h handle to datastore to disconnect and reconnect
539 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
541 if (h->client == NULL)
543 LOG (GNUNET_ERROR_TYPE_DEBUG,
544 "client NULL in disconnect, will not try to reconnect\n");
547 GNUNET_CLIENT_disconnect (h->client);
548 h->skip_next_messages = 0;
551 GNUNET_SCHEDULER_add_delayed (h->retry_time, &try_reconnect, h);
556 * Function called whenever we receive a message from
557 * the service. Calls the appropriate handler.
559 * @param cls the 'struct GNUNET_DATASTORE_Handle'
560 * @param msg the received message
563 receive_cb (void *cls, const struct GNUNET_MessageHeader *msg)
565 struct GNUNET_DATASTORE_Handle *h = cls;
566 struct GNUNET_DATASTORE_QueueEntry *qe;
568 h->in_receive = GNUNET_NO;
569 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving reply from datastore\n");
570 if (h->skip_next_messages > 0)
572 h->skip_next_messages--;
576 if (NULL == (qe = h->queue_head))
582 qe->response_proc (h, msg);
587 * Transmit request from queue to datastore service.
589 * @param cls the 'struct GNUNET_DATASTORE_Handle'
590 * @param size number of bytes that can be copied to buf
591 * @param buf where to copy the drop message
592 * @return number of bytes written to buf
595 transmit_request (void *cls, size_t size, void *buf)
597 struct GNUNET_DATASTORE_Handle *h = cls;
598 struct GNUNET_DATASTORE_QueueEntry *qe;
602 if (NULL == (qe = h->queue_head))
603 return 0; /* no entry in queue */
606 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to transmit request to DATASTORE.\n");
607 GNUNET_STATISTICS_update (h->stats,
608 gettext_noop ("# transmission request failures"),
613 if (size < (msize = qe->message_size))
618 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u byte request to DATASTORE\n",
620 memcpy (buf, &qe[1], msize);
621 qe->was_transmitted = GNUNET_YES;
622 GNUNET_SCHEDULER_cancel (qe->task);
623 qe->task = GNUNET_SCHEDULER_NO_TASK;
624 GNUNET_assert (GNUNET_NO == h->in_receive);
625 h->in_receive = GNUNET_YES;
626 GNUNET_CLIENT_receive (h->client, &receive_cb, h,
627 GNUNET_TIME_absolute_get_remaining (qe->timeout));
628 #if INSANE_STATISTICS
629 GNUNET_STATISTICS_update (h->stats,
630 gettext_noop ("# bytes sent to datastore"), 1,
638 * Process entries in the queue (or do nothing if we are already
641 * @param h handle to the datastore
644 process_queue (struct GNUNET_DATASTORE_Handle *h)
646 struct GNUNET_DATASTORE_QueueEntry *qe;
648 if (NULL == (qe = h->queue_head))
650 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue empty\n");
651 return; /* no entry in queue */
653 if (qe->was_transmitted == GNUNET_YES)
655 LOG (GNUNET_ERROR_TYPE_DEBUG, "Head request already transmitted\n");
656 return; /* waiting for replies */
660 LOG (GNUNET_ERROR_TYPE_DEBUG, "Pending transmission request\n");
661 return; /* request pending */
663 if (h->client == NULL)
665 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not connected\n");
666 return; /* waiting for reconnect */
668 if (GNUNET_YES == h->in_receive)
670 /* wait for response to previous query */
673 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queueing %u byte request to DATASTORE\n",
676 GNUNET_CLIENT_notify_transmit_ready (h->client, qe->message_size,
677 GNUNET_TIME_absolute_get_remaining
678 (qe->timeout), GNUNET_YES,
679 &transmit_request, h);
680 GNUNET_assert (GNUNET_NO == h->in_receive);
681 GNUNET_break (NULL != h->th);
686 * Dummy continuation used to do nothing (but be non-zero).
689 * @param result result
690 * @param min_expiration expiration time
691 * @param emsg error message
694 drop_status_cont (void *cls, int32_t result,
695 struct GNUNET_TIME_Absolute min_expiration,
703 * Free a queue entry. Removes the given entry from the
704 * queue and releases associated resources. Does NOT
707 * @param qe entry to free.
710 free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
712 struct GNUNET_DATASTORE_Handle *h = qe->h;
714 GNUNET_CONTAINER_DLL_remove (h->queue_head, h->queue_tail, qe);
715 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
717 GNUNET_SCHEDULER_cancel (qe->task);
718 qe->task = GNUNET_SCHEDULER_NO_TASK;
721 qe->was_transmitted = GNUNET_SYSERR; /* use-after-free warning */
727 * Type of a function to call when we receive a message
731 * @param msg message received, NULL on timeout or fatal error
734 process_status_message (void *cls, const struct GNUNET_MessageHeader *msg)
736 struct GNUNET_DATASTORE_Handle *h = cls;
737 struct GNUNET_DATASTORE_QueueEntry *qe;
738 struct StatusContext rc;
739 const struct StatusMessage *sm;
744 if (NULL == (qe = h->queue_head))
753 was_transmitted = qe->was_transmitted;
754 free_queue_entry (qe);
755 if (was_transmitted == GNUNET_YES)
760 rc.cont (rc.cont_cls, GNUNET_SYSERR,
761 GNUNET_TIME_UNIT_ZERO_ABS,
762 _("Failed to receive status response from database."));
765 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
766 free_queue_entry (qe);
767 if ((ntohs (msg->size) < sizeof (struct StatusMessage)) ||
768 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS))
771 h->retry_time = GNUNET_TIME_UNIT_ZERO;
774 rc.cont (rc.cont_cls, GNUNET_SYSERR,
775 GNUNET_TIME_UNIT_ZERO_ABS,
776 _("Error reading response from datastore service"));
779 sm = (const struct StatusMessage *) msg;
780 status = ntohl (sm->status);
782 if (ntohs (msg->size) > sizeof (struct StatusMessage))
784 emsg = (const char *) &sm[1];
785 if (emsg[ntohs (msg->size) - sizeof (struct StatusMessage) - 1] != '\0')
788 emsg = _("Invalid error message received from datastore service");
791 if ((status == GNUNET_SYSERR) && (emsg == NULL))
794 emsg = _("Invalid error message received from datastore service");
796 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received status %d/%s\n", (int) status, emsg);
797 GNUNET_STATISTICS_update (h->stats,
798 gettext_noop ("# status messages received"), 1,
800 h->retry_time = GNUNET_TIME_UNIT_ZERO;
803 rc.cont (rc.cont_cls, status,
804 GNUNET_TIME_absolute_ntoh (sm->min_expiration),
810 * Store an item in the datastore. If the item is already present,
811 * the priorities are summed up and the higher expiration time and
812 * lower anonymity level is used.
814 * @param h handle to the datastore
815 * @param rid reservation ID to use (from "reserve"); use 0 if no
816 * prior reservation was made
817 * @param key key for the value
818 * @param size number of bytes in data
819 * @param data content stored
820 * @param type type of the content
821 * @param priority priority of the content
822 * @param anonymity anonymity-level for the content
823 * @param replication how often should the content be replicated to other peers?
824 * @param expiration expiration time for the content
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 timeout for the operation
829 * @param cont continuation to call when done
830 * @param cont_cls closure for cont
831 * @return NULL if the entry was not queued, otherwise a handle that can be used to
832 * cancel; note that even if NULL is returned, the callback will be invoked
833 * (or rather, will already have been invoked)
835 struct GNUNET_DATASTORE_QueueEntry *
836 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h, uint32_t rid,
837 const struct GNUNET_HashCode * key, size_t size,
838 const void *data, enum GNUNET_BLOCK_Type type,
839 uint32_t priority, uint32_t anonymity,
840 uint32_t replication,
841 struct GNUNET_TIME_Absolute expiration,
842 unsigned int queue_priority, unsigned int max_queue_size,
843 struct GNUNET_TIME_Relative timeout,
844 GNUNET_DATASTORE_ContinuationWithStatus cont,
847 struct GNUNET_DATASTORE_QueueEntry *qe;
848 struct DataMessage *dm;
850 union QueueContext qc;
852 LOG (GNUNET_ERROR_TYPE_DEBUG,
853 "Asked to put %u bytes of data under key `%s' for %s\n", size,
855 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (expiration),
857 msize = sizeof (struct DataMessage) + size;
858 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
860 qc.sc.cont_cls = cont_cls;
861 qe = make_queue_entry (h, msize, queue_priority, max_queue_size, timeout,
862 &process_status_message, &qc);
865 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not create queue entry for PUT\n");
868 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# PUT requests executed"),
870 dm = (struct DataMessage *) &qe[1];
871 dm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
872 dm->header.size = htons (msize);
873 dm->rid = htonl (rid);
874 dm->size = htonl ((uint32_t) size);
875 dm->type = htonl (type);
876 dm->priority = htonl (priority);
877 dm->anonymity = htonl (anonymity);
878 dm->replication = htonl (replication);
879 dm->reserved = htonl (0);
880 dm->uid = GNUNET_htonll (0);
881 dm->expiration = GNUNET_TIME_absolute_hton (expiration);
883 memcpy (&dm[1], data, size);
890 * Reserve space in the datastore. This function should be used
891 * to avoid "out of space" failures during a longer sequence of "put"
892 * operations (for example, when a file is being inserted).
894 * @param h handle to the datastore
895 * @param amount how much space (in bytes) should be reserved (for content only)
896 * @param entries how many entries will be created (to calculate per-entry overhead)
897 * @param queue_priority ranking of this request in the priority queue
898 * @param max_queue_size at what queue size should this request be dropped
899 * (if other requests of higher priority are in the queue)
900 * @param timeout how long to wait at most for a response (or before dying in queue)
901 * @param cont continuation to call when done; "success" will be set to
902 * a positive reservation value if space could be reserved.
903 * @param cont_cls closure for cont
904 * @return NULL if the entry was not queued, otherwise a handle that can be used to
905 * cancel; note that even if NULL is returned, the callback will be invoked
906 * (or rather, will already have been invoked)
908 struct GNUNET_DATASTORE_QueueEntry *
909 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h, uint64_t amount,
910 uint32_t entries, unsigned int queue_priority,
911 unsigned int max_queue_size,
912 struct GNUNET_TIME_Relative timeout,
913 GNUNET_DATASTORE_ContinuationWithStatus cont,
916 struct GNUNET_DATASTORE_QueueEntry *qe;
917 struct ReserveMessage *rm;
918 union QueueContext qc;
921 cont = &drop_status_cont;
922 LOG (GNUNET_ERROR_TYPE_DEBUG,
923 "Asked to reserve %llu bytes of data and %u entries\n",
924 (unsigned long long) amount, (unsigned int) entries);
926 qc.sc.cont_cls = cont_cls;
927 qe = make_queue_entry (h, sizeof (struct ReserveMessage), queue_priority,
928 max_queue_size, timeout, &process_status_message, &qc);
931 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not create queue entry to reserve\n");
934 GNUNET_STATISTICS_update (h->stats,
935 gettext_noop ("# RESERVE requests executed"), 1,
937 rm = (struct ReserveMessage *) &qe[1];
938 rm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
939 rm->header.size = htons (sizeof (struct ReserveMessage));
940 rm->entries = htonl (entries);
941 rm->amount = GNUNET_htonll (amount);
948 * Signal that all of the data for which a reservation was made has
949 * been stored and that whatever excess space might have been reserved
950 * can now be released.
952 * @param h handle to the datastore
953 * @param rid reservation ID (value of "success" in original continuation
954 * from the "reserve" function).
955 * @param queue_priority ranking of this request in the priority queue
956 * @param max_queue_size at what queue size should this request be dropped
957 * (if other requests of higher priority are in the queue)
958 * @param queue_priority ranking of this request in the priority queue
959 * @param max_queue_size at what queue size should this request be dropped
960 * (if other requests of higher priority are in the queue)
961 * @param timeout how long to wait at most for a response
962 * @param cont continuation to call when done
963 * @param cont_cls closure for cont
964 * @return NULL if the entry was not queued, otherwise a handle that can be used to
965 * cancel; note that even if NULL is returned, the callback will be invoked
966 * (or rather, will already have been invoked)
968 struct GNUNET_DATASTORE_QueueEntry *
969 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
970 uint32_t rid, unsigned int queue_priority,
971 unsigned int max_queue_size,
972 struct GNUNET_TIME_Relative timeout,
973 GNUNET_DATASTORE_ContinuationWithStatus cont,
976 struct GNUNET_DATASTORE_QueueEntry *qe;
977 struct ReleaseReserveMessage *rrm;
978 union QueueContext qc;
981 cont = &drop_status_cont;
982 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to release reserve %d\n", rid);
984 qc.sc.cont_cls = cont_cls;
985 qe = make_queue_entry (h, sizeof (struct ReleaseReserveMessage),
986 queue_priority, max_queue_size, timeout,
987 &process_status_message, &qc);
990 LOG (GNUNET_ERROR_TYPE_DEBUG,
991 "Could not create queue entry to release reserve\n");
994 GNUNET_STATISTICS_update (h->stats,
996 ("# RELEASE RESERVE requests executed"), 1,
998 rrm = (struct ReleaseReserveMessage *) &qe[1];
999 rrm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
1000 rrm->header.size = htons (sizeof (struct ReleaseReserveMessage));
1001 rrm->rid = htonl (rid);
1008 * Update a value in the datastore.
1010 * @param h handle to the datastore
1011 * @param uid identifier for the value
1012 * @param priority how much to increase the priority of the value
1013 * @param expiration new expiration value should be MAX of existing and this argument
1014 * @param queue_priority ranking of this request in the priority queue
1015 * @param max_queue_size at what queue size should this request be dropped
1016 * (if other requests of higher priority are in the queue)
1017 * @param timeout how long to wait at most for a response
1018 * @param cont continuation to call when done
1019 * @param cont_cls closure for cont
1020 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1021 * cancel; note that even if NULL is returned, the callback will be invoked
1022 * (or rather, will already have been invoked)
1024 struct GNUNET_DATASTORE_QueueEntry *
1025 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h, uint64_t uid,
1027 struct GNUNET_TIME_Absolute expiration,
1028 unsigned int queue_priority,
1029 unsigned int max_queue_size,
1030 struct GNUNET_TIME_Relative timeout,
1031 GNUNET_DATASTORE_ContinuationWithStatus cont,
1034 struct GNUNET_DATASTORE_QueueEntry *qe;
1035 struct UpdateMessage *um;
1036 union QueueContext qc;
1039 cont = &drop_status_cont;
1040 LOG (GNUNET_ERROR_TYPE_DEBUG,
1041 "Asked to update entry %llu raising priority by %u and expiration to %s\n",
1043 (unsigned int) priority,
1044 GNUNET_STRINGS_absolute_time_to_string (expiration));
1046 qc.sc.cont_cls = cont_cls;
1047 qe = make_queue_entry (h, sizeof (struct UpdateMessage), queue_priority,
1048 max_queue_size, timeout, &process_status_message, &qc);
1051 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not create queue entry for UPDATE\n");
1054 GNUNET_STATISTICS_update (h->stats,
1055 gettext_noop ("# UPDATE requests executed"), 1,
1057 um = (struct UpdateMessage *) &qe[1];
1058 um->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
1059 um->header.size = htons (sizeof (struct UpdateMessage));
1060 um->priority = htonl (priority);
1061 um->expiration = GNUNET_TIME_absolute_hton (expiration);
1062 um->uid = GNUNET_htonll (uid);
1069 * Explicitly remove some content from the database.
1070 * The "cont"inuation will be called with status
1071 * "GNUNET_OK" if content was removed, "GNUNET_NO"
1072 * if no matching entry was found and "GNUNET_SYSERR"
1073 * on all other types of errors.
1075 * @param h handle to the datastore
1076 * @param key key for the value
1077 * @param size number of bytes in data
1078 * @param data content stored
1079 * @param queue_priority ranking of this request in the priority queue
1080 * @param max_queue_size at what queue size should this request be dropped
1081 * (if other requests of higher priority are in the queue)
1082 * @param timeout how long to wait at most for a response
1083 * @param cont continuation to call when done
1084 * @param cont_cls closure for cont
1085 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1086 * cancel; note that even if NULL is returned, the callback will be invoked
1087 * (or rather, will already have been invoked)
1089 struct GNUNET_DATASTORE_QueueEntry *
1090 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
1091 const struct GNUNET_HashCode * key, size_t size,
1092 const void *data, unsigned int queue_priority,
1093 unsigned int max_queue_size,
1094 struct GNUNET_TIME_Relative timeout,
1095 GNUNET_DATASTORE_ContinuationWithStatus cont,
1098 struct GNUNET_DATASTORE_QueueEntry *qe;
1099 struct DataMessage *dm;
1101 union QueueContext qc;
1104 cont = &drop_status_cont;
1105 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to remove %u bytes under key `%s'\n",
1106 size, GNUNET_h2s (key));
1108 qc.sc.cont_cls = cont_cls;
1109 msize = sizeof (struct DataMessage) + size;
1110 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1111 qe = make_queue_entry (h, msize, queue_priority, max_queue_size, timeout,
1112 &process_status_message, &qc);
1115 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not create queue entry for REMOVE\n");
1118 GNUNET_STATISTICS_update (h->stats,
1119 gettext_noop ("# REMOVE requests executed"), 1,
1121 dm = (struct DataMessage *) &qe[1];
1122 dm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
1123 dm->header.size = htons (msize);
1124 dm->rid = htonl (0);
1125 dm->size = htonl (size);
1126 dm->type = htonl (0);
1127 dm->priority = htonl (0);
1128 dm->anonymity = htonl (0);
1129 dm->uid = GNUNET_htonll (0);
1130 dm->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_ZERO_ABS);
1132 memcpy (&dm[1], data, size);
1139 * Type of a function to call when we receive a message
1142 * @param cls closure
1143 * @param msg message received, NULL on timeout or fatal error
1146 process_result_message (void *cls, const struct GNUNET_MessageHeader *msg)
1148 struct GNUNET_DATASTORE_Handle *h = cls;
1149 struct GNUNET_DATASTORE_QueueEntry *qe;
1150 struct ResultContext rc;
1151 const struct DataMessage *dm;
1152 int was_transmitted;
1157 GNUNET_assert (NULL != qe);
1159 was_transmitted = qe->was_transmitted;
1160 free_queue_entry (qe);
1161 if (GNUNET_YES == was_transmitted)
1163 LOG (GNUNET_ERROR_TYPE_WARNING,
1164 _("Failed to receive response from database.\n"));
1171 if (NULL != rc.proc)
1172 rc.proc (rc.proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1176 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1178 GNUNET_break (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader));
1181 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1182 free_queue_entry (qe);
1183 LOG (GNUNET_ERROR_TYPE_DEBUG,
1184 "Received end of result set, new queue size is %u\n", h->queue_size);
1185 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1186 h->result_count = 0;
1188 if (rc.proc != NULL)
1189 rc.proc (rc.proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1194 GNUNET_assert (NULL != qe);
1196 if (GNUNET_YES != qe->was_transmitted)
1199 free_queue_entry (qe);
1200 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1202 if (rc.proc != NULL)
1203 rc.proc (rc.proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1207 if ((ntohs (msg->size) < sizeof (struct DataMessage)) ||
1208 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1209 (ntohs (msg->size) !=
1210 sizeof (struct DataMessage) +
1211 ntohl (((const struct DataMessage *) msg)->size)))
1214 free_queue_entry (qe);
1215 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1217 if (rc.proc != NULL)
1218 rc.proc (rc.proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1222 #if INSANE_STATISTICS
1223 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# Results received"), 1,
1226 dm = (const struct DataMessage *) msg;
1227 LOG (GNUNET_ERROR_TYPE_DEBUG,
1228 "Received result %llu with type %u and size %u with key %s\n",
1229 (unsigned long long) GNUNET_ntohll (dm->uid), ntohl (dm->type),
1230 ntohl (dm->size), GNUNET_h2s (&dm->key));
1231 free_queue_entry (qe);
1232 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1234 if (rc.proc != NULL)
1235 rc.proc (rc.proc_cls, &dm->key, ntohl (dm->size), &dm[1], ntohl (dm->type),
1236 ntohl (dm->priority), ntohl (dm->anonymity),
1237 GNUNET_TIME_absolute_ntoh (dm->expiration),
1238 GNUNET_ntohll (dm->uid));
1243 * Get a random value from the datastore for content replication.
1244 * Returns a single, random value among those with the highest
1245 * replication score, lowering positive replication scores by one for
1246 * the chosen value (if only content with a replication score exists,
1247 * a random value is returned and replication scores are not changed).
1249 * @param h handle to the datastore
1250 * @param queue_priority ranking of this request in the priority queue
1251 * @param max_queue_size at what queue size should this request be dropped
1252 * (if other requests of higher priority are in the queue)
1253 * @param timeout how long to wait at most for a response
1254 * @param proc function to call on a random value; it
1255 * will be called once with a value (if available)
1256 * and always once with a value of NULL.
1257 * @param proc_cls closure for proc
1258 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1261 struct GNUNET_DATASTORE_QueueEntry *
1262 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
1263 unsigned int queue_priority,
1264 unsigned int max_queue_size,
1265 struct GNUNET_TIME_Relative timeout,
1266 GNUNET_DATASTORE_DatumProcessor proc,
1269 struct GNUNET_DATASTORE_QueueEntry *qe;
1270 struct GNUNET_MessageHeader *m;
1271 union QueueContext qc;
1273 GNUNET_assert (NULL != proc);
1274 LOG (GNUNET_ERROR_TYPE_DEBUG,
1275 "Asked to get replication entry in %s\n",
1276 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
1278 qc.rc.proc_cls = proc_cls;
1279 qe = make_queue_entry (h, sizeof (struct GNUNET_MessageHeader),
1280 queue_priority, max_queue_size, timeout,
1281 &process_result_message, &qc);
1284 LOG (GNUNET_ERROR_TYPE_DEBUG,
1285 "Could not create queue entry for GET REPLICATION\n");
1288 GNUNET_STATISTICS_update (h->stats,
1290 ("# GET REPLICATION requests executed"), 1,
1292 m = (struct GNUNET_MessageHeader *) &qe[1];
1293 m->type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION);
1294 m->size = htons (sizeof (struct GNUNET_MessageHeader));
1301 * Get a single zero-anonymity value from the datastore.
1303 * @param h handle to the datastore
1304 * @param offset offset of the result (modulo num-results); set to
1305 * a random 64-bit value initially; then increment by
1306 * one each time; detect that all results have been found by uid
1307 * being again the first uid ever returned.
1308 * @param queue_priority ranking of this request in the priority queue
1309 * @param max_queue_size at what queue size should this request be dropped
1310 * (if other requests of higher priority are in the queue)
1311 * @param timeout how long to wait at most for a response
1312 * @param type allowed type for the operation (never zero)
1313 * @param proc function to call on a random value; it
1314 * will be called once with a value (if available)
1315 * or with NULL if none value exists.
1316 * @param proc_cls closure for proc
1317 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1320 struct GNUNET_DATASTORE_QueueEntry *
1321 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
1323 unsigned int queue_priority,
1324 unsigned int max_queue_size,
1325 struct GNUNET_TIME_Relative timeout,
1326 enum GNUNET_BLOCK_Type type,
1327 GNUNET_DATASTORE_DatumProcessor proc,
1330 struct GNUNET_DATASTORE_QueueEntry *qe;
1331 struct GetZeroAnonymityMessage *m;
1332 union QueueContext qc;
1334 GNUNET_assert (NULL != proc);
1335 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
1336 LOG (GNUNET_ERROR_TYPE_DEBUG,
1337 "Asked to get %llu-th zero-anonymity entry of type %d in %s\n",
1338 (unsigned long long) offset, type,
1339 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
1341 qc.rc.proc_cls = proc_cls;
1342 qe = make_queue_entry (h, sizeof (struct GetZeroAnonymityMessage),
1343 queue_priority, max_queue_size, timeout,
1344 &process_result_message, &qc);
1347 LOG (GNUNET_ERROR_TYPE_DEBUG,
1348 "Could not create queue entry for zero-anonymity procation\n");
1351 GNUNET_STATISTICS_update (h->stats,
1353 ("# GET ZERO ANONYMITY requests executed"), 1,
1355 m = (struct GetZeroAnonymityMessage *) &qe[1];
1356 m->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY);
1357 m->header.size = htons (sizeof (struct GetZeroAnonymityMessage));
1358 m->type = htonl ((uint32_t) type);
1359 m->offset = GNUNET_htonll (offset);
1366 * Get a result for a particular key from the datastore. The processor
1367 * will only be called once.
1369 * @param h handle to the datastore
1370 * @param offset offset of the result (modulo num-results); set to
1371 * a random 64-bit value initially; then increment by
1372 * one each time; detect that all results have been found by uid
1373 * being again the first uid ever returned.
1374 * @param key maybe NULL (to match all entries)
1375 * @param type desired type, 0 for any
1376 * @param queue_priority ranking of this request in the priority queue
1377 * @param max_queue_size at what queue size should this request be dropped
1378 * (if other requests of higher priority are in the queue)
1379 * @param timeout how long to wait at most for a response
1380 * @param proc function to call on each matching value;
1381 * will be called once with a NULL value at the end
1382 * @param proc_cls closure for proc
1383 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1386 struct GNUNET_DATASTORE_QueueEntry *
1387 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h, uint64_t offset,
1388 const struct GNUNET_HashCode * key,
1389 enum GNUNET_BLOCK_Type type,
1390 unsigned int queue_priority,
1391 unsigned int max_queue_size,
1392 struct GNUNET_TIME_Relative timeout,
1393 GNUNET_DATASTORE_DatumProcessor proc, void *proc_cls)
1395 struct GNUNET_DATASTORE_QueueEntry *qe;
1396 struct GetMessage *gm;
1397 union QueueContext qc;
1399 GNUNET_assert (NULL != proc);
1400 LOG (GNUNET_ERROR_TYPE_DEBUG,
1401 "Asked to look for data of type %u under key `%s'\n",
1402 (unsigned int) type, GNUNET_h2s (key));
1404 qc.rc.proc_cls = proc_cls;
1405 qe = make_queue_entry (h, sizeof (struct GetMessage), queue_priority,
1406 max_queue_size, timeout, &process_result_message, &qc);
1409 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not queue request for `%s'\n",
1413 #if INSANE_STATISTICS
1414 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# GET requests executed"),
1417 gm = (struct GetMessage *) &qe[1];
1418 gm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1419 gm->type = htonl (type);
1420 gm->offset = GNUNET_htonll (offset);
1423 gm->header.size = htons (sizeof (struct GetMessage));
1429 htons (sizeof (struct GetMessage) - sizeof (struct GNUNET_HashCode));
1437 * Cancel a datastore operation. The final callback from the
1438 * operation must not have been done yet.
1440 * @param qe operation to cancel
1443 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1445 struct GNUNET_DATASTORE_Handle *h;
1447 GNUNET_assert (GNUNET_SYSERR != qe->was_transmitted);
1449 LOG (GNUNET_ERROR_TYPE_DEBUG,
1450 "Pending DATASTORE request %p cancelled (%d, %d)\n", qe,
1451 qe->was_transmitted, h->queue_head == qe);
1452 if (GNUNET_YES == qe->was_transmitted)
1454 free_queue_entry (qe);
1455 h->skip_next_messages++;
1458 free_queue_entry (qe);
1463 /* end of datastore_api.c */