0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2025-02-18 09:40:12 +01:00

ircd:Ⓜ️:room::state: Add state::history interface derived from state::space.

This commit is contained in:
Jason Volk 2019-06-16 00:14:15 -07:00
parent 6443fad43c
commit 24ce076c3d
5 changed files with 282 additions and 0 deletions

View file

@ -185,6 +185,7 @@ struct ircd::m::room
#include "timeline.h"
#include "state.h"
#include "state_space.h"
#include "state_history.h"
#include "members.h"
#include "origins.h"
#include "head.h"

View file

@ -24,6 +24,7 @@ struct ircd::m::room::state
{
struct opts;
struct space;
struct history;
using keys = std::function<void (const string_view &)>;
using keys_bool = std::function<bool (const string_view &)>;

View file

@ -0,0 +1,42 @@
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2019 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_ROOM_STATE_HISTORY_H
/// Interface to the state of a room at some previous point in time. This is
/// constructed out of the data obtained through the lower-level state::space
/// interface.
///
struct ircd::m::room::state::history
{
using closure = std::function<bool (const string_view &, const string_view &, const int64_t &, const event::idx &)>;
state::space space;
int64_t bound {-1};
public:
bool for_each(const string_view &type, const string_view &state_key, const closure &) const;
bool for_each(const string_view &type, const closure &) const;
bool for_each(const closure &) const;
size_t count(const string_view &type, const string_view &state_key) const;
size_t count(const string_view &type) const;
bool has(const string_view &type, const string_view &state_key) const;
bool has(const string_view &type) const;
event::idx get(std::nothrow_t, const string_view &type, const string_view &state_key) const;
event::idx get(const string_view &type, const string_view &state_key) const;
history(const m::room &, const int64_t &bound);
history(const m::room::id &, const m::event::id &);
history(const m::room &);
};

View file

@ -2581,6 +2581,178 @@ const
return false;
}
//
// room::state::history
//
ircd::m::room::state::history::history(const m::room &room)
:history
{
room, -1
}
{
}
ircd::m::room::state::history::history(const m::room::id &room_id,
const m::event::id &event_id)
:history
{
m::room
{
room_id, event_id
}
}
{
}
ircd::m::room::state::history::history(const m::room &room,
const int64_t &bound)
:space
{
room
}
,bound
{
bound < 0 && room.event_id?
m::get<int64_t>(m::index(room.event_id), "depth"):
bound
}
{
}
ircd::m::event::idx
ircd::m::room::state::history::get(const string_view &type,
const string_view &state_key)
const
{
const auto ret
{
get(std::nothrow, type, state_key)
};
if(unlikely(!ret))
throw m::NOT_FOUND
{
"(%s,%s) in %s @%ld$%s",
type,
state_key,
string_view{space.room.room_id},
bound,
string_view{space.room.event_id},
};
return ret;
}
ircd::m::event::idx
ircd::m::room::state::history::get(std::nothrow_t,
const string_view &type,
const string_view &state_key)
const
{
event::idx ret{0};
assert(type && defined(state_key));
for_each(type, state_key, [&ret]
(const auto &, const auto &, const auto &, const auto &event_idx)
{
ret = event_idx;
return false;
});
return ret;
}
bool
ircd::m::room::state::history::has(const string_view &type)
const
{
return has(type, string_view{});
}
bool
ircd::m::room::state::history::has(const string_view &type,
const string_view &state_key)
const
{
return !for_each(type, state_key, []
(const auto &type, const auto &state_key, const auto &depth, const auto &event_idx)
{
return false;
});
}
size_t
ircd::m::room::state::history::count(const string_view &type)
const
{
return count(type, string_view{});
}
size_t
ircd::m::room::state::history::count(const string_view &type,
const string_view &state_key)
const
{
size_t ret(0);
for_each(type, state_key, [&ret]
(const auto &type, const auto &state_key, const auto &depth, const auto &event_idx)
{
++ret;
return true;
});
return ret;
}
bool
ircd::m::room::state::history::for_each(const closure &closure)
const
{
return for_each(string_view{}, string_view{}, closure);
}
bool
ircd::m::room::state::history::for_each(const string_view &type,
const closure &closure)
const
{
return for_each(type, string_view{}, closure);
}
bool
ircd::m::room::state::history::for_each(const string_view &type,
const string_view &state_key,
const closure &closure)
const
{
char type_buf[m::event::TYPE_MAX_SIZE];
char state_key_buf[m::event::STATE_KEY_MAX_SIZE];
string_view last_type;
string_view last_state_key;
return space.for_each(type, state_key, [&]
(const auto &type, const auto &state_key, const auto &depth, const auto &event_idx)
{
if(bound > -1 && depth > bound)
return true;
if(type == last_type && state_key == last_state_key)
return true;
if(!closure(type, state_key, depth, event_idx))
return false;
if(type != last_type)
last_type = { type_buf, copy(type_buf, type) };
if(state_key != last_state_key)
last_state_key = { state_key_buf, copy(state_key_buf, state_key) };
return true;
});
}
//
// room::state::space
//

View file

@ -8617,6 +8617,72 @@ console_cmd__room__state__keys(opt &out, const string_view &line)
return true;
}
bool
console_cmd__room__state__history(opt &out, const string_view &line)
{
const params param{line, " ",
{
"room_id", "event_id|depth", "type", "state_key"
}};
const auto &room_id
{
m::room_id(param.at("room_id"))
};
const auto point
{
param.at("event_id|depth")
};
const string_view &type
{
param["type"]
};
const string_view &state_key
{
param["state_key"]
};
const m::event::id &event_id
{
!try_lex_cast<int64_t>(point)?
m::event::id{point}:
m::event::id{}
};
const int64_t bound
{
try_lex_cast<int64_t>(point)?
lex_cast<int64_t>(point):
-1L
};
const m::room room
{
room_id, event_id
};
const m::room::state::history history
{
room, bound
};
history.for_each(type, state_key, [&out]
(const auto &type, const auto &state_key, const auto &depth, const auto &event_idx)
{
out << std::setw(11) << std::left << event_idx;
out << " " << std::setw(9) << std::left << depth;
out << " " << std::setw(32) << std::left << type;
out << " " << std::setw(64) << std::left << state_key;
out << std::endl;
return true;
});
return true;
}
bool
console_cmd__room__state__space(opt &out, const string_view &line)
{