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_protocols.h"
30 #include "plugin_datastore.h"
31 #include "datastore.h"
34 * How many messages do we queue at most per client?
36 #define MAX_PENDING 1024
39 * How long are we at most keeping "expired" content
40 * past the expiration date in the database?
42 #define MAX_EXPIRE_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
47 * Our datastore plugin.
49 struct DatastorePlugin
53 * API of the transport as returned by the plugin's
54 * initialization function.
56 struct GNUNET_DATASTORE_PluginFunctions *api;
59 * Short name for the plugin (i.e. "sqlite").
64 * Name of the library (i.e. "gnunet_plugin_datastore_sqlite").
69 * Environment this transport service is using
72 struct GNUNET_DATASTORE_PluginEnvironment env;
78 * Linked list of active reservations.
80 struct ReservationList
84 * This is a linked list.
86 struct ReservationList *next;
89 * Client that made the reservation.
91 struct GNUNET_SERVER_Client *client;
94 * Number of bytes (still) reserved.
99 * Number of items (still) reserved.
104 * Reservation identifier.
112 * Our datastore plugin (NULL if not available).
114 static struct DatastorePlugin *plugin;
117 * Linked list of space reservations made by clients.
119 static struct ReservationList *reservations;
122 * Bloomfilter to quickly tell if we don't have the content.
124 static struct GNUNET_CONTAINER_BloomFilter *filter;
127 * Static counter to produce reservation identifiers.
129 static int reservation_gen;
132 * How much space are we allowed to use?
134 static unsigned long long quota;
137 * How much space are we using for the cache?
138 * (space available for insertions that will be
139 * instantly reclaimed by discarding less
140 * important content --- or possibly whatever
141 * we just inserted into the "cache").
143 static unsigned long long cache_size;
146 * How much space have we currently reserved?
148 static unsigned long long reserved;
151 * Identity of the task that is used to delete
154 static GNUNET_SCHEDULER_TaskIdentifier expired_kill_task;
159 struct GNUNET_CONFIGURATION_Handle *cfg;
164 struct GNUNET_SCHEDULER_Handle *sched;
167 * Function called once the transmit operation has
168 * either failed or succeeded.
171 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
173 typedef void (*TransmitContinuation)(void *cls,
177 struct TransmitCallbackContext
180 * The message that we're asked to transmit.
182 struct GNUNET_MessageHeader *msg;
185 * Client that we are transmitting to.
187 struct GNUNET_SERVER_Client *client;
190 * Function to call once msg has been transmitted
191 * (or at least added to the buffer).
193 TransmitContinuation tc;
201 * GNUNET_YES if we are supposed to signal the server
202 * completion of the client's request.
209 * Task that is used to remove expired entries from
210 * the datastore. This task will schedule itself
211 * again automatically to always delete all expired
214 * @param cls not used
215 * @param tc task context
218 delete_expired (void *cls,
219 const struct GNUNET_SCHEDULER_TaskContext *tc);
223 * Iterate over the expired items stored in the datastore.
224 * Delete all expired items; once we have processed all
225 * expired items, re-schedule the "delete_expired" task.
227 * @param cls not used
228 * @param next_cls closure to pass to the "next" function.
229 * @param key key for the content
230 * @param size number of bytes in data
231 * @param data content stored
232 * @param type type of the content
233 * @param priority priority of the content
234 * @param anonymity anonymity-level for the content
235 * @param expiration expiration time for the content
236 * @param uid unique identifier for the datum;
237 * maybe 0 if no unique identifier is available
239 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
240 * (continue on call to "next", of course),
241 * GNUNET_NO to delete the item and continue (if supported)
244 expired_processor (void *cls,
246 const GNUNET_HashCode * key,
252 struct GNUNET_TIME_Absolute
256 struct GNUNET_TIME_Absolute now;
258 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
262 = GNUNET_SCHEDULER_add_delayed (sched,
264 GNUNET_SCHEDULER_PRIORITY_IDLE,
265 GNUNET_SCHEDULER_NO_TASK,
269 return GNUNET_SYSERR;
271 now = GNUNET_TIME_absolute_get ();
272 if (expiration.value > now.value)
274 /* finished processing */
275 plugin->api->next_request (next_cls, GNUNET_YES);
276 return GNUNET_SYSERR;
278 plugin->api->next_request (next_cls, GNUNET_NO);
279 return GNUNET_NO; /* delete */
284 * Task that is used to remove expired entries from
285 * the datastore. This task will schedule itself
286 * again automatically to always delete all expired
289 * @param cls not used
290 * @param tc task context
293 delete_expired (void *cls,
294 const struct GNUNET_SCHEDULER_TaskContext *tc)
296 plugin->api->iter_ascending_expiration (plugin->api->cls,
304 * An iterator over a set of items stored in the datastore.
307 * @param next_cls closure to pass to the "next" function.
308 * @param key key for the content
309 * @param size number of bytes in data
310 * @param data content stored
311 * @param type type of the content
312 * @param priority priority of the content
313 * @param anonymity anonymity-level for the content
314 * @param expiration expiration time for the content
315 * @param uid unique identifier for the datum;
316 * maybe 0 if no unique identifier is available
318 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
319 * (continue on call to "next", of course),
320 * GNUNET_NO to delete the item and continue (if supported)
325 const GNUNET_HashCode * key,
331 struct GNUNET_TIME_Absolute
335 unsigned long long *need = cls;
340 return GNUNET_SYSERR;
342 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
345 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
346 plugin->api->next_request (next_cls,
347 (0 == *need) ? GNUNET_YES : GNUNET_NO);
353 * Manage available disk space by running tasks
354 * that will discard content if necessary. This
355 * function will be run whenever a request for
356 * "need" bytes of storage could only be satisfied
357 * by eating into the "cache" (and we want our cache
360 * @param need number of bytes of content that were
361 * placed into the "cache" (and hence the
362 * number of bytes that should be removed).
365 manage_space (unsigned long long need)
367 unsigned long long *n;
369 n = GNUNET_malloc (sizeof(unsigned long long));
371 plugin->api->iter_low_priority (plugin->api->cls,
379 * Function called to notify a client about the socket
380 * begin ready to queue more data. "buf" will be
381 * NULL and "size" zero if the socket was closed for
382 * writing in the meantime.
385 * @param size number of bytes available in buf
386 * @param buf where the callee should write the message
387 * @return number of bytes written to buf
390 transmit_callback (void *cls,
391 size_t size, void *buf)
393 struct TransmitCallbackContext *tcc = cls;
396 msize = ntohs(tcc->msg->size);
400 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
401 "Transmission failed.\n");
404 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
405 if (GNUNET_YES == tcc->end)
407 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
409 GNUNET_free (tcc->msg);
413 GNUNET_assert (size >= msize);
414 memcpy (buf, tcc->msg, msize);
416 tcc->tc (tcc->tc_cls, GNUNET_OK);
417 if (GNUNET_YES == tcc->end)
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "Request completed, ready for the next request!\n");
423 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "Response transmitted, more pending!\n");
432 GNUNET_free (tcc->msg);
439 * Transmit the given message to the client.
441 * @param client target of the message
442 * @param msg message to transmit, will be freed!
443 * @param end is this the last response (and we should
444 * signal the server completion accodingly after
445 * transmitting this message)?
448 transmit (struct GNUNET_SERVER_Client *client,
449 struct GNUNET_MessageHeader *msg,
450 TransmitContinuation tc,
454 struct TransmitCallbackContext *tcc;
456 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
458 tcc->client = client;
460 tcc->tc_cls = tc_cls;
464 GNUNET_SERVER_notify_transmit_ready (client,
466 GNUNET_TIME_UNIT_FOREVER_REL,
471 if (GNUNET_YES == end)
474 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
475 "Disconnecting client.\n");
477 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
480 tc (tc_cls, GNUNET_SYSERR);
488 * Transmit a status code to the client.
490 * @param client receiver of the response
491 * @param code status code
492 * @param msg optional error message (can be NULL)
495 transmit_status (struct GNUNET_SERVER_Client *client,
499 struct StatusMessage *sm;
503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
504 "Transmitting `%s' message with value %d and message %s\n",
507 msg != NULL ? msg : "(none)");
509 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
510 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
511 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
512 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
513 sm->status = htonl(code);
514 memcpy (&sm[1], msg, slen);
515 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
520 * Function called once the transmit operation has
521 * either failed or succeeded.
524 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
527 get_next(void *next_cls,
530 if (status != GNUNET_OK)
532 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
533 _("Failed to transmit an item to the client; aborting iteration.\n"));
534 plugin->api->next_request (next_cls, GNUNET_YES);
537 plugin->api->next_request (next_cls, GNUNET_NO);
542 * Function that will transmit the given datastore entry
545 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
546 * @param next_cls closure to use to ask for the next item
547 * @param key key for the content
548 * @param size number of bytes in data
549 * @param data content stored
550 * @param type type of the content
551 * @param priority priority of the content
552 * @param anonymity anonymity-level for the content
553 * @param expiration expiration time for the content
554 * @param uid unique identifier for the datum;
555 * maybe 0 if no unique identifier is available
557 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
558 * GNUNET_NO to delete the item and continue (if supported)
561 transmit_item (void *cls,
563 const GNUNET_HashCode * key,
569 struct GNUNET_TIME_Absolute
570 expiration, uint64_t uid)
572 struct GNUNET_SERVER_Client *client = cls;
573 struct GNUNET_MessageHeader *end;
574 struct DataMessage *dm;
578 /* transmit 'DATA_END' */
580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
581 "Transmitting `%s' message\n",
584 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
585 end->size = htons(sizeof(struct GNUNET_MessageHeader));
586 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
587 transmit (client, end, NULL, NULL, GNUNET_YES);
588 GNUNET_SERVER_client_drop (client);
591 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
592 dm->header.size = htons(sizeof(struct DataMessage) + size);
593 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
595 dm->size = htonl(size);
596 dm->type = htonl(type);
597 dm->priority = htonl(priority);
598 dm->anonymity = htonl(anonymity);
599 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
600 dm->uid = GNUNET_htonll(uid);
602 memcpy (&dm[1], data, size);
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
605 "Transmitting `%s' message\n",
608 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
614 * Handle RESERVE-message.
617 * @param client identification of the client
618 * @param message the actual message
621 handle_reserve (void *cls,
622 struct GNUNET_SERVER_Client *client,
623 const struct GNUNET_MessageHeader *message)
625 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
626 struct ReservationList *e;
627 unsigned long long used;
628 unsigned long long req;
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
634 "Processing `%s' request\n",
637 amount = ntohl(msg->amount);
638 entries = GNUNET_ntohll(msg->entries);
639 used = plugin->api->get_size (plugin->api->cls) + reserved;
640 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
641 if (used + req > quota)
644 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
645 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
646 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
650 if (cache_size < req)
652 /* TODO: document this in the FAQ; essentially, if this
653 message happens, the insertion request could be blocked
654 by less-important content from migration because it is
655 larger than 1/8th of the overall available space, and
656 we only reserve 1/8th for "fresh" insertions */
657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
658 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
661 transmit_status (client, 0,
662 gettext_noop ("Insufficient space to satisfy request and "
663 "requested amount is larger than cache size"));
667 transmit_status (client, 0,
668 gettext_noop ("Insufficient space to satisfy request"));
673 e = GNUNET_malloc (sizeof(struct ReservationList));
674 e->next = reservations;
678 e->entries = entries;
679 e->rid = ++reservation_gen;
680 if (reservation_gen < 0)
681 reservation_gen = 0; /* wrap around */
682 transmit_status (client, e->rid, NULL);
687 * Handle RELEASE_RESERVE-message.
690 * @param client identification of the client
691 * @param message the actual message
694 handle_release_reserve (void *cls,
695 struct GNUNET_SERVER_Client *client,
696 const struct GNUNET_MessageHeader *message)
698 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
699 struct ReservationList *pos;
700 struct ReservationList *prev;
701 struct ReservationList *next;
702 int rid = ntohl(msg->rid);
703 unsigned long long rem;
706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
707 "Processing `%s' request\n",
712 while (NULL != (pos = next))
721 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
722 GNUNET_assert (reserved >= rem);
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726 "Returning %llu remaining reserved bytes to storage pool\n",
730 transmit_status (client, GNUNET_OK, NULL);
737 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
742 * Check that the given message is a valid data message.
744 * @return NULL if the message is not well-formed, otherwise the message
746 static const struct DataMessage *
747 check_data (const struct GNUNET_MessageHeader *message)
751 const struct DataMessage *dm;
753 size = ntohs(message->size);
754 if (size < sizeof(struct DataMessage))
759 dm = (const struct DataMessage *) message;
760 dsize = ntohl(dm->size);
761 if (size != dsize + sizeof(struct DataMessage))
771 * Handle PUT-message.
774 * @param client identification of the client
775 * @param message the actual message
778 handle_put (void *cls,
779 struct GNUNET_SERVER_Client *client,
780 const struct GNUNET_MessageHeader *message)
782 const struct DataMessage *dm = check_data (message);
786 struct ReservationList *pos;
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
791 "Processing `%s' request\n",
794 if (ntohl(dm->type) == 0)
802 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
805 rid = ntohl(dm->rid);
806 size = ntohl(dm->size);
810 while ( (NULL != pos) &&
813 GNUNET_break (pos != NULL);
816 GNUNET_break (pos->entries > 0);
817 GNUNET_break (pos->amount > size);
820 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
824 ret = plugin->api->put (plugin->api->cls,
830 ntohl(dm->anonymity),
831 GNUNET_TIME_absolute_ntoh(dm->expiration),
833 if (GNUNET_OK == ret)
834 GNUNET_CONTAINER_bloomfilter_add (filter,
836 transmit_status (client,
837 (GNUNET_SYSERR == ret) ? GNUNET_SYSERR : GNUNET_OK,
839 GNUNET_free_non_null (msg);
840 if (quota - reserved - cache_size < plugin->api->get_size (plugin->api->cls))
841 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
846 * Handle GET-message.
849 * @param client identification of the client
850 * @param message the actual message
853 handle_get (void *cls,
854 struct GNUNET_SERVER_Client *client,
855 const struct GNUNET_MessageHeader *message)
857 static struct GNUNET_TIME_Absolute zero;
858 const struct GetMessage *msg;
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863 "Processing `%s' request\n",
866 size = ntohs(message->size);
867 if ( (size != sizeof(struct GetMessage)) &&
868 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
871 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
874 msg = (const struct GetMessage*) message;
875 if ( (size == sizeof(struct GetMessage)) &&
876 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
879 /* don't bother database... */
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
882 "Empty result set for `%s' request.\n",
885 transmit_item (client,
886 NULL, NULL, 0, NULL, 0, 0, 0, zero, 0);
889 GNUNET_SERVER_client_keep (client);
890 plugin->api->get (plugin->api->cls,
891 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
900 * Handle UPDATE-message.
903 * @param client identification of the client
904 * @param message the actual message
907 handle_update (void *cls,
908 struct GNUNET_SERVER_Client *client,
909 const struct GNUNET_MessageHeader *message)
911 const struct UpdateMessage *msg;
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917 "Processing `%s' request\n",
920 msg = (const struct UpdateMessage*) message;
922 ret = plugin->api->update (plugin->api->cls,
923 GNUNET_ntohll(msg->uid),
924 (int32_t) ntohl(msg->priority),
925 GNUNET_TIME_absolute_ntoh(msg->expiration),
927 transmit_status (client, ret, emsg);
928 GNUNET_free_non_null (emsg);
933 * Handle GET_RANDOM-message.
936 * @param client identification of the client
937 * @param message the actual message
940 handle_get_random (void *cls,
941 struct GNUNET_SERVER_Client *client,
942 const struct GNUNET_MessageHeader *message)
945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
946 "Processing `%s' request\n",
949 GNUNET_SERVER_client_keep (client);
950 plugin->api->iter_migration_order (plugin->api->cls,
958 * Context for the 'remove_callback'.
963 * Client for whom we're doing the remvoing.
965 struct GNUNET_SERVER_Client *client;
968 * GNUNET_YES if we managed to remove something.
975 * Callback function that will cause the item that is passed
976 * in to be deleted (by returning GNUNET_NO).
979 remove_callback (void *cls,
981 const GNUNET_HashCode * key,
987 struct GNUNET_TIME_Absolute
988 expiration, uint64_t uid)
990 struct RemoveContext *rc = cls;
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
996 "No further matches for `%s' request.\n",
999 if (GNUNET_YES == rc->found)
1000 transmit_status (rc->client, GNUNET_OK, NULL);
1002 transmit_status (rc->client, GNUNET_SYSERR, _("Content not found"));
1003 GNUNET_SERVER_client_drop (rc->client);
1005 return GNUNET_OK; /* last item */
1007 rc->found = GNUNET_YES;
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010 "Item %llu matches `%s' request.\n",
1011 (unsigned long long) uid,
1014 GNUNET_CONTAINER_bloomfilter_remove (filter,
1016 plugin->api->next_request (next_cls, GNUNET_YES);
1022 * Handle REMOVE-message.
1024 * @param cls closure
1025 * @param client identification of the client
1026 * @param message the actual message
1029 handle_remove (void *cls,
1030 struct GNUNET_SERVER_Client *client,
1031 const struct GNUNET_MessageHeader *message)
1033 const struct DataMessage *dm = check_data (message);
1034 GNUNET_HashCode vhash;
1035 struct RemoveContext *rc;
1038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1039 "Processing `%s' request\n",
1045 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1048 rc = GNUNET_malloc (sizeof(struct RemoveContext));
1049 GNUNET_SERVER_client_keep (client);
1050 rc->client = client;
1051 GNUNET_CRYPTO_hash (&dm[1],
1054 GNUNET_SERVER_client_keep (client);
1055 plugin->api->get (plugin->api->cls,
1065 * Handle DROP-message.
1067 * @param cls closure
1068 * @param client identification of the client
1069 * @param message the actual message
1072 handle_drop (void *cls,
1073 struct GNUNET_SERVER_Client *client,
1074 const struct GNUNET_MessageHeader *message)
1077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1078 "Processing `%s' request\n",
1081 plugin->api->drop (plugin->api->cls);
1082 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1087 * List of handlers for the messages understood by this
1090 static struct GNUNET_SERVER_MessageHandler handlers[] = {
1091 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1092 sizeof(struct ReserveMessage) },
1093 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1094 sizeof(struct ReleaseReserveMessage) },
1095 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1096 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1097 sizeof (struct UpdateMessage) },
1098 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1099 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
1100 sizeof(struct GNUNET_MessageHeader) },
1101 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1102 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1103 sizeof(struct GNUNET_MessageHeader) },
1110 * Load the datastore plugin.
1112 static struct DatastorePlugin *
1115 struct DatastorePlugin *ret;
1120 GNUNET_CONFIGURATION_get_value_string (cfg,
1121 "DATASTORE", "DATABASE", &name))
1123 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1124 _("No `%s' specified for `%s' in configuration!\n"),
1129 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1131 ret->env.sched = sched;
1132 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1133 _("Loading `%s' datastore plugin\n"), name);
1134 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1135 ret->short_name = name;
1136 ret->lib_name = libname;
1137 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1138 if (ret->api == NULL)
1140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1141 _("Failed to load datastore plugin for `%s'\n"), name);
1142 GNUNET_free (ret->short_name);
1143 GNUNET_free (libname);
1152 * Function called when the service shuts
1153 * down. Unloads our datastore plugin.
1155 * @param plug plugin to unload
1158 unload_plugin (struct DatastorePlugin *plug)
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1162 "Datastore service is unloading plugin...\n");
1164 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1165 GNUNET_free (plug->lib_name);
1166 GNUNET_free (plug->short_name);
1172 * Last task run during shutdown. Disconnects us from
1173 * the transport and core.
1176 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1178 unload_plugin (plugin);
1184 * Function that removes all active reservations made
1185 * by the given client and releases the space for other
1188 * @param cls closure
1189 * @param client identification of the client
1192 cleanup_reservations (void *cls,
1193 struct GNUNET_SERVER_Client
1196 struct ReservationList *pos;
1197 struct ReservationList *prev;
1198 struct ReservationList *next;
1205 if (pos->client == client)
1208 reservations = next;
1211 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1224 * Process datastore requests.
1226 * @param cls closure
1227 * @param s scheduler to use
1228 * @param server the initialized server
1229 * @param c configuration to use
1233 struct GNUNET_SCHEDULER_Handle *s,
1234 struct GNUNET_SERVER_Handle *server,
1235 struct GNUNET_CONFIGURATION_Handle *c)
1238 unsigned int bf_size;
1243 GNUNET_CONFIGURATION_get_value_number (cfg,
1244 "DATASTORE", "QUOTA", "a))
1246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1247 _("No `%s' specified for `%s' in configuration!\n"),
1252 cache_size = quota / 8; /* Or should we make this an option? */
1253 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1256 GNUNET_CONFIGURATION_get_value_filename (cfg,
1261 GNUNET_DISK_directory_create_for_file (fn)) )
1263 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1264 _("Could not use specified filename `%s' for bloomfilter.\n"),
1265 fn != NULL ? fn : "");
1266 GNUNET_free_non_null (fn);
1269 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1270 GNUNET_free_non_null (fn);
1273 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1274 _("Failed to initialize bloomfilter.\n"));
1277 plugin = load_plugin ();
1280 GNUNET_CONTAINER_bloomfilter_free (filter);
1283 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1284 GNUNET_SERVER_add_handlers (server, handlers);
1286 = GNUNET_SCHEDULER_add_delayed (sched,
1288 GNUNET_SCHEDULER_PRIORITY_IDLE,
1289 GNUNET_SCHEDULER_NO_TASK,
1290 GNUNET_TIME_UNIT_ZERO,
1291 &delete_expired, NULL);
1292 GNUNET_SCHEDULER_add_delayed (sched,
1294 GNUNET_SCHEDULER_PRIORITY_IDLE,
1295 GNUNET_SCHEDULER_NO_TASK,
1296 GNUNET_TIME_UNIT_FOREVER_REL,
1297 &cleaning_task, NULL);
1303 * The main function for the datastore service.
1305 * @param argc number of arguments from the command line
1306 * @param argv command line arguments
1307 * @return 0 ok, 1 on error
1310 main (int argc, char *const *argv)
1315 GNUNET_SERVICE_run (argc,
1317 "datastore", &run, NULL, NULL, NULL)) ? 0 : 1;
1322 /* end of gnunet-service-datastore.c */