Skip to content

Commit

Permalink
chore: update to 2021 edition and implement clippy fixes
Browse files Browse the repository at this point in the history
Signed-off-by: simonsan <[email protected]>
  • Loading branch information
simonsan committed Dec 20, 2023
1 parent 7a1a421 commit 7c70779
Show file tree
Hide file tree
Showing 37 changed files with 301 additions and 290 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "native_db"
version = "0.5.1"
authors = ["Vincent Herlemont <[email protected]>"]
edition = "2018"
edition = "2021"
description = "Drop-in embedded database"
license = "MIT"
repository = "https://github.com/vincent-herlemont/native_db"
Expand Down
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ fn main() {
// Run skeptic
skeptic::generate_doc_tests(&["README.md"]);
}
}
}
11 changes: 1 addition & 10 deletions native_db_macro/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<O: ToTokenStream> ToTokenStream for DatabaseKeyDefinition<O> {
}
}

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

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

impl<O: ToTokenStream> DatabaseKeyDefinition<O> {
pub(crate) fn name(&self) -> String {
if let Some(field_name) = &self.field_name {
Expand Down
8 changes: 4 additions & 4 deletions native_db_macro/src/model_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl ModelAttributes {
} else {
panic!(
"Unknown attribute: {}",
meta.path.get_ident().unwrap().to_string()
meta.path.get_ident().unwrap()
);
}
Ok(())
Expand All @@ -46,7 +46,7 @@ impl ModelAttributes {
} else {
panic!(
"Unknown attribute: {}",
meta.path.get_ident().unwrap().to_string()
meta.path.get_ident().unwrap()
);
}
Ok(())
Expand All @@ -55,7 +55,7 @@ impl ModelAttributes {
} else {
panic!(
"Unknown attribute: {}",
meta.path.get_ident().unwrap().to_string()
meta.path.get_ident().unwrap()
);
}
Ok(())
Expand All @@ -71,7 +71,7 @@ impl ModelAttributes {
));
} else if attr.path().is_ident("secondary_key") {
let mut secondary_options = DatabaseSecondaryKeyOptions::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
29 changes: 12 additions & 17 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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.
/// 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.
///
/// # Example
/// ```rust
Expand All @@ -40,7 +40,7 @@ pub struct Database<'a> {

impl Database<'_> {
/// Creates a new read-write transaction.
pub fn rw_transaction(&self) -> Result<RwTransaction> {
pub fn rw_transaction(&self) -> Result<RwTransaction<'_>> {
let rw = self.instance.begin_write()?;
let write_txn = RwTransaction {
watcher: &self.watchers,
Expand All @@ -54,7 +54,7 @@ impl Database<'_> {
}

/// Creates a new read-only transaction.
pub fn r_transaction(&self) -> Result<RTransaction> {
pub fn r_transaction(&self) -> Result<RTransaction<'_>> {
let txn = self.instance.begin_read()?;
let read_txn = RTransaction {
internal: InternalRTransaction {
Expand All @@ -68,7 +68,7 @@ impl Database<'_> {

impl Database<'_> {
/// Watch queries.
pub fn watch(&self) -> Watch {
pub const fn watch(&self) -> Watch<'_> {
Watch {
internal: InternalWatch {
watchers: &self.watchers,
Expand All @@ -82,8 +82,7 @@ impl Database<'_> {
/// If the `id` is not valid anymore, this function will do nothing.
/// If the `id` is valid, the corresponding watcher will be removed.
pub fn unwatch(&self, id: u64) -> Result<()> {
let mut watchers = self.watchers.write().unwrap();
watchers.remove_sender(id);
self.watchers.write().unwrap().remove_sender(id);
Ok(())
}
}
Expand All @@ -92,26 +91,22 @@ impl<'a> Database<'a> {
pub(crate) fn seed_model(&mut self, model_builder: &'a ModelBuilder) -> Result<()> {
let main_table_definition =
redb::TableDefinition::new(model_builder.model.primary_key.unique_table_name.as_str());
let mut primary_table_definition: PrimaryTableDefinition =
let mut primary_table_definition: PrimaryTableDefinition<'_> =
(model_builder, main_table_definition).into();

let rw = self.instance.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(
_ = primary_table_definition.secondary_tables.insert(
secondary_key.clone(),
redb::TableDefinition::new(secondary_key.unique_table_name.as_str()).into(),
);
rw.open_table(
primary_table_definition.secondary_tables[&secondary_key]
.redb
.clone(),
)?;
_ = rw.open_table(primary_table_definition.secondary_tables[&secondary_key].redb)?;
}
rw.commit()?;

self.primary_table_definitions.insert(
_ = self.primary_table_definitions.insert(
model_builder.model.primary_key.unique_table_name.clone(),
primary_table_definition,
);
Expand All @@ -124,7 +119,7 @@ impl<'a> Database<'a> {
let rx = self.instance.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 @@ -146,7 +141,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_table(secondary_table.redb.clone());
let result_table_open = rx.open_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
23 changes: 12 additions & 11 deletions src/database_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::atomic::AtomicU64;
use std::sync::{Arc, RwLock};

/// Builder that allows you to create a [`Database`](crate::Database) instance via [`create`](Self::create) or [`open`](Self::open) etc. and [define](Self::define) models.
#[derive(Default)]
pub struct DatabaseBuilder {
cache_size_bytes: Option<usize>,
models_builder: HashMap<String, ModelBuilder>,
Expand All @@ -16,21 +17,21 @@ impl DatabaseBuilder {
fn new_rdb_builder(&self) -> redb::Builder {
let mut redb_builder = redb::Builder::new();
if let Some(cache_size_bytes) = self.cache_size_bytes {
redb_builder.set_cache_size(cache_size_bytes);
_ = redb_builder.set_cache_size(cache_size_bytes);
}
redb_builder
}

fn init<'a>(&'a self, redb_database: redb::Database) -> Result<Database<'a>> {
fn init(&self, redb_database: redb::Database) -> Result<Database<'_>> {
let mut database = Database {
instance: redb_database,
primary_table_definitions: HashMap::new(),
watchers: Arc::new(RwLock::new(watch::Watchers::new())),
watchers_counter_id: AtomicU64::new(0),
};

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

Ok(database)
Expand All @@ -55,21 +56,21 @@ impl DatabaseBuilder {
/// Creates a new `Db` instance using the given path.
///
/// Similar to [redb::Builder.create(...)](https://docs.rs/redb/latest/redb/struct.Builder.html#method.create)
pub fn create(&self, path: impl AsRef<Path>) -> Result<Database> {
pub fn create(&self, path: impl AsRef<Path>) -> Result<Database<'_>> {
let db = self.new_rdb_builder().create(path)?;
// Ok(Self::from_redb(db))
self.init(db)
}

/// Similar to [redb::Builder::open(...)](https://docs.rs/redb/latest/redb/struct.Builder.html#method.open)
pub fn open(&self, path: impl AsRef<Path>) -> Result<Database> {
pub fn open(&self, path: impl AsRef<Path>) -> Result<Database<'_>> {
let db = self.new_rdb_builder().open(path)?;
// Ok(Self::from_redb(db))
self.init(db)
}

/// Creates a new [`Database`](crate::Database) instance in memory.
pub fn create_in_memory(&self) -> Result<Database> {
pub fn create_in_memory(&self) -> Result<Database<'_>> {
let in_memory_backend = redb::backends::InMemoryBackend::new();
let db = self.new_rdb_builder();
let db = db.create_with_backend(in_memory_backend)?;
Expand Down Expand Up @@ -299,7 +300,7 @@ impl DatabaseBuilder {
}
}

self.models_builder.insert(
_ = self.models_builder.insert(
new_model_builder
.model
.primary_key
Expand All @@ -321,7 +322,7 @@ impl DatabaseBuilder {
}
}

pub(crate) struct ModelBuilder {
pub(crate) model: DatabaseModel,
pub(crate) native_model_options: NativeModelOptions,
pub struct ModelBuilder {
pub model: DatabaseModel,
pub native_model_options: NativeModelOptions,
}
8 changes: 4 additions & 4 deletions src/db_type/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ impl DatabaseInput {
) -> Result<DatabaseKeyValue> {
let secondary_key = self.secondary_keys.get(secondary_key_def).ok_or(
Error::SecondaryKeyDefinitionNotFound {
table: "".to_string(),
table: String::new(),
key: secondary_key_def.unique_table_name.clone(),
},
)?;
let out = if !secondary_key_def.options.unique {
let out = if secondary_key_def.options.unique {
secondary_key.clone()
} else {
match secondary_key {
DatabaseKeyValue::Default(value) => {
DatabaseKeyValue::Default(composite_key(value, &self.primary_key))
Expand All @@ -36,8 +38,6 @@ impl DatabaseInput {
DatabaseKeyValue::Optional(value)
}
}
} else {
secondary_key.clone()
};
Ok(out)
}
Expand Down
Loading

0 comments on commit 7c70779

Please sign in to comment.