Storage

Struct Storage 

Source
pub struct Storage { /* private fields */ }
Expand description

Storage provides a simple interface for interacting with databases

Implementations§

Source§

impl Storage

Source

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(())
}
Source

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(())
}
Source

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(())
}
Source

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(())
}
Source

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(())
}
Source

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(())
}
Source

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(())
}
Source

pub fn truncate<T: Record>(&mut self) -> Result<(), StorageError>

Removes all records in the corresponding type’s database

Source

pub fn drop<T: Record>(&mut self) -> Result<(), StorageError>

Completely removes the database for a specific type

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.