Can't retreive a string key after insertion from mdbx key value store, after app restarts

51 Views Asked by At

I have been playing with mdbx key value store (Upgraded LMDB). I am able to insert and get value if I run a app. However after rerun the app (commenting insertion code). I am not able to retrive the valeu for the key.

#include <cstring>
#include <exception>
#include <iostream>
#include "mdbx/mdbx.h"
#include "mdbx/mdbx.h++"
#include <string>

using namespace mdbx;


int main(int, char**) {
    std::cout << "Starting mdbx app!"<< std::endl ;

    env::geometry db_geometry;
    
    env_managed::create_parameters create_param;
    create_param.geometry=db_geometry;


    mdbx::env::operate_parameters operator_param;
    operator_param.max_maps=MDBX_MAX_DBI;

    std::string&& db_file_path="./db/my-db";

    mdbx::env_managed env1 = mdbx::env_managed(db_file_path,create_param, operator_param);
    auto tx = env1.start_write();
    MDBX_dbi dbi;
    mdbx_dbi_open(tx,"test1",MDBX_db_flags_t::MDBX_CREATE,&dbi);
    mdbx::map_handle handle(dbi);

    MDBX_val k;
    k.iov_len=sizeof(int);
    std::string j ="k1";
    k.iov_base=&j;

    MDBX_val v;
    auto hw = "v1";
    v.iov_len=std::strlen(hw);
    v.iov_base=(char*)hw;

    tx.insert(handle,k,v);

    std::cout << tx.get(handle,k).string_view() << std::endl;

    tx.commit();

    env1.close();
    
}

Run this app for the first time and it all works fine and prints v1 in the output. However to check if data persisted among the successive runs. I commented the code for insertion tx.insert(handle,k,v); It gives me an error:

terminate called after throwing an instance of 'mdbx::not_found'
  what():  MDBX_NOTFOUND: No matching key/data pair found

(for int keys it works, but for string and char* it seems to give this error).

0

There are 0 best solutions below