knowledge-search

git clone git://archive.git.mtrnord.blog/MTRNord/knowledge-search.git
Log | Files | Refs | README | LICENSE

commit 089a5b99a29d7aeca51ebba9cc8a484a6d021afb
parent c9a97cd6632d2207a83272c32e6713d3cfeedc9f
Author: MTRNord <mtrnord1@gmail.com>
Date:   Thu, 23 Mar 2023 19:31:05 +0100

refactor: Apply more clippy suggestions

Diffstat:
Mcrates/matrix-indexer/src/indradb_utils.rs | 27+++++++++++++++------------
Mcrates/matrix-indexer/src/main.rs | 2+-
Mcrates/matrix-indexer/src/matrix.rs | 21+++++++++++----------
3 files changed, 27 insertions(+), 23 deletions(-)

diff --git a/crates/matrix-indexer/src/indradb_utils.rs b/crates/matrix-indexer/src/indradb_utils.rs @@ -1,5 +1,5 @@ use std::{ - collections::{HashMap, VecDeque}, + collections::{BTreeMap, VecDeque}, mem::replace, }; @@ -21,7 +21,7 @@ lazy_static! { pub struct BulkInserter { requests: async_channel::Sender<Vec<indradb::BulkInsertItem>>, - workers: Vec<JoinHandle<()>>, + workers: Vec<JoinHandle<Result<()>>>, buf: Vec<indradb::BulkInsertItem>, client: proto::Client, } @@ -36,8 +36,9 @@ impl BulkInserter { let mut client = client.clone(); workers.push(tokio::spawn(async move { while let Ok(buf) = rx.recv().await { - client.bulk_insert(buf).await.unwrap(); + client.bulk_insert(buf).await?; } + Ok(()) })); } @@ -54,23 +55,25 @@ impl BulkInserter { Ok(()) } - pub async fn flush(mut self) { + pub async fn flush(mut self) -> Result<()> { if !self.buf.is_empty() { - self.requests.send(self.buf).await.unwrap(); + self.requests.send(self.buf).await?; } self.requests.close(); for worker in self.workers { - worker.await.unwrap(); + worker.await??; } - self.client.sync().await.unwrap(); + self.client.sync().await?; + Ok(()) } - pub async fn push(&mut self, item: indradb::BulkInsertItem) { + pub async fn push(&mut self, item: indradb::BulkInsertItem) -> Result<()> { self.buf.push(item); if self.buf.len() >= REQUEST_BUFFER_SIZE { let buf = replace(&mut self.buf, Vec::with_capacity(REQUEST_BUFFER_SIZE)); - self.requests.send(buf).await.unwrap(); + self.requests.send(buf).await?; } + Ok(()) } } @@ -106,11 +109,11 @@ pub type EventUuid = Uuid; // TODO: Track all the properties! #[derive(Default)] pub struct MessagesMap { - event_uuids: HashMap<OwnedEventId, EventUuid>, + event_uuids: BTreeMap<OwnedEventId, EventUuid>, pub message_list: VecDeque<UUIDEventMapType>, - room_uuids: HashMap<OwnedRoomId, RoomUuid>, + room_uuids: BTreeMap<OwnedRoomId, RoomUuid>, pub room_list: VecDeque<UUIDRoomMapType>, - pub room_event_links: HashMap<EventUuid, RoomUuid>, + pub room_event_links: BTreeMap<EventUuid, RoomUuid>, } impl EventProperties { diff --git a/crates/matrix-indexer/src/main.rs b/crates/matrix-indexer/src/main.rs @@ -45,7 +45,7 @@ async fn main() -> Result<()> { // TODO: config which rewrites itself to have the data after login let mut bot = IndexerBot::new(String::new(), String::new(), String::new()).await?; - bot.start_processing().await; + bot.start_processing().await?; Ok(()) } diff --git a/crates/matrix-indexer/src/matrix.rs b/crates/matrix-indexer/src/matrix.rs @@ -149,7 +149,7 @@ impl IndexerBot { // FIXME:_split into multiple functions #[allow(clippy::too_many_lines)] - pub async fn start_processing(&mut self) { + pub async fn start_processing(&mut self) -> Result<()> { let mut inserter = BulkInserter::new(self.indexer_client.clone()); let mut sync_stream = Box::pin(self.client.sync_stream(SyncSettings::default()).await); @@ -236,14 +236,14 @@ impl IndexerBot { .push(utils::indradb::BulkInsertItem::Vertex( utils::indradb::Vertex::with_id(*uuid, self.identifiers.room_type), )) - .await; + .await?; inserter .push(utils::indradb::BulkInsertItem::VertexProperty( *uuid, self.identifiers.room_id_type, serde_json::Value::String(room_id.to_string()).into(), )) - .await; + .await?; if let Some(room_name) = &room_properties.name { inserter .push(utils::indradb::BulkInsertItem::VertexProperty( @@ -251,7 +251,7 @@ impl IndexerBot { self.identifiers.room_name_type, serde_json::Value::String(room_name.to_string()).into(), )) - .await; + .await?; } if let Some(room_topic) = &room_properties.topic { inserter @@ -260,7 +260,7 @@ impl IndexerBot { self.identifiers.room_topic_type, serde_json::Value::String(room_topic.to_string()).into(), )) - .await; + .await?; } } for UUIDEventMapType { @@ -274,19 +274,19 @@ impl IndexerBot { .push(utils::indradb::BulkInsertItem::Vertex( utils::indradb::Vertex::with_id(*uuid, *event_type), )) - .await; + .await?; inserter .push(utils::indradb::BulkInsertItem::VertexProperty( *uuid, self.identifiers.event_id_type, serde_json::Value::String(event_id.to_string()).into(), )) - .await; + .await?; for event_property in event_properties .as_vec(*uuid) .expect("Unable to convert to indradb properties") { - inserter.push(event_property).await; + inserter.push(event_property).await?; } } @@ -299,9 +299,10 @@ impl IndexerBot { *room_uuid, ), )) - .await; + .await?; } - inserter.sync().await.expect("Unable to sync indradb"); + inserter.sync().await?; } + Ok(()) } }