40cdfd06ecff70a817b787c5d236fb103677dc3a
[oweals/gnunet.git] / src / util / container_bloomfilter.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2006, 2008 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file util/container_bloomfilter.c
22  * @brief data structure used to reduce disk accesses.
23  *
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'.
28  *
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".
33  *
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
36  * bit in memory).
37  *
38  * @author Igor Wronsky
39  * @author Christian Grothoff
40  */
41
42 #include "platform.h"
43 #include "gnunet_common.h"
44 #include "gnunet_container_lib.h"
45 #include "gnunet_disk_lib.h"
46
47 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
48
49 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
50
51 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
52
53 struct GNUNET_CONTAINER_BloomFilter
54 {
55
56   /**
57    * The actual bloomfilter bit array
58    */
59   char *bitArray;
60
61   /**
62    * Filename of the filter
63    */
64   char *filename;
65
66   /**
67    * The bit counter file on disk
68    */
69   struct GNUNET_DISK_FileHandle *fh;
70
71   /**
72    * How many bits we set for each stored element
73    */
74   unsigned int addressesPerElement;
75
76   /**
77    * Size of bitArray in bytes
78    */
79   size_t bitArraySize;
80
81 };
82
83
84
85 /**
86  * Get size of the bloom filter.
87  *
88  * @param bf the filter
89  * @return number of bytes used for the data of the bloom filter
90  */
91 size_t
92 GNUNET_CONTAINER_bloomfilter_get_size (const struct GNUNET_CONTAINER_BloomFilter
93                                        *bf)
94 {
95   if (bf == NULL)
96     return 0;
97   return bf->bitArraySize;
98 }
99
100
101 /**
102  * Copy an existing memory.  Any association with a file
103  * on-disk will be lost in the process.
104  * @param bf the filter to copy
105  * @return copy of the bf
106  */
107 struct GNUNET_CONTAINER_BloomFilter *
108 GNUNET_CONTAINER_bloomfilter_copy (const struct GNUNET_CONTAINER_BloomFilter
109                                    *bf)
110 {
111   return GNUNET_CONTAINER_bloomfilter_init (bf->bitArray, bf->bitArraySize,
112                                             bf->addressesPerElement);
113 }
114
115
116 /**
117  * Sets a bit active in the bitArray. Increment bit-specific
118  * usage counter on disk only if below 4bit max (==15).
119  *
120  * @param bitArray memory area to set the bit in
121  * @param bitIdx which bit to set
122  */
123 static void
124 setBit (char *bitArray, unsigned int bitIdx)
125 {
126   size_t arraySlot;
127   unsigned int targetBit;
128
129   arraySlot = bitIdx / 8;
130   targetBit = (1L << (bitIdx % 8));
131   bitArray[arraySlot] |= targetBit;
132 }
133
134 /**
135  * Clears a bit from bitArray. Bit is cleared from the array
136  * only if the respective usage counter on the disk hits/is zero.
137  *
138  * @param bitArray memory area to set the bit in
139  * @param bitIdx which bit to unset
140  */
141 static void
142 clearBit (char *bitArray, unsigned int bitIdx)
143 {
144   size_t slot;
145   unsigned int targetBit;
146
147   slot = bitIdx / 8;
148   targetBit = (1L << (bitIdx % 8));
149   bitArray[slot] = bitArray[slot] & (~targetBit);
150 }
151
152 /**
153  * Checks if a bit is active in the bitArray
154  *
155  * @param bitArray memory area to set the bit in
156  * @param bitIdx which bit to test
157  * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
158  */
159 static int
160 testBit (char *bitArray, unsigned int bitIdx)
161 {
162   size_t slot;
163   unsigned int targetBit;
164
165   slot = bitIdx / 8;
166   targetBit = (1L << (bitIdx % 8));
167   if (bitArray[slot] & targetBit)
168     return GNUNET_YES;
169   else
170     return GNUNET_NO;
171 }
172
173 /**
174  * Sets a bit active in the bitArray and increments
175  * bit-specific usage counter on disk (but only if
176  * the counter was below 4 bit max (==15)).
177  *
178  * @param bitArray memory area to set the bit in
179  * @param bitIdx which bit to test
180  * @param fh A file to keep the 4 bit address usage counters in
181  */
182 static void
183 incrementBit (char *bitArray, unsigned int bitIdx,
184               const struct GNUNET_DISK_FileHandle *fh)
185 {
186   off_t fileSlot;
187   unsigned char value;
188   unsigned int high;
189   unsigned int low;
190   unsigned int targetLoc;
191
192   setBit (bitArray, bitIdx);
193   if (GNUNET_DISK_handle_invalid (fh))
194     return;
195   /* Update the counter file on disk */
196   fileSlot = bitIdx / 2;
197   targetLoc = bitIdx % 2;
198
199   GNUNET_assert (fileSlot ==
200                  GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET));
201   if (1 != GNUNET_DISK_file_read (fh, &value, 1))
202     value = 0;
203   low = value & 0xF;
204   high = (value & (~0xF)) >> 4;
205
206   if (targetLoc == 0)
207   {
208     if (low < 0xF)
209       low++;
210   }
211   else
212   {
213     if (high < 0xF)
214       high++;
215   }
216   value = ((high << 4) | low);
217   GNUNET_assert (fileSlot ==
218                  GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET));
219   GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
220 }
221
222 /**
223  * Clears a bit from bitArray if the respective usage
224  * counter on the disk hits/is zero.
225  *
226  * @param bitArray memory area to set the bit in
227  * @param bitIdx which bit to test
228  * @param fh A file to keep the 4bit address usage counters in
229  */
230 static void
231 decrementBit (char *bitArray, unsigned int bitIdx,
232               const struct GNUNET_DISK_FileHandle *fh)
233 {
234   off_t fileSlot;
235   unsigned char value;
236   unsigned int high;
237   unsigned int low;
238   unsigned int targetLoc;
239
240   if (GNUNET_DISK_handle_invalid (fh))
241     return;                     /* cannot decrement! */
242   /* Each char slot in the counter file holds two 4 bit counters */
243   fileSlot = bitIdx / 2;
244   targetLoc = bitIdx % 2;
245   GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET);
246   if (1 != GNUNET_DISK_file_read (fh, &value, 1))
247     value = 0;
248   low = value & 0xF;
249   high = (value & 0xF0) >> 4;
250
251   /* decrement, but once we have reached the max, never go back! */
252   if (targetLoc == 0)
253   {
254     if ((low > 0) && (low < 0xF))
255       low--;
256     if (low == 0)
257     {
258       clearBit (bitArray, bitIdx);
259     }
260   }
261   else
262   {
263     if ((high > 0) && (high < 0xF))
264       high--;
265     if (high == 0)
266     {
267       clearBit (bitArray, bitIdx);
268     }
269   }
270   value = ((high << 4) | low);
271   GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET);
272   GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
273 }
274
275 #define BUFFSIZE 65536
276
277 /**
278  * Creates a file filled with zeroes
279  *
280  * @param fh the file handle
281  * @param size the size of the file
282  * @return GNUNET_OK if created ok, GNUNET_SYSERR otherwise
283  */
284 static int
285 make_empty_file (const struct GNUNET_DISK_FileHandle *fh, size_t size)
286 {
287   char buffer[BUFFSIZE];
288   size_t bytesleft = size;
289   int res = 0;
290
291   if (GNUNET_DISK_handle_invalid (fh))
292     return GNUNET_SYSERR;
293   memset (buffer, 0, sizeof (buffer));
294   GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_SET);
295   while (bytesleft > 0)
296   {
297     if (bytesleft > sizeof (buffer))
298     {
299       res = GNUNET_DISK_file_write (fh, buffer, sizeof (buffer));
300       if (res >= 0)
301         bytesleft -= res;
302     }
303     else
304     {
305       res = GNUNET_DISK_file_write (fh, buffer, bytesleft);
306       if (res >= 0)
307         bytesleft -= res;
308     }
309     if (GNUNET_SYSERR == res)
310       return GNUNET_SYSERR;
311   }
312   return GNUNET_OK;
313 }
314
315 /* ************** GNUNET_CONTAINER_BloomFilter iterator ********* */
316
317 /**
318  * Iterator (callback) method to be called by the
319  * bloomfilter iterator on each bit that is to be
320  * set or tested for the key.
321  *
322  * @param cls closure
323  * @param bf the filter to manipulate
324  * @param bit the current bit
325  * @return GNUNET_YES to continue, GNUNET_NO to stop early
326  */
327 typedef int (*BitIterator) (void *cls,
328                             const struct GNUNET_CONTAINER_BloomFilter * bf,
329                             unsigned int bit);
330
331
332 /**
333  * Call an iterator for each bit that the bloomfilter
334  * must test or set for this element.
335  *
336  * @param bf the filter
337  * @param callback the method to call
338  * @param arg extra argument to callback
339  * @param key the key for which we iterate over the BF bits
340  */
341 static void
342 iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
343              BitIterator callback, void *arg, const GNUNET_HashCode * key)
344 {
345   GNUNET_HashCode tmp[2];
346   int bitCount;
347   unsigned int round;
348   unsigned int slot = 0;
349
350   bitCount = bf->addressesPerElement;
351   tmp[0] = *key;
352   round = 0;
353   while (bitCount > 0)
354   {
355     while (slot < (sizeof (GNUNET_HashCode) / sizeof (uint32_t)))
356     {
357       if (GNUNET_YES !=
358           callback (arg, bf,
359                     (((uint32_t *) & tmp[round & 1])[slot]) &
360                     ((bf->bitArraySize * 8) - 1)))
361         return;
362       slot++;
363       bitCount--;
364       if (bitCount == 0)
365         break;
366     }
367     if (bitCount > 0)
368     {
369       GNUNET_CRYPTO_hash (&tmp[round & 1], sizeof (GNUNET_HashCode),
370                           &tmp[(round + 1) & 1]);
371       round++;
372       slot = 0;
373     }
374   }
375 }
376
377
378 /**
379  * Callback: increment bit
380  *
381  * @param cls pointer to writeable form of bf
382  * @param bf the filter to manipulate
383  * @param bit the bit to increment
384  * @return GNUNET_YES
385  */
386 static int
387 incrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
388                       unsigned int bit)
389 {
390   struct GNUNET_CONTAINER_BloomFilter *b = cls;
391
392   incrementBit (b->bitArray, bit, bf->fh);
393   return GNUNET_YES;
394 }
395
396
397 /**
398  * Callback: decrement bit
399  *
400  * @param cls pointer to writeable form of bf
401  * @param bf the filter to manipulate
402  * @param bit the bit to decrement
403  * @return GNUNET_YES
404  */
405 static int
406 decrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
407                       unsigned int bit)
408 {
409   struct GNUNET_CONTAINER_BloomFilter *b = cls;
410
411   decrementBit (b->bitArray, bit, bf->fh);
412   return GNUNET_YES;
413 }
414
415
416 /**
417  * Callback: test if all bits are set
418  *
419  * @param cls pointer set to GNUNET_NO if bit is not set
420  * @param bf the filter
421  * @param bit the bit to test
422  * @return YES if the bit is set, NO if not
423  */
424 static int
425 testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
426                  unsigned int bit)
427 {
428   int *arg = cls;
429
430   if (GNUNET_NO == testBit (bf->bitArray, bit))
431   {
432     *arg = GNUNET_NO;
433     return GNUNET_NO;
434   }
435   return GNUNET_YES;
436 }
437
438 /* *********************** INTERFACE **************** */
439
440 /**
441  * Load a bloom-filter from a file.
442  *
443  * @param filename the name of the file (or the prefix)
444  * @param size the size of the bloom-filter (number of
445  *        bytes of storage space to use)
446  * @param k the number of GNUNET_CRYPTO_hash-functions to apply per
447  *        element (number of bits set per element in the set)
448  * @return the bloomfilter
449  */
450 struct GNUNET_CONTAINER_BloomFilter *
451 GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
452                                    unsigned int k)
453 {
454   struct GNUNET_CONTAINER_BloomFilter *bf;
455   char *rbuff;
456   off_t pos;
457   int i;
458   size_t ui;
459   off_t fsize;
460   int must_read;
461
462   GNUNET_assert (NULL != filename);
463   if ((k == 0) || (size == 0))
464     return NULL;
465   if (size < BUFFSIZE)
466     size = BUFFSIZE;
467   ui = 1;
468   while ( (ui < size) &&
469           (ui * 2 > ui) )
470     ui *= 2;
471   size = ui;                    /* make sure it's a power of 2 */
472
473   bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
474   /* Try to open a bloomfilter file */
475   if (GNUNET_YES == GNUNET_DISK_file_test (filename))
476     bf->fh =
477       GNUNET_DISK_file_open (filename,
478                              GNUNET_DISK_OPEN_READWRITE,
479                              GNUNET_DISK_PERM_USER_READ |
480                              GNUNET_DISK_PERM_USER_WRITE);
481   if (NULL == bf->fh)
482   {
483     /* file did not exist, don't read */
484     must_read = GNUNET_NO;
485     bf->fh =
486       GNUNET_DISK_file_open (filename,
487                              GNUNET_DISK_OPEN_CREATE |
488                              GNUNET_DISK_OPEN_READWRITE,
489                              GNUNET_DISK_PERM_USER_READ |
490                              GNUNET_DISK_PERM_USER_WRITE);
491     if (NULL == bf->fh)
492       {
493         GNUNET_free (bf);
494         return NULL;
495       }
496     if (GNUNET_OK != make_empty_file (bf->fh, size * 4LL))
497     {
498       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
499                            "write");
500       GNUNET_DISK_file_close (bf->fh);
501       GNUNET_free (bf);
502       return NULL;
503     }
504   }
505   else
506   {
507     /* file existed, try to read it! */
508     must_read = GNUNET_YES;
509     if (GNUNET_OK !=
510         GNUNET_DISK_file_handle_size (bf->fh, &fsize))
511     {
512       GNUNET_DISK_file_close (bf->fh);
513       GNUNET_free (bf);
514       return NULL;
515     }
516     if (fsize != size * 4LL)
517     {
518       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
519                   _("Size of file on disk is incorrect for this Bloom filter (want %llu, have %llu)\n"),
520                   (unsigned long long) fsize,
521                   (unsigned long long) (size * 4LL));
522       GNUNET_DISK_file_close (bf->fh);
523       GNUNET_free (bf);
524       return NULL;
525     }
526   }
527   bf->filename = GNUNET_strdup (filename);
528   /* Alloc block */
529   bf->bitArray = GNUNET_malloc_large (size);
530   if (bf->bitArray == NULL)
531   {
532     if (bf->fh != NULL)
533       GNUNET_DISK_file_close (bf->fh);
534     GNUNET_free (bf->filename);
535     GNUNET_free (bf);
536     return NULL;
537   }
538   bf->bitArraySize = size;
539   bf->addressesPerElement = k;
540   memset (bf->bitArray, 0, bf->bitArraySize);
541
542   if (GNUNET_YES != must_read)      
543     return bf; /* already done! */  
544   /* Read from the file what bits we can */
545   rbuff = GNUNET_malloc (BUFFSIZE);
546   pos = 0;
547   while (pos < size * 8LL)
548   {
549     int res;
550
551     res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE);
552     if (res == -1)
553     {
554       LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", bf->filename);
555       GNUNET_free (rbuff);
556       GNUNET_free (bf->filename);
557       GNUNET_DISK_file_close (bf->fh);
558       GNUNET_free (bf);
559       return NULL;
560     }
561     if (res == 0)
562       break;                    /* is ok! we just did not use that many bits yet */
563     for (i = 0; i < res; i++)
564     {
565       if ((rbuff[i] & 0x0F) != 0)
566         setBit (bf->bitArray, pos + i * 2);
567       if ((rbuff[i] & 0xF0) != 0)
568         setBit (bf->bitArray, pos + i * 2 + 1);
569     }
570     if (res < BUFFSIZE)
571       break;
572     pos += BUFFSIZE * 2;        /* 2 bits per byte in the buffer */
573   }
574   GNUNET_free (rbuff);
575   return bf;
576 }
577
578
579 /**
580  * Create a bloom filter from raw bits.
581  *
582  * @param data the raw bits in memory (maybe NULL,
583  *        in which case all bits should be considered
584  *        to be zero).
585  * @param size the size of the bloom-filter (number of
586  *        bytes of storage space to use); also size of data
587  *        -- unless data is NULL
588  * @param k the number of GNUNET_CRYPTO_hash-functions to apply per
589  *        element (number of bits set per element in the set)
590  * @return the bloomfilter
591  */
592 struct GNUNET_CONTAINER_BloomFilter *
593 GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size,
594                                    unsigned int k)
595 {
596   struct GNUNET_CONTAINER_BloomFilter *bf;
597   size_t ui;
598
599   if ((k == 0) || (size == 0))
600     return NULL;
601   ui = 1;
602   while (ui < size)
603     ui *= 2;
604   if (size != ui)
605   {
606     GNUNET_break (0);
607     return NULL;
608   }
609   bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
610   bf->filename = NULL;
611   bf->fh = NULL;
612   bf->bitArray = GNUNET_malloc_large (size);
613   if (bf->bitArray == NULL)
614   {
615     GNUNET_free (bf);
616     return NULL;
617   }
618   bf->bitArraySize = size;
619   bf->addressesPerElement = k;
620   if (data != NULL)
621     memcpy (bf->bitArray, data, size);
622   else
623     memset (bf->bitArray, 0, bf->bitArraySize);
624   return bf;
625 }
626
627
628 /**
629  * Copy the raw data of this bloomfilter into
630  * the given data array.
631  *
632  * @param bf bloomfilter to take the raw data from
633  * @param data where to write the data
634  * @param size the size of the given data array
635  * @return GNUNET_SYSERR if the data array is not big enough
636  */
637 int
638 GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct
639                                            GNUNET_CONTAINER_BloomFilter *bf,
640                                            char *data, size_t size)
641 {
642   if (NULL == bf)
643     return GNUNET_SYSERR;
644   if (bf->bitArraySize != size)
645     return GNUNET_SYSERR;
646   memcpy (data, bf->bitArray, size);
647   return GNUNET_OK;
648 }
649
650
651 /**
652  * Free the space associated with a filter
653  * in memory, flush to drive if needed (do not
654  * free the space on the drive)
655  *
656  * @param bf the filter
657  */
658 void
659 GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf)
660 {
661   if (NULL == bf)
662     return;
663   if (bf->fh != NULL)
664     GNUNET_DISK_file_close (bf->fh);
665   GNUNET_free_non_null (bf->filename);
666   GNUNET_free (bf->bitArray);
667   GNUNET_free (bf);
668 }
669
670
671 /**
672  * Reset a bloom filter to empty. Clears the file on disk.
673  *
674  * @param bf the filter
675  */
676 void
677 GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf)
678 {
679   if (NULL == bf)
680     return;
681
682   memset (bf->bitArray, 0, bf->bitArraySize);
683   if (bf->filename != NULL)
684     make_empty_file (bf->fh, bf->bitArraySize * 4LL);
685 }
686
687
688 /**
689  * Test if an element is in the filter.
690  *
691  * @param e the element
692  * @param bf the filter
693  * @return GNUNET_YES if the element is in the filter, GNUNET_NO if not
694  */
695 int
696 GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter
697                                    *bf, const GNUNET_HashCode * e)
698 {
699   int res;
700
701   if (NULL == bf)
702     return GNUNET_YES;
703   res = GNUNET_YES;
704   iterateBits (bf, &testBitCallback, &res, e);
705   return res;
706 }
707
708
709 /**
710  * Add an element to the filter
711  *
712  * @param bf the filter
713  * @param e the element
714  */
715 void
716 GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
717                                   const GNUNET_HashCode * e)
718 {
719   if (NULL == bf)
720     return;
721   iterateBits (bf, &incrementBitCallback, bf, e);
722 }
723
724
725 /**
726  * Or the entries of the given raw data array with the
727  * data of the given bloom filter.  Assumes that
728  * the size of the data array and the current filter
729  * match.
730  *
731  * @param bf the filter
732  * @param data the data to or-in
733  * @param size number of bytes in data
734  */
735 int
736 GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
737                                  const char *data, size_t size)
738 {
739   unsigned int i;
740   unsigned int n;
741   unsigned long long *fc;
742   const unsigned long long *dc;
743
744   if (NULL == bf)
745     return GNUNET_YES;
746   if (bf->bitArraySize != size)
747     return GNUNET_SYSERR;
748   fc = (unsigned long long *) bf->bitArray;
749   dc = (const unsigned long long *) data;
750   n = size / sizeof (unsigned long long);
751
752   for (i = 0; i < n; i++)
753     fc[i] |= dc[i];
754   for (i = n * sizeof (unsigned long long); i < size; i++)
755     bf->bitArray[i] |= data[i];
756   return GNUNET_OK;
757 }
758
759 /**
760  * Or the entries of the given raw data array with the
761  * data of the given bloom filter.  Assumes that
762  * the size of the data array and the current filter
763  * match.
764  *
765  * @param bf the filter
766  * @param to_or the bloomfilter to or-in
767  * @param size number of bytes in data
768  */
769 int
770 GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
771                                   const struct GNUNET_CONTAINER_BloomFilter
772                                   *to_or, size_t size)
773 {
774   unsigned int i;
775   unsigned int n;
776   unsigned long long *fc;
777   const unsigned long long *dc;
778
779   if (NULL == bf)
780     return GNUNET_YES;
781   if (bf->bitArraySize != size)
782     return GNUNET_SYSERR;
783   fc = (unsigned long long *) bf->bitArray;
784   dc = (const unsigned long long *) to_or->bitArray;
785   n = size / sizeof (unsigned long long);
786
787   for (i = 0; i < n; i++)
788     fc[i] |= dc[i];
789   for (i = n * sizeof (unsigned long long); i < size; i++)
790     bf->bitArray[i] |= to_or->bitArray[i];
791   return GNUNET_OK;
792 }
793
794 /**
795  * Remove an element from the filter.
796  *
797  * @param bf the filter
798  * @param e the element to remove
799  */
800 void
801 GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
802                                      const GNUNET_HashCode * e)
803 {
804   if (NULL == bf)
805     return;
806   if (bf->filename == NULL)
807     return;
808   iterateBits (bf, &decrementBitCallback, bf, e);
809 }
810
811 /**
812  * Resize a bloom filter.  Note that this operation
813  * is pretty costly.  Essentially, the bloom filter
814  * needs to be completely re-build.
815  *
816  * @param bf the filter
817  * @param iterator an iterator over all elements stored in the BF
818  * @param iterator_cls argument to the iterator function
819  * @param size the new size for the filter
820  * @param k the new number of GNUNET_CRYPTO_hash-function to apply per element
821  */
822 void
823 GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
824                                      GNUNET_HashCodeIterator iterator,
825                                      void *iterator_cls, size_t size,
826                                      unsigned int k)
827 {
828   GNUNET_HashCode hc;
829   unsigned int i;
830
831   GNUNET_free (bf->bitArray);
832   i = 1;
833   while (i < size)
834     i *= 2;
835   size = i;                     /* make sure it's a power of 2 */
836
837   bf->bitArraySize = size;
838   bf->bitArray = GNUNET_malloc (size);
839   memset (bf->bitArray, 0, bf->bitArraySize);
840   if (bf->filename != NULL)
841     make_empty_file (bf->fh, bf->bitArraySize * 4LL);
842   while (GNUNET_YES == iterator (iterator_cls, &hc))
843     GNUNET_CONTAINER_bloomfilter_add (bf, &hc);
844 }
845
846 /* end of container_bloomfilter.c */