use redb::{Database, Error, ReadableTable, TableDefinition};
const TABLE: TableDefinition<&str, u64> = TableDefinition::new("my_data");
fn main() -> Result<(), Error> {
let db = Database::create("my_db.redb")?;
let write_txn = db.begin_write()?;
{
let mut table = write_txn.open_table(TABLE)?;
table.insert("my_key", &123)?;
}
write_txn.commit()?;
let read_txn = db.begin_read()?;
let table = read_txn.open_table(TABLE)?;
assert_eq!(table.get("my_key")?.unwrap().value(), 123);
Ok(())
}
lets say I had a structure which is going to be used as a value, so I want to write to it and I want to find a particular value from one of the structure's fields, is this possible?
No idea if there is a way around implementing Value by hand/creating your own macro(s) to implement it for you. I couldn't find a derive macro that implements Value for you in the redb crate, but there might be some out there. The gist of the implementation is that you need to serialise and deserialise your type to and from bytes. I assume you could just implement serde's Serialize and Deserialize for your type and forward the Value implementation to a (de)serializer of your choice. Then a simple declarative macro would suffice to implement Value.
Like I said, because redb needs to serialise the data to some binary format it saves inside the database structure. This is not different from any other database out there, you need some sort of logic that translates the stored data to and from your types.