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
105 return GNUNET_CONTAINER_bloomfilter_init (bf->bitArray, bf->bitArraySize,
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 ==
212 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET));
213 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
217 * Clears a bit from bitArray if the respective usage
218 * counter on the disk hits/is zero.
220 * @param bitArray memory area to set the bit in
221 * @param bitIdx which bit to test
222 * @param fh A file to keep the 4bit address usage counters in
225 decrementBit (char *bitArray, unsigned int bitIdx,
226 const struct GNUNET_DISK_FileHandle *fh)
232 unsigned int targetLoc;
234 if (GNUNET_DISK_handle_invalid (fh))
235 return; /* cannot decrement! */
236 /* Each char slot in the counter file holds two 4 bit counters */
237 fileSlot = bitIdx / 2;
238 targetLoc = bitIdx % 2;
239 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET);
240 if (1 != GNUNET_DISK_file_read (fh, &value, 1))
243 high = (value & 0xF0) >> 4;
245 /* decrement, but once we have reached the max, never go back! */
248 if ((low > 0) && (low < 0xF))
252 clearBit (bitArray, bitIdx);
257 if ((high > 0) && (high < 0xF))
261 clearBit (bitArray, bitIdx);
264 value = ((high << 4) | low);
265 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET);
266 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
269 #define BUFFSIZE 65536
272 * Creates a file filled with zeroes
274 * @param fh the file handle
275 * @param size the size of the file
276 * @return GNUNET_OK if created ok, GNUNET_SYSERR otherwise
279 makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, size_t size)
282 size_t bytesleft = size;
285 if (GNUNET_DISK_handle_invalid (fh))
286 return GNUNET_SYSERR;
287 buffer = GNUNET_malloc (BUFFSIZE);
288 memset (buffer, 0, BUFFSIZE);
289 GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_SET);
291 while (bytesleft > 0)
293 if (bytesleft > BUFFSIZE)
295 res = GNUNET_DISK_file_write (fh, buffer, BUFFSIZE);
296 bytesleft -= BUFFSIZE;
300 res = GNUNET_DISK_file_write (fh, buffer, bytesleft);
303 GNUNET_assert (res != GNUNET_SYSERR);
305 GNUNET_free (buffer);
309 /* ************** GNUNET_CONTAINER_BloomFilter iterator ********* */
312 * Iterator (callback) method to be called by the
313 * bloomfilter iterator on each bit that is to be
314 * set or tested for the key.
317 * @param bf the filter to manipulate
318 * @param bit the current bit
319 * @return GNUNET_YES to continue, GNUNET_NO to stop early
321 typedef int (*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;
348 while (slot < (sizeof (GNUNET_HashCode) / sizeof (uint32_t)))
352 (((uint32_t *) & tmp[round & 1])[slot]) &
353 ((bf->bitArraySize * 8) - 1)))
362 GNUNET_CRYPTO_hash (&tmp[round & 1], sizeof (GNUNET_HashCode),
363 &tmp[(round + 1) & 1]);
371 * Callback: increment bit
373 * @param cls pointer to writeable form of bf
374 * @param bf the filter to manipulate
375 * @param bit the bit to increment
379 incrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
382 struct GNUNET_CONTAINER_BloomFilter *b = cls;
384 incrementBit (b->bitArray, bit, bf->fh);
389 * Callback: decrement bit
391 * @param cls pointer to writeable form of bf
392 * @param bf the filter to manipulate
393 * @param bit the bit to decrement
397 decrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
400 struct GNUNET_CONTAINER_BloomFilter *b = cls;
402 decrementBit (b->bitArray, bit, bf->fh);
407 * Callback: test if all bits are set
409 * @param cls pointer set to GNUNET_NO if bit is not set
410 * @param bf the filter
411 * @param bit the bit to test
412 * @return YES if the bit is set, NO if not
415 testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
420 if (GNUNET_NO == testBit (bf->bitArray, bit))
428 /* *********************** INTERFACE **************** */
431 * Load a bloom-filter from a file.
433 * @param filename the name of the file (or the prefix)
434 * @param size the size of the bloom-filter (number of
435 * bytes of storage space to use)
436 * @param k the number of GNUNET_CRYPTO_hash-functions to apply per
437 * element (number of bits set per element in the set)
438 * @return the bloomfilter
440 struct GNUNET_CONTAINER_BloomFilter *
441 GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
444 struct GNUNET_CONTAINER_BloomFilter *bf;
450 GNUNET_assert (NULL != filename);
451 if ((k == 0) || (size == 0))
458 size = ui; /* make sure it's a power of 2 */
460 bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
461 /* Try to open a bloomfilter file */
463 GNUNET_DISK_file_open (filename,
464 GNUNET_DISK_OPEN_READWRITE |
465 GNUNET_DISK_OPEN_CREATE,
466 GNUNET_DISK_PERM_USER_READ |
467 GNUNET_DISK_PERM_USER_WRITE);
473 bf->filename = GNUNET_strdup (filename);
475 bf->bitArray = GNUNET_malloc_large (size);
476 if (bf->bitArray == NULL)
479 GNUNET_DISK_file_close (bf->fh);
480 GNUNET_free (bf->filename);
484 bf->bitArraySize = size;
485 bf->addressesPerElement = k;
486 memset (bf->bitArray, 0, bf->bitArraySize);
488 /* Read from the file what bits we can */
489 rbuff = GNUNET_malloc (BUFFSIZE);
491 while (pos < size * 8)
495 res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE);
498 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read",
502 break; /* is ok! we just did not use that many bits yet */
503 for (i = 0; i < res; i++)
505 if ((rbuff[i] & 0x0F) != 0)
506 setBit (bf->bitArray, pos + i * 2);
507 if ((rbuff[i] & 0xF0) != 0)
508 setBit (bf->bitArray, pos + i * 2 + 1);
512 pos += BUFFSIZE * 2; /* 2 bits per byte in the buffer */
520 * Create a bloom filter from raw bits.
522 * @param data the raw bits in memory (maybe NULL,
523 * in which case all bits should be considered
525 * @param size the size of the bloom-filter (number of
526 * bytes of storage space to use); also size of data
527 * -- unless data is NULL
528 * @param k the number of GNUNET_CRYPTO_hash-functions to apply per
529 * element (number of bits set per element in the set)
530 * @return the bloomfilter
532 struct GNUNET_CONTAINER_BloomFilter *
533 GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size,
536 struct GNUNET_CONTAINER_BloomFilter *bf;
539 if ((k == 0) || (size == 0))
549 bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
552 bf->bitArray = GNUNET_malloc_large (size);
553 if (bf->bitArray == NULL)
558 bf->bitArraySize = size;
559 bf->addressesPerElement = k;
561 memcpy (bf->bitArray, data, size);
563 memset (bf->bitArray, 0, bf->bitArraySize);
569 * Copy the raw data of this bloomfilter into
570 * the given data array.
572 * @param bf bloomfilter to take the raw data from
573 * @param data where to write the data
574 * @param size the size of the given data array
575 * @return GNUNET_SYSERR if the data array is not big enough
578 GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct
579 GNUNET_CONTAINER_BloomFilter *bf,
580 char *data, size_t size)
583 return GNUNET_SYSERR;
584 if (bf->bitArraySize != size)
585 return GNUNET_SYSERR;
586 memcpy (data, bf->bitArray, size);
591 * Free the space associated with a filter
592 * in memory, flush to drive if needed (do not
593 * free the space on the drive)
595 * @param bf the filter
598 GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf)
603 GNUNET_DISK_file_close (bf->fh);
604 GNUNET_free_non_null (bf->filename);
605 GNUNET_free (bf->bitArray);
610 * Reset a bloom filter to empty. Clears the file on disk.
612 * @param bf the filter
615 GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf)
620 memset (bf->bitArray, 0, bf->bitArraySize);
621 if (bf->filename != NULL)
622 makeEmptyFile (bf->fh, bf->bitArraySize * 4);
627 * Test if an element is in the filter.
629 * @param e the element
630 * @param bf the filter
631 * @return GNUNET_YES if the element is in the filter, GNUNET_NO if not
634 GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter
635 *bf, const GNUNET_HashCode * e)
642 iterateBits (bf, &testBitCallback, &res, e);
647 * Add an element to the filter
649 * @param bf the filter
650 * @param e the element
653 GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
654 const GNUNET_HashCode * e)
659 iterateBits (bf, &incrementBitCallback, bf, e);
664 * Or the entries of the given raw data array with the
665 * data of the given bloom filter. Assumes that
666 * the size of the data array and the current filter
669 * @param bf the filter
670 * @param data the data to or-in
671 * @param size number of bytes in data
674 GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
675 const char *data, size_t size)
679 unsigned long long *fc;
680 const unsigned long long *dc;
684 if (bf->bitArraySize != size)
685 return GNUNET_SYSERR;
686 fc = (unsigned long long *) bf->bitArray;
687 dc = (const unsigned long long *) data;
688 n = size / sizeof (unsigned long long);
690 for (i = 0; i < n; i++)
692 for (i = n * sizeof (unsigned long long); i < size; i++)
693 bf->bitArray[i] |= data[i];
698 * Or the entries of the given raw data array with the
699 * data of the given bloom filter. Assumes that
700 * the size of the data array and the current filter
703 * @param bf the filter
704 * @param to_or the bloomfilter to or-in
705 * @param size number of bytes in data
708 GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
709 const struct GNUNET_CONTAINER_BloomFilter
714 unsigned long long *fc;
715 const unsigned long long *dc;
719 if (bf->bitArraySize != size)
720 return GNUNET_SYSERR;
721 fc = (unsigned long long *) bf->bitArray;
722 dc = (const unsigned long long *) to_or->bitArray;
723 n = size / sizeof (unsigned long long);
725 for (i = 0; i < n; i++)
727 for (i = n * sizeof (unsigned long long); i < size; i++)
728 bf->bitArray[i] |= to_or->bitArray[i];
733 * Remove an element from the filter.
735 * @param bf the filter
736 * @param e the element to remove
739 GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
740 const GNUNET_HashCode * e)
744 if (bf->filename == NULL)
746 iterateBits (bf, &decrementBitCallback, bf, e);
750 * Resize a bloom filter. Note that this operation
751 * is pretty costly. Essentially, the bloom filter
752 * needs to be completely re-build.
754 * @param bf the filter
755 * @param iterator an iterator over all elements stored in the BF
756 * @param iterator_cls argument to the iterator function
757 * @param size the new size for the filter
758 * @param k the new number of GNUNET_CRYPTO_hash-function to apply per element
761 GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
762 GNUNET_HashCodeIterator iterator,
763 void *iterator_cls, size_t size,
769 GNUNET_free (bf->bitArray);
773 size = i; /* make sure it's a power of 2 */
775 bf->bitArraySize = size;
776 bf->bitArray = GNUNET_malloc (size);
777 memset (bf->bitArray, 0, bf->bitArraySize);
778 if (bf->filename != NULL)
779 makeEmptyFile (bf->fh, bf->bitArraySize * 4);
780 while (GNUNET_YES == iterator (iterator_cls, &hc))
781 GNUNET_CONTAINER_bloomfilter_add (bf, &hc);
784 /* end of container_bloomfilter.c */