aboutsummaryrefslogtreecommitdiff
path: root/include/Database.hpp
blob: 0104a6e021faad4c676f095655694f22e6383411 (plain)
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
#pragma once

#include "types.hpp"
#include "Key.hpp"
#include "StagedObject.hpp"
#include "DataView.hpp"
#include "DatabaseStorage.hpp"
#include <opendht/dhtrunner.h>
#include <vector>
#include <ntp/NtpClient.hpp>
#include <boost/filesystem/path.hpp>

namespace odhtdb
{
    class Group;
    class LocalUser;

    class Database
    {
    public:
        Database(const char *bootstrapNodeAddr, u16 port, boost::filesystem::path storageDir);
        ~Database();

        void seed();
        void create(const Key &key, Group *primaryAdminGroup);
        void add(const Key &key, DataView data, LocalUser *creator);
        void commit();
    private:
        void commitStagedCreateObject(const StagedCreateObject &stagedObject);
        void commitStagedAddObject(const StagedAddObject &stagedObject);
        ntp::NtpTimestamp getSyncedTimestampUtc() const;
        StagedCreateObject deserializeCreateRequest(const std::shared_ptr<dht::Value> &value);
        StagedAddObject deserializeAddRequest(const std::shared_ptr<dht::Value> &value);
        bool listenCreateData(std::shared_ptr<dht::Value> value);
        bool listenAddData(std::shared_ptr<dht::Value> value);
    private:
        dht::DhtRunner node;
        std::vector<StagedCreateObject> stagedCreateObjects;
        std::vector<StagedAddObject> stagedAddObjects;
        DatabaseStorage databaseStorage;
    };
}