0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 18:22:50 +01:00

ircd:Ⓜ️:event::fetch: Add options to seek(); add event_json query branch.

This commit is contained in:
Jason Volk 2019-01-15 14:46:35 -08:00
parent 15c13d5c9b
commit c5489cecb0
2 changed files with 106 additions and 37 deletions

View file

@ -11,15 +11,6 @@
#pragma once #pragma once
#define HAVE_IRCD_M_EVENT_FETCH_H #define HAVE_IRCD_M_EVENT_FETCH_H
namespace ircd::m
{
bool seek(event::fetch &, const event::idx &, std::nothrow_t);
void seek(event::fetch &, const event::idx &);
bool seek(event::fetch &, const event::id &, std::nothrow_t);
void seek(event::fetch &, const event::id &);
}
struct ircd::m::event::fetch struct ircd::m::event::fetch
:event :event
{ {
@ -31,6 +22,7 @@ struct ircd::m::event::fetch
static const opts default_opts; static const opts default_opts;
std::array<db::cell, event::size()> cell; std::array<db::cell, event::size()> cell;
db::cell _json;
db::row row; db::row row;
bool valid; bool valid;
@ -45,11 +37,34 @@ struct ircd::m::event::fetch
static void event_id(const idx &, const id::closure &); static void event_id(const idx &, const id::closure &);
}; };
namespace ircd::m
{
bool seek(event::fetch &, const event::idx &, std::nothrow_t, const event::fetch::opts *const & = nullptr);
void seek(event::fetch &, const event::idx &, const event::fetch::opts *const & = nullptr);
bool seek(event::fetch &, const event::id &, std::nothrow_t, const event::fetch::opts *const & = nullptr);
void seek(event::fetch &, const event::id &, const event::fetch::opts *const & = nullptr);
}
struct ircd::m::event::fetch::opts struct ircd::m::event::fetch::opts
{ {
/// Event property selector
event::keys keys; event::keys keys;
/// Database get options passthru
db::gopts gopts; db::gopts gopts;
/// Whether to allowing querying the event_json to populate the event if
/// it would be more efficient based on the keys being sought. Ex. If all
/// keys are being sought then we can make a single query to event_json
/// rather than a concurrent row query. This is enabled by default.
bool query_json_maybe {true};
/// Whether to force only querying for event_json to populate the event,
/// regardless of what keys are sought in the property selector. This is
/// not enabled by default.
bool query_json_only {false};
opts(const event::keys &, const db::gopts & = {}); opts(const event::keys &, const db::gopts & = {});
opts(const event::keys::selection &, const db::gopts & = {}); opts(const event::keys::selection &, const db::gopts & = {});
opts(const db::gopts &, const event::keys::selection & = {}); opts(const db::gopts &, const event::keys::selection & = {});

View file

@ -1384,6 +1384,11 @@ ircd::m::get(std::nothrow_t,
const string_view &key, const string_view &key,
const event::fetch::view_closure &closure) const event::fetch::view_closure &closure)
{ {
const string_view &column_key
{
byte_view<string_view>{event_idx}
};
const auto &column_idx const auto &column_idx
{ {
json::indexof<event>(key) json::indexof<event>(key)
@ -1394,14 +1399,15 @@ ircd::m::get(std::nothrow_t,
dbs::event_column.at(column_idx) dbs::event_column.at(column_idx)
}; };
return column(byte_view<string_view>{event_idx}, std::nothrow, closure); return column(column_key, std::nothrow, closure);
} }
void void
ircd::m::seek(event::fetch &fetch, ircd::m::seek(event::fetch &fetch,
const event::id &event_id) const event::id &event_id,
const event::fetch::opts *const &opts)
{ {
if(!seek(fetch, event_id, std::nothrow)) if(!seek(fetch, event_id, std::nothrow, opts))
throw m::NOT_FOUND throw m::NOT_FOUND
{ {
"%s not found in database", event_id "%s not found in database", event_id
@ -1411,21 +1417,25 @@ ircd::m::seek(event::fetch &fetch,
bool bool
ircd::m::seek(event::fetch &fetch, ircd::m::seek(event::fetch &fetch,
const event::id &event_id, const event::id &event_id,
std::nothrow_t) std::nothrow_t,
const event::fetch::opts *const &opts)
{ {
const auto &event_idx const auto &event_idx
{ {
index(event_id, std::nothrow) index(event_id, std::nothrow)
}; };
return seek(fetch, event_idx, std::nothrow); return event_idx?
seek(fetch, event_idx, std::nothrow, opts):
false;
} }
void void
ircd::m::seek(event::fetch &fetch, ircd::m::seek(event::fetch &fetch,
const event::idx &event_idx) const event::idx &event_idx,
const event::fetch::opts *const &opts)
{ {
if(!seek(fetch, event_idx, std::nothrow)) if(!seek(fetch, event_idx, std::nothrow, opts))
throw m::NOT_FOUND throw m::NOT_FOUND
{ {
"%lu not found in database", event_idx "%lu not found in database", event_idx
@ -1435,21 +1445,68 @@ ircd::m::seek(event::fetch &fetch,
bool bool
ircd::m::seek(event::fetch &fetch, ircd::m::seek(event::fetch &fetch,
const event::idx &event_idx, const event::idx &event_idx,
std::nothrow_t) std::nothrow_t,
const event::fetch::opts *const &opts)
{ {
auto &event
{
static_cast<m::event &>(fetch)
};
const string_view &key const string_view &key
{ {
byte_view<string_view>(event_idx) byte_view<string_view>(event_idx)
}; };
db::seek(fetch.row, key); const db::gopts &gopts
fetch.valid = fetch.row.valid(key); {
if(!fetch.valid) opts?
return false; opts->gopts:
db::gopts{}
};
auto &event{static_cast<m::event &>(fetch)}; const bool query_json
assign(event, fetch.row, key); {
return true; opts && opts->query_json_only?
true: // User only wants to make the event_json query
opts && !opts->query_json_maybe?
false: // User never wants to make the event_json query
fetch.row.size() < fetch.cell.size()?
false: // User is making specific column queries
fetch.row.size() == fetch.cell.size()?
true: // User is querying all columns
false
};
if(query_json)
{
if((fetch.valid = fetch._json.load(key, gopts)))
event = m::event
{
fetch._json.val()
};
if(fetch.valid)
return fetch.valid;
if(opts && opts->query_json_only)
return fetch.valid;
// graceful fallback to row query if json query failed.
assert(!fetch.valid);
}
if(!(fetch.valid = db::seek(fetch.row, key, gopts)))
return fetch.valid;
if((fetch.valid = fetch.row.valid(key)))
assign(event, fetch.row, key);
return fetch.valid;
} }
ircd::m::event::idx ircd::m::event::idx
@ -1554,7 +1611,13 @@ ircd::m::event::fetch::event_id(const idx &idx,
/// Seekless constructor. /// Seekless constructor.
ircd::m::event::fetch::fetch(const opts *const &opts) ircd::m::event::fetch::fetch(const opts *const &opts)
:row :_json
{
m::dbs::event_json,
string_view{},
opts? opts->gopts : default_opts.gopts
}
,row
{ {
*dbs::events, *dbs::events,
string_view{}, string_view{},
@ -1613,21 +1676,12 @@ ircd::m::event::fetch::fetch(const event::idx &event_idx,
ircd::m::event::fetch::fetch(const event::idx &event_idx, ircd::m::event::fetch::fetch(const event::idx &event_idx,
std::nothrow_t, std::nothrow_t,
const opts *const &opts) const opts *const &opts)
:row :fetch
{ {
*dbs::events, opts
byte_view<string_view>{event_idx},
opts? opts->keys : default_opts.keys,
cell,
opts? opts->gopts : default_opts.gopts
}
,valid
{
row.valid(byte_view<string_view>{event_idx})
} }
{ {
if(valid) seek(*this, event_idx, std::nothrow, opts);
assign(*this, row, byte_view<string_view>{event_idx});
} }
// //