feat: Support incremental mode:

- Support incremental mode in heromodels
- Updated the example to refelct the changes
- Updated the tests to reflect the changes
This commit is contained in:
Mahmoud Emad
2025-05-17 11:12:09 +03:00
parent bd4770b99b
commit bde5db0e52
16 changed files with 1074 additions and 136 deletions

View File

@@ -3,6 +3,29 @@ use heromodels::models::userexample::user::user_index::{is_active, username};
use heromodels::models::{Comment, User};
use heromodels_core::Model;
// Helper function to print user details
fn print_user_details(user: &User) {
println!("\n--- User Details ---");
println!("ID: {}", user.get_id());
println!("Username: {}", user.username);
println!("Email: {}", user.email);
println!("Full Name: {}", user.full_name);
println!("Active: {}", user.is_active);
println!("Created At: {}", user.base_data.created_at);
println!("Modified At: {}", user.base_data.modified_at);
println!("Comments: {:?}", user.base_data.comments);
}
// Helper function to print comment details
fn print_comment_details(comment: &Comment) {
println!("\n--- Comment Details ---");
println!("ID: {}", comment.get_id());
println!("User ID: {}", comment.user_id);
println!("Content: {}", comment.content);
println!("Created At: {}", comment.base_data.created_at);
println!("Modified At: {}", comment.base_data.modified_at);
}
fn main() {
// Create a new DB instance in /tmp/ourdb, and reset before every run
let db = heromodels::db::hero::OurDB::new("/tmp/ourdb", true).expect("Can create DB");
@@ -10,50 +33,75 @@ fn main() {
println!("Hero Models - Basic Usage Example");
println!("================================");
// Create a new user using the fluent interface
let user = User::new(1)
// Create users with different ID configurations
// User 1: With explicit ID
let user1 = User::new(Some(1))
.username("johndoe")
.email("john.doe@example.com")
.full_name("John Doe")
.is_active(false)
.build();
let user2 = User::new(2)
// User 2: With auto-generated ID
let user2 = User::new(None)
.username("janesmith")
.email("jane.smith@example.com")
.full_name("Jane Smith")
.is_active(true)
.build();
let user3 = User::new(3)
// User 3: With explicit ID
let user3 = User::new(Some(3))
.username("willism")
.email("willis.masters@example.com")
.full_name("Willis Masters")
.is_active(true)
.build();
let user4 = User::new(4)
// User 4: With explicit ID
let user4 = User::new(Some(4))
.username("carrols")
.email("carrol.smith@example.com")
.full_name("Carrol Smith")
.is_active(false)
.build();
db.collection()
.expect("can open user collection")
.set(&user)
.expect("can set user");
db.collection()
.expect("can open user collection")
.set(&user2)
.expect("can set user");
db.collection()
.expect("can open user collection")
.set(&user3)
.expect("can set user");
db.collection()
.expect("can open user collection")
.set(&user4)
.expect("can set user");
// Save all users to database
db.collection().expect("can open user collection").set(&user1).expect("can set user");
db.collection().expect("can open user collection").set(&user2).expect("can set user");
db.collection().expect("can open user collection").set(&user3).expect("can set user");
db.collection().expect("can open user collection").set(&user4).expect("can set user");
// Perform an indexed lookup on the Username
// Retrieve all users from database
let db_user1 = db.collection::<User>().expect("can open user collection")
.get_by_id(user1.get_id()).expect("can load user").expect("user should exist");
let db_user2 = db.collection::<User>().expect("can open user collection")
.get_by_id(user2.get_id()).expect("can load user").expect("user should exist");
let db_user3 = db.collection::<User>().expect("can open user collection")
.get_by_id(user3.get_id()).expect("can load user").expect("user should exist");
let db_user4 = db.collection::<User>().expect("can open user collection")
.get_by_id(user4.get_id()).expect("can load user").expect("user should exist");
// Print all users retrieved from database
println!("\n--- Users Retrieved from Database ---");
println!("\n1. User with explicit ID (1):");
print_user_details(&db_user1);
println!("\n2. User with auto-generated ID:");
print_user_details(&db_user2);
println!("\n3. User with explicit ID (3):");
print_user_details(&db_user3);
println!("\n4. User with explicit ID (4):");
print_user_details(&db_user4);
// Demonstrate different ways to retrieve users from the database
// 1. Retrieve by username index
println!("\n--- Retrieving Users by Different Methods ---");
println!("\n1. By Username Index:");
let stored_users = db
.collection::<User>()
.expect("can open user collection")
@@ -61,72 +109,105 @@ fn main() {
.expect("can load stored user");
assert_eq!(stored_users.len(), 1);
let stored_user = &stored_users[0];
print_user_details(&stored_users[0]);
assert_eq!(user.username, stored_user.username);
assert_eq!(user.email, stored_user.email);
assert_eq!(user.is_active, stored_user.is_active);
assert_eq!(user.full_name, stored_user.full_name);
// Load all active users using the IsActive field index
// TODO: expand Index type so it defines the type of the key
// 2. Retrieve by active status
println!("\n2. By Active Status (Active = true):");
let active_users = db
.collection::<User>()
.expect("can open user collection")
.get::<is_active, _>(&true)
.expect("can load stored users");
// We should have 2 active users
assert_eq!(active_users.len(), 2);
// Now remove a user
assert_eq!(active_users.len(), 2);
for (i, active_user) in active_users.iter().enumerate() {
print_user_details(active_user);
}
// 3. Delete a user and show the updated results
println!("\n3. After Deleting a User:");
db.collection::<User>()
.expect("can open user collection")
.delete_by_id(active_users[0].get_id())
.expect("can delete existing user");
// Load the active users again, should be 1 left
// Show remaining active users
let active_users = db
.collection::<User>()
.expect("can open user collection")
.get::<is_active, _>(&true)
.expect("can load stored users");
println!(" a. Remaining Active Users:");
assert_eq!(active_users.len(), 1);
// And verify we still have 2 inactive users
for (i, active_user) in active_users.iter().enumerate() {
print_user_details(active_user);
}
// Show inactive users
let inactive_users = db
.collection::<User>()
.expect("can open user collection")
.get::<is_active, _>(&false)
.expect("can load stored users");
assert_eq!(inactive_users.len(), 2);
println!("Created user: {:?}", user);
println!("User ID: {}", user.get_id());
println!(" b. Inactive Users:");
assert_eq!(inactive_users.len(), 2);
for (i, inactive_user) in inactive_users.iter().enumerate() {
print_user_details(inactive_user);
}
println!("\n--- User Model Information ---");
println!("User DB Prefix: {}", User::db_prefix());
// Create a comment for the user
let comment = Comment::new(5)
.user_id(1) // commenter's user ID
// Demonstrate comment creation and association with a user
println!("\n--- Working with Comments ---");
// 1. Create and save a comment
println!("\n1. Creating a Comment:");
let comment = Comment::new(None)
.user_id(db_user1.get_id()) // commenter's user ID
.content("This is a comment on the user")
.build();
db.collection()
.expect("can open commen collection")
.expect("can open comment collection")
.set(&comment)
.expect("can set comment");
let stored_comment = db
// 2. Retrieve the comment from database
let db_comment = db
.collection::<Comment>()
.expect("can open comment collection")
.get_by_id(5)
.expect("can load stored comment");
.get_by_id(comment.get_id())
.expect("can load comment")
.expect("comment should exist");
assert!(stored_comment.is_some());
let stored_comment = stored_comment.unwrap();
println!(" a. Comment Retrieved from Database:");
print_comment_details(&db_comment);
assert_eq!(comment.get_id(), stored_comment.get_id());
assert_eq!(comment.content, stored_comment.content);
// 3. Associate the comment with a user
println!("\n2. Associating Comment with User:");
let mut updated_user = db_user1.clone();
updated_user.base_data.add_comment(db_comment.get_id());
println!("\nCreated comment: {:?}", comment);
println!("Comment ID: {}", comment.get_id());
db.collection::<User>()
.expect("can open user collection")
.set(&updated_user)
.expect("can set updated user");
// 4. Retrieve the updated user
let user_with_comment = db
.collection::<User>()
.expect("can open user collection")
.get_by_id(updated_user.get_id())
.expect("can load user")
.expect("user should exist");
println!(" a. User with Associated Comment:");
print_user_details(&user_with_comment);
println!("\n--- Model Information ---");
println!("User DB Prefix: {}", User::db_prefix());
println!("Comment DB Prefix: {}", Comment::db_prefix());
}

View File

@@ -40,7 +40,7 @@ fn main() {
.description("Brainstorming session for new project features.")
.add_attendee(attendee1.clone())
.add_attendee(attendee3.clone());
let event3_for_calendar2 = Event::new(
"event_gamma".to_string(),
"Client Call",
@@ -50,12 +50,15 @@ fn main() {
// --- Create Calendars ---
// Note: Calendar::new directly returns Calendar, no separate .build() step like the user example.
let calendar1 = Calendar::new(1, "Work Calendar")
// Create a calendar with auto-generated ID
let calendar1 = Calendar::new(None, "Work Calendar")
.description("Calendar for all work-related events.")
.add_event(event1.clone())
.add_event(event2.clone());
let calendar2 = Calendar::new(2, "Personal Calendar")
// Create a calendar with explicit ID
let calendar2 = Calendar::new(Some(2), "Personal Calendar")
.add_event(event3_for_calendar2.clone());
@@ -72,7 +75,7 @@ fn main() {
let stored_calendar1_opt = cal_collection.get_by_id(calendar1.get_id()).expect("can try to load calendar1");
assert!(stored_calendar1_opt.is_some(), "Calendar1 should be found in DB");
let mut stored_calendar1 = stored_calendar1_opt.unwrap();
println!("\nRetrieved calendar1 from DB: Name - '{}', Events count: {}", stored_calendar1.name, stored_calendar1.events.len());
assert_eq!(stored_calendar1.name, "Work Calendar");
assert_eq!(stored_calendar1.events.len(), 2);
@@ -87,7 +90,7 @@ fn main() {
println!("Rescheduling event '{}'...", event_to_update.title);
event_to_update.reschedule(new_start_time, new_end_time)
});
let rescheduled_event = stored_calendar1.events.iter().find(|e| e.id == event_id_to_reschedule)
.expect("Rescheduled event should exist");
assert_eq!(rescheduled_event.start_time, new_start_time);
@@ -123,7 +126,7 @@ fn main() {
println!("\nDeleted calendar2 (ID: {}) from DB.", calendar2.get_id());
println!("Calendar model DB Prefix: {}", Calendar::db_prefix());
println!("\nExample finished. DB stored at {}", db_path);
println!("To clean up, you can manually delete the directory: {}", db_path);
}

View File

@@ -10,9 +10,9 @@ fn main() {
// --- PART 1: ACCOUNTS AND ASSETS ---
println!("=== ACCOUNTS AND ASSETS ===\n");
// Create a new account
// Create a new account with auto-generated ID
let mut account = Account::new(
1, // id
None, // id (auto-generated)
"Main ETH Wallet", // name
1001, // user_id
"My primary Ethereum wallet", // description
@@ -28,8 +28,9 @@ fn main() {
println!("");
// Create some assets
// Asset with auto-generated ID
let eth_asset = Asset::new(
101, // id
None, // id (auto-generated)
"Ethereum", // name
"Native ETH cryptocurrency", // description
1.5, // amount
@@ -38,8 +39,9 @@ fn main() {
18, // decimals
);
// Assets with explicit IDs
let usdc_asset = Asset::new(
102, // id
Some(102), // id
"USDC", // name
"USD Stablecoin on Ethereum", // description
1000.0, // amount
@@ -49,7 +51,7 @@ fn main() {
);
let nft_asset = Asset::new(
103, // id
Some(103), // id
"CryptoPunk #1234", // name
"Rare digital collectible", // description
1.0, // amount
@@ -95,9 +97,9 @@ fn main() {
// --- PART 2: MARKETPLACE LISTINGS ---
println!("\n=== MARKETPLACE LISTINGS ===\n");
// Create a fixed price listing
// Create a fixed price listing with auto-generated ID
let mut fixed_price_listing = Listing::new(
201, // id
None, // id (auto-generated)
"1000 USDC for Sale", // title
"Selling 1000 USDC tokens at fixed price", // description
"102", // asset_id (referencing the USDC asset)
@@ -131,9 +133,9 @@ fn main() {
Err(e) => println!("Error completing sale: {}", e),
}
// Create an auction listing for the NFT
// Create an auction listing for the NFT with explicit ID
let mut auction_listing = Listing::new(
202, // id
Some(202), // id (explicit)
"CryptoPunk #1234 Auction", // title
"Rare CryptoPunk NFT for auction", // description
"103", // asset_id (referencing the NFT asset)
@@ -176,7 +178,7 @@ fn main() {
// Add bids to the auction
println!("Adding Bids to Auction:");
// Using clone() to avoid ownership issues with match expressions
match auction_listing.clone().add_bid(bid1) {
Ok(updated_listing) => {
@@ -185,7 +187,7 @@ fn main() {
},
Err(e) => println!("Error adding bid: {}", e),
}
match auction_listing.clone().add_bid(bid2) {
Ok(updated_listing) => {
auction_listing = updated_listing;
@@ -193,7 +195,7 @@ fn main() {
},
Err(e) => println!("Error adding bid: {}", e),
}
match auction_listing.clone().add_bid(bid3) {
Ok(updated_listing) => {
auction_listing = updated_listing;
@@ -204,14 +206,14 @@ fn main() {
println!("\nCurrent Auction Status:");
println!("Current Price: {} {}", auction_listing.price, auction_listing.currency);
if let Some(highest_bid) = auction_listing.highest_bid() {
println!("Highest Bid: {} {} from User {}",
highest_bid.amount,
highest_bid.currency,
println!("Highest Bid: {} {} from User {}",
highest_bid.amount,
highest_bid.currency,
highest_bid.bidder_id);
}
println!("Total Bids: {}", auction_listing.bids.len());
println!("");
@@ -224,13 +226,13 @@ fn main() {
println!("Winner: User {}", auction_listing.buyer_id.as_ref().unwrap());
println!("Winning Bid: {} {}", auction_listing.sale_price.as_ref().unwrap(), auction_listing.currency);
println!("");
println!("Final Bid Statuses:");
for bid in &auction_listing.bids {
println!("- User {}: {} {} (Status: {:?})",
bid.bidder_id,
bid.amount,
bid.currency,
println!("- User {}: {} {} (Status: {:?})",
bid.bidder_id,
bid.amount,
bid.currency,
bid.status);
}
println!("");
@@ -238,9 +240,9 @@ fn main() {
Err(e) => println!("Error completing auction: {}", e),
}
// Create an exchange listing
// Create an exchange listing with auto-generated ID
let exchange_listing = Listing::new(
203, // id
None, // id (auto-generated)
"ETH for BTC Exchange", // title
"Looking to exchange ETH for BTC", // description
"101", // asset_id (referencing the ETH asset)
@@ -262,9 +264,9 @@ fn main() {
// --- PART 3: DEMONSTRATING EDGE CASES ---
println!("\n=== EDGE CASES AND VALIDATIONS ===\n");
// Create a new auction listing for edge case testing
// Create a new auction listing for edge case testing with explicit ID
let test_auction = Listing::new(
205, // id
Some(205), // id (explicit)
"Test Auction", // title
"For testing edge cases", // description
"101", // asset_id
@@ -277,7 +279,7 @@ fn main() {
vec![], // tags
None::<String>, // image_url
);
// Try to add a bid that's too low
let low_bid = Bid::new(
test_auction.base_data.id.to_string(), // listing_id
@@ -285,7 +287,7 @@ fn main() {
5.0, // amount (lower than starting price)
"ETH", // currency
);
println!("Attempting to add a bid that's too low (5.0 ETH):");
match test_auction.add_bid(low_bid) {
Ok(_) => println!("Bid accepted (This shouldn't happen)"),
@@ -301,9 +303,9 @@ fn main() {
}
println!("");
// Create a listing that will expire
// Create a listing that will expire with auto-generated ID
let mut expiring_listing = Listing::new(
204, // id
None, // id (auto-generated)
"About to Expire", // title
"This listing will expire immediately", // description
"101", // asset_id
@@ -316,10 +318,10 @@ fn main() {
vec![], // tags
None::<String>, // image_url
);
println!("Created Expiring Listing: '{}' (ID: {})", expiring_listing.title, expiring_listing.base_data.id);
println!("Initial Status: {:?}", expiring_listing.status);
// Check expiration
expiring_listing = expiring_listing.check_expiration();
println!("After Checking Expiration: {:?}", expiring_listing.status);

View File

@@ -6,9 +6,9 @@ use heromodels::models::governance::{Proposal, ProposalStatus, VoteEventStatus};
fn main() {
println!("Governance Proposal Model Example\n");
// Create a new proposal
// Create a new proposal with auto-generated ID
let mut proposal = Proposal::new(
1, // id
None, // id (auto-generated)
"user_creator_123", // creator_id
"Community Fund Allocation for Q3", // title
"Proposal to allocate funds for community projects in the third quarter.", // description
@@ -33,18 +33,18 @@ fn main() {
// Simulate casting votes
println!("Simulating Votes...");
// User 1 votes for 'Approve Allocation' with 100 shares
proposal = proposal.cast_vote(101, 1, 1, 100);
// User 2 votes for 'Reject Allocation' with 50 shares
proposal = proposal.cast_vote(102, 2, 2, 50);
// User 3 votes for 'Approve Allocation' with 75 shares
proposal = proposal.cast_vote(103, 3, 1, 75);
// User 4 abstains with 20 shares
proposal = proposal.cast_vote(104, 4, 3, 20);
// User 1 votes for 'Approve Allocation' with 100 shares (with explicit ballot ID)
proposal = proposal.cast_vote(Some(101), 1, 1, 100);
// User 2 votes for 'Reject Allocation' with 50 shares (with explicit ballot ID)
proposal = proposal.cast_vote(Some(102), 2, 2, 50);
// User 3 votes for 'Approve Allocation' with 75 shares (with auto-generated ballot ID)
proposal = proposal.cast_vote(None, 3, 1, 75);
// User 4 abstains with 20 shares (with auto-generated ballot ID)
proposal = proposal.cast_vote(None, 4, 3, 20);
// User 5 attempts to vote for a non-existent option (should be handled gracefully)
proposal = proposal.cast_vote(105, 5, 99, 10);
proposal = proposal.cast_vote(Some(105), 5, 99, 10);
// User 1 tries to vote again (not explicitly prevented by current model, but could be a future enhancement)
// proposal = proposal.cast_vote(106, 1, 1, 10);
// proposal = proposal.cast_vote(Some(106), 1, 1, 10);
println!("\nVote Counts After Simulation:");
for option in &proposal.options {
@@ -53,7 +53,7 @@ fn main() {
println!("\nBallots Cast:");
for ballot in &proposal.ballots {
println!("- Ballot ID: {}, User ID: {}, Option ID: {}, Shares: {}",
println!("- Ballot ID: {}, User ID: {}, Option ID: {}, Shares: {}",
ballot.base_data.id, ballot.user_id, ballot.vote_option_id, ballot.shares_count);
}
println!("");
@@ -68,7 +68,7 @@ fn main() {
// Attempt to cast a vote after closing (should be handled)
println!("\nAttempting to cast vote after voting is closed...");
proposal = proposal.cast_vote(107, 6, 1, 25);
proposal = proposal.cast_vote(None, 6, 1, 25);
// Final proposal state
println!("\nFinal Proposal State:");
@@ -83,24 +83,24 @@ fn main() {
// Example of a private proposal (not fully implemented in cast_vote eligibility yet)
let mut private_proposal = Proposal::new(
2,
"user_admin_001",
"Internal Team Restructure Vote",
"Vote on proposed internal team changes.",
Utc::now(),
Some(2), // explicit ID
"user_admin_001",
"Internal Team Restructure Vote",
"Vote on proposed internal team changes.",
Utc::now(),
Utc::now() + Duration::days(7)
);
private_proposal.private_group = Some(vec![10, 20, 30]); // Only users 10, 20, 30 can vote
private_proposal = private_proposal.add_option(1, "Accept Restructure");
private_proposal = private_proposal.add_option(2, "Reject Restructure");
println!("\nCreated Private Proposal: '{}'", private_proposal.title);
println!("Eligible Voters (Group): {:?}", private_proposal.private_group);
// User 10 (eligible) votes
private_proposal = private_proposal.cast_vote(201, 10, 1, 100);
// User 40 (ineligible) tries to vote
private_proposal = private_proposal.cast_vote(202, 40, 1, 50);
// User 10 (eligible) votes with explicit ballot ID
private_proposal = private_proposal.cast_vote(Some(201), 10, 1, 100);
// User 40 (ineligible) tries to vote with auto-generated ballot ID
private_proposal = private_proposal.cast_vote(None, 40, 1, 50);
println!("Private Proposal Vote Counts:");
for option in &private_proposal.options {
println!(" - {}: {} (Votes: {})", option.id, option.text, option.count);