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 "plugin_datastore.h"
32 #include "datastore.h"
35 * How many messages do we queue at most per client?
37 #define MAX_PENDING 1024
40 * How long are we at most keeping "expired" content
41 * past the expiration date in the database?
43 #define MAX_EXPIRE_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
48 * Our datastore plugin.
50 struct DatastorePlugin
54 * API of the transport as returned by the plugin's
55 * initialization function.
57 struct GNUNET_DATASTORE_PluginFunctions *api;
60 * Short name for the plugin (i.e. "sqlite").
65 * Name of the library (i.e. "gnunet_plugin_datastore_sqlite").
70 * Environment this transport service is using
73 struct GNUNET_DATASTORE_PluginEnvironment env;
79 * Linked list of active reservations.
81 struct ReservationList
85 * This is a linked list.
87 struct ReservationList *next;
90 * Client that made the reservation.
92 struct GNUNET_SERVER_Client *client;
95 * Number of bytes (still) reserved.
100 * Number of items (still) reserved.
105 * Reservation identifier.
113 * Our datastore plugin (NULL if not available).
115 static struct DatastorePlugin *plugin;
118 * Linked list of space reservations made by clients.
120 static struct ReservationList *reservations;
123 * Bloomfilter to quickly tell if we don't have the content.
125 static struct GNUNET_CONTAINER_BloomFilter *filter;
128 * Static counter to produce reservation identifiers.
130 static int reservation_gen;
133 * How much space are we allowed to use?
135 static unsigned long long quota;
138 * How much space are we using for the cache? (space available for
139 * insertions that will be instantly reclaimed by discarding less
140 * important content --- or possibly whatever we just inserted into
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 const 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);
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Deleting content that expired %llu ms ago\n",
282 (unsigned long long) (now.value - expiration.value));
284 GNUNET_CONTAINER_bloomfilter_remove (filter,
286 return GNUNET_NO; /* delete */
291 * Task that is used to remove expired entries from
292 * the datastore. This task will schedule itself
293 * again automatically to always delete all expired
296 * @param cls not used
297 * @param tc task context
300 delete_expired (void *cls,
301 const struct GNUNET_SCHEDULER_TaskContext *tc)
303 plugin->api->iter_ascending_expiration (plugin->api->cls,
311 * An iterator over a set of items stored in the datastore.
314 * @param next_cls closure to pass to the "next" function.
315 * @param key key for the content
316 * @param size number of bytes in data
317 * @param data content stored
318 * @param type type of the content
319 * @param priority priority of the content
320 * @param anonymity anonymity-level for the content
321 * @param expiration expiration time for the content
322 * @param uid unique identifier for the datum;
323 * maybe 0 if no unique identifier is available
325 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
326 * (continue on call to "next", of course),
327 * GNUNET_NO to delete the item and continue (if supported)
332 const GNUNET_HashCode * key,
338 struct GNUNET_TIME_Absolute
342 unsigned long long *need = cls;
347 return GNUNET_SYSERR;
349 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
352 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
353 plugin->api->next_request (next_cls,
354 (0 == *need) ? GNUNET_YES : GNUNET_NO);
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
357 "Deleting %llu bytes of low-priority content (still trying to free another %llu bytes)\n",
358 size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
361 GNUNET_CONTAINER_bloomfilter_remove (filter,
368 * Manage available disk space by running tasks
369 * that will discard content if necessary. This
370 * function will be run whenever a request for
371 * "need" bytes of storage could only be satisfied
372 * by eating into the "cache" (and we want our cache
375 * @param need number of bytes of content that were
376 * placed into the "cache" (and hence the
377 * number of bytes that should be removed).
380 manage_space (unsigned long long need)
382 unsigned long long *n;
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386 "Asked to free up %llu bytes of cache space\n",
389 n = GNUNET_malloc (sizeof(unsigned long long));
391 plugin->api->iter_low_priority (plugin->api->cls,
399 * Function called to notify a client about the socket
400 * begin ready to queue more data. "buf" will be
401 * NULL and "size" zero if the socket was closed for
402 * writing in the meantime.
405 * @param size number of bytes available in buf
406 * @param buf where the callee should write the message
407 * @return number of bytes written to buf
410 transmit_callback (void *cls,
411 size_t size, void *buf)
413 struct TransmitCallbackContext *tcc = cls;
416 msize = ntohs(tcc->msg->size);
420 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
421 "Transmission failed.\n");
424 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
425 if (GNUNET_YES == tcc->end)
427 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
429 GNUNET_free (tcc->msg);
433 GNUNET_assert (size >= msize);
434 memcpy (buf, tcc->msg, msize);
436 tcc->tc (tcc->tc_cls, GNUNET_OK);
437 if (GNUNET_YES == tcc->end)
439 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 "Response transmitted, more pending!\n");
448 GNUNET_free (tcc->msg);
455 * Transmit the given message to the client.
457 * @param client target of the message
458 * @param msg message to transmit, will be freed!
459 * @param tc function to call afterwards
460 * @param tc_cls closure for tc
461 * @param end is this the last response (and we should
462 * signal the server completion accodingly after
463 * transmitting this message)?
466 transmit (struct GNUNET_SERVER_Client *client,
467 struct GNUNET_MessageHeader *msg,
468 TransmitContinuation tc,
472 struct TransmitCallbackContext *tcc;
474 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
476 tcc->client = client;
478 tcc->tc_cls = tc_cls;
482 GNUNET_SERVER_notify_transmit_ready (client,
484 GNUNET_TIME_UNIT_FOREVER_REL,
489 if (GNUNET_YES == end)
492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
493 "Disconnecting client.\n");
495 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
498 tc (tc_cls, GNUNET_SYSERR);
506 * Transmit a status code to the client.
508 * @param client receiver of the response
509 * @param code status code
510 * @param msg optional error message (can be NULL)
513 transmit_status (struct GNUNET_SERVER_Client *client,
517 struct StatusMessage *sm;
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
522 "Transmitting `%s' message with value %d and message `%s'\n",
525 msg != NULL ? msg : "(none)");
527 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
528 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
529 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
530 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
531 sm->status = htonl(code);
533 memcpy (&sm[1], msg, slen);
534 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
539 * Function called once the transmit operation has
540 * either failed or succeeded.
542 * @param next_cls closure for calling "next_request" callback
543 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
546 get_next(void *next_cls,
549 if (status != GNUNET_OK)
551 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
552 _("Failed to transmit an item to the client; aborting iteration.\n"));
553 plugin->api->next_request (next_cls, GNUNET_YES);
556 plugin->api->next_request (next_cls, GNUNET_NO);
561 * Function that will transmit the given datastore entry
564 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
565 * @param next_cls closure to use to ask for the next item
566 * @param key key for the content
567 * @param size number of bytes in data
568 * @param data content stored
569 * @param type type of the content
570 * @param priority priority of the content
571 * @param anonymity anonymity-level for the content
572 * @param expiration expiration time for the content
573 * @param uid unique identifier for the datum;
574 * maybe 0 if no unique identifier is available
576 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
577 * GNUNET_NO to delete the item and continue (if supported)
580 transmit_item (void *cls,
582 const GNUNET_HashCode * key,
588 struct GNUNET_TIME_Absolute
589 expiration, uint64_t uid)
591 struct GNUNET_SERVER_Client *client = cls;
592 struct GNUNET_MessageHeader *end;
593 struct DataMessage *dm;
597 /* transmit 'DATA_END' */
599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
600 "Transmitting `%s' message\n",
603 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
604 end->size = htons(sizeof(struct GNUNET_MessageHeader));
605 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
606 transmit (client, end, NULL, NULL, GNUNET_YES);
607 GNUNET_SERVER_client_drop (client);
610 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
611 dm->header.size = htons(sizeof(struct DataMessage) + size);
612 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
614 dm->size = htonl(size);
615 dm->type = htonl(type);
616 dm->priority = htonl(priority);
617 dm->anonymity = htonl(anonymity);
618 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
619 dm->uid = GNUNET_htonll(uid);
621 memcpy (&dm[1], data, size);
623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
624 "Transmitting `%s' message\n",
627 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
633 * Handle RESERVE-message.
636 * @param client identification of the client
637 * @param message the actual message
640 handle_reserve (void *cls,
641 struct GNUNET_SERVER_Client *client,
642 const struct GNUNET_MessageHeader *message)
644 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
645 struct ReservationList *e;
646 unsigned long long used;
647 unsigned long long req;
652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
653 "Processing `%s' request\n",
656 amount = GNUNET_ntohll(msg->amount);
657 entries = ntohl(msg->entries);
658 used = plugin->api->get_size (plugin->api->cls) + reserved;
659 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
660 if (used + req > quota)
663 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
664 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
665 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
669 if (cache_size < req)
671 /* TODO: document this in the FAQ; essentially, if this
672 message happens, the insertion request could be blocked
673 by less-important content from migration because it is
674 larger than 1/8th of the overall available space, and
675 we only reserve 1/8th for "fresh" insertions */
676 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
677 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
680 transmit_status (client, 0,
681 gettext_noop ("Insufficient space to satisfy request and "
682 "requested amount is larger than cache size"));
686 transmit_status (client, 0,
687 gettext_noop ("Insufficient space to satisfy request"));
692 e = GNUNET_malloc (sizeof(struct ReservationList));
693 e->next = reservations;
697 e->entries = entries;
698 e->rid = ++reservation_gen;
699 if (reservation_gen < 0)
700 reservation_gen = 0; /* wrap around */
701 transmit_status (client, e->rid, NULL);
706 * Handle RELEASE_RESERVE-message.
709 * @param client identification of the client
710 * @param message the actual message
713 handle_release_reserve (void *cls,
714 struct GNUNET_SERVER_Client *client,
715 const struct GNUNET_MessageHeader *message)
717 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
718 struct ReservationList *pos;
719 struct ReservationList *prev;
720 struct ReservationList *next;
721 int rid = ntohl(msg->rid);
722 unsigned long long rem;
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726 "Processing `%s' request\n",
731 while (NULL != (pos = next))
740 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
741 GNUNET_assert (reserved >= rem);
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 "Returning %llu remaining reserved bytes to storage pool\n",
749 transmit_status (client, GNUNET_OK, NULL);
755 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
760 * Check that the given message is a valid data message.
762 * @return NULL if the message is not well-formed, otherwise the message
764 static const struct DataMessage *
765 check_data (const struct GNUNET_MessageHeader *message)
769 const struct DataMessage *dm;
771 size = ntohs(message->size);
772 if (size < sizeof(struct DataMessage))
777 dm = (const struct DataMessage *) message;
778 dsize = ntohl(dm->size);
779 if (size != dsize + sizeof(struct DataMessage))
789 * Handle PUT-message.
792 * @param client identification of the client
793 * @param message the actual message
796 handle_put (void *cls,
797 struct GNUNET_SERVER_Client *client,
798 const struct GNUNET_MessageHeader *message)
800 const struct DataMessage *dm = check_data (message);
804 struct ReservationList *pos;
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809 "Processing `%s' request\n",
812 if (ntohl(dm->type) == 0)
820 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
823 rid = ntohl(dm->rid);
824 size = ntohl(dm->size);
828 while ( (NULL != pos) &&
831 GNUNET_break (pos != NULL);
834 GNUNET_break (pos->entries > 0);
835 GNUNET_break (pos->amount > size);
838 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
842 ret = plugin->api->put (plugin->api->cls,
848 ntohl(dm->anonymity),
849 GNUNET_TIME_absolute_ntoh(dm->expiration),
851 if (GNUNET_OK == ret)
853 GNUNET_CONTAINER_bloomfilter_add (filter,
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
857 "Successfully stored %u bytes under key `%s'\n",
859 GNUNET_h2s (&dm->key));
862 transmit_status (client,
863 (GNUNET_SYSERR == ret) ? GNUNET_SYSERR : GNUNET_OK,
865 GNUNET_free_non_null (msg);
866 if (quota - reserved - cache_size < plugin->api->get_size (plugin->api->cls))
867 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
872 * Handle GET-message.
875 * @param client identification of the client
876 * @param message the actual message
879 handle_get (void *cls,
880 struct GNUNET_SERVER_Client *client,
881 const struct GNUNET_MessageHeader *message)
883 const struct GetMessage *msg;
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
888 "Processing `%s' request\n",
891 size = ntohs(message->size);
892 if ( (size != sizeof(struct GetMessage)) &&
893 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
896 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
899 msg = (const struct GetMessage*) message;
900 if ( (size == sizeof(struct GetMessage)) &&
901 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
904 /* don't bother database... */
906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
907 "Empty result set for `%s' request for `%s'.\n",
909 GNUNET_h2s (&msg->key));
911 GNUNET_SERVER_client_keep (client);
912 transmit_item (client,
913 NULL, NULL, 0, NULL, 0, 0, 0,
914 GNUNET_TIME_UNIT_ZERO_ABS, 0);
917 GNUNET_SERVER_client_keep (client);
918 plugin->api->get (plugin->api->cls,
919 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
928 * Handle UPDATE-message.
931 * @param client identification of the client
932 * @param message the actual message
935 handle_update (void *cls,
936 struct GNUNET_SERVER_Client *client,
937 const struct GNUNET_MessageHeader *message)
939 const struct UpdateMessage *msg;
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
945 "Processing `%s' request\n",
948 msg = (const struct UpdateMessage*) message;
950 ret = plugin->api->update (plugin->api->cls,
951 GNUNET_ntohll(msg->uid),
952 (int32_t) ntohl(msg->priority),
953 GNUNET_TIME_absolute_ntoh(msg->expiration),
955 transmit_status (client, ret, emsg);
956 GNUNET_free_non_null (emsg);
961 * Handle GET_RANDOM-message.
964 * @param client identification of the client
965 * @param message the actual message
968 handle_get_random (void *cls,
969 struct GNUNET_SERVER_Client *client,
970 const struct GNUNET_MessageHeader *message)
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
974 "Processing `%s' request\n",
977 GNUNET_SERVER_client_keep (client);
978 plugin->api->iter_migration_order (plugin->api->cls,
986 * Context for the 'remove_callback'.
991 * Client for whom we're doing the remvoing.
993 struct GNUNET_SERVER_Client *client;
996 * GNUNET_YES if we managed to remove something.
1003 * Callback function that will cause the item that is passed
1004 * in to be deleted (by returning GNUNET_NO).
1007 remove_callback (void *cls,
1009 const GNUNET_HashCode * key,
1015 struct GNUNET_TIME_Absolute
1016 expiration, uint64_t uid)
1018 struct RemoveContext *rc = cls;
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1024 "No further matches for `%s' request.\n",
1027 if (GNUNET_YES == rc->found)
1028 transmit_status (rc->client, GNUNET_OK, NULL);
1030 transmit_status (rc->client, GNUNET_NO, _("Content not found"));
1031 GNUNET_SERVER_client_drop (rc->client);
1033 return GNUNET_OK; /* last item */
1035 rc->found = GNUNET_YES;
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1038 "Item %llu matches `%s' request.\n",
1039 (unsigned long long) uid,
1042 GNUNET_CONTAINER_bloomfilter_remove (filter,
1044 plugin->api->next_request (next_cls, GNUNET_YES);
1050 * Handle REMOVE-message.
1052 * @param cls closure
1053 * @param client identification of the client
1054 * @param message the actual message
1057 handle_remove (void *cls,
1058 struct GNUNET_SERVER_Client *client,
1059 const struct GNUNET_MessageHeader *message)
1061 const struct DataMessage *dm = check_data (message);
1062 GNUNET_HashCode vhash;
1063 struct RemoveContext *rc;
1066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1067 "Processing `%s' request\n",
1073 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1076 rc = GNUNET_malloc (sizeof(struct RemoveContext));
1077 GNUNET_SERVER_client_keep (client);
1078 rc->client = client;
1079 GNUNET_CRYPTO_hash (&dm[1],
1082 plugin->api->get (plugin->api->cls,
1092 * Handle DROP-message.
1094 * @param cls closure
1095 * @param client identification of the client
1096 * @param message the actual message
1099 handle_drop (void *cls,
1100 struct GNUNET_SERVER_Client *client,
1101 const struct GNUNET_MessageHeader *message)
1104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1105 "Processing `%s' request\n",
1108 plugin->api->drop (plugin->api->cls);
1109 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1114 * List of handlers for the messages understood by this
1117 static struct GNUNET_SERVER_MessageHandler handlers[] = {
1118 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1119 sizeof(struct ReserveMessage) },
1120 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1121 sizeof(struct ReleaseReserveMessage) },
1122 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1123 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1124 sizeof (struct UpdateMessage) },
1125 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1126 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
1127 sizeof(struct GNUNET_MessageHeader) },
1128 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1129 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1130 sizeof(struct GNUNET_MessageHeader) },
1137 * Load the datastore plugin.
1139 static struct DatastorePlugin *
1142 struct DatastorePlugin *ret;
1147 GNUNET_CONFIGURATION_get_value_string (cfg,
1148 "DATASTORE", "DATABASE", &name))
1150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1151 _("No `%s' specified for `%s' in configuration!\n"),
1156 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1158 ret->env.sched = sched;
1159 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1160 _("Loading `%s' datastore plugin\n"), name);
1161 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1162 ret->short_name = name;
1163 ret->lib_name = libname;
1164 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1165 if (ret->api == NULL)
1167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1168 _("Failed to load datastore plugin for `%s'\n"), name);
1169 GNUNET_free (ret->short_name);
1170 GNUNET_free (libname);
1179 * Function called when the service shuts
1180 * down. Unloads our datastore plugin.
1182 * @param plug plugin to unload
1185 unload_plugin (struct DatastorePlugin *plug)
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189 "Datastore service is unloading plugin...\n");
1191 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1192 GNUNET_free (plug->lib_name);
1193 GNUNET_free (plug->short_name);
1199 * Last task run during shutdown. Disconnects us from
1200 * the transport and core.
1203 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1205 unload_plugin (plugin);
1209 GNUNET_CONTAINER_bloomfilter_free (filter);
1212 // GNUNET_ARM_stop_services (cfg, tc->sched, "statistics", NULL);
1217 * Function that removes all active reservations made
1218 * by the given client and releases the space for other
1221 * @param cls closure
1222 * @param client identification of the client
1225 cleanup_reservations (void *cls,
1226 struct GNUNET_SERVER_Client
1229 struct ReservationList *pos;
1230 struct ReservationList *prev;
1231 struct ReservationList *next;
1238 if (pos->client == client)
1241 reservations = next;
1244 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1257 * Process datastore requests.
1259 * @param cls closure
1260 * @param s scheduler to use
1261 * @param server the initialized server
1262 * @param c configuration to use
1266 struct GNUNET_SCHEDULER_Handle *s,
1267 struct GNUNET_SERVER_Handle *server,
1268 const struct GNUNET_CONFIGURATION_Handle *c)
1271 unsigned int bf_size;
1276 GNUNET_CONFIGURATION_get_value_number (cfg,
1277 "DATASTORE", "QUOTA", "a))
1279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1280 _("No `%s' specified for `%s' in configuration!\n"),
1285 cache_size = quota / 8; /* Or should we make this an option? */
1286 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1289 GNUNET_CONFIGURATION_get_value_filename (cfg,
1294 GNUNET_DISK_directory_create_for_file (fn)) )
1296 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1297 _("Could not use specified filename `%s' for bloomfilter.\n"),
1298 fn != NULL ? fn : "");
1299 GNUNET_free_non_null (fn);
1302 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1303 GNUNET_free_non_null (fn);
1306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1307 _("Failed to initialize bloomfilter.\n"));
1310 // GNUNET_ARM_start_services (cfg, sched, "statistics", NULL);
1311 plugin = load_plugin ();
1314 GNUNET_CONTAINER_bloomfilter_free (filter);
1316 // GNUNET_ARM_stop_services (cfg, sched, "statistics", NULL);
1319 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1320 GNUNET_SERVER_add_handlers (server, handlers);
1322 = GNUNET_SCHEDULER_add_delayed (sched,
1324 GNUNET_SCHEDULER_PRIORITY_IDLE,
1325 GNUNET_SCHEDULER_NO_TASK,
1326 GNUNET_TIME_UNIT_ZERO,
1327 &delete_expired, NULL);
1328 GNUNET_SCHEDULER_add_delayed (sched,
1330 GNUNET_SCHEDULER_PRIORITY_IDLE,
1331 GNUNET_SCHEDULER_NO_TASK,
1332 GNUNET_TIME_UNIT_FOREVER_REL,
1333 &cleaning_task, NULL);
1339 * The main function for the datastore service.
1341 * @param argc number of arguments from the command line
1342 * @param argv command line arguments
1343 * @return 0 ok, 1 on error
1346 main (int argc, char *const *argv)
1351 GNUNET_SERVICE_run (argc,
1353 "datastore", &run, NULL, NULL, NULL)) ? 0 : 1;
1358 /* end of gnunet-service-datastore.c */