]> git.ozlabs.org Git - ccan/blob - ccan/invbloom/invbloom.h
6306d5b84e0b8eb3c82069d8268ccfb9845d082c
[ccan] / ccan / invbloom / invbloom.h
1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #ifndef CCAN_INVBLOOM_H
3 #define CCAN_INVBLOOM_H
4 #include <ccan/short_types/short_types.h>
5 #include <ccan/tal/tal.h>
6
7 struct invbloom {
8         size_t n_elems;
9         size_t id_size;
10         u32 salt;
11         s32 *count; /* [n_elems] */
12         u8 *idsum; /* [n_elems][id_size] */
13 };
14
15 /**
16  * invbloom_new - create a new invertable bloom lookup table
17  * @ctx: context to tal() from, or NULL.
18  * @type: type to place into the buckets (must not contain padding)
19  * @n_elems: number of entries in table
20  * @salt: 32 bit seed for table
21  *
22  * Returns a new table, which can be freed with tal_free().
23  */
24 #define invbloom_new(ctx, type, n_elems, salt)  \
25         invbloom_new_((ctx), sizeof(type), (n_elems), (salt))
26 struct invbloom *invbloom_new_(const tal_t *ctx,
27                                size_t id_size,
28                                size_t n_elems, u32 salt);
29
30
31 /**
32  * invbloom_insert - add a new element
33  * @ib: the invertable bloom lookup table.
34  * @elem: the element
35  *
36  * This is guaranteed to be the inverse of invbloom_delete.
37  */
38 void invbloom_insert(struct invbloom *ib, const void *elem);
39
40 /**
41  * invbloom_delete - remove an element
42  * @ib: the invertable bloom lookup table.
43  * @elem: the element
44  *
45  * Note that this doesn't check the element was previously added (as
46  * that can not be done in general anyway).
47  *
48  * This is guaranteed to be the inverse of invbloom_delete.
49  */
50 void invbloom_delete(struct invbloom *ib, const void *elem);
51
52 /**
53  * invbloom_get - check if an element is (probably) in the table.
54  * @ib: the invertable bloom lookup table.
55  * @elem: the element
56  *
57  * This may return a false negative if the table is too full.
58  *
59  * It will only return a false positive if deletions and insertions
60  * don't match, and have managed to produce a result which matches the
61  * element.  This is much less likely.
62  */
63 bool invbloom_get(const struct invbloom *ib, const void *elem);
64
65 /**
66  * invbloom_extract - try to recover an entry added to a bloom lookup table.
67  * @ctx: the context to tal() the return value from.
68  * @ib: the invertable bloom lookup table.
69  *
70  * This may be able to recover (and delete) an element which was
71  * invbloom_insert()ed into the table (and not deleted).  This will
72  * not work if the table is too full.
73  *
74  * It may return a bogus element if deletions and insertions don't
75  * match.
76  */
77 void *invbloom_extract(const tal_t *ctx, struct invbloom *ib);
78
79 /**
80  * invbloom_extract_negative - try to recover an entry deleted from a bloom lookup table.
81  * @ctx: the context to tal() the return value from.
82  * @ib: the invertable bloom lookup table.
83  *
84  * This may be able to recover (and insert/undo) an element which was
85  * invbloom_delete()ed from the table (and not inserted).  This will
86  * not work if the table is too full.
87  *
88  * It may return a bogus element if deletions and insertions don't
89  * match.
90  */
91 void *invbloom_extract_negative(const tal_t *ctx, struct invbloom *ib);
92
93 /**
94  * invbloom_subtract - return the differences of two IBLTs.
95  * @ib1: the invertable bloom lookup table to alter
96  * @ib2: the invertable bloom lookup table to subtract.
97  *
98  * This produces exactly the same result as if a new table had all the
99  * elements only in @ib1 inserted, then all the elements onlt in @ib2
100  * deleted.
101  *
102  * ie. if @ib1 and @ib2 are similar, the result may be a usable by
103  * invbloom_extract and invbloom_extract_negative.
104  */
105 void invbloom_subtract(struct invbloom *ib1, const struct invbloom *ib2);
106
107 /**
108  * invbloom_empty - is an invertable bloom lookup table completely clean?
109  * @ib: the invertable bloom lookup table
110  *
111  * This is always true if @ib has had the same elements inserted and
112  * deleted.  It is far less likely to be true if different ones were
113  * deleted than inserted.
114  */
115 bool invbloom_empty(const struct invbloom *ib);
116 #endif /* CCAN_INVBLOOM_H */