aboutsummaryrefslogtreecommitdiff
path: root/src/datacache/datacache.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/datacache/datacache.c')
-rw-r--r--src/datacache/datacache.c174
1 files changed, 74 insertions, 100 deletions
diff --git a/src/datacache/datacache.c b/src/datacache/datacache.c
index b937c63bb..a17eb9628 100644
--- a/src/datacache/datacache.c
+++ b/src/datacache/datacache.c
@@ -70,7 +70,7 @@ struct GNUNET_DATACACHE_Handle
70 * Name of the library (i.e. "gnunet_plugin_datacache_sqlite"). 70 * Name of the library (i.e. "gnunet_plugin_datacache_sqlite").
71 */ 71 */
72 char *lib_name; 72 char *lib_name;
73 73
74 /** 74 /**
75 * Name for the bloom filter file. 75 * Name for the bloom filter file.
76 */ 76 */
@@ -97,19 +97,16 @@ struct GNUNET_DATACACHE_Handle
97 * @param key key of the content that was deleted 97 * @param key key of the content that was deleted
98 * @param size number of bytes that were made available 98 * @param size number of bytes that were made available
99 */ 99 */
100static void 100static void
101env_delete_notify (void *cls, 101env_delete_notify (void *cls, const GNUNET_HashCode * key, size_t size)
102 const GNUNET_HashCode *key,
103 size_t size)
104{ 102{
105 struct GNUNET_DATACACHE_Handle * h = cls; 103 struct GNUNET_DATACACHE_Handle *h = cls;
104
106 GNUNET_assert (h->utilization >= size); 105 GNUNET_assert (h->utilization >= size);
107 h->utilization -= size; 106 h->utilization -= size;
108 GNUNET_CONTAINER_bloomfilter_remove (h->filter, key); 107 GNUNET_CONTAINER_bloomfilter_remove (h->filter, key);
109 GNUNET_STATISTICS_update (h->stats, 108 GNUNET_STATISTICS_update (h->stats,
110 gettext_noop ("# bytes stored"), 109 gettext_noop ("# bytes stored"), -size, GNUNET_NO);
111 -size,
112 GNUNET_NO);
113} 110}
114 111
115 112
@@ -122,7 +119,7 @@ env_delete_notify (void *cls,
122 */ 119 */
123struct GNUNET_DATACACHE_Handle * 120struct GNUNET_DATACACHE_Handle *
124GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg, 121GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
125 const char *section) 122 const char *section)
126{ 123{
127 unsigned int bf_size; 124 unsigned int bf_size;
128 unsigned long long quota; 125 unsigned long long quota;
@@ -131,45 +128,38 @@ GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
131 char *name; 128 char *name;
132 129
133 if (GNUNET_OK != 130 if (GNUNET_OK !=
134 GNUNET_CONFIGURATION_get_value_number (cfg, 131 GNUNET_CONFIGURATION_get_value_number (cfg, section, "QUOTA", &quota))
135 section, "QUOTA", &quota)) 132 {
136 { 133 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 134 _("No `%s' specified for `%s' in configuration!\n"),
138 _("No `%s' specified for `%s' in configuration!\n"), 135 "QUOTA", section);
139 "QUOTA", 136 return NULL;
140 section); 137 }
141 return NULL;
142 }
143 if (GNUNET_OK != 138 if (GNUNET_OK !=
144 GNUNET_CONFIGURATION_get_value_string (cfg, 139 GNUNET_CONFIGURATION_get_value_string (cfg, section, "DATABASE", &name))
145 section, 140 {
146 "DATABASE", &name)) 141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
147 { 142 _("No `%s' specified for `%s' in configuration!\n"),
148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 143 "DATABASE", section);
149 _("No `%s' specified for `%s' in configuration!\n"), 144 return NULL;
150 "DATABASE", 145 }
151 section); 146 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
152 return NULL; 147
153 } 148 ret = GNUNET_malloc (sizeof (struct GNUNET_DATACACHE_Handle));
154 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
155
156 ret = GNUNET_malloc (sizeof(struct GNUNET_DATACACHE_Handle));
157 ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom"); 149 ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
158 if (NULL != ret->bloom_name) 150 if (NULL != ret->bloom_name)
159 { 151 {
160 ret->filter = GNUNET_CONTAINER_bloomfilter_load (ret->bloom_name, 152 ret->filter = GNUNET_CONTAINER_bloomfilter_load (ret->bloom_name, quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
161 quota / 1024, /* 8 bit per entry in DB, expect 1k entries */ 153 5);
162 5); 154 }
163 }
164 else 155 else
165 { 156 {
166 ret->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */ 157 ret->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
167 } 158 }
168 ret->stats = GNUNET_STATISTICS_create ("datacache", 159 ret->stats = GNUNET_STATISTICS_create ("datacache", cfg);
169 cfg);
170 ret->section = GNUNET_strdup (section); 160 ret->section = GNUNET_strdup (section);
171 ret->env.cfg = cfg; 161 ret->env.cfg = cfg;
172 ret->env.delete_notify = &env_delete_notify; 162 ret->env.delete_notify = &env_delete_notify;
173 ret->env.section = ret->section; 163 ret->env.section = ret->section;
174 ret->env.cls = ret; 164 ret->env.cls = ret;
175 ret->env.delete_notify = &env_delete_notify; 165 ret->env.delete_notify = &env_delete_notify;
@@ -181,12 +171,12 @@ GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
181 ret->lib_name = libname; 171 ret->lib_name = libname;
182 ret->api = GNUNET_PLUGIN_load (libname, &ret->env); 172 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
183 if (ret->api == NULL) 173 if (ret->api == NULL)
184 { 174 {
185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
186 _("Failed to load datacache plugin for `%s'\n"), name); 176 _("Failed to load datacache plugin for `%s'\n"), name);
187 GNUNET_DATACACHE_destroy (ret); 177 GNUNET_DATACACHE_destroy (ret);
188 return NULL; 178 return NULL;
189 } 179 }
190 return ret; 180 return ret;
191} 181}
192 182
@@ -196,7 +186,8 @@ GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
196 * 186 *
197 * @param h handle to the datastore 187 * @param h handle to the datastore
198 */ 188 */
199void GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h) 189void
190GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
200{ 191{
201 if (h->filter != NULL) 192 if (h->filter != NULL)
202 GNUNET_CONTAINER_bloomfilter_free (h->filter); 193 GNUNET_CONTAINER_bloomfilter_free (h->filter);
@@ -206,15 +197,13 @@ void GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
206 GNUNET_free (h->short_name); 197 GNUNET_free (h->short_name);
207 GNUNET_free (h->section); 198 GNUNET_free (h->section);
208 if (h->bloom_name != NULL) 199 if (h->bloom_name != NULL)
209 { 200 {
210 if (0 != UNLINK (h->bloom_name)) 201 if (0 != UNLINK (h->bloom_name))
211 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 202 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
212 "unlink", 203 "unlink", h->bloom_name);
213 h->bloom_name); 204 GNUNET_free (h->bloom_name);
214 GNUNET_free (h->bloom_name); 205 }
215 } 206 GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
216 GNUNET_STATISTICS_destroy (h->stats,
217 GNUNET_NO);
218 GNUNET_free (h); 207 GNUNET_free (h);
219} 208}
220 209
@@ -230,31 +219,24 @@ void GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
230 * @param discard_time when to discard the value in any case 219 * @param discard_time when to discard the value in any case
231 * @return GNUNET_OK on success, GNUNET_SYSERR on error (full, etc.) 220 * @return GNUNET_OK on success, GNUNET_SYSERR on error (full, etc.)
232 */ 221 */
233int 222int
234GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h, 223GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
235 const GNUNET_HashCode * key, 224 const GNUNET_HashCode * key,
236 size_t size, 225 size_t size,
237 const char *data, 226 const char *data,
238 enum GNUNET_BLOCK_Type type, 227 enum GNUNET_BLOCK_Type type,
239 struct GNUNET_TIME_Absolute discard_time) 228 struct GNUNET_TIME_Absolute discard_time)
240{ 229{
241 uint32_t used; 230 uint32_t used;
242 231
243 used = h->api->put (h->api->cls, 232 used = h->api->put (h->api->cls, key, size, data, type, discard_time);
244 key,
245 size,
246 data,
247 type,
248 discard_time);
249 if (used == 0) 233 if (used == 0)
250 { 234 {
251 GNUNET_break (0); 235 GNUNET_break (0);
252 return GNUNET_SYSERR; 236 return GNUNET_SYSERR;
253 } 237 }
254 GNUNET_STATISTICS_update (h->stats, 238 GNUNET_STATISTICS_update (h->stats,
255 gettext_noop ("# bytes stored"), 239 gettext_noop ("# bytes stored"), size, GNUNET_NO);
256 size,
257 GNUNET_NO);
258 GNUNET_CONTAINER_bloomfilter_add (h->filter, key); 240 GNUNET_CONTAINER_bloomfilter_add (h->filter, key);
259 while (h->utilization + used > h->env.quota) 241 while (h->utilization + used > h->env.quota)
260 GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls)); 242 GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
@@ -274,31 +256,23 @@ GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
274 * @param iter_cls closure for iter 256 * @param iter_cls closure for iter
275 * @return the number of results found 257 * @return the number of results found
276 */ 258 */
277unsigned int 259unsigned int
278GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h, 260GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
279 const GNUNET_HashCode * key, 261 const GNUNET_HashCode * key,
280 enum GNUNET_BLOCK_Type type, 262 enum GNUNET_BLOCK_Type type,
281 GNUNET_DATACACHE_Iterator iter, 263 GNUNET_DATACACHE_Iterator iter, void *iter_cls)
282 void *iter_cls)
283{ 264{
284 GNUNET_STATISTICS_update (h->stats, 265 GNUNET_STATISTICS_update (h->stats,
285 gettext_noop ("# requests received"), 266 gettext_noop ("# requests received"), 1, GNUNET_NO);
286 1, 267 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key))
287 GNUNET_NO); 268 {
288 if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, 269 GNUNET_STATISTICS_update (h->stats,
289 key)) 270 gettext_noop
290 { 271 ("# requests filtered by bloom filter"), 1,
291 GNUNET_STATISTICS_update (h->stats, 272 GNUNET_NO);
292 gettext_noop ("# requests filtered by bloom filter"), 273 return 0; /* can not be present */
293 1, 274 }
294 GNUNET_NO); 275 return h->api->get (h->api->cls, key, type, iter, iter_cls);
295 return 0; /* can not be present */
296 }
297 return h->api->get (h->api->cls,
298 key,
299 type,
300 iter,
301 iter_cls);
302} 276}
303 277
304 278