0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-09 13:38:55 +02:00

ircd:Ⓜ️:room: Add type iteration interface.

This commit is contained in:
Jason Volk 2020-03-24 12:50:28 -07:00
parent 70315e2459
commit 7a6ba49c34
4 changed files with 143 additions and 0 deletions

View file

@ -114,6 +114,7 @@ struct ircd::m::room
struct state;
struct members;
struct origins;
struct type;
struct head;
struct auth;
struct power;
@ -190,6 +191,7 @@ struct ircd::m::room
#include "state_history.h"
#include "members.h"
#include "origins.h"
#include "type.h"
#include "head.h"
#include "auth.h"
#include "power.h"

View file

@ -0,0 +1,49 @@
// The Construct
//
// Copyright (C) The Construct Developers, Authors & Contributors
// Copyright (C) 2016-2020 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_TYPE_H
/// Interface to all room events sorted by type. This is not the "room type"
/// or m::type(room) classification string. This is an interface to the
/// _room_type table allowing efficient iteration of events similar to
/// room::events (_room_events) for a single event type. Events are sorted
/// by descending depth and event_idx within each type (similar to
/// room::events).
///
struct ircd::m::room::type
{
using closure = std::function<bool (const string_view &, const uint64_t &, const event::idx &)>;
m::room room;
string_view _type;
std::pair<uint64_t, int64_t> range; // highest (inclusive) to lowest (exclusive)
public:
bool for_each(const closure &) const;
size_t count() const;
bool empty() const;
type(const m::room &,
const string_view &type = {},
const decltype(range) & = { -1UL, -1L });
static bool prefetch(const room::id &, const string_view &type, const int64_t &depth);
static bool prefetch(const room::id &, const string_view &type);
};
inline
ircd::m::room::type::type(const m::room &room,
const string_view &type,
const decltype(range) &range)
:room{room}
,_type{type}
,range{range}
{}

View file

@ -83,6 +83,7 @@ libircd_matrix_la_SOURCES += room_leave.cc
libircd_matrix_la_SOURCES += room_visible.cc
libircd_matrix_la_SOURCES += room_members.cc
libircd_matrix_la_SOURCES += room_origins.cc
libircd_matrix_la_SOURCES += room_type.cc
libircd_matrix_la_SOURCES += room_power.cc
libircd_matrix_la_SOURCES += room_state.cc
libircd_matrix_la_SOURCES += room_state_history.cc

91
matrix/room_type.cc Normal file
View file

@ -0,0 +1,91 @@
// The Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2020 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.
bool
ircd::m::room::type::prefetch(const room::id &room_id,
const string_view &type)
{
return prefetch(room_id, type, -1L);
}
bool
ircd::m::room::type::prefetch(const room::id &room_id,
const string_view &type,
const int64_t &depth)
{
char buf[dbs::ROOM_TYPE_KEY_MAX_SIZE];
const string_view &key
{
dbs::room_type_key(buf, room_id, type, depth, -1L)
};
return db::prefetch(dbs::room_type, key);
}
bool
ircd::m::room::type::empty()
const
{
return for_each([]
(const auto &type, const auto &depth, const auto &event_idx)
{
return false;
});
}
size_t
ircd::m::room::type::count()
const
{
size_t ret(0);
for_each([&ret]
(const auto &type, const auto &depth, const auto &event_idx)
{
++ret;
return true;
});
return ret;
}
bool
ircd::m::room::type::for_each(const closure &closure)
const
{
char buf[dbs::ROOM_TYPE_KEY_MAX_SIZE];
const string_view &key
{
dbs::room_type_key(buf, room.room_id, _type, range.first, -1UL)
};
auto it
{
dbs::room_type.begin(key)
};
for(; it; ++it)
{
const auto &[_type, depth, event_idx]
{
dbs::room_type_key(it->first)
};
if(this->_type && this->_type != _type)
break;
if(int64_t(depth) <= range.second)
break;
if(!closure(_type, depth, event_idx))
return false;
}
return true;
}