From 0e58aceee97d449648fff1072ba697d1afc942bc Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 29 Dec 2018 17:33:51 -0800 Subject: [PATCH] modules/media: Optimize various event fetches. --- modules/media/download.cc | 26 +++++++++++++++++++------- modules/media/media.cc | 18 +++++++++++++++--- modules/media/thumbnail.cc | 14 ++++++++++++-- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/modules/media/download.cc b/modules/media/download.cc index 286040583..1804548f0 100644 --- a/modules/media/download.cc +++ b/modules/media/download.cc @@ -81,9 +81,19 @@ get__download_local(client &client, const string_view &file, const m::room &room) { + static const m::event::fetch::opts fopts + { + m::event::keys::include {"content"} + }; + + const m::room::state state + { + room, &fopts + }; + // Get the file's total size size_t file_size{0}; - room.get("ircd.file.stat", "size", [&file_size] + state.get("ircd.file.stat", "size", [&file_size] (const m::event &event) { file_size = at<"content"_>(event).get("value"); @@ -96,7 +106,7 @@ get__download_local(client &client, "application/octet-stream" }; - room.get("ircd.file.stat", "type", [&type_buf, &content_type] + state.get("ircd.file.stat", "type", [&type_buf, &content_type] (const m::event &event) { const auto &value @@ -116,12 +126,14 @@ get__download_local(client &client, client, http::OK, content_type, file_size }; - size_t sent{0}, read; - read = read_each_block(room, [&client, &sent] - (const string_view &block) + size_t sent{0}, read { - sent += write_all(*client.sock, block); - }); + read_each_block(room, [&client, &sent] + (const string_view &block) + { + sent += write_all(*client.sock, block); + }) + }; if(unlikely(read != file_size)) log::error { diff --git a/modules/media/media.cc b/modules/media/media.cc index 8caee371e..eee87a3df 100644 --- a/modules/media/media.cc +++ b/modules/media/media.cc @@ -371,14 +371,26 @@ size_t read_each_block(const m::room &room, const std::function &closure) { + static const m::event::fetch::opts fopts + { + m::event::keys::include + { + "content", "type" + } + }; + + size_t ret{0}; + m::room::messages it + { + room, 1, &fopts + }; + // Block buffer const unique_buffer buf { 64_KiB }; - size_t ret{0}; - m::room::messages it{room, 1}; for(; bool(it); ++it) { const m::event &event{*it}; @@ -404,7 +416,7 @@ read_each_block(const m::room &room, { "File [%s] block [%s] (%s) blksz %zu != %zu", string_view{room.room_id}, - string_view{at<"event_id"_>(event)}, + string_view{m::get(std::nothrow, it.event_idx(), "event_id", buf)}, hash, blksz, size(block) diff --git a/modules/media/thumbnail.cc b/modules/media/thumbnail.cc index b47661e4c..0f70192ef 100644 --- a/modules/media/thumbnail.cc +++ b/modules/media/thumbnail.cc @@ -105,9 +105,19 @@ get__thumbnail_local(client &client, const string_view &mediaid, const m::room &room) { + static const m::event::fetch::opts fopts + { + m::event::keys::include {"content"} + }; + + const m::room::state state + { + room, &fopts + }; + // Get the file's total size size_t file_size{0}; - room.get("ircd.file.stat", "size", [&file_size] + state.get("ircd.file.stat", "size", [&file_size] (const m::event &event) { file_size = at<"content"_>(event).get("value"); @@ -120,7 +130,7 @@ get__thumbnail_local(client &client, "application/octet-stream" }; - room.get("ircd.file.stat", "type", [&type_buf, &content_type] + state.get("ircd.file.stat", "type", [&type_buf, &content_type] (const m::event &event) { const auto &value