pub struct Storage { /* private fields */ }Expand description
Storage provides a simple interface for interacting with databases
Implementations§
Source§impl Storage
impl Storage
Sourcepub fn new<P: Into<PathBuf>>(path: P) -> Result<Storage, StorageError>
pub fn new<P: Into<PathBuf>>(path: P) -> Result<Storage, StorageError>
Creates or Opens a storage directory for managing databases.
LMDB storage expects path to be a directory.
If the path does not exist it will be created.
§Arguments
path- The path where the database should be created / opened
§Examples
use nostalgia::{Storage, StorageError};
fn main() -> Result<(), StorageError> {
// Into trait allows for str argument
let a = Storage::new("/tmp/db")?;
// Also allows for a std::string::String
let b = Storage::new(String::from("/tmp/db2"))?;
// PathBuf's also work
let c = Storage::new(std::env::temp_dir())?;
Ok(())
}
Sourcepub fn save<T: Record>(&mut self, record: &T) -> Result<(), StorageError>
pub fn save<T: Record>(&mut self, record: &T) -> Result<(), StorageError>
Serializes and Saves a record in one of the databases contained in storage.
Input should implement the Record trait. The database the record is saved to and the key used is configured using that trait.
§Arguments
record- A type that implements the Record trait.
§Examples
#[macro_use]
extern crate nostalgia_derive;
use nostalgia::{Storage, StorageError, Record, Key};
use serde::{Serialize, Deserialize};
#[derive(Storable, Serialize, Deserialize)]
#[key = "id"]
struct Place {
id: u32,
name: std::string::String
}
fn main() -> Result<(), StorageError> {
let mut storage = Storage::new("/tmp/db")?;
let place = Place { id: 1, name: "Vienna".to_string() };
storage.save(&place)?;
Ok(())
}Sourcepub fn save_batch<T: Record>(
&mut self,
records: Vec<T>,
) -> Result<(), StorageError>
pub fn save_batch<T: Record>( &mut self, records: Vec<T>, ) -> Result<(), StorageError>
Saves a group of records to the internal type’s database
§Arguments
records- A Vec that contains objects that implement Record trait
§Examples
#[macro_use]
extern crate nostalgia_derive;
use nostalgia::{Storage, Record, StorageError, Key};
use serde::{Serialize, Deserialize};
#[derive(Storable, Serialize, Deserialize)]
#[key = "id"]
struct Place {
id: u32,
name: std::string::String
}
fn main() -> Result<(), StorageError> {
let mut storage = Storage::new("/tmp/db")?;
let records = vec![
Place { id: 1, name: "Vienna".to_string() },
Place { id: 2, name: "Paris".to_string() },
Place { id: 3, name: "Istanbul".to_string() },
Place { id: 4, name: "London".to_string() },
];
storage.save_batch(records)?;
Ok(())
}Sourcepub fn get<T: Record, K: Into<T::Key>>(
&mut self,
key: K,
) -> Result<Option<T>, StorageError>
pub fn get<T: Record, K: Into<T::Key>>( &mut self, key: K, ) -> Result<Option<T>, StorageError>
Retrieves a record from the database
§Arguments
key- A Vec of usigned 8bit integers representing the key. Will make this more sugar-y eventually
§Examples
#[macro_use]
extern crate nostalgia_derive;
use nostalgia::{Storage, Record, StorageError, Key};
use serde::{Serialize, Deserialize};
#[derive(Storable, Serialize, Deserialize)]
#[key = "id"]
struct Place {
id: u32,
name: std::string::String
}
fn main() -> Result<(), StorageError> {
let mut storage = Storage::new("/tmp/db")?;
let paris: Place = storage.get(2)
.expect("Error fetching")
.expect("Empty record");
assert_eq!("Paris", paris.name);
Ok(())
}Sourcepub fn delete<T: Record>(&mut self, record: &T) -> Result<(), StorageError>
pub fn delete<T: Record>(&mut self, record: &T) -> Result<(), StorageError>
Deletes a record from the database
§Arguments
record- A type that implements the Record trait.
§Examples
#[macro_use]
extern crate nostalgia_derive;
use nostalgia::{Storage, Record, Key, StorageError};
use serde::{Serialize, Deserialize};
#[derive(Storable, Serialize, Deserialize)]
#[key = "id"]
struct Place {
id: u32,
name: std::string::String
}
fn main() -> Result<(), StorageError> {
let mut storage = Storage::new("/tmp/db")?;
let place = Place { id: 1, name: "Vienna".to_string() };
storage.save(&place)?;
storage.delete(&place)?;
Ok(())
}Sourcepub fn query<T: Record>(&mut self) -> Result<RoQuery<'_, T>, StorageError>
pub fn query<T: Record>(&mut self) -> Result<RoQuery<'_, T>, StorageError>
Returns an RoQuery object that allows you to Iterate over all records in a database.
§Examples
#[macro_use]
extern crate nostalgia_derive;
use nostalgia::{Storage, Record, Key, StorageError};
use serde::{Serialize, Deserialize};
#[derive(Storable, Serialize, Deserialize)]
#[key = "id"]
struct Place {
id: u32,
name: std::string::String
}
fn main() -> Result<(), StorageError> {
let mut storage = Storage::new("/tmp/db")?;
let query = storage.query::<Place>()?;
for place in query {
println!("{}", place.name);
}
Ok(())
}Sourcepub fn find<T: Record>(
&mut self,
p: &dyn Fn(&T) -> bool,
) -> Result<Option<T>, StorageError>
pub fn find<T: Record>( &mut self, p: &dyn Fn(&T) -> bool, ) -> Result<Option<T>, StorageError>
Returns the first record that matches a predicate
§Examples
#[macro_use]
extern crate nostalgia_derive;
use nostalgia::{Storage, Record, Key, StorageError};
use serde::{Serialize, Deserialize};
#[derive(Storable, Serialize, Deserialize)]
#[key = "id"]
struct Place {
id: u32,
name: std::string::String
}
fn main() -> Result<(), StorageError> {
let mut storage = Storage::new("/tmp/db")?;
let place = storage.find::<Place>(&|p| p.name == "Istanbul")?;
if let Some(istanbul) = place {
assert_eq!(istanbul.name, "Istanbul");
} else {
assert_ne!(0, 0, "Could not find record");
}
Ok(())
}Auto Trait Implementations§
impl !Freeze for Storage
impl RefUnwindSafe for Storage
impl Send for Storage
impl Sync for Storage
impl Unpin for Storage
impl UnwindSafe for Storage
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more