]> git.ozlabs.org Git - ccan/blob - ccan/invbloom/invbloom.c
08d83c882f8786786453c9981e34fb5c78359b76
[ccan] / ccan / invbloom / invbloom.c
1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #include "invbloom.h"
3 #include <ccan/hash/hash.h>
4 #include <ccan/endian/endian.h>
5 #include <assert.h>
6
7 /*      "We will show that hash_count values of 3 or 4 work well in practice"
8
9         From:
10
11         Eppstein, David, et al. "What's the difference?: efficient set reconciliation without prior context." ACM SIGCOMM Computer Communication Review. Vol. 41. No. 4. ACM, 2011. http://conferences.sigcomm.org/sigcomm/2011/papers/sigcomm/p218.pdf
12 */
13 #define NUM_HASHES 4
14
15 struct invbloom *invbloom_new_(const tal_t *ctx,
16                                size_t id_size,
17                                size_t n_elems,
18                                u32 salt)
19 {
20         struct invbloom *ib = tal(ctx, struct invbloom);
21
22         if (ib) {
23                 ib->n_elems = n_elems;
24                 ib->id_size = id_size;
25                 ib->salt = salt;
26                 ib->singleton = NULL;
27                 ib->count = tal_arrz(ib, s32, n_elems);
28                 ib->idsum = tal_arrz(ib, u8, id_size * n_elems);
29                 if (!ib->count || !ib->idsum)
30                         ib = tal_free(ib);
31         }
32         return ib;
33 }
34
35 void invbloom_singleton_cb_(struct invbloom *ib,
36                             void (*cb)(struct invbloom *,
37                                        size_t bucket, void *),
38                             void *data)
39 {
40         ib->singleton = cb;
41         ib->singleton_data = data;
42 }
43
44 static size_t hash_bucket(const struct invbloom *ib, const void *id, size_t i)
45 {
46         return hash((const char *)id, ib->id_size, ib->salt+i*7) % ib->n_elems;
47 }
48
49 static u8 *idsum_ptr(const struct invbloom *ib, size_t bucket)
50 {
51         return (u8 *)ib->idsum + bucket * ib->id_size;
52 }
53
54 static void check_for_singleton(struct invbloom *ib, size_t bucket)
55 {
56         if (!ib->singleton)
57                 return;
58
59         if (ib->count[bucket] != 1 && ib->count[bucket] != -1)
60                 return;
61
62         ib->singleton(ib, bucket, ib->singleton_data);
63 }
64
65 static void add_to_bucket(struct invbloom *ib, size_t n, const u8 *id)
66 {
67         size_t i;
68         u8 *idsum = idsum_ptr(ib, n);
69         
70         ib->count[n]++;
71
72         for (i = 0; i < ib->id_size; i++)
73                 idsum[i] ^= id[i];
74
75         check_for_singleton(ib, n);
76 }
77
78 static void remove_from_bucket(struct invbloom *ib, size_t n, const u8 *id)
79 {
80         size_t i;
81         u8 *idsum = idsum_ptr(ib, n);
82
83         ib->count[n]--;
84         for (i = 0; i < ib->id_size; i++)
85                 idsum[i] ^= id[i];
86
87         check_for_singleton(ib, n);
88 }
89
90 void invbloom_insert(struct invbloom *ib, const void *id)
91 {
92         unsigned int i;
93
94         for (i = 0; i < NUM_HASHES; i++)
95                 add_to_bucket(ib, hash_bucket(ib, id, i), id);
96 }
97
98 void invbloom_delete(struct invbloom *ib, const void *id)
99 {
100         unsigned int i;
101
102         for (i = 0; i < NUM_HASHES; i++)
103                 remove_from_bucket(ib, hash_bucket(ib, id, i), id);
104 }
105
106 static bool all_zero(const u8 *mem, size_t size)
107 {
108         unsigned int i;
109
110         for (i = 0; i < size; i++)
111                 if (mem[i])
112                         return false;
113         return true;
114 }
115
116 bool invbloom_get(const struct invbloom *ib, const void *id)
117 {
118         unsigned int i;
119
120         for (i = 0; i < NUM_HASHES; i++) {
121                 size_t h = hash_bucket(ib, id, i);
122                 u8 *idsum = idsum_ptr(ib, h);
123
124                 if (ib->count[h] == 0 && all_zero(idsum, ib->id_size))
125                         return false;
126
127                 if (ib->count[h] == 1)
128                         return (memcmp(idsum, id, ib->id_size) == 0);
129         }
130         return false;
131 }
132
133 static void *extract(const tal_t *ctx, struct invbloom *ib, int count)
134 {
135         size_t i;
136
137         /* FIXME: this makes full extraction O(n^2). */
138         for (i = 0; i < ib->n_elems; i++) {
139                 void *id;
140
141                 if (ib->count[i] != count)
142                         continue;
143
144                 id = tal_dup(ctx, u8, idsum_ptr(ib, i), ib->id_size, 0);
145                 return id;
146         }
147         return NULL;
148 }
149
150 void *invbloom_extract(const tal_t *ctx, struct invbloom *ib)
151 {
152         void *id;
153
154         id = extract(ctx, ib, 1);
155         if (id)
156                 invbloom_delete(ib, id);
157         return id;
158 }
159
160 void *invbloom_extract_negative(const tal_t *ctx, struct invbloom *ib)
161 {
162         void *id;
163
164         id = extract(ctx, ib, -1);
165         if (id)
166                 invbloom_insert(ib, id);
167         return id;
168 }
169
170 void invbloom_subtract(struct invbloom *ib1, const struct invbloom *ib2)
171 {
172         size_t i;
173
174         assert(ib1->n_elems == ib2->n_elems);
175         assert(ib1->id_size == ib2->id_size);
176         assert(ib1->salt == ib2->salt);
177
178         for (i = 0; i < ib1->n_elems * ib1->id_size; i++)
179                 ib1->idsum[i] ^= ib2->idsum[i];
180
181         for (i = 0; i < ib1->n_elems; i++) {
182                 ib1->count[i] -= ib2->count[i];
183                 check_for_singleton(ib1, i);
184         }
185 }
186
187 bool invbloom_empty(const struct invbloom *ib)
188 {
189         size_t i;
190
191         for (i = 0; i < ib->n_elems; i++) {
192                 if (ib->count[i])
193                         return false;
194                 if (!all_zero(idsum_ptr(ib, i), ib->id_size))
195                         return false;
196         }
197         return true;
198 }