Skip to content

Commit

Permalink
de-wrap all database remove calls
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Volk <[email protected]>
  • Loading branch information
jevolk committed Aug 8, 2024
1 parent 758d70b commit 8898e10
Show file tree
Hide file tree
Showing 21 changed files with 92 additions and 80 deletions.
24 changes: 14 additions & 10 deletions src/database/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,21 @@ impl Map {
}

#[tracing::instrument(skip(self), level = "trace")]
pub fn remove(&self, key: &Key) -> Result<()> {
pub fn remove(&self, key: &Key) {
let write_options = &self.write_options;
let res = self.db.db.delete_cf_opt(&self.cf(), key, write_options);
self.db
.db
.delete_cf_opt(&self.cf(), key, write_options)
.or_else(or_else)
.expect("database remove error");

if !self.db.corked() {
self.db.flush()?;
self.db.flush().expect("database flush error");
}

result(res)
}

#[tracing::instrument(skip(self, iter), level = "trace")]
pub fn remove_batch<'a, I>(&'a self, iter: I) -> Result<()>
pub fn remove_batch<'a, I>(&'a self, iter: I)
where
I: Iterator<Item = &'a Key>,
{
Expand All @@ -127,13 +129,15 @@ impl Map {
}

let write_options = &self.write_options;
let res = self.db.db.write_opt(batch, write_options);
self.db
.db
.write_opt(batch, write_options)
.or_else(or_else)
.expect("database remove batch error");

if !self.db.corked() {
self.db.flush()?;
self.db.flush().expect("database flush error");
}

result(res)
}

