Search code examples
c++data-storage

Data storage library for C++


I would like to use some tiny C++ library in my code, which would allow to do something like:

DataStore ds;
ds.open("data.bin");
int num=5;
std::string str="some text";
ds.put("key1",num);
ds.put("key2",str);
ds.get("key1");// returns int(5)
ds.get("key2");// returns std::string("some text")

The usage style doesn't have to be the same as that code example, but the principle should remain (get/set value of any type and store it in the file blob). The library should also not be SQL based, nor be an SQL wrapper. What are such libraries and what are their advantages?

EDIT: max 10k keys would be used, with approx. 100bytes data per key, file doesn't need to be portable between computers or OS, file shouldn't be editable with text editor (it looks more professional if it isn't) and doesn't have to be multi-threaded aware.


Solution

  • One option for you is to use BerkeleyDB and its C API, C++ API or C++ STL API:

    BekeleyDB has a small footprint, is fast, mature and robust. Another advantage of BerkeleyDB is that most scripting languages like Python, Perl, etc. have bindings to it so you can manipulate (examine, visualize) the data with them.

    The disadvantage is that all you can store in it is a key-value pair where both key and value are strings (or rather blobs), so you have to convert from C++ data types to strings/blobs.