GNUnet 0.24.4-talerdev.1-12-g8362a9920
 
Loading...
Searching...
No Matches
datacache.c File Reference

datacache API implementation More...

Include dependency graph for datacache.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_DATACACHE_Handle
 Internal state of the datacache library. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "datacache", __VA_ARGS__)
 
#define LOG_STRERROR_FILE(kind, op, fn)    GNUNET_log_from_strerror_file (kind, "datacache", op, fn)
 

Functions

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.
 
struct GNUNET_DATACACHE_HandleGNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
 Create a data cache.
 
void GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
 Destroy a data cache (and free associated resources).
 
enum GNUNET_GenericReturnValue GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h, uint32_t xor_distance, const struct GNUNET_DATACACHE_Block *block)
 Store an item in the datacache.
 
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.
 
unsigned int GNUNET_DATACACHE_get_closest (struct GNUNET_DATACACHE_Handle *h, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, 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.
 

Detailed Description

datacache API implementation

Author
Christian Grothoff

Definition in file datacache.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "datacache", __VA_ARGS__)

Definition at line 32 of file datacache.c.

◆ LOG_STRERROR_FILE

#define LOG_STRERROR_FILE (   kind,
  op,
  fn 
)     GNUNET_log_from_strerror_file (kind, "datacache", op, fn)

Definition at line 34 of file datacache.c.

