2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2007, 2009 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 2, 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/gnunet-service-datastore.c
23 * @brief Management for the datastore for files stored on a GNUnet node
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_statistics_service.h"
32 #include "plugin_datastore.h"
33 #include "datastore.h"
36 * How many messages do we queue at most per client?
38 #define MAX_PENDING 1024
41 * How long are we at most keeping "expired" content
42 * past the expiration date in the database?
44 #define MAX_EXPIRE_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
49 * Our datastore plugin.
51 struct DatastorePlugin
55 * API of the transport as returned by the plugin's
56 * initialization function.
58 struct GNUNET_DATASTORE_PluginFunctions *api;
61 * Short name for the plugin (i.e. "sqlite").
66 * Name of the library (i.e. "gnunet_plugin_datastore_sqlite").
71 * Environment this transport service is using
74 struct GNUNET_DATASTORE_PluginEnvironment env;
80 * Linked list of active reservations.
82 struct ReservationList
86 * This is a linked list.
88 struct ReservationList *next;
91 * Client that made the reservation.
93 struct GNUNET_SERVER_Client *client;
96 * Number of bytes (still) reserved.
101 * Number of items (still) reserved.
106 * Reservation identifier.
114 * Our datastore plugin (NULL if not available).
116 static struct DatastorePlugin *plugin;
119 * Linked list of space reservations made by clients.
121 static struct ReservationList *reservations;
124 * Bloomfilter to quickly tell if we don't have the content.
126 static struct GNUNET_CONTAINER_BloomFilter *filter;
129 * Static counter to produce reservation identifiers.
131 static int reservation_gen;
134 * How much space are we allowed to use?
136 static unsigned long long quota;
139 * How much space are we using for the cache? (space available for
140 * insertions that will be instantly reclaimed by discarding less
141 * important content --- or possibly whatever we just inserted into
144 static unsigned long long cache_size;
147 * How much space have we currently reserved?
149 static unsigned long long reserved;
152 * Identity of the task that is used to delete
155 static GNUNET_SCHEDULER_TaskIdentifier expired_kill_task;
160 const struct GNUNET_CONFIGURATION_Handle *cfg;
165 struct GNUNET_SCHEDULER_Handle *sched;
168 * Handle for reporting statistics.
170 static struct GNUNET_STATISTICS_Handle *stats;
174 * Function called once the transmit operation has
175 * either failed or succeeded.
178 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
180 typedef void (*TransmitContinuation)(void *cls,
185 * Context for transmitting replies to clients.
187 struct TransmitCallbackContext
191 * We keep these in a doubly-linked list (for cleanup).
193 struct TransmitCallbackContext *next;
196 * We keep these in a doubly-linked list (for cleanup).
198 struct TransmitCallbackContext *prev;
201 * The message that we're asked to transmit.
203 struct GNUNET_MessageHeader *msg;
206 * Handle for the transmission request.
208 struct GNUNET_CONNECTION_TransmitHandle *th;
211 * Client that we are transmitting to.
213 struct GNUNET_SERVER_Client *client;
216 * Function to call once msg has been transmitted
217 * (or at least added to the buffer).
219 TransmitContinuation tc;
227 * GNUNET_YES if we are supposed to signal the server
228 * completion of the client's request.
235 * Head of the doubly-linked list (for cleanup).
237 static struct TransmitCallbackContext *tcc_head;
240 * Tail of the doubly-linked list (for cleanup).
242 static struct TransmitCallbackContext *tcc_tail;
245 * Have we already clean ed up the TCCs and are hence no longer
246 * willing (or able) to transmit anything to anyone?
248 static int cleaning_done;
251 * Task that is used to remove expired entries from
252 * the datastore. This task will schedule itself
253 * again automatically to always delete all expired
256 * @param cls not used
257 * @param tc task context
260 delete_expired (void *cls,
261 const struct GNUNET_SCHEDULER_TaskContext *tc);
265 * Iterate over the expired items stored in the datastore.
266 * Delete all expired items; once we have processed all
267 * expired items, re-schedule the "delete_expired" task.
269 * @param cls not used
270 * @param next_cls closure to pass to the "next" function.
271 * @param key key for the content
272 * @param size number of bytes in data
273 * @param data content stored
274 * @param type type of the content
275 * @param priority priority of the content
276 * @param anonymity anonymity-level for the content
277 * @param expiration expiration time for the content
278 * @param uid unique identifier for the datum;
279 * maybe 0 if no unique identifier is available
281 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
282 * (continue on call to "next", of course),
283 * GNUNET_NO to delete the item and continue (if supported)
286 expired_processor (void *cls,
288 const GNUNET_HashCode * key,
291 enum GNUNET_BLOCK_Type type,
294 struct GNUNET_TIME_Absolute
298 struct GNUNET_TIME_Absolute now;
303 = GNUNET_SCHEDULER_add_delayed (sched,
307 return GNUNET_SYSERR;
309 now = GNUNET_TIME_absolute_get ();
310 if (expiration.value > now.value)
312 /* finished processing */
313 plugin->api->next_request (next_cls, GNUNET_YES);
314 return GNUNET_SYSERR;
316 plugin->api->next_request (next_cls, GNUNET_NO);
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319 "Deleting content that expired %llu ms ago\n",
320 (unsigned long long) (now.value - expiration.value));
322 GNUNET_STATISTICS_update (stats,
323 gettext_noop ("# bytes expired"),
326 GNUNET_CONTAINER_bloomfilter_remove (filter,
328 return GNUNET_NO; /* delete */
333 * Task that is used to remove expired entries from
334 * the datastore. This task will schedule itself
335 * again automatically to always delete all expired
338 * @param cls not used
339 * @param tc task context
342 delete_expired (void *cls,
343 const struct GNUNET_SCHEDULER_TaskContext *tc)
345 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
346 plugin->api->iter_ascending_expiration (plugin->api->cls,
354 * An iterator over a set of items stored in the datastore.
357 * @param next_cls closure to pass to the "next" function.
358 * @param key key for the content
359 * @param size number of bytes in data
360 * @param data content stored
361 * @param type type of the content
362 * @param priority priority of the content
363 * @param anonymity anonymity-level for the content
364 * @param expiration expiration time for the content
365 * @param uid unique identifier for the datum;
366 * maybe 0 if no unique identifier is available
368 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
369 * (continue on call to "next", of course),
370 * GNUNET_NO to delete the item and continue (if supported)
375 const GNUNET_HashCode * key,
378 enum GNUNET_BLOCK_Type type,
381 struct GNUNET_TIME_Absolute
385 unsigned long long *need = cls;
390 return GNUNET_SYSERR;
392 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
395 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
396 plugin->api->next_request (next_cls,
397 (0 == *need) ? GNUNET_YES : GNUNET_NO);
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "Deleting %llu bytes of low-priority content (still trying to free another %llu bytes)\n",
401 size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
404 GNUNET_STATISTICS_update (stats,
405 gettext_noop ("# bytes purged (low-priority)"),
408 GNUNET_CONTAINER_bloomfilter_remove (filter,
415 * Manage available disk space by running tasks
416 * that will discard content if necessary. This
417 * function will be run whenever a request for
418 * "need" bytes of storage could only be satisfied
419 * by eating into the "cache" (and we want our cache
422 * @param need number of bytes of content that were
423 * placed into the "cache" (and hence the
424 * number of bytes that should be removed).
427 manage_space (unsigned long long need)
429 unsigned long long *n;
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "Asked to free up %llu bytes of cache space\n",
436 n = GNUNET_malloc (sizeof(unsigned long long));
438 plugin->api->iter_low_priority (plugin->api->cls,
446 * Function called to notify a client about the socket
447 * begin ready to queue more data. "buf" will be
448 * NULL and "size" zero if the socket was closed for
449 * writing in the meantime.
452 * @param size number of bytes available in buf
453 * @param buf where the callee should write the message
454 * @return number of bytes written to buf
457 transmit_callback (void *cls,
458 size_t size, void *buf)
460 struct TransmitCallbackContext *tcc = cls;
464 GNUNET_CONTAINER_DLL_remove (tcc_head,
467 msize = ntohs(tcc->msg->size);
471 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
472 "Transmission failed.\n");
475 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
476 if (GNUNET_YES == tcc->end)
477 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
478 GNUNET_SERVER_client_drop (tcc->client);
479 GNUNET_free (tcc->msg);
483 GNUNET_assert (size >= msize);
484 memcpy (buf, tcc->msg, msize);
486 tcc->tc (tcc->tc_cls, GNUNET_OK);
487 if (GNUNET_YES == tcc->end)
489 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495 "Response transmitted, more pending!\n");
498 GNUNET_SERVER_client_drop (tcc->client);
499 GNUNET_free (tcc->msg);
506 * Transmit the given message to the client.
508 * @param client target of the message
509 * @param msg message to transmit, will be freed!
510 * @param tc function to call afterwards
511 * @param tc_cls closure for tc
512 * @param end is this the last response (and we should
513 * signal the server completion accodingly after
514 * transmitting this message)?
517 transmit (struct GNUNET_SERVER_Client *client,
518 struct GNUNET_MessageHeader *msg,
519 TransmitContinuation tc,
523 struct TransmitCallbackContext *tcc;
525 if (GNUNET_YES == cleaning_done)
528 tc (tc_cls, GNUNET_SYSERR);
531 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
533 tcc->client = client;
535 tcc->tc_cls = tc_cls;
538 (tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
540 GNUNET_TIME_UNIT_FOREVER_REL,
545 if (GNUNET_YES == end)
548 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
549 "Disconnecting client.\n");
551 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
554 tc (tc_cls, GNUNET_SYSERR);
559 GNUNET_SERVER_client_keep (client);
560 GNUNET_CONTAINER_DLL_insert (tcc_head,
567 * Transmit a status code to the client.
569 * @param client receiver of the response
570 * @param code status code
571 * @param msg optional error message (can be NULL)
574 transmit_status (struct GNUNET_SERVER_Client *client,
578 struct StatusMessage *sm;
582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
583 "Transmitting `%s' message with value %d and message `%s'\n",
586 msg != NULL ? msg : "(none)");
588 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
589 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
590 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
591 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
592 sm->status = htonl(code);
594 memcpy (&sm[1], msg, slen);
595 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
600 * Function called once the transmit operation has
601 * either failed or succeeded.
603 * @param next_cls closure for calling "next_request" callback
604 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
607 get_next(void *next_cls,
610 if (status != GNUNET_OK)
612 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
613 _("Failed to transmit an item to the client; aborting iteration.\n"));
615 plugin->api->next_request (next_cls, GNUNET_YES);
618 plugin->api->next_request (next_cls, GNUNET_NO);
623 * Function that will transmit the given datastore entry
626 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
627 * @param next_cls closure to use to ask for the next item
628 * @param key key for the content
629 * @param size number of bytes in data
630 * @param data content stored
631 * @param type type of the content
632 * @param priority priority of the content
633 * @param anonymity anonymity-level for the content
634 * @param expiration expiration time for the content
635 * @param uid unique identifier for the datum;
636 * maybe 0 if no unique identifier is available
638 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
639 * GNUNET_NO to delete the item and continue (if supported)
642 transmit_item (void *cls,
644 const GNUNET_HashCode * key,
647 enum GNUNET_BLOCK_Type type,
650 struct GNUNET_TIME_Absolute
651 expiration, uint64_t uid)
653 struct GNUNET_SERVER_Client *client = cls;
654 struct GNUNET_MessageHeader *end;
655 struct DataMessage *dm;
659 /* transmit 'DATA_END' */
661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
662 "Transmitting `%s' message\n",
665 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
666 end->size = htons(sizeof(struct GNUNET_MessageHeader));
667 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
668 transmit (client, end, NULL, NULL, GNUNET_YES);
669 GNUNET_SERVER_client_drop (client);
672 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
673 dm->header.size = htons(sizeof(struct DataMessage) + size);
674 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
676 dm->size = htonl(size);
677 dm->type = htonl(type);
678 dm->priority = htonl(priority);
679 dm->anonymity = htonl(anonymity);
680 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
681 dm->uid = GNUNET_htonll(uid);
683 memcpy (&dm[1], data, size);
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686 "Transmitting `%s' message\n",
689 GNUNET_STATISTICS_update (stats,
690 gettext_noop ("# results found"),
693 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
699 * Handle RESERVE-message.
702 * @param client identification of the client
703 * @param message the actual message
706 handle_reserve (void *cls,
707 struct GNUNET_SERVER_Client *client,
708 const struct GNUNET_MessageHeader *message)
710 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
711 struct ReservationList *e;
712 unsigned long long used;
713 unsigned long long req;
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
719 "Processing `%s' request\n",
722 amount = GNUNET_ntohll(msg->amount);
723 entries = ntohl(msg->entries);
724 used = plugin->api->get_size (plugin->api->cls) + reserved;
725 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
726 if (used + req > quota)
729 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
730 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
731 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
735 if (cache_size < req)
737 /* TODO: document this in the FAQ; essentially, if this
738 message happens, the insertion request could be blocked
739 by less-important content from migration because it is
740 larger than 1/8th of the overall available space, and
741 we only reserve 1/8th for "fresh" insertions */
742 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
743 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
746 transmit_status (client, 0,
747 gettext_noop ("Insufficient space to satisfy request and "
748 "requested amount is larger than cache size"));
752 transmit_status (client, 0,
753 gettext_noop ("Insufficient space to satisfy request"));
758 e = GNUNET_malloc (sizeof(struct ReservationList));
759 e->next = reservations;
763 e->entries = entries;
764 e->rid = ++reservation_gen;
765 if (reservation_gen < 0)
766 reservation_gen = 0; /* wrap around */
767 transmit_status (client, e->rid, NULL);
772 * Handle RELEASE_RESERVE-message.
775 * @param client identification of the client
776 * @param message the actual message
779 handle_release_reserve (void *cls,
780 struct GNUNET_SERVER_Client *client,
781 const struct GNUNET_MessageHeader *message)
783 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
784 struct ReservationList *pos;
785 struct ReservationList *prev;
786 struct ReservationList *next;
787 int rid = ntohl(msg->rid);
788 unsigned long long rem;
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
792 "Processing `%s' request\n",
797 while (NULL != (pos = next))
806 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
807 GNUNET_assert (reserved >= rem);
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
811 "Returning %llu remaining reserved bytes to storage pool\n",
815 transmit_status (client, GNUNET_OK, NULL);
821 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
826 * Check that the given message is a valid data message.
828 * @return NULL if the message is not well-formed, otherwise the message
830 static const struct DataMessage *
831 check_data (const struct GNUNET_MessageHeader *message)
835 const struct DataMessage *dm;
837 size = ntohs(message->size);
838 if (size < sizeof(struct DataMessage))
843 dm = (const struct DataMessage *) message;
844 dsize = ntohl(dm->size);
845 if (size != dsize + sizeof(struct DataMessage))
855 * Handle PUT-message.
858 * @param client identification of the client
859 * @param message the actual message
862 handle_put (void *cls,
863 struct GNUNET_SERVER_Client *client,
864 const struct GNUNET_MessageHeader *message)
866 const struct DataMessage *dm = check_data (message);
870 struct ReservationList *pos;
874 (ntohl(dm->type) == 0) )
877 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
882 "Processing `%s' request for `%s'\n",
884 GNUNET_h2s (&dm->key));
886 rid = ntohl(dm->rid);
887 size = ntohl(dm->size);
891 while ( (NULL != pos) &&
894 GNUNET_break (pos != NULL);
897 GNUNET_break (pos->entries > 0);
898 GNUNET_break (pos->amount > size);
901 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
905 ret = plugin->api->put (plugin->api->cls,
911 ntohl(dm->anonymity),
912 GNUNET_TIME_absolute_ntoh(dm->expiration),
914 if (GNUNET_OK == ret)
916 GNUNET_STATISTICS_update (stats,
917 gettext_noop ("# bytes stored"),
920 GNUNET_CONTAINER_bloomfilter_add (filter,
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
924 "Successfully stored %u bytes under key `%s'\n",
926 GNUNET_h2s (&dm->key));
929 transmit_status (client,
930 (GNUNET_SYSERR == ret) ? GNUNET_SYSERR : GNUNET_OK,
932 GNUNET_free_non_null (msg);
933 if (quota - reserved - cache_size < plugin->api->get_size (plugin->api->cls))
934 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
939 * Handle GET-message.
942 * @param client identification of the client
943 * @param message the actual message
946 handle_get (void *cls,
947 struct GNUNET_SERVER_Client *client,
948 const struct GNUNET_MessageHeader *message)
950 const struct GetMessage *msg;
953 size = ntohs(message->size);
954 if ( (size != sizeof(struct GetMessage)) &&
955 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
958 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
961 msg = (const struct GetMessage*) message;
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "Processing `%s' request for `%s' of type %u\n",
966 GNUNET_h2s (&msg->key),
969 GNUNET_STATISTICS_update (stats,
970 gettext_noop ("# GET requests received"),
973 GNUNET_SERVER_client_keep (client);
974 if ( (size == sizeof(struct GetMessage)) &&
975 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
978 /* don't bother database... */
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981 "Empty result set for `%s' request for `%s'.\n",
983 GNUNET_h2s (&msg->key));
985 GNUNET_STATISTICS_update (stats,
986 gettext_noop ("# requests filtered by bloomfilter"),
989 transmit_item (client,
990 NULL, NULL, 0, NULL, 0, 0, 0,
991 GNUNET_TIME_UNIT_ZERO_ABS, 0);
994 plugin->api->get (plugin->api->cls,
995 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
1004 * Handle UPDATE-message.
1006 * @param cls closure
1007 * @param client identification of the client
1008 * @param message the actual message
1011 handle_update (void *cls,
1012 struct GNUNET_SERVER_Client *client,
1013 const struct GNUNET_MessageHeader *message)
1015 const struct UpdateMessage *msg;
1019 GNUNET_STATISTICS_update (stats,
1020 gettext_noop ("# UPDATE requests received"),
1023 msg = (const struct UpdateMessage*) message;
1026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1027 "Processing `%s' request for %llu\n",
1029 (unsigned long long) GNUNET_ntohll (msg->uid));
1031 ret = plugin->api->update (plugin->api->cls,
1032 GNUNET_ntohll(msg->uid),
1033 (int32_t) ntohl(msg->priority),
1034 GNUNET_TIME_absolute_ntoh(msg->expiration),
1036 transmit_status (client, ret, emsg);
1037 GNUNET_free_non_null (emsg);
1042 * Handle GET_RANDOM-message.
1044 * @param cls closure
1045 * @param client identification of the client
1046 * @param message the actual message
1049 handle_get_random (void *cls,
1050 struct GNUNET_SERVER_Client *client,
1051 const struct GNUNET_MessageHeader *message)
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1055 "Processing `%s' request\n",
1058 GNUNET_STATISTICS_update (stats,
1059 gettext_noop ("# GET RANDOM requests received"),
1062 GNUNET_SERVER_client_keep (client);
1063 plugin->api->iter_migration_order (plugin->api->cls,
1071 * Context for the 'remove_callback'.
1073 struct RemoveContext
1076 * Client for whom we're doing the remvoing.
1078 struct GNUNET_SERVER_Client *client;
1081 * GNUNET_YES if we managed to remove something.
1088 * Callback function that will cause the item that is passed
1089 * in to be deleted (by returning GNUNET_NO).
1092 remove_callback (void *cls,
1094 const GNUNET_HashCode * key,
1097 enum GNUNET_BLOCK_Type type,
1100 struct GNUNET_TIME_Absolute
1101 expiration, uint64_t uid)
1103 struct RemoveContext *rc = cls;
1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1109 "No further matches for `%s' request.\n",
1112 if (GNUNET_YES == rc->found)
1113 transmit_status (rc->client, GNUNET_OK, NULL);
1115 transmit_status (rc->client, GNUNET_NO, _("Content not found"));
1116 GNUNET_SERVER_client_drop (rc->client);
1118 return GNUNET_OK; /* last item */
1120 rc->found = GNUNET_YES;
1122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1123 "Item %llu matches `%s' request for key `%s'.\n",
1124 (unsigned long long) uid,
1128 GNUNET_STATISTICS_update (stats,
1129 gettext_noop ("# bytes removed (explicit request)"),
1132 GNUNET_CONTAINER_bloomfilter_remove (filter,
1134 plugin->api->next_request (next_cls, GNUNET_YES);
1140 * Handle REMOVE-message.
1142 * @param cls closure
1143 * @param client identification of the client
1144 * @param message the actual message
1147 handle_remove (void *cls,
1148 struct GNUNET_SERVER_Client *client,
1149 const struct GNUNET_MessageHeader *message)
1151 const struct DataMessage *dm = check_data (message);
1152 GNUNET_HashCode vhash;
1153 struct RemoveContext *rc;
1158 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1163 "Processing `%s' request for `%s'\n",
1165 GNUNET_h2s (&dm->key));
1167 GNUNET_STATISTICS_update (stats,
1168 gettext_noop ("# REMOVE requests received"),
1171 rc = GNUNET_malloc (sizeof(struct RemoveContext));
1172 GNUNET_SERVER_client_keep (client);
1173 rc->client = client;
1174 GNUNET_CRYPTO_hash (&dm[1],
1177 plugin->api->get (plugin->api->cls,
1187 * Handle DROP-message.
1189 * @param cls closure
1190 * @param client identification of the client
1191 * @param message the actual message
1194 handle_drop (void *cls,
1195 struct GNUNET_SERVER_Client *client,
1196 const struct GNUNET_MessageHeader *message)
1199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1200 "Processing `%s' request\n",
1203 plugin->api->drop (plugin->api->cls);
1204 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1209 * List of handlers for the messages understood by this
1212 static struct GNUNET_SERVER_MessageHandler handlers[] = {
1213 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1214 sizeof(struct ReserveMessage) },
1215 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1216 sizeof(struct ReleaseReserveMessage) },
1217 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1218 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1219 sizeof (struct UpdateMessage) },
1220 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1221 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
1222 sizeof(struct GNUNET_MessageHeader) },
1223 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1224 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1225 sizeof(struct GNUNET_MessageHeader) },
1232 * Load the datastore plugin.
1234 static struct DatastorePlugin *
1237 struct DatastorePlugin *ret;
1242 GNUNET_CONFIGURATION_get_value_string (cfg,
1243 "DATASTORE", "DATABASE", &name))
1245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1246 _("No `%s' specified for `%s' in configuration!\n"),
1251 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1253 ret->env.sched = sched;
1254 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1255 _("Loading `%s' datastore plugin\n"), name);
1256 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1257 ret->short_name = name;
1258 ret->lib_name = libname;
1259 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1260 if (ret->api == NULL)
1262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1263 _("Failed to load datastore plugin for `%s'\n"), name);
1264 GNUNET_free (ret->short_name);
1265 GNUNET_free (libname);
1274 * Function called when the service shuts
1275 * down. Unloads our datastore plugin.
1277 * @param plug plugin to unload
1280 unload_plugin (struct DatastorePlugin *plug)
1283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1284 "Datastore service is unloading plugin...\n");
1286 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1287 GNUNET_free (plug->lib_name);
1288 GNUNET_free (plug->short_name);
1294 * Final task run after shutdown. Unloads plugins and disconnects us from
1298 unload_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1300 unload_plugin (plugin);
1304 GNUNET_CONTAINER_bloomfilter_free (filter);
1307 GNUNET_ARM_stop_services (cfg, tc->sched, "statistics", NULL);
1310 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1317 * Last task run during shutdown. Disconnects us from
1318 * the transport and core.
1321 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1323 struct TransmitCallbackContext *tcc;
1325 cleaning_done = GNUNET_YES;
1326 while (NULL != (tcc = tcc_head))
1328 GNUNET_CONTAINER_DLL_remove (tcc_head,
1331 if (tcc->th != NULL)
1333 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1334 GNUNET_SERVER_client_drop (tcc->client);
1336 if (NULL != tcc->tc)
1337 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
1338 GNUNET_free (tcc->msg);
1341 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1343 GNUNET_SCHEDULER_cancel (sched,
1345 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1347 GNUNET_SCHEDULER_add_continuation (sched,
1350 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1355 * Function that removes all active reservations made
1356 * by the given client and releases the space for other
1359 * @param cls closure
1360 * @param client identification of the client
1363 cleanup_reservations (void *cls,
1364 struct GNUNET_SERVER_Client
1367 struct ReservationList *pos;
1368 struct ReservationList *prev;
1369 struct ReservationList *next;
1378 if (pos->client == client)
1381 reservations = next;
1384 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1397 * Process datastore requests.
1399 * @param cls closure
1400 * @param s scheduler to use
1401 * @param server the initialized server
1402 * @param c configuration to use
1406 struct GNUNET_SCHEDULER_Handle *s,
1407 struct GNUNET_SERVER_Handle *server,
1408 const struct GNUNET_CONFIGURATION_Handle *c)
1411 unsigned int bf_size;
1416 GNUNET_CONFIGURATION_get_value_number (cfg,
1417 "DATASTORE", "QUOTA", "a))
1419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1420 _("No `%s' specified for `%s' in configuration!\n"),
1425 stats = GNUNET_STATISTICS_create (sched, "datastore", cfg);
1426 cache_size = quota / 8; /* Or should we make this an option? */
1427 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1430 GNUNET_CONFIGURATION_get_value_filename (cfg,
1435 GNUNET_DISK_directory_create_for_file (fn)) )
1437 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1438 _("Could not use specified filename `%s' for bloomfilter.\n"),
1439 fn != NULL ? fn : "");
1440 GNUNET_free_non_null (fn);
1443 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1444 GNUNET_free_non_null (fn);
1447 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1448 _("Failed to initialize bloomfilter.\n"));
1451 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1456 GNUNET_ARM_start_services (cfg, sched, "statistics", NULL);
1457 plugin = load_plugin ();
1460 GNUNET_CONTAINER_bloomfilter_free (filter);
1462 GNUNET_ARM_stop_services (cfg, sched, "statistics", NULL);
1465 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1470 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1471 GNUNET_SERVER_add_handlers (server, handlers);
1473 = GNUNET_SCHEDULER_add_with_priority (sched,
1474 GNUNET_SCHEDULER_PRIORITY_IDLE,
1475 &delete_expired, NULL);
1476 GNUNET_SCHEDULER_add_delayed (sched,
1477 GNUNET_TIME_UNIT_FOREVER_REL,
1478 &cleaning_task, NULL);
1484 * The main function for the datastore service.
1486 * @param argc number of arguments from the command line
1487 * @param argv command line arguments
1488 * @return 0 ok, 1 on error
1491 main (int argc, char *const *argv)
1496 GNUNET_SERVICE_run (argc,
1499 GNUNET_SERVICE_OPTION_NONE,
1500 &run, NULL)) ? 0 : 1;
1505 /* end of gnunet-service-datastore.c */