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
|
#include "database.hpp"
#include <functional>
#include "esp_log.h"
#include "ff.h"
#include "leveldb/cache.h"
#include "db_task.hpp"
#include "env_esp.hpp"
#include "file_gatherer.hpp"
#include "leveldb/iterator.h"
#include "leveldb/options.h"
#include "leveldb/slice.h"
#include "result.hpp"
#include "tag_processor.hpp"
namespace database {
static SingletonEnv<leveldb::EspEnv> sEnv;
static const char* kTag = "DB";
static std::atomic<bool> sIsDbOpen(false);
auto Database::Open() -> cpp::result<Database*, DatabaseError> {
// TODO(jacqueline): Why isn't compare_and_exchange_* available?
if (sIsDbOpen.exchange(true)) {
return cpp::fail(DatabaseError::ALREADY_OPEN);
}
if (!StartDbTask()) {
return cpp::fail(DatabaseError::ALREADY_OPEN);
}
return RunOnDbTask<cpp::result<Database*, DatabaseError>>(
[]() -> cpp::result<Database*, DatabaseError> {
leveldb::DB* db;
leveldb::Cache* cache = leveldb::NewLRUCache(24 * 1024);
leveldb::Options options;
options.env = sEnv.env();
options.create_if_missing = true;
options.write_buffer_size = 48 * 1024;
options.max_file_size = 32;
options.block_cache = cache;
options.block_size = 512;
auto status = leveldb::DB::Open(options, "/.db", &db);
if (!status.ok()) {
delete cache;
ESP_LOGE(kTag, "failed to open db, status %s",
status.ToString().c_str());
return cpp::fail(FAILED_TO_OPEN);
}
ESP_LOGI(kTag, "Database opened successfully");
return new Database(db, cache);
})
.get();
}
Database::Database(leveldb::DB* db, leveldb::Cache* cache)
: db_(db), cache_(cache) {}
Database::~Database() {
QuitDbTask();
sIsDbOpen.store(false);
}
template <typename Parser>
auto IterateAndParse(leveldb::Iterator* it, std::size_t limit, Parser p)
-> void {
for (int i = 0; i < limit; i++) {
if (!it->Valid()) {
break;
}
std::invoke(p, it->key(), it->value());
it->Next();
}
}
auto Database::Populate() -> std::future<void> {
return RunOnDbTask<void>([&]() -> void {
leveldb::WriteOptions opt;
opt.sync = true;
FindFiles("", [&](const std::string& path) {
ESP_LOGI(kTag, "considering %s", path.c_str());
FileInfo info;
if (GetInfo(path, &info)) {
ESP_LOGI(kTag, "added as '%s'", info.title.c_str());
db_->Put(opt, "title:" + info.title, path);
}
});
db_->Put(opt, "title:coolkeywithoutval", leveldb::Slice());
});
}
auto parse_song(const leveldb::Slice& key, const leveldb::Slice& val)
-> std::optional<Song> {
Song s;
s.title = key.ToString();
return s;
}
auto Database::GetSongs(std::size_t page_size) -> std::future<Result<Song>> {
return RunOnDbTask<Result<Song>>([=, this]() -> Result<Song> {
return Query<Song>("title:", page_size, &parse_song);
});
}
auto Database::GetMoreSongs(std::size_t page_size, Continuation c)
-> std::future<Result<Song>> {
leveldb::Iterator* it = c.release();
return RunOnDbTask<Result<Song>>([=, this]() -> Result<Song> {
return Query<Song>(it, page_size, &parse_song);
});
}
} // namespace database
|