rkv
rkv copied to clipboard
Using RKV inside struct implementation
The below code fails once it calls commit function. What is it that I am not doing well here. Looks like I am omitting something important
pub struct Datastore {
pub store: SingleStore<SafeModeDatabase>,
pub env: Rkv<SafeModeEnvironment>,
}
impl Datastore {
pub fn new(file_name: &str, db_name: &str) -> Datastore {
let root = Builder::new().prefix(file_name).tempdir().unwrap();
fs::create_dir_all(root.path()).unwrap();
let path = root.path();
let env = Rkv::new::<SafeMode>(path).expect("new succeeded");
let store = env
.open_single(db_name, StoreOptions::create())
.expect("opened");
//let store = env.open_single(db_name, StoreOptions::create()).unwrap();
Datastore {
store,
env: env,
}
// unimplemented!();
}
pub fn insert(&mut self, x: &TxOutpoint, address: &str) {
let mut writer = self.env.write().unwrap();
let tx = bincode::serialize(&x).unwrap();
self.store
.put(&mut writer, &tx, &Value::Str(address))
.unwrap();
writer.commit().unwrap();
}
The call that leads to this issue:
let mut datastore = Datastore::new("foo", "deal");
let dd: [u8; 32] = [
90, 34, 12, 44, 1, 3, 4, 1, 4, 5, 12, 90, 2, 9, 3, 0, 0, 0, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3,
1, 2, 3, 1,
];
let ddx = TxOutpoint {
txid: dd,
index: 90,
};
datastore.insert(&ddx, "offor");
The error message:
thread 'main' panicked at 'called
Result::unwrap()on an
Errvalue: IoError(Os { code: 2, kind: NotFound, message: "No such file or directory" })', src/block/db/kv.rs:56:25 note: run with
RUST_BACKTRACE=1environment variable to display a backtrace
The commit function is at line 56
Hi there! Could you please post the error you're seeing?
@linacambridge I have added error message.
No help yet. I need support.
The error is "No such file or directory", which means that the destination directory does not exist when calling commit
. Can you confirm that the directory exists on disk before you call the commit function? Are you using rkv from multiple threads?