add load_or_create
This commit is contained in:
parent
01aa158b03
commit
3fa0c892e0
3 changed files with 31 additions and 7 deletions
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "microdb"
|
||||
version = "0.3.5"
|
||||
version = "0.4.0"
|
||||
edition = "2021"
|
||||
description = "A very small in-program database with cache, disk storage, etc."
|
||||
license = "MIT"
|
||||
|
|
|
|||
15
src/db.rs
15
src/db.rs
|
|
@ -10,9 +10,20 @@ pub struct MicroDB {
|
|||
|
||||
impl MicroDB {
|
||||
/// Loads a database. Can NOT be used to create one.
|
||||
pub fn new<S: ToString>(data: S, alloc: S, cache_period: u128) -> Result<Self, io::Error> {
|
||||
pub fn load<S: ToString>(data: S, alloc: S, cache_period: u128) -> Result<Self, io::Error> {
|
||||
Ok(Self {
|
||||
storage: FAlloc::new(data, alloc, cache_period)?,
|
||||
storage: FAlloc::load(data, alloc, cache_period)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn load_or_create<S: ToString>(
|
||||
data: S,
|
||||
alloc: S,
|
||||
cache_period: u128,
|
||||
block_size: usize,
|
||||
) -> Result<Self, io::Error> {
|
||||
Ok(Self {
|
||||
storage: FAlloc::load_or_create(data, alloc, cache_period, block_size)?,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -358,8 +358,21 @@ impl FAlloc {
|
|||
Ok(Self { inner })
|
||||
}
|
||||
|
||||
pub fn load_or_create<S: ToString>(
|
||||
data: S,
|
||||
alloc: S,
|
||||
cache_period: u128,
|
||||
block_size: usize,
|
||||
) -> Result<Self, io::Error> {
|
||||
if fs::exists(alloc.to_string())? {
|
||||
Self::load(data, alloc, cache_period)
|
||||
} else {
|
||||
Self::create(data, alloc, cache_period, block_size)
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads a database. Can NOT be used to create one.
|
||||
pub fn new<S: ToString>(data: S, alloc: S, cache_period: u128) -> Result<Self, io::Error> {
|
||||
pub fn load<S: ToString>(data: S, alloc: S, cache_period: u128) -> Result<Self, io::Error> {
|
||||
Self::internal_new(
|
||||
File::options()
|
||||
.read(true)
|
||||
|
|
@ -612,18 +625,18 @@ mod test {
|
|||
db.shutdown().unwrap();
|
||||
}
|
||||
fn load() {
|
||||
let db = FAlloc::new("test.dat", "test.alloc", 500).unwrap();
|
||||
let db = FAlloc::load("test.dat", "test.alloc", 500).unwrap();
|
||||
assert_eq!(db.get("test").unwrap().unwrap(), vec![40_u8; 400]);
|
||||
db.shutdown().unwrap();
|
||||
}
|
||||
fn delete_val() {
|
||||
let db = FAlloc::new("test.dat", "test.alloc", 500).unwrap();
|
||||
let db = FAlloc::load("test.dat", "test.alloc", 500).unwrap();
|
||||
db.set("test", vec![0; 0]).unwrap();
|
||||
assert!(db.get("test").unwrap().is_none());
|
||||
db.shutdown().unwrap();
|
||||
}
|
||||
fn create_new_val() {
|
||||
let db = FAlloc::new("test.dat", "test.alloc", 500).unwrap();
|
||||
let db = FAlloc::load("test.dat", "test.alloc", 500).unwrap();
|
||||
db.set("test2", vec![40; 200]).unwrap();
|
||||
assert_eq!(db.get("test2").unwrap().unwrap(), vec![40_u8; 200]);
|
||||
db.sync().unwrap();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue