blob: 3411dfbace651b85e606579f29ef269ba45bf514 [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"
Alexander Afanasyevc0e26582017-08-13 21:16:49 -040021#include "../../build/src/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;
106 item.fullName.wireDecode(Block(sqlite3_column_blob(m_stmt, 1),
107 sqlite3_column_bytes(m_stmt, 1)));
108 item.id = sqlite3_column_int(m_stmt, 0);
109 item.keyLocatorHash = make_shared<const ndn::Buffer>
110 (ndn::Buffer(sqlite3_column_blob(m_stmt, 3), sqlite3_column_bytes(m_stmt, 3)));
111
112 try {
113 f(item);
114 }
115 catch (...){
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 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
158 if (sqlite3_bind_null(insertStmt, 1) == SQLITE_OK &&
159 sqlite3_bind_blob(insertStmt, 2,
160 entry.getName().wireEncode().wire(),
161 entry.getName().wireEncode().size(), 0) == SQLITE_OK &&
162 sqlite3_bind_blob(insertStmt, 3,
163 data.wireEncode().wire(),
164 data.wireEncode().size(),0 ) == SQLITE_OK &&
165 sqlite3_bind_blob(insertStmt, 4,
166 (const void*)&(*entry.getKeyLocatorHash()),
Alexander Afanasyevc0e26582017-08-13 21:16:49 -0400167 ndn::util::Sha256::DIGEST_SIZE, 0) == SQLITE_OK) {
Weiqi Shif0330d52014-07-09 10:54:27 -0700168 rc = sqlite3_step(insertStmt);
169 if (rc == SQLITE_CONSTRAINT) {
170 std::cerr << "Insert failed" << std::endl;
171 sqlite3_finalize(insertStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800172 BOOST_THROW_EXCEPTION(Error("Insert failed"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700173 }
174 sqlite3_reset(insertStmt);
175 m_size++;
176 id = sqlite3_last_insert_rowid(m_db);
177 }
178 else {
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800179 BOOST_THROW_EXCEPTION(Error("Some error with insert"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700180 }
181
182 sqlite3_finalize(insertStmt);
183 return id;
184}
185
186
187bool
188SqliteStorage::erase(const int64_t id)
189{
190 sqlite3_stmt* deleteStmt = 0;
191
192 string deleteSql = string("DELETE from NDN_REPO where id = ?;");
193
194 if (sqlite3_prepare_v2(m_db, deleteSql.c_str(), -1, &deleteStmt, 0) != SQLITE_OK) {
195 sqlite3_finalize(deleteStmt);
196 std::cerr << "delete statement prepared failed" << std::endl;
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800197 BOOST_THROW_EXCEPTION(Error("delete statement prepared failed"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700198 }
199
200 if (sqlite3_bind_int64(deleteStmt, 1, id) == SQLITE_OK) {
201 int rc = sqlite3_step(deleteStmt);
202 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
203 std::cerr << " node delete error rc:" << rc << std::endl;
204 sqlite3_finalize(deleteStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800205 BOOST_THROW_EXCEPTION(Error(" node delete error"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700206 }
207 if (sqlite3_changes(m_db) != 1)
208 return false;
209 m_size--;
210 }
211 else {
212 std::cerr << "delete bind error" << std::endl;
213 sqlite3_finalize(deleteStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800214 BOOST_THROW_EXCEPTION(Error("delete bind error"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700215 }
216 sqlite3_finalize(deleteStmt);
217 return true;
218}
219
220
221shared_ptr<Data>
222SqliteStorage::read(const int64_t id)
223{
224 sqlite3_stmt* queryStmt = 0;
225 string sql = string("SELECT * FROM NDN_REPO WHERE id = ? ;");
226 int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
227 if (rc == SQLITE_OK) {
228 if (sqlite3_bind_int64(queryStmt, 1, id) == SQLITE_OK) {
229 rc = sqlite3_step(queryStmt);
230 if (rc == SQLITE_ROW) {
231 shared_ptr<Data> data(new Data());
232 data->wireDecode(Block(sqlite3_column_blob(queryStmt, 2),
233 sqlite3_column_bytes(queryStmt, 2)));
234 sqlite3_finalize(queryStmt);
235 return data;
236 }
237 else if (rc == SQLITE_DONE) {
238 return shared_ptr<Data>();
239 }
240 else {
241 std::cerr << "Database query failure rc:" << rc << std::endl;
242 sqlite3_finalize(queryStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800243 BOOST_THROW_EXCEPTION(Error("Database query failure"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700244 }
245 }
246 else {
247 std::cerr << "select bind error" << std::endl;
248 sqlite3_finalize(queryStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800249 BOOST_THROW_EXCEPTION(Error("select bind error"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700250 }
251 sqlite3_finalize(queryStmt);
252 }
253 else {
254 sqlite3_finalize(queryStmt);
255 std::cerr << "select statement prepared failed" << std::endl;
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800256 BOOST_THROW_EXCEPTION(Error("select statement prepared failed"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700257 }
258 return shared_ptr<Data>();
259}
260
261int64_t
262SqliteStorage::size()
263{
264 sqlite3_stmt* queryStmt = 0;
265 string sql("SELECT count(*) FROM NDN_REPO ");
266 int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
267 if (rc != SQLITE_OK)
268 {
269 std::cerr << "Database query failure rc:" << rc << std::endl;
270 sqlite3_finalize(queryStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800271 BOOST_THROW_EXCEPTION(Error("Database query failure"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700272 }
273
274 rc = sqlite3_step(queryStmt);
275 if (rc != SQLITE_ROW)
276 {
277 std::cerr << "Database query failure rc:" << rc << std::endl;
278 sqlite3_finalize(queryStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800279 BOOST_THROW_EXCEPTION(Error("Database query failure"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700280 }
281
282 int64_t nDatas = sqlite3_column_int64(queryStmt, 0);
283 if (m_size != nDatas) {
284 std::cerr << "The size of database is not correct! " << std::endl;
285 }
286 return nDatas;
287}
288
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800289} // namespace repo