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

ircd:Ⓜ️:event: Add topological before; improve prev iteration closure; reorg some util.

This commit is contained in:
Jason Volk 2018-06-09 14:32:46 -07:00
parent 3da5afdb85
commit 0da4dec13c
2 changed files with 86 additions and 46 deletions

View file

@ -28,14 +28,17 @@ namespace ircd::m
bool bad(const id::event &, uint64_t &); bool bad(const id::event &, uint64_t &);
bool bad(const id::event &); bool bad(const id::event &);
// Equality tests the event_id only! know this.
bool operator==(const event &a, const event &b);
// Depth comparison; expect unstable sorting. // Depth comparison; expect unstable sorting.
bool operator<(const event &, const event &); bool operator<(const event &, const event &);
bool operator>(const event &, const event &); bool operator>(const event &, const event &);
bool operator<=(const event &, const event &); bool operator<=(const event &, const event &);
bool operator>=(const event &, const event &); bool operator>=(const event &, const event &);
// Equality tests the event_id only! know this. // Topological
bool operator==(const event &a, const event &b); bool before(const event &a, const event &b); // A directly referenced by B
id::event make_id(const event &, id::event::buf &buf, const const_buffer &hash); id::event make_id(const event &, id::event::buf &buf, const const_buffer &hash);
id::event make_id(const event &, id::event::buf &buf); id::event make_id(const event &, id::event::buf &buf);
@ -142,7 +145,8 @@ struct ircd::m::event
namespace ircd::m namespace ircd::m
{ {
void for_each(const event::prev &, const std::function<void (const event::id &)> &); bool for_each(const event::prev &, const event::id::closure_bool &);
void for_each(const event::prev &, const event::id::closure &);
size_t degree(const event::prev &); size_t degree(const event::prev &);
size_t count(const event::prev &); size_t count(const event::prev &);

View file

@ -255,10 +255,27 @@ ircd::m::make_id(const event &event,
} }
bool bool
ircd::m::operator==(const event &a, const event &b) ircd::m::before(const event &a,
const event &b)
{ {
assert(json::get<"room_id"_>(a) == json::get<"room_id"_>(b)); const event::prev prev_b{b};
return at<"event_id"_>(a) == at<"event_id"_>(b); const json::array &prev_b_events
{
json::get<"prev_events"_>(prev_b)
};
for(const json::array &prev_event : prev_b_events)
{
const event::id prev_event_id
{
unquote(prev_event.at(0))
};
if(prev_event_id == at<"event_id"_>(a))
return true;
}
return false;
} }
bool bool
@ -289,6 +306,13 @@ ircd::m::operator<(const event &a, const event &b)
return at<"depth"_>(a) < at<"depth"_>(b); return at<"depth"_>(a) < at<"depth"_>(b);
} }
bool
ircd::m::operator==(const event &a, const event &b)
{
assert(json::get<"room_id"_>(a) == json::get<"room_id"_>(b));
return at<"event_id"_>(a) == at<"event_id"_>(b);
}
bool bool
ircd::m::bad(const id::event &event_id) ircd::m::bad(const id::event &event_id)
{ {
@ -378,46 +402,6 @@ ircd::m::degree(const event &event)
return degree(event::prev{event}); return degree(event::prev{event});
} }
size_t
ircd::m::degree(const event::prev &prev)
{
size_t ret{0};
json::for_each(prev, [&ret]
(const auto &, const json::array &prevs)
{
ret += prevs.count();
});
return ret;
}
size_t
ircd::m::count(const event::prev &prev)
{
size_t ret{0};
m::for_each(prev, [&ret](const event::id &event_id)
{
++ret;
});
return ret;
}
void
ircd::m::for_each(const event::prev &prev,
const std::function<void (const event::id &)> &closure)
{
json::for_each(prev, [&closure]
(const auto &key, const json::array &prevs)
{
for(const json::array &prev : prevs)
{
const event::id &id{unquote(prev[0])};
closure(id);
}
});
}
std::string std::string
ircd::m::pretty(const event::prev &prev) ircd::m::pretty(const event::prev &prev)
{ {
@ -518,6 +502,58 @@ ircd::m::pretty_oneline(std::ostream &s,
return s; return s;
} }
size_t
ircd::m::degree(const event::prev &prev)
{
size_t ret{0};
json::for_each(prev, [&ret]
(const auto &, const json::array &prevs)
{
ret += prevs.count();
});
return ret;
}
size_t
ircd::m::count(const event::prev &prev)
{
size_t ret{0};
m::for_each(prev, [&ret](const event::id &event_id)
{
++ret;
});
return ret;
}
void
ircd::m::for_each(const event::prev &prev,
const event::id::closure &closure)
{
m::for_each(prev, event::id::closure_bool{[&closure]
(const event::id &event_id)
{
closure(event_id);
return true;
}});
}
bool
ircd::m::for_each(const event::prev &prev,
const event::id::closure_bool &closure)
{
return json::until(prev, [&closure]
(const auto &key, const json::array &prevs)
{
for(const json::array &prev : prevs)
if(!closure(event::id(unquote(prev.at(0)))))
return false;
return true;
});
}
bool bool
ircd::m::my(const event &event) ircd::m::my(const event &event)
{ {