Skip to content

Commit

Permalink
refactor: macro
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-herlemont committed Nov 1, 2023
1 parent ca9d6cc commit b06ff9e
Show file tree
Hide file tree
Showing 39 changed files with 1,541 additions and 226 deletions.
591 changes: 591 additions & 0 deletions 05_update.expanded.1.rs

Large diffs are not rendered by default.

592 changes: 592 additions & 0 deletions 05_update.expanded.rs

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bincode = { version = "2.0.0-rc.3", features = ["serde"] }

# Optional tokio support
tokio = { version = "1", features = ["sync"], optional = true }
native_model_dep = { package = "native_model", path = "../native_model", optional = true }
native_model = { path = "../native_model", optional = true }

[dev-dependencies]
assert_fs = "1.0"
Expand All @@ -36,7 +36,6 @@ tokio = { version = "1.33", features = ["test-util","macros"] }

[features]
default = []
native_model = ["native_model_dep", "struct_db_macro/native_model"]

[build-dependencies]
skeptic = "0.13"
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ use struct_db::*;

#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[struct_db(
fn_primary_key(p_key), // required
fn_secondary_key(s_key), // optional
// ... other fn_secondary_key ...
pk = p_key, // required
gk = s_key, // optional
// ... other gk ...
)]
struct Data(u32, String);

Expand Down
6 changes: 5 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ test_no_default:
test_default:
cargo test

test_all: test_no_default test_default
test_all: test_no_default test_default


