gnunet-service-setu_strata_estimator.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2012 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file set/gnunet-service-setu_strata_estimator.c
  18. * @brief invertible bloom filter
  19. * @author Florian Dold
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "ibf.h"
  25. #include "gnunet-service-setu_strata_estimator.h"
  26. /**
  27. * Write the given strata estimator to the buffer.
  28. *
  29. * @param se strata estimator to serialize
  30. * @param[out] buf buffer to write to, must be of appropriate size
  31. * @return number of bytes written to @a buf
  32. */
  33. size_t
  34. strata_estimator_write (const struct StrataEstimator *se,
  35. void *buf)
  36. {
  37. char *sbuf = buf;
  38. size_t osize;
  39. GNUNET_assert (NULL != se);
  40. for (unsigned int i = 0; i < se->strata_count; i++)
  41. {
  42. ibf_write_slice (se->strata[i],
  43. 0,
  44. se->ibf_size,
  45. &sbuf[se->ibf_size * IBF_BUCKET_SIZE * i]);
  46. }
  47. osize = se->ibf_size * IBF_BUCKET_SIZE * se->strata_count;
  48. {
  49. char *cbuf;
  50. size_t nsize;
  51. if (GNUNET_YES ==
  52. GNUNET_try_compression (buf,
  53. osize,
  54. &cbuf,
  55. &nsize))
  56. {
  57. GNUNET_memcpy (buf,
  58. cbuf,
  59. nsize);
  60. osize = nsize;
  61. GNUNET_free (cbuf);
  62. }
  63. }
  64. return osize;
  65. }
  66. /**
  67. * Read strata from the buffer into the given strata
  68. * estimator. The strata estimator must already be allocated.
  69. *
  70. * @param buf buffer to read from
  71. * @param buf_len number of bytes in @a buf
  72. * @param is_compressed is the data compressed?
  73. * @param[out] se strata estimator to write to
  74. * @return #GNUNET_OK on success
  75. */
  76. int
  77. strata_estimator_read (const void *buf,
  78. size_t buf_len,
  79. int is_compressed,
  80. struct StrataEstimator *se)
  81. {
  82. size_t osize;
  83. char *dbuf;
  84. dbuf = NULL;
  85. if (GNUNET_YES == is_compressed)
  86. {
  87. osize = se->ibf_size * IBF_BUCKET_SIZE * se->strata_count;
  88. dbuf = GNUNET_decompress (buf,
  89. buf_len,
  90. osize);
  91. if (NULL == dbuf)
  92. {
  93. GNUNET_break_op (0); /* bad compressed input data */
  94. return GNUNET_SYSERR;
  95. }
  96. buf = dbuf;
  97. buf_len = osize;
  98. }
  99. if (buf_len != se->strata_count * se->ibf_size * IBF_BUCKET_SIZE)
  100. {
  101. GNUNET_break (0); /* very odd error */
  102. GNUNET_free (dbuf);
  103. return GNUNET_SYSERR;
  104. }
  105. for (unsigned int i = 0; i < se->strata_count; i++)
  106. {
  107. ibf_read_slice (buf, 0, se->ibf_size, se->strata[i]);
  108. buf += se->ibf_size * IBF_BUCKET_SIZE;
  109. }
  110. GNUNET_free (dbuf);
  111. return GNUNET_OK;
  112. }
  113. /**
  114. * Add a key to the strata estimator.
  115. *
  116. * @param se strata estimator to add the key to
  117. * @param key key to add
  118. */
  119. void
  120. strata_estimator_insert (struct StrataEstimator *se,
  121. struct IBF_Key key)
  122. {
  123. uint64_t v;
  124. unsigned int i;
  125. v = key.key_val;
  126. /* count trailing '1'-bits of v */
  127. for (i = 0; v & 1; v >>= 1, i++)
  128. /* empty */;
  129. ibf_insert (se->strata[i], key);
  130. }
  131. /**
  132. * Remove a key from the strata estimator.
  133. *
  134. * @param se strata estimator to remove the key from
  135. * @param key key to remove
  136. */
  137. void
  138. strata_estimator_remove (struct StrataEstimator *se,
  139. struct IBF_Key key)
  140. {
  141. uint64_t v;
  142. unsigned int i;
  143. v = key.key_val;
  144. /* count trailing '1'-bits of v */
  145. for (i = 0; v & 1; v >>= 1, i++)
  146. /* empty */;
  147. ibf_remove (se->strata[i], key);
  148. }
  149. /**
  150. * Create a new strata estimator with the given parameters.
  151. *
  152. * @param strata_count number of stratas, that is, number of ibfs in the estimator
  153. * @param ibf_size size of each ibf stratum
  154. * @param ibf_hashnum hashnum parameter of each ibf
  155. * @return a freshly allocated, empty strata estimator, NULL on error
  156. */
  157. struct StrataEstimator *
  158. strata_estimator_create (unsigned int strata_count,
  159. uint32_t ibf_size,
  160. uint8_t ibf_hashnum)
  161. {
  162. struct StrataEstimator *se;
  163. se = GNUNET_new (struct StrataEstimator);
  164. se->strata_count = strata_count;
  165. se->ibf_size = ibf_size;
  166. se->strata = GNUNET_new_array (strata_count,
  167. struct InvertibleBloomFilter *);
  168. for (unsigned int i = 0; i < strata_count; i++)
  169. {
  170. se->strata[i] = ibf_create (ibf_size, ibf_hashnum);
  171. if (NULL == se->strata[i])
  172. {
  173. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  174. "Failed to allocate memory for strata estimator\n");
  175. for (unsigned int j = 0; j < i; j++)
  176. ibf_destroy (se->strata[i]);
  177. GNUNET_free (se);
  178. return NULL;
  179. }
  180. }
  181. return se;
  182. }
  183. /**
  184. * Estimate set difference with two strata estimators,
  185. * i.e. arrays of IBFs.
  186. * Does not not modify its arguments.
  187. *
  188. * @param se1 first strata estimator
  189. * @param se2 second strata estimator
  190. * @return the estimated difference
  191. */
  192. unsigned int
  193. strata_estimator_difference (const struct StrataEstimator *se1,
  194. const struct StrataEstimator *se2)
  195. {
  196. unsigned int count;
  197. GNUNET_assert (se1->strata_count == se2->strata_count);
  198. count = 0;
  199. for (int i = se1->strata_count - 1; i >= 0; i--)
  200. {
  201. struct InvertibleBloomFilter *diff;
  202. /* number of keys decoded from the ibf */
  203. /* FIXME: implement this without always allocating new IBFs */
  204. diff = ibf_dup (se1->strata[i]);
  205. ibf_subtract (diff,
  206. se2->strata[i]);
  207. for (int ibf_count = 0; GNUNET_YES; ibf_count++)
  208. {
  209. int more;
  210. more = ibf_decode (diff,
  211. NULL,
  212. NULL);
  213. if (GNUNET_NO == more)
  214. {
  215. count += ibf_count;
  216. break;
  217. }
  218. /* Estimate if decoding fails or would not terminate */
  219. if ( (GNUNET_SYSERR == more) ||
  220. (ibf_count > diff->size) )
  221. {
  222. ibf_destroy (diff);
  223. return count * (1 << (i + 1));
  224. }
  225. }
  226. ibf_destroy (diff);
  227. }
  228. return count;
  229. }
  230. /**
  231. * Make a copy of a strata estimator.
  232. *
  233. * @param se the strata estimator to copy
  234. * @return the copy
  235. */
  236. struct StrataEstimator *
  237. strata_estimator_dup (struct StrataEstimator *se)
  238. {
  239. struct StrataEstimator *c;
  240. c = GNUNET_new (struct StrataEstimator);
  241. c->strata_count = se->strata_count;
  242. c->ibf_size = se->ibf_size;
  243. c->strata = GNUNET_new_array (se->strata_count,
  244. struct InvertibleBloomFilter *);
  245. for (unsigned int i = 0; i < se->strata_count; i++)
  246. c->strata[i] = ibf_dup (se->strata[i]);
  247. return c;
  248. }
  249. /**
  250. * Destroy a strata estimator, free all of its resources.
  251. *
  252. * @param se strata estimator to destroy.
  253. */
  254. void
  255. strata_estimator_destroy (struct StrataEstimator *se)
  256. {
  257. for (unsigned int i = 0; i < se->strata_count; i++)
  258. ibf_destroy (se->strata[i]);
  259. GNUNET_free (se->strata);
  260. GNUNET_free (se);
  261. }