gnunet_datastore_plugin.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2009, 2011 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. * @author Christian Grothoff
  18. *
  19. * @file
  20. * API for the database backend plugins.
  21. *
  22. * @defgroup datastore-plugin Data Store service plugin API
  23. * API for the database backend plugins.
  24. * @{
  25. */
  26. #ifndef PLUGIN_DATASTORE_H
  27. #define PLUGIN_DATASTORE_H
  28. #include "gnunet_block_lib.h"
  29. #include "gnunet_configuration_lib.h"
  30. #include "gnunet_datastore_service.h"
  31. #include "gnunet_statistics_service.h"
  32. #include "gnunet_scheduler_lib.h"
  33. /**
  34. * How many bytes of overhead will we assume per entry
  35. * in any DB (for reservations)?
  36. */
  37. #define GNUNET_DATASTORE_ENTRY_OVERHEAD 256
  38. /**
  39. * Function invoked to notify service of disk utilization
  40. * changes.
  41. *
  42. * @param cls closure
  43. * @param delta change in disk utilization,
  44. * 0 for "reset to empty"
  45. */
  46. typedef void
  47. (*GNUNET_DATASTORE_DiskUtilizationChange) (void *cls,
  48. int delta);
  49. /**
  50. * The datastore service will pass a pointer to a struct
  51. * of this type as the first and only argument to the
  52. * entry point of each datastore plugin.
  53. */
  54. struct GNUNET_DATASTORE_PluginEnvironment
  55. {
  56. /**
  57. * Configuration to use.
  58. */
  59. const struct GNUNET_CONFIGURATION_Handle *cfg;
  60. /**
  61. * Function to call on disk utilization change.
  62. */
  63. GNUNET_DATASTORE_DiskUtilizationChange duc;
  64. /**
  65. * Closure.
  66. */
  67. void *cls;
  68. };
  69. /**
  70. * An processor over a set of items stored in the datastore.
  71. *
  72. * @param cls closure
  73. * @param key key for the content
  74. * @param size number of bytes in data
  75. * @param data content stored
  76. * @param type type of the content
  77. * @param priority priority of the content
  78. * @param anonymity anonymity-level for the content
  79. * @param replication replication-level for the content
  80. * @param expiration expiration time for the content
  81. * @param uid unique identifier for the datum
  82. * @return #GNUNET_OK to keep the item
  83. * #GNUNET_NO to delete the item
  84. */
  85. typedef int
  86. (*PluginDatumProcessor) (void *cls,
  87. const struct GNUNET_HashCode *key,
  88. uint32_t size,
  89. const void *data,
  90. enum GNUNET_BLOCK_Type type,
  91. uint32_t priority,
  92. uint32_t anonymity,
  93. uint32_t replication,
  94. struct GNUNET_TIME_Absolute expiration,
  95. uint64_t uid);
  96. /**
  97. * Get an estimate of how much space the database is
  98. * currently using.
  99. *
  100. * NB: estimate is an output parameter because emscripten cannot handle
  101. * returning 64-bit integers from dynamically loaded modules.
  102. *
  103. * @param cls closure
  104. * @param estimate location to store estimate
  105. * @return number of bytes used on disk
  106. */
  107. typedef void
  108. (*PluginEstimateSize) (void *cls,
  109. unsigned long long *estimate);
  110. /**
  111. * Put continuation.
  112. *
  113. * @param cls closure
  114. * @param key key for the item stored
  115. * @param size size of the item stored
  116. * @param status #GNUNET_OK if inserted, #GNUNET_NO if updated,
  117. * or #GNUNET_SYSERROR if error
  118. * @param msg error message on error
  119. */
  120. typedef void
  121. (*PluginPutCont) (void *cls,
  122. const struct GNUNET_HashCode *key,
  123. uint32_t size,
  124. int status,
  125. const char *msg);
  126. /**
  127. * Store an item in the datastore. If the item is already present,
  128. * the priorities and replication levels are summed up and the higher
  129. * expiration time and lower anonymity level is used.
  130. *
  131. * @param cls closure
  132. * @param key key for the item
  133. * @param absent true if the key was not found in the bloom filter
  134. * @param size number of bytes in @a data
  135. * @param data content stored
  136. * @param type type of the content
  137. * @param priority priority of the content
  138. * @param anonymity anonymity-level for the content
  139. * @param replication replication-level for the content
  140. * @param expiration expiration time for the content
  141. * @param cont continuation called with success or failure status
  142. * @param cont_cls continuation closure for @a cont
  143. */
  144. typedef void
  145. (*PluginPut) (void *cls,
  146. const struct GNUNET_HashCode *key,
  147. bool absent,
  148. uint32_t size,
  149. const void *data,
  150. enum GNUNET_BLOCK_Type type,
  151. uint32_t priority,
  152. uint32_t anonymity,
  153. uint32_t replication,
  154. struct GNUNET_TIME_Absolute expiration,
  155. PluginPutCont cont,
  156. void *cont_cls);
  157. /**
  158. * An processor over a set of keys stored in the datastore.
  159. *
  160. * @param cls closure
  161. * @param key key in the data store, if NULL iteration is finished
  162. * @param count how many values are stored under this key in the datastore
  163. */
  164. typedef void
  165. (*PluginKeyProcessor) (void *cls,
  166. const struct GNUNET_HashCode *key,
  167. unsigned int count);
  168. /**
  169. * Get all of the keys in the datastore.
  170. *
  171. * @param cls closure
  172. * @param proc function to call on each key
  173. * @param proc_cls closure for @a proc
  174. */
  175. typedef void
  176. (*PluginGetKeys) (void *cls,
  177. PluginKeyProcessor proc,
  178. void *proc_cls);
  179. /**
  180. * Get one of the results for a particular key in the datastore.
  181. *
  182. * @param cls closure
  183. * @param next_uid return the result with lowest uid >= next_uid
  184. * @param random if true, return a random result instead of using next_uid
  185. * @param key maybe NULL (to match all entries)
  186. * @param type entries of which type are relevant?
  187. * Use 0 for any type.
  188. * @param proc function to call on the matching value;
  189. * will be called with NULL if nothing matches
  190. * @param proc_cls closure for @a proc
  191. */
  192. typedef void
  193. (*PluginGetKey) (void *cls,
  194. uint64_t next_uid,
  195. bool random,
  196. const struct GNUNET_HashCode *key,
  197. enum GNUNET_BLOCK_Type type,
  198. PluginDatumProcessor proc,
  199. void *proc_cls);
  200. /**
  201. * Remove continuation.
  202. *
  203. * @param cls closure
  204. * @param key key for the content removed
  205. * @param size number of bytes removed
  206. * @param status #GNUNET_OK if removed, #GNUNET_NO if not found,
  207. * or #GNUNET_SYSERROR if error
  208. * @param msg error message on error
  209. */
  210. typedef void
  211. (*PluginRemoveCont) (void *cls,
  212. const struct GNUNET_HashCode *key,
  213. uint32_t size,
  214. int status,
  215. const char *msg);
  216. /**
  217. * Remove a particular key in the datastore.
  218. *
  219. * @param cls closure
  220. * @param key key for the content
  221. * @param size number of bytes in data
  222. * @param data content stored
  223. * @param cont continuation called with success or failure status
  224. * @param cont_cls continuation closure for @a cont
  225. */
  226. typedef void
  227. (*PluginRemoveKey) (void *cls,
  228. const struct GNUNET_HashCode *key,
  229. uint32_t size,
  230. const void *data,
  231. PluginRemoveCont cont,
  232. void *cont_cls);
  233. /**
  234. * Get a random item (additional constraints may apply depending on
  235. * the specific implementation). Calls @a proc with all values ZERO or
  236. * NULL if no item applies, otherwise @a proc is called once and only
  237. * once with an item.
  238. *
  239. * @param cls closure
  240. * @param proc function to call the value (once only).
  241. * @param proc_cls closure for @a proc
  242. */
  243. typedef void
  244. (*PluginGetRandom) (void *cls,
  245. PluginDatumProcessor proc,
  246. void *proc_cls);
  247. /**
  248. * Select a single item from the datastore (among those applicable).
  249. *
  250. * @param cls closure
  251. * @param next_uid return the result with lowest uid >= next_uid
  252. * @param type entries of which type should be considered?
  253. * Must not be zero (ANY).
  254. * @param proc function to call on the matching value;
  255. * will be called with NULL if no value matches
  256. * @param proc_cls closure for @a proc
  257. */
  258. typedef void
  259. (*PluginGetType) (void *cls,
  260. uint64_t next_uid,
  261. enum GNUNET_BLOCK_Type type,
  262. PluginDatumProcessor proc,
  263. void *proc_cls);
  264. /**
  265. * Drop database.
  266. *
  267. * @param cls closure
  268. */
  269. typedef void
  270. (*PluginDrop) (void *cls);
  271. /**
  272. * Each plugin is required to return a pointer to a struct of this
  273. * type as the return value from its entry point.
  274. */
  275. struct GNUNET_DATASTORE_PluginFunctions
  276. {
  277. /**
  278. * Closure to use for all of the following callbacks
  279. * (except "next_request").
  280. */
  281. void *cls;
  282. /**
  283. * Calculate the current on-disk size of the SQ store. Estimates
  284. * are fine, if that's the only thing available.
  285. */
  286. PluginEstimateSize estimate_size;
  287. /**
  288. * Function to store an item in the datastore.
  289. */
  290. PluginPut put;
  291. /**
  292. * Get a particular datum matching a given hash from the datastore.
  293. */
  294. PluginGetKey get_key;
  295. /**
  296. * Get datum (of the specified type) with anonymity level zero.
  297. */
  298. PluginGetType get_zero_anonymity;
  299. /**
  300. * Function to get a random item with high replication score from
  301. * the database, lowering the item's replication score. Returns a
  302. * single random item from those with the highest replication
  303. * counters. The item's replication counter is decremented by one
  304. * IF it was positive before.
  305. */
  306. PluginGetRandom get_replication;
  307. /**
  308. * Function to get a random expired item or, if none are expired,
  309. * either the oldest entry or one with a low priority (depending
  310. * on what was efficiently implementable).
  311. */
  312. PluginGetRandom get_expiration;
  313. /**
  314. * Delete the database. The next operation is
  315. * guaranteed to be unloading of the module.
  316. */
  317. PluginDrop drop;
  318. /**
  319. * Iterate over all keys in the database.
  320. */
  321. PluginGetKeys get_keys;
  322. /**
  323. * Function to remove an item from the database.
  324. */
  325. PluginRemoveKey remove_key;
  326. };
  327. #endif
  328. /** @} */ /* end of group */