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 | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 30 | 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; |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 146 | span buffer(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, iCol)), |
| 147 | sqlite3_column_bytes(stmt, iCol)); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 148 | |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 149 | while (!buffer.empty()) { |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 150 | name::Component component; |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 151 | try { |
| 152 | component.wireDecode(Block(buffer)); |
| 153 | } |
| 154 | catch (const ndn::tlv::Error&) { |
| 155 | NDN_THROW_NESTED(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); |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 158 | buffer = buffer.subspan(component.size()); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | return name; |
| 162 | } |
| 163 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 164 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 165 | // Zone |
| 166 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 167 | |
| 168 | void |
| 169 | DbMgr::insert(Zone& zone) |
| 170 | { |
| 171 | if (zone.getId() > 0) |
| 172 | return; |
| 173 | |
| 174 | sqlite3_stmt* stmt; |
| 175 | const char* sql = "INSERT INTO zones (name, ttl) VALUES (?, ?)"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 176 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 177 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 178 | NDN_THROW(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 181 | saveName(zone.getName(), stmt, 1); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 182 | sqlite3_bind_int(stmt, 2, zone.getTtl().count()); |
| 183 | |
| 184 | rc = sqlite3_step(stmt); |
| 185 | if (rc != SQLITE_DONE) { |
| 186 | sqlite3_finalize(stmt); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 187 | NDN_THROW(ExecuteError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | zone.setId(sqlite3_last_insert_rowid(m_conn)); |
| 191 | sqlite3_finalize(stmt); |
| 192 | } |
| 193 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 194 | void |
| 195 | DbMgr::setZoneInfo(Zone& zone, |
| 196 | const std::string& key, |
| 197 | const Block& value) |
| 198 | { |
| 199 | if (zone.getId() == 0) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 200 | NDN_THROW(Error("zone has not been initialized")); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | if (key.length() > 10) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 204 | NDN_THROW(Error("key length should not exceed 10")); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | sqlite3_stmt* stmt; |
| 208 | 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] | 209 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 210 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 211 | NDN_THROW(PrepareError(sql)); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | sqlite3_bind_int(stmt, 1, zone.getId()); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame] | 215 | sqlite3_bind_text(stmt, 2, key.data(), key.length(), SQLITE_STATIC); |
| 216 | sqlite3_bind_blob(stmt, 3, value.data(), value.size(), SQLITE_STATIC); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 217 | |
| 218 | rc = sqlite3_step(stmt); |
| 219 | if (rc != SQLITE_DONE) { |
| 220 | sqlite3_finalize(stmt); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 221 | NDN_THROW(ExecuteError(sql)); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | sqlite3_finalize(stmt); |
| 225 | } |
| 226 | |
| 227 | std::map<std::string, Block> |
| 228 | DbMgr::getZoneInfo(Zone& zone) |
| 229 | { |
| 230 | using std::string; |
| 231 | std::map<string, Block> rtn; |
| 232 | |
| 233 | if (zone.getId() == 0) { |
| 234 | find(zone); |
| 235 | } |
| 236 | |
| 237 | if (zone.getId() == 0) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 238 | NDN_THROW(Error("zone has not been initialized")); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | sqlite3_stmt* stmt; |
| 242 | const char* sql = "SELECT key, value FROM zone_info WHERE zone_id=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 243 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 244 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 245 | NDN_THROW(PrepareError(sql)); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | sqlite3_bind_int(stmt, 1, zone.getId()); |
| 249 | |
| 250 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 251 | const char* key = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)); |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 252 | rtn[string(key)] = Block(span(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 1)), |
| 253 | sqlite3_column_bytes(stmt, 1))); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | sqlite3_finalize(stmt); |
| 257 | return rtn; |
| 258 | } |
| 259 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 260 | bool |
| 261 | DbMgr::find(Zone& zone) |
| 262 | { |
| 263 | sqlite3_stmt* stmt; |
| 264 | const char* sql = "SELECT id, ttl FROM zones WHERE name=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 265 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 266 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 267 | NDN_THROW(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 270 | saveName(zone.getName(), stmt, 1); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 271 | |
| 272 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
| 273 | zone.setId(sqlite3_column_int64(stmt, 0)); |
| 274 | zone.setTtl(time::seconds(sqlite3_column_int(stmt, 1))); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 275 | } |
| 276 | else { |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 277 | zone.setId(0); |
| 278 | } |
| 279 | |
| 280 | sqlite3_finalize(stmt); |
| 281 | |
| 282 | return zone.getId() != 0; |
| 283 | } |
| 284 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 285 | std::vector<Zone> |
| 286 | DbMgr::listZones() |
| 287 | { |
| 288 | sqlite3_stmt* stmt; |
| 289 | const char* sql = "SELECT id, name, ttl FROM zones"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 290 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 291 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 292 | NDN_THROW(PrepareError(sql)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | std::vector<Zone> vec; |
| 296 | |
| 297 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 298 | vec.emplace_back(); |
| 299 | Zone& zone = vec.back(); |
| 300 | zone.setId(sqlite3_column_int64(stmt, 0)); |
| 301 | zone.setTtl(time::seconds(sqlite3_column_int(stmt, 2))); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 302 | zone.setName(restoreName(stmt, 1)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 303 | } |
| 304 | sqlite3_finalize(stmt); |
| 305 | |
| 306 | return vec; |
| 307 | } |
| 308 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 309 | void |
| 310 | DbMgr::remove(Zone& zone) |
| 311 | { |
| 312 | if (zone.getId() == 0) |
| 313 | return; |
| 314 | |
| 315 | sqlite3_stmt* stmt; |
| 316 | const char* sql = "DELETE FROM zones where id=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 317 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 318 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 319 | NDN_THROW(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | sqlite3_bind_int64(stmt, 1, zone.getId()); |
| 323 | |
| 324 | rc = sqlite3_step(stmt); |
| 325 | if (rc != SQLITE_DONE) { |
| 326 | sqlite3_finalize(stmt); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 327 | NDN_THROW(ExecuteError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | sqlite3_finalize(stmt); |
| 331 | |
| 332 | zone = Zone(); |
| 333 | } |
| 334 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 335 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 336 | // Rrset |
| 337 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 338 | |
| 339 | void |
| 340 | DbMgr::insert(Rrset& rrset) |
| 341 | { |
| 342 | if (rrset.getId() != 0) |
| 343 | return; |
| 344 | |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 345 | if (rrset.getZone() == nullptr) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 346 | NDN_THROW(RrsetError("Rrset has not been assigned to a zone")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | if (rrset.getZone()->getId() == 0) { |
| 350 | insert(*rrset.getZone()); |
| 351 | } |
| 352 | |
| 353 | const char* sql = |
| 354 | "INSERT INTO rrsets (zone_id, label, type, version, ttl, data)" |
| 355 | " VALUES (?, ?, ?, ?, ?, ?)"; |
| 356 | |
| 357 | sqlite3_stmt* stmt; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 358 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 359 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 360 | NDN_THROW(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | sqlite3_bind_int64(stmt, 1, rrset.getZone()->getId()); |
| 364 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 365 | saveName(rrset.getLabel(), stmt, 2); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame] | 366 | sqlite3_bind_blob(stmt, 3, rrset.getType().data(), rrset.getType().size(), SQLITE_STATIC); |
| 367 | 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] | 368 | sqlite3_bind_int64(stmt, 5, rrset.getTtl().count()); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame] | 369 | 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] | 370 | |
| 371 | rc = sqlite3_step(stmt); |
| 372 | if (rc != SQLITE_DONE) { |
| 373 | sqlite3_finalize(stmt); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 374 | NDN_THROW(ExecuteError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | rrset.setId(sqlite3_last_insert_rowid(m_conn)); |
| 378 | sqlite3_finalize(stmt); |
| 379 | } |
| 380 | |
| 381 | bool |
| 382 | DbMgr::find(Rrset& rrset) |
| 383 | { |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 384 | if (rrset.getZone() == nullptr) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 385 | NDN_THROW(RrsetError("Rrset has not been assigned to a zone")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | if (rrset.getZone()->getId() == 0) { |
| 389 | bool isFound = find(*rrset.getZone()); |
| 390 | if (!isFound) { |
| 391 | return false; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | sqlite3_stmt* stmt; |
| 396 | const char* sql = |
| 397 | "SELECT id, ttl, version, data FROM rrsets" |
| 398 | " WHERE zone_id=? and label=? and type=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 399 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 400 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 401 | NDN_THROW(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | sqlite3_bind_int64(stmt, 1, rrset.getZone()->getId()); |
| 405 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 406 | saveName(rrset.getLabel(), stmt, 2); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame] | 407 | 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] | 408 | |
| 409 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
| 410 | rrset.setId(sqlite3_column_int64(stmt, 0)); |
| 411 | rrset.setTtl(time::seconds(sqlite3_column_int64(stmt, 1))); |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 412 | rrset.setVersion(name::Component(Block(span(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 2)), |
| 413 | sqlite3_column_bytes(stmt, 2))))); |
| 414 | rrset.setData(Block(span(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 3)), |
| 415 | sqlite3_column_bytes(stmt, 3)))); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 416 | } |
| 417 | else { |
| 418 | rrset.setId(0); |
| 419 | } |
| 420 | sqlite3_finalize(stmt); |
| 421 | |
| 422 | return rrset.getId() != 0; |
| 423 | } |
| 424 | |
| 425 | bool |
| 426 | DbMgr::findLowerBound(Rrset& rrset) |
| 427 | { |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 428 | if (rrset.getZone() == nullptr) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 429 | NDN_THROW(RrsetError("Rrset has not been assigned to a zone")); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | if (rrset.getZone()->getId() == 0) { |
| 433 | bool isFound = find(*rrset.getZone()); |
| 434 | if (!isFound) { |
| 435 | return false; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | sqlite3_stmt* stmt; |
| 440 | const char* sql = |
| 441 | "SELECT id, ttl, version, data FROM rrsets" |
| 442 | " WHERE zone_id=? and label<? and type=? ORDER BY label DESC"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 443 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 444 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 445 | NDN_THROW(PrepareError(sql)); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | sqlite3_bind_int64(stmt, 1, rrset.getZone()->getId()); |
| 449 | |
| 450 | saveName(rrset.getLabel(), stmt, 2); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame] | 451 | 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] | 452 | |
| 453 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
| 454 | rrset.setId(sqlite3_column_int64(stmt, 0)); |
| 455 | rrset.setTtl(time::seconds(sqlite3_column_int64(stmt, 1))); |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 456 | rrset.setVersion(name::Component(Block(span(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 2)), |
| 457 | sqlite3_column_bytes(stmt, 2))))); |
| 458 | rrset.setData(Block(span(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 3)), |
| 459 | sqlite3_column_bytes(stmt, 3)))); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 460 | } |
| 461 | else { |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 462 | rrset.setId(0); |
| 463 | } |
| 464 | sqlite3_finalize(stmt); |
| 465 | |
| 466 | return rrset.getId() != 0; |
| 467 | } |
| 468 | |
| 469 | std::vector<Rrset> |
| 470 | DbMgr::findRrsets(Zone& zone) |
| 471 | { |
| 472 | if (zone.getId() == 0) |
| 473 | find(zone); |
| 474 | |
| 475 | if (zone.getId() == 0) |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 476 | 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] | 477 | |
| 478 | std::vector<Rrset> vec; |
| 479 | sqlite3_stmt* stmt; |
| 480 | const char* sql = "SELECT id, ttl, version, data, label, type " |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 481 | "FROM rrsets where zone_id=? ORDER BY label"; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 482 | |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 483 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 484 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 485 | NDN_THROW(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 486 | } |
| 487 | sqlite3_bind_int64(stmt, 1, zone.getId()); |
| 488 | |
| 489 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 490 | vec.emplace_back(&zone); |
| 491 | Rrset& rrset = vec.back(); |
| 492 | |
| 493 | rrset.setId(sqlite3_column_int64(stmt, 0)); |
| 494 | rrset.setTtl(time::seconds(sqlite3_column_int64(stmt, 1))); |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 495 | rrset.setVersion(name::Component(Block(span(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 2)), |
| 496 | sqlite3_column_bytes(stmt, 2))))); |
| 497 | rrset.setData(Block(span(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 3)), |
| 498 | sqlite3_column_bytes(stmt, 3)))); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 499 | rrset.setLabel(restoreName(stmt, 4)); |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 500 | rrset.setType(name::Component(Block(span(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 5)), |
| 501 | sqlite3_column_bytes(stmt, 5))))); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 502 | } |
| 503 | sqlite3_finalize(stmt); |
| 504 | |
| 505 | return vec; |
| 506 | } |
| 507 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 508 | void |
| 509 | DbMgr::removeRrsetsOfZoneByType(Zone& zone, const name::Component& type) |
| 510 | { |
| 511 | if (zone.getId() == 0) |
| 512 | find(zone); |
| 513 | |
| 514 | if (zone.getId() == 0) |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 515 | 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] | 516 | |
| 517 | sqlite3_stmt* stmt; |
| 518 | const char* sql = "DELETE FROM rrsets WHERE zone_id = ? AND type = ?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 519 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 520 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 521 | NDN_THROW(PrepareError(sql)); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | sqlite3_bind_int64(stmt, 1, zone.getId()); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame] | 525 | sqlite3_bind_blob(stmt, 2, type.data(), type.size(), SQLITE_STATIC); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 526 | |
| 527 | rc = sqlite3_step(stmt); |
| 528 | if (rc != SQLITE_DONE) { |
| 529 | sqlite3_finalize(stmt); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 530 | NDN_THROW(ExecuteError(sql)); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 531 | } |
| 532 | sqlite3_finalize(stmt); |
| 533 | } |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 534 | |
| 535 | void |
| 536 | DbMgr::remove(Rrset& rrset) |
| 537 | { |
| 538 | if (rrset.getId() == 0) |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 539 | NDN_THROW(RrsetError("Attempting to remove Rrset that has no assigned id")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 540 | |
| 541 | sqlite3_stmt* stmt; |
| 542 | const char* sql = "DELETE FROM rrsets WHERE id=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 543 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 544 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 545 | NDN_THROW(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | sqlite3_bind_int64(stmt, 1, rrset.getId()); |
| 549 | |
| 550 | rc = sqlite3_step(stmt); |
| 551 | if (rc != SQLITE_DONE) { |
| 552 | sqlite3_finalize(stmt); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 553 | NDN_THROW(ExecuteError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | sqlite3_finalize(stmt); |
| 557 | |
| 558 | rrset = Rrset(rrset.getZone()); |
| 559 | } |
| 560 | |
| 561 | void |
| 562 | DbMgr::update(Rrset& rrset) |
| 563 | { |
| 564 | if (rrset.getId() == 0) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 565 | NDN_THROW(RrsetError("Attempting to replace Rrset that has no assigned id")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 568 | if (rrset.getZone() == nullptr) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 569 | NDN_THROW(RrsetError("Rrset has not been assigned to a zone")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | sqlite3_stmt* stmt; |
| 573 | const char* sql = "UPDATE rrsets SET ttl=?, version=?, data=? WHERE id=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 574 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 575 | if (rc != SQLITE_OK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 576 | NDN_THROW(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | sqlite3_bind_int64(stmt, 1, rrset.getTtl().count()); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame] | 580 | sqlite3_bind_blob(stmt, 2, rrset.getVersion().data(), rrset.getVersion().size(), SQLITE_STATIC); |
| 581 | 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] | 582 | sqlite3_bind_int64(stmt, 4, rrset.getId()); |
| 583 | |
| 584 | sqlite3_step(stmt); |
| 585 | sqlite3_finalize(stmt); |
| 586 | } |
| 587 | |
| 588 | } // namespace ndns |
| 589 | } // namespace ndn |