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 | /* |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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" |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 22 | #include "index.hpp" |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 23 | |
| 24 | #include <ndn-cxx/util/sha256.hpp> |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 25 | #include <boost/filesystem.hpp> |
| 26 | #include <istream> |
| 27 | |
| 28 | namespace repo { |
| 29 | |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 30 | using std::string; |
| 31 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 32 | SqliteStorage::SqliteStorage(const string& dbPath) |
| 33 | : m_size(0) |
| 34 | { |
| 35 | if (dbPath.empty()) { |
| 36 | std::cerr << "Create db file in local location [" << dbPath << "]. " << std::endl |
| 37 | << "You can assign the path using -d option" << std::endl; |
| 38 | m_dbPath = string("ndn_repo.db"); |
| 39 | } |
| 40 | else { |
| 41 | boost::filesystem::path fsPath(dbPath); |
| 42 | boost::filesystem::file_status fsPathStatus = boost::filesystem::status(fsPath); |
| 43 | if (!boost::filesystem::is_directory(fsPathStatus)) { |
| 44 | if (!boost::filesystem::create_directory(boost::filesystem::path(fsPath))) { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 45 | 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] | 46 | } |
| 47 | } |
| 48 | |
| 49 | m_dbPath = dbPath + "/ndn_repo.db"; |
| 50 | } |
| 51 | initializeRepo(); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | void |
| 56 | SqliteStorage::initializeRepo() |
| 57 | { |
| 58 | char* errMsg = 0; |
| 59 | |
| 60 | int rc = sqlite3_open_v2(m_dbPath.c_str(), &m_db, |
| 61 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, |
| 62 | #ifdef DISABLE_SQLITE3_FS_LOCKING |
| 63 | "unix-dotfile" |
| 64 | #else |
| 65 | 0 |
| 66 | #endif |
| 67 | ); |
| 68 | |
| 69 | if (rc == SQLITE_OK) { |
| 70 | sqlite3_exec(m_db, "CREATE TABLE NDN_REPO (" |
| 71 | "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " |
| 72 | "name BLOB, " |
| 73 | "data BLOB, " |
| 74 | "keylocatorHash BLOB);\n " |
| 75 | , 0, 0, &errMsg); |
| 76 | // Ignore errors (when database already exists, errors are expected) |
| 77 | } |
| 78 | else { |
| 79 | std::cerr << "Database file open failure rc:" << rc << std::endl; |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 80 | BOOST_THROW_EXCEPTION(Error("Database file open failure")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 81 | } |
| 82 | sqlite3_exec(m_db, "PRAGMA synchronous = OFF", 0, 0, &errMsg); |
| 83 | sqlite3_exec(m_db, "PRAGMA journal_mode = WAL", 0, 0, &errMsg); |
| 84 | } |
| 85 | |
| 86 | SqliteStorage::~SqliteStorage() |
| 87 | { |
| 88 | sqlite3_close(m_db); |
| 89 | } |
| 90 | |
| 91 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 92 | SqliteStorage::fullEnumerate(const std::function<void(const Storage::ItemMeta)>& f) |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 93 | { |
| 94 | sqlite3_stmt* m_stmt = 0; |
| 95 | int rc = SQLITE_DONE; |
| 96 | string sql = string("SELECT id, name, keylocatorHash FROM NDN_REPO;"); |
| 97 | rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &m_stmt, 0); |
| 98 | if (rc != SQLITE_OK) |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 99 | BOOST_THROW_EXCEPTION(Error("Initiation Read Entries from Database Prepare error")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 100 | int entryNumber = 0; |
| 101 | while (true) { |
| 102 | rc = sqlite3_step(m_stmt); |
| 103 | if (rc == SQLITE_ROW) { |
| 104 | |
| 105 | ItemMeta item; |
Davide Pesavento | 5d66961 | 2017-09-22 23:49:37 -0400 | [diff] [blame^] | 106 | item.fullName.wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_blob(m_stmt, 1)), |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 107 | sqlite3_column_bytes(m_stmt, 1))); |
| 108 | item.id = sqlite3_column_int(m_stmt, 0); |
Davide Pesavento | 5d66961 | 2017-09-22 23:49:37 -0400 | [diff] [blame^] | 109 | item.keyLocatorHash = make_shared<const ndn::Buffer>(sqlite3_column_blob(m_stmt, 3), |
| 110 | sqlite3_column_bytes(m_stmt, 3)); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 111 | |
| 112 | try { |
| 113 | f(item); |
| 114 | } |
Davide Pesavento | 5d66961 | 2017-09-22 23:49:37 -0400 | [diff] [blame^] | 115 | catch (...) { |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 116 | sqlite3_finalize(m_stmt); |
| 117 | throw; |
| 118 | } |
| 119 | entryNumber++; |
| 120 | } |
| 121 | else if (rc == SQLITE_DONE) { |
| 122 | sqlite3_finalize(m_stmt); |
| 123 | break; |
| 124 | } |
| 125 | else { |
| 126 | std::cerr << "Initiation Read Entries rc:" << rc << std::endl; |
| 127 | sqlite3_finalize(m_stmt); |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 128 | BOOST_THROW_EXCEPTION(Error("Initiation Read Entries error")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | m_size = entryNumber; |
| 132 | } |
| 133 | |
| 134 | int64_t |
| 135 | SqliteStorage::insert(const Data& data) |
| 136 | { |
| 137 | Name name = data.getName(); |
| 138 | |
| 139 | Index::Entry entry(data, 0); //the id is not used |
| 140 | int64_t id = -1; |
| 141 | if (name.empty()) { |
| 142 | std::cerr << "name is empty" << std::endl; |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | int rc = 0; |
| 147 | |
| 148 | sqlite3_stmt* insertStmt = 0; |
| 149 | |
| 150 | string insertSql = string("INSERT INTO NDN_REPO (id, name, data, keylocatorHash) " |
| 151 | "VALUES (?, ?, ?, ?)"); |
| 152 | |
| 153 | if (sqlite3_prepare_v2(m_db, insertSql.c_str(), -1, &insertStmt, 0) != SQLITE_OK) { |
| 154 | sqlite3_finalize(insertStmt); |
| 155 | std::cerr << "insert sql not prepared" << std::endl; |
| 156 | } |
| 157 | //Insert |
Alexander Afanasyev | f34a355 | 2017-08-13 21:17:05 -0400 | [diff] [blame] | 158 | auto result = sqlite3_bind_null(insertStmt, 1); |
| 159 | if (result == SQLITE_OK) { |
| 160 | result = sqlite3_bind_blob(insertStmt, 2, |
| 161 | entry.getName().wireEncode().wire(), |
| 162 | entry.getName().wireEncode().size(), SQLITE_STATIC); |
| 163 | } |
| 164 | if (result == SQLITE_OK) { |
| 165 | result = sqlite3_bind_blob(insertStmt, 3, |
| 166 | data.wireEncode().wire(), |
| 167 | data.wireEncode().size(), SQLITE_STATIC); |
| 168 | } |
| 169 | if (result == SQLITE_OK) { |
| 170 | BOOST_ASSERT(entry.getKeyLocatorHash()->size() == ndn::util::Sha256::DIGEST_SIZE); |
| 171 | result = sqlite3_bind_blob(insertStmt, 4, |
| 172 | entry.getKeyLocatorHash()->data(), |
| 173 | entry.getKeyLocatorHash()->size(), SQLITE_STATIC); |
| 174 | } |
| 175 | |
| 176 | if (result == SQLITE_OK) { |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 177 | rc = sqlite3_step(insertStmt); |
| 178 | if (rc == SQLITE_CONSTRAINT) { |
Davide Pesavento | 5d66961 | 2017-09-22 23:49:37 -0400 | [diff] [blame^] | 179 | std::cerr << "Insert failed" << std::endl; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 180 | sqlite3_finalize(insertStmt); |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 181 | BOOST_THROW_EXCEPTION(Error("Insert failed")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 182 | } |
| 183 | sqlite3_reset(insertStmt); |
| 184 | m_size++; |
| 185 | id = sqlite3_last_insert_rowid(m_db); |
| 186 | } |
| 187 | else { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 188 | BOOST_THROW_EXCEPTION(Error("Some error with insert")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | sqlite3_finalize(insertStmt); |
| 192 | return id; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | bool |
| 197 | SqliteStorage::erase(const int64_t id) |
| 198 | { |
| 199 | sqlite3_stmt* deleteStmt = 0; |
| 200 | |
| 201 | string deleteSql = string("DELETE from NDN_REPO where id = ?;"); |
| 202 | |
| 203 | if (sqlite3_prepare_v2(m_db, deleteSql.c_str(), -1, &deleteStmt, 0) != SQLITE_OK) { |
| 204 | sqlite3_finalize(deleteStmt); |
| 205 | std::cerr << "delete statement prepared failed" << std::endl; |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 206 | BOOST_THROW_EXCEPTION(Error("delete statement prepared failed")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | if (sqlite3_bind_int64(deleteStmt, 1, id) == SQLITE_OK) { |
| 210 | int rc = sqlite3_step(deleteStmt); |
| 211 | if (rc != SQLITE_DONE && rc != SQLITE_ROW) { |
| 212 | std::cerr << " node delete error rc:" << rc << std::endl; |
| 213 | sqlite3_finalize(deleteStmt); |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 214 | BOOST_THROW_EXCEPTION(Error(" node delete error")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 215 | } |
| 216 | if (sqlite3_changes(m_db) != 1) |
| 217 | return false; |
| 218 | m_size--; |
| 219 | } |
| 220 | else { |
| 221 | std::cerr << "delete bind error" << std::endl; |
| 222 | sqlite3_finalize(deleteStmt); |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 223 | BOOST_THROW_EXCEPTION(Error("delete bind error")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 224 | } |
| 225 | sqlite3_finalize(deleteStmt); |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | shared_ptr<Data> |
| 231 | SqliteStorage::read(const int64_t id) |
| 232 | { |
| 233 | sqlite3_stmt* queryStmt = 0; |
| 234 | string sql = string("SELECT * FROM NDN_REPO WHERE id = ? ;"); |
| 235 | int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0); |
| 236 | if (rc == SQLITE_OK) { |
| 237 | if (sqlite3_bind_int64(queryStmt, 1, id) == SQLITE_OK) { |
| 238 | rc = sqlite3_step(queryStmt); |
| 239 | if (rc == SQLITE_ROW) { |
Davide Pesavento | 5d66961 | 2017-09-22 23:49:37 -0400 | [diff] [blame^] | 240 | auto data = make_shared<Data>(); |
| 241 | data->wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_blob(queryStmt, 2)), |
| 242 | sqlite3_column_bytes(queryStmt, 2))); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 243 | sqlite3_finalize(queryStmt); |
| 244 | return data; |
| 245 | } |
| 246 | else if (rc == SQLITE_DONE) { |
Davide Pesavento | 5d66961 | 2017-09-22 23:49:37 -0400 | [diff] [blame^] | 247 | return nullptr; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 248 | } |
| 249 | else { |
| 250 | std::cerr << "Database query failure rc:" << rc << std::endl; |
| 251 | sqlite3_finalize(queryStmt); |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 252 | BOOST_THROW_EXCEPTION(Error("Database query failure")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | else { |
| 256 | std::cerr << "select bind error" << std::endl; |
| 257 | sqlite3_finalize(queryStmt); |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 258 | BOOST_THROW_EXCEPTION(Error("select bind error")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 259 | } |
| 260 | sqlite3_finalize(queryStmt); |
| 261 | } |
| 262 | else { |
| 263 | sqlite3_finalize(queryStmt); |
| 264 | std::cerr << "select statement prepared failed" << std::endl; |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 265 | BOOST_THROW_EXCEPTION(Error("select statement prepared failed")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 266 | } |
Davide Pesavento | 5d66961 | 2017-09-22 23:49:37 -0400 | [diff] [blame^] | 267 | return nullptr; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | int64_t |
| 271 | SqliteStorage::size() |
| 272 | { |
| 273 | sqlite3_stmt* queryStmt = 0; |
| 274 | string sql("SELECT count(*) FROM NDN_REPO "); |
| 275 | int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0); |
| 276 | if (rc != SQLITE_OK) |
| 277 | { |
| 278 | std::cerr << "Database query failure rc:" << rc << std::endl; |
| 279 | sqlite3_finalize(queryStmt); |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 280 | BOOST_THROW_EXCEPTION(Error("Database query failure")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | rc = sqlite3_step(queryStmt); |
| 284 | if (rc != SQLITE_ROW) |
| 285 | { |
| 286 | std::cerr << "Database query failure rc:" << rc << std::endl; |
| 287 | sqlite3_finalize(queryStmt); |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 288 | BOOST_THROW_EXCEPTION(Error("Database query failure")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | int64_t nDatas = sqlite3_column_int64(queryStmt, 0); |
| 292 | if (m_size != nDatas) { |
| 293 | std::cerr << "The size of database is not correct! " << std::endl; |
| 294 | } |
| 295 | return nDatas; |
| 296 | } |
| 297 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 298 | } // namespace repo |