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