Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, Regents of the University of California. |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 7 | * |
| 8 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "db-mgr.hpp" |
| 21 | #include "logger.hpp" |
| 22 | #include "clients/response.hpp" |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 23 | #include "util/util.hpp" |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 24 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 25 | namespace ndn { |
| 26 | namespace ndns { |
| 27 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 28 | NDNS_LOG_INIT(DbMgr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 29 | |
Davide Pesavento | caedf11 | 2021-05-09 20:21:53 -0400 | [diff] [blame] | 30 | static const std::string NDNS_SCHEMA = R"SQL( |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 31 | CREATE TABLE IF NOT EXISTS zones ( |
| 32 | id INTEGER NOT NULL PRIMARY KEY, |
Davide Pesavento | caedf11 | 2021-05-09 20:21:53 -0400 | [diff] [blame] | 33 | name BLOB NOT NULL UNIQUE, |
| 34 | ttl INTEGER NOT NULL |
| 35 | ); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 36 | |
| 37 | CREATE TABLE IF NOT EXISTS zone_info ( |
Davide Pesavento | caedf11 | 2021-05-09 20:21:53 -0400 | [diff] [blame] | 38 | zone_id INTEGER NOT NULL, |
| 39 | key TEXT NOT NULL, |
| 40 | value BLOB NOT NULL, |
| 41 | PRIMARY KEY(zone_id, key), |
| 42 | FOREIGN KEY(zone_id) REFERENCES zones(id) ON UPDATE CASCADE ON DELETE CASCADE |
| 43 | ); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 44 | |
| 45 | CREATE TABLE IF NOT EXISTS rrsets ( |
| 46 | id INTEGER NOT NULL PRIMARY KEY, |
Davide Pesavento | caedf11 | 2021-05-09 20:21:53 -0400 | [diff] [blame] | 47 | zone_id INTEGER NOT NULL, |
| 48 | label BLOB NOT NULL, |
| 49 | type BLOB NOT NULL, |
| 50 | version BLOB NOT NULL, |
| 51 | ttl INTEGER NOT NULL, |
| 52 | data BLOB NOT NULL, |
| 53 | FOREIGN KEY(zone_id) REFERENCES zones(id) ON UPDATE CASCADE ON DELETE CASCADE |
| 54 | ); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 55 | |
| 56 | CREATE UNIQUE INDEX rrsets_zone_id_label_type_version |
Davide Pesavento | caedf11 | 2021-05-09 20:21:53 -0400 | [diff] [blame] | 57 | ON rrsets(zone_id, label, type, version); |
| 58 | )SQL"; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 59 | |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 60 | DbMgr::DbMgr(const std::string& dbFile) |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 61 | : m_dbFile(dbFile) |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 62 | , m_conn(nullptr) |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 63 | { |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 64 | if (m_dbFile.empty()) |
| 65 | m_dbFile = getDefaultDatabaseFile(); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 66 | |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 67 | open(); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 68 | |
| 69 | NDNS_LOG_INFO("open database: " << m_dbFile); |
| 70 | } |
| 71 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 72 | DbMgr::~DbMgr() |
| 73 | { |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 74 | close(); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void |
| 78 | DbMgr::open() |
| 79 | { |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 80 | int res = sqlite3_open_v2(m_dbFile.data(), &m_conn, |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 81 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, |
| 82 | #ifdef DISABLE_SQLITE3_FS_LOCKING |
| 83 | "unix-dotfile" |
| 84 | #else |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 85 | nullptr |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 86 | #endif |
| 87 | ); |
| 88 | |
| 89 | if (res != SQLITE_OK) { |
| 90 | NDNS_LOG_FATAL("Cannot open the db file: " << m_dbFile); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 91 | NDN_THROW(ConnectError("Cannot open the db file: " + m_dbFile)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 92 | } |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 93 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 94 | // ignore any errors from DB creation (command will fail for the existing database, which is ok) |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 95 | sqlite3_exec(m_conn, NDNS_SCHEMA.data(), nullptr, nullptr, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
| 99 | DbMgr::close() |
| 100 | { |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 101 | if (m_conn == nullptr) |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 102 | return; |
| 103 | |
| 104 | int ret = sqlite3_close(m_conn); |
| 105 | if (ret != SQLITE_OK) { |
| 106 | NDNS_LOG_FATAL("Cannot close the db: " << m_dbFile); |
| 107 | } |
| 108 | else { |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 109 | m_conn = nullptr; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 110 | NDNS_LOG_INFO("Close database: " << m_dbFile); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | DbMgr::clearAllData() |
| 116 | { |
| 117 | const char* sql = "DELETE FROM zones; DELETE FROM rrsets;"; |
| 118 | |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 119 | // sqlite3_step cannot execute multiple SQL statements |
| 120 | int rc = sqlite3_exec(m_conn, sql, nullptr, nullptr, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 121 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 122 | NDN_THROW(ExecuteError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | NDNS_LOG_INFO("clear all the data in the database: " << m_dbFile); |
| 126 | } |
| 127 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 128 | void |
| 129 | DbMgr::saveName(const Name& name, sqlite3_stmt* stmt, int iCol, bool isStatic) |
| 130 | { |
Davide Pesavento | caedf11 | 2021-05-09 20:21:53 -0400 | [diff] [blame] | 131 | static const uint8_t dummy = 0; |
| 132 | const auto& wire = name.wireEncode(); |
| 133 | const auto* ptr = wire.value(); |
| 134 | if (ptr == nullptr) { |
| 135 | // if value() returns nullptr (i.e., value_size() == 0), pass a non-null dummy |
| 136 | // pointer instead; we cannot bind nullptr because the column may be "NOT NULL" |
| 137 | ptr = &dummy; |
| 138 | } |
| 139 | sqlite3_bind_blob(stmt, iCol, ptr, wire.value_size(), isStatic ? SQLITE_STATIC : SQLITE_TRANSIENT); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | Name |
| 143 | DbMgr::restoreName(sqlite3_stmt* stmt, int iCol) |
| 144 | { |
| 145 | Name name; |
| 146 | |
| 147 | const uint8_t* buffer = static_cast<const uint8_t*>(sqlite3_column_blob(stmt, iCol)); |
| 148 | size_t nBytesLeft = sqlite3_column_bytes(stmt, iCol); |
| 149 | |
| 150 | while (nBytesLeft > 0) { |
| 151 | bool hasDecodingSucceeded; |
| 152 | name::Component component; |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 153 | std::tie(hasDecodingSucceeded, component) = Block::fromBuffer({buffer, nBytesLeft}); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 154 | if (!hasDecodingSucceeded) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 155 | NDN_THROW(Error("Error while decoding name from the database")); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 156 | } |
| 157 | name.append(component); |
| 158 | buffer += component.size(); |
| 159 | nBytesLeft -= component.size(); |
| 160 | } |
| 161 | |
| 162 | return name; |
| 163 | } |
| 164 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 165 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 166 | // Zone |
| 167 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 168 | |
| 169 | void |
| 170 | DbMgr::insert(Zone& zone) |
| 171 | { |
| 172 | if (zone.getId() > 0) |
| 173 | return; |
| 174 | |
| 175 | sqlite3_stmt* stmt; |
| 176 | const char* sql = "INSERT INTO zones (name, ttl) VALUES (?, ?)"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 177 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 178 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 179 | NDN_THROW(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 182 | saveName(zone.getName(), stmt, 1); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 183 | sqlite3_bind_int(stmt, 2, zone.getTtl().count()); |
| 184 | |
| 185 | rc = sqlite3_step(stmt); |
| 186 | if (rc != SQLITE_DONE) { |
| 187 | sqlite3_finalize(stmt); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 188 | NDN_THROW(ExecuteError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | zone.setId(sqlite3_last_insert_rowid(m_conn)); |
| 192 | sqlite3_finalize(stmt); |
| 193 | } |
| 194 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 195 | void |
| 196 | DbMgr::setZoneInfo(Zone& zone, |
| 197 | const std::string& key, |
| 198 | const Block& value) |
| 199 | { |
| 200 | if (zone.getId() == 0) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 201 | NDN_THROW(Error("zone has not been initialized")); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | if (key.length() > 10) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 205 | NDN_THROW(Error("key length should not exceed 10")); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | sqlite3_stmt* stmt; |
| 209 | const char* sql = "INSERT OR REPLACE INTO zone_info (zone_id, key, value) VALUES (?, ?, ?)"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 210 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 211 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 212 | NDN_THROW(PrepareError(sql)); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | sqlite3_bind_int(stmt, 1, zone.getId()); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 216 | sqlite3_bind_text(stmt, 2, key.data(), key.length(), SQLITE_STATIC); |
| 217 | sqlite3_bind_blob(stmt, 3, value.data(), value.size(), SQLITE_STATIC); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 218 | |
| 219 | rc = sqlite3_step(stmt); |
| 220 | if (rc != SQLITE_DONE) { |
| 221 | sqlite3_finalize(stmt); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 222 | NDN_THROW(ExecuteError(sql)); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | sqlite3_finalize(stmt); |
| 226 | } |
| 227 | |
| 228 | std::map<std::string, Block> |
| 229 | DbMgr::getZoneInfo(Zone& zone) |
| 230 | { |
| 231 | using std::string; |
| 232 | std::map<string, Block> rtn; |
| 233 | |
| 234 | if (zone.getId() == 0) { |
| 235 | find(zone); |
| 236 | } |
| 237 | |
| 238 | if (zone.getId() == 0) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 239 | NDN_THROW(Error("zone has not been initialized")); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | sqlite3_stmt* stmt; |
| 243 | const char* sql = "SELECT key, value FROM zone_info WHERE zone_id=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 244 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 245 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 246 | NDN_THROW(PrepareError(sql)); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | sqlite3_bind_int(stmt, 1, zone.getId()); |
| 250 | |
| 251 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 252 | const char* key = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 253 | rtn[string(key)] = Block(make_span(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 1)), |
| 254 | sqlite3_column_bytes(stmt, 1))); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | sqlite3_finalize(stmt); |
| 258 | return rtn; |
| 259 | } |
| 260 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 261 | bool |
| 262 | DbMgr::find(Zone& zone) |
| 263 | { |
| 264 | sqlite3_stmt* stmt; |
| 265 | const char* sql = "SELECT id, ttl FROM zones WHERE name=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 266 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 267 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 268 | NDN_THROW(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 271 | saveName(zone.getName(), stmt, 1); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 272 | |
| 273 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
| 274 | zone.setId(sqlite3_column_int64(stmt, 0)); |
| 275 | zone.setTtl(time::seconds(sqlite3_column_int(stmt, 1))); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 276 | } |
| 277 | else { |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 278 | zone.setId(0); |
| 279 | } |
| 280 | |
| 281 | sqlite3_finalize(stmt); |
| 282 | |
| 283 | return zone.getId() != 0; |
| 284 | } |
| 285 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 286 | std::vector<Zone> |
| 287 | DbMgr::listZones() |
| 288 | { |
| 289 | sqlite3_stmt* stmt; |
| 290 | const char* sql = "SELECT id, name, ttl FROM zones"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 291 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 292 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 293 | NDN_THROW(PrepareError(sql)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | std::vector<Zone> vec; |
| 297 | |
| 298 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 299 | vec.emplace_back(); |
| 300 | Zone& zone = vec.back(); |
| 301 | zone.setId(sqlite3_column_int64(stmt, 0)); |
| 302 | zone.setTtl(time::seconds(sqlite3_column_int(stmt, 2))); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 303 | zone.setName(restoreName(stmt, 1)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 304 | } |
| 305 | sqlite3_finalize(stmt); |
| 306 | |
| 307 | return vec; |
| 308 | } |
| 309 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 310 | void |
| 311 | DbMgr::remove(Zone& zone) |
| 312 | { |
| 313 | if (zone.getId() == 0) |
| 314 | return; |
| 315 | |
| 316 | sqlite3_stmt* stmt; |
| 317 | const char* sql = "DELETE FROM zones where id=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 318 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 319 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 320 | NDN_THROW(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | sqlite3_bind_int64(stmt, 1, zone.getId()); |
| 324 | |
| 325 | rc = sqlite3_step(stmt); |
| 326 | if (rc != SQLITE_DONE) { |
| 327 | sqlite3_finalize(stmt); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 328 | NDN_THROW(ExecuteError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | sqlite3_finalize(stmt); |
| 332 | |
| 333 | zone = Zone(); |
| 334 | } |
| 335 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 336 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 337 | // Rrset |
| 338 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 339 | |
| 340 | void |
| 341 | DbMgr::insert(Rrset& rrset) |
| 342 | { |
| 343 | if (rrset.getId() != 0) |
| 344 | return; |
| 345 | |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 346 | if (rrset.getZone() == nullptr) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 347 | NDN_THROW(RrsetError("Rrset has not been assigned to a zone")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | if (rrset.getZone()->getId() == 0) { |
| 351 | insert(*rrset.getZone()); |
| 352 | } |
| 353 | |
| 354 | const char* sql = |
| 355 | "INSERT INTO rrsets (zone_id, label, type, version, ttl, data)" |
| 356 | " VALUES (?, ?, ?, ?, ?, ?)"; |
| 357 | |
| 358 | sqlite3_stmt* stmt; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 359 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 360 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 361 | NDN_THROW(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | sqlite3_bind_int64(stmt, 1, rrset.getZone()->getId()); |
| 365 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 366 | saveName(rrset.getLabel(), stmt, 2); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 367 | sqlite3_bind_blob(stmt, 3, rrset.getType().data(), rrset.getType().size(), SQLITE_STATIC); |
| 368 | sqlite3_bind_blob(stmt, 4, rrset.getVersion().data(), rrset.getVersion().size(), SQLITE_STATIC); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 369 | sqlite3_bind_int64(stmt, 5, rrset.getTtl().count()); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 370 | sqlite3_bind_blob(stmt, 6, rrset.getData().data(), rrset.getData().size(), SQLITE_STATIC); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 371 | |
| 372 | rc = sqlite3_step(stmt); |
| 373 | if (rc != SQLITE_DONE) { |
| 374 | sqlite3_finalize(stmt); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 375 | NDN_THROW(ExecuteError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | rrset.setId(sqlite3_last_insert_rowid(m_conn)); |
| 379 | sqlite3_finalize(stmt); |
| 380 | } |
| 381 | |
| 382 | bool |
| 383 | DbMgr::find(Rrset& rrset) |
| 384 | { |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 385 | if (rrset.getZone() == nullptr) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 386 | NDN_THROW(RrsetError("Rrset has not been assigned to a zone")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | if (rrset.getZone()->getId() == 0) { |
| 390 | bool isFound = find(*rrset.getZone()); |
| 391 | if (!isFound) { |
| 392 | return false; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | sqlite3_stmt* stmt; |
| 397 | const char* sql = |
| 398 | "SELECT id, ttl, version, data FROM rrsets" |
| 399 | " WHERE zone_id=? and label=? and type=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 400 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 401 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 402 | NDN_THROW(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | sqlite3_bind_int64(stmt, 1, rrset.getZone()->getId()); |
| 406 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 407 | saveName(rrset.getLabel(), stmt, 2); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 408 | sqlite3_bind_blob(stmt, 3, rrset.getType().data(), rrset.getType().size(), SQLITE_STATIC); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 409 | |
| 410 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
| 411 | rrset.setId(sqlite3_column_int64(stmt, 0)); |
| 412 | rrset.setTtl(time::seconds(sqlite3_column_int64(stmt, 1))); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 413 | rrset.setVersion(Block(make_span(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 2)), |
| 414 | sqlite3_column_bytes(stmt, 2)))); |
| 415 | rrset.setData(Block(make_span(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 3)), |
| 416 | sqlite3_column_bytes(stmt, 3)))); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 417 | } |
| 418 | else { |
| 419 | rrset.setId(0); |
| 420 | } |
| 421 | sqlite3_finalize(stmt); |
| 422 | |
| 423 | return rrset.getId() != 0; |
| 424 | } |
| 425 | |
| 426 | bool |
| 427 | DbMgr::findLowerBound(Rrset& rrset) |
| 428 | { |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 429 | if (rrset.getZone() == nullptr) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 430 | NDN_THROW(RrsetError("Rrset has not been assigned to a zone")); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | if (rrset.getZone()->getId() == 0) { |
| 434 | bool isFound = find(*rrset.getZone()); |
| 435 | if (!isFound) { |
| 436 | return false; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | sqlite3_stmt* stmt; |
| 441 | const char* sql = |
| 442 | "SELECT id, ttl, version, data FROM rrsets" |
| 443 | " WHERE zone_id=? and label<? and type=? ORDER BY label DESC"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 444 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 445 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 446 | NDN_THROW(PrepareError(sql)); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | sqlite3_bind_int64(stmt, 1, rrset.getZone()->getId()); |
| 450 | |
| 451 | saveName(rrset.getLabel(), stmt, 2); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 452 | sqlite3_bind_blob(stmt, 3, rrset.getType().data(), rrset.getType().size(), SQLITE_STATIC); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 453 | |
| 454 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
| 455 | rrset.setId(sqlite3_column_int64(stmt, 0)); |
| 456 | rrset.setTtl(time::seconds(sqlite3_column_int64(stmt, 1))); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 457 | rrset.setVersion(Block(make_span(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 2)), |
| 458 | sqlite3_column_bytes(stmt, 2)))); |
| 459 | rrset.setData(Block(make_span(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 3)), |
| 460 | sqlite3_column_bytes(stmt, 3)))); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 461 | } |
| 462 | else { |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 463 | rrset.setId(0); |
| 464 | } |
| 465 | sqlite3_finalize(stmt); |
| 466 | |
| 467 | return rrset.getId() != 0; |
| 468 | } |
| 469 | |
| 470 | std::vector<Rrset> |
| 471 | DbMgr::findRrsets(Zone& zone) |
| 472 | { |
| 473 | if (zone.getId() == 0) |
| 474 | find(zone); |
| 475 | |
| 476 | if (zone.getId() == 0) |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 477 | NDN_THROW(RrsetError("Attempting to find all the rrsets with a zone does not in the database")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 478 | |
| 479 | std::vector<Rrset> vec; |
| 480 | sqlite3_stmt* stmt; |
| 481 | const char* sql = "SELECT id, ttl, version, data, label, type " |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 482 | "FROM rrsets where zone_id=? ORDER BY label"; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 483 | |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 484 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 485 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 486 | NDN_THROW(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 487 | } |
| 488 | sqlite3_bind_int64(stmt, 1, zone.getId()); |
| 489 | |
| 490 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 491 | vec.emplace_back(&zone); |
| 492 | Rrset& rrset = vec.back(); |
| 493 | |
| 494 | rrset.setId(sqlite3_column_int64(stmt, 0)); |
| 495 | rrset.setTtl(time::seconds(sqlite3_column_int64(stmt, 1))); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 496 | rrset.setVersion(Block(make_span(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 2)), |
| 497 | sqlite3_column_bytes(stmt, 2)))); |
| 498 | rrset.setData(Block(make_span(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 3)), |
| 499 | sqlite3_column_bytes(stmt, 3)))); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 500 | rrset.setLabel(restoreName(stmt, 4)); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 501 | rrset.setType(Block(make_span(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 5)), |
| 502 | sqlite3_column_bytes(stmt, 5)))); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 503 | } |
| 504 | sqlite3_finalize(stmt); |
| 505 | |
| 506 | return vec; |
| 507 | } |
| 508 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 509 | void |
| 510 | DbMgr::removeRrsetsOfZoneByType(Zone& zone, const name::Component& type) |
| 511 | { |
| 512 | if (zone.getId() == 0) |
| 513 | find(zone); |
| 514 | |
| 515 | if (zone.getId() == 0) |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 516 | NDN_THROW(RrsetError("Attempting to find all the rrsets with a zone does not in the database")); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 517 | |
| 518 | sqlite3_stmt* stmt; |
| 519 | const char* sql = "DELETE FROM rrsets WHERE zone_id = ? AND type = ?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 520 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 521 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 522 | NDN_THROW(PrepareError(sql)); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | sqlite3_bind_int64(stmt, 1, zone.getId()); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 526 | sqlite3_bind_blob(stmt, 2, type.data(), type.size(), SQLITE_STATIC); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 527 | |
| 528 | rc = sqlite3_step(stmt); |
| 529 | if (rc != SQLITE_DONE) { |
| 530 | sqlite3_finalize(stmt); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 531 | NDN_THROW(ExecuteError(sql)); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 532 | } |
| 533 | sqlite3_finalize(stmt); |
| 534 | } |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 535 | |
| 536 | void |
| 537 | DbMgr::remove(Rrset& rrset) |
| 538 | { |
| 539 | if (rrset.getId() == 0) |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 540 | NDN_THROW(RrsetError("Attempting to remove Rrset that has no assigned id")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 541 | |
| 542 | sqlite3_stmt* stmt; |
| 543 | const char* sql = "DELETE FROM rrsets WHERE id=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 544 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 545 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 546 | NDN_THROW(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | sqlite3_bind_int64(stmt, 1, rrset.getId()); |
| 550 | |
| 551 | rc = sqlite3_step(stmt); |
| 552 | if (rc != SQLITE_DONE) { |
| 553 | sqlite3_finalize(stmt); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 554 | NDN_THROW(ExecuteError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | sqlite3_finalize(stmt); |
| 558 | |
| 559 | rrset = Rrset(rrset.getZone()); |
| 560 | } |
| 561 | |
| 562 | void |
| 563 | DbMgr::update(Rrset& rrset) |
| 564 | { |
| 565 | if (rrset.getId() == 0) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 566 | NDN_THROW(RrsetError("Attempting to replace Rrset that has no assigned id")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 569 | if (rrset.getZone() == nullptr) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 570 | NDN_THROW(RrsetError("Rrset has not been assigned to a zone")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | sqlite3_stmt* stmt; |
| 574 | const char* sql = "UPDATE rrsets SET ttl=?, version=?, data=? WHERE id=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 575 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 576 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 577 | NDN_THROW(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | sqlite3_bind_int64(stmt, 1, rrset.getTtl().count()); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 581 | sqlite3_bind_blob(stmt, 2, rrset.getVersion().data(), rrset.getVersion().size(), SQLITE_STATIC); |
| 582 | sqlite3_bind_blob(stmt, 3, rrset.getData().data(), rrset.getData().size(), SQLITE_STATIC); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 583 | sqlite3_bind_int64(stmt, 4, rrset.getId()); |
| 584 | |
| 585 | sqlite3_step(stmt); |
| 586 | sqlite3_finalize(stmt); |
| 587 | } |
| 588 | |
| 589 | } // namespace ndns |
| 590 | } // namespace ndn |