2018-01-29 18:36:05 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 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. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_M_STATE_H
|
|
|
|
|
2018-02-08 00:53:58 +01:00
|
|
|
/// Matrix machine state unit and bus.
|
2018-02-04 22:11:26 +01:00
|
|
|
///
|
2018-04-12 02:18:15 +02:00
|
|
|
/// !!! note: This is actually a really low-level interface. If you don't
|
|
|
|
/// know that you are almost certainly looking for the room::state interface
|
|
|
|
/// and not this.
|
|
|
|
///
|
2018-02-04 22:11:26 +01:00
|
|
|
/// This section deals specifically with the aspect of Matrix called "state"
|
|
|
|
/// providing tools and utilities as well as local databasing. IO is done for
|
|
|
|
/// reads, and indirect into db::txn's for writes. No network activities are
|
|
|
|
/// conducted here.
|
|
|
|
///
|
|
|
|
/// These tools allow the user to query aspects of the "state" of a room at
|
|
|
|
/// the point of any event. Composed out of these queries are a suite of more
|
|
|
|
/// utilities to efficiently aid the Matrix virtual machine with the rest of
|
|
|
|
/// its tasks.
|
|
|
|
///
|
2018-01-29 18:36:05 +01:00
|
|
|
namespace ircd::m::state
|
|
|
|
{
|
|
|
|
struct node;
|
|
|
|
|
2018-02-01 03:49:40 +01:00
|
|
|
constexpr size_t ID_MAX_SZ { 64 };
|
|
|
|
constexpr size_t KEY_MAX_SZ { 256 + 256 + 16 };
|
|
|
|
constexpr size_t VAL_MAX_SZ { 256 + 16 };
|
|
|
|
constexpr size_t NODE_MAX_SZ { 4_KiB };
|
2018-02-02 00:18:45 +01:00
|
|
|
constexpr size_t NODE_MAX_KEY { 2 }; // tmp for now
|
|
|
|
constexpr size_t NODE_MAX_VAL { NODE_MAX_KEY };
|
|
|
|
constexpr size_t NODE_MAX_DEG { NODE_MAX_KEY + 1 }; // tmp for now
|
2018-02-01 03:49:40 +01:00
|
|
|
constexpr int8_t MAX_HEIGHT { 16 }; // good for few mil at any degree :)
|
2018-01-30 22:15:27 +01:00
|
|
|
|
2018-02-11 02:11:23 +01:00
|
|
|
using id = string_view;
|
2018-02-09 06:02:10 +01:00
|
|
|
using id_buffer = fixed_buffer<mutable_buffer, ID_MAX_SZ>;
|
2018-02-11 02:11:23 +01:00
|
|
|
using id_closure = std::function<void (const id &)>;
|
2018-02-06 04:02:37 +01:00
|
|
|
using val_closure = std::function<void (const string_view &)>;
|
2018-02-05 20:49:40 +01:00
|
|
|
using node_closure = std::function<void (const json::object &)>;
|
|
|
|
using search_closure = std::function<bool (const json::array &, const string_view &, const uint &, const uint &)>;
|
|
|
|
using iter_closure = std::function<void (const json::array &, const string_view &)>;
|
|
|
|
using iter_bool_closure = std::function<bool (const json::array &, const string_view &)>;
|
2018-01-30 22:15:27 +01:00
|
|
|
|
2018-02-05 20:49:40 +01:00
|
|
|
int keycmp(const json::array &a, const json::array &b);
|
2018-02-06 02:27:43 +01:00
|
|
|
bool prefix_eq(const json::array &a, const json::array &b);
|
2018-01-29 18:36:05 +01:00
|
|
|
json::array make_key(const mutable_buffer &out, const string_view &type, const string_view &state_key);
|
2018-02-06 02:27:43 +01:00
|
|
|
json::array make_key(const mutable_buffer &out, const string_view &type);
|
2018-01-29 18:36:05 +01:00
|
|
|
|
2018-02-11 02:11:23 +01:00
|
|
|
id set_node(db::txn &txn, const mutable_buffer &id, const json::object &node);
|
2018-04-12 02:18:51 +02:00
|
|
|
bool get_node(const std::nothrow_t, const string_view &id, const node_closure &);
|
2018-01-29 18:36:05 +01:00
|
|
|
void get_node(const string_view &id, const node_closure &);
|
|
|
|
|
2018-02-28 07:42:25 +01:00
|
|
|
id remove(db::txn &, const mutable_buffer &rootout, const id &rootin, const json::array &key);
|
|
|
|
id remove(db::txn &, const mutable_buffer &rootout, const id &rootin, const string_view &type, const string_view &state_key);
|
|
|
|
id remove(db::txn &, const mutable_buffer &rootout, const id &rootin, const event &);
|
|
|
|
|
2018-02-11 02:11:23 +01:00
|
|
|
id insert(db::txn &, const mutable_buffer &rootout, const id &rootin, const json::array &key, const m::id::event &);
|
|
|
|
id insert(db::txn &, const mutable_buffer &rootout, const id &rootin, const string_view &type, const string_view &state_key, const m::id::event &);
|
|
|
|
id insert(db::txn &, const mutable_buffer &rootout, const id &rootin, const event &);
|
2018-02-04 22:11:26 +01:00
|
|
|
|
2018-02-11 02:11:23 +01:00
|
|
|
bool dfs(const id &root, const json::array &key, const search_closure &);
|
|
|
|
bool dfs(const id &root, const search_closure &);
|
2018-02-06 02:27:43 +01:00
|
|
|
|
2018-02-25 04:27:17 +01:00
|
|
|
size_t count(const id &root, const string_view &type);
|
|
|
|
size_t count(const id &root);
|
|
|
|
|
2018-02-11 02:11:23 +01:00
|
|
|
bool test(const id &root, const iter_bool_closure &);
|
|
|
|
bool test(const id &root, const string_view &type, const iter_bool_closure &);
|
2018-02-24 04:38:56 +01:00
|
|
|
bool test(const id &root, const string_view &type, const string_view &state_key_lb, const iter_bool_closure &);
|
2018-02-10 09:26:15 +01:00
|
|
|
|
2018-02-11 02:11:23 +01:00
|
|
|
void for_each(const id &root, const iter_closure &);
|
|
|
|
void for_each(const id &root, const string_view &type, const iter_closure &);
|
2018-02-10 09:26:15 +01:00
|
|
|
|
2018-02-25 04:27:17 +01:00
|
|
|
size_t accumulate(const id &root, const iter_bool_closure &);
|
2018-02-04 22:11:26 +01:00
|
|
|
|
2018-02-11 02:11:23 +01:00
|
|
|
bool get(std::nothrow_t, const id &root, const json::array &key, const val_closure &);
|
|
|
|
void get(const id &root, const json::array &key, const val_closure &);
|
2018-02-09 20:55:49 +01:00
|
|
|
|
2018-02-11 02:11:23 +01:00
|
|
|
bool get(std::nothrow_t, const id &root, const string_view &type, const string_view &state_key, const val_closure &);
|
|
|
|
void get(const id &root, const string_view &type, const string_view &state_key, const val_closure &);
|
2018-01-29 18:36:05 +01:00
|
|
|
}
|
|
|
|
|
2018-02-04 22:11:26 +01:00
|
|
|
/// JSON property name strings specifically for use in m::state
|
2018-01-29 18:36:05 +01:00
|
|
|
namespace ircd::m::state::name
|
|
|
|
{
|
2018-02-25 04:47:30 +01:00
|
|
|
constexpr const char *const key {"k"};
|
|
|
|
constexpr const char *const val {"v"};
|
|
|
|
constexpr const char *const child {"c"};
|
|
|
|
constexpr const char *const count {"n"};
|
2018-01-29 18:36:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wsubobject-linkage"
|
2018-02-02 23:57:50 +01:00
|
|
|
|
|
|
|
/// Format for node: Node is plaintext and not binary at this time. In fact,
|
|
|
|
/// *evil chuckle*, node might as well be JSON and can easily become content
|
|
|
|
/// of another event sent to other rooms over network *snorts*. (important:
|
|
|
|
/// database is well compressed).
|
|
|
|
///
|
|
|
|
/// { ;
|
|
|
|
/// "k": ; Key array
|
|
|
|
/// [ ;
|
|
|
|
/// ["m.room.member", "@ar4an:matrix.org"], ; Left key
|
|
|
|
/// ["m.room.member", "@jzk:matrix.org"] ; Right key
|
|
|
|
/// ], ;
|
|
|
|
/// "v": ; Value array
|
|
|
|
/// [ ;
|
|
|
|
/// "$14961836116kXQRA:matrix.org", ; Left accept
|
|
|
|
/// "$15018692261xPQDB:matrix.org", ; Right accept
|
|
|
|
/// ] ;
|
|
|
|
/// "c": ; Child array
|
|
|
|
/// [ ;
|
|
|
|
/// "nPKN9twTF9a8k5dD7AApFcaraHTX", ; Left child
|
|
|
|
/// "PcxAAACvkvyUMz19AZcCfrC3S84s", ; Center child
|
|
|
|
/// "2jVYKIMKErJ6w6BLMhfVjsXearhB", ; Right child
|
|
|
|
/// ] ;
|
2018-02-25 04:27:17 +01:00
|
|
|
/// "n": ; Counting array
|
|
|
|
/// [ ;
|
|
|
|
/// 15, ; Left child value count
|
|
|
|
/// 12, ; Center child value count
|
|
|
|
/// 19, ; Right child value count
|
|
|
|
/// ] ;
|
2018-02-02 23:57:50 +01:00
|
|
|
/// } ;
|
|
|
|
///
|
2018-02-04 00:01:12 +01:00
|
|
|
/// Elements are ordered based on type+state_key lexical sort. The type and
|
|
|
|
/// the state_key strings are literally concatenated to this effect. They're
|
|
|
|
/// not hashed. We can have some more control over data locality this way. Any
|
|
|
|
/// number of values may be in a key array, not just type+state_key. The
|
|
|
|
/// concatenation involves the string with its surrounding quotes as to not
|
|
|
|
/// allow the user to mess about conflicting values:
|
2018-02-02 23:57:50 +01:00
|
|
|
/// ```
|
|
|
|
/// "m.room.member""@jzk" > "m.room.create"""
|
|
|
|
/// ```
|
|
|
|
/// Unlike traditional trees of such variety, the number of elements is not
|
2018-02-04 00:01:12 +01:00
|
|
|
/// really well defined and not even fixed. There just can be one more value
|
|
|
|
/// in the "child" list than there are keys in the "key" list. We have an
|
|
|
|
/// opportunity to vary the degree for different levels in different areas.
|
2018-01-29 18:36:05 +01:00
|
|
|
struct ircd::m::state::node
|
|
|
|
:json::tuple
|
|
|
|
<
|
2018-02-25 04:47:30 +01:00
|
|
|
json::property<name::key, json::array>,
|
|
|
|
json::property<name::val, json::array>,
|
|
|
|
json::property<name::child, json::array>,
|
|
|
|
json::property<name::count, json::array>
|
2018-01-29 18:36:05 +01:00
|
|
|
>
|
|
|
|
{
|
2018-02-02 00:18:45 +01:00
|
|
|
struct rep;
|
|
|
|
|
2018-01-30 22:15:27 +01:00
|
|
|
size_t keys() const;
|
|
|
|
size_t vals() const;
|
2018-02-01 03:49:40 +01:00
|
|
|
size_t childs() const;
|
2018-02-25 04:27:17 +01:00
|
|
|
size_t counts() const;
|
|
|
|
size_t totals() const;
|
2018-01-30 22:15:27 +01:00
|
|
|
|
|
|
|
json::array key(const size_t &) const;
|
|
|
|
string_view val(const size_t &) const;
|
2018-02-11 02:11:23 +01:00
|
|
|
state::id child(const size_t &) const;
|
2018-02-25 04:27:17 +01:00
|
|
|
size_t count(const size_t &) const;
|
2018-01-30 22:15:27 +01:00
|
|
|
|
2018-02-02 00:18:45 +01:00
|
|
|
size_t keys(json::array *const &out, const size_t &max) const;
|
|
|
|
size_t vals(string_view *const &out, const size_t &max) const;
|
2018-02-11 02:11:23 +01:00
|
|
|
size_t childs(state::id *const &out, const size_t &max) const;
|
2018-02-25 04:27:17 +01:00
|
|
|
size_t counts(size_t *const &out, const size_t &max) const;
|
2018-02-02 00:18:45 +01:00
|
|
|
|
2018-01-30 22:15:27 +01:00
|
|
|
size_t find(const json::array &key) const;
|
2018-02-02 00:18:45 +01:00
|
|
|
bool has_key(const json::array &key) const;
|
|
|
|
bool has_child(const size_t &) const;
|
2018-01-30 22:15:27 +01:00
|
|
|
|
2018-01-29 18:36:05 +01:00
|
|
|
using super_type::tuple;
|
|
|
|
using super_type::operator=;
|
|
|
|
};
|
|
|
|
#pragma GCC diagnostic pop
|
2018-02-02 00:18:45 +01:00
|
|
|
|
2018-02-04 00:01:12 +01:00
|
|
|
/// Internal representation of a node for manipulation purposes. This is
|
|
|
|
/// because json::tuple's (like most of json::) are oriented around the
|
|
|
|
/// dominant use-case of reading const datas. These arrays could be
|
|
|
|
/// vectors optimized with a local allocator but the size_t members are
|
|
|
|
/// used to count active elements instead. One more element than the node
|
|
|
|
/// maximum is provided so that insertions and sorts can safely take place
|
|
|
|
/// before splits.
|
2018-02-02 23:57:50 +01:00
|
|
|
struct ircd::m::state::node::rep
|
|
|
|
{
|
|
|
|
std::array<json::array, NODE_MAX_KEY + 1> keys;
|
|
|
|
std::array<string_view, NODE_MAX_VAL + 1> vals;
|
2018-02-11 02:11:23 +01:00
|
|
|
std::array<state::id, NODE_MAX_DEG + 1> chld;
|
2018-02-25 04:27:17 +01:00
|
|
|
std::array<size_t, NODE_MAX_DEG + 1> cnts;
|
2018-02-02 23:57:50 +01:00
|
|
|
size_t kn {0};
|
|
|
|
size_t vn {0};
|
|
|
|
size_t cn {0};
|
2018-02-25 04:27:17 +01:00
|
|
|
size_t nn {0};
|
2018-02-02 23:57:50 +01:00
|
|
|
|
|
|
|
bool full() const;
|
|
|
|
bool overfull() const;
|
2018-02-05 03:22:04 +01:00
|
|
|
bool duplicates() const;
|
2018-02-05 01:15:34 +01:00
|
|
|
size_t childs() const;
|
2018-02-25 04:27:17 +01:00
|
|
|
size_t counts() const;
|
|
|
|
size_t totals() const;
|
2018-02-02 23:57:50 +01:00
|
|
|
size_t find(const json::array &key) const;
|
|
|
|
|
2018-02-25 04:27:17 +01:00
|
|
|
void shl(const size_t &pos);
|
2018-02-02 23:57:50 +01:00
|
|
|
void shr(const size_t &pos);
|
|
|
|
|
|
|
|
json::object write(const mutable_buffer &out);
|
2018-02-11 02:11:23 +01:00
|
|
|
state::id write(db::txn &, const mutable_buffer &id);
|
2018-02-02 23:57:50 +01:00
|
|
|
|
|
|
|
rep(const node &node);
|
|
|
|
rep() = default;
|
|
|
|
};
|
|
|
|
|
2018-02-02 00:18:45 +01:00
|
|
|
static_assert
|
|
|
|
(
|
|
|
|
ircd::m::state::NODE_MAX_KEY == ircd::m::state::NODE_MAX_VAL
|
|
|
|
);
|