1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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);
}
}
|