rkv icon indicating copy to clipboard operation
rkv copied to clipboard

Using RKV inside struct implementation

Open janus opened this issue 4 years ago • 4 comments

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 'calledResult::unwrap()on anErrvalue: IoError(Os { code: 2, kind: NotFound, message: "No such file or directory" })', src/block/db/kv.rs:56:25 note: run withRUST_BACKTRACE=1environment variable to display a backtrace The commit function is at line 56

janus avatar Sep 20 '20 23:09 janus

Hi there! Could you please post the error you're seeing?

linabutler avatar Sep 21 '20 02:09 linabutler

@linacambridge I have added error message.

janus avatar Sep 21 '20 10:09 janus

No help yet. I need support.

janus avatar Oct 23 '20 11:10 janus

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?

victorporof avatar Dec 02 '20 09:12 victorporof