#[tracing::instrument(skip(self), level = "trace")]
Expand Down
2 changes: 1 addition & 1 deletion src/service/account_data/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Data {

// Remove old entry
if let Some(prev) = prev {
self.roomuserdataid_accountdata.remove(&prev)?;
self.roomuserdataid_accountdata.remove(&prev);
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/service/appservice/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Data {
/// * `service_name` - the name you send to register the service previously
pub(super) fn unregister_appservice(&self, service_name: &str) -> Result<()> {
self.id_appserviceregistrations
.remove(service_name.as_bytes())?;
.remove(service_name.as_bytes());
Ok(())
}

Expand Down
5 changes: 4 additions & 1 deletion src/service/globals/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ impl Data {
}

#[inline]
pub fn remove_keypair(&self) -> Result<()> { self.global.remove(b"keypair") }
pub fn remove_keypair(&self) -> Result<()> {
self.global.remove(b"keypair");
Ok(())
}

/// TODO: the key valid until timestamp (`valid_until_ts`) is only honored
/// in room version > 4
Expand Down
8 changes: 4 additions & 4 deletions src/service/globals/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ async fn db_lt_9(services: &Services) -> Result<()> {
.collect();

for key in batch2 {
tokenids.remove(&key)?;
tokenids.remove(&key);
}

services.globals.db.bump_database_version(9)?;
Expand Down Expand Up @@ -836,8 +836,8 @@ async fn handle_media_check(
"Media is missing at all paths. Removing from database..."
);

mediaid_file.remove(key)?;
mediaid_user.remove(key)?;
mediaid_file.remove(key);
mediaid_user.remove(key);
}

if config.media_compat_file_link && !old_exists && new_exists {
Expand Down Expand Up @@ -905,7 +905,7 @@ async fn fix_bad_double_separator_in_state_cache(services: &Services) -> Result<
== vec![0xFF, 0xFF]
{
debug_warn!("Found bad key: {key:?}");
roomuserid_joined.remove(&key)?;
roomuserid_joined.remove(&key);

key.remove(first_sep_index);
debug_warn!("Fixed key: {key:?}");
Expand Down
12 changes: 6 additions & 6 deletions src/service/key_backups/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ impl Data {
key.push(0xFF);
key.extend_from_slice(version.as_bytes());

self.backupid_algorithm.remove(&key)?;
self.backupid_etag.remove(&key)?;
self.backupid_algorithm.remove(&key);
self.backupid_etag.remove(&key);

key.push(0xFF);

for (outdated_key, _) in self.backupkeyid_backup.scan_prefix(key) {
self.backupkeyid_backup.remove(&outdated_key)?;
self.backupkeyid_backup.remove(&outdated_key);
}

Ok(())
Expand Down Expand Up @@ -303,7 +303,7 @@ impl Data {
key.push(0xFF);

for (outdated_key, _) in self.backupkeyid_backup.scan_prefix(key) {
self.backupkeyid_backup.remove(&outdated_key)?;
self.backupkeyid_backup.remove(&outdated_key);
}

Ok(())
Expand All @@ -318,7 +318,7 @@ impl Data {
key.push(0xFF);

for (outdated_key, _) in self.backupkeyid_backup.scan_prefix(key) {
self.backupkeyid_backup.remove(&outdated_key)?;
self.backupkeyid_backup.remove(&outdated_key);
}

Ok(())
Expand All @@ -336,7 +336,7 @@ impl Data {
key.extend_from_slice(session_id.as_bytes());

for (outdated_key, _) in self.backupkeyid_backup.scan_prefix(key) {
self.backupkeyid_backup.remove(&outdated_key)?;
self.backupkeyid_backup.remove(&outdated_key);
}

Ok(())
Expand Down
9 changes: 6 additions & 3 deletions src/service/media/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ impl Data {

for (key, _) in self.mediaid_file.scan_prefix(prefix) {
debug!("Deleting key: {:?}", key);
self.mediaid_file.remove(&key)?;
self.mediaid_file.remove(&key);
}

for (key, value) in self.mediaid_user.scan_prefix(mxc.as_bytes().to_vec()) {
if key == mxc.as_bytes().to_vec() {
let user = string_from_bytes(&value).unwrap_or_default();

debug_info!("Deleting key \"{key:?}\" which was uploaded by user {user}");
self.mediaid_user.remove(&key)?;
self.mediaid_user.remove(&key);
}
}

Expand Down Expand Up @@ -159,7 +159,10 @@ impl Data {
pub(crate) fn get_all_media_keys(&self) -> Vec<Vec<u8>> { self.mediaid_file.iter().map(|(key, _)| key).collect() }

#[inline]
pub(super) fn remove_url_preview(&self, url: &str) -> Result<()> { self.url_previews.remove(url.as_bytes()) }
pub(super) fn remove_url_preview(&self, url: &str) -> Result<()> {
self.url_previews.remove(url.as_bytes());
Ok(())
}

pub(super) fn set_url_preview(
&self, url: &str, data: &UrlPreviewData, timestamp: std::time::Duration,
Expand Down
6 changes: 3 additions & 3 deletions src/service/presence/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl Data {

if let Some((last_count, _)) = last_presence {
let key = presenceid_key(last_count, user_id);
self.presenceid_presence.remove(&key)?;
self.presenceid_presence.remove(&key);
}

Ok(())
Expand All @@ -133,8 +133,8 @@ impl Data {
let count = utils::u64_from_bytes(&count_bytes)
.map_err(|_e| Error::bad_database("No 'count' bytes in presence key"))?;
let key = presenceid_key(count, user_id);
self.presenceid_presence.remove(&key)?;
self.userid_presenceid.remove(user_id.as_bytes())?;
self.presenceid_presence.remove(&key);
self.userid_presenceid.remove(user_id.as_bytes());
}

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion src/service/pusher/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ impl Data {
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_err(Into::into)
self.senderkey_pusher.remove(&key);
Ok(())
},
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/service/rooms/alias/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ impl Data {
prefix.push(0xFF);

for (key, _) in self.aliasid_alias.scan_prefix(prefix) {
self.aliasid_alias.remove(&key)?;
self.aliasid_alias.remove(&key);
}

self.alias_roomid.remove(alias.alias().as_bytes())?;

self.alias_userid.remove(alias.alias().as_bytes())?;
self.alias_roomid.remove(alias.alias().as_bytes());
self.alias_userid.remove(alias.alias().as_bytes());
} else {
return Err(Error::BadRequest(ErrorKind::NotFound, "Alias does not exist or is invalid."));
}
Expand Down
3 changes: 2 additions & 1 deletion src/service/rooms/directory/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ impl Data {
}

pub(super) fn set_not_public(&self, room_id: &RoomId) -> Result<()> {
self.publicroomids.remove(room_id.as_bytes())
self.publicroomids.remove(room_id.as_bytes());
Ok(())
}

pub(super) fn is_public_room(&self, room_id: &RoomId) -> Result<bool> {
Expand Down
2 changes: 1 addition & 1 deletion src/service/rooms/lazy_loading/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Data {
prefix.push(0xFF);

for (key, _) in self.lazyloadedids.scan_prefix(prefix) {
self.lazyloadedids.remove(&key)?;
self.lazyloadedids.remove(&key);
}

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/service/rooms/metadata/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Data {
if disabled {
self.disabledroomids.insert(room_id.as_bytes(), &[]);
} else {
self.disabledroomids.remove(room_id.as_bytes())?;
self.disabledroomids.remove(room_id.as_bytes());
}

Ok(())
Expand All @@ -83,7 +83,7 @@ impl Data {
if banned {
self.bannedroomids.insert(room_id.as_bytes(), &[]);
} else {
self.bannedroomids.remove(room_id.as_bytes())?;
self.bannedroomids.remove(room_id.as_bytes());
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/service/rooms/read_receipt/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Data {
== user_id.as_bytes()
}) {
// This is the old room_latest
self.readreceiptid_readreceipt.remove(&old)?;
self.readreceiptid_readreceipt.remove(&old);
}

let mut room_latest_id = prefix;
Expand Down
2 changes: 1 addition & 1 deletion src/service/rooms/search/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Data {
});

for token in batch {
self.tokenids.remove(&token)?;
self.tokenids.remove(&token);
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/service/rooms/state/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Data {
prefix.push(0xFF);

for (key, _) in self.roomid_pduleaves.scan_prefix(prefix.clone()) {
self.roomid_pduleaves.remove(&key)?;
self.roomid_pduleaves.remove(&key);
}

for event_id in event_ids {
Expand Down
36 changes: 18 additions & 18 deletions src/service/rooms/state_cache/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ impl Data {

self.userroomid_joined.insert(&userroom_id, &[]);
self.roomuserid_joined.insert(&roomuser_id, &[]);
self.userroomid_invitestate.remove(&userroom_id)?;
self.roomuserid_invitecount.remove(&roomuser_id)?;
self.userroomid_leftstate.remove(&userroom_id)?;
self.roomuserid_leftcount.remove(&roomuser_id)?;
self.userroomid_invitestate.remove(&userroom_id);
self.roomuserid_invitecount.remove(&roomuser_id);
self.userroomid_leftstate.remove(&userroom_id);
self.roomuserid_leftcount.remove(&roomuser_id);

self.roomid_inviteviaservers.remove(&roomid)?;
self.roomid_inviteviaservers.remove(&roomid);

Ok(())
}
Expand All @@ -114,10 +114,10 @@ impl Data {
);
self.roomuserid_invitecount
.insert(&roomuser_id, &self.services.globals.next_count()?.to_be_bytes());
self.userroomid_joined.remove(&userroom_id)?;
self.roomuserid_joined.remove(&roomuser_id)?;
self.userroomid_leftstate.remove(&userroom_id)?;
self.roomuserid_leftcount.remove(&roomuser_id)?;
self.userroomid_joined.remove(&userroom_id);
self.roomuserid_joined.remove(&roomuser_id);
self.userroomid_leftstate.remove(&userroom_id);
self.roomuserid_leftcount.remove(&roomuser_id);

if let Some(servers) = invite_via {
let mut prev_servers = self
Expand Down Expand Up @@ -158,12 +158,12 @@ impl Data {
); // TODO
self.roomuserid_leftcount
.insert(&roomuser_id, &self.services.globals.next_count()?.to_be_bytes());
self.userroomid_joined.remove(&userroom_id)?;
self.roomuserid_joined.remove(&roomuser_id)?;
self.userroomid_invitestate.remove(&userroom_id)?;
self.roomuserid_invitecount.remove(&roomuser_id)?;
self.userroomid_joined.remove(&userroom_id);
self.roomuserid_joined.remove(&roomuser_id);
self.userroomid_invitestate.remove(&userroom_id);
self.roomuserid_invitecount.remove(&roomuser_id);

self.roomid_inviteviaservers.remove(&roomid)?;
self.roomid_inviteviaservers.remove(&roomid);

Ok(())
}
Expand Down Expand Up @@ -199,8 +199,8 @@ impl Data {
serverroom_id.push(0xFF);
serverroom_id.extend_from_slice(room_id.as_bytes());

self.roomserverids.remove(&roomserver_id)?;
self.serverroomids.remove(&serverroom_id)?;
self.roomserverids.remove(&roomserver_id);
self.serverroomids.remove(&serverroom_id);
}
}

Expand Down Expand Up @@ -272,8 +272,8 @@ impl Data {
roomuser_id.push(0xFF);
roomuser_id.extend_from_slice(user_id.as_bytes());

self.userroomid_leftstate.remove(&userroom_id)?;
self.roomuserid_leftcount.remove(&roomuser_id)?;
self.userroomid_leftstate.remove(&userroom_id);
self.roomuserid_leftcount.remove(&roomuser_id);

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/service/rooms/timeline/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl Data {
.insert(pdu.room_id.clone(), PduCount::Normal(count));

self.eventid_pduid.insert(pdu.event_id.as_bytes(), pdu_id);
self.eventid_outlierpdu.remove(pdu.event_id.as_bytes())?;
self.eventid_outlierpdu.remove(pdu.event_id.as_bytes());

Ok(())
}
Expand All @@ -198,7 +198,7 @@ impl Data {
);

self.eventid_pduid.insert(event_id.as_bytes(), pdu_id);
self.eventid_outlierpdu.remove(event_id.as_bytes())?;
self.eventid_outlierpdu.remove(event_id.as_bytes());

Ok(())
}
Expand Down
Loading

0 comments on commit 8898e10

Please sign in to comment.