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