aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2022-06-25 18:01:15 +0200
committerChristian Grothoff <christian@grothoff.org>2022-06-25 18:01:23 +0200
commit4644e3ae1def4f43a24bc76f5dacc3dd94807126 (patch)
treef9757382a18521cd2f1f05fa78eb7b18d8677d28
parentfa7f0a3a47b4f35270b83e0835b0632e8ee5f00a (diff)
downloadgnunet-4644e3ae1def4f43a24bc76f5dacc3dd94807126.tar.gz
gnunet-4644e3ae1def4f43a24bc76f5dacc3dd94807126.zip
-add interop test for bernd
-rw-r--r--src/include/gnunet_container_lib.h4
-rw-r--r--src/util/container_bloomfilter.c203
-rw-r--r--src/util/test_container_bloomfilter.c50
3 files changed, 112 insertions, 145 deletions
diff --git a/src/include/gnunet_container_lib.h b/src/include/gnunet_container_lib.h
index 3eb80595c..ae6c34f2b 100644
--- a/src/include/gnunet_container_lib.h
+++ b/src/include/gnunet_container_lib.h
@@ -277,9 +277,9 @@ GNUNET_CONTAINER_bloomfilter_get_raw_data (
277 * 277 *
278 * @param e the element 278 * @param e the element
279 * @param bf the filter 279 * @param bf the filter
280 * @return #GNUNET_YES if the element is in the filter, #GNUNET_NO if not 280 * @return true if the element is in the filter, false if not
281 */ 281 */
282int 282bool
283GNUNET_CONTAINER_bloomfilter_test ( 283GNUNET_CONTAINER_bloomfilter_test (
284 const struct GNUNET_CONTAINER_BloomFilter *bf, 284 const struct GNUNET_CONTAINER_BloomFilter *bf,
285 const struct GNUNET_HashCode *e); 285 const struct GNUNET_HashCode *e);
diff --git a/src/util/container_bloomfilter.c b/src/util/container_bloomfilter.c
index 8a0487e04..9f6c3c0cc 100644
--- a/src/util/container_bloomfilter.c
+++ b/src/util/container_bloomfilter.c
@@ -83,12 +83,6 @@ struct GNUNET_CONTAINER_BloomFilter
83}; 83};
84 84
85 85
86/**
87 * Get the number of the addresses set per element in the bloom filter.
88 *
89 * @param bf the filter
90 * @return addresses set per element in the bf
91 */
92size_t 86size_t
93GNUNET_CONTAINER_bloomfilter_get_element_addresses ( 87GNUNET_CONTAINER_bloomfilter_get_element_addresses (
94 const struct GNUNET_CONTAINER_BloomFilter *bf) 88 const struct GNUNET_CONTAINER_BloomFilter *bf)
@@ -99,12 +93,6 @@ GNUNET_CONTAINER_bloomfilter_get_element_addresses (
99} 93}
100 94
101 95
102/**
103 * Get size of the bloom filter.
104 *
105 * @param bf the filter
106 * @return number of bytes used for the data of the bloom filter
107 */
108size_t 96size_t
109GNUNET_CONTAINER_bloomfilter_get_size ( 97GNUNET_CONTAINER_bloomfilter_get_size (
110 const struct GNUNET_CONTAINER_BloomFilter *bf) 98 const struct GNUNET_CONTAINER_BloomFilter *bf)
@@ -115,12 +103,6 @@ GNUNET_CONTAINER_bloomfilter_get_size (
115} 103}
116 104
117 105
118/**
119 * Copy an existing memory. Any association with a file
120 * on-disk will be lost in the process.
121 * @param bf the filter to copy
122 * @return copy of the bf
123 */
124struct GNUNET_CONTAINER_BloomFilter * 106struct GNUNET_CONTAINER_BloomFilter *
125GNUNET_CONTAINER_bloomfilter_copy ( 107GNUNET_CONTAINER_bloomfilter_copy (
126 const struct GNUNET_CONTAINER_BloomFilter *bf) 108 const struct GNUNET_CONTAINER_BloomFilter *bf)
@@ -139,7 +121,8 @@ GNUNET_CONTAINER_bloomfilter_copy (
139 * @param bitIdx which bit to set 121 * @param bitIdx which bit to set
140 */ 122 */
141static void 123static void
142setBit (char *bitArray, unsigned int bitIdx) 124setBit (char *bitArray,
125 unsigned int bitIdx)
143{ 126{
144 size_t arraySlot; 127 size_t arraySlot;
145 unsigned int targetBit; 128 unsigned int targetBit;
@@ -174,10 +157,11 @@ clearBit (char *bitArray, unsigned int bitIdx)
174 * 157 *
175 * @param bitArray memory area to set the bit in 158 * @param bitArray memory area to set the bit in
176 * @param bitIdx which bit to test 159 * @param bitIdx which bit to test
177 * @return GNUNET_YES if the bit is set, GNUNET_NO if not. 160 * @return true if the bit is set, false if not.
178 */ 161 */
179static int 162static bool
180testBit (char *bitArray, unsigned int bitIdx) 163testBit (char *bitArray,
164 unsigned int bitIdx)
181{ 165{
182 size_t slot; 166 size_t slot;
183 unsigned int targetBit; 167 unsigned int targetBit;
@@ -185,9 +169,8 @@ testBit (char *bitArray, unsigned int bitIdx)
185 slot = bitIdx / 8; 169 slot = bitIdx / 8;
186 targetBit = (1L << (bitIdx % 8)); 170 targetBit = (1L << (bitIdx % 8));
187 if (bitArray[slot] & targetBit) 171 if (bitArray[slot] & targetBit)
188 return GNUNET_YES; 172 return true;
189 else 173 return false;
190 return GNUNET_NO;
191} 174}
192 175
193 176
@@ -211,7 +194,8 @@ incrementBit (char *bitArray,
211 unsigned int low; 194 unsigned int low;
212 unsigned int targetLoc; 195 unsigned int targetLoc;
213 196
214 setBit (bitArray, bitIdx); 197 setBit (bitArray,
198 bitIdx);
215 if (GNUNET_DISK_handle_invalid (fh)) 199 if (GNUNET_DISK_handle_invalid (fh))
216 return; 200 return;
217 /* Update the counter file on disk */ 201 /* Update the counter file on disk */
@@ -314,10 +298,11 @@ decrementBit (char *bitArray,
314 * 298 *
315 * @param fh the file handle 299 * @param fh the file handle
316 * @param size the size of the file 300 * @param size the size of the file
317 * @return GNUNET_OK if created ok, GNUNET_SYSERR otherwise 301 * @return #GNUNET_OK if created ok, #GNUNET_SYSERR otherwise
318 */ 302 */
319static int 303static enum GNUNET_GenericReturnValue
320make_empty_file (const struct GNUNET_DISK_FileHandle *fh, size_t size) 304make_empty_file (const struct GNUNET_DISK_FileHandle *fh,
305 size_t size)
321{ 306{
322 char buffer[BUFFSIZE]; 307 char buffer[BUFFSIZE];
323 size_t bytesleft = size; 308 size_t bytesleft = size;
@@ -360,10 +345,10 @@ make_empty_file (const struct GNUNET_DISK_FileHandle *fh, size_t size)
360 * @param bit the current bit 345 * @param bit the current bit
361 * @return #GNUNET_YES to continue, #GNUNET_NO to stop early 346 * @return #GNUNET_YES to continue, #GNUNET_NO to stop early
362 */ 347 */
363typedef int 348typedef enum GNUNET_GenericReturnValue
364(*BitIterator) (void *cls, 349(*BitIterator)(void *cls,
365 const struct GNUNET_CONTAINER_BloomFilter *bf, 350 const struct GNUNET_CONTAINER_BloomFilter *bf,
366 unsigned int bit); 351 unsigned int bit);
367 352
368 353
369/** 354/**
@@ -418,16 +403,18 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
418 * @param cls pointer to writeable form of bf 403 * @param cls pointer to writeable form of bf
419 * @param bf the filter to manipulate 404 * @param bf the filter to manipulate
420 * @param bit the bit to increment 405 * @param bit the bit to increment
421 * @return GNUNET_YES 406 * @return #GNUNET_YES
422 */ 407 */
423static int 408static enum GNUNET_GenericReturnValue
424incrementBitCallback (void *cls, 409incrementBitCallback (void *cls,
425 const struct GNUNET_CONTAINER_BloomFilter *bf, 410 const struct GNUNET_CONTAINER_BloomFilter *bf,
426 unsigned int bit) 411 unsigned int bit)
427{ 412{
428 struct GNUNET_CONTAINER_BloomFilter *b = cls; 413 struct GNUNET_CONTAINER_BloomFilter *b = cls;
429 414
430 incrementBit (b->bitArray, bit, bf->fh); 415 incrementBit (b->bitArray,
416 bit,
417 bf->fh);
431 return GNUNET_YES; 418 return GNUNET_YES;
432} 419}
433 420
@@ -438,16 +425,18 @@ incrementBitCallback (void *cls,
438 * @param cls pointer to writeable form of bf 425 * @param cls pointer to writeable form of bf
439 * @param bf the filter to manipulate 426 * @param bf the filter to manipulate
440 * @param bit the bit to decrement 427 * @param bit the bit to decrement
441 * @return GNUNET_YES 428 * @return #GNUNET_YES
442 */ 429 */
443static int 430static enum GNUNET_GenericReturnValue
444decrementBitCallback (void *cls, 431decrementBitCallback (void *cls,
445 const struct GNUNET_CONTAINER_BloomFilter *bf, 432 const struct GNUNET_CONTAINER_BloomFilter *bf,
446 unsigned int bit) 433 unsigned int bit)
447{ 434{
448 struct GNUNET_CONTAINER_BloomFilter *b = cls; 435 struct GNUNET_CONTAINER_BloomFilter *b = cls;
449 436
450 decrementBit (b->bitArray, bit, bf->fh); 437 decrementBit (b->bitArray,
438 bit,
439 bf->fh);
451 return GNUNET_YES; 440 return GNUNET_YES;
452} 441}
453 442
@@ -455,21 +444,21 @@ decrementBitCallback (void *cls,
455/** 444/**
456 * Callback: test if all bits are set 445 * Callback: test if all bits are set
457 * 446 *
458 * @param cls pointer set to GNUNET_NO if bit is not set 447 * @param cls pointer set to false if bit is not set
459 * @param bf the filter 448 * @param bf the filter
460 * @param bit the bit to test 449 * @param bit the bit to test
461 * @return YES if the bit is set, NO if not 450 * @return #GNUNET_YES if the bit is set, #GNUNET_NO if not
462 */ 451 */
463static int 452static enum GNUNET_GenericReturnValue
464testBitCallback (void *cls, 453testBitCallback (void *cls,
465 const struct GNUNET_CONTAINER_BloomFilter *bf, 454 const struct GNUNET_CONTAINER_BloomFilter *bf,
466 unsigned int bit) 455 unsigned int bit)
467{ 456{
468 int *arg = cls; 457 bool *arg = cls;
469 458
470 if (GNUNET_NO == testBit (bf->bitArray, bit)) 459 if (! testBit (bf->bitArray, bit))
471 { 460 {
472 *arg = GNUNET_NO; 461 *arg = false;
473 return GNUNET_NO; 462 return GNUNET_NO;
474 } 463 }
475 return GNUNET_YES; 464 return GNUNET_YES;
@@ -523,7 +512,9 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename,
523 { 512 {
524 /* file existed, try to read it! */ 513 /* file existed, try to read it! */
525 must_read = GNUNET_YES; 514 must_read = GNUNET_YES;
526 if (GNUNET_OK != GNUNET_DISK_file_handle_size (bf->fh, &fsize)) 515 if (GNUNET_OK !=
516 GNUNET_DISK_file_handle_size (bf->fh,
517 &fsize))
527 { 518 {
528 GNUNET_DISK_file_close (bf->fh); 519 GNUNET_DISK_file_close (bf->fh);
529 GNUNET_free (bf); 520 GNUNET_free (bf);
@@ -532,9 +523,12 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename,
532 if (0 == fsize) 523 if (0 == fsize)
533 { 524 {
534 /* found existing empty file, just overwrite */ 525 /* found existing empty file, just overwrite */
535 if (GNUNET_OK != make_empty_file (bf->fh, size * 4LL)) 526 if (GNUNET_OK !=
527 make_empty_file (bf->fh,
528 size * 4LL))
536 { 529 {
537 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write"); 530 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
531 "write");
538 GNUNET_DISK_file_close (bf->fh); 532 GNUNET_DISK_file_close (bf->fh);
539 GNUNET_free (bf); 533 GNUNET_free (bf);
540 return NULL; 534 return NULL;
@@ -625,19 +619,6 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename,
625} 619}
626 620
627 621
628/**
629 * Create a bloom filter from raw bits.
630 *
631 * @param data the raw bits in memory (maybe NULL,
632 * in which case all bits should be considered
633 * to be zero).
634 * @param size the size of the bloom-filter (number of
635 * bytes of storage space to use); also size of data
636 * -- unless data is NULL
637 * @param k the number of GNUNET_CRYPTO_hash-functions to apply per
638 * element (number of bits set per element in the set)
639 * @return the bloomfilter
640 */
641struct GNUNET_CONTAINER_BloomFilter * 622struct GNUNET_CONTAINER_BloomFilter *
642GNUNET_CONTAINER_bloomfilter_init (const char *data, 623GNUNET_CONTAINER_bloomfilter_init (const char *data,
643 size_t size, 624 size_t size,
@@ -664,16 +645,7 @@ GNUNET_CONTAINER_bloomfilter_init (const char *data,
664} 645}
665 646
666 647
667/** 648enum GNUNET_GenericReturnValue
668 * Copy the raw data of this bloomfilter into
669 * the given data array.
670 *
671 * @param bf bloomfilter to take the raw data from
672 * @param data where to write the data
673 * @param size the size of the given data array
674 * @return #GNUNET_SYSERR if the data array is not big enough
675 */
676int
677GNUNET_CONTAINER_bloomfilter_get_raw_data ( 649GNUNET_CONTAINER_bloomfilter_get_raw_data (
678 const struct GNUNET_CONTAINER_BloomFilter *bf, 650 const struct GNUNET_CONTAINER_BloomFilter *bf,
679 char *data, 651 char *data,
@@ -688,13 +660,6 @@ GNUNET_CONTAINER_bloomfilter_get_raw_data (
688} 660}
689 661
690 662
691/**
692 * Free the space associated with a filter
693 * in memory, flush to drive if needed (do not
694 * free the space on the drive)
695 *
696 * @param bf the filter
697 */
698void 663void
699GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf) 664GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf)
700{ 665{
@@ -708,11 +673,6 @@ GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf)
708} 673}
709 674
710 675
711/**
712 * Reset a bloom filter to empty. Clears the file on disk.
713 *
714 * @param bf the filter
715 */
716void 676void
717GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf) 677GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf)
718{ 678{
@@ -725,55 +685,38 @@ GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf)
725} 685}
726 686
727 687
728/** 688bool
729 * Test if an element is in the filter.
730 *
731 * @param e the element
732 * @param bf the filter
733 * @return #GNUNET_YES if the element is in the filter, #GNUNET_NO if not
734 */
735int
736GNUNET_CONTAINER_bloomfilter_test ( 689GNUNET_CONTAINER_bloomfilter_test (
737 const struct GNUNET_CONTAINER_BloomFilter *bf, 690 const struct GNUNET_CONTAINER_BloomFilter *bf,
738 const struct GNUNET_HashCode *e) 691 const struct GNUNET_HashCode *e)
739{ 692{
740 int res; 693 bool res;
741 694
742 if (NULL == bf) 695 if (NULL == bf)
743 return GNUNET_YES; 696 return true;
744 res = GNUNET_YES; 697 res = true;
745 iterateBits (bf, &testBitCallback, &res, e); 698 iterateBits (bf,
699 &testBitCallback,
700 &res,
701 e);
746 return res; 702 return res;
747} 703}
748 704
749 705
750/**
751 * Add an element to the filter
752 *
753 * @param bf the filter
754 * @param e the element
755 */
756void 706void
757GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf, 707GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
758 const struct GNUNET_HashCode *e) 708 const struct GNUNET_HashCode *e)
759{ 709{
760 if (NULL == bf) 710 if (NULL == bf)
761 return; 711 return;
762 iterateBits (bf, &incrementBitCallback, bf, e); 712 iterateBits (bf,
713 &incrementBitCallback,
714 bf,
715 e);
763} 716}
764 717
765 718
766/** 719enum GNUNET_GenericReturnValue
767 * Or the entries of the given raw data array with the
768 * data of the given bloom filter. Assumes that
769 * the size of the data array and the current filter
770 * match.
771 *
772 * @param bf the filter
773 * @param data the data to or-in
774 * @param size number of bytes in data
775 */
776int
777GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf, 720GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
778 const char *data, 721 const char *data,
779 size_t size) 722 size_t size)
@@ -799,17 +742,7 @@ GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
799} 742}
800 743
801 744
802/** 745enum GNUNET_GenericReturnValue
803 * Or the entries of the given raw data array with the
804 * data of the given bloom filter. Assumes that
805 * the size of the data array and the current filter
806 * match.
807 *
808 * @param bf the filter
809 * @param to_or the bloomfilter to or-in
810 * @return #GNUNET_OK on success
811 */
812int
813GNUNET_CONTAINER_bloomfilter_or2 ( 746GNUNET_CONTAINER_bloomfilter_or2 (
814 struct GNUNET_CONTAINER_BloomFilter *bf, 747 struct GNUNET_CONTAINER_BloomFilter *bf,
815 const struct GNUNET_CONTAINER_BloomFilter *to_or) 748 const struct GNUNET_CONTAINER_BloomFilter *to_or)
@@ -840,12 +773,6 @@ GNUNET_CONTAINER_bloomfilter_or2 (
840} 773}
841 774
842 775
843/**
844 * Remove an element from the filter.
845 *
846 * @param bf the filter
847 * @param e the element to remove
848 */
849void 776void
850GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf, 777GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
851 const struct GNUNET_HashCode *e) 778 const struct GNUNET_HashCode *e)
@@ -854,21 +781,13 @@ GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
854 return; 781 return;
855 if (NULL == bf->filename) 782 if (NULL == bf->filename)
856 return; 783 return;
857 iterateBits (bf, &decrementBitCallback, bf, e); 784 iterateBits (bf,
785 &decrementBitCallback,
786 bf,
787 e);
858} 788}
859 789
860 790
861/**
862 * Resize a bloom filter. Note that this operation
863 * is pretty costly. Essentially, the bloom filter
864 * needs to be completely re-build.
865 *
866 * @param bf the filter
867 * @param iterator an iterator over all elements stored in the BF
868 * @param iterator_cls argument to the iterator function
869 * @param size the new size for the filter
870 * @param k the new number of GNUNET_CRYPTO_hash-function to apply per element
871 */
872void 791void
873GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf, 792GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
874 GNUNET_CONTAINER_HashCodeIterator iterator, 793 GNUNET_CONTAINER_HashCodeIterator iterator,
diff --git a/src/util/test_container_bloomfilter.c b/src/util/test_container_bloomfilter.c
index 67fbaf38b..06a3fb500 100644
--- a/src/util/test_container_bloomfilter.c
+++ b/src/util/test_container_bloomfilter.c
@@ -31,6 +31,47 @@
31#define SIZE 65536 31#define SIZE 65536
32#define TESTFILE "/tmp/bloomtest.dat" 32#define TESTFILE "/tmp/bloomtest.dat"
33 33
34
35static void
36bernd_interop (void)
37{
38 struct GNUNET_HashCode hc;
39 char val[128];
40 size_t len;
41 struct GNUNET_CONTAINER_BloomFilter *bf;
42
43 len = GNUNET_DNSPARSER_hex_to_bin (
44 "ac4d46b62f8ddaf3cefbc1c01e47536b7ff297cb081e27a396362b1e92e5729b",
45 val);
46 GNUNET_assert (len < 128);
47 GNUNET_CRYPTO_hash (val,
48 len,
49 &hc);
50 fprintf (stderr,
51 "sha512: %s\n",
52 GNUNET_DNSPARSER_bin_to_hex (&hc,
53 sizeof (hc)));
54 bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
55 128,
56 16);
57 GNUNET_CONTAINER_bloomfilter_add (bf,
58 &hc);
59 len = GNUNET_CONTAINER_bloomfilter_get_size (bf);
60 {
61 char raw[len];
62
63 GNUNET_CONTAINER_bloomfilter_get_raw_data (bf,
64 raw,
65 len);
66 fprintf (stderr,
67 "BF: %s\n",
68 GNUNET_DNSPARSER_bin_to_hex (raw,
69 len));
70 }
71
72}
73
74
34/** 75/**
35 * Generate a random hashcode. 76 * Generate a random hashcode.
36 */ 77 */
@@ -68,7 +109,14 @@ main (int argc, char *argv[])
68 char buf[SIZE]; 109 char buf[SIZE];
69 struct stat sbuf; 110 struct stat sbuf;
70 111
71 GNUNET_log_setup ("test-container-bloomfilter", "WARNING", NULL); 112 GNUNET_log_setup ("test-container-bloomfilter",
113 "WARNING",
114 NULL);
115 if (0)
116 {
117 bernd_interop ();
118 return 0;
119 }
72 GNUNET_CRYPTO_seed_weak_random (1); 120 GNUNET_CRYPTO_seed_weak_random (1);
73 if (0 == stat (TESTFILE, &sbuf)) 121 if (0 == stat (TESTFILE, &sbuf))
74 if (0 != unlink (TESTFILE)) 122 if (0 != unlink (TESTFILE))