Skip to content

Commit

Permalink
refactor: clippy fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-herlemont committed Oct 12, 2024
1 parent 77b4ba0 commit 9a54153
Show file tree
Hide file tree
Showing 33 changed files with 126 additions and 147 deletions.
8 changes: 4 additions & 4 deletions benches/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ fn bench_select_range<T: Default + Item + native_db::ToInput + Clone + Debug>(
bench_display: BenchDisplay,
cfg: BenchSelectRangeRandomDataCfg,
) {
let mut group = c.benchmark_group(format!("Select Range Secondary Key"));
let mut group = c.benchmark_group("Select Range Secondary Key".to_string());
group.plot_config(
criterion::PlotConfiguration::default().summary_scale(criterion::AxisScale::Linear),
);
Expand Down Expand Up @@ -280,7 +280,7 @@ fn bench_select_range<T: Default + Item + native_db::ToInput + Clone + Debug>(
let transaction = db
.transaction_with_behavior(TransactionBehavior::Immediate)
.unwrap();
let sql = T::generate_select_range_sk(&"sk_1");
let sql = T::generate_select_range_sk("sk_1");
let mut stmt = transaction.prepare(&sql).unwrap();
let rows =
stmt.query_map(&[(":from_sk", &from_sk), (":to_sk", &to_sk)], |row| {
Expand All @@ -301,7 +301,7 @@ fn bench_get<T: Default + Item + native_db::ToInput + Clone + Debug>(
c: &mut Criterion,
bench_display: BenchDisplay,
) {
let mut group = c.benchmark_group(format!("Get"));
let mut group = c.benchmark_group("Get".to_string());
group.plot_config(
criterion::PlotConfiguration::default().summary_scale(criterion::AxisScale::Linear),
);
Expand Down Expand Up @@ -392,7 +392,7 @@ fn bench_delete<T: Default + Item + native_db::ToInput + Clone + Debug>(
c: &mut Criterion,
bench_display: BenchDisplay,
) {
let mut group = c.benchmark_group(format!("Delete"));
let mut group = c.benchmark_group("Delete".to_string());
group.plot_config(
criterion::PlotConfiguration::default().summary_scale(criterion::AxisScale::Linear),
);
Expand Down
11 changes: 6 additions & 5 deletions benches/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ macro_rules! define_item_struct {
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
#[native_model(id = $id, version = 1)]
#[native_db]
#[allow(non_camel_case_types)]
pub struct $struct_name {
#[primary_key]
pub pk: i64,
Expand Down Expand Up @@ -220,7 +221,7 @@ impl BenchDisplay {
}

pub fn display_1_by_tranaction(&self) -> String {
format!("{}", self.display('1'))
self.display('1').to_string()
}

pub fn display_n_by_tranaction(&self) -> String {
Expand All @@ -229,10 +230,10 @@ impl BenchDisplay {

pub fn display_read(&self) -> String {
match self {
BenchDisplay::SK_1 => format!("1:SK"),
BenchDisplay::SK_10 => format!("10:SK"),
BenchDisplay::SK_50 => format!("50:SK"),
BenchDisplay::SK_100 => format!("100:SK"),
BenchDisplay::SK_1 => "1:SK".to_string(),
BenchDisplay::SK_10 => "10:SK".to_string(),
BenchDisplay::SK_50 => "50:SK".to_string(),
BenchDisplay::SK_100 => "100:SK".to_string(),
}
}

Expand Down
6 changes: 5 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,8 @@ expand test_file_name="util":
RUSTFLAGS="-Zmacro-backtrace" cargo expand --test {{test_file_name}} | save -f --raw src/{{test_file_name}}_expanded.rs

expand_clean:
rm -f src/*_expanded.rs
rm -f src/*_expanded.rs

format:
cargo clippy; \
cargo fmt --all
11 changes: 1 addition & 10 deletions native_db_macro/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<O: ToTokenStream> ToTokenStream for KeyDefinition<O> {
}
}

#[derive(Clone)]
#[derive(Clone, Default)]
pub(crate) struct KeyOptions {
pub(crate) unique: bool,
pub(crate) optional: bool,
Expand All @@ -100,15 +100,6 @@ impl ToTokenStream for () {
}
}

impl Default for KeyOptions {
fn default() -> Self {
Self {
unique: false,
optional: false,
}
}
}

impl<O: ToTokenStream> KeyDefinition<O> {
pub(crate) fn name(&self) -> String {
if let Some(field_name) = &self.field_name {
Expand Down
4 changes: 2 additions & 2 deletions native_db_macro/src/model_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl ModelAttributes {
} else {
panic!(
"Unknown attribute: {}",
meta.path.get_ident().expect("Expected ident").to_string()
meta.path.get_ident().expect("Expected ident")
);
}
Ok(())
Expand All @@ -102,7 +102,7 @@ impl ModelAttributes {
field.ty.to_tokens(&mut field_type_token_stream);
let field_type = field_type_token_stream.to_string();
let mut secondary_options = KeyOptions::default();
if let Ok(_) = attr.meta.require_list() {
if attr.meta.require_list().is_ok() {
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("unique") {
secondary_options.unique = true;
Expand Down
19 changes: 7 additions & 12 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::atomic::AtomicU64;
use std::sync::{Arc, RwLock};
use std::u64;

/// The database instance. Allows you to create [rw_transaction](database/struct.Database.html#method.rw_transaction) and [r_transaction](database/struct.Database.html#method.r_transaction), [watch](database/struct.Database.html#method.watch) queries, and [unwatch](database/struct.Database.html#method.unwatch) etc.
///
Expand Down Expand Up @@ -120,18 +119,14 @@ impl<'a> Database<'a> {
(model_builder, main_table_definition).into();

let rw = self.instance.redb_database()?.begin_write()?;
rw.open_table(primary_table_definition.redb.clone())?;
rw.open_table(primary_table_definition.redb)?;

for secondary_key in model_builder.model.secondary_keys.iter() {
primary_table_definition.secondary_tables.insert(
secondary_key.clone(),
redb::MultimapTableDefinition::new(secondary_key.unique_table_name.as_str()).into(),
);
rw.open_multimap_table(
primary_table_definition.secondary_tables[&secondary_key]
.redb
.clone(),
)?;
rw.open_multimap_table(primary_table_definition.secondary_tables[secondary_key].redb)?;
}
rw.commit()?;

Expand Down Expand Up @@ -191,7 +186,7 @@ impl<'a> Database<'a> {
use semver::VersionReq;
let metadata = self.metadata();
let comparator = VersionReq::parse(selector)
.expect(format!("Invalid version selector: {}", selector).as_str());
.unwrap_or_else(|_| panic!("Invalid version selector: {}", selector));

let previous_version = if let Some(previous_version) = metadata.previous_version() {
previous_version
Expand All @@ -200,9 +195,9 @@ impl<'a> Database<'a> {
};

let previous_version = Version::parse(previous_version)
.expect(format!("Invalid previous version: {}", previous_version).as_str());
.unwrap_or_else(|_| panic!("Invalid previous version: {}", previous_version));
let current_version = Version::parse(metadata.current_version())
.expect(format!("Invalid current version: {}", metadata.current_version()).as_str());
.unwrap_or_else(|_| panic!("Invalid current version: {}", metadata.current_version()));

// If the previous version is the same as the current version, the database is not upgrading
if previous_version == current_version {
Expand All @@ -216,7 +211,7 @@ impl<'a> Database<'a> {
let rx = self.instance.redb_database()?.begin_read()?;
let mut stats_primary_tables = vec![];
for primary_table in self.primary_table_definitions.values() {
let result_table_open = rx.open_table(primary_table.redb.clone());
let result_table_open = rx.open_table(primary_table.redb);
let stats_table = match result_table_open {
Err(redb::TableError::TableDoesNotExist(_)) => StatsTable {
name: primary_table.redb.name().to_string(),
Expand All @@ -238,7 +233,7 @@ impl<'a> Database<'a> {
let mut stats_secondary_tables = vec![];
for primary_table in self.primary_table_definitions.values() {
for secondary_table in primary_table.secondary_tables.values() {
let result_table_open = rx.open_multimap_table(secondary_table.redb.clone());
let result_table_open = rx.open_multimap_table(secondary_table.redb);
let stats_table = match result_table_open {
Err(redb::TableError::TableDoesNotExist(_)) => StatsTable {
name: secondary_table.redb.name().to_string(),
Expand Down
8 changes: 7 additions & 1 deletion src/database_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Builder {
};

for (_, model_builder) in models.models_builder.iter() {
database.seed_model(&model_builder)?;
database.seed_model(model_builder)?;
}

// TODO: Maybe we can do some migration with models here.
Expand All @@ -65,6 +65,12 @@ impl Builder {
}
}

impl Default for Builder {
fn default() -> Self {
Self::new()
}
}

impl Builder {
/// Construct a new [Builder] with sensible defaults.
pub fn new() -> Self {
Expand Down
5 changes: 2 additions & 3 deletions src/db_type/key/inner_key_value_redb1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl Value for DatabaseInnerKeyValue {

impl Key for DatabaseInnerKeyValue {
fn compare(data1: &[u8], data2: &[u8]) -> std::cmp::Ordering {
data1.cmp(&data2)
data1.cmp(data2)
}
}

Expand Down Expand Up @@ -347,8 +347,7 @@ mod tests {
use std::ops::RangeBounds;

fn range<T: InnerKeyValue, R: RangeBounds<T>>(range: R) -> DatabaseInnerKeyValueRange {
let range = DatabaseInnerKeyValueRange::new(range);
range
DatabaseInnerKeyValueRange::new(range)
}

#[test]
Expand Down
6 changes: 2 additions & 4 deletions src/db_type/key/key.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use redb::{Key as RedbKey, TypeName, Value as RedbValue};
use std::fmt::Debug;
use std::ops::{Bound, Range, RangeBounds, RangeFrom, RangeInclusive, RangeTo, RangeToInclusive};
use std::u8;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Key(Vec<u8>);
Expand Down Expand Up @@ -455,7 +454,7 @@ impl RedbValue for Key {

impl RedbKey for Key {
fn compare(data1: &[u8], data2: &[u8]) -> std::cmp::Ordering {
data1.cmp(&data2)
data1.cmp(data2)
}
}

Expand Down Expand Up @@ -533,8 +532,7 @@ mod tests {
use std::ops::RangeBounds;

fn range<T: ToKey, R: RangeBounds<T>>(range: R) -> KeyRange {
let range = KeyRange::new(range);
range
KeyRange::new(range)
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions src/db_type/key/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[allow(clippy::module_inception)]
mod key;
mod key_definition;
mod key_value;
Expand Down
1 change: 1 addition & 0 deletions src/metadata/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod current_version;
#[allow(clippy::module_inception)]
mod metadata;
mod table;

Expand Down
9 changes: 4 additions & 5 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ impl Model {
where
F: Fn(KeyOptions) -> bool,
{
let key = self
.secondary_keys
.get(secondary_key.into())
.ok_or_else(|| Error::SecondaryKeyDefinitionNotFound {
let key = self.secondary_keys.get(secondary_key).ok_or_else(|| {
Error::SecondaryKeyDefinitionNotFound {
table: self.primary_key.unique_table_name.to_string(),
key: secondary_key.unique_table_name.clone(),
})?;
}
})?;

if check(key.options.clone()) {
Ok(())
Expand Down
5 changes: 3 additions & 2 deletions src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl Database<'_> {
let w = new_db.instance.redb_database()?.begin_write()?;
{
// Copy primary tables
for (_, primary_table_definition) in &self.primary_table_definitions {
for primary_table_definition in self.primary_table_definitions.values() {
let table = r.open_table(primary_table_definition.redb)?;
let mut new_table = w.open_table(primary_table_definition.redb)?;
for result in table.iter()? {
Expand All @@ -20,7 +20,8 @@ impl Database<'_> {
}

// Copy secondary tables
for (_, secondary_table_definition) in &primary_table_definition.secondary_tables {
for secondary_table_definition in primary_table_definition.secondary_tables.values()
{
let table = r.open_multimap_table(secondary_table_definition.redb)?;
let mut new_table = w.open_multimap_table(secondary_table_definition.redb)?;
for result in table.iter()? {
Expand Down
12 changes: 1 addition & 11 deletions src/table_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct PrimaryTableDefinition<'a> {
pub(crate) native_model_options: NativeModelOptions,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub struct NativeModelOptions {
pub(crate) native_model_id: u32,
pub(crate) native_model_version: u32,
Expand All @@ -23,16 +23,6 @@ pub struct NativeModelOptions {
pub(crate) native_model_legacy: bool,
}

impl Default for NativeModelOptions {
fn default() -> Self {
Self {
native_model_id: 0,
native_model_version: 0,
native_model_legacy: false,
}
}
}

impl<'a> From<(&ModelBuilder, RedbPrimaryTableDefinition<'a>)> for PrimaryTableDefinition<'a> {
fn from(input: (&ModelBuilder, RedbPrimaryTableDefinition<'a>)) -> Self {
let (builder, redb) = input;
Expand Down
2 changes: 1 addition & 1 deletion src/transaction/internal/private_readable_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub trait PrivateReadableTransaction<'db, 'txn> {
) -> Result<Option<Output>> {
let secondary_key = key_def.key_definition();
// Provide a better error for the test of unicity of the secondary key
model.check_secondary_options(&secondary_key, |options| options.unique == true)?;
model.check_secondary_options(&secondary_key, |options| options.unique)?;

let table = self.get_secondary_table(&model, &secondary_key)?;

Expand Down
4 changes: 2 additions & 2 deletions src/transaction/internal/r_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ where
type RedbTransaction<'db_bis> = redb::ReadTransaction where Self: 'db_bis;

fn table_definitions(&self) -> &HashMap<String, PrimaryTableDefinition> {
&self.table_definitions
self.table_definitions
}

fn get_primary_table(&'txn self, model: &Model) -> Result<Self::RedbPrimaryTable> {
Expand All @@ -47,7 +47,7 @@ where
})?;
let secondary_table_definition = main_table_definition
.secondary_tables
.get(&secondary_key)
.get(secondary_key)
.ok_or_else(|| Error::TableDefinitionNotFound {
table: secondary_key.unique_table_name.to_string(),
})?;
Expand Down
Loading

0 comments on commit 9a54153

Please sign in to comment.