diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-10-08 11:30:19 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-10-08 11:30:19 +0000 |
commit | ae53a8d1d043cbd5967fb01316a32685c7a8af9b (patch) | |
tree | a8d791aed993ac8691f68ee6152a6ffb14a88ee3 /src/set/gnunet-service-set_union_strata_estimator.c | |
parent | e105a542a8ddb22184712bfe565aa734f1d0349d (diff) | |
download | gnunet-ae53a8d1d043cbd5967fb01316a32685c7a8af9b.tar.gz gnunet-ae53a8d1d043cbd5967fb01316a32685c7a8af9b.zip |
add logic for strata compression
Diffstat (limited to 'src/set/gnunet-service-set_union_strata_estimator.c')
-rw-r--r-- | src/set/gnunet-service-set_union_strata_estimator.c | 90 |
1 files changed, 81 insertions, 9 deletions
diff --git a/src/set/gnunet-service-set_union_strata_estimator.c b/src/set/gnunet-service-set_union_strata_estimator.c index 38b32961c..e94d52650 100644 --- a/src/set/gnunet-service-set_union_strata_estimator.c +++ b/src/set/gnunet-service-set_union_strata_estimator.c | |||
@@ -21,6 +21,7 @@ | |||
21 | * @file set/gnunet-service-set_union_strata_estimator.c | 21 | * @file set/gnunet-service-set_union_strata_estimator.c |
22 | * @brief invertible bloom filter | 22 | * @brief invertible bloom filter |
23 | * @author Florian Dold | 23 | * @author Florian Dold |
24 | * @author Christian Grothoff | ||
24 | */ | 25 | */ |
25 | #include "platform.h" | 26 | #include "platform.h" |
26 | #include "gnunet_util_lib.h" | 27 | #include "gnunet_util_lib.h" |
@@ -29,12 +30,20 @@ | |||
29 | 30 | ||
30 | 31 | ||
31 | /** | 32 | /** |
33 | * Should we try compressing the strata estimator? This will | ||
34 | * break compatibility with the 0.10.1-network. | ||
35 | */ | ||
36 | #define FAIL_10_1_COMPATIBILTIY 0 | ||
37 | |||
38 | |||
39 | /** | ||
32 | * Write the given strata estimator to the buffer. | 40 | * Write the given strata estimator to the buffer. |
33 | * | 41 | * |
34 | * @param se strata estimator to serialize | 42 | * @param se strata estimator to serialize |
35 | * @param buf buffer to write to, must be of appropriate size | 43 | * @param[out] buf buffer to write to, must be of appropriate size |
44 | * @return number of bytes written to @a buf | ||
36 | */ | 45 | */ |
37 | void | 46 | size_t |
38 | strata_estimator_write (const struct StrataEstimator *se, | 47 | strata_estimator_write (const struct StrataEstimator *se, |
39 | void *buf) | 48 | void *buf) |
40 | { | 49 | { |
@@ -43,9 +52,32 @@ strata_estimator_write (const struct StrataEstimator *se, | |||
43 | GNUNET_assert (NULL != se); | 52 | GNUNET_assert (NULL != se); |
44 | for (i = 0; i < se->strata_count; i++) | 53 | for (i = 0; i < se->strata_count; i++) |
45 | { | 54 | { |
46 | ibf_write_slice (se->strata[i], 0, se->ibf_size, buf); | 55 | ibf_write_slice (se->strata[i], |
56 | 0, | ||
57 | se->ibf_size, | ||
58 | buf); | ||
47 | buf += se->ibf_size * IBF_BUCKET_SIZE; | 59 | buf += se->ibf_size * IBF_BUCKET_SIZE; |
48 | } | 60 | } |
61 | osize = se->ibf_size * IBF_BUCKET_SIZE * se->strata_count; | ||
62 | #if FAIL_10_1_COMPATIBILTIY | ||
63 | { | ||
64 | size_t osize; | ||
65 | char *cbuf; | ||
66 | size_t nsize; | ||
67 | |||
68 | if (GNUNET_YES == | ||
69 | GNUNET_try_compression (buf, | ||
70 | osize, | ||
71 | &cbuf, | ||
72 | &nsize)) | ||
73 | { | ||
74 | memcpy (buf, cbuf, nsize); | ||
75 | osize = nsize; | ||
76 | GNUNET_free (cbuf); | ||
77 | } | ||
78 | } | ||
79 | #endif | ||
80 | return osize; | ||
49 | } | 81 | } |
50 | 82 | ||
51 | 83 | ||
@@ -54,19 +86,50 @@ strata_estimator_write (const struct StrataEstimator *se, | |||
54 | * estimator. The strata estimator must already be allocated. | 86 | * estimator. The strata estimator must already be allocated. |
55 | * | 87 | * |
56 | * @param buf buffer to read from | 88 | * @param buf buffer to read from |
57 | * @param se strata estimator to write to | 89 | * @param buf_len number of bytes in @a buf |
90 | * @param is_compressed is the data compressed? | ||
91 | * @param[out] se strata estimator to write to | ||
92 | * @return #GNUNET_OK on success | ||
58 | */ | 93 | */ |
59 | void | 94 | int |
60 | strata_estimator_read (const void *buf, | 95 | strata_estimator_read (const void *buf, |
96 | size_t buf_len, | ||
97 | int is_compressed, | ||
61 | struct StrataEstimator *se) | 98 | struct StrataEstimator *se) |
62 | { | 99 | { |
63 | unsigned int i; | 100 | unsigned int i; |
101 | size_t osize; | ||
102 | char *dbuf; | ||
64 | 103 | ||
104 | dbuf = NULL; | ||
105 | if (GNUNET_YES == is_compressed) | ||
106 | { | ||
107 | osize = se->ibf_size * IBF_BUCKET_SIZE * se->strata_count; | ||
108 | dbuf = GNUNET_decompress (buf, | ||
109 | buf_len, | ||
110 | osize); | ||
111 | if (NULL == dbuf) | ||
112 | { | ||
113 | GNUNET_break_op (0); /* bad compressed input data */ | ||
114 | return GNUNET_SYSERR; | ||
115 | } | ||
116 | buf = dbuf; | ||
117 | buf_len = osize; | ||
118 | } | ||
119 | |||
120 | if (buf_len != se->strata_count * se->ibf_size * IBF_BUCKET_SIZE) | ||
121 | { | ||
122 | GNUNET_break (0); /* very odd error */ | ||
123 | GNUNET_free_non_null (dbuf); | ||
124 | return GNUNET_SYSERR; | ||
125 | } | ||
65 | for (i = 0; i < se->strata_count; i++) | 126 | for (i = 0; i < se->strata_count; i++) |
66 | { | 127 | { |
67 | ibf_read_slice (buf, 0, se->ibf_size, se->strata[i]); | 128 | ibf_read_slice (buf, 0, se->ibf_size, se->strata[i]); |
68 | buf += se->ibf_size * IBF_BUCKET_SIZE; | 129 | buf += se->ibf_size * IBF_BUCKET_SIZE; |
69 | } | 130 | } |
131 | GNUNET_free_non_null (dbuf); | ||
132 | return GNUNET_OK; | ||
70 | } | 133 | } |
71 | 134 | ||
72 | 135 | ||
@@ -118,7 +181,7 @@ strata_estimator_remove (struct StrataEstimator *se, | |||
118 | * @param strata_count number of stratas, that is, number of ibfs in the estimator | 181 | * @param strata_count number of stratas, that is, number of ibfs in the estimator |
119 | * @param ibf_size size of each ibf stratum | 182 | * @param ibf_size size of each ibf stratum |
120 | * @param ibf_hashnum hashnum parameter of each ibf | 183 | * @param ibf_hashnum hashnum parameter of each ibf |
121 | * @return a freshly allocated, empty strata estimator | 184 | * @return a freshly allocated, empty strata estimator, NULL on error |
122 | */ | 185 | */ |
123 | struct StrataEstimator * | 186 | struct StrataEstimator * |
124 | strata_estimator_create (unsigned int strata_count, | 187 | strata_estimator_create (unsigned int strata_count, |
@@ -127,14 +190,24 @@ strata_estimator_create (unsigned int strata_count, | |||
127 | { | 190 | { |
128 | struct StrataEstimator *se; | 191 | struct StrataEstimator *se; |
129 | unsigned int i; | 192 | unsigned int i; |
193 | unsigned int j; | ||
130 | 194 | ||
131 | /* fixme: allocate everything in one chunk */ | ||
132 | se = GNUNET_new (struct StrataEstimator); | 195 | se = GNUNET_new (struct StrataEstimator); |
133 | se->strata_count = strata_count; | 196 | se->strata_count = strata_count; |
134 | se->ibf_size = ibf_size; | 197 | se->ibf_size = ibf_size; |
135 | se->strata = GNUNET_malloc (sizeof (struct InvertibleBloomFilter *) * strata_count); | 198 | se->strata = GNUNET_new_array (strata_count, |
199 | struct InvertibleBloomFilter *); | ||
136 | for (i = 0; i < strata_count; i++) | 200 | for (i = 0; i < strata_count; i++) |
201 | { | ||
137 | se->strata[i] = ibf_create (ibf_size, ibf_hashnum); | 202 | se->strata[i] = ibf_create (ibf_size, ibf_hashnum); |
203 | if (NULL == se->strata[i]) | ||
204 | { | ||
205 | for (j = 0; j < i; j++) | ||
206 | ibf_destroy (se->strata[i]); | ||
207 | GNUNET_free (se); | ||
208 | return NULL; | ||
209 | } | ||
210 | } | ||
138 | return se; | 211 | return se; |
139 | } | 212 | } |
140 | 213 | ||
@@ -226,4 +299,3 @@ strata_estimator_destroy (struct StrataEstimator *se) | |||
226 | GNUNET_free (se->strata); | 299 | GNUNET_free (se->strata); |
227 | GNUNET_free (se); | 300 | GNUNET_free (se); |
228 | } | 301 | } |
229 | |||