diff options
author | Christian Grothoff <christian@grothoff.org> | 2017-02-20 17:19:47 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2017-02-20 17:19:47 +0100 |
commit | f6f7fbbe98c110867febbcca647da8308be123c7 (patch) | |
tree | af69447cf4f08c417197685855c097c132aea8a1 /src/block/bg_bf.c | |
parent | a3882b58f1c5976677aa65b0af8a48e8e946b06e (diff) | |
download | gnunet-f6f7fbbe98c110867febbcca647da8308be123c7.tar.gz gnunet-f6f7fbbe98c110867febbcca647da8308be123c7.zip |
completed big block refactoring in preparation for SET-BLOCK integration
Diffstat (limited to 'src/block/bg_bf.c')
-rw-r--r-- | src/block/bg_bf.c | 64 |
1 files changed, 63 insertions, 1 deletions
diff --git a/src/block/bg_bf.c b/src/block/bg_bf.c index f03ae5247..9c4dc9060 100644 --- a/src/block/bg_bf.c +++ b/src/block/bg_bf.c | |||
@@ -56,6 +56,7 @@ struct BfGroupInternals | |||
56 | * Serialize state of a block group. | 56 | * Serialize state of a block group. |
57 | * | 57 | * |
58 | * @param bg group to serialize | 58 | * @param bg group to serialize |
59 | * @param[out] nonce set to the nonce of the @a bg | ||
59 | * @param[out] raw_data set to the serialized state | 60 | * @param[out] raw_data set to the serialized state |
60 | * @param[out] raw_data_size set to the number of bytes in @a raw_data | 61 | * @param[out] raw_data_size set to the number of bytes in @a raw_data |
61 | * @return #GNUNET_OK on success, #GNUNET_NO if serialization is not | 62 | * @return #GNUNET_OK on success, #GNUNET_NO if serialization is not |
@@ -63,6 +64,7 @@ struct BfGroupInternals | |||
63 | */ | 64 | */ |
64 | static int | 65 | static int |
65 | bf_group_serialize_cb (struct GNUNET_BLOCK_Group *bg, | 66 | bf_group_serialize_cb (struct GNUNET_BLOCK_Group *bg, |
67 | uint32_t *nonce, | ||
66 | void **raw_data, | 68 | void **raw_data, |
67 | size_t *raw_data_size) | 69 | size_t *raw_data_size) |
68 | { | 70 | { |
@@ -78,6 +80,7 @@ bf_group_serialize_cb (struct GNUNET_BLOCK_Group *bg, | |||
78 | GNUNET_break (0); | 80 | GNUNET_break (0); |
79 | return GNUNET_SYSERR; | 81 | return GNUNET_SYSERR; |
80 | } | 82 | } |
83 | *nonce = gi->bf_mutator; | ||
81 | *raw_data = raw; | 84 | *raw_data = raw; |
82 | *raw_data_size = gi->bf_size; | 85 | *raw_data_size = gi->bf_size; |
83 | return GNUNET_OK; | 86 | return GNUNET_OK; |
@@ -85,6 +88,60 @@ bf_group_serialize_cb (struct GNUNET_BLOCK_Group *bg, | |||
85 | 88 | ||
86 | 89 | ||
87 | /** | 90 | /** |
91 | * Mark elements as "seen" using a hash of the element. Not supported | ||
92 | * by all block plugins. | ||
93 | * | ||
94 | * @param bg group to update | ||
95 | * @param seen_results results already seen | ||
96 | * @param seen_results_count number of entries in @a seen_results | ||
97 | */ | ||
98 | static void | ||
99 | bf_group_mark_seen_cb (struct GNUNET_BLOCK_Group *bg, | ||
100 | const struct GNUNET_HashCode *seen_results, | ||
101 | unsigned int seen_results_count) | ||
102 | { | ||
103 | struct BfGroupInternals *gi = bg->internal_cls; | ||
104 | |||
105 | for (unsigned int i=0;i<seen_results_count;i++) | ||
106 | { | ||
107 | struct GNUNET_HashCode mhash; | ||
108 | |||
109 | GNUNET_BLOCK_mingle_hash (&seen_results[i], | ||
110 | gi->bf_mutator, | ||
111 | &mhash); | ||
112 | GNUNET_CONTAINER_bloomfilter_add (gi->bf, | ||
113 | &mhash); | ||
114 | } | ||
115 | } | ||
116 | |||
117 | |||
118 | /** | ||
119 | * Merge two groups, if possible. Not supported by all block plugins, | ||
120 | * can also fail if the nonces were different. | ||
121 | * | ||
122 | * @param bg1 group to update | ||
123 | * @param bg2 group to merge into @a bg1 | ||
124 | * @return #GNUNET_OK on success, #GNUNET_NO if the nonces were different and thus | ||
125 | * we failed. | ||
126 | */ | ||
127 | static int | ||
128 | bf_group_merge_cb (struct GNUNET_BLOCK_Group *bg1, | ||
129 | const struct GNUNET_BLOCK_Group *bg2) | ||
130 | { | ||
131 | struct BfGroupInternals *gi1 = bg1->internal_cls; | ||
132 | struct BfGroupInternals *gi2 = bg2->internal_cls; | ||
133 | |||
134 | if (gi1->bf_mutator != gi2->bf_mutator) | ||
135 | return GNUNET_NO; | ||
136 | if (gi1->bf_size != gi2->bf_size) | ||
137 | return GNUNET_NO; | ||
138 | GNUNET_CONTAINER_bloomfilter_or2 (gi1->bf, | ||
139 | gi2->bf); | ||
140 | return GNUNET_OK; | ||
141 | } | ||
142 | |||
143 | |||
144 | /** | ||
88 | * Destroy resources used by a block group. | 145 | * Destroy resources used by a block group. |
89 | * | 146 | * |
90 | * @param bg group to destroy, NULL is allowed | 147 | * @param bg group to destroy, NULL is allowed |
@@ -134,6 +191,8 @@ GNUNET_BLOCK_GROUP_bf_create (void *cls, | |||
134 | bg = GNUNET_new (struct GNUNET_BLOCK_Group); | 191 | bg = GNUNET_new (struct GNUNET_BLOCK_Group); |
135 | bg->type = type; | 192 | bg->type = type; |
136 | bg->serialize_cb = &bf_group_serialize_cb; | 193 | bg->serialize_cb = &bf_group_serialize_cb; |
194 | bg->mark_seen_cb = &bf_group_mark_seen_cb; | ||
195 | bg->merge_cb = &bf_group_merge_cb; | ||
137 | bg->destroy_cb = &bf_group_destroy_cb; | 196 | bg->destroy_cb = &bf_group_destroy_cb; |
138 | bg->internal_cls = gi; | 197 | bg->internal_cls = gi; |
139 | return bg; | 198 | return bg; |
@@ -154,9 +213,12 @@ int | |||
154 | GNUNET_BLOCK_GROUP_bf_test_and_set (struct GNUNET_BLOCK_Group *bg, | 213 | GNUNET_BLOCK_GROUP_bf_test_and_set (struct GNUNET_BLOCK_Group *bg, |
155 | const struct GNUNET_HashCode *hc) | 214 | const struct GNUNET_HashCode *hc) |
156 | { | 215 | { |
157 | struct BfGroupInternals *gi = bg->internal_cls; | 216 | struct BfGroupInternals *gi; |
158 | struct GNUNET_HashCode mhash; | 217 | struct GNUNET_HashCode mhash; |
159 | 218 | ||
219 | if (NULL == bg) | ||
220 | return GNUNET_NO; | ||
221 | gi = bg->internal_cls; | ||
160 | GNUNET_BLOCK_mingle_hash (hc, | 222 | GNUNET_BLOCK_mingle_hash (hc, |
161 | gi->bf_mutator, | 223 | gi->bf_mutator, |
162 | &mhash); | 224 | &mhash); |