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