2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2006, 2008 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file util/container_bloomfilter.c
22 * @brief data structure used to reduce disk accesses.
24 * The idea basically: Create a signature for each element in the
25 * database. Add those signatures to a bit array. When doing a lookup,
26 * check if the bit array matches the signature of the requested
27 * element. If yes, address the disk, otherwise return 'not found'.
29 * A property of the bloom filter is that sometimes we will have
30 * a match even if the element is not on the disk (then we do
31 * an unnecessary disk access), but what's most important is that
32 * we never get a single "false negative".
34 * To be able to delete entries from the bloom filter, we maintain
35 * a 4 bit counter in the file on the drive (we still use only one
38 * @author Igor Wronsky
39 * @author Christian Grothoff
43 #include "gnunet_common.h"
44 #include "gnunet_container_lib.h"
45 #include "gnunet_disk_lib.h"
47 struct GNUNET_CONTAINER_BloomFilter
51 * The actual bloomfilter bit array
56 * Filename of the filter
61 * The bit counter file on disk
63 struct GNUNET_DISK_FileHandle *fh;
66 * How many bits we set for each stored element
68 unsigned int addressesPerElement;
71 * Size of bitArray in bytes
80 * Get size of the bloom filter.
82 * @param bf the filter
83 * @return number of bytes used for the data of the bloom filter
86 GNUNET_CONTAINER_bloomfilter_get_size (const struct GNUNET_CONTAINER_BloomFilter
91 return bf->bitArraySize;
96 * Copy an existing memory. Any association with a file
97 * on-disk will be lost in the process.
98 * @param bf the filter to copy
99 * @return copy of the bf
101 struct GNUNET_CONTAINER_BloomFilter *
102 GNUNET_CONTAINER_bloomfilter_copy (const struct GNUNET_CONTAINER_BloomFilter *bf)
104 return GNUNET_CONTAINER_bloomfilter_init (bf->bitArray,
106 bf->addressesPerElement);
111 * Sets a bit active in the bitArray. Increment bit-specific
112 * usage counter on disk only if below 4bit max (==15).
114 * @param bitArray memory area to set the bit in
115 * @param bitIdx which bit to set
118 setBit (char *bitArray, unsigned int bitIdx)
121 unsigned int targetBit;
123 arraySlot = bitIdx / 8;
124 targetBit = (1L << (bitIdx % 8));
125 bitArray[arraySlot] |= targetBit;
129 * Clears a bit from bitArray. Bit is cleared from the array
130 * only if the respective usage counter on the disk hits/is zero.
132 * @param bitArray memory area to set the bit in
133 * @param bitIdx which bit to unset
136 clearBit (char *bitArray, unsigned int bitIdx)
139 unsigned int targetBit;
142 targetBit = (1L << (bitIdx % 8));
143 bitArray[slot] = bitArray[slot] & (~targetBit);
147 * Checks if a bit is active in the bitArray
149 * @param bitArray memory area to set the bit in
150 * @param bitIdx which bit to test
151 * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
154 testBit (char *bitArray, unsigned int bitIdx)
157 unsigned int targetBit;
160 targetBit = (1L << (bitIdx % 8));
161 if (bitArray[slot] & targetBit)
168 * Sets a bit active in the bitArray and increments
169 * bit-specific usage counter on disk (but only if
170 * the counter was below 4 bit max (==15)).
172 * @param bitArray memory area to set the bit in
173 * @param bitIdx which bit to test
174 * @param fh A file to keep the 4 bit address usage counters in
177 incrementBit (char *bitArray, unsigned int bitIdx,
178 const struct GNUNET_DISK_FileHandle *fh)
184 unsigned int targetLoc;
186 setBit (bitArray, bitIdx);
187 if (GNUNET_DISK_handle_invalid (fh))
189 /* Update the counter file on disk */
190 fileSlot = bitIdx / 2;
191 targetLoc = bitIdx % 2;
193 GNUNET_assert (fileSlot ==
194 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET));
195 if (1 != GNUNET_DISK_file_read (fh, &value, 1))
198 high = (value & (~0xF)) >> 4;
210 value = ((high << 4) | low);
211 GNUNET_assert (fileSlot == GNUNET_DISK_file_seek (fh,
213 GNUNET_DISK_SEEK_SET));
214 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
218 * Clears a bit from bitArray if the respective usage
219 * counter on the disk hits/is zero.
221 * @param bitArray memory area to set the bit in
222 * @param bitIdx which bit to test
223 * @param fh A file to keep the 4bit address usage counters in
226 decrementBit (char *bitArray, unsigned int bitIdx,
227 const struct GNUNET_DISK_FileHandle *fh)
233 unsigned int targetLoc;
235 if (GNUNET_DISK_handle_invalid (fh))
236 return; /* cannot decrement! */
237 /* Each char slot in the counter file holds two 4 bit counters */
238 fileSlot = bitIdx / 2;
239 targetLoc = bitIdx % 2;
240 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET);
241 if (1 != GNUNET_DISK_file_read (fh, &value, 1))
244 high = (value & 0xF0) >> 4;
246 /* decrement, but once we have reached the max, never go back! */
249 if ((low > 0) && (low < 0xF))
253 clearBit (bitArray, bitIdx);
258 if ((high > 0) && (high < 0xF))
262 clearBit (bitArray, bitIdx);
265 value = ((high << 4) | low);
266 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET);
267 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
270 #define BUFFSIZE 65536
273 * Creates a file filled with zeroes
275 * @param fh the file handle
276 * @param size the size of the file
277 * @return GNUNET_OK if created ok, GNUNET_SYSERR otherwise
280 makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, size_t size)
283 size_t bytesleft = size;
286 if (GNUNET_DISK_handle_invalid (fh))
287 return GNUNET_SYSERR;
288 buffer = GNUNET_malloc (BUFFSIZE);
289 memset (buffer, 0, BUFFSIZE);
290 GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_SET);
292 while (bytesleft > 0)
294 if (bytesleft > BUFFSIZE)
296 res = GNUNET_DISK_file_write (fh, buffer, BUFFSIZE);
297 bytesleft -= BUFFSIZE;
301 res = GNUNET_DISK_file_write (fh, buffer, bytesleft);
304 GNUNET_assert (res != GNUNET_SYSERR);
306 GNUNET_free (buffer);
310 /* ************** GNUNET_CONTAINER_BloomFilter iterator ********* */
313 * Iterator (callback) method to be called by the
314 * bloomfilter iterator on each bit that is to be
315 * set or tested for the key.
318 * @param bf the filter to manipulate
319 * @param bit the current bit
321 typedef void (*BitIterator) (void *cls,
322 const struct GNUNET_CONTAINER_BloomFilter *bf,
326 * Call an iterator for each bit that the bloomfilter
327 * must test or set for this element.
329 * @param bf the filter
330 * @param callback the method to call
331 * @param arg extra argument to callback
332 * @param key the key for which we iterate over the BF bits
335 iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
336 BitIterator callback, void *arg, const GNUNET_HashCode *key)
338 GNUNET_HashCode tmp[2];
341 unsigned int slot = 0;
343 bitCount = bf->addressesPerElement;
344 memcpy (&tmp[0], key, sizeof (GNUNET_HashCode));
348 while (slot < (sizeof (GNUNET_HashCode) / sizeof (uint32_t)))
352 (((uint32_t *) &tmp[round & 1])[slot]) &
353 ((bf->bitArraySize * 8) - 1));
361 GNUNET_CRYPTO_hash (&tmp[round & 1], sizeof (GNUNET_HashCode),
362 &tmp[(round + 1) & 1]);
370 * Callback: increment bit
372 * @param cls pointer to writeable form of bf
373 * @param bf the filter to manipulate
374 * @param bit the bit to increment
377 incrementBitCallback (void *cls,
378 const struct GNUNET_CONTAINER_BloomFilter *bf,
381 struct GNUNET_CONTAINER_BloomFilter *b = cls;
382 incrementBit (b->bitArray, bit, bf->fh);
386 * Callback: decrement bit
388 * @param cls pointer to writeable form of bf
389 * @param bf the filter to manipulate
390 * @param bit the bit to decrement
393 decrementBitCallback (void *cls,
394 const struct GNUNET_CONTAINER_BloomFilter *bf,
397 struct GNUNET_CONTAINER_BloomFilter *b = cls;
398 decrementBit (b->bitArray, bit, bf->fh);
402 * Callback: test if all bits are set
404 * @param cls pointer set to GNUNET_NO if bit is not set
405 * @param bf the filter
406 * @param bit the bit to test
409 testBitCallback (void *cls,
410 const struct GNUNET_CONTAINER_BloomFilter *bf,
414 if (GNUNET_NO == testBit (bf->bitArray, bit))
418 /* *********************** INTERFACE **************** */
421 * Load a bloom-filter from a file.
423 * @param filename the name of the file (or the prefix)
424 * @param size the size of the bloom-filter (number of
425 * bytes of storage space to use)
426 * @param k the number of GNUNET_CRYPTO_hash-functions to apply per
427 * element (number of bits set per element in the set)
428 * @return the bloomfilter
430 struct GNUNET_CONTAINER_BloomFilter *
431 GNUNET_CONTAINER_bloomfilter_load (const char *filename,
432 size_t size, unsigned int k)
434 struct GNUNET_CONTAINER_BloomFilter *bf;
440 if ((k == 0) || (size == 0))
447 size = ui; /* make sure it's a power of 2 */
449 bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
450 /* Try to open a bloomfilter file */
451 if (filename != NULL)
453 bf->fh = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READWRITE
454 | GNUNET_DISK_OPEN_CREATE,
455 GNUNET_DISK_PERM_USER_READ |
456 GNUNET_DISK_PERM_USER_WRITE);
462 bf->filename = GNUNET_strdup (filename);
470 bf->bitArray = GNUNET_malloc_large (size);
471 if (bf->bitArray == NULL)
474 GNUNET_DISK_file_close (bf->fh);
475 GNUNET_free_non_null (bf->filename);
479 bf->bitArraySize = size;
480 bf->addressesPerElement = k;
481 memset (bf->bitArray, 0, bf->bitArraySize);
483 if (bf->filename != NULL)
485 /* Read from the file what bits we can */
486 rbuff = GNUNET_malloc (BUFFSIZE);
488 while (pos < size * 8)
492 res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE);
495 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
496 "read", bf->filename);
499 break; /* is ok! we just did not use that many bits yet */
500 for (i = 0; i < res; i++)
502 if ((rbuff[i] & 0x0F) != 0)
503 setBit (bf->bitArray, pos + i * 2);
504 if ((rbuff[i] & 0xF0) != 0)
505 setBit (bf->bitArray, pos + i * 2 + 1);
509 pos += BUFFSIZE * 2; /* 2 bits per byte in the buffer */
518 * Create a bloom filter from raw bits.
520 * @param data the raw bits in memory (maybe NULL,
521 * in which case all bits should be considered
523 * @param size the size of the bloom-filter (number of
524 * bytes of storage space to use); also size of data
525 * -- unless data is NULL
526 * @param k the number of GNUNET_CRYPTO_hash-functions to apply per
527 * element (number of bits set per element in the set)
528 * @return the bloomfilter
530 struct GNUNET_CONTAINER_BloomFilter *
531 GNUNET_CONTAINER_bloomfilter_init (const char *data,
532 size_t size, unsigned int k)
534 struct GNUNET_CONTAINER_BloomFilter *bf;
537 if ((k == 0) || (size == 0))
547 bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
550 bf->bitArray = GNUNET_malloc_large (size);
551 if (bf->bitArray == NULL)
556 bf->bitArraySize = size;
557 bf->addressesPerElement = k;
559 memcpy (bf->bitArray, data, size);
561 memset (bf->bitArray, 0, bf->bitArraySize);
567 * Copy the raw data of this bloomfilter into
568 * the given data array.
570 * @param bf bloomfilter to take the raw data from
571 * @param data where to write the data
572 * @param size the size of the given data array
573 * @return GNUNET_SYSERR if the data array is not big enough
576 GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct GNUNET_CONTAINER_BloomFilter
577 *bf, char *data, size_t size)
580 return GNUNET_SYSERR;
581 if (bf->bitArraySize != size)
582 return GNUNET_SYSERR;
583 memcpy (data, bf->bitArray, size);
588 * Free the space associated with a filter
589 * in memory, flush to drive if needed (do not
590 * free the space on the drive)
592 * @param bf the filter
595 GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf)
600 GNUNET_DISK_file_close (bf->fh);
601 GNUNET_free_non_null (bf->filename);
602 GNUNET_free (bf->bitArray);
607 * Reset a bloom filter to empty. Clears the file on disk.
609 * @param bf the filter
612 GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf)
617 memset (bf->bitArray, 0, bf->bitArraySize);
618 if (bf->filename != NULL)
619 makeEmptyFile (bf->fh, bf->bitArraySize * 4);
624 * Test if an element is in the filter.
626 * @param e the element
627 * @param bf the filter
628 * @return GNUNET_YES if the element is in the filter, GNUNET_NO if not
631 GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter *bf,
632 const GNUNET_HashCode * e)
639 iterateBits (bf, &testBitCallback, &res, e);
644 * Add an element to the filter
646 * @param bf the filter
647 * @param e the element
650 GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
651 const GNUNET_HashCode * e)
656 iterateBits (bf, &incrementBitCallback, bf, e);
661 * Or the entries of the given raw data array with the
662 * data of the given bloom filter. Assumes that
663 * the size of the data array and the current filter
666 * @param bf the filter
667 * @param data the data to or-in
668 * @param size number of bytes in data
671 GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
672 const char *data, size_t size)
676 unsigned long long* fc;
677 const unsigned long long* dc;
681 if (bf->bitArraySize != size)
682 return GNUNET_SYSERR;
683 fc = (unsigned long long*) bf->bitArray;
684 dc = (const unsigned long long*) data;
685 n = size / sizeof (unsigned long long);
687 for (i = 0; i < n; i++)
689 for (i = n * sizeof(unsigned long long); i < size; i++)
690 bf->bitArray[i] |= data[i];
695 * Or the entries of the given raw data array with the
696 * data of the given bloom filter. Assumes that
697 * the size of the data array and the current filter
700 * @param bf the filter
701 * @param to_or the bloomfilter to or-in
702 * @param size number of bytes in data
705 GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
706 const struct GNUNET_CONTAINER_BloomFilter *to_or,
711 unsigned long long* fc;
712 const unsigned long long* dc;
716 if (bf->bitArraySize != size)
717 return GNUNET_SYSERR;
718 fc = (unsigned long long*) bf->bitArray;
719 dc = (const unsigned long long*) to_or->bitArray;
720 n = size / sizeof (unsigned long long);
722 for (i = 0; i < n; i++)
724 for (i = n * sizeof(unsigned long long); i < size; i++)
725 bf->bitArray[i] |= to_or->bitArray[i];
730 * Remove an element from the filter.
732 * @param bf the filter
733 * @param e the element to remove
736 GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
737 const GNUNET_HashCode * e)
741 if (bf->filename == NULL)
743 iterateBits (bf, &decrementBitCallback, bf, e);
747 * Resize a bloom filter. Note that this operation
748 * is pretty costly. Essentially, the bloom filter
749 * needs to be completely re-build.
751 * @param bf the filter
752 * @param iterator an iterator over all elements stored in the BF
753 * @param iterator_cls argument to the iterator function
754 * @param size the new size for the filter
755 * @param k the new number of GNUNET_CRYPTO_hash-function to apply per element
758 GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
759 GNUNET_HashCodeIterator iterator,
761 size_t size, unsigned int k)
766 GNUNET_free (bf->bitArray);
770 size = i; /* make sure it's a power of 2 */
772 bf->bitArraySize = size;
773 bf->bitArray = GNUNET_malloc (size);
774 memset (bf->bitArray, 0, bf->bitArraySize);
775 if (bf->filename != NULL)
776 makeEmptyFile (bf->fh, bf->bitArraySize * 4);
777 while (GNUNET_YES == iterator (iterator_cls, &hc))
778 GNUNET_CONTAINER_bloomfilter_add (bf, &hc);
781 /* end of container_bloomfilter.c */