Skip to content

Using RKV inside struct implementation #205

@janus

Description

@janus

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions