vkdb
A time series database engine in C++.
Loading...
Searching...
No Matches
table.cpp
1#include <vkdb/table.h>
2#include <filesystem>
3#include <stdexcept>
4
5namespace vkdb {
6Table::Table(const FilePath& db_path, const TableName& name)
7 : name_{name}
8 , db_path_{db_path}
9 , storage_engine_{path()} {
10 load();
11}
12
13Table& Table::setTagColumns(const TagColumns& tag_columns) noexcept {
14 tag_columns_ = tag_columns;
15 return *this;
16}
17
18Table& Table::addTagColumn(const TagKey& tag_column) {
19 if (been_populated()) {
20 throw std::runtime_error{
21 "Table::addTagColumn(): Table '" + name_
22 + "' has previously been populated with data."
23 };
24 }
25 auto inserted{tag_columns_.insert(tag_column).second};
26 if (!inserted) {
27 throw std::runtime_error{
28 "Table::addTagColumn(): Tag column '" + tag_column
29 + "' already exists in '" + name_ + "'."
30 };
31 }
32 save_tag_columns();
33 return *this;
34}
35
36Table& Table::removeTagColumn(const TagKey& tag_column) {
37 if (been_populated()) {
38 throw std::runtime_error{
39 "Table::removeTagColumn(): Table '" + name_
40 + "' has previously been populated with data."
41 };
42 }
43 auto removed{tag_columns_.erase(tag_column) > 0};
44 if (!removed) {
45 throw std::runtime_error{
46 "Table::removeTagColumn(): Tag column '" + tag_column
47 + "' does not exist in '" + name_ + "'."
48 };
49 }
50 save_tag_columns();
51 return *this;
52}
53
54void Table::clear() const noexcept {
55 std::filesystem::remove_all(path());
56 std::filesystem::create_directories(path());
57}
58
60 return FriendlyQueryBuilder<double>(storage_engine_, tag_columns_);
61}
62
63TableName Table::name() const noexcept {
64 return name_;
65}
66
67TagColumns Table::tagColumns() const noexcept {
68 return tag_columns_;
69}
70
71FilePath Table::path() const noexcept {
72 return db_path_ / FilePath{name_};
73}
74
75bool Table::been_populated() const noexcept {
76 return !storage_engine_.empty();
77}
78
79void Table::save_tag_columns() const {
80 std::ofstream file{tag_columns_path()};
81 if (!file.is_open()) {
82 throw std::runtime_error{
83 "Table::save_tag_columns(): Unable to open file "
84 + std::string(tag_columns_path()) + "."
85 };
86 }
87 for (const auto& column : tag_columns_) {
88 file << column << "\n";
89 }
90 file.close();
91}
92
93void Table::load_tag_columns() {
94 tag_columns_.clear();
95 if (!std::filesystem::exists(tag_columns_path())) {
96 return;
97 }
98 std::ifstream file{tag_columns_path()};
99 if (!file.is_open()) {
100 throw std::runtime_error{
101 "Table::load_tag_columns(): Unable to open file "
102 + std::string(tag_columns_path()) + "."
103 };
104 }
105 std::string column;
106 while (std::getline(file, column)) {
107 if (!column.empty()) {
108 tag_columns_.insert(column);
109 }
110 }
111 file.close();
112}
113
114FilePath Table::tag_columns_path() const noexcept {
115 return path() / TAG_COLUMNS_FILENAME;
116}
117
118void Table::load() {
119 std::filesystem::create_directories(path());
120 load_tag_columns();
121 storage_engine_.replayWAL();
122}
123
124} // namespace vkdb
Friendly query builder for querying a Table.
void replayWAL()
Replay the write-ahead log.
Definition lsm_tree.h:203
bool empty() const noexcept
Check if the LSM tree is empty.
Definition lsm_tree.h:280
Represents a table in vkdb.
Definition table.h:32
void clear() const noexcept
Clear the table.
Definition table.cpp:54
Table & addTagColumn(const TagKey &tag_column)
Add a tag column.
Definition table.cpp:18
TableName name() const noexcept
Get the name of the table.
Definition table.cpp:63
Table & setTagColumns(const TagColumns &tag_columns) noexcept
Set the TagColumns object.
Definition table.cpp:13
TagColumns tagColumns() const noexcept
Get the TagColumns object.
Definition table.cpp:67
FilePath path() const noexcept
Get the path to the table directory.
Definition table.cpp:71
FriendlyQueryBuilder< double > query() noexcept
Get a FriendlyQueryBuilder object.
Definition table.cpp:59
Table & removeTagColumn(const TagKey &tag_column)
Remove a tag column.
Definition table.cpp:36
Table()=delete
Deleted default constructor.