Skip to content

Instantly share code, notes, and snippets.

@bw2012

bw2012/kvdb_example.cpp

Last active Feb 20, 2019
Embed
What would you like to do?
kvdb example
#include "kvdb.h"
#include <cstring>
typedef struct TestStr {
int id = 0;
}TestStr;
int main() {
std::string fileName = "d://test_storage.dat";
// create empty storage file
std::unordered_map<int, TestStr> test;
kvdb::KvFile<int, TestStr>::create(fileName, test);
// open storage file
kvdb::KvFile<int, TestStr> testDb;
if (testDb.open(fileName)) {
// save structure
testDb.put(3, TestStr());
// load structure
int key = 3;
std::shared_ptr<TestStr> tmp = testDb[key];
if (tmp == nullptr) {
printf("key %d -> not found", key);
} else {
printf("key %d -> %d", key, tmp->id);
}
} else {
printf("Unable to open file\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment