summaryrefslogtreecommitdiffstats
path: root/veslibrary/ves_cpplibrary/src/lib/transport/XDiskQueue.cpp
blob: 5834f97bcfad5cb6b2d9d64fa362ad5486418f6f (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "XDiskQueue.h"
#ifndef SPDLOG_ACTIVE_LEVEL 
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
#endif
#include "spdlog/spdlog.h"
#include "leveldb/env.h"

using namespace std;
using namespace vagt::queue;
using namespace leveldb;

constexpr auto XDiskQueueCompactInterval = 1800;

class XDummyLogger : public leveldb::Logger
{
    void Logv(const char* format, va_list ap) {}
};

vagt::queue::XDiskQueue::XDiskQueue(const std::string & path):db_(nullptr),it_(nullptr),keyId_(0)
{
    compactTime_ = chrono::system_clock::now();

    Options opt;
    opt.create_if_missing = true;
    opt.write_buffer_size = 4 * 1024 * 1024;
    opt.max_open_files = 10;
    opt.info_log = new XDummyLogger;

    opt_.fill_cache = true;

    Status status = DB::Open(opt, path, &db_);
    if (status.ok() && db_ != nullptr)
    {
        SPDLOG_INFO("Disk queue is ready.");
        return;
    }

    SPDLOG_INFO("Repairing disk queue.");
    status = RepairDB(path, opt);
    if (status.ok())
    {
        status = DB::Open(opt, path.c_str(), &db_);
        if (status.ok() && db_ != nullptr)
        {
            SPDLOG_INFO("Disk queue is ready.");
            return;
        }
    }

    SPDLOG_ERROR("Fail to initialize disk queue:{}.", status.ToString());
}

vagt::queue::XDiskQueue::~XDiskQueue()
{
    if (it_)
    {
        delete it_;
        it_ = nullptr;
    }

    if (db_)
    {
        delete db_;
        db_ = nullptr;
    }
}

bool vagt::queue::XDiskQueue::empty()
{
    if (!it_)
    {
        it_ = db_->NewIterator(opt_);
        it_->SeekToFirst();
        if (it_->Valid())
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    if (it_->Valid())
    {
        return false;
    }

    delete it_;
    it_ = db_->NewIterator(opt_);
    it_->SeekToFirst();
    if (it_->Valid())
    {
        return false;
    }
    else
    {
        return true;
    }
}

XErrorCode vagt::queue::XDiskQueue::push(const std::string & val)
{
    auto key = createKey();
    SPDLOG_DEBUG("Push {} to disk queue:{}.", val, key);

    WriteOptions opt;
    opt.sync = false;
    auto status = db_->Put(opt, key, val);
    if (!status.ok())
    {
        SPDLOG_ERROR("Fail to push {} to disk queue:{}.", key, status.ToString());
        return XErrorNok;
    }

    return XErrorOk;
}

void vagt::queue::XDiskQueue::pop()
{
    if (!it_ || !it_->Valid())
    {
        SPDLOG_ERROR("Iterator is not valid.");
        return;
    }

    auto delKey = it_->key().ToString();
    it_->Next();

    WriteOptions wo;
    wo.sync = false;

    auto status = db_->Delete(wo, delKey);
    if (status.ok())
    {
        SPDLOG_DEBUG("Pop {} from disk queue.", delKey);

        tryCompact(delKey);

        return;
    }
    else
    {
        SPDLOG_ERROR("Fail to pop {} from disk queue:{}.", delKey, status.ToString());
        return;
    }
}

std::string vagt::queue::XDiskQueue::front()
{
    if (it_ && it_->Valid())
    {
        return it_->value().ToString();
    }

    SPDLOG_ERROR("Iterator is not valid.");
    return "";
}

std::string vagt::queue::XDiskQueue::createKey()
{
    auto now = chrono::system_clock::now().time_since_epoch().count();
    snprintf(key, sizeof(key), "%020ld_%010ld", now, keyId_.fetch_add(1));
    return key;
}

void vagt::queue::XDiskQueue::tryCompact(const std::string & key)
{
    if (it_->Valid())
    {
        return;
    }

    delete it_;
    it_ = nullptr;

    auto now = chrono::system_clock::now();
    auto duration = chrono::duration_cast<std::chrono::seconds>(now - compactTime_).count();

    if (duration > XDiskQueueCompactInterval &&
        db_ &&
        !key.empty())
    {
        SPDLOG_INFO("Disk queue compaction starting...");
        Slice end(key);
        SPDLOG_INFO("Compact key older than {}.", end.ToString());
        db_->CompactRange(NULL, &end);
        SPDLOG_INFO("Disk queue compaction complete...");

        compactTime_ = now;
    }
}