aboutsummaryrefslogtreecommitdiff
path: root/src/server/database.cc
blob: 077c86c23891497493bcb05ce58d554a192adee4 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "server/database.hh"

namespace server {
namespace database {

sqlite3* get_database() noexcept {

    static sqlite3* const database = []() -> sqlite3* {
        std::filesystem::create_directory(server::state.directory);
        const std::string path = server::state.directory + "world.dat";
        if (!std::filesystem::exists(path)) {
            shared::print::warn(
                "server: regenerating non-existent world data\n");
        }

        sqlite3* database = nullptr;
        if (const int status = sqlite3_open(path.c_str(), &database);
            status != SQLITE_OK) {

            throw std::runtime_error("failed to open chunk database at " +
                                     path);
        }

        return database;
    }();

    return database;
}

static void prepare(sqlite3_stmt*& statement, const std::string& sql) noexcept {
    sqlite3* const db = get_database();

    if (const int status = sqlite3_prepare_v2(
            db, sql.c_str(), static_cast<int>(std::size(sql) + 1), &statement,
            nullptr);
        status != SQLITE_OK) {

        throw std::runtime_error(std::string{"sqlite prepare error \""} +
                                 sqlite3_errmsg(db) + '\"');
    }
}

static auto step(sqlite3_stmt*& statement) noexcept {
    const int status = sqlite3_step(statement);
    if (status == SQLITE_ERROR) {
        sqlite3* const db = get_database();
        throw std::runtime_error(std::string{"sqlite step error \""} +
                                 sqlite3_errmsg(db) + '\"');
    }
    return status;
}

static void finalise(sqlite3_stmt*& statement) noexcept {
    if (const int status = sqlite3_finalize(statement); status != SQLITE_OK) {
        throw std::runtime_error(std::string{"sqlite bind blob error \""} +
                                 sqlite3_errstr(status) + '\"');
    }
}

static void bind_int64(sqlite3_stmt*& statement, const int index,
                       const std::uint64_t& key) noexcept {
    if (const int status = sqlite3_bind_int64(statement, index,
                                              std::bit_cast<std::int64_t>(key));
        status != SQLITE_OK) {
        throw std::runtime_error(std::string{"sqlite bind int error \""} +
                                 sqlite3_errstr(status) + '\"');
    }
}

static void bind_text(sqlite3_stmt*& statement, const int index,
                      const std::string& text) noexcept {
    if (const int status =
            sqlite3_bind_text(statement, index, std::data(text),
                              static_cast<int>(std::size(text)), SQLITE_STATIC);
        status != SQLITE_OK) {
        throw std::runtime_error(std::string{"sqlite bind text error \""} +
                                 sqlite3_errstr(status) + '\"');
    }
}

template <typename T>
static void bind_blob(sqlite3_stmt*& statement, const int index,
                      const T& blob) noexcept {
    if (const int status =
            sqlite3_bind_blob(statement, index, std::data(blob),
                              static_cast<int>(std::size(blob)), SQLITE_STATIC);
        status != SQLITE_OK) {
        throw std::runtime_error(std::string{"sqlite bind blob error \""} +
                                 sqlite3_errstr(status) + '\"');
    }
}

static void exec_void_stmt(const std::string& sql) noexcept {
    sqlite3_stmt* statement = nullptr;
    prepare(statement, sql);
    step(statement);
    finalise(statement);
}

void init() noexcept {
    exec_void_stmt("CREATE TABLE IF NOT EXISTS Chunks ("
                   "    coords BIGINT PRIMARY KEY,"
                   "    blocks BLOB NOT NULL"
                   ");");

    exec_void_stmt("CREATE TABLE IF NOT EXISTS Players ("
                   "    username TEXT PRIMARY KEY,"
                   "    password TEXT NOT NULL,"
                   "    player BLOB NOT NULL"
                   ");");
}

void quit() noexcept {
    sqlite3* database = get_database();
    if (const int status = sqlite3_close(database); status != SQLITE_OK) {
        throw std::runtime_error(std::string{"failed to close chunk database"} +
                                 sqlite3_errmsg(database));
    };
}

// We have to store our key in network byte order!
static std::uint64_t make_key(const shared::math::coords& coords) noexcept {
    const auto to_64 = [](const std::int32_t& val, const int lshift) {
        return static_cast<std::uint64_t>(
                   htonl(std::bit_cast<std::uint32_t>(val)))
               << lshift;
    };

    return to_64(coords.x, 32) + to_64(coords.z, 0);
}

std::optional<proto::chunk>
maybe_read_chunk(const shared::math::coords& pos) noexcept {
    sqlite3_stmt* statement = nullptr;
    prepare(statement, "SELECT blocks FROM Chunks WHERE Chunks.coords = ?;");
    bind_int64(statement, 1, make_key(pos));

    if (step(statement) != SQLITE_ROW) {
        finalise(statement);
        return std::nullopt;
    }

    const char* addr =
        static_cast<const char*>(sqlite3_column_blob(statement, 0));
    std::string blocks{addr, addr + sqlite3_column_bytes(statement, 0)};
    finalise(statement);

    shared::decompress_string(blocks);
    proto::chunk chunk;
    if (!chunk.ParseFromString(blocks)) {

        shared::print::fault("server: chunk [" + std::to_string(pos.x) + ", " +
                             std::to_string(pos.z) +
                             "] failed to parse and was evicted\n");

        statement = nullptr;
        prepare(statement, "DELETE FROM Chunks WHERE Chunks.coords = ?;");
        bind_int64(statement, 1, make_key(pos));
        step(statement);
        finalise(statement);
        return std::nullopt;
    }
    return chunk;
}

void write_chunk(const shared::math::coords& pos,
                 const proto::chunk& chunk) noexcept {
    std::string blocks;
    chunk.SerializeToString(&blocks);
    shared::compress_string(blocks);

    sqlite3_stmt* statement = nullptr;

    prepare(statement, "INSERT OR REPLACE INTO Chunks VALUES(?, ?);");
    bind_int64(statement, 1, make_key(pos));
    bind_blob(statement, 2, blocks);

    step(statement);

    finalise(statement);
}

std::optional<std::pair<proto::player, std::string>>
maybe_read_player(const std::string& username) noexcept {
    sqlite3_stmt* statement = nullptr;
    prepare(statement, "SELECT player, password "
                       "FROM Players "
                       "WHERE Players.username = ?");
    bind_text(statement, 1, username);

    if (step(statement) != SQLITE_ROW) {
        finalise(statement);
        return std::nullopt;
    }

    const char* plr_adr =
        static_cast<const char*>(sqlite3_column_blob(statement, 0));
    std::string player_bytes{plr_adr, plr_adr + sqlite3_column_bytes(statement, 0)};

    const unsigned char* pass_adr = sqlite3_column_text(statement, 1);
    std::string pass_bytes{pass_adr, pass_adr + sqlite3_column_bytes(statement, 1)};
    finalise(statement);

    shared::decompress_string(player_bytes);
    proto::player player;
    if (!player.ParseFromString(player_bytes)) {
        shared::print::fault("server: player \"" + username +
                             "\" failed to parse and was evicted\n");

        statement = nullptr;
        prepare(statement, "DELETE FROM Players WHERE Players.username = ?;");
        bind_text(statement, 1, username);
        step(statement);
        finalise(statement);
        return std::nullopt;
    }
    return std::make_pair(player, pass_bytes);
}

void write_player(const std::string& username, const std::string& password,
                  const proto::player& player) noexcept {
    std::string player_bytes;
    player.SerializeToString(&player_bytes);
    shared::compress_string(player_bytes);

    sqlite3_stmt* statement = nullptr;
    prepare(statement, "INSERT OR REPLACE INTO Players VALUES(?, ?, ?);");
    bind_text(statement, 1, username);
    bind_text(statement, 2, password);
    bind_blob(statement, 3, player_bytes);

    step(statement);
    finalise(statement);
}

} // namespace database
} // namespace server