2 This file is part of GNUnet
3 Copyright (C) 2004-2013, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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
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__)
36 #define DELAY_WARN_TIMEOUT GNUNET_TIME_UNIT_MINUTES
39 * Collect an instane number of statistics? May cause excessive IPC.
41 #define INSANE_STATISTICS GNUNET_NO
44 * If a client stopped asking for more results, how many more do
45 * we receive from the DB before killing the connection? Trade-off
46 * between re-doing TCP handshakes and (needlessly) receiving
49 #define MAX_EXCESS_RESULTS 8
52 * Context for processing status messages.
57 * Continuation to call with the status.
59 GNUNET_DATASTORE_ContinuationWithStatus cont;
62 * Closure for @e cont.
69 * Context for processing result messages.
74 * Function to call with the result.
76 GNUNET_DATASTORE_DatumProcessor proc;
79 * Closure for @e proc.
86 * Context for a queue operation.
90 struct StatusContext sc;
92 struct ResultContext rc;
97 * Entry in our priority queue.
99 struct GNUNET_DATASTORE_QueueEntry
102 * This is a linked list.
104 struct GNUNET_DATASTORE_QueueEntry *next;
107 * This is a linked list.
109 struct GNUNET_DATASTORE_QueueEntry *prev;
112 * Handle to the master context.
114 struct GNUNET_DATASTORE_Handle *h;
117 * Function to call after transmission of the request.
119 GNUNET_DATASTORE_ContinuationWithStatus cont;
122 * Closure for @e cont.
127 * Context for the operation.
129 union QueueContext qc;
132 * Envelope of the request to transmit, NULL after
135 struct GNUNET_MQ_Envelope *env;
138 * Task we run if this entry stalls the queue and we
139 * need to warn the user.
141 struct GNUNET_SCHEDULER_Task *delay_warn_task;
144 * Priority in the queue.
146 unsigned int priority;
149 * Maximum allowed length of queue (otherwise
150 * this request should be discarded).
152 unsigned int max_queue;
155 * Expected response type.
157 uint16_t response_type;
162 * Handle to the datastore service.
164 struct GNUNET_DATASTORE_Handle
169 const struct GNUNET_CONFIGURATION_Handle *cfg;
172 * Current connection to the datastore service.
174 struct GNUNET_MQ_Handle *mq;
177 * Handle for statistics.
179 struct GNUNET_STATISTICS_Handle *stats;
182 * Current head of priority queue.
184 struct GNUNET_DATASTORE_QueueEntry *queue_head;
187 * Current tail of priority queue.
189 struct GNUNET_DATASTORE_QueueEntry *queue_tail;
192 * Task for trying to reconnect.
194 struct GNUNET_SCHEDULER_Task *reconnect_task;
197 * How quickly should we retry? Used for exponential back-off on
200 struct GNUNET_TIME_Relative retry_time;
203 * Number of entries in the queue.
205 unsigned int queue_size;
208 * Number of results we're receiving for the current query
209 * after application stopped to care. Used to determine when
210 * to reset the connection.
212 unsigned int result_count;
215 * We should ignore the next message(s) from the service.
217 unsigned int skip_next_messages;
222 * Try reconnecting to the datastore service.
224 * @param cls the `struct GNUNET_DATASTORE_Handle`
227 try_reconnect (void *cls);
231 * Disconnect from the service and then try reconnecting to the datastore service
234 * @param h handle to datastore to disconnect and reconnect
237 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
244 GNUNET_MQ_destroy (h->mq);
246 h->skip_next_messages = 0;
248 = GNUNET_SCHEDULER_add_delayed (h->retry_time,
255 * Free a queue entry. Removes the given entry from the
256 * queue and releases associated resources. Does NOT
259 * @param qe entry to free.
262 free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
264 struct GNUNET_DATASTORE_Handle *h = qe->h;
266 GNUNET_CONTAINER_DLL_remove (h->queue_head,
271 GNUNET_MQ_discard (qe->env);
272 if (NULL != qe->delay_warn_task)
273 GNUNET_SCHEDULER_cancel (qe->delay_warn_task);
279 * Task that logs an error after some time.
281 * @param qe `struct GNUNET_DATASTORE_QueueEntry` about which the error is
284 delay_warning (void *cls)
286 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
288 qe->delay_warn_task = NULL;
289 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
290 "Request %p of type %u at head of datastore queue for more than %s\n",
292 (unsigned int) qe->response_type,
293 GNUNET_STRINGS_relative_time_to_string (DELAY_WARN_TIMEOUT,
295 qe->delay_warn_task = GNUNET_SCHEDULER_add_delayed (DELAY_WARN_TIMEOUT,
302 * Handle error in sending drop request to datastore.
304 * @param cls closure with the datastore handle
305 * @param error error code
308 mq_error_handler (void *cls,
309 enum GNUNET_MQ_Error error)
311 struct GNUNET_DATASTORE_Handle *h = cls;
312 struct GNUNET_DATASTORE_QueueEntry *qe;
314 LOG (GNUNET_ERROR_TYPE_DEBUG,
315 "MQ error, reconnecting to DATASTORE\n");
320 if (NULL != qe->delay_warn_task)
322 GNUNET_SCHEDULER_cancel (qe->delay_warn_task);
323 qe->delay_warn_task = NULL;
327 union QueueContext qc = qe->qc;
328 uint16_t rt = qe->response_type;
330 LOG (GNUNET_ERROR_TYPE_DEBUG,
331 "Failed to receive response from database.\n");
332 free_queue_entry (qe);
335 case GNUNET_MESSAGE_TYPE_DATASTORE_STATUS:
336 if (NULL != qc.sc.cont)
337 qc.sc.cont (qc.sc.cont_cls,
339 GNUNET_TIME_UNIT_ZERO_ABS,
340 _ ("DATASTORE disconnected"));
343 case GNUNET_MESSAGE_TYPE_DATASTORE_DATA:
344 if (NULL != qc.rc.proc)
345 qc.rc.proc (qc.rc.proc_cls,
353 GNUNET_TIME_UNIT_ZERO_ABS,
365 * Connect to the datastore service.
367 * @param cfg configuration to use
368 * @return handle to use to access the service
370 struct GNUNET_DATASTORE_Handle *
371 GNUNET_DATASTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
373 struct GNUNET_DATASTORE_Handle *h;
375 LOG (GNUNET_ERROR_TYPE_DEBUG,
376 "Establishing DATASTORE connection!\n");
377 h = GNUNET_new (struct GNUNET_DATASTORE_Handle);
385 h->stats = GNUNET_STATISTICS_create ("datastore-api",
392 * Task used by to disconnect from the datastore after
393 * we send the #GNUNET_MESSAGE_TYPE_DATASTORE_DROP message.
395 * @param cls the datastore handle
398 disconnect_after_drop (void *cls)
400 struct GNUNET_DATASTORE_Handle *h = cls;
402 LOG (GNUNET_ERROR_TYPE_DEBUG,
403 "Drop sent, disconnecting\n");
404 GNUNET_DATASTORE_disconnect (h,
410 * Handle error in sending drop request to datastore.
412 * @param cls closure with the datastore handle
413 * @param error error code
416 disconnect_on_mq_error (void *cls,
417 enum GNUNET_MQ_Error error)
419 struct GNUNET_DATASTORE_Handle *h = cls;
421 LOG (GNUNET_ERROR_TYPE_ERROR,
422 "Failed to ask datastore to drop tables\n");
423 GNUNET_DATASTORE_disconnect (h,
429 * Disconnect from the datastore service (and free
430 * associated resources).
432 * @param h handle to the datastore
433 * @param drop set to #GNUNET_YES to delete all data in datastore (!)
436 GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
439 struct GNUNET_DATASTORE_QueueEntry *qe;
441 LOG (GNUNET_ERROR_TYPE_DEBUG,
442 "Datastore disconnect\n");
445 GNUNET_MQ_destroy (h->mq);
448 if (NULL != h->reconnect_task)
450 GNUNET_SCHEDULER_cancel (h->reconnect_task);
451 h->reconnect_task = NULL;
453 while (NULL != (qe = h->queue_head))
455 switch (qe->response_type)
457 case GNUNET_MESSAGE_TYPE_DATASTORE_STATUS:
458 if (NULL != qe->qc.sc.cont)
459 qe->qc.sc.cont (qe->qc.sc.cont_cls,
461 GNUNET_TIME_UNIT_ZERO_ABS,
462 _ ("Disconnected from DATASTORE"));
465 case GNUNET_MESSAGE_TYPE_DATASTORE_DATA:
466 if (NULL != qe->qc.rc.proc)
467 qe->qc.rc.proc (qe->qc.rc.proc_cls,
475 GNUNET_TIME_UNIT_ZERO_ABS,
482 free_queue_entry (qe);
484 if (GNUNET_YES == drop)
486 LOG (GNUNET_ERROR_TYPE_DEBUG,
487 "Re-connecting to issue DROP!\n");
488 GNUNET_assert (NULL == h->mq);
489 h->mq = GNUNET_CLIENT_connect (h->cfg,
492 &disconnect_on_mq_error,
496 struct GNUNET_MessageHeader *hdr;
497 struct GNUNET_MQ_Envelope *env;
499 env = GNUNET_MQ_msg (hdr,
500 GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
501 GNUNET_MQ_notify_sent (env,
502 &disconnect_after_drop,
504 GNUNET_MQ_send (h->mq,
510 GNUNET_STATISTICS_destroy (h->stats,
518 * Create a new entry for our priority queue (and possibly discard other entires if
519 * the queue is getting too long).
521 * @param h handle to the datastore
522 * @param env envelope with the message to queue
523 * @param queue_priority priority of the entry
524 * @param max_queue_size at what queue size should this request be dropped
525 * (if other requests of higher priority are in the queue)
526 * @param expected_type which type of response do we expect,
527 * #GNUNET_MESSAGE_TYPE_DATASTORE_STATUS or
528 * #GNUNET_MESSAGE_TYPE_DATASTORE_DATA
529 * @param qc client context (NOT a closure for @a response_proc)
530 * @return NULL if the queue is full
532 static struct GNUNET_DATASTORE_QueueEntry *
533 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
534 struct GNUNET_MQ_Envelope *env,
535 unsigned int queue_priority,
536 unsigned int max_queue_size,
537 uint16_t expected_type,
538 const union QueueContext *qc)
540 struct GNUNET_DATASTORE_QueueEntry *qe;
541 struct GNUNET_DATASTORE_QueueEntry *pos;
544 if ((NULL != h->queue_tail) &&
545 (h->queue_tail->priority >= queue_priority))
555 while ((NULL != pos) &&
556 (c < max_queue_size) &&
557 (pos->priority >= queue_priority))
562 if (c >= max_queue_size)
564 GNUNET_STATISTICS_update (h->stats,
565 gettext_noop ("# queue overflows"),
568 GNUNET_MQ_discard (env);
571 qe = GNUNET_new (struct GNUNET_DATASTORE_QueueEntry);
574 qe->response_type = expected_type;
576 qe->priority = queue_priority;
577 qe->max_queue = max_queue_size;
580 /* append at the tail */
586 /* do not insert at HEAD if HEAD query was already
587 * transmitted and we are still receiving replies! */
589 (NULL == h->queue_head->env))
593 #if INSANE_STATISTICS
594 GNUNET_STATISTICS_update (h->stats,
595 gettext_noop ("# queue entries created"),
599 GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
609 * Process entries in the queue (or do nothing if we are already
612 * @param h handle to the datastore
615 process_queue (struct GNUNET_DATASTORE_Handle *h)
617 struct GNUNET_DATASTORE_QueueEntry *qe;
619 if (NULL == (qe = h->queue_head))
621 /* no entry in queue */
622 LOG (GNUNET_ERROR_TYPE_DEBUG,
628 /* waiting for replies */
629 LOG (GNUNET_ERROR_TYPE_DEBUG,
630 "Head request already transmitted\n");
635 /* waiting for reconnect */
636 LOG (GNUNET_ERROR_TYPE_DEBUG,
640 GNUNET_assert (NULL == qe->delay_warn_task);
641 qe->delay_warn_task = GNUNET_SCHEDULER_add_delayed (DELAY_WARN_TIMEOUT,
644 GNUNET_MQ_send (h->mq,
651 * Get the entry at the head of the message queue.
653 * @param h handle to the datastore
654 * @param response_type the expected response type
655 * @return the queue entry
657 static struct GNUNET_DATASTORE_QueueEntry *
658 get_queue_head (struct GNUNET_DATASTORE_Handle *h,
659 uint16_t response_type)
661 struct GNUNET_DATASTORE_QueueEntry *qe;
663 if (h->skip_next_messages > 0)
665 h->skip_next_messages--;
682 if (response_type != qe->response_type)
693 * Function called to check status message from the service.
696 * @param sm status message received
697 * @return #GNUNET_OK if the message is well-formed
700 check_status (void *cls,
701 const struct StatusMessage *sm)
703 uint16_t msize = ntohs (sm->header.size) - sizeof(*sm);
704 int32_t status = ntohl (sm->status);
708 const char *emsg = (const char *) &sm[1];
710 if ('\0' != emsg[msize - 1])
713 return GNUNET_SYSERR;
716 else if (GNUNET_SYSERR == status)
719 return GNUNET_SYSERR;
726 * Function called to handle status message from the service.
729 * @param sm status message received
732 handle_status (void *cls,
733 const struct StatusMessage *sm)
735 struct GNUNET_DATASTORE_Handle *h = cls;
736 struct GNUNET_DATASTORE_QueueEntry *qe;
737 struct StatusContext rc;
739 int32_t status = ntohl (sm->status);
741 qe = get_queue_head (h,
742 GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
746 free_queue_entry (qe);
747 if (ntohs (sm->header.size) > sizeof(struct StatusMessage))
748 emsg = (const char *) &sm[1];
751 LOG (GNUNET_ERROR_TYPE_DEBUG,
752 "Received status %d/%s\n",
755 GNUNET_STATISTICS_update (h->stats,
756 gettext_noop ("# status messages received"),
759 h->retry_time = GNUNET_TIME_UNIT_ZERO;
762 rc.cont (rc.cont_cls,
764 GNUNET_TIME_absolute_ntoh (sm->min_expiration),
770 * Check data message we received from the service.
772 * @param cls closure with the `struct GNUNET_DATASTORE_Handle *`
773 * @param dm message received
776 check_data (void *cls,
777 const struct DataMessage *dm)
779 uint16_t msize = ntohs (dm->header.size) - sizeof(*dm);
781 if (msize != ntohl (dm->size))
784 return GNUNET_SYSERR;
791 * Handle data message we got from the service.
793 * @param cls closure with the `struct GNUNET_DATASTORE_Handle *`
794 * @param dm message received
797 handle_data (void *cls,
798 const struct DataMessage *dm)
800 struct GNUNET_DATASTORE_Handle *h = cls;
801 struct GNUNET_DATASTORE_QueueEntry *qe;
802 struct ResultContext rc;
804 qe = get_queue_head (h,
805 GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
808 #if INSANE_STATISTICS
809 GNUNET_STATISTICS_update (h->stats,
810 gettext_noop ("# Results received"),
814 LOG (GNUNET_ERROR_TYPE_DEBUG,
815 "Received result %llu with type %u and size %u with key %s\n",
816 (unsigned long long) GNUNET_ntohll (dm->uid),
819 GNUNET_h2s (&dm->key));
821 free_queue_entry (qe);
822 h->retry_time = GNUNET_TIME_UNIT_ZERO;
825 rc.proc (rc.proc_cls,
830 ntohl (dm->priority),
831 ntohl (dm->anonymity),
832 ntohl (dm->replication),
833 GNUNET_TIME_absolute_ntoh (dm->expiration),
834 GNUNET_ntohll (dm->uid));
839 * Type of a function to call when we receive a
840 * #GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END message from the service.
842 * @param cls closure with the `struct GNUNET_DATASTORE_Handle *`
843 * @param msg message received
846 handle_data_end (void *cls,
847 const struct GNUNET_MessageHeader *msg)
849 struct GNUNET_DATASTORE_Handle *h = cls;
850 struct GNUNET_DATASTORE_QueueEntry *qe;
851 struct ResultContext rc;
853 qe = get_queue_head (h,
854 GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
858 free_queue_entry (qe);
859 LOG (GNUNET_ERROR_TYPE_DEBUG,
860 "Received end of result set, new queue size is %u\n",
862 h->retry_time = GNUNET_TIME_UNIT_ZERO;
865 /* signal end of iteration */
867 rc.proc (rc.proc_cls,
875 GNUNET_TIME_UNIT_ZERO_ABS,
881 * Try reconnecting to the datastore service.
883 * @param cls the `struct GNUNET_DATASTORE_Handle`
886 try_reconnect (void *cls)
888 struct GNUNET_DATASTORE_Handle *h = cls;
889 struct GNUNET_MQ_MessageHandler handlers[] = {
890 GNUNET_MQ_hd_var_size (status,
891 GNUNET_MESSAGE_TYPE_DATASTORE_STATUS,
892 struct StatusMessage,
894 GNUNET_MQ_hd_var_size (data,
895 GNUNET_MESSAGE_TYPE_DATASTORE_DATA,
898 GNUNET_MQ_hd_fixed_size (data_end,
899 GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END,
900 struct GNUNET_MessageHeader,
902 GNUNET_MQ_handler_end ()
905 h->retry_time = GNUNET_TIME_STD_BACKOFF (h->retry_time);
906 h->reconnect_task = NULL;
907 GNUNET_assert (NULL == h->mq);
908 h->mq = GNUNET_CLIENT_connect (h->cfg,
915 GNUNET_STATISTICS_update (h->stats,
917 "# datastore connections (re)created"),
920 LOG (GNUNET_ERROR_TYPE_DEBUG,
921 "Reconnected to DATASTORE\n");
927 * Dummy continuation used to do nothing (but be non-zero).
930 * @param result result
931 * @param min_expiration expiration time
932 * @param emsg error message
935 drop_status_cont (void *cls,
937 struct GNUNET_TIME_Absolute min_expiration,
945 * Store an item in the datastore. If the item is already present,
946 * the priorities are summed up and the higher expiration time and
947 * lower anonymity level is used.
949 * @param h handle to the datastore
950 * @param rid reservation ID to use (from "reserve"); use 0 if no
951 * prior reservation was made
952 * @param key key for the value
953 * @param size number of bytes in data
954 * @param data content stored
955 * @param type type of the content
956 * @param priority priority of the content
957 * @param anonymity anonymity-level for the content
958 * @param replication how often should the content be replicated to other peers?
959 * @param expiration expiration time for the content
960 * @param queue_priority ranking of this request in the priority queue
961 * @param max_queue_size at what queue size should this request be dropped
962 * (if other requests of higher priority are in the queue)
963 * @param cont continuation to call when done
964 * @param cont_cls closure for @a cont
965 * @return NULL if the entry was not queued, otherwise a handle that can be used to
966 * cancel; note that even if NULL is returned, the callback will be invoked
967 * (or rather, will already have been invoked)
969 struct GNUNET_DATASTORE_QueueEntry *
970 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
972 const struct GNUNET_HashCode *key,
975 enum GNUNET_BLOCK_Type type,
978 uint32_t replication,
979 struct GNUNET_TIME_Absolute expiration,
980 unsigned int queue_priority,
981 unsigned int max_queue_size,
982 GNUNET_DATASTORE_ContinuationWithStatus cont,
985 struct GNUNET_DATASTORE_QueueEntry *qe;
986 struct GNUNET_MQ_Envelope *env;
987 struct DataMessage *dm;
988 union QueueContext qc;
990 if (size + sizeof(*dm) >= GNUNET_MAX_MESSAGE_SIZE)
996 LOG (GNUNET_ERROR_TYPE_DEBUG,
997 "Asked to put %u bytes of data under key `%s' for %s\n",
1000 GNUNET_STRINGS_relative_time_to_string (
1001 GNUNET_TIME_absolute_get_remaining (expiration),
1003 env = GNUNET_MQ_msg_extra (dm,
1005 GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
1006 dm->rid = htonl (rid);
1007 dm->size = htonl ((uint32_t) size);
1008 dm->type = htonl (type);
1009 dm->priority = htonl (priority);
1010 dm->anonymity = htonl (anonymity);
1011 dm->replication = htonl (replication);
1012 dm->expiration = GNUNET_TIME_absolute_hton (expiration);
1014 GNUNET_memcpy (&dm[1],
1018 qc.sc.cont_cls = cont_cls;
1019 qe = make_queue_entry (h,
1023 GNUNET_MESSAGE_TYPE_DATASTORE_STATUS,
1027 LOG (GNUNET_ERROR_TYPE_DEBUG,
1028 "Could not create queue entry for PUT\n");
1031 GNUNET_STATISTICS_update (h->stats,
1032 gettext_noop ("# PUT requests executed"),
1041 * Reserve space in the datastore. This function should be used
1042 * to avoid "out of space" failures during a longer sequence of "put"
1043 * operations (for example, when a file is being inserted).
1045 * @param h handle to the datastore
1046 * @param amount how much space (in bytes) should be reserved (for content only)
1047 * @param entries how many entries will be created (to calculate per-entry overhead)
1048 * @param cont continuation to call when done; "success" will be set to
1049 * a positive reservation value if space could be reserved.
1050 * @param cont_cls closure for @a cont
1051 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1052 * cancel; note that even if NULL is returned, the callback will be invoked
1053 * (or rather, will already have been invoked)
1055 struct GNUNET_DATASTORE_QueueEntry *
1056 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
1059 GNUNET_DATASTORE_ContinuationWithStatus cont,
1062 struct GNUNET_DATASTORE_QueueEntry *qe;
1063 struct GNUNET_MQ_Envelope *env;
1064 struct ReserveMessage *rm;
1065 union QueueContext qc;
1068 cont = &drop_status_cont;
1069 LOG (GNUNET_ERROR_TYPE_DEBUG,
1070 "Asked to reserve %llu bytes of data and %u entries\n",
1071 (unsigned long long) amount,
1072 (unsigned int) entries);
1073 env = GNUNET_MQ_msg (rm,
1074 GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
1075 rm->entries = htonl (entries);
1076 rm->amount = GNUNET_htonll (amount);
1079 qc.sc.cont_cls = cont_cls;
1080 qe = make_queue_entry (h,
1084 GNUNET_MESSAGE_TYPE_DATASTORE_STATUS,
1088 LOG (GNUNET_ERROR_TYPE_DEBUG,
1089 "Could not create queue entry to reserve\n");
1092 GNUNET_STATISTICS_update (h->stats,
1093 gettext_noop ("# RESERVE requests executed"),
1102 * Signal that all of the data for which a reservation was made has
1103 * been stored and that whatever excess space might have been reserved
1104 * can now be released.
1106 * @param h handle to the datastore
1107 * @param rid reservation ID (value of "success" in original continuation
1108 * from the "reserve" function).
1109 * @param queue_priority ranking of this request in the priority queue
1110 * @param max_queue_size at what queue size should this request be dropped
1111 * (if other requests of higher priority are in the queue)
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 cont continuation to call when done
1116 * @param cont_cls closure for @a cont
1117 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1118 * cancel; note that even if NULL is returned, the callback will be invoked
1119 * (or rather, will already have been invoked)
1121 struct GNUNET_DATASTORE_QueueEntry *
1122 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
1124 unsigned int queue_priority,
1125 unsigned int max_queue_size,
1126 GNUNET_DATASTORE_ContinuationWithStatus cont,
1129 struct GNUNET_DATASTORE_QueueEntry *qe;
1130 struct GNUNET_MQ_Envelope *env;
1131 struct ReleaseReserveMessage *rrm;
1132 union QueueContext qc;
1135 cont = &drop_status_cont;
1136 LOG (GNUNET_ERROR_TYPE_DEBUG,
1137 "Asked to release reserve %d\n",
1139 env = GNUNET_MQ_msg (rrm,
1140 GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
1141 rrm->rid = htonl (rid);
1143 qc.sc.cont_cls = cont_cls;
1144 qe = make_queue_entry (h,
1148 GNUNET_MESSAGE_TYPE_DATASTORE_STATUS,
1152 LOG (GNUNET_ERROR_TYPE_DEBUG,
1153 "Could not create queue entry to release reserve\n");
1156 GNUNET_STATISTICS_update (h->stats,
1158 ("# RELEASE RESERVE requests executed"), 1,
1166 * Explicitly remove some content from the database.
1167 * The @a cont continuation will be called with `status`
1168 * #GNUNET_OK" if content was removed, #GNUNET_NO
1169 * if no matching entry was found and #GNUNET_SYSERR
1170 * on all other types of errors.
1172 * @param h handle to the datastore
1173 * @param key key for the value
1174 * @param size number of bytes in data
1175 * @param data content stored
1176 * @param queue_priority ranking of this request in the priority queue
1177 * @param max_queue_size at what queue size should this request be dropped
1178 * (if other requests of higher priority are in the queue)
1179 * @param cont continuation to call when done
1180 * @param cont_cls closure for @a cont
1181 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1182 * cancel; note that even if NULL is returned, the callback will be invoked
1183 * (or rather, will already have been invoked)
1185 struct GNUNET_DATASTORE_QueueEntry *
1186 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
1187 const struct GNUNET_HashCode *key,
1190 unsigned int queue_priority,
1191 unsigned int max_queue_size,
1192 GNUNET_DATASTORE_ContinuationWithStatus cont,
1195 struct GNUNET_DATASTORE_QueueEntry *qe;
1196 struct DataMessage *dm;
1197 struct GNUNET_MQ_Envelope *env;
1198 union QueueContext qc;
1200 if (sizeof(*dm) + size >= GNUNET_MAX_MESSAGE_SIZE)
1206 cont = &drop_status_cont;
1207 LOG (GNUNET_ERROR_TYPE_DEBUG,
1208 "Asked to remove %u bytes under key `%s'\n",
1211 env = GNUNET_MQ_msg_extra (dm,
1213 GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
1214 dm->size = htonl (size);
1216 GNUNET_memcpy (&dm[1],
1221 qc.sc.cont_cls = cont_cls;
1223 qe = make_queue_entry (h,
1227 GNUNET_MESSAGE_TYPE_DATASTORE_STATUS,
1231 LOG (GNUNET_ERROR_TYPE_DEBUG,
1232 "Could not create queue entry for REMOVE\n");
1235 GNUNET_STATISTICS_update (h->stats,
1236 gettext_noop ("# REMOVE requests executed"),
1245 * Get a random value from the datastore for content replication.
1246 * Returns a single, random value among those with the highest
1247 * replication score, lowering positive replication scores by one for
1248 * the chosen value (if only content with a replication score exists,
1249 * a random value is returned and replication scores are not changed).
1251 * @param h handle to the datastore
1252 * @param queue_priority ranking of this request in the priority queue
1253 * @param max_queue_size at what queue size should this request be dropped
1254 * (if other requests of higher priority are in the queue)
1255 * @param proc function to call on a random value; it
1256 * will be called once with a value (if available)
1257 * and always once with a value of NULL.
1258 * @param proc_cls closure for @a proc
1259 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1262 struct GNUNET_DATASTORE_QueueEntry *
1263 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
1264 unsigned int queue_priority,
1265 unsigned int max_queue_size,
1266 GNUNET_DATASTORE_DatumProcessor proc,
1269 struct GNUNET_DATASTORE_QueueEntry *qe;
1270 struct GNUNET_MQ_Envelope *env;
1271 struct GNUNET_MessageHeader *m;
1272 union QueueContext qc;
1274 GNUNET_assert (NULL != proc);
1275 LOG (GNUNET_ERROR_TYPE_DEBUG,
1276 "Asked to get replication entry\n");
1277 env = GNUNET_MQ_msg (m,
1278 GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION);
1280 qc.rc.proc_cls = proc_cls;
1281 qe = make_queue_entry (h,
1285 GNUNET_MESSAGE_TYPE_DATASTORE_DATA,
1289 LOG (GNUNET_ERROR_TYPE_DEBUG,
1290 "Could not create queue entry for GET REPLICATION\n");
1293 GNUNET_STATISTICS_update (h->stats,
1295 ("# GET REPLICATION requests executed"), 1,
1303 * Get a single zero-anonymity value from the datastore.
1305 * @param h handle to the datastore
1306 * @param next_uid return the result with lowest uid >= next_uid
1307 * @param queue_priority ranking of this request in the priority queue
1308 * @param max_queue_size at what queue size should this request be dropped
1309 * (if other requests of higher priority are in the queue)
1310 * @param type allowed type for the operation (never zero)
1311 * @param proc function to call on a random value; it
1312 * will be called once with a value (if available)
1313 * or with NULL if none value exists.
1314 * @param proc_cls closure for @a proc
1315 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1318 struct GNUNET_DATASTORE_QueueEntry *
1319 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
1321 unsigned int queue_priority,
1322 unsigned int max_queue_size,
1323 enum GNUNET_BLOCK_Type type,
1324 GNUNET_DATASTORE_DatumProcessor proc,
1327 struct GNUNET_DATASTORE_QueueEntry *qe;
1328 struct GNUNET_MQ_Envelope *env;
1329 struct GetZeroAnonymityMessage *m;
1330 union QueueContext qc;
1332 GNUNET_assert (NULL != proc);
1333 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
1334 LOG (GNUNET_ERROR_TYPE_DEBUG,
1335 "Asked to get a zero-anonymity entry of type %d\n",
1337 env = GNUNET_MQ_msg (m,
1338 GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY);
1339 m->type = htonl ((uint32_t) type);
1340 m->next_uid = GNUNET_htonll (next_uid);
1342 qc.rc.proc_cls = proc_cls;
1343 qe = make_queue_entry (h,
1347 GNUNET_MESSAGE_TYPE_DATASTORE_DATA,
1351 LOG (GNUNET_ERROR_TYPE_DEBUG,
1352 "Could not create queue entry for zero-anonymity procation\n");
1355 GNUNET_STATISTICS_update (h->stats,
1357 ("# GET ZERO ANONYMITY requests executed"), 1,
1365 * Get a result for a particular key from the datastore. The processor
1366 * will only be called once.
1368 * @param h handle to the datastore
1369 * @param next_uid return the result with lowest uid >= next_uid
1370 * @param random if true, return a random result instead of using next_uid
1371 * @param key maybe NULL (to match all entries)
1372 * @param type desired type, 0 for any
1373 * @param queue_priority ranking of this request in the priority queue
1374 * @param max_queue_size at what queue size should this request be dropped
1375 * (if other requests of higher priority are in the queue)
1376 * @param proc function to call on each matching value;
1377 * will be called once with a NULL value at the end
1378 * @param proc_cls closure for @a proc
1379 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1382 struct GNUNET_DATASTORE_QueueEntry *
1383 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h,
1386 const struct GNUNET_HashCode *key,
1387 enum GNUNET_BLOCK_Type type,
1388 unsigned int queue_priority,
1389 unsigned int max_queue_size,
1390 GNUNET_DATASTORE_DatumProcessor proc,
1393 struct GNUNET_DATASTORE_QueueEntry *qe;
1394 struct GNUNET_MQ_Envelope *env;
1395 struct GetKeyMessage *gkm;
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,
1406 env = GNUNET_MQ_msg (gm,
1407 GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1408 gm->type = htonl (type);
1409 gm->next_uid = GNUNET_htonll (next_uid);
1410 gm->random = random;
1414 env = GNUNET_MQ_msg (gkm,
1415 GNUNET_MESSAGE_TYPE_DATASTORE_GET_KEY);
1416 gkm->type = htonl (type);
1417 gkm->next_uid = GNUNET_htonll (next_uid);
1418 gkm->random = random;
1422 qc.rc.proc_cls = proc_cls;
1423 qe = make_queue_entry (h,
1427 GNUNET_MESSAGE_TYPE_DATASTORE_DATA,
1431 LOG (GNUNET_ERROR_TYPE_DEBUG,
1432 "Could not queue request for `%s'\n",
1436 #if INSANE_STATISTICS
1437 GNUNET_STATISTICS_update (h->stats,
1438 gettext_noop ("# GET requests executed"),
1448 * Cancel a datastore operation. The final callback from the
1449 * operation must not have been done yet.
1451 * @param qe operation to cancel
1454 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1456 struct GNUNET_DATASTORE_Handle *h = qe->h;
1458 LOG (GNUNET_ERROR_TYPE_DEBUG,
1459 "Pending DATASTORE request %p cancelled (%d, %d)\n",
1462 h->queue_head == qe);
1463 if (NULL == qe->env)
1465 free_queue_entry (qe);
1466 h->skip_next_messages++;
1469 free_queue_entry (qe);
1474 /* end of datastore_api.c */