1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-06-27 16:35:59 +00:00

refactor: all the clippy lints

This commit is contained in:
Matthias Ahouansou 2024-05-06 17:06:25 +01:00
parent 3ad7675bbf
commit 41e56baf60
No known key found for this signature in database
72 changed files with 1091 additions and 1152 deletions

View file

@ -32,7 +32,7 @@ fn db_options(max_open_files: i32, rocksdb_cache: &rocksdb::Cache) -> rocksdb::O
let mut db_opts = rocksdb::Options::default();
db_opts.set_block_based_table_factory(&block_based_options);
db_opts.create_if_missing(true);
db_opts.increase_parallelism(num_cpus::get() as i32);
db_opts.increase_parallelism(num_cpus::get().try_into().unwrap_or(i32::MAX));
db_opts.set_max_open_files(max_open_files);
db_opts.set_compression_type(rocksdb::DBCompressionType::Lz4);
db_opts.set_bottommost_compression_type(rocksdb::DBCompressionType::Zstd);
@ -41,7 +41,7 @@ fn db_options(max_open_files: i32, rocksdb_cache: &rocksdb::Cache) -> rocksdb::O
// https://github.com/facebook/rocksdb/wiki/Setup-Options-and-Basic-Tuning
db_opts.set_level_compaction_dynamic_level_bytes(true);
db_opts.set_max_background_jobs(6);
db_opts.set_bytes_per_sync(1048576);
db_opts.set_bytes_per_sync(1_048_576);
// https://github.com/facebook/rocksdb/issues/849
db_opts.set_keep_log_file_num(100);
@ -112,6 +112,7 @@ impl KeyValueDatabaseEngine for Arc<Engine> {
fn memory_usage(&self) -> Result<String> {
let stats =
rocksdb::perf::get_memory_usage_stats(Some(&[&self.rocks]), Some(&[&self.cache]))?;
#[allow(clippy::cast_precision_loss)]
Ok(format!(
"Approximate memory usage of all the mem-tables: {:.3} MB\n\
Approximate memory usage of un-flushed mem-tables: {:.3} MB\n\
@ -219,7 +220,7 @@ impl KvTree for RocksDbEngineTree<'_> {
let lock = self.write_lock.write().unwrap();
let old = self.db.rocks.get_cf_opt(&self.cf(), key, &readoptions)?;
let new = utils::increment(old.as_deref()).unwrap();
let new = utils::increment(old.as_deref());
self.db
.rocks
.put_cf_opt(&self.cf(), key, &new, &writeoptions)?;
@ -236,7 +237,7 @@ impl KvTree for RocksDbEngineTree<'_> {
for key in iter {
let old = self.db.rocks.get_cf_opt(&self.cf(), &key, &readoptions)?;
let new = utils::increment(old.as_deref()).unwrap();
let new = utils::increment(old.as_deref());
self.db
.rocks
.put_cf_opt(&self.cf(), key, new, &writeoptions)?;

View file

@ -88,6 +88,7 @@ impl KeyValueDatabaseEngine for Arc<Engine> {
// 1. convert MB to KiB
// 2. divide by permanent connections + permanent iter connections + write connection
// 3. round down to nearest integer
#[allow(clippy::cast_precision_loss)]
let cache_size_per_thread: u32 = ((config.db_cache_capacity_mb * 1024.0)
/ ((num_cpus::get().max(1) * 2) + 1) as f64)
as u32;
@ -217,8 +218,7 @@ impl KvTree for SqliteTable {
guard.execute("BEGIN", [])?;
for key in iter {
let old = self.get_with_guard(&guard, &key)?;
let new = crate::utils::increment(old.as_deref())
.expect("utils::increment always returns Some");
let new = crate::utils::increment(old.as_deref());
self.insert_with_guard(&guard, &key, &new)?;
}
guard.execute("COMMIT", [])?;
@ -308,8 +308,7 @@ impl KvTree for SqliteTable {
let old = self.get_with_guard(&guard, key)?;
let new =
crate::utils::increment(old.as_deref()).expect("utils::increment always returns Some");
let new = crate::utils::increment(old.as_deref());
self.insert_with_guard(&guard, key, &new)?;

View file

@ -20,7 +20,7 @@ impl service::account_data::Data for KeyValueDatabase {
data: &serde_json::Value,
) -> Result<()> {
let mut prefix = room_id
.map(|r| r.to_string())
.map(ToString::to_string)
.unwrap_or_default()
.as_bytes()
.to_vec();
@ -70,7 +70,7 @@ impl service::account_data::Data for KeyValueDatabase {
kind: RoomAccountDataEventType,
) -> Result<Option<Box<serde_json::value::RawValue>>> {
let mut key = room_id
.map(|r| r.to_string())
.map(ToString::to_string)
.unwrap_or_default()
.as_bytes()
.to_vec();
@ -105,7 +105,7 @@ impl service::account_data::Data for KeyValueDatabase {
let mut userdata = HashMap::new();
let mut prefix = room_id
.map(|r| r.to_string())
.map(ToString::to_string)
.unwrap_or_default()
.as_bytes()
.to_vec();

View file

@ -136,7 +136,7 @@ impl service::globals::Data for KeyValueDatabase {
}
fn cleanup(&self) -> Result<()> {
self._db.cleanup()
self.db.cleanup()
}
fn memory_usage(&self) -> String {
@ -160,7 +160,7 @@ our_real_users_cache: {our_real_users_cache}
appservice_in_room_cache: {appservice_in_room_cache}
lasttimelinecount_cache: {lasttimelinecount_cache}\n"
);
if let Ok(db_stats) = self._db.memory_usage() {
if let Ok(db_stats) = self.db.memory_usage() {
response += &db_stats;
}
@ -209,7 +209,7 @@ lasttimelinecount_cache: {lasttimelinecount_cache}\n"
self.global.insert(b"keypair", &keypair)?;
Ok::<_, Error>(keypair)
},
|s| Ok(s.to_vec()),
|s| Ok(s.clone()),
)?;
let mut parts = keypair_bytes.splitn(2, |&b| b == 0xff);
@ -285,7 +285,7 @@ lasttimelinecount_cache: {lasttimelinecount_cache}\n"
.server_signingkeys
.get(origin.as_bytes())?
.and_then(|bytes| serde_json::from_slice(&bytes).ok())
.map(|keys: ServerSigningKeys| {
.map_or_else(BTreeMap::new, |keys: ServerSigningKeys| {
let mut tree = keys.verify_keys;
tree.extend(
keys.old_verify_keys
@ -293,8 +293,7 @@ lasttimelinecount_cache: {lasttimelinecount_cache}\n"
.map(|old| (old.0, VerifyKey::new(old.1.key))),
);
tree
})
.unwrap_or_else(BTreeMap::new);
});
Ok(signingkeys)
}

View file

@ -22,10 +22,7 @@ impl service::pusher::Data for KeyValueDatabase {
let mut key = sender.as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(ids.pushkey.as_bytes());
self.senderkey_pusher
.remove(&key)
.map(|_| ())
.map_err(Into::into)
self.senderkey_pusher.remove(&key).map_err(Into::into)
}
}
}

View file

@ -15,7 +15,7 @@ impl service::rooms::alias::Data for KeyValueDatabase {
fn remove_alias(&self, alias: &RoomAliasId) -> Result<()> {
if let Some(room_id) = self.alias_roomid.get(alias.alias().as_bytes())? {
let mut prefix = room_id.to_vec();
let mut prefix = room_id.clone();
prefix.push(0xff);
for (key, _) in self.aliasid_alias.scan_prefix(prefix) {

View file

@ -54,12 +54,11 @@ impl service::rooms::search::Data for KeyValueDatabase {
.map(move |(key, _)| key[prefix3.len()..].to_vec())
});
let common_elements = match utils::common_elements(iterators, |a, b| {
let Some(common_elements) = utils::common_elements(iterators, |a, b| {
// We compare b with a because we reversed the iterator earlier
b.cmp(a)
}) {
Some(it) => it,
None => return Ok(None),
}) else {
return Ok(None);
};
Ok(Some((Box::new(common_elements), words)))

View file

@ -10,18 +10,18 @@ impl service::rooms::short::Data for KeyValueDatabase {
return Ok(*short);
}
let short = match self.eventid_shorteventid.get(event_id.as_bytes())? {
Some(shorteventid) => utils::u64_from_bytes(&shorteventid)
.map_err(|_| Error::bad_database("Invalid shorteventid in db."))?,
None => {
let short =
if let Some(shorteventid) = self.eventid_shorteventid.get(event_id.as_bytes())? {
utils::u64_from_bytes(&shorteventid)
.map_err(|_| Error::bad_database("Invalid shorteventid in db."))?
} else {
let shorteventid = services().globals.next_count()?;
self.eventid_shorteventid
.insert(event_id.as_bytes(), &shorteventid.to_be_bytes())?;
self.shorteventid_eventid
.insert(&shorteventid.to_be_bytes(), event_id.as_bytes())?;
shorteventid
}
};
};
self.eventidshort_cache
.lock()
@ -45,13 +45,13 @@ impl service::rooms::short::Data for KeyValueDatabase {
return Ok(Some(*short));
}
let mut statekey = event_type.to_string().as_bytes().to_vec();
statekey.push(0xff);
statekey.extend_from_slice(state_key.as_bytes());
let mut state_key_bytes = event_type.to_string().as_bytes().to_vec();
state_key_bytes.push(0xff);
state_key_bytes.extend_from_slice(state_key.as_bytes());
let short = self
.statekey_shortstatekey
.get(&statekey)?
.get(&state_key_bytes)?
.map(|shortstatekey| {
utils::u64_from_bytes(&shortstatekey)
.map_err(|_| Error::bad_database("Invalid shortstatekey in db."))
@ -82,22 +82,22 @@ impl service::rooms::short::Data for KeyValueDatabase {
return Ok(*short);
}
let mut statekey = event_type.to_string().as_bytes().to_vec();
statekey.push(0xff);
statekey.extend_from_slice(state_key.as_bytes());
let mut state_key_bytes = event_type.to_string().as_bytes().to_vec();
state_key_bytes.push(0xff);
state_key_bytes.extend_from_slice(state_key.as_bytes());
let short = match self.statekey_shortstatekey.get(&statekey)? {
Some(shortstatekey) => utils::u64_from_bytes(&shortstatekey)
.map_err(|_| Error::bad_database("Invalid shortstatekey in db."))?,
None => {
let short =
if let Some(shortstatekey) = self.statekey_shortstatekey.get(&state_key_bytes)? {
utils::u64_from_bytes(&shortstatekey)
.map_err(|_| Error::bad_database("Invalid shortstatekey in db."))?
} else {
let shortstatekey = services().globals.next_count()?;
self.statekey_shortstatekey
.insert(&statekey, &shortstatekey.to_be_bytes())?;
.insert(&state_key_bytes, &shortstatekey.to_be_bytes())?;
self.shortstatekey_statekey
.insert(&shortstatekey.to_be_bytes(), &statekey)?;
.insert(&shortstatekey.to_be_bytes(), &state_key_bytes)?;
shortstatekey
}
};
};
self.statekeyshort_cache
.lock()
@ -175,21 +175,22 @@ impl service::rooms::short::Data for KeyValueDatabase {
Ok(result)
}
/// Returns (shortstatehash, already_existed)
/// Returns (shortstatehash, `already_existed`)
fn get_or_create_shortstatehash(&self, state_hash: &[u8]) -> Result<(u64, bool)> {
Ok(match self.statehash_shortstatehash.get(state_hash)? {
Some(shortstatehash) => (
utils::u64_from_bytes(&shortstatehash)
.map_err(|_| Error::bad_database("Invalid shortstatehash in db."))?,
true,
),
None => {
Ok(
if let Some(shortstatehash) = self.statehash_shortstatehash.get(state_hash)? {
(
utils::u64_from_bytes(&shortstatehash)
.map_err(|_| Error::bad_database("Invalid shortstatehash in db."))?,
true,
)
} else {
let shortstatehash = services().globals.next_count()?;
self.statehash_shortstatehash
.insert(state_hash, &shortstatehash.to_be_bytes())?;
(shortstatehash, false)
}
})
},
)
}
fn get_shortroomid(&self, room_id: &RoomId) -> Result<Option<u64>> {
@ -203,15 +204,16 @@ impl service::rooms::short::Data for KeyValueDatabase {
}
fn get_or_create_shortroomid(&self, room_id: &RoomId) -> Result<u64> {
Ok(match self.roomid_shortroomid.get(room_id.as_bytes())? {
Some(short) => utils::u64_from_bytes(&short)
.map_err(|_| Error::bad_database("Invalid shortroomid in db."))?,
None => {
Ok(
if let Some(short) = self.roomid_shortroomid.get(room_id.as_bytes())? {
utils::u64_from_bytes(&short)
.map_err(|_| Error::bad_database("Invalid shortroomid in db."))?
} else {
let short = services().globals.next_count()?;
self.roomid_shortroomid
.insert(room_id.as_bytes(), &short.to_be_bytes())?;
short
}
})
},
)
}
}

View file

@ -63,7 +63,7 @@ impl service::rooms::state::Data for KeyValueDatabase {
}
for event_id in event_ids {
let mut key = prefix.to_owned();
let mut key = prefix.clone();
key.extend_from_slice(event_id.as_bytes());
self.roomid_pduleaves.insert(&key, event_id.as_bytes())?;
}

View file

@ -79,13 +79,12 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
event_type: &StateEventType,
state_key: &str,
) -> Result<Option<Arc<EventId>>> {
let shortstatekey = match services()
let Some(shortstatekey) = services()
.rooms
.short
.get_shortstatekey(event_type, state_key)?
{
Some(s) => s,
None => return Ok(None),
else {
return Ok(None);
};
let full_state = services()
.rooms

View file

@ -31,11 +31,10 @@ impl service::rooms::user::Data for KeyValueDatabase {
self.userroomid_notificationcount
.get(&userroom_id)?
.map(|bytes| {
.map_or(Ok(0), |bytes| {
utils::u64_from_bytes(&bytes)
.map_err(|_| Error::bad_database("Invalid notification count in db."))
})
.unwrap_or(Ok(0))
}
fn highlight_count(&self, user_id: &UserId, room_id: &RoomId) -> Result<u64> {
@ -45,11 +44,10 @@ impl service::rooms::user::Data for KeyValueDatabase {
self.userroomid_highlightcount
.get(&userroom_id)?
.map(|bytes| {
.map_or(Ok(0), |bytes| {
utils::u64_from_bytes(&bytes)
.map_err(|_| Error::bad_database("Invalid highlight count in db."))
})
.unwrap_or(Ok(0))
}
fn last_notification_read(&self, user_id: &UserId, room_id: &RoomId) -> Result<u64> {

View file

@ -67,9 +67,9 @@ impl service::sending::Data for KeyValueDatabase {
for (outgoing_kind, event) in requests {
let mut key = outgoing_kind.get_prefix();
if let SendingEventType::Pdu(value) = &event {
key.extend_from_slice(value)
key.extend_from_slice(value);
} else {
key.extend_from_slice(&services().globals.next_count()?.to_be_bytes())
key.extend_from_slice(&services().globals.next_count()?.to_be_bytes());
}
let value = if let SendingEventType::Edu(value) = &event {
&**value

View file

@ -12,7 +12,7 @@ impl service::transaction_ids::Data for KeyValueDatabase {
) -> Result<()> {
let mut key = user_id.as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(device_id.map(|d| d.as_bytes()).unwrap_or_default());
key.extend_from_slice(device_id.map(DeviceId::as_bytes).unwrap_or_default());
key.push(0xff);
key.extend_from_slice(txn_id.as_bytes());
@ -29,7 +29,7 @@ impl service::transaction_ids::Data for KeyValueDatabase {
) -> Result<Option<Vec<u8>>> {
let mut key = user_id.as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(device_id.map(|d| d.as_bytes()).unwrap_or_default());
key.extend_from_slice(device_id.map(DeviceId::as_bytes).unwrap_or_default());
key.push(0xff);
key.extend_from_slice(txn_id.as_bytes());

View file

@ -34,7 +34,7 @@ impl service::uiaa::Data for KeyValueDatabase {
.read()
.unwrap()
.get(&(user_id.to_owned(), device_id.to_owned(), session.to_owned()))
.map(|j| j.to_owned())
.map(ToOwned::to_owned)
}
fn update_uiaa_session(

View file

@ -141,7 +141,7 @@ impl service::users::Data for KeyValueDatabase {
Ok(())
}
/// Get the avatar_url of a user.
/// Get the `avatar_url` of a user.
fn avatar_url(&self, user_id: &UserId) -> Result<Option<OwnedMxcUri>> {
self.userid_avatarurl
.get(user_id.as_bytes())?
@ -153,7 +153,7 @@ impl service::users::Data for KeyValueDatabase {
.transpose()
}
/// Sets a new avatar_url or removes it if avatar_url is None.
/// Sets a new `avatar_url` or removes it if `avatar_url` is None.
fn set_avatar_url(&self, user_id: &UserId, avatar_url: Option<OwnedMxcUri>) -> Result<()> {
if let Some(avatar_url) = avatar_url {
self.userid_avatarurl
@ -178,7 +178,7 @@ impl service::users::Data for KeyValueDatabase {
.transpose()
}
/// Sets a new avatar_url or removes it if avatar_url is None.
/// Sets a new `avatar_url` or removes it if `avatar_url` is None.
fn set_blurhash(&self, user_id: &UserId, blurhash: Option<String>) -> Result<()> {
if let Some(blurhash) = blurhash {
self.userid_blurhash
@ -343,12 +343,11 @@ impl service::users::Data for KeyValueDatabase {
fn last_one_time_keys_update(&self, user_id: &UserId) -> Result<u64> {
self.userid_lastonetimekeyupdate
.get(user_id.as_bytes())?
.map(|bytes| {
.map_or(Ok(0), |bytes| {
utils::u64_from_bytes(&bytes).map_err(|_| {
Error::bad_database("Count in roomid_lastroomactiveupdate is invalid.")
})
})
.unwrap_or(Ok(0))
}
fn take_one_time_key(
@ -949,7 +948,7 @@ impl KeyValueDatabase {}
/// Will only return with Some(username) if the password was not empty and the
/// username could be successfully parsed.
/// If utils::string_from_bytes(...) returns an error that username will be skipped
/// If `utils::string_from_bytes`(...) returns an error that username will be skipped
/// and the error will be logged.
fn get_username_with_valid_password(username: &[u8], password: &[u8]) -> Option<String> {
// A valid password is not empty

View file

@ -34,7 +34,7 @@ use tokio::time::interval;
use tracing::{debug, error, info, warn};
pub struct KeyValueDatabase {
_db: Arc<dyn KeyValueDatabaseEngine>,
db: Arc<dyn KeyValueDatabaseEngine>,
//pub globals: globals::Globals,
pub(super) global: Arc<dyn KvTree>,
@ -113,7 +113,7 @@ pub struct KeyValueDatabase {
pub(super) roomsynctoken_shortstatehash: Arc<dyn KvTree>,
/// Remember the state hash at events in the past.
pub(super) shorteventid_shortstatehash: Arc<dyn KvTree>,
/// StateKey = EventType + StateKey, ShortStateKey = Count
/// `StateKey` = `EventType` + `StateKey`, `ShortStateKey` = Count
pub(super) statekey_shortstatekey: Arc<dyn KvTree>,
pub(super) shortstatekey_statekey: Arc<dyn KvTree>,
@ -127,14 +127,14 @@ pub struct KeyValueDatabase {
pub(super) shorteventid_authchain: Arc<dyn KvTree>,
/// RoomId + EventId -> outlier PDU.
/// `RoomId` + `EventId` -> outlier PDU.
/// Any pdu that has passed the steps 1-8 in the incoming event /federation/send/txn.
pub(super) eventid_outlierpdu: Arc<dyn KvTree>,
pub(super) softfailedeventids: Arc<dyn KvTree>,
/// ShortEventId + ShortEventId -> ().
/// `ShortEventId` + `ShortEventId` -> ().
pub(super) tofrom_relation: Arc<dyn KvTree>,
/// RoomId + EventId -> Parent PDU EventId.
/// `RoomId` + `EventId` -> Parent PDU `EventId`.
pub(super) referencedevents: Arc<dyn KvTree>,
//pub account_data: account_data::AccountData,
@ -241,6 +241,8 @@ impl KeyValueDatabase {
.map_err(|_| Error::BadConfig("Database folder doesn't exists and couldn't be created (e.g. due to missing permissions). Please create the database folder yourself."))?;
}
// Databases which are disabled will trigger this
#[allow(clippy::match_same_arms)]
let builder: Arc<dyn KeyValueDatabaseEngine> = match &*config.database_backend {
"sqlite" => {
#[cfg(not(feature = "sqlite"))]
@ -274,7 +276,7 @@ impl KeyValueDatabase {
}
let db_raw = Box::new(Self {
_db: builder.clone(),
db: builder.clone(),
userid_password: builder.open_tree("userid_password")?,
userid_displayname: builder.open_tree("userid_displayname")?,
userid_avatarurl: builder.open_tree("userid_avatarurl")?,
@ -428,12 +430,9 @@ impl KeyValueDatabase {
for (roomserverid, _) in db.roomserverids.iter() {
let mut parts = roomserverid.split(|&b| b == 0xff);
let room_id = parts.next().expect("split always returns one element");
let servername = match parts.next() {
Some(s) => s,
None => {
error!("Migration: Invalid roomserverid in db.");
continue;
}
let Some(servername) = parts.next() else {
error!("Migration: Invalid roomserverid in db.");
continue;
};
let mut serverroomid = servername.to_vec();
serverroomid.push(0xff);
@ -620,7 +619,7 @@ impl KeyValueDatabase {
Ok::<_, Error>(())
};
for (k, seventid) in db._db.open_tree("stateid_shorteventid")?.iter() {
for (k, seventid) in db.db.open_tree("stateid_shorteventid")?.iter() {
let sstatehash = utils::u64_from_bytes(&k[0..size_of::<u64>()])
.expect("number of bytes is correct");
let sstatekey = k[size_of::<u64>()..].to_vec();
@ -793,7 +792,7 @@ impl KeyValueDatabase {
}
// Force E2EE device list updates so we can send them over federation
for user_id in services().users.iter().filter_map(|r| r.ok()) {
for user_id in services().users.iter().filter_map(std::result::Result::ok) {
services().users.mark_device_key_update(&user_id)?;
}
@ -803,7 +802,7 @@ impl KeyValueDatabase {
}
if services().globals.database_version()? < 11 {
db._db
db.db
.open_tree("userdevicesessionid_uiaarequest")?
.clear()?;
services().globals.bump_database_version(11)?;
@ -979,7 +978,7 @@ impl KeyValueDatabase {
error!(
"Could not set the configured emergency password for the conduit user: {}",
e
)
);
}
};
@ -997,7 +996,7 @@ impl KeyValueDatabase {
pub fn flush(&self) -> Result<()> {
let start = std::time::Instant::now();
let res = self._db.flush();
let res = self.db.flush();
debug!("flush: took {:?}", start.elapsed());
@ -1017,17 +1016,10 @@ impl KeyValueDatabase {
}
async fn try_handle_updates() -> Result<()> {
let response = services()
.globals
.default_client()
.get("https://conduit.rs/check-for-updates/stable")
.send()
.await?;
#[derive(Deserialize)]
struct CheckForUpdatesResponseEntry {
id: u64,
date: String,
id: u64,
message: String,
}
#[derive(Deserialize)]
@ -1035,6 +1027,13 @@ impl KeyValueDatabase {
updates: Vec<CheckForUpdatesResponseEntry>,
}
let response = services()
.globals
.default_client()
.get("https://conduit.rs/check-for-updates/stable")
.send()
.await?;
let response = serde_json::from_str::<CheckForUpdatesResponse>(&response.text().await?)
.map_err(|_| Error::BadServerResponse("Bad version check response"))?;
@ -1048,7 +1047,7 @@ impl KeyValueDatabase {
.send_message(RoomMessageEventContent::text_plain(format!(
"@room: The following is a message from the Conduit developers. It was sent on '{}':\n\n{}",
update.date, update.message
)))
)));
}
}
services()
@ -1066,7 +1065,7 @@ impl KeyValueDatabase {
use std::time::{Duration, Instant};
let timer_interval =
Duration::from_secs(services().globals.config.cleanup_second_interval as u64);
Duration::from_secs(u64::from(services().globals.config.cleanup_second_interval));
tokio::spawn(async move {
let mut i = interval(timer_interval);