aboutsummaryrefslogtreecommitdiff
path: root/src/Database.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Database.cpp')
-rw-r--r--src/Database.cpp48
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