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? (space available for
138 * insertions that will be instantly reclaimed by discarding less
139 * important content --- or possibly whatever we just inserted into
142 static unsigned long long cache_size;
145 * How much space have we currently reserved?
147 static unsigned long long reserved;
150 * Identity of the task that is used to delete
153 static GNUNET_SCHEDULER_TaskIdentifier expired_kill_task;
158 const struct GNUNET_CONFIGURATION_Handle *cfg;
163 struct GNUNET_SCHEDULER_Handle *sched;
166 * Function called once the transmit operation has
167 * either failed or succeeded.
170 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
172 typedef void (*TransmitContinuation)(void *cls,
176 struct TransmitCallbackContext
179 * The message that we're asked to transmit.
181 struct GNUNET_MessageHeader *msg;
184 * Client that we are transmitting to.
186 struct GNUNET_SERVER_Client *client;
189 * Function to call once msg has been transmitted
190 * (or at least added to the buffer).
192 TransmitContinuation tc;
200 * GNUNET_YES if we are supposed to signal the server
201 * completion of the client's request.
208 * Task that is used to remove expired entries from
209 * the datastore. This task will schedule itself
210 * again automatically to always delete all expired
213 * @param cls not used
214 * @param tc task context
217 delete_expired (void *cls,
218 const struct GNUNET_SCHEDULER_TaskContext *tc);
222 * Iterate over the expired items stored in the datastore.
223 * Delete all expired items; once we have processed all
224 * expired items, re-schedule the "delete_expired" task.
226 * @param cls not used
227 * @param next_cls closure to pass to the "next" function.
228 * @param key key for the content
229 * @param size number of bytes in data
230 * @param data content stored
231 * @param type type of the content
232 * @param priority priority of the content
233 * @param anonymity anonymity-level for the content
234 * @param expiration expiration time for the content
235 * @param uid unique identifier for the datum;
236 * maybe 0 if no unique identifier is available
238 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
239 * (continue on call to "next", of course),
240 * GNUNET_NO to delete the item and continue (if supported)
243 expired_processor (void *cls,
245 const GNUNET_HashCode * key,
251 struct GNUNET_TIME_Absolute
255 struct GNUNET_TIME_Absolute now;
257 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
261 = GNUNET_SCHEDULER_add_delayed (sched,
263 GNUNET_SCHEDULER_PRIORITY_IDLE,
264 GNUNET_SCHEDULER_NO_TASK,
268 return GNUNET_SYSERR;
270 now = GNUNET_TIME_absolute_get ();
271 if (expiration.value > now.value)
273 /* finished processing */
274 plugin->api->next_request (next_cls, GNUNET_YES);
275 return GNUNET_SYSERR;
277 plugin->api->next_request (next_cls, GNUNET_NO);
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 "Deleting content that expired %llu ms ago\n",
281 (unsigned long long) (now.value - expiration.value));
283 GNUNET_CONTAINER_bloomfilter_remove (filter,
285 return GNUNET_NO; /* delete */
290 * Task that is used to remove expired entries from
291 * the datastore. This task will schedule itself
292 * again automatically to always delete all expired
295 * @param cls not used
296 * @param tc task context
299 delete_expired (void *cls,
300 const struct GNUNET_SCHEDULER_TaskContext *tc)
302 plugin->api->iter_ascending_expiration (plugin->api->cls,
310 * An iterator over a set of items stored in the datastore.
313 * @param next_cls closure to pass to the "next" function.
314 * @param key key for the content
315 * @param size number of bytes in data
316 * @param data content stored
317 * @param type type of the content
318 * @param priority priority of the content
319 * @param anonymity anonymity-level for the content
320 * @param expiration expiration time for the content
321 * @param uid unique identifier for the datum;
322 * maybe 0 if no unique identifier is available
324 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
325 * (continue on call to "next", of course),
326 * GNUNET_NO to delete the item and continue (if supported)
331 const GNUNET_HashCode * key,
337 struct GNUNET_TIME_Absolute
341 unsigned long long *need = cls;
346 return GNUNET_SYSERR;
348 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
351 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
352 plugin->api->next_request (next_cls,
353 (0 == *need) ? GNUNET_YES : GNUNET_NO);
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "Deleting %llu bytes of low-priority content (still trying to free another %llu bytes)\n",
357 size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
360 GNUNET_CONTAINER_bloomfilter_remove (filter,
367 * Manage available disk space by running tasks
368 * that will discard content if necessary. This
369 * function will be run whenever a request for
370 * "need" bytes of storage could only be satisfied
371 * by eating into the "cache" (and we want our cache
374 * @param need number of bytes of content that were
375 * placed into the "cache" (and hence the
376 * number of bytes that should be removed).
379 manage_space (unsigned long long need)
381 unsigned long long *n;
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "Asked to free up %llu bytes of cache space\n",
388 n = GNUNET_malloc (sizeof(unsigned long long));
390 plugin->api->iter_low_priority (plugin->api->cls,
398 * Function called to notify a client about the socket
399 * begin ready to queue more data. "buf" will be
400 * NULL and "size" zero if the socket was closed for
401 * writing in the meantime.
404 * @param size number of bytes available in buf
405 * @param buf where the callee should write the message
406 * @return number of bytes written to buf
409 transmit_callback (void *cls,
410 size_t size, void *buf)
412 struct TransmitCallbackContext *tcc = cls;
415 msize = ntohs(tcc->msg->size);
419 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
420 "Transmission failed.\n");
423 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
424 if (GNUNET_YES == tcc->end)
426 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
428 GNUNET_free (tcc->msg);
432 GNUNET_assert (size >= msize);
433 memcpy (buf, tcc->msg, msize);
435 tcc->tc (tcc->tc_cls, GNUNET_OK);
436 if (GNUNET_YES == tcc->end)
438 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
444 "Response transmitted, more pending!\n");
447 GNUNET_free (tcc->msg);
454 * Transmit the given message to the client.
456 * @param client target of the message
457 * @param msg message to transmit, will be freed!
458 * @param tc function to call afterwards
459 * @param tc_cls closure for tc
460 * @param end is this the last response (and we should
461 * signal the server completion accodingly after
462 * transmitting this message)?
465 transmit (struct GNUNET_SERVER_Client *client,
466 struct GNUNET_MessageHeader *msg,
467 TransmitContinuation tc,
471 struct TransmitCallbackContext *tcc;
473 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
475 tcc->client = client;
477 tcc->tc_cls = tc_cls;
481 GNUNET_SERVER_notify_transmit_ready (client,
483 GNUNET_TIME_UNIT_FOREVER_REL,
488 if (GNUNET_YES == end)
491 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
492 "Disconnecting client.\n");
494 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
497 tc (tc_cls, GNUNET_SYSERR);
505 * Transmit a status code to the client.
507 * @param client receiver of the response
508 * @param code status code
509 * @param msg optional error message (can be NULL)
512 transmit_status (struct GNUNET_SERVER_Client *client,
516 struct StatusMessage *sm;
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
521 "Transmitting `%s' message with value %d and message `%s'\n",
524 msg != NULL ? msg : "(none)");
526 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
527 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
528 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
529 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
530 sm->status = htonl(code);
532 memcpy (&sm[1], msg, slen);
533 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
538 * Function called once the transmit operation has
539 * either failed or succeeded.
541 * @param next_cls closure for calling "next_request" callback
542 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
545 get_next(void *next_cls,
548 if (status != GNUNET_OK)
550 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
551 _("Failed to transmit an item to the client; aborting iteration.\n"));
552 plugin->api->next_request (next_cls, GNUNET_YES);
555 plugin->api->next_request (next_cls, GNUNET_NO);
560 * Function that will transmit the given datastore entry
563 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
564 * @param next_cls closure to use to ask for the next item
565 * @param key key for the content
566 * @param size number of bytes in data
567 * @param data content stored
568 * @param type type of the content
569 * @param priority priority of the content
570 * @param anonymity anonymity-level for the content
571 * @param expiration expiration time for the content
572 * @param uid unique identifier for the datum;
573 * maybe 0 if no unique identifier is available
575 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
576 * GNUNET_NO to delete the item and continue (if supported)
579 transmit_item (void *cls,
581 const GNUNET_HashCode * key,
587 struct GNUNET_TIME_Absolute
588 expiration, uint64_t uid)
590 struct GNUNET_SERVER_Client *client = cls;
591 struct GNUNET_MessageHeader *end;
592 struct DataMessage *dm;
596 /* transmit 'DATA_END' */
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
599 "Transmitting `%s' message\n",
602 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
603 end->size = htons(sizeof(struct GNUNET_MessageHeader));
604 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
605 transmit (client, end, NULL, NULL, GNUNET_YES);
606 GNUNET_SERVER_client_drop (client);
609 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
610 dm->header.size = htons(sizeof(struct DataMessage) + size);
611 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
613 dm->size = htonl(size);
614 dm->type = htonl(type);
615 dm->priority = htonl(priority);
616 dm->anonymity = htonl(anonymity);
617 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
618 dm->uid = GNUNET_htonll(uid);
620 memcpy (&dm[1], data, size);
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
623 "Transmitting `%s' message\n",
626 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
632 * Handle RESERVE-message.
635 * @param client identification of the client
636 * @param message the actual message
639 handle_reserve (void *cls,
640 struct GNUNET_SERVER_Client *client,
641 const struct GNUNET_MessageHeader *message)
643 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
644 struct ReservationList *e;
645 unsigned long long used;
646 unsigned long long req;
651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
652 "Processing `%s' request\n",
655 amount = GNUNET_ntohll(msg->amount);
656 entries = ntohl(msg->entries);
657 used = plugin->api->get_size (plugin->api->cls) + reserved;
658 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
659 if (used + req > quota)
662 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
663 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
664 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
668 if (cache_size < req)
670 /* TODO: document this in the FAQ; essentially, if this
671 message happens, the insertion request could be blocked
672 by less-important content from migration because it is
673 larger than 1/8th of the overall available space, and
674 we only reserve 1/8th for "fresh" insertions */
675 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
676 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
679 transmit_status (client, 0,
680 gettext_noop ("Insufficient space to satisfy request and "
681 "requested amount is larger than cache size"));
685 transmit_status (client, 0,
686 gettext_noop ("Insufficient space to satisfy request"));
691 e = GNUNET_malloc (sizeof(struct ReservationList));
692 e->next = reservations;
696 e->entries = entries;
697 e->rid = ++reservation_gen;
698 if (reservation_gen < 0)
699 reservation_gen = 0; /* wrap around */
700 transmit_status (client, e->rid, NULL);
705 * Handle RELEASE_RESERVE-message.
708 * @param client identification of the client
709 * @param message the actual message
712 handle_release_reserve (void *cls,
713 struct GNUNET_SERVER_Client *client,
714 const struct GNUNET_MessageHeader *message)
716 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
717 struct ReservationList *pos;
718 struct ReservationList *prev;
719 struct ReservationList *next;
720 int rid = ntohl(msg->rid);
721 unsigned long long rem;
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
725 "Processing `%s' request\n",
730 while (NULL != (pos = next))
739 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
740 GNUNET_assert (reserved >= rem);
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
744 "Returning %llu remaining reserved bytes to storage pool\n",
748 transmit_status (client, GNUNET_OK, NULL);
754 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
759 * Check that the given message is a valid data message.
761 * @return NULL if the message is not well-formed, otherwise the message
763 static const struct DataMessage *
764 check_data (const struct GNUNET_MessageHeader *message)
768 const struct DataMessage *dm;
770 size = ntohs(message->size);
771 if (size < sizeof(struct DataMessage))
776 dm = (const struct DataMessage *) message;
777 dsize = ntohl(dm->size);
778 if (size != dsize + sizeof(struct DataMessage))
788 * Handle PUT-message.
791 * @param client identification of the client
792 * @param message the actual message
795 handle_put (void *cls,
796 struct GNUNET_SERVER_Client *client,
797 const struct GNUNET_MessageHeader *message)
799 const struct DataMessage *dm = check_data (message);
803 struct ReservationList *pos;
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
808 "Processing `%s' request\n",
811 if (ntohl(dm->type) == 0)
819 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
822 rid = ntohl(dm->rid);
823 size = ntohl(dm->size);
827 while ( (NULL != pos) &&
830 GNUNET_break (pos != NULL);
833 GNUNET_break (pos->entries > 0);
834 GNUNET_break (pos->amount > size);
837 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
841 ret = plugin->api->put (plugin->api->cls,
847 ntohl(dm->anonymity),
848 GNUNET_TIME_absolute_ntoh(dm->expiration),
850 if (GNUNET_OK == ret)
852 GNUNET_CONTAINER_bloomfilter_add (filter,
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
856 "Successfully stored %u bytes under key `%s'\n",
858 GNUNET_h2s (&dm->key));
861 transmit_status (client,
862 (GNUNET_SYSERR == ret) ? GNUNET_SYSERR : GNUNET_OK,
864 GNUNET_free_non_null (msg);
865 if (quota - reserved - cache_size < plugin->api->get_size (plugin->api->cls))
866 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
871 * Handle GET-message.
874 * @param client identification of the client
875 * @param message the actual message
878 handle_get (void *cls,
879 struct GNUNET_SERVER_Client *client,
880 const struct GNUNET_MessageHeader *message)
882 const struct GetMessage *msg;
886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
887 "Processing `%s' request\n",
890 size = ntohs(message->size);
891 if ( (size != sizeof(struct GetMessage)) &&
892 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
898 msg = (const struct GetMessage*) message;
899 if ( (size == sizeof(struct GetMessage)) &&
900 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
903 /* don't bother database... */
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "Empty result set for `%s' request for `%s'.\n",
908 GNUNET_h2s (&msg->key));
910 GNUNET_SERVER_client_keep (client);
911 transmit_item (client,
912 NULL, NULL, 0, NULL, 0, 0, 0,
913 GNUNET_TIME_UNIT_ZERO_ABS, 0);
916 GNUNET_SERVER_client_keep (client);
917 plugin->api->get (plugin->api->cls,
918 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
927 * Handle UPDATE-message.
930 * @param client identification of the client
931 * @param message the actual message
934 handle_update (void *cls,
935 struct GNUNET_SERVER_Client *client,
936 const struct GNUNET_MessageHeader *message)
938 const struct UpdateMessage *msg;
943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944 "Processing `%s' request\n",
947 msg = (const struct UpdateMessage*) message;
949 ret = plugin->api->update (plugin->api->cls,
950 GNUNET_ntohll(msg->uid),
951 (int32_t) ntohl(msg->priority),
952 GNUNET_TIME_absolute_ntoh(msg->expiration),
954 transmit_status (client, ret, emsg);
955 GNUNET_free_non_null (emsg);
960 * Handle GET_RANDOM-message.
963 * @param client identification of the client
964 * @param message the actual message
967 handle_get_random (void *cls,
968 struct GNUNET_SERVER_Client *client,
969 const struct GNUNET_MessageHeader *message)
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 "Processing `%s' request\n",
976 GNUNET_SERVER_client_keep (client);
977 plugin->api->iter_migration_order (plugin->api->cls,
985 * Context for the 'remove_callback'.
990 * Client for whom we're doing the remvoing.
992 struct GNUNET_SERVER_Client *client;
995 * GNUNET_YES if we managed to remove something.
1002 * Callback function that will cause the item that is passed
1003 * in to be deleted (by returning GNUNET_NO).
1006 remove_callback (void *cls,
1008 const GNUNET_HashCode * key,
1014 struct GNUNET_TIME_Absolute
1015 expiration, uint64_t uid)
1017 struct RemoveContext *rc = cls;
1022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1023 "No further matches for `%s' request.\n",
1026 if (GNUNET_YES == rc->found)
1027 transmit_status (rc->client, GNUNET_OK, NULL);
1029 transmit_status (rc->client, GNUNET_NO, _("Content not found"));
1030 GNUNET_SERVER_client_drop (rc->client);
1032 return GNUNET_OK; /* last item */
1034 rc->found = GNUNET_YES;
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1037 "Item %llu matches `%s' request.\n",
1038 (unsigned long long) uid,
1041 GNUNET_CONTAINER_bloomfilter_remove (filter,
1043 plugin->api->next_request (next_cls, GNUNET_YES);
1049 * Handle REMOVE-message.
1051 * @param cls closure
1052 * @param client identification of the client
1053 * @param message the actual message
1056 handle_remove (void *cls,
1057 struct GNUNET_SERVER_Client *client,
1058 const struct GNUNET_MessageHeader *message)
1060 const struct DataMessage *dm = check_data (message);
1061 GNUNET_HashCode vhash;
1062 struct RemoveContext *rc;
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1066 "Processing `%s' request\n",
1072 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1075 rc = GNUNET_malloc (sizeof(struct RemoveContext));
1076 GNUNET_SERVER_client_keep (client);
1077 rc->client = client;
1078 GNUNET_CRYPTO_hash (&dm[1],
1081 plugin->api->get (plugin->api->cls,
1091 * Handle DROP-message.
1093 * @param cls closure
1094 * @param client identification of the client
1095 * @param message the actual message
1098 handle_drop (void *cls,
1099 struct GNUNET_SERVER_Client *client,
1100 const struct GNUNET_MessageHeader *message)
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1104 "Processing `%s' request\n",
1107 plugin->api->drop (plugin->api->cls);
1108 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1113 * List of handlers for the messages understood by this
1116 static struct GNUNET_SERVER_MessageHandler handlers[] = {
1117 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1118 sizeof(struct ReserveMessage) },
1119 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1120 sizeof(struct ReleaseReserveMessage) },
1121 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1122 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1123 sizeof (struct UpdateMessage) },
1124 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1125 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
1126 sizeof(struct GNUNET_MessageHeader) },
1127 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1128 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1129 sizeof(struct GNUNET_MessageHeader) },
1136 * Load the datastore plugin.
1138 static struct DatastorePlugin *
1141 struct DatastorePlugin *ret;
1146 GNUNET_CONFIGURATION_get_value_string (cfg,
1147 "DATASTORE", "DATABASE", &name))
1149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1150 _("No `%s' specified for `%s' in configuration!\n"),
1155 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1157 ret->env.sched = sched;
1158 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1159 _("Loading `%s' datastore plugin\n"), name);
1160 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1161 ret->short_name = name;
1162 ret->lib_name = libname;
1163 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1164 if (ret->api == NULL)
1166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1167 _("Failed to load datastore plugin for `%s'\n"), name);
1168 GNUNET_free (ret->short_name);
1169 GNUNET_free (libname);
1178 * Function called when the service shuts
1179 * down. Unloads our datastore plugin.
1181 * @param plug plugin to unload
1184 unload_plugin (struct DatastorePlugin *plug)
1187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1188 "Datastore service is unloading plugin...\n");
1190 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1191 GNUNET_free (plug->lib_name);
1192 GNUNET_free (plug->short_name);
1198 * Last task run during shutdown. Disconnects us from
1199 * the transport and core.
1202 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1204 unload_plugin (plugin);
1208 GNUNET_CONTAINER_bloomfilter_free (filter);
1215 * Function that removes all active reservations made
1216 * by the given client and releases the space for other
1219 * @param cls closure
1220 * @param client identification of the client
1223 cleanup_reservations (void *cls,
1224 struct GNUNET_SERVER_Client
1227 struct ReservationList *pos;
1228 struct ReservationList *prev;
1229 struct ReservationList *next;
1236 if (pos->client == client)
1239 reservations = next;
1242 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1255 * Process datastore requests.
1257 * @param cls closure
1258 * @param s scheduler to use
1259 * @param server the initialized server
1260 * @param c configuration to use
1264 struct GNUNET_SCHEDULER_Handle *s,
1265 struct GNUNET_SERVER_Handle *server,
1266 const struct GNUNET_CONFIGURATION_Handle *c)
1269 unsigned int bf_size;
1274 GNUNET_CONFIGURATION_get_value_number (cfg,
1275 "DATASTORE", "QUOTA", "a))
1277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1278 _("No `%s' specified for `%s' in configuration!\n"),
1283 cache_size = quota / 8; /* Or should we make this an option? */
1284 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1287 GNUNET_CONFIGURATION_get_value_filename (cfg,
1292 GNUNET_DISK_directory_create_for_file (fn)) )
1294 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1295 _("Could not use specified filename `%s' for bloomfilter.\n"),
1296 fn != NULL ? fn : "");
1297 GNUNET_free_non_null (fn);
1300 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1301 GNUNET_free_non_null (fn);
1304 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1305 _("Failed to initialize bloomfilter.\n"));
1308 plugin = load_plugin ();
1311 GNUNET_CONTAINER_bloomfilter_free (filter);
1315 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1316 GNUNET_SERVER_add_handlers (server, handlers);
1318 = GNUNET_SCHEDULER_add_delayed (sched,
1320 GNUNET_SCHEDULER_PRIORITY_IDLE,
1321 GNUNET_SCHEDULER_NO_TASK,
1322 GNUNET_TIME_UNIT_ZERO,
1323 &delete_expired, NULL);
1324 GNUNET_SCHEDULER_add_delayed (sched,
1326 GNUNET_SCHEDULER_PRIORITY_IDLE,
1327 GNUNET_SCHEDULER_NO_TASK,
1328 GNUNET_TIME_UNIT_FOREVER_REL,
1329 &cleaning_task, NULL);
1335 * The main function for the datastore service.
1337 * @param argc number of arguments from the command line
1338 * @param argv command line arguments
1339 * @return 0 ok, 1 on error
1342 main (int argc, char *const *argv)
1347 GNUNET_SERVICE_run (argc,
1349 "datastore", &run, NULL, NULL, NULL)) ? 0 : 1;
1354 /* end of gnunet-service-datastore.c */