GNUnet  0.11.x
Typedefs | Functions
Data Cache library

Simple, transient hash table of bounded size with content expiration. More...

Typedefs

typedef enum GNUNET_GenericReturnValue(* 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_DHT_PathElement *path_info)
 An iterator over a set of items stored in the datacache. More...
 

Functions

struct GNUNET_DATACACHE_HandleGNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
 Create a data cache. More...
 
void GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
 Destroy a data cache (and free associated resources). More...
 
enum GNUNET_GenericReturnValue 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_DHT_PathElement *path_info)
 Store an item in the datacache. More...
 
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. More...
 
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. More...
 

Detailed Description

Simple, transient hash table of bounded size with content expiration.

In contrast to the sqstore there is no prioritization, deletion or iteration. All of the data is discarded when the peer shuts down!

Typedef Documentation

◆ GNUNET_DATACACHE_Iterator

typedef enum GNUNET_GenericReturnValue(* 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_DHT_PathElement *path_info)

An iterator over a set of items stored in the datacache.

Parameters
clsclosure
keykey for the content
data_sizenumber of bytes in data
datacontent stored
typetype of the content
expwhen will the content expire?
path_info_lennumber of entries in path_info
path_infoa path through the network
Returns
GNUNET_OK to continue iterating, GNUNET_SYSERR to abort

Definition at line 77 of file gnunet_datacache_lib.h.

Function Documentation

◆ GNUNET_DATACACHE_create()

struct GNUNET_DATACACHE_Handle* GNUNET_DATACACHE_create ( const struct GNUNET_CONFIGURATION_Handle cfg,
const char *  section 
)

Create a data cache.

Parameters
cfgconfiguration to use
sectionsection in the configuration that contains our options
Returns
handle to use to access the service

Definition at line 128 of file datacache.c.

130 {
131  unsigned int bf_size;
132  unsigned long long quota;
134  char *libname;
135  char *name;
136  const struct GNUNET_OS_ProjectData *pd;
137 
138  if (GNUNET_OK !=
139  GNUNET_CONFIGURATION_get_value_size (cfg, section, "QUOTA", &quota))
140  {
142  return NULL;
143  }
144  if (GNUNET_OK !=
145  GNUNET_CONFIGURATION_get_value_string (cfg, section, "DATABASE", &name))
146  {
147  GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, "DATABASE");
148  return NULL;
149  }
150  bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
151 
153 
154  if (GNUNET_YES !=
155  GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF"))
156  {
157  if (GNUNET_YES !=
158  GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF_RC"))
159  {
160  ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
161  }
162  if (NULL != ret->bloom_name)
163  {
165  ret->bloom_name,
166  quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
167  5);
168  }
169  if (NULL == ret->filter)
170  {
171  ret->filter =
173  bf_size,
174  5); /* approx. 3% false positives at max use */
175  }
176  }
177  ret->stats = GNUNET_STATISTICS_create ("datacache", cfg);
178  ret->section = GNUNET_strdup (section);
179  ret->env.cfg = cfg;
180  ret->env.delete_notify = &env_delete_notify;
181  ret->env.section = ret->section;
182  ret->env.cls = ret;
183  ret->env.delete_notify = &env_delete_notify;
184  ret->env.quota = quota;
185  LOG (GNUNET_ERROR_TYPE_INFO, _ ("Loading `%s' datacache plugin\n"), name);
186  GNUNET_asprintf (&libname, "libgnunet_plugin_datacache_%s", name);
187  ret->short_name = name;
188  ret->lib_name = libname;
189  /* Load the plugin within GNUnet's default context */
192  ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
193  GNUNET_OS_init (pd);
194  if (NULL == ret->api)
195  {
196  /* Try to load the plugin within the application's context
197  This normally happens when the application is not GNUnet itself but a
198  third party; inside GNUnet this is effectively a double failure. */
199  ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
200  if (NULL == ret->api)
201  {
203  _ ("Failed to load datacache plugin for `%s'\n"),
204  name);
206  return NULL;
207  }
208  }
209  return ret;
210 }
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:104
#define LOG(kind,...)
Definition: datacache.c:32
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static unsigned long long quota
How much space are we allowed to use?
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_YES
Definition: gnunet_common.h:97
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 GNUNET_CONTAINER_BloomFilter * GNUNET_CONTAINER_bloomfilter_init(const char *data, size_t size, unsigned int k)
Create a Bloom filter from raw bits.
enum GNUNET_GenericReturnValue 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.
enum GNUNET_GenericReturnValue 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".
enum GNUNET_GenericReturnValue 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.
void GNUNET_DATACACHE_destroy(struct GNUNET_DATACACHE_Handle *h)
Destroy a data cache (and free associated resources).
Definition: datacache.c:214
char * GNUNET_DISK_mktemp(const char *t)
Create an (empty) temporary file on disk.
Definition: disk.c:381
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_INFO
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_default(void)
Return default project data used by 'libgnunetutil' for GNUnet.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_get(void)
void GNUNET_OS_init(const struct GNUNET_OS_ProjectData *pd)
Setup OS subsystem with project data.
void * GNUNET_PLUGIN_load(const char *library_name, void *arg)
Setup plugin (runs the "init" callback and returns whatever "init" returned).
Definition: plugin.c:217
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
const char * name
Internal state of the datacache library.
Definition: datacache.c:41
Project-specific data used to help the OS subsystem find installation paths.
const char * libname
Name of a library that is installed in the "lib/" directory of the project, such as "libgnunetutil".

