blob: 42739c30c75ddee064b81551b24e04129244fa01 [file] [log] [blame]
Weiqi Shif0330d52014-07-09 10:54:27 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevc0e26582017-08-13 21:16:49 -04002/*
Alexander Afanasyev42290b22017-03-09 12:58:29 -08003 * Copyright (c) 2014-2017, Regents of the University of California.
Weiqi Shif0330d52014-07-09 10:54:27 -07004 *
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 Shif0330d52014-07-09 10:54:27 -070020#include "sqlite-storage.hpp"
Davide Pesavento5d669612017-09-22 23:49:37 -040021#include "config.hpp"
Weiqi Shif0330d52014-07-09 10:54:27 -070022#include "index.hpp"
Alexander Afanasyevc0e26582017-08-13 21:16:49 -040023
24#include <ndn-cxx/util/sha256.hpp>
Weiqi Shif0330d52014-07-09 10:54:27 -070025#include <boost/filesystem.hpp>
26#include <istream>
27
28namespace repo {
29
Wentao Shanga8f3c402014-10-30 14:03:27 -070030using std::string;
31
Weiqi Shif0330d52014-07-09 10:54:27 -070032SqliteStorage::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 Afanasyev42290b22017-03-09 12:58:29 -080045 BOOST_THROW_EXCEPTION(Error("Folder '" + dbPath + "' does not exists and cannot be created"));
Weiqi Shif0330d52014-07-09 10:54:27 -070046 }
47 }
48
49 m_dbPath = dbPath + "/ndn_repo.db";
50 }
51 initializeRepo();
52}
53
54
55void
56SqliteStorage::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 Afanasyev42290b22017-03-09 12:58:29 -080080 BOOST_THROW_EXCEPTION(Error("Database file open failure"));
Weiqi Shif0330d52014-07-09 10:54:27 -070081 }
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
86SqliteStorage::~SqliteStorage()
87{
88 sqlite3_close(m_db);
89}
90
91void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080092SqliteStorage::fullEnumerate(const std::function<void(const Storage::ItemMeta)>& f)
Weiqi Shif0330d52014-07-09 10:54:27 -070093{
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 Afanasyev42290b22017-03-09 12:58:29 -080099 BOOST_THROW_EXCEPTION(Error("Initiation Read Entries from Database Prepare error"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700100 int entryNumber = 0;
101 while (true) {
102 rc = sqlite3_step(m_stmt);
103 if (rc == SQLITE_ROW) {
104
105 ItemMeta item;
Davide Pesavento5d669612017-09-22 23:49:37 -0400106 item.fullName.wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_blob(m_stmt, 1)),
Weiqi Shif0330d52014-07-09 10:54:27 -0700107 sqlite3_column_bytes(m_stmt, 1)));
108 item.id = sqlite3_column_int(m_stmt, 0);
Davide Pesavento5d669612017-09-22 23:49:37 -0400109 item.keyLocatorHash = make_shared<const ndn::Buffer>(sqlite3_column_blob(m_stmt, 3),
110 sqlite3_column_bytes(m_stmt, 3));
Weiqi Shif0330d52014-07-09 10:54:27 -0700111
112 try {
113 f(item);
114 }
Davide Pesavento5d669612017-09-22 23:49:37 -0400115 catch (...) {
Weiqi Shif0330d52014-07-09 10:54:27 -0700116 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 Afanasyev42290b22017-03-09 12:58:29 -0800128 BOOST_THROW_EXCEPTION(Error("Initiation Read Entries error"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700129 }
130 }
131 m_size = entryNumber;
132}
133
134int64_t
135SqliteStorage::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 Afanasyevf34a3552017-08-13 21:17:05 -0400158 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 Shif0330d52014-07-09 10:54:27 -0700177 rc = sqlite3_step(insertStmt);
178 if (rc == SQLITE_CONSTRAINT) {
Davide Pesavento5d669612017-09-22 23:49:37 -0400179 std::cerr << "Insert failed" << std::endl;
Weiqi Shif0330d52014-07-09 10:54:27 -0700180 sqlite3_finalize(insertStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800181 BOOST_THROW_EXCEPTION(Error("Insert failed"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700182 }
183 sqlite3_reset(insertStmt);
184 m_size++;
185 id = sqlite3_last_insert_rowid(m_db);
186 }
187 else {
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800188 BOOST_THROW_EXCEPTION(Error("Some error with insert"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700189 }
190
191 sqlite3_finalize(insertStmt);
192 return id;
193}
194
195
196bool
197SqliteStorage::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 Afanasyev42290b22017-03-09 12:58:29 -0800206 BOOST_THROW_EXCEPTION(Error("delete statement prepared failed"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700207 }
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 Afanasyev42290b22017-03-09 12:58:29 -0800214 BOOST_THROW_EXCEPTION(Error(" node delete error"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700215 }
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 Afanasyev42290b22017-03-09 12:58:29 -0800223 BOOST_THROW_EXCEPTION(Error("delete bind error"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700224 }
225 sqlite3_finalize(deleteStmt);
226 return true;
227}
228
229
230shared_ptr<Data>
231SqliteStorage::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 Pesavento5d669612017-09-22 23:49:37 -0400240 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 Shif0330d52014-07-09 10:54:27 -0700243 sqlite3_finalize(queryStmt);
244 return data;
245 }
246 else if (rc == SQLITE_DONE) {
Davide Pesavento5d669612017-09-22 23:49:37 -0400247 return nullptr;
Weiqi Shif0330d52014-07-09 10:54:27 -0700248 }
249 else {
250 std::cerr << "Database query failure rc:" << rc << std::endl;
251 sqlite3_finalize(queryStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800252 BOOST_THROW_EXCEPTION(Error("Database query failure"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700253 }
254 }
255 else {
256 std::cerr << "select bind error" << std::endl;
257 sqlite3_finalize(queryStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800258 BOOST_THROW_EXCEPTION(Error("select bind error"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700259 }
260 sqlite3_finalize(queryStmt);
261 }
262 else {
263 sqlite3_finalize(queryStmt);
264 std::cerr << "select statement prepared failed" << std::endl;
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800265 BOOST_THROW_EXCEPTION(Error("select statement prepared failed"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700266 }
Davide Pesavento5d669612017-09-22 23:49:37 -0400267 return nullptr;
Weiqi Shif0330d52014-07-09 10:54:27 -0700268}
269
270int64_t
271SqliteStorage::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 Afanasyev42290b22017-03-09 12:58:29 -0800280 BOOST_THROW_EXCEPTION(Error("Database query failure"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700281 }
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 Afanasyev42290b22017-03-09 12:58:29 -0800288 BOOST_THROW_EXCEPTION(Error("Database query failure"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700289 }
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 Afanasyev42290b22017-03-09 12:58:29 -0800298} // namespace repo