2 This file is part of GNUnet
3 Copyright (C) 2005-2012 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file fs/fs_sharetree.c
23 * @brief code to manipulate the 'struct GNUNET_FS_ShareTreeItem' tree
25 * @author Christian Grothoff
28 #include "gnunet_fs_service.h"
29 #include "gnunet_scheduler_lib.h"
34 * Entry for each unique keyword to track how often
35 * it occured. Contains the keyword and the counter.
40 * This is a doubly-linked list
42 struct KeywordCounter *prev;
45 * This is a doubly-linked list
47 struct KeywordCounter *next;
50 * Keyword that was found.
55 * How many files have this keyword?
62 * Aggregate information we keep for meta data in each directory.
67 * This is a doubly-linked list
69 struct MetaCounter *prev;
72 * This is a doubly-linked list
74 struct MetaCounter *next;
77 * Name of the plugin that provided that piece of metadata
79 const char *plugin_name;
82 * MIME-type of the metadata itself
84 const char *data_mime_type;
87 * The actual meta data.
92 * Number of bytes in 'data'.
99 enum EXTRACTOR_MetaType type;
104 enum EXTRACTOR_MetaFormat format;
107 * How many files have meta entries matching this value?
108 * (type and format do not have to match).
115 * A structure that forms a singly-linked list that serves as a stack
116 * for metadata-processing function.
121 * Map from the hash over the keyword to an 'struct KeywordCounter *'
122 * counter that says how often this keyword was
123 * encountered in the current directory.
125 struct GNUNET_CONTAINER_MultiHashMap *keywordcounter;
128 * Map from the hash over the metadata to an 'struct MetaCounter *'
129 * counter that says how often this metadata was
130 * encountered in the current directory.
132 struct GNUNET_CONTAINER_MultiHashMap *metacounter;
135 * Position we are currently manipulating.
137 struct GNUNET_FS_ShareTreeItem *pos;
140 * Number of times an item has to be found to be moved to the parent.
142 unsigned int move_threshold;
147 * Add the given keyword to the keyword statistics tracker.
149 * @param cls the multihashmap we store the keyword counters in
150 * @param keyword the keyword to count
151 * @param is_mandatory ignored
152 * @return always GNUNET_OK
155 add_to_keyword_counter (void *cls, const char *keyword, int is_mandatory)
157 struct GNUNET_CONTAINER_MultiHashMap *mcm = cls;
158 struct KeywordCounter *cnt;
159 struct GNUNET_HashCode hc;
162 klen = strlen (keyword) + 1;
163 GNUNET_CRYPTO_hash (keyword, klen - 1, &hc);
164 cnt = GNUNET_CONTAINER_multihashmap_get (mcm, &hc);
167 cnt = GNUNET_malloc (sizeof(struct KeywordCounter) + klen);
168 cnt->value = (const char *) &cnt[1];
169 GNUNET_memcpy (&cnt[1], keyword, klen);
170 GNUNET_assert (GNUNET_OK ==
171 GNUNET_CONTAINER_multihashmap_put (mcm,
173 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
181 * Function called on each meta data item. Increments the
182 * respective counter.
184 * @param cls the container multihashmap to update
185 * @param plugin_name name of the plugin that produced this value;
186 * special values can be used (i.e. '<zlib>' for zlib being
187 * used in the main libextractor library and yielding
189 * @param type libextractor-type describing the meta data
190 * @param format basic format information about data
191 * @param data_mime_type mime-type of data (not of the original file);
192 * can be NULL (if mime-type is not known)
193 * @param data actual meta-data found
194 * @param data_len number of bytes in data
195 * @return 0 to continue extracting / iterating
198 add_to_meta_counter (void *cls, const char *plugin_name,
199 enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat
201 const char *data_mime_type, const char *data, size_t
204 struct GNUNET_CONTAINER_MultiHashMap *map = cls;
205 struct GNUNET_HashCode key;
206 struct MetaCounter *cnt;
208 GNUNET_CRYPTO_hash (data, data_len, &key);
209 cnt = GNUNET_CONTAINER_multihashmap_get (map, &key);
212 cnt = GNUNET_new (struct MetaCounter);
214 cnt->data_size = data_len;
215 cnt->plugin_name = plugin_name;
217 cnt->format = format;
218 cnt->data_mime_type = data_mime_type;
219 GNUNET_assert (GNUNET_OK ==
220 GNUNET_CONTAINER_multihashmap_put (map,
222 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
230 * Remove keywords above the threshold.
232 * @param cls the 'struct TrimContext' with the pos to remove the keywords from
233 * @param keyword the keyword to check
234 * @param is_mandatory ignored
235 * @return always GNUNET_OK
238 remove_high_frequency_keywords (void *cls, const char *keyword, int
241 struct TrimContext *tc = cls;
242 struct KeywordCounter *counter;
243 struct GNUNET_HashCode hc;
246 klen = strlen (keyword) + 1;
247 GNUNET_CRYPTO_hash (keyword, klen - 1, &hc);
248 counter = GNUNET_CONTAINER_multihashmap_get (tc->keywordcounter, &hc);
249 GNUNET_assert (NULL != counter);
250 if (counter->count < tc->move_threshold)
252 GNUNET_FS_uri_ksk_remove_keyword (tc->pos->ksk_uri,
259 * Move "frequent" keywords over to the target ksk uri, free the
262 * @param cls the 'struct TrimContext'
263 * @param key key of the entry
264 * @param value the 'struct KeywordCounter'
265 * @return GNUNET_YES (always)
268 migrate_and_drop_keywords (void *cls, const struct GNUNET_HashCode *key,
271 struct TrimContext *tc = cls;
272 struct KeywordCounter *counter = value;
274 if (counter->count >= tc->move_threshold)
276 if (NULL == tc->pos->ksk_uri)
277 tc->pos->ksk_uri = GNUNET_FS_uri_ksk_create_from_args (1,
280 GNUNET_FS_uri_ksk_add_keyword (tc->pos->ksk_uri, counter->value,
283 GNUNET_assert (GNUNET_YES ==
284 GNUNET_CONTAINER_multihashmap_remove (tc->keywordcounter,
287 GNUNET_free (counter);
293 * Copy "frequent" metadata items over to the
294 * target metadata container, free the counters.
296 * @param cls the 'struct TrimContext'
297 * @param key key of the entry
298 * @param value the 'struct KeywordCounter'
299 * @return GNUNET_YES (always)
302 migrate_and_drop_metadata (void *cls, const struct GNUNET_HashCode *key,
305 struct TrimContext *tc = cls;
306 struct MetaCounter *counter = value;
308 if (counter->count >= tc->move_threshold)
310 if (NULL == tc->pos->meta)
311 tc->pos->meta = GNUNET_CONTAINER_meta_data_create ();
312 GNUNET_CONTAINER_meta_data_insert (tc->pos->meta,
313 counter->plugin_name,
316 counter->data_mime_type, counter->data,
319 GNUNET_assert (GNUNET_YES ==
320 GNUNET_CONTAINER_multihashmap_remove (tc->metacounter,
323 GNUNET_free (counter);
329 * Process a share item tree, moving frequent keywords up and
330 * copying frequent metadata up.
332 * @param tc trim context with hash maps to use
333 * @param tree tree to trim
336 share_tree_trim (struct TrimContext *tc,
337 struct GNUNET_FS_ShareTreeItem *tree)
339 struct GNUNET_FS_ShareTreeItem *pos;
340 unsigned int num_children;
342 /* first, trim all children */
344 for (pos = tree->children_head; NULL != pos; pos = pos->next)
346 share_tree_trim (tc, pos);
350 /* consider adding filename to directory meta data */
351 if (tree->is_directory == GNUNET_YES)
353 const char *user = getenv ("USER");
354 if ((user == NULL) ||
355 (0 != strncasecmp (user, tree->short_filename, strlen (user))))
357 /* only use filename if it doesn't match $USER */
358 if (NULL == tree->meta)
359 tree->meta = GNUNET_CONTAINER_meta_data_create ();
360 GNUNET_CONTAINER_meta_data_insert (tree->meta, "<libgnunetfs>",
361 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
362 EXTRACTOR_METAFORMAT_UTF8,
363 "text/plain", tree->short_filename,
364 strlen (tree->short_filename) + 1);
368 if (1 >= num_children)
369 return; /* nothing to trim */
371 /* now, count keywords and meta data in children */
372 for (pos = tree->children_head; NULL != pos; pos = pos->next)
374 if (NULL != pos->meta)
375 GNUNET_CONTAINER_meta_data_iterate (pos->meta, &add_to_meta_counter,
377 if (NULL != pos->ksk_uri)
378 GNUNET_FS_uri_ksk_get_keywords (pos->ksk_uri, &add_to_keyword_counter,
382 /* calculate threshold for moving keywords / meta data */
383 tc->move_threshold = 1 + (num_children / 2);
385 /* remove high-frequency keywords from children */
386 for (pos = tree->children_head; NULL != pos; pos = pos->next)
389 if (NULL != pos->ksk_uri)
391 struct GNUNET_FS_Uri *ksk_uri_copy = GNUNET_FS_uri_dup (pos->ksk_uri);
392 GNUNET_FS_uri_ksk_get_keywords (ksk_uri_copy,
393 &remove_high_frequency_keywords, tc);
394 GNUNET_FS_uri_destroy (ksk_uri_copy);
398 /* add high-frequency meta data and keywords to parent */
400 GNUNET_CONTAINER_multihashmap_iterate (tc->keywordcounter,
401 &migrate_and_drop_keywords,
403 GNUNET_CONTAINER_multihashmap_iterate (tc->metacounter,
404 &migrate_and_drop_metadata,
410 * Process a share item tree, moving frequent keywords up and
411 * copying frequent metadata up.
413 * @param toplevel toplevel directory in the tree, returned by the scanner
416 GNUNET_FS_share_tree_trim (struct GNUNET_FS_ShareTreeItem *toplevel)
418 struct TrimContext tc;
420 if (toplevel == NULL)
422 tc.keywordcounter = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
423 tc.metacounter = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
424 share_tree_trim (&tc, toplevel);
425 GNUNET_CONTAINER_multihashmap_destroy (tc.keywordcounter);
426 GNUNET_CONTAINER_multihashmap_destroy (tc.metacounter);
431 * Release memory of a share item tree.
433 * @param toplevel toplevel of the tree to be freed
436 GNUNET_FS_share_tree_free (struct GNUNET_FS_ShareTreeItem *toplevel)
438 struct GNUNET_FS_ShareTreeItem *pos;
440 while (NULL != (pos = toplevel->children_head))
441 GNUNET_FS_share_tree_free (pos);
442 if (NULL != toplevel->parent)
443 GNUNET_CONTAINER_DLL_remove (toplevel->parent->children_head,
444 toplevel->parent->children_tail,
446 if (NULL != toplevel->meta)
447 GNUNET_CONTAINER_meta_data_destroy (toplevel->meta);
448 if (NULL != toplevel->ksk_uri)
449 GNUNET_FS_uri_destroy (toplevel->ksk_uri);
450 GNUNET_free_non_null (toplevel->filename);
451 GNUNET_free_non_null (toplevel->short_filename);
452 GNUNET_free (toplevel);
456 /* end fs_sharetree.c */