Add alternative DB interface and implementation
Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
@@ -1,28 +1,141 @@
|
||||
use heromodels::models::{Model, Comment, User};
|
||||
use heromodels::db::{Collection, Db};
|
||||
use heromodels::models::userexample::user::{IsActive, UserName};
|
||||
use heromodels::models::{Comment, Model, User};
|
||||
|
||||
fn main() {
|
||||
let index_db = tst::TST::new("/tmp/ourdb/tst", true).expect("can create index DB");
|
||||
let data_db = ourdb::OurDB::new(ourdb::OurDBConfig {
|
||||
incremental_mode: false,
|
||||
path: "/tmp/ourdb/ourdb".into(),
|
||||
file_size: None,
|
||||
keysize: None,
|
||||
reset: Some(true),
|
||||
})
|
||||
.expect("can create data DB");
|
||||
let db = heromodels::db::hero::OurDB::new(index_db, data_db);
|
||||
|
||||
println!("Hero Models - Basic Usage Example");
|
||||
println!("================================");
|
||||
|
||||
|
||||
// Create a new user using the fluent interface
|
||||
let user = User::new(1)
|
||||
.username("johndoe")
|
||||
.email("john.doe@example.com")
|
||||
.full_name("John Doe")
|
||||
.is_active(false)
|
||||
.build();
|
||||
|
||||
let user2 = User::new(2)
|
||||
.username("janesmith")
|
||||
.email("jane.smith@example.com")
|
||||
.full_name("Jane Smith")
|
||||
.is_active(true)
|
||||
.build();
|
||||
let user3 = User::new(3)
|
||||
.username("willism")
|
||||
.email("willis.masters@example.com")
|
||||
.full_name("Willis Masters")
|
||||
.is_active(true)
|
||||
.build();
|
||||
let user4 = User::new(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");
|
||||
|
||||
// Perform an indexed lookup on the Username
|
||||
let stored_users = db
|
||||
.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.get::<UserName>("johndoe")
|
||||
.expect("can load stored user");
|
||||
|
||||
assert_eq!(stored_users.len(), 1);
|
||||
let stored_user = &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
|
||||
let key = true.to_string();
|
||||
let active_users = db
|
||||
.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.get::<IsActive>(&key)
|
||||
.expect("can load stored users");
|
||||
// We should have 2 active users
|
||||
assert_eq!(active_users.len(), 2);
|
||||
|
||||
// Now remove 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
|
||||
let active_users = db
|
||||
.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.get::<IsActive>(&key)
|
||||
.expect("can load stored users");
|
||||
assert_eq!(active_users.len(), 1);
|
||||
// And verify we still have 2 inactive users
|
||||
let key = false.to_string();
|
||||
let inactive_users = db
|
||||
.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.get::<IsActive>(&key)
|
||||
.expect("can load stored users");
|
||||
assert_eq!(inactive_users.len(), 2);
|
||||
|
||||
println!("Created user: {:?}", user);
|
||||
println!("User ID: {}", user.get_id());
|
||||
println!("User DB Prefix: {}", User::db_prefix());
|
||||
|
||||
|
||||
// Create a comment for the user
|
||||
let comment = Comment::new(1)
|
||||
.user_id(2) // commenter's user ID
|
||||
let comment = Comment::new(5)
|
||||
.user_id(1) // commenter's user ID
|
||||
.content("This is a comment on the user")
|
||||
.build();
|
||||
|
||||
|
||||
db.collection()
|
||||
.expect("can open commen collection")
|
||||
.set(&comment)
|
||||
.expect("can set comment");
|
||||
|
||||
let stored_comment = db
|
||||
.collection::<Comment>()
|
||||
.expect("can open comment collection")
|
||||
.get_by_id(5)
|
||||
.expect("can load stored comment");
|
||||
|
||||
assert!(stored_comment.is_some());
|
||||
let stored_comment = stored_comment.unwrap();
|
||||
|
||||
assert_eq!(comment.get_id(), stored_comment.get_id());
|
||||
assert_eq!(comment.content, stored_comment.content);
|
||||
|
||||
println!("\nCreated comment: {:?}", comment);
|
||||
println!("Comment ID: {}", comment.get_id());
|
||||
println!("Comment DB Prefix: {}", Comment::db_prefix());
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user