40{
45
49 const struct GNUNET_CONFIGURATION_Handle *cfg;
50
55
59 char *section;
60
66
70 char *short_name;
71
75 char *lib_name;
76
80 char *bloom_name;
81
86
90 unsigned long long utilization;
91};
92
93
102static void
104 const struct GNUNET_HashCode *key,
105 size_t size)
106{
107 struct GNUNET_DATACACHE_Handle *h = cls;
108
110 "Content under key `%s' discarded\n",
111 GNUNET_h2s (key));
112 GNUNET_assert (h->utilization >= size);
113 h->utilization -= size;
116 "# bytes stored",
117 -(long long) size,
118 GNUNET_NO);
120 "# items stored",
121 -1,
122 GNUNET_NO);
123}
124
125
128 const char *section)
129{
130 unsigned int bf_size;
131 unsigned long long quota;
133 char *libname;
134 char *name;
135
136 if (GNUNET_OK !=
138 section,
139 "QUOTA",
140 &quota))
141 {
143 section,
144 "QUOTA");
145 return NULL;
146 }
147 if (GNUNET_OK !=
149 section,
150 "DATABASE",
151 &name))
152 {
154 section,
155 "DATABASE");
156 return NULL;
157 }
158 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
159
161
162 if (GNUNET_YES !=
164 section,
165 "DISABLE_BF"))
166 {
167 if (GNUNET_YES !=
169 section,
170 "DISABLE_BF_RC"))
171 {
172 ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
173 }
174 if (NULL != ret->bloom_name)
175 {
177 ret->bloom_name,
178 quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
179 5);
180 }
181 if (NULL == ret->filter)
182 {
183 ret->filter =
185 bf_size,
186 5); /* approx. 3% false positives at max use */
187 }
188 }
189 ret->stats = GNUNET_STATISTICS_create ("datacache",
190 cfg);
191 ret->section = GNUNET_strdup (section);
192 ret->env.cfg = cfg;
193 ret->env.delete_notify = &env_delete_notify;
194 ret->env.section = ret->section;
195 ret->env.cls = ret;
196 ret->env.delete_notify = &env_delete_notify;
197 ret->env.quota = quota;
199 "Loading `%s' datacache plugin\n",
200 name);
201 GNUNET_asprintf (&libname,
202 "libgnunet_plugin_datacache_%s",
203 name);
204 ret->short_name = name;
205 ret->lib_name = libname;
206 /* Load the plugin within GNUnet's default context */
208 libname,
209 &ret->env);
210 if (NULL == ret->api)
211 {
213 "Failed to load datacache plugin for `%s'\n",
214 name);
216 return NULL;
217 }
218 return ret;
219}
220
221
222void
224{
225 if (NULL != h->filter)
227 if (NULL != h->api)
228 GNUNET_break (NULL ==
229 GNUNET_PLUGIN_unload (h->lib_name,
230 h->api));
231 GNUNET_free (h->lib_name);
232 GNUNET_free (h->short_name);
233 GNUNET_free (h->section);
234 if (NULL != h->bloom_name)
235 {
236 if (0 != unlink (h->bloom_name))
238 "datacache",
239 "unlink",
240 h->bloom_name);
241 GNUNET_free (h->bloom_name);
242 }
244 GNUNET_free (h);
245}
246
247
250 uint32_t xor_distance,
251 const struct GNUNET_DATACACHE_Block *block)
252{
253 ssize_t used;
254
255 used = h->api->put (h->api->cls,
256 xor_distance,
257 block);
258 if (-1 == used)
259 {
260 GNUNET_break (0);
261 return GNUNET_SYSERR;
262 }
263 if (0 == used)
264 {
265 /* duplicate */
266 return GNUNET_NO;
267 }
269 "Stored data under key `%s' in cache\n",
270 GNUNET_h2s (&block->key));
271 if (NULL != h->filter)
273 &block->key);
275 "# bytes stored",
276 used,
277 GNUNET_NO);
279 "# items stored",
280 1,
281 GNUNET_NO);
282 while (h->utilization + used > h->env.quota)
284 h->api->del (h->api->cls));
285 h->utilization += used;
286 return GNUNET_OK;
287}
288
289
290unsigned int
292 const struct GNUNET_HashCode *key,
295 void *iter_cls)
296{
298 "# requests received",
299 1,
300 GNUNET_NO);
302 "Processing request for key `%s'\n",
303 GNUNET_h2s (key));
304 if ( (NULL != h->filter) &&
305 (GNUNET_OK !=
307 key)) )
308 {
310 "# requests filtered by bloom filter",
311 1,
312 GNUNET_NO);
314 "Bloomfilter filters request for key `%s'\n",
315 GNUNET_h2s (key));
316 return 0; /* can not be present */
317 }
318 return h->api->get (h->api->cls,
319 key,
320 type,
321 iter, iter_cls);
322}
323
324
325unsigned int
327 const struct GNUNET_HashCode *key,
329 unsigned int num_results,
331 void *iter_cls)
332{
334 "# proximity search requests received",
335 1,
336 GNUNET_NO);
338 "Processing proximity search at `%s'\n",
339 GNUNET_h2s (key));
340 return h->api->get_closest (h->api->cls,
341 key,
342 type,
343 num_results,
344 iter,
345 iter_cls);
346}
347
348
349/* end of datacache.c */
struct GNUNET_MQ_Envelope * env
Definition 005.c:1
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 struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition gnunet-arm.c:98
static int ret
Final status code.
Definition gnunet-arm.c:93
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition gnunet-arm.c:108
struct GNUNET_HashCode key
The key used in the DHT.
static char * name
Name (label) of the records to list.
static uint32_t type
Type string converted to DNS type value.
static struct GNUNET_CONTAINER_BloomFilter * filter
Bloomfilter to quickly tell if we don't have the content.
static unsigned long long quota
How much space are we allowed to use?
static struct GNUNET_STATISTICS_Handle * stats
Handle to the statistics service.
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
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 GNUNET_CONTAINER_bloomfilter_add(struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Add an element to the filter.
bool GNUNET_CONTAINER_bloomfilter_test(const struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Test if an element is in the filter.
struct GNUNET_CONTAINER_BloomFilter * GNUNET_CONTAINER_bloomfilter_load(const char *filename, size_t size, unsigned int k)
Load a Bloom filter from a file.
void GNUNET_CONTAINER_bloomfilter_remove(struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Remove an element from the filter.
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...
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.
const struct GNUNET_OS_ProjectData * GNUNET_CONFIGURATION_get_project_data(const struct GNUNET_CONFIGURATION_Handle *cfg)
Return the project data associated with this configuration.
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:292
enum GNUNET_GenericReturnValue GNUNET_DATACACHE_put(struct GNUNET_DATACACHE_Handle *h, uint32_t xor_distance, const struct GNUNET_DATACACHE_Block *block)
Store an item in the datacache.
Definition datacache.c:250
struct GNUNET_DATACACHE_Handle * GNUNET_DATACACHE_create(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
Create a data cache.
Definition datacache.c:128
enum GNUNET_GenericReturnValue(* GNUNET_DATACACHE_Iterator)(void *cls, const struct GNUNET_DATACACHE_Block *block)
An iterator over a set of items stored in the datacache.
void GNUNET_DATACACHE_destroy(struct GNUNET_DATACACHE_Handle *h)
Destroy a data cache (and free associated resources).
Definition datacache.c:224
unsigned int GNUNET_DATACACHE_get_closest(struct GNUNET_DATACACHE_Handle *h, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, 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:327
char * GNUNET_DISK_mktemp(const char *t)
Create an (empty) temporary file on disk.
Definition disk.c:431
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#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_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
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_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
@ 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.
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_PLUGIN_load(const struct GNUNET_OS_ProjectData *pd, const char *library_name, void *arg)
Setup plugin (runs the "init" callback and returns whatever "init" returned).
Definition plugin.c:221
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition plugin.c:277
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
static unsigned int size
Size of the "table".
Definition peer.c:68
Information about a block stored in the datacache.
struct GNUNET_HashCode key
Key of the block.
Internal state of the datacache library.
Definition datacache.c:41
char * section
Configuration section to use.
Definition datacache.c:60
The datastore service will pass a pointer to a struct of this type as the first and only argument to ...
void * cls
Closure to use for callbacks.
struct returned by the initialization function of the plugin
A 512-bit hashcode.
Handle for the service.

Function Documentation

◆ env_delete_notify()

static void env_delete_notify ( void *  cls,
const struct GNUNET_HashCode key,
size_t  size 
)
static

Function called by plugins to notify the datacache about content deletions.

Parameters
clsclosure
keykey of the content that was deleted
sizenumber of bytes that were made available

Definition at line 104 of file datacache.c.

107{
108 struct GNUNET_DATACACHE_Handle *h = cls;
109
111 "Content under key `%s' discarded\n",
112 GNUNET_h2s (key));
113 GNUNET_assert (h->utilization >= size);
114 h->utilization -= size;
117 "# bytes stored",
118 -(long long) size,
119 GNUNET_NO);
121 "# items stored",
122 -1,
123 GNUNET_NO);
124}

References GNUNET_assert, GNUNET_CONTAINER_bloomfilter_remove(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_h2s(), GNUNET_NO, GNUNET_STATISTICS_update(), h, key, LOG, and size.

Referenced by GNUNET_DATACACHE_create().

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