From 3a057c80a401f4c3256e83e0fad3cbda26f21376 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 30 Nov 2017 11:07:07 -0800 Subject: [PATCH] ircd: Add preliminary skeleton for cuckoo suite. --- include/ircd/cuckoo.h | 108 ++++++++++++++++++++++++++++++++++++++++++ include/ircd/stdinc.h | 1 + ircd/Makefile.am | 1 + ircd/cuckoo.cc | 67 ++++++++++++++++++++++++++ 4 files changed, 177 insertions(+) create mode 100644 include/ircd/cuckoo.h create mode 100644 ircd/cuckoo.cc diff --git a/include/ircd/cuckoo.h b/include/ircd/cuckoo.h new file mode 100644 index 000000000..fdf01763e --- /dev/null +++ b/include/ircd/cuckoo.h @@ -0,0 +1,108 @@ +/* + * charybdis: 21st Century IRC++d + * + * Copyright (C) 2017 Charybdis Development Team + * Copyright (C) 2017 Jason Volk + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice is present in all copies. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once +#define HAVE_IRCD_CUCKOO_H + +/// Cuckoo filtering and hashing. +/// +/// This is an API for building cuckoo filters for efficient set membership +/// tests. Cuckoo filters are a recently celebrated result by Bin Fan inspired +/// by Mitzenmacher's seminal thesis: the Power Of Two choices in randomized +/// linear load balancing. These filters are used extensively to optimize +/// queries made into matrix room state after accumulating events. +/// +/// Note that the hash residue has to be kept secret from an adversary who +/// may try to craft strings to attack the filter. A secret salt should be +/// used. Filters may also be serialized to the db so the salt will have to +/// persist secretly too. +/// +namespace ircd::cuckoo +{ + IRCD_EXCEPTION(ircd::error, error) + + template struct entry; + template struct bucket; + template struct table; + + struct filter; + struct counter; +} + +template +struct ircd::cuckoo::entry +:std::array +{ + using array_type = std::array; + +}; + +template +struct ircd::cuckoo::bucket +:std::array +{ + using array_type = std::array; + +}; + +template +struct ircd::cuckoo::table +:std::array +{ + using array_type = std::array; + + static_assert(is_powerof2(buckets), "Bucket count must be power of 2"); +}; + +struct ircd::cuckoo::filter +{ + using entry = cuckoo::entry; + using bucket = cuckoo::bucket; + using table = cuckoo::table; + + table a, b; + + bool has(const string_view &) const; + + void add(const string_view &); + void del(const string_view &); +}; + +struct ircd::cuckoo::counter +{ + using int_t = int32_t; + using entry = cuckoo::entry; + using bucket = cuckoo::bucket; + using table = cuckoo::table; + + table a, b; + + int_t count(const string_view &) const; + bool has(const string_view &) const; + + void add(const string_view &); + void del(const string_view &); +}; diff --git a/include/ircd/stdinc.h b/include/ircd/stdinc.h index ab9fc608e..906e61559 100644 --- a/include/ircd/stdinc.h +++ b/include/ircd/stdinc.h @@ -209,6 +209,7 @@ namespace ircd #include "tokens.h" #include "params.h" #include "iov.h" +#include "cuckoo.h" #include "parse.h" #include "rfc1459.h" #include "json/json.h" diff --git a/ircd/Makefile.am b/ircd/Makefile.am index 3ac1b9e11..af772f4bc 100644 --- a/ircd/Makefile.am +++ b/ircd/Makefile.am @@ -63,6 +63,7 @@ libircd_la_SOURCES = \ hash.cc \ rand.cc \ ctx.cc \ + cuckoo.cc \ db.cc \ exception.cc \ fmt.cc \ diff --git a/ircd/cuckoo.cc b/ircd/cuckoo.cc new file mode 100644 index 000000000..4c98d1bee --- /dev/null +++ b/ircd/cuckoo.cc @@ -0,0 +1,67 @@ +/* + * charybdis: 21st Century IRC++d + * + * Copyright (C) 2017 Charybdis Development Team + * Copyright (C) 2017 Jason Volk + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice is present in all copies. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +namespace ircd::cuckoo +{ +} + +void +ircd::cuckoo::counter::add(const string_view &s) +{ +} + +void +ircd::cuckoo::counter::del(const string_view &s) +{ +} + +bool +ircd::cuckoo::counter::has(const string_view &s) +const +{ + return false; +} + +ircd::cuckoo::counter::int_t +ircd::cuckoo::counter::count(const string_view &s) +const +{ + return 0; +} + +void +ircd::cuckoo::filter::add(const string_view &s) +{ +} + +void +ircd::cuckoo::filter::del(const string_view &s) +{ +} + +bool +ircd::cuckoo::filter::has(const string_view &s) +const +{ + return false; +}