diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-01-28 07:35:37 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-08-18 23:25:12 +0200 |
commit | bd2bd91ac947a7b1f6d097d7efa4b0ab2041d4db (patch) | |
tree | d934d57f39c8ce321688308317476baacd4efd83 /src/Database.cpp | |
parent | 2ca8686eabc3b624c9981509c161b9be6af439cb (diff) |
Add some files...
Diffstat (limited to 'src/Database.cpp')
-rw-r--r-- | src/Database.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/Database.cpp b/src/Database.cpp new file mode 100644 index 0000000..4fa8405 --- /dev/null +++ b/src/Database.cpp @@ -0,0 +1,48 @@ +#include "../include/Database.hpp" +#include <opendht.h> +#include <fmt/format.h> + +using namespace dht; +using namespace std; + +namespace odhtdb +{ + Database::Database(const char *bootstrapNodeAddr, u16 port) + { + node.run(port, dht::crypto::generateIdentity(), true); + fmt::MemoryWriter portStr; + portStr << port; + node.bootstrap(bootstrapNodeAddr, portStr.c_str()); + } + + Database::~Database() + { + node.join(); + } + + void Database::add(const Key &key, DataView data) + { + stagedObjects.emplace_back(StagedObject{ key, data }); + } + + void Database::commit() + { + // TODO: Combine staged objects into one object + for(StagedObject stagedObject : stagedObjects) + { + commitStagedObject(stagedObject); + } + stagedObjects.clear(); + } + + void Database::commitStagedObject(const StagedObject &stagedObject) + { + Value value((const u8*)stagedObject.data.data, stagedObject.data.size); + node.put(stagedObject.key.hashedKey, move(value), [](bool ok) + { + // TODO: Handle failure to put data + if(!ok) + fprintf(stderr, "Failed to put: %s, what to do?\n", "commitStagedObject"); + }, time_point(), false); + } +}
\ No newline at end of file |