0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 10:12:39 +01:00

ircd: Add preliminary skeleton for cuckoo suite.

This commit is contained in:
Jason Volk 2017-11-30 11:07:07 -08:00
parent 315d86a8d2
commit 3a057c80a4
4 changed files with 177 additions and 0 deletions

108
include/ircd/cuckoo.h Normal file
View file

@ -0,0 +1,108 @@
/*
* charybdis: 21st Century IRC++d
*
* Copyright (C) 2017 Charybdis Development Team
* Copyright (C) 2017 Jason Volk <jason@zemos.net>
*
* 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<class word, size_t words> struct entry;
template<class entry, size_t entries> struct bucket;
template<class bucket, size_t buckets> struct table;
struct filter;
struct counter;
}
template<class word,
size_t words>
struct ircd::cuckoo::entry
:std::array<word, words>
{
using array_type = std::array<word, words>;
};
template<class entry,
size_t entries>
struct ircd::cuckoo::bucket
:std::array<entry, entries>
{
using array_type = std::array<entry, entries>;
};
template<class bucket,
size_t buckets>
struct ircd::cuckoo::table
:std::array<bucket, buckets>
{
using array_type = std::array<bucket, buckets>;
static_assert(is_powerof2(buckets), "Bucket count must be power of 2");
};
struct ircd::cuckoo::filter
{
using entry = cuckoo::entry<uint8_t, 1>;
using bucket = cuckoo::bucket<entry, 1>;
using table = cuckoo::table<bucket, 32>;
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<int_t, 1>;
using bucket = cuckoo::bucket<entry, 1>;
using table = cuckoo::table<bucket, 32>;
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 &);
};

View file

@ -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"

View file

@ -63,6 +63,7 @@ libircd_la_SOURCES = \
hash.cc \
rand.cc \
ctx.cc \
cuckoo.cc \
db.cc \
exception.cc \
fmt.cc \

67
ircd/cuckoo.cc Normal file
View file

@ -0,0 +1,67 @@
/*
* charybdis: 21st Century IRC++d
*
* Copyright (C) 2017 Charybdis Development Team
* Copyright (C) 2017 Jason Volk <jason@zemos.net>
*
* 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;
}