Open torfinno opened 3 months ago
This is an working code. If you are not particular to space efficiency, using JSON is also a practical solution.
using namespace tkrzw;
// Serializes a value of a basic type into a string.
template
// Deserializes a string into a value of a basic type.
template
// Serializes an array of a basic type into a string.
template
// Deserializes a string into an array of a basic type.
template
int main(int argc, char** argv) {
HashDBM dbm;
const std::vector
// Opens the dtabase.
dbm.Open("mydatabase.tkh", true);
// Stores a serialized double array.
dbm.Set("temperatures", SerializeBasicArray(temperatures));
// Stores a serialized double value.
dbm.Set("average", SerializeBasicValue(average));
// Stores a string as-is.
dbm.Set("description", description);
// Closes the database.
dbm.Close();
// Opens the database again, as read-only one.
dbm.Open("mydatabase.tkh", false);
// Retrieves the serialized double array.
std::string value;
if (dbm.Get("temperatures", &value).IsOK()) {
for (const auto& elem : DeserializeBasicArray
// Retrieves the serialized double value.
value.clear();
if (dbm.Get("average", &value).IsOK()) {
std::cout << DeserializeBasicValue
// Retrieves the string as-is.
value.clear();
if (dbm.Get("description", &value).IsOK()) {
std::cout << value << std::endl;
}
// Closes the database.
dbm.Close();
return 0; }
I pushed some code to make serialization/deserialization tasks easier. Those functions are implemented as SerializeBasicValue/DeerializeBasicValue and SerializeBasicVector/DeerializeBasicVector. And, you can store serialized values into a string map, which can be serialized as a string by SerializeStrMap.
This example code clarifies the usage. https://github.com/estraier/tkrzw/blob/master/example/struct_ex1.cc
Hi, I have been reading up on your 'tkrzw' as a potential replacement for Berkeley DB.
Based on some 'tkrzw' examples, I have tried to store and retrieve double scalars, vectors of doubles and text from a tkrzw-database. Unfortunately, I have not managed to create working code. I am using Linux.
Therefore, could you please give an example on how to save and retrieve from a tkrzw-database: a) double scalars;
b) vector of doubles; c) some text.
As an example, consider some daily temperature data: ~ temperatures = [ 19.1, 20.1, 21.1 ]; // C++: vector temperatures
average = 20.1; // C++: double average;
description = "The average is the sum of temperatures divided by three"; // C++: string description
~
Questions: 1: How to save the above into a tkrzw-database?
Best regards