Skip to content

Commit

Permalink
Fix more clippy warnings
Browse files Browse the repository at this point in the history
Fixes: matrix-org#96
Signed-off-by: Jan Alexander Steffens (heftig) <[email protected]>
  • Loading branch information
heftig committed Aug 2, 2022
1 parent dd62afb commit 7f63149
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 61 deletions.
64 changes: 35 additions & 29 deletions compressor_integration_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ use postgres::{fallible_iterator::FallibleIterator, Client};
use postgres_openssl::MakeTlsConnector;
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use state_map::StateMap;
use std::{borrow::Cow, collections::BTreeMap, env, fmt};
use std::{
borrow::Cow,
collections::BTreeMap,
env,
fmt::{self, Write as _},
};
use string_cache::DefaultAtom as Atom;

use synapse_compress_state::StateGroupEntry;
Expand All @@ -23,47 +28,48 @@ pub fn add_contents_to_database(room_id: &str, state_group_map: &BTreeMap<i64, S
let mut client = Client::connect(DB_URL, connector).unwrap();

// build up the query
let mut sql = "".to_string();
let mut sql = String::new();

let room_id = PGEscape(room_id);
let event_id = PGEscape("left_blank");

for (sg, entry) in state_group_map {
// create the entry for state_groups
sql.push_str(&format!(
"INSERT INTO state_groups (id, room_id, event_id) VALUES ({},{},{});\n",
sg,
PGEscape(room_id),
PGEscape("left_blank")
));
writeln!(
sql,
"INSERT INTO state_groups (id, room_id, event_id) \
VALUES ({sg}, {room_id}, {event_id});",
)
.expect("Writing to a String cannot fail");

// create the entry in state_group_edges IF exists
if let Some(prev_sg) = entry.prev_state_group {
sql.push_str(&format!(
"INSERT INTO state_group_edges (state_group, prev_state_group) VALUES ({}, {});\n",
sg, prev_sg
));
writeln!(
sql,
"INSERT INTO state_group_edges (state_group, prev_state_group) \
VALUES ({sg}, {prev_sg});",
)
.unwrap();
}

// write entry for each row in delta
if !entry.state_map.is_empty() {
sql.push_str("INSERT INTO state_groups_state (state_group, room_id, type, state_key, event_id) VALUES");
sql.push_str(
"INSERT INTO state_groups_state \
(state_group, room_id, type, state_key, event_id) \
VALUES\n",
);

let mut first = true;
for ((t, s), e) in entry.state_map.iter() {
if first {
sql.push_str(" ");
first = false;
} else {
sql.push_str(" ,");
}
sql.push_str(&format!(
"({}, {}, {}, {}, {})",
sg,
PGEscape(room_id),
PGEscape(t),
PGEscape(s),
PGEscape(e)
));
let t = PGEscape(t);
let s = PGEscape(s);
let e = PGEscape(e);

writeln!(sql, " ({sg}, {room_id}, {t}, {s}, {e}),").unwrap();
}
sql.push_str(";\n");

// Replace the last comma with a semicolon
sql.replace_range((sql.len() - 2).., ";\n");
}
}

Expand Down
67 changes: 35 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ use clap::{crate_authors, crate_description, crate_name, crate_version, Arg, Com
use indicatif::{ProgressBar, ProgressStyle};
use rayon::prelude::*;
use state_map::StateMap;
use std::{collections::BTreeMap, convert::TryInto, fs::File, io::Write, str::FromStr};
use std::{
collections::BTreeMap, convert::TryInto, fmt::Write as _, fs::File, io::Write, str::FromStr,
};
use string_cache::DefaultAtom as Atom;

mod compressor;
Expand Down Expand Up @@ -424,8 +426,7 @@ fn generate_sql<'a>(
new_map: &'a BTreeMap<i64, StateGroupEntry>,
room_id: &'a str,
) -> impl Iterator<Item = String> + 'a {
old_map.iter().map(move |(sg,old_entry)| {

old_map.iter().map(move |(sg, old_entry)| {
let new_entry = &new_map[sg];

// Check if the new map has a different entry for this state group
Expand All @@ -435,48 +436,50 @@ fn generate_sql<'a>(
let mut sql = String::new();

// remove the current edge
sql.push_str(&format!(
"DELETE FROM state_group_edges WHERE state_group = {};\n",
sg
));
writeln!(
sql,
"DELETE FROM state_group_edges WHERE state_group = {sg};",
)
.expect("Writing to a String cannot fail");

// if the new entry has a predecessor then put that into state_group_edges
if let Some(prev_sg) = new_entry.prev_state_group {
sql.push_str(&format!("INSERT INTO state_group_edges (state_group, prev_state_group) VALUES ({}, {});\n", sg, prev_sg));
writeln!(
sql,
"INSERT INTO state_group_edges (state_group, prev_state_group) \
VALUES ({sg}, {prev_sg});",
)
.unwrap();
}

// remove the current deltas for this state group
sql.push_str(&format!(
"DELETE FROM state_groups_state WHERE state_group = {};\n",
sg
));
writeln!(
sql,
"DELETE FROM state_groups_state WHERE state_group = {sg};",
)
.unwrap();

if !new_entry.state_map.is_empty() {
// place all the deltas for the state group in the new map into state_groups_state
sql.push_str("INSERT INTO state_groups_state (state_group, room_id, type, state_key, event_id) VALUES\n");
sql.push_str(
"INSERT INTO state_groups_state \
(state_group, room_id, type, state_key, event_id) \
VALUES\n",
);

let mut first = true;
let room_id = PGEscape(room_id);
for ((t, s), e) in new_entry.state_map.iter() {
// Add a comma at the start if not the first row to be inserted
if first {
sql.push_str(" ");
first = false;
} else {
sql.push_str(" ,");
}

// write the row to be insterted of the form:
let t = PGEscape(t);
let s = PGEscape(s);
let e = PGEscape(e);

// write the row to be inserted of the form:
// (state_group, room_id, type, state_key, event_id)
sql.push_str(&format!(
"({}, {}, {}, {}, {})",
sg,
PGEscape(room_id),
PGEscape(t),
PGEscape(s),
PGEscape(e)
));
writeln!(sql, " ({sg}, {room_id}, {t}, {s}, {e}),").unwrap();
}
sql.push_str(";\n");

// Replace the last comma with a semicolon
sql.replace_range((sql.len() - 2).., ";\n");
}

sql
Expand Down

0 comments on commit 7f63149

Please sign in to comment.