2019-09-27 03:36:03 +02: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.
|
|
|
|
|
|
|
|
//
|
|
|
|
// seek
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::seek(event::fetch &fetch,
|
|
|
|
const event::id &event_id)
|
|
|
|
{
|
2020-04-03 04:50:00 +02:00
|
|
|
if(!seek(std::nothrow, fetch, event_id))
|
2019-09-27 03:36:03 +02:00
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
2020-04-03 04:50:00 +02:00
|
|
|
"%s not found in database",
|
|
|
|
event_id
|
2019-09-27 03:36:03 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-03 04:50:00 +02:00
|
|
|
ircd::m::seek(std::nothrow_t,
|
|
|
|
event::fetch &fetch,
|
|
|
|
const event::id &event_id)
|
2019-09-27 03:36:03 +02:00
|
|
|
{
|
|
|
|
const auto &event_idx
|
|
|
|
{
|
2020-04-02 01:12:41 +02:00
|
|
|
index(std::nothrow, event_id)
|
2019-09-27 03:36:03 +02:00
|
|
|
};
|
|
|
|
|
2020-04-03 04:50:00 +02:00
|
|
|
return seek(std::nothrow, fetch, event_idx, event_id);
|
2019-09-27 03:36:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::seek(event::fetch &fetch,
|
|
|
|
const event::idx &event_idx)
|
|
|
|
{
|
2020-04-03 04:50:00 +02:00
|
|
|
if(!seek(std::nothrow, fetch, event_idx))
|
2019-09-27 03:36:03 +02:00
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
2020-04-03 04:50:00 +02:00
|
|
|
"%lu not found in database",
|
|
|
|
event_idx
|
2019-09-27 03:36:03 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-03 04:50:00 +02:00
|
|
|
ircd::m::seek(std::nothrow_t,
|
|
|
|
event::fetch &fetch,
|
|
|
|
const event::idx &event_idx)
|
2019-09-27 03:36:03 +02:00
|
|
|
{
|
2020-04-03 04:50:00 +02:00
|
|
|
return seek(std::nothrow, fetch, event_idx, m::event::id{});
|
2019-09-27 03:36:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-03 04:50:00 +02:00
|
|
|
ircd::m::seek(std::nothrow_t,
|
|
|
|
event::fetch &fetch,
|
2019-09-27 03:36:03 +02:00
|
|
|
const event::idx &event_idx,
|
2020-04-03 04:50:00 +02:00
|
|
|
const event::id &event_id)
|
2019-09-27 03:36:03 +02:00
|
|
|
{
|
|
|
|
fetch.event_idx = event_idx;
|
|
|
|
fetch.event_id_buf = event_id?
|
|
|
|
event::id::buf{event_id}:
|
|
|
|
event::id::buf{};
|
|
|
|
|
|
|
|
if(!event_idx)
|
|
|
|
{
|
|
|
|
fetch.valid = false;
|
|
|
|
return fetch.valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
const string_view &key
|
|
|
|
{
|
|
|
|
byte_view<string_view>(event_idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &event
|
|
|
|
{
|
|
|
|
static_cast<m::event &>(fetch)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(fetch.fopts);
|
|
|
|
const auto &opts(*fetch.fopts);
|
|
|
|
if(!fetch.should_seek_json(opts))
|
|
|
|
if((fetch.valid = db::seek(fetch.row, key, opts.gopts)))
|
|
|
|
if((fetch.valid = fetch.assign_from_row(key)))
|
|
|
|
return fetch.valid;
|
|
|
|
|
|
|
|
if((fetch.valid = fetch._json.load(key, opts.gopts)))
|
|
|
|
fetch.valid = fetch.assign_from_json(key);
|
|
|
|
|
|
|
|
return fetch.valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// event::fetch
|
|
|
|
//
|
|
|
|
|
|
|
|
decltype(ircd::m::event::fetch::default_opts)
|
|
|
|
ircd::m::event::fetch::default_opts
|
|
|
|
{};
|
|
|
|
|
|
|
|
//
|
|
|
|
// event::fetch::fetch
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Seek to event_id and populate this event from database.
|
|
|
|
/// Throws if event not in database.
|
|
|
|
ircd::m::event::fetch::fetch(const event::id &event_id,
|
|
|
|
const opts &opts)
|
|
|
|
:fetch
|
|
|
|
{
|
2020-04-03 04:50:00 +02:00
|
|
|
std::nothrow,
|
|
|
|
index(event_id),
|
|
|
|
event_id,
|
|
|
|
opts,
|
2019-09-27 03:36:03 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
if(!valid)
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
2020-04-03 04:50:00 +02:00
|
|
|
"%s not found in database",
|
|
|
|
string_view{event_id}
|
2019-09-27 03:36:03 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Seek to event_id and populate this event from database.
|
|
|
|
/// Event is not populated if not found in database.
|
2020-04-03 04:50:00 +02:00
|
|
|
ircd::m::event::fetch::fetch(std::nothrow_t,
|
|
|
|
const event::id &event_id,
|
2019-09-27 03:36:03 +02:00
|
|
|
const opts &opts)
|
|
|
|
:fetch
|
|
|
|
{
|
2020-04-03 04:50:00 +02:00
|
|
|
std::nothrow,
|
|
|
|
index(std::nothrow, event_id),
|
|
|
|
event_id,
|
|
|
|
opts,
|
2019-09-27 03:36:03 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Seek to event_idx and populate this event from database.
|
|
|
|
/// Throws if event not in database.
|
|
|
|
ircd::m::event::fetch::fetch(const event::idx &event_idx,
|
|
|
|
const opts &opts)
|
|
|
|
:fetch
|
|
|
|
{
|
2020-04-03 04:50:00 +02:00
|
|
|
std::nothrow,
|
|
|
|
event_idx,
|
|
|
|
opts,
|
2019-09-27 03:36:03 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
if(!valid)
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
2020-04-03 04:50:00 +02:00
|
|
|
"idx %zu not found in database",
|
|
|
|
event_idx
|
2019-09-27 03:36:03 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-03 04:50:00 +02:00
|
|
|
ircd::m::event::fetch::fetch(std::nothrow_t,
|
|
|
|
const event::idx &event_idx,
|
2019-09-27 03:36:03 +02:00
|
|
|
const opts &opts)
|
|
|
|
:fetch
|
|
|
|
{
|
2020-04-03 04:50:00 +02:00
|
|
|
std::nothrow,
|
|
|
|
event_idx,
|
|
|
|
m::event::id{},
|
|
|
|
opts,
|
2019-09-27 03:36:03 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Seek to event_idx and populate this event from database.
|
|
|
|
/// Event is not populated if not found in database.
|
2020-04-03 04:50:00 +02:00
|
|
|
ircd::m::event::fetch::fetch(std::nothrow_t,
|
|
|
|
const event::idx &event_idx,
|
2019-09-27 03:36:03 +02:00
|
|
|
const event::id &event_id,
|
|
|
|
const opts &opts)
|
|
|
|
:fopts
|
|
|
|
{
|
|
|
|
&opts
|
|
|
|
}
|
|
|
|
,event_idx
|
|
|
|
{
|
|
|
|
event_idx
|
|
|
|
}
|
|
|
|
,_json
|
|
|
|
{
|
|
|
|
dbs::event_json,
|
|
|
|
event_idx && should_seek_json(opts)?
|
|
|
|
key(&event_idx):
|
|
|
|
string_view{},
|
|
|
|
opts.gopts
|
|
|
|
}
|
|
|
|
,row
|
|
|
|
{
|
|
|
|
*dbs::events,
|
|
|
|
event_idx && !_json.valid(key(&event_idx))?
|
|
|
|
key(&event_idx):
|
|
|
|
string_view{},
|
|
|
|
event_idx && !_json.valid(key(&event_idx))?
|
|
|
|
event::keys{opts.keys}:
|
|
|
|
event::keys{event::keys::include{}},
|
|
|
|
cell,
|
|
|
|
opts.gopts
|
|
|
|
}
|
|
|
|
,valid
|
|
|
|
{
|
|
|
|
false
|
|
|
|
}
|
|
|
|
,event_id_buf
|
|
|
|
{
|
|
|
|
event_id?
|
|
|
|
event::id::buf{event_id}:
|
|
|
|
event::id::buf{}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
valid =
|
|
|
|
event_idx && _json.valid(key(&event_idx))?
|
|
|
|
assign_from_json(key(&event_idx)):
|
|
|
|
event_idx?
|
|
|
|
assign_from_row(key(&event_idx)):
|
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Seekless constructor.
|
|
|
|
ircd::m::event::fetch::fetch(const opts &opts)
|
|
|
|
:fopts
|
|
|
|
{
|
|
|
|
&opts
|
|
|
|
}
|
|
|
|
,_json
|
|
|
|
{
|
|
|
|
dbs::event_json,
|
|
|
|
string_view{},
|
|
|
|
opts.gopts
|
|
|
|
}
|
|
|
|
,row
|
|
|
|
{
|
|
|
|
*dbs::events,
|
|
|
|
string_view{},
|
|
|
|
!should_seek_json(opts)?
|
|
|
|
event::keys{opts.keys}:
|
|
|
|
event::keys{event::keys::include{}},
|
|
|
|
cell,
|
|
|
|
opts.gopts
|
|
|
|
}
|
|
|
|
,valid
|
|
|
|
{
|
|
|
|
false
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-19 00:01:38 +02:00
|
|
|
// array of db::cell::~cell()
|
|
|
|
ircd::m::event::fetch::~fetch()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-02 18:23:18 +02:00
|
|
|
[[gnu::visibility("hidden")]]
|
2019-09-27 03:36:03 +02:00
|
|
|
bool
|
|
|
|
ircd::m::event::fetch::assign_from_json(const string_view &key)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &event
|
|
|
|
{
|
|
|
|
static_cast<m::event &>(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(_json.valid(key));
|
|
|
|
const json::object source
|
|
|
|
{
|
|
|
|
_json.val()
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(!empty(source));
|
|
|
|
const bool source_event_id
|
|
|
|
{
|
|
|
|
!event_id_buf && source.has("event_id")
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto event_id
|
|
|
|
{
|
|
|
|
source_event_id?
|
|
|
|
id(json::string(source.at("event_id"))):
|
|
|
|
event_id_buf?
|
|
|
|
id(event_id_buf):
|
2020-04-02 00:42:33 +02:00
|
|
|
m::event_id(std::nothrow, event_idx, event_id_buf)
|
2019-09-27 03:36:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(fopts);
|
|
|
|
assert(event_id);
|
|
|
|
event =
|
|
|
|
{
|
|
|
|
source, event_id, event::keys{fopts->keys}
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(data(event.source) == data(source));
|
|
|
|
assert(event.event_id == event_id);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const json::parse_error &e)
|
|
|
|
{
|
|
|
|
const ctx::exception_handler eh;
|
|
|
|
|
|
|
|
const auto event_id
|
|
|
|
{
|
|
|
|
event_id_buf?
|
|
|
|
id(event_id_buf):
|
2020-04-02 00:42:33 +02:00
|
|
|
m::event_id(std::nothrow, event_idx, event_id_buf)
|
2019-09-27 03:36:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
m::log, "Fetching event:%lu %s JSON from local database :%s",
|
|
|
|
event_idx,
|
|
|
|
string_view{event_id},
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-02 18:23:18 +02:00
|
|
|
[[gnu::visibility("hidden")]]
|
2019-09-27 03:36:03 +02:00
|
|
|
bool
|
|
|
|
ircd::m::event::fetch::assign_from_row(const string_view &key)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &event
|
|
|
|
{
|
|
|
|
static_cast<m::event &>(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!row.valid(key))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
event.source = {};
|
|
|
|
assign(event, row, key);
|
2020-06-09 03:59:33 +02:00
|
|
|
|
|
|
|
// N.B. a row assignment might not produce an event.event_id unless
|
|
|
|
// the key is explicitly selected or it was otherwise trivially found.
|
|
|
|
event.event_id =
|
2019-09-27 03:36:03 +02:00
|
|
|
{
|
2020-06-09 03:59:33 +02:00
|
|
|
event.event_id?
|
|
|
|
event.event_id:
|
|
|
|
|
2019-09-27 03:36:03 +02:00
|
|
|
!empty(json::get<"event_id"_>(event))?
|
2020-06-09 03:59:33 +02:00
|
|
|
event::id{json::get<"event_id"_>(event)}:
|
|
|
|
|
2019-09-27 03:36:03 +02:00
|
|
|
event_id_buf?
|
2020-06-09 03:59:33 +02:00
|
|
|
event::id{event_id_buf}:
|
|
|
|
|
|
|
|
cell.at(json::indexof<m::event, "event_id"_>())?
|
|
|
|
event::id{cell.at(json::indexof<m::event, "event_id"_>()).val()}:
|
|
|
|
|
|
|
|
event::id{}
|
2019-09-27 03:36:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const json::parse_error &e)
|
|
|
|
{
|
|
|
|
const ctx::exception_handler eh;
|
|
|
|
|
|
|
|
const auto event_id
|
|
|
|
{
|
|
|
|
event_id_buf?
|
|
|
|
id(event_id_buf):
|
2020-04-02 00:42:33 +02:00
|
|
|
m::event_id(std::nothrow, event_idx, event_id_buf)
|
2019-09-27 03:36:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
m::log, "Fetching event:%lu %s JSON from local database :%s",
|
|
|
|
event_idx,
|
|
|
|
string_view{event_id},
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-02 18:23:18 +02:00
|
|
|
[[gnu::visibility("hidden")]]
|
2019-09-27 03:36:03 +02:00
|
|
|
bool
|
|
|
|
ircd::m::event::fetch::should_seek_json(const opts &opts)
|
|
|
|
{
|
|
|
|
// User always wants to make the event_json query regardless
|
|
|
|
// of their keys selection.
|
|
|
|
if(opts.query_json_force)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If and only if selected keys have direct columns we can return
|
|
|
|
// false to seek direct columns. If any other keys are selected we
|
|
|
|
/// must perform the event_json query instead.
|
|
|
|
for(size_t i(0); i < opts.keys.size(); ++i)
|
|
|
|
if(opts.keys.test(i))
|
|
|
|
if(!dbs::event_column.at(i))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-02 18:23:18 +02:00
|
|
|
[[gnu::visibility("hidden")]]
|
2019-09-27 03:36:03 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::event::fetch::key(const event::idx *const &event_idx)
|
|
|
|
{
|
|
|
|
assert(event_idx);
|
|
|
|
return byte_view<string_view>(*event_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// event::fetch::opts
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::event::fetch::opts::opts(const db::gopts &gopts,
|
|
|
|
const event::keys::selection &keys)
|
|
|
|
:opts
|
|
|
|
{
|
|
|
|
keys, gopts
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::fetch::opts::opts(const event::keys::selection &keys,
|
|
|
|
const db::gopts &gopts)
|
|
|
|
:keys{keys}
|
|
|
|
,gopts{gopts}
|
|
|
|
{
|
|
|
|
}
|
2019-12-03 21:09:25 +01:00
|
|
|
|
|
|
|
ircd::m::event::fetch::opts::opts()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|