References _, cfg, env_delete_notify(), GNUNET_asprintf(), GNUNET_CONFIGURATION_get_value_size(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_get_value_yesno(), GNUNET_CONTAINER_bloomfilter_init(), GNUNET_CONTAINER_bloomfilter_load(), GNUNET_DATACACHE_destroy(), GNUNET_DISK_mktemp(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_log_config_missing(), GNUNET_new, GNUNET_OK, GNUNET_OS_init(), GNUNET_OS_project_data_default(), GNUNET_OS_project_data_get(), GNUNET_PLUGIN_load(), GNUNET_STATISTICS_create(), GNUNET_strdup, GNUNET_YES, GNUNET_OS_ProjectData::libname, LOG, name, quota, and ret.

Referenced by GDS_DATACACHE_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_DATACACHE_destroy()

void GNUNET_DATACACHE_destroy ( struct GNUNET_DATACACHE_Handle h)

Destroy a data cache (and free associated resources).

Parameters
hhandle to the datastore

Definition at line 214 of file datacache.c.

215 {
216  if (NULL != h->filter)
218  if (NULL != h->api)
219  GNUNET_break (NULL == GNUNET_PLUGIN_unload (h->lib_name, h->api));
220  GNUNET_free (h->lib_name);
221  GNUNET_free (h->short_name);
222  GNUNET_free (h->section);
223  if (NULL != h->bloom_name)
224  {
225  if (0 != unlink (h->bloom_name))
227  "datacache",
228  "unlink",
229  h->bloom_name);
230  GNUNET_free (h->bloom_name);
231  }
233  GNUNET_free (h);
234 }
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
@ GNUNET_NO
Definition: gnunet_common.h:94
void GNUNET_CONTAINER_bloomfilter_free(struct GNUNET_CONTAINER_BloomFilter *bf)
Free the space associated with a filter in memory, flush to drive if needed (do not free the space on...
#define GNUNET_log_from_strerror_file(level, component, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_WARNING
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition: plugin.c:269
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).

References GNUNET_break, GNUNET_CONTAINER_bloomfilter_free(), GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log_from_strerror_file, GNUNET_NO, GNUNET_PLUGIN_unload(), GNUNET_STATISTICS_destroy(), and h.

Referenced by GDS_DATACACHE_done(), and GNUNET_DATACACHE_create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_DATACACHE_put()

enum GNUNET_GenericReturnValue 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_DHT_PathElement path_info 
)

Store an item in the datacache.

Parameters
hhandle to the datacache
keykey to store data under
howclose is key to our pid?
data_sizenumber of bytes in data
datadata to store
typetype of the value
discard_timewhen to discard the value in any case
path_info_lennumber of entries in path_info
path_infoa path through the network
Returns
GNUNET_OK on success, GNUNET_SYSERR on error, GNUNET_NO if duplicate

Definition at line 214 of file datacache.c.

247 {
248  ssize_t used;
249 
250  used = h->api->put (h->api->cls,
251  key,
252  xor_distance,
253  data_size,
254  data,
255  type,
256  discard_time,
257  path_info_len,
258  path_info);
259  if (-1 == used)
260  {
261  GNUNET_break (0);
262  return GNUNET_SYSERR;
263  }
264  if (0 == used)
265  {
266  /* duplicate */
267  return GNUNET_NO;
268  }
270  "Stored data under key `%s' in cache\n",
271  GNUNET_h2s (key));
272  if (NULL != h->filter)
274  GNUNET_STATISTICS_update (h->stats,
275  gettext_noop ("# bytes stored"),
276  used,
277  GNUNET_NO);
278  GNUNET_STATISTICS_update (h->stats,
279  gettext_noop ("# items stored"),
280  1,
281  GNUNET_NO);
282  while (h->utilization + used > h->env.quota)
283  GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
284  h->utilization += used;
285  return GNUNET_OK;
286 }
#define gettext_noop(String)
Definition: gettext.h:69
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
struct GNUNET_HashCode key
The key used in the DHT.
uint32_t data
The data value.
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
void GNUNET_CONTAINER_bloomfilter_add(struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Add an element to the filter.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_DEBUG
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model

Referenced by datacache_get_iterator().

Here is the caller graph for this function:

◆ GNUNET_DATACACHE_get()

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.

Parameters
hhandle to the datacache
keywhat to look up
typeentries of which type are relevant?
itermaybe NULL (to just count)
iter_clsclosure for iter
Returns
the number of results found

Definition at line 290 of file datacache.c.

295 {
296  GNUNET_STATISTICS_update (h->stats,
297  gettext_noop ("# requests received"),
298  1,
299  GNUNET_NO);
301  "Processing request for key `%s'\n",
302  GNUNET_h2s (key));
303  if ((NULL != h->filter) &&
305  {
306  GNUNET_STATISTICS_update (h->stats,
307  gettext_noop (
308  "# requests filtered by bloom filter"),
309  1,
310  GNUNET_NO);
312  "Bloomfilter filters request for key `%s'\n",
313  GNUNET_h2s (key));
314  return 0; /* can not be present */
315  }
316  return h->api->get (h->api->cls, key, type, iter, iter_cls);
317 }
int GNUNET_CONTAINER_bloomfilter_test(const struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Test if an element is in the filter.

References gettext_noop, GNUNET_CONTAINER_bloomfilter_test(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_h2s(), GNUNET_NO, GNUNET_OK, GNUNET_STATISTICS_update(), h, key, LOG, and type.

Here is the call graph for this function:

◆ GNUNET_DATACACHE_get_closest()

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.

"close" is defined as numerically larger than key (when interpreted as a circular address space), with small distance.

Parameters
hhandle to the datacache
keyarea of the keyspace to look into
num_resultsnumber of results that should be returned to iter
itermaybe NULL (to just count)
iter_clsclosure for iter
Returns
the number of results found

Definition at line 321 of file datacache.c.

326 {
327  GNUNET_STATISTICS_update (h->stats,
328  gettext_noop (
329  "# proximity search requests received"),
330  1,
331  GNUNET_NO);
333  "Processing proximity search at `%s'\n",
334  GNUNET_h2s (key));
335  return h->api->get_closest (h->api->cls, key, num_results, iter, iter_cls);
336 }

References gettext_noop, GNUNET_ERROR_TYPE_DEBUG, GNUNET_h2s(), GNUNET_NO, GNUNET_STATISTICS_update(), h, key, and LOG.

Referenced by GDS_DATACACHE_get_closest().

Here is the call graph for this function:
Here is the caller graph for this function: