Question

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.

Was it helpful?

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.

OTHER TIPS

It wouldn't be hard to create a class that does what you describe in a basic way. All you need is a some functions that can read/write keys, a tag for "what type is this" [perhaps combined with the size of the data stored, if we assume the data stored isn't enormous - and I mean a few MB per item or so]. You may find having some sort of "index structure" or "where is the next element" location references help.

There is a small problem with the way your ds.get(std::string) is shown: you can't actually return an int and a std::string from the same function. You may be able to write a function that takes a std::string as a reference, and another that takes an int as a reference, or some such.

It gets more interesting if you need to have many keys - at this point, you probably need some sort of hash or binary tree type organisation to search through the keys. 10k keys is probably not a big deal - if you store them in sorted order it gets easier.

file shouldn't be editable with text editor (it looks more professional if it isn't)

I must say, I don't agree with that. I find text editable files are VERY professional looking. The fact that it's binary is just making things awkward, and harder to deal with should something in the application not work as you wish (e.g. it stored the installation path, you moved it, and it no longer works, and since you can't start it, it won't allow you to edit that configuration).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top