datacache.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2015 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file datacache/datacache.c
  18. * @brief datacache API implementation
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_datacache_lib.h"
  24. #include "gnunet_statistics_service.h"
  25. #include "gnunet_datacache_plugin.h"
  26. #define LOG(kind,...) GNUNET_log_from (kind, "datacache", __VA_ARGS__)
  27. #define LOG_STRERROR_FILE(kind,op,fn) GNUNET_log_from_strerror_file (kind, "datacache", op, fn)
  28. /**
  29. * Internal state of the datacache library.
  30. */
  31. struct GNUNET_DATACACHE_Handle
  32. {
  33. /**
  34. * Bloomfilter to quickly tell if we don't have the content.
  35. */
  36. struct GNUNET_CONTAINER_BloomFilter *filter;
  37. /**
  38. * Our configuration.
  39. */
  40. const struct GNUNET_CONFIGURATION_Handle *cfg;
  41. /**
  42. * Opaque handle for the statistics service.
  43. */
  44. struct GNUNET_STATISTICS_Handle *stats;
  45. /**
  46. * Configuration section to use.
  47. */
  48. char *section;
  49. /**
  50. * API of the transport as returned by the plugin's
  51. * initialization function.
  52. */
  53. struct GNUNET_DATACACHE_PluginFunctions *api;
  54. /**
  55. * Short name for the plugin (i.e. "sqlite").
  56. */
  57. char *short_name;
  58. /**
  59. * Name of the library (i.e. "gnunet_plugin_datacache_sqlite").
  60. */
  61. char *lib_name;
  62. /**
  63. * Name for the bloom filter file.
  64. */
  65. char *bloom_name;
  66. /**
  67. * Environment provided to our plugin.
  68. */
  69. struct GNUNET_DATACACHE_PluginEnvironment env;
  70. /**
  71. * How much space is in use right now?
  72. */
  73. unsigned long long utilization;
  74. };
  75. /**
  76. * Function called by plugins to notify the datacache
  77. * about content deletions.
  78. *
  79. * @param cls closure
  80. * @param key key of the content that was deleted
  81. * @param size number of bytes that were made available
  82. */
  83. static void
  84. env_delete_notify (void *cls,
  85. const struct GNUNET_HashCode *key,
  86. size_t size)
  87. {
  88. struct GNUNET_DATACACHE_Handle *h = cls;
  89. LOG (GNUNET_ERROR_TYPE_DEBUG,
  90. "Content under key `%s' discarded\n",
  91. GNUNET_h2s (key));
  92. GNUNET_assert (h->utilization >= size);
  93. h->utilization -= size;
  94. GNUNET_CONTAINER_bloomfilter_remove (h->filter,
  95. key);
  96. GNUNET_STATISTICS_update (h->stats,
  97. gettext_noop ("# bytes stored"),
  98. - (long long) size,
  99. GNUNET_NO);
  100. GNUNET_STATISTICS_update (h->stats,
  101. gettext_noop ("# items stored"),
  102. -1,
  103. GNUNET_NO);
  104. }
  105. /**
  106. * Create a data cache.
  107. *
  108. * @param cfg configuration to use
  109. * @param section section in the configuration that contains our options
  110. * @return handle to use to access the service
  111. */
  112. struct GNUNET_DATACACHE_Handle *
  113. GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
  114. const char *section)
  115. {
  116. unsigned int bf_size;
  117. unsigned long long quota;
  118. struct GNUNET_DATACACHE_Handle *ret;
  119. char *libname;
  120. char *name;
  121. if (GNUNET_OK !=
  122. GNUNET_CONFIGURATION_get_value_size (cfg,
  123. section,
  124. "QUOTA",
  125. &quota))
  126. {
  127. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  128. section,
  129. "QUOTA");
  130. return NULL;
  131. }
  132. if (GNUNET_OK !=
  133. GNUNET_CONFIGURATION_get_value_string (cfg,
  134. section,
  135. "DATABASE",
  136. &name))
  137. {
  138. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  139. section,
  140. "DATABASE");
  141. return NULL;
  142. }
  143. bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
  144. ret = GNUNET_new (struct GNUNET_DATACACHE_Handle);
  145. if (GNUNET_YES !=
  146. GNUNET_CONFIGURATION_get_value_yesno (cfg,
  147. section,
  148. "DISABLE_BF"))
  149. {
  150. if (GNUNET_YES !=
  151. GNUNET_CONFIGURATION_get_value_yesno (cfg,
  152. section,
  153. "DISABLE_BF_RC"))
  154. {
  155. ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
  156. }
  157. if (NULL != ret->bloom_name)
  158. {
  159. ret->filter = GNUNET_CONTAINER_bloomfilter_load (ret->bloom_name,
  160. quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
  161. 5);
  162. }
  163. if (NULL == ret->filter)
  164. {
  165. ret->filter = GNUNET_CONTAINER_bloomfilter_init (NULL,
  166. bf_size,
  167. 5); /* approx. 3% false positives at max use */
  168. }
  169. }
  170. ret->stats = GNUNET_STATISTICS_create ("datacache", cfg);
  171. ret->section = GNUNET_strdup (section);
  172. ret->env.cfg = cfg;
  173. ret->env.delete_notify = &env_delete_notify;
  174. ret->env.section = ret->section;
  175. ret->env.cls = ret;
  176. ret->env.delete_notify = &env_delete_notify;
  177. ret->env.quota = quota;
  178. LOG (GNUNET_ERROR_TYPE_INFO,
  179. _("Loading `%s' datacache plugin\n"),
  180. name);
  181. GNUNET_asprintf (&libname,
  182. "libgnunet_plugin_datacache_%s",
  183. name);
  184. ret->short_name = name;
  185. ret->lib_name = libname;
  186. ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
  187. if (ret->api == NULL)
  188. {
  189. LOG (GNUNET_ERROR_TYPE_ERROR,
  190. _("Failed to load datacache plugin for `%s'\n"),
  191. name);
  192. GNUNET_DATACACHE_destroy (ret);
  193. return NULL;
  194. }
  195. return ret;
  196. }
  197. /**
  198. * Destroy a data cache (and free associated resources).
  199. *
  200. * @param h handle to the datastore
  201. */
  202. void
  203. GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
  204. {
  205. if (NULL != h->filter)
  206. GNUNET_CONTAINER_bloomfilter_free (h->filter);
  207. if (NULL != h->api)
  208. GNUNET_break (NULL ==
  209. GNUNET_PLUGIN_unload (h->lib_name,
  210. h->api));
  211. GNUNET_free (h->lib_name);
  212. GNUNET_free (h->short_name);
  213. GNUNET_free (h->section);
  214. if (NULL != h->bloom_name)
  215. {
  216. if (0 != UNLINK (h->bloom_name))
  217. GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  218. "datacache",
  219. "unlink",
  220. h->bloom_name);
  221. GNUNET_free (h->bloom_name);
  222. }
  223. GNUNET_STATISTICS_destroy (h->stats,
  224. GNUNET_NO);
  225. GNUNET_free (h);
  226. }
  227. /**
  228. * Store an item in the datastore.
  229. *
  230. * @param h handle to the datacache
  231. * @param key key to store data under
  232. * @param xor_distance distance of @a key to our PID
  233. * @param data_size number of bytes in @a data
  234. * @param data data to store
  235. * @param type type of the value
  236. * @param discard_time when to discard the value in any case
  237. * @param path_info_len number of entries in @a path_info
  238. * @param path_info a path through the network
  239. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error, #GNUNET_NO if duplicate
  240. */
  241. int
  242. GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
  243. const struct GNUNET_HashCode *key,
  244. uint32_t xor_distance,
  245. size_t data_size,
  246. const char *data,
  247. enum GNUNET_BLOCK_Type type,
  248. struct GNUNET_TIME_Absolute discard_time,
  249. unsigned int path_info_len,
  250. const struct GNUNET_PeerIdentity *path_info)
  251. {
  252. ssize_t used;
  253. used = h->api->put (h->api->cls,
  254. key,
  255. xor_distance,
  256. data_size,
  257. data,
  258. type,
  259. discard_time,
  260. path_info_len,
  261. path_info);
  262. if (-1 == used)
  263. {
  264. GNUNET_break (0);
  265. return GNUNET_SYSERR;
  266. }
  267. if (0 == used)
  268. {
  269. /* duplicate */
  270. return GNUNET_NO;
  271. }
  272. LOG (GNUNET_ERROR_TYPE_DEBUG,
  273. "Stored data under key `%s' in cache\n",
  274. GNUNET_h2s (key));
  275. if (NULL != h->filter)
  276. GNUNET_CONTAINER_bloomfilter_add (h->filter,
  277. key);
  278. GNUNET_STATISTICS_update (h->stats,
  279. gettext_noop ("# bytes stored"),
  280. used,
  281. GNUNET_NO);
  282. GNUNET_STATISTICS_update (h->stats,
  283. gettext_noop ("# items stored"),
  284. 1,
  285. GNUNET_NO);
  286. while (h->utilization + used > h->env.quota)
  287. GNUNET_assert (GNUNET_OK ==
  288. h->api->del (h->api->cls));
  289. h->utilization += used;
  290. return GNUNET_OK;
  291. }
  292. /**
  293. * Iterate over the results for a particular key
  294. * in the datacache.
  295. *
  296. * @param h handle to the datacache
  297. * @param key what to look up
  298. * @param type entries of which type are relevant?
  299. * @param iter maybe NULL (to just count)
  300. * @param iter_cls closure for @a iter
  301. * @return the number of results found
  302. */
  303. unsigned int
  304. GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
  305. const struct GNUNET_HashCode *key,
  306. enum GNUNET_BLOCK_Type type,
  307. GNUNET_DATACACHE_Iterator iter,
  308. void *iter_cls)
  309. {
  310. GNUNET_STATISTICS_update (h->stats,
  311. gettext_noop ("# requests received"),
  312. 1,
  313. GNUNET_NO);
  314. LOG (GNUNET_ERROR_TYPE_DEBUG,
  315. "Processing request for key `%s'\n",
  316. GNUNET_h2s (key));
  317. if ( (NULL != h->filter) &&
  318. (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key)) )
  319. {
  320. GNUNET_STATISTICS_update (h->stats,
  321. gettext_noop ("# requests filtered by bloom filter"),
  322. 1,
  323. GNUNET_NO);
  324. LOG (GNUNET_ERROR_TYPE_DEBUG,
  325. "Bloomfilter filters request for key `%s'\n",
  326. GNUNET_h2s (key));
  327. return 0; /* can not be present */
  328. }
  329. return h->api->get (h->api->cls,
  330. key,
  331. type,
  332. iter,
  333. iter_cls);
  334. }
  335. /**
  336. * Obtain a random element from the datacache.
  337. *
  338. * @param h handle to the datacache
  339. * @param iter maybe NULL (to just count)
  340. * @param iter_cls closure for @a iter
  341. * @return the number of results found (zero or 1)
  342. */
  343. unsigned int
  344. GNUNET_DATACACHE_get_random (struct GNUNET_DATACACHE_Handle *h,
  345. GNUNET_DATACACHE_Iterator iter,
  346. void *iter_cls)
  347. {
  348. GNUNET_STATISTICS_update (h->stats,
  349. gettext_noop ("# requests for random value received"),
  350. 1,
  351. GNUNET_NO);
  352. LOG (GNUNET_ERROR_TYPE_DEBUG,
  353. "Processing request for random value\n");
  354. return h->api->get_random (h->api->cls,
  355. iter,
  356. iter_cls);
  357. }
  358. /**
  359. * Iterate over the results that are "close" to a particular key in
  360. * the datacache. "close" is defined as numerically larger than @a
  361. * key (when interpreted as a circular address space), with small
  362. * distance.
  363. *
  364. * @param h handle to the datacache
  365. * @param key area of the keyspace to look into
  366. * @param num_results number of results that should be returned to @a iter
  367. * @param iter maybe NULL (to just count)
  368. * @param iter_cls closure for @a iter
  369. * @return the number of results found
  370. */
  371. unsigned int
  372. GNUNET_DATACACHE_get_closest (struct GNUNET_DATACACHE_Handle *h,
  373. const struct GNUNET_HashCode *key,
  374. unsigned int num_results,
  375. GNUNET_DATACACHE_Iterator iter,
  376. void *iter_cls)
  377. {
  378. GNUNET_STATISTICS_update (h->stats,
  379. gettext_noop ("# proximity search requests received"),
  380. 1,
  381. GNUNET_NO);
  382. LOG (GNUNET_ERROR_TYPE_DEBUG,
  383. "Processing proximity search at `%s'\n",
  384. GNUNET_h2s (key));
  385. return h->api->get_closest (h->api->cls,
  386. key,
  387. num_results,
  388. iter,
  389. iter_cls);
  390. }
  391. /* end of datacache.c */