GNUnet  0.10.x
datacache.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2015 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
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  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_datacache_lib.h"
30 
31 
32 #define LOG(kind,...) GNUNET_log_from (kind, "datacache", __VA_ARGS__)
33 
34 #define LOG_STRERROR_FILE(kind,op,fn) GNUNET_log_from_strerror_file (kind, "datacache", op, fn)
35 
40 {
41 
46 
51 
56 
60  char *section;
61 
67 
71  char *short_name;
72 
76  char *lib_name;
77 
81  char *bloom_name;
82 
87 
91  unsigned long long utilization;
92 
93 };
94 
95 
104 static void
106  const struct GNUNET_HashCode *key,
107  size_t size)
108 {
109  struct GNUNET_DATACACHE_Handle *h = cls;
110 
112  "Content under key `%s' discarded\n",
113  GNUNET_h2s (key));
114  GNUNET_assert (h->utilization >= size);
115  h->utilization -= size;
117  key);
119  gettext_noop ("# bytes stored"),
120  - (long long) size,
121  GNUNET_NO);
123  gettext_noop ("# items stored"),
124  -1,
125  GNUNET_NO);
126 }
127 
128 
138  const char *section)
139 {
140  unsigned int bf_size;
141  unsigned long long quota;
143  char *libname;
144  char *name;
145 
146  if (GNUNET_OK !=
148  section,
149  "QUOTA",
150  &quota))
151  {
153  section,
154  "QUOTA");
155  return NULL;
156  }
157  if (GNUNET_OK !=
159  section,
160  "DATABASE",
161  &name))
162  {
164  section,
165  "DATABASE");
166  return NULL;
167  }
168  bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
169 
170  ret = GNUNET_new (struct GNUNET_DATACACHE_Handle);
171 
172  if (GNUNET_YES !=
174  section,
175  "DISABLE_BF"))
176  {
177  if (GNUNET_YES !=
179  section,
180  "DISABLE_BF_RC"))
181  {
182  ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
183  }
184  if (NULL != ret->bloom_name)
185  {
187  quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
188  5);
189  }
190  if (NULL == ret->filter)
191  {
193  bf_size,
194  5); /* approx. 3% false positives at max use */
195  }
196  }
197  ret->stats = GNUNET_STATISTICS_create ("datacache", cfg);
198  ret->section = GNUNET_strdup (section);
199  ret->env.cfg = cfg;
201  ret->env.section = ret->section;
202  ret->env.cls = ret;
204  ret->env.quota = quota;
206  _("Loading `%s' datacache plugin\n"),
207  name);
208  GNUNET_asprintf (&libname,
209  "libgnunet_plugin_datacache_%s",
210  name);
211  ret->short_name = name;
212  ret->lib_name = libname;
213  ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
214  if (ret->api == NULL)
215  {
217  _("Failed to load datacache plugin for `%s'\n"),
218  name);
220  return NULL;
221  }
222  return ret;
223 }
224 
225 
231 void
233 {
234  if (NULL != h->filter)
236  if (NULL != h->api)
237  GNUNET_break (NULL ==
239  h->api));
240  GNUNET_free (h->lib_name);
241  GNUNET_free (h->short_name);
242  GNUNET_free (h->section);
243  if (NULL != h->bloom_name)
244  {
245  if (0 != UNLINK (h->bloom_name))
247  "datacache",
248  "unlink",
249  h->bloom_name);
250  GNUNET_free (h->bloom_name);
251  }
253  GNUNET_NO);
254  GNUNET_free (h);
255 }
256 
257 
272 int
274  const struct GNUNET_HashCode *key,
275  uint32_t xor_distance,
276  size_t data_size,
277  const char *data,
278  enum GNUNET_BLOCK_Type type,
279  struct GNUNET_TIME_Absolute discard_time,
280  unsigned int path_info_len,
281  const struct GNUNET_PeerIdentity *path_info)
282 {
283  ssize_t used;
284 
285  used = h->api->put (h->api->cls,
286  key,
287  xor_distance,
288  data_size,
289  data,
290  type,
291  discard_time,
292  path_info_len,
293  path_info);
294  if (-1 == used)
295  {
296  GNUNET_break (0);
297  return GNUNET_SYSERR;
298  }
299  if (0 == used)
300  {
301  /* duplicate */
302  return GNUNET_NO;
303  }
305  "Stored data under key `%s' in cache\n",
306  GNUNET_h2s (key));
307  if (NULL != h->filter)
309  key);
311  gettext_noop ("# bytes stored"),
312  used,
313  GNUNET_NO);
315  gettext_noop ("# items stored"),
316  1,
317  GNUNET_NO);
318  while (h->utilization + used > h->env.quota)
320  h->api->del (h->api->cls));
321  h->utilization += used;
322  return GNUNET_OK;
323 }
324 
325 
337 unsigned int
339  const struct GNUNET_HashCode *key,
340  enum GNUNET_BLOCK_Type type,
342  void *iter_cls)
343 {
345  gettext_noop ("# requests received"),
346  1,
347  GNUNET_NO);
349  "Processing request for key `%s'\n",
350  GNUNET_h2s (key));
351  if ( (NULL != h->filter) &&
353  {
355  gettext_noop ("# requests filtered by bloom filter"),
356  1,
357  GNUNET_NO);
359  "Bloomfilter filters request for key `%s'\n",
360  GNUNET_h2s (key));
361  return 0; /* can not be present */
362  }
363  return h->api->get (h->api->cls,
364  key,
365  type,
366  iter,
367  iter_cls);
368 }
369 
370 
379 unsigned int
382  void *iter_cls)
383 {
385  gettext_noop ("# requests for random value received"),
386  1,
387  GNUNET_NO);
389  "Processing request for random value\n");
390  return h->api->get_random (h->api->cls,
391  iter,
392  iter_cls);
393 }
394 
395 
409 unsigned int
411  const struct GNUNET_HashCode *key,
412  unsigned int num_results,
414  void *iter_cls)
415 {
417  gettext_noop ("# proximity search requests received"),
418  1,
419  GNUNET_NO);
421  "Processing proximity search at `%s'\n",
422  GNUNET_h2s (key));
423  return h->api->get_closest (h->api->cls,
424  key,
425  num_results,
426  iter,
427  iter_cls);
428 }
429 
430 
431 /* end of datacache.c */
const char * section
Configuration section to use.
struct GNUNET_CONTAINER_BloomFilter * GNUNET_CONTAINER_bloomfilter_init(const char *data, size_t size, unsigned int k)
Create a Bloom filter from raw bits.
void * cls
Closure to pass to all plugin functions.
struct GNUNET_DATACACHE_PluginEnvironment env
Environment provided to our plugin.
Definition: datacache.c:86
ssize_t(* put)(void *cls, const struct GNUNET_HashCode *key, uint32_t xor_distance, size_t size, const char *data, enum GNUNET_BLOCK_Type type, struct GNUNET_TIME_Absolute discard_time, unsigned int path_info_len, const struct GNUNET_PeerIdentity *path_info)
Store an item in the datastore.
void GNUNET_CONTAINER_bloomfilter_add(struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Add an element to the filter.
int(* del)(void *cls)
Delete the entry with the lowest expiration value from the datacache right now.
GNUNET_BLOCK_Type
Blocks in the datastore and the datacache must have a unique type.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void GNUNET_DATACACHE_destroy(struct GNUNET_DATACACHE_Handle *h)
Destroy a data cache (and free associated resources).
Definition: datacache.c:232
int GNUNET_DATACACHE_put(struct GNUNET_DATACACHE_Handle *h, const struct GNUNET_HashCode *key, uint32_t xor_distance, size_t data_size, const char *data, enum GNUNET_BLOCK_Type type, struct GNUNET_TIME_Absolute discard_time, unsigned int path_info_len, const struct GNUNET_PeerIdentity *path_info)
Store an item in the datastore.
Definition: datacache.c:273
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
#define GNUNET_new(type)
Allocate a struct or union of the given type.
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
static int ret
Final status code.
Definition: gnunet-arm.c:89
Handle for the service.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
GNUNET_DATACACHE_DeleteNotifyCallback delete_notify
Function to call whenever the plugin needs to discard content that it was asked to store...
#define UNLINK(f)
Definition: plibc.h:666
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition: plugin.c:258
struct GNUNET_STATISTICS_Handle * stats
Opaque handle for the statistics service.
Definition: datacache.c:55
#define GNUNET_log_from_strerror_file(level, component, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
char * short_name
Short name for the plugin (i.e.
Definition: datacache.c:71
unsigned long long utilization
How much space is in use right now?
Definition: datacache.c:91
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
char * GNUNET_DISK_mktemp(const char *t)
Create an (empty) temporary file on disk.
Definition: disk.c:593
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
void GNUNET_CONTAINER_bloomfilter_remove(struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Remove an element from the filter.
int GNUNET_CONTAINER_bloomfilter_test(const struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Test if an element is in the filter.
unsigned int GNUNET_DATACACHE_get(struct GNUNET_DATACACHE_Handle *h, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Iterate over the results for a particular key in the datacache.
Definition: datacache.c:338
unsigned int(* get_closest)(void *cls, const struct GNUNET_HashCode *key, unsigned int num_results, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Iterate over the results that are "close" to a particular key in the datacache.
struct GNUNET_DATACACHE_Handle * GNUNET_DATACACHE_create(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
Create a data cache.
Definition: datacache.c:137
struct GNUNET_CONTAINER_BloomFilter * filter
Bloomfilter to quickly tell if we don&#39;t have the content.
Definition: datacache.c:45
char * lib_name
Name of the library (i.e.
Definition: datacache.c:76
struct GNUNET_CONTAINER_BloomFilter * GNUNET_CONTAINER_bloomfilter_load(const char *filename, size_t size, unsigned int k)
Load a Bloom filter from a file.
struct returned by the initialization function of the plugin
A 512-bit hashcode.
int GNUNET_CONFIGURATION_get_value_size(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *size)
Get a configuration value that should be a size in bytes.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: datacache.c:50
int GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
struct GNUNET_HashCode key
The key used in the DHT.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static unsigned int size
Size of the "table".
Definition: peer.c:67
const char * name
#define LOG(kind,...)
Definition: datacache.c:32
static unsigned long long quota
How much space are we allowed to use?
The identity of the host (wraps the signing key of the peer).
struct GNUNET_DATACACHE_PluginFunctions * api
API of the transport as returned by the plugin&#39;s initialization function.
Definition: datacache.c:66
Internal state of the datacache library.
Definition: datacache.c:39
static void env_delete_notify(void *cls, const struct GNUNET_HashCode *key, size_t size)
Function called by plugins to notify the datacache about content deletions.
Definition: datacache.c:105
configuration data
Definition: configuration.c:85
int(* GNUNET_DATACACHE_Iterator)(void *cls, const struct GNUNET_HashCode *key, size_t data_size, const char *data, enum GNUNET_BLOCK_Type type, struct GNUNET_TIME_Absolute exp, unsigned int path_info_len, const struct GNUNET_PeerIdentity *path_info)
An iterator over a set of items stored in the datacache.
void * cls
Closure to use for callbacks.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_YES
Definition: gnunet_common.h:80
void * GNUNET_PLUGIN_load(const char *library_name, void *arg)
Setup plugin (runs the "init" callback and returns whatever "init" returned).
Definition: plugin.c:210
unsigned int(* get_random)(void *cls, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Return a random value from the datastore.
unsigned long long quota
How much space are we allowed to use?
int GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
uint32_t data
The data value.
The datastore service will pass a pointer to a struct of this type as the first and only argument to ...
static size_t data_size
Number of bytes in data.
unsigned int GNUNET_DATACACHE_get_closest(struct GNUNET_DATACACHE_Handle *h, const struct GNUNET_HashCode *key, unsigned int num_results, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Iterate over the results that are "close" to a particular key in the datacache.
Definition: datacache.c:410
char * section
Configuration section to use.
Definition: datacache.c:60
void GNUNET_CONTAINER_bloomfilter_free(struct GNUNET_CONTAINER_BloomFilter *bf)
Free the space associcated with a filter in memory, flush to drive if needed (do not free the space o...
char * bloom_name
Name for the bloom filter file.
Definition: datacache.c:81
unsigned int GNUNET_DATACACHE_get_random(struct GNUNET_DATACACHE_Handle *h, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Obtain a random element from the datacache.
Definition: datacache.c:380
unsigned int(* get)(void *cls, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Iterate over the results for a particular key in the datastore.
#define GNUNET_free(ptr)
Wrapper around free.
#define gettext_noop(String)
Definition: gettext.h:69