Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 01a2a8c | 2024-12-13 14:25:50 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2024, Regents of the University of California. |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng 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 | * repo-ng 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 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 20 | #include "sqlite-storage.hpp" |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 21 | |
Davide Pesavento | 01a2a8c | 2024-12-13 14:25:50 -0500 | [diff] [blame^] | 22 | #include <ndn-cxx/util/logger.hpp> |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 23 | #include <ndn-cxx/util/sha256.hpp> |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 24 | #include <ndn-cxx/util/sqlite3-statement.hpp> |
| 25 | |
Davide Pesavento | 01a2a8c | 2024-12-13 14:25:50 -0500 | [diff] [blame^] | 26 | #include <filesystem> |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 27 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 28 | namespace repo { |
| 29 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 30 | NDN_LOG_INIT(repo.SqliteStorage); |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 31 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 32 | SqliteStorage::SqliteStorage(const std::string& dbPath) |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 33 | { |
| 34 | if (dbPath.empty()) { |
Davide Pesavento | 01a2a8c | 2024-12-13 14:25:50 -0500 | [diff] [blame^] | 35 | m_dbPath = "ndn_repo.db"; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 36 | } |
| 37 | else { |
Davide Pesavento | 01a2a8c | 2024-12-13 14:25:50 -0500 | [diff] [blame^] | 38 | std::filesystem::path fsPath(dbPath); |
| 39 | if (!std::filesystem::is_directory(fsPath)) { |
| 40 | if (!std::filesystem::create_directory(fsPath)) { |
| 41 | NDN_THROW(Error("Directory '" + dbPath + "' does not exists and cannot be created")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 42 | } |
| 43 | } |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 44 | m_dbPath = dbPath + "/ndn_repo.db"; |
| 45 | } |
Davide Pesavento | 01a2a8c | 2024-12-13 14:25:50 -0500 | [diff] [blame^] | 46 | |
| 47 | NDN_LOG_DEBUG("Using database file " << m_dbPath); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 48 | initializeRepo(); |
| 49 | } |
| 50 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 51 | void |
| 52 | SqliteStorage::initializeRepo() |
| 53 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 54 | char* errMsg = nullptr; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 55 | int rc = sqlite3_open_v2(m_dbPath.c_str(), &m_db, |
| 56 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, |
| 57 | #ifdef DISABLE_SQLITE3_FS_LOCKING |
| 58 | "unix-dotfile" |
| 59 | #else |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 60 | nullptr |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 61 | #endif |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 62 | ); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 63 | |
| 64 | if (rc == SQLITE_OK) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 65 | // Create a new table named NDN_REPO_V2, distinguish from the old table name(NDN_REPO) |
| 66 | sqlite3_exec(m_db, "CREATE TABLE NDN_REPO_V2 (name BLOB, data BLOB);", nullptr, nullptr, &errMsg); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 67 | // Ignore errors (when database already exists, errors are expected) |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 68 | sqlite3_exec(m_db, "CREATE UNIQUE INDEX index_name ON NDN_REPO_V2 (name);", nullptr, nullptr, &errMsg); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 69 | } |
| 70 | else { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 71 | NDN_LOG_DEBUG("Database file open failure rc:" << rc); |
Alexander Afanasyev | 5c16cc2 | 2019-04-02 14:17:12 -0400 | [diff] [blame] | 72 | NDN_THROW(Error("Database file open failure")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 73 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 74 | |
| 75 | // SQLite continues without syncing as soon as it has handed data off to the operating system |
| 76 | sqlite3_exec(m_db, "PRAGMA synchronous = OFF;", nullptr, nullptr, &errMsg); |
| 77 | // Uses a write-ahead log instead of a rollback journal to implement transactions. |
| 78 | sqlite3_exec(m_db, "PRAGMA journal_mode = WAL;", nullptr, nullptr, &errMsg); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | SqliteStorage::~SqliteStorage() |
| 82 | { |
| 83 | sqlite3_close(m_db); |
| 84 | } |
| 85 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 86 | int64_t |
| 87 | SqliteStorage::insert(const Data& data) |
| 88 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 89 | Name name = data.getFullName(); // store the full name |
| 90 | ndn::util::Sqlite3Statement stmt(m_db, "INSERT INTO NDN_REPO_V2 (name, data) VALUES (?, ?);"); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 91 | |
Davide Pesavento | 01a2a8c | 2024-12-13 14:25:50 -0500 | [diff] [blame^] | 92 | // Insert |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 93 | // Bind NULL to name value in NDN_REPO_V2 when initialize result. |
| 94 | auto result = sqlite3_bind_null(stmt, 1); |
Alexander Afanasyev | f34a355 | 2017-08-13 21:17:05 -0400 | [diff] [blame] | 95 | if (result == SQLITE_OK) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 96 | result = stmt.bind(1, name.wireEncode().value(), |
| 97 | name.wireEncode().value_size(), SQLITE_STATIC); |
Alexander Afanasyev | f34a355 | 2017-08-13 21:17:05 -0400 | [diff] [blame] | 98 | } |
| 99 | if (result == SQLITE_OK) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 100 | result = stmt.bind(2, data.wireEncode(), SQLITE_STATIC); |
Alexander Afanasyev | f34a355 | 2017-08-13 21:17:05 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | if (result == SQLITE_OK) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 104 | int rc = 0; |
| 105 | rc = stmt.step(); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 106 | if (rc == SQLITE_CONSTRAINT) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 107 | NDN_LOG_DEBUG("Insert failed"); |
Alexander Afanasyev | 5c16cc2 | 2019-04-02 14:17:12 -0400 | [diff] [blame] | 108 | NDN_THROW(Error("Insert failed")); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 109 | } |
| 110 | sqlite3_reset(stmt); |
Davide Pesavento | 01a2a8c | 2024-12-13 14:25:50 -0500 | [diff] [blame^] | 111 | return sqlite3_last_insert_rowid(m_db); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 112 | } |
| 113 | else { |
Davide Pesavento | 01a2a8c | 2024-12-13 14:25:50 -0500 | [diff] [blame^] | 114 | NDN_THROW(Error("Database insert failure (code: " + std::to_string(result))); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 115 | } |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 118 | bool |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 119 | SqliteStorage::erase(const Name& name) |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 120 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 121 | ndn::util::Sqlite3Statement stmt(m_db, "DELETE FROM NDN_REPO_V2 WHERE name = ?;"); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 122 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 123 | auto result = stmt.bind(1, |
| 124 | name.wireEncode().value(), |
| 125 | name.wireEncode().value_size(), SQLITE_STATIC); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 126 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 127 | if (result == SQLITE_OK) { |
| 128 | int rc = stmt.step(); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 129 | if (rc != SQLITE_DONE && rc != SQLITE_ROW) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 130 | NDN_LOG_DEBUG("Node delete error rc:" << rc); |
Alexander Afanasyev | 5c16cc2 | 2019-04-02 14:17:12 -0400 | [diff] [blame] | 131 | NDN_THROW(Error("Node delete error")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 132 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 133 | if (sqlite3_changes(m_db) != 1) { |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 134 | return false; |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 135 | } |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 136 | } |
| 137 | else { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 138 | NDN_LOG_DEBUG("delete bind error"); |
Alexander Afanasyev | 5c16cc2 | 2019-04-02 14:17:12 -0400 | [diff] [blame] | 139 | NDN_THROW(Error("delete bind error")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 140 | } |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 144 | std::shared_ptr<Data> |
| 145 | SqliteStorage::read(const Name& name) |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 146 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 147 | return find(name); |
| 148 | } |
| 149 | |
| 150 | bool |
| 151 | SqliteStorage::has(const Name& name) |
| 152 | { |
| 153 | // find exact match |
| 154 | return find(name, true) != nullptr; |
| 155 | } |
| 156 | |
| 157 | std::shared_ptr<Data> |
| 158 | SqliteStorage::find(const Name& name, bool exactMatch) |
| 159 | { |
| 160 | NDN_LOG_DEBUG("Trying to find: " << name); |
| 161 | Name nameSuccessor; |
| 162 | if (!exactMatch) { |
| 163 | nameSuccessor = name.getSuccessor(); |
| 164 | } |
| 165 | |
| 166 | std::string sql; |
| 167 | if (exactMatch) |
| 168 | sql = "SELECT * FROM NDN_REPO_V2 WHERE name = ?;"; |
| 169 | else |
| 170 | sql = "SELECT * FROM NDN_REPO_V2 WHERE name >= ? and name < ?;"; |
| 171 | |
| 172 | ndn::util::Sqlite3Statement stmt(m_db, sql); |
| 173 | |
| 174 | auto result = stmt.bind(1, |
| 175 | name.wireEncode().value(), |
| 176 | name.wireEncode().value_size(), SQLITE_STATIC); |
| 177 | |
| 178 | // use getsuccessor to locate prefix match items |
| 179 | if (result == SQLITE_OK && !exactMatch) { |
| 180 | // use V in TLV for prefix match when there is no exact match |
| 181 | result = stmt.bind(2, |
| 182 | nameSuccessor.wireEncode().value(), |
| 183 | nameSuccessor.wireEncode().value_size(), SQLITE_STATIC); |
| 184 | } |
| 185 | |
| 186 | if (result == SQLITE_OK) { |
| 187 | int rc = stmt.step(); |
| 188 | if (rc == SQLITE_ROW) { |
| 189 | Name foundName; |
| 190 | |
| 191 | auto data = std::make_shared<Data>(); |
| 192 | try { |
| 193 | data->wireDecode(stmt.getBlock(1)); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 194 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 195 | catch (const ndn::Block::Error& error) { |
| 196 | NDN_LOG_DEBUG(error.what()); |
Davide Pesavento | 5d66961 | 2017-09-22 23:49:37 -0400 | [diff] [blame] | 197 | return nullptr; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 198 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 199 | NDN_LOG_DEBUG("Data from db: " << *data); |
| 200 | |
| 201 | foundName = data->getFullName(); |
| 202 | |
| 203 | if ((exactMatch && name == foundName) || (!exactMatch && name.isPrefixOf(foundName))) { |
| 204 | NDN_LOG_DEBUG("Found: " << foundName << " " << stmt.getInt(0)); |
| 205 | return data; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 208 | else if (rc == SQLITE_DONE) { |
| 209 | return nullptr; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 210 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 211 | else { |
| 212 | NDN_LOG_DEBUG("Database query failure rc:" << rc); |
Alexander Afanasyev | 5c16cc2 | 2019-04-02 14:17:12 -0400 | [diff] [blame] | 213 | NDN_THROW(Error("Database query failure")); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 214 | } |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 215 | } |
| 216 | else { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 217 | NDN_LOG_DEBUG("select bind error"); |
Alexander Afanasyev | 5c16cc2 | 2019-04-02 14:17:12 -0400 | [diff] [blame] | 218 | NDN_THROW(Error("select bind error")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 219 | } |
Davide Pesavento | 5d66961 | 2017-09-22 23:49:37 -0400 | [diff] [blame] | 220 | return nullptr; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Alexander Afanasyev | 5c16cc2 | 2019-04-02 14:17:12 -0400 | [diff] [blame] | 223 | void |
| 224 | SqliteStorage::forEach(const std::function<void(const Name&)>& f) |
| 225 | { |
| 226 | ndn::util::Sqlite3Statement stmt(m_db, "SELECT data FROM NDN_REPO_V2;"); |
| 227 | |
| 228 | while (true) { |
| 229 | int rc = stmt.step(); |
| 230 | if (rc == SQLITE_ROW) { |
| 231 | Data data; |
| 232 | try { |
| 233 | data.wireDecode(stmt.getBlock(0)); |
| 234 | } |
| 235 | catch (const ndn::Block::Error& error) { |
| 236 | NDN_LOG_DEBUG("Error while decoding data from the database: " << error.what()); |
| 237 | continue; |
| 238 | } |
| 239 | f(data.getName()); |
| 240 | } |
| 241 | else if (rc == SQLITE_DONE) { |
| 242 | break; |
| 243 | } |
| 244 | else { |
Davide Pesavento | 01a2a8c | 2024-12-13 14:25:50 -0500 | [diff] [blame^] | 245 | NDN_THROW(Error("Database query failure (code: " + std::to_string(rc))); |
Alexander Afanasyev | 5c16cc2 | 2019-04-02 14:17:12 -0400 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 250 | uint64_t |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 251 | SqliteStorage::size() |
| 252 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 253 | ndn::util::Sqlite3Statement stmt(m_db, "SELECT count(*) FROM NDN_REPO_V2;"); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 254 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 255 | int rc = stmt.step(); |
| 256 | if (rc != SQLITE_ROW) { |
| 257 | NDN_LOG_DEBUG("Database query failure rc:" << rc); |
Alexander Afanasyev | 5c16cc2 | 2019-04-02 14:17:12 -0400 | [diff] [blame] | 258 | NDN_THROW(Error("Database query failure")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 259 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 260 | |
Davide Pesavento | 01a2a8c | 2024-12-13 14:25:50 -0500 | [diff] [blame^] | 261 | return stmt.getInt(0); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 264 | } // namespace repo |