expand test_file_name:
cargo expand --test {{test_file_name}} | save --raw {{test_file_name}}.expanded.rs
10 changes: 5 additions & 5 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Db {
/// use struct_db::*;
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
/// #[struct_db(fn_primary_key(p_key))]
/// #[struct_db(pk = p_key)]
/// struct Data(u32);
/// impl Data {pub fn p_key(&self) -> Vec<u8> {self.0.to_be_bytes().to_vec()}}
///
Expand Down Expand Up @@ -189,7 +189,7 @@ impl Db {
}

pub fn redb_stats(&self) -> Result<Stats> {
use redb::{ReadableTable, TableHandle};
use redb::ReadableTable;
let rx = self.instance.begin_read()?;
let mut stats_tables = vec![];
for table in rx.list_tables()? {
Expand All @@ -215,7 +215,7 @@ impl Db {
/// use struct_db::*;
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
/// #[struct_db(fn_primary_key(p_key))]
/// #[struct_db(pk = p_key)]
/// struct Data(u32);
/// impl Data {pub fn p_key(&self) -> Vec<u8> {self.0.to_be_bytes().to_vec()}}
///
Expand Down Expand Up @@ -274,7 +274,7 @@ impl Db {
/// use struct_db::*;
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
/// #[struct_db(fn_primary_key(p_key))]
/// #[struct_db(pk = p_key)]
/// struct Data(u32);
/// impl Data {pub fn p_key(&self) -> Vec<u8> {self.0.to_be_bytes().to_vec()}}
///
Expand Down Expand Up @@ -356,7 +356,7 @@ impl Db {
/// use struct_db::*;
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
/// #[struct_db(fn_primary_key(p_key))]
/// #[struct_db(pk = p_key)]
/// struct Data(u32);
/// impl Data {pub fn p_key(&self) -> Vec<u8> {self.0.to_be_bytes().to_vec()}}
///
Expand Down
16 changes: 8 additions & 8 deletions src/item.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#[cfg(not(feature = "native_model"))]
pub trait SDBItem: Sized {
fn struct_db_schema() -> crate::Schema;
fn struct_db_primary_key(&self) -> Vec<u8>;
fn struct_db_pk(&self) -> Vec<u8>;

// Return map of secondary table name and the value of the key
fn struct_db_keys(&self) -> std::collections::HashMap<&'static str, Vec<u8>>;
fn struct_db_gks(&self) -> std::collections::HashMap<&'static str, Vec<u8>>;
fn struct_db_bincode_encode_to_vec(&self) -> Vec<u8>;
fn struct_db_bincode_decode_from_slice(slice: &[u8]) -> Self;

fn to_item(&self) -> Item {
Item {
primary_key: self.struct_db_primary_key(),
secondary_keys: self.struct_db_keys(),
primary_key: self.struct_db_pk(),
secondary_keys: self.struct_db_gks(),
value: self.struct_db_bincode_encode_to_vec(),
}
}
Expand All @@ -20,15 +20,15 @@ pub trait SDBItem: Sized {
#[cfg(feature = "native_model")]
pub trait SDBItem: Sized + native_model::Model {
fn struct_db_schema() -> crate::Schema;
fn struct_db_primary_key(&self) -> Vec<u8>;
fn struct_db_keys(&self) -> std::collections::HashMap<&'static str, Vec<u8>>;
fn struct_db_pk(&self) -> Vec<u8>;
fn struct_db_gks(&self) -> std::collections::HashMap<&'static str, Vec<u8>>;
fn struct_db_bincode_encode_to_vec(&self) -> Vec<u8>;
fn struct_db_bincode_decode_from_slice(slice: &[u8]) -> Self;

fn to_item(&self) -> Item {
Item {
primary_key: self.struct_db_primary_key(),
secondary_keys: self.struct_db_keys(),
primary_key: self.struct_db_pk(),
secondary_keys: self.struct_db_gks(),
value: self.struct_db_bincode_encode_to_vec(),
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
//!
//! Use macro `struct_db`:
//!
//! - required: `fn_primary_key(<function name>)` associates a function of the struct that generates the **primary key** of the struct. Allows **only one** `fn_primary_key` declaration.
//! - optional: `fn_secondary_key(<function name>)` associates a function of the struct that generates a **secondary key** of the struct. Allows **multiple** `fn_secondary_key` declarations.
//! - required: `pk(<function name>)` associates a function of the struct that generates the **primary key** of the struct. Allows **only one** `pk` declaration.
//! - optional: `gk(<function name>)` associates a function of the struct that generates a **secondary key** of the struct. Allows **multiple** `gk` declarations.
//!
//! `struct_db` generates an enum `<your_type>` with the suffix `Key` that contains all the secondary keys like: E.g. `<your_type>Key::<your_secondary_key>` more details [`here`](crate::ReadableTable::secondary_get).
//!
Expand Down Expand Up @@ -55,9 +55,9 @@
//!
//! #[derive(Serialize, Deserialize, PartialEq, Debug)]
//! #[struct_db(
//! fn_primary_key(p_key), // required
//! fn_secondary_key(s_key), // optional
//! // ... other fn_secondary_key ...
//! pk = p_key, // required
//! gk = s_key, // optional
//! // ... other gk ...
//! )]
//! struct Data(u32, String);
//!
Expand Down Expand Up @@ -119,6 +119,7 @@ mod db;
mod item;
mod iterator;
mod operation;
mod query;
mod readable_table;
mod readonly_tables;
mod readonly_transaction;
Expand Down
11 changes: 11 additions & 0 deletions src/query/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
enum QueryGet {
Pk(String),
Dk(String, String),
Gk(String),
}

enum QueryIter {
Pk(String),
Dk(String),
Gk(String),
}
8 changes: 4 additions & 4 deletions src/readable_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub trait ReadableTable<'db, 'txn> {
/// use struct_db::*;
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
/// #[struct_db(fn_primary_key(p_key))]
/// #[struct_db(pk = p_key)]
/// struct Data(u32);
/// impl Data {pub fn p_key(&self) -> Vec<u8> {self.0.to_be_bytes().to_vec()}}
///
Expand Down Expand Up @@ -86,7 +86,7 @@ pub trait ReadableTable<'db, 'txn> {
/// use struct_db::*;
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
/// #[struct_db(fn_primary_key(p_key))]
/// #[struct_db(pk = p_key)]
/// struct Data(u32);
/// impl Data{ pub fn p_key(&self) -> Vec<u8> {self.0.to_be_bytes().to_vec()} }
///
Expand Down Expand Up @@ -191,7 +191,7 @@ pub trait ReadableTable<'db, 'txn> {
/// use struct_db::*;
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
/// #[struct_db(fn_primary_key(p_key),fn_secondary_key(s_key))]
/// #[struct_db(pk = p_key,gk = s_key)]
/// struct Data(u32, String);
/// impl Data {
/// pub fn p_key(&self) -> Vec<u8> {self.0.to_be_bytes().to_vec()}
Expand Down Expand Up @@ -337,7 +337,7 @@ pub trait ReadableTable<'db, 'txn> {
/// use struct_db::*;
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
/// #[struct_db(fn_primary_key(p_key))]
/// #[struct_db(pk = p_key)]
/// struct Data(u32);
/// impl Data{ pub fn p_key(&self) -> Vec<u8> {self.0.to_be_bytes().to_vec()} }
///
Expand Down
14 changes: 7 additions & 7 deletions src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<'db, 'txn> Tables<'db, 'txn> {
/// use struct_db::*;
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
/// #[struct_db(fn_primary_key(p_key),fn_secondary_key(s_key))]
/// #[struct_db(pk = p_key,gk = s_key)]
/// struct Data(u32, String);
/// impl Data {
/// pub fn p_key(&self) -> Vec<u8> {self.0.to_be_bytes().to_vec()}
Expand Down Expand Up @@ -132,7 +132,7 @@ impl<'db, 'txn> Tables<'db, 'txn> {
/// use struct_db::*;
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
/// #[struct_db(fn_primary_key(p_key))]
/// #[struct_db(pk = p_key)]
/// struct Data(u32);
/// impl Data{ pub fn p_key(&self) -> Vec<u8> {self.0.to_be_bytes().to_vec()} }
///
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<'db, 'txn> Tables<'db, 'txn> {
/// use struct_db::*;
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
/// #[struct_db(fn_primary_key(p_key))]
/// #[struct_db(pk = p_key)]
/// struct Data(u32);
/// impl Data{ pub fn p_key(&self) -> Vec<u8> {self.0.to_be_bytes().to_vec()} }
///
Expand Down Expand Up @@ -237,8 +237,8 @@ impl<'db, 'txn> Tables<'db, 'txn> {
let schema = T::struct_db_schema();
let table_name = schema.table_name;

let primary_key = item.struct_db_primary_key();
let keys = item.struct_db_keys();
let primary_key = item.struct_db_pk();
let keys = item.struct_db_gks();
let value = item.struct_db_bincode_encode_to_vec();
{
self.open_primary_table(txn, table_name)?;
Expand Down Expand Up @@ -270,7 +270,7 @@ impl<'db, 'txn> Tables<'db, 'txn> {
/// type Data = DataV2;
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
/// #[struct_db(fn_primary_key(p_key))]
/// #[struct_db(pk = p_key)]
/// struct DataV1(u32);
///
/// impl DataV1 {
Expand All @@ -280,7 +280,7 @@ impl<'db, 'txn> Tables<'db, 'txn> {
/// }
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
/// #[struct_db(fn_primary_key(p_key))]
/// #[struct_db(pk = p_key)]
/// struct DataV2(String);
///
/// impl DataV2 {
Expand Down
4 changes: 2 additions & 2 deletions struct_db_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ proc-macro = true
[dependencies]
syn = { version = "2.0", features = ["full"] }
quote = "1.0"
proc-macro2 = "1.0"

[features]
default = []
native_model = []
default = []
2 changes: 2 additions & 0 deletions struct_db_macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extern crate proc_macro;

mod model_attributes;
mod model_struct_db;
mod struct_db;

use proc_macro::TokenStream;
Expand Down
39 changes: 39 additions & 0 deletions struct_db_macro/src/model_attributes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::collections::HashSet;
use syn::meta::ParseNestedMeta;
use syn::parse::Result;
use syn::Ident;

#[derive(Default, Clone)]
pub(crate) struct ModelAttributes {
pk_function_name: Option<Ident>, // Primary Key Function Name
gk_function_names: HashSet<Ident>, // Generic Secondary Key Function Names // gk ou gsk
// TODO: Derived Secondary Key Function Names: dk ou dsk
}

impl ModelAttributes {
pub(crate) fn pk(&self) -> Ident {
self.pk_function_name.clone().expect("pk is required")
}

pub(crate) fn pk_name(&self) -> String {
self.pk().to_string().to_lowercase()
}

pub(crate) fn gk_function_names(&self) -> HashSet<Ident> {
self.gk_function_names.clone()
}

pub(crate) fn parse(&mut self, meta: ParseNestedMeta) -> Result<()> {
if meta.path.is_ident("pk") {
self.pk_function_name = Some(meta.value()?.parse()?);
} else if meta.path.is_ident("gk") {
self.gk_function_names.insert(meta.value()?.parse()?);
} else {
panic!(
"Unknown attribute: {}",
meta.path.get_ident().unwrap().to_string()
);
}
Ok(())
}
}
Loading

0 comments on commit b06ff9e

Please sign in to comment.