blob: 034ae66a71fac9532fbc7e61a17cab4161e6bd1b [file] [log] [blame]
Weiqi Shif0330d52014-07-09 10:54:27 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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
20#include "../../build/src/config.hpp"
21#include "sqlite-storage.hpp"
22#include "index.hpp"
Junxiao Shi047a6fb2017-06-08 16:16:05 +000023#include <ndn-cxx/util/crypto.hpp>
Weiqi Shif0330d52014-07-09 10:54:27 -070024#include <boost/filesystem.hpp>
25#include <istream>
26
27namespace repo {
28
Wentao Shanga8f3c402014-10-30 14:03:27 -070029using std::string;
30
Weiqi Shif0330d52014-07-09 10:54:27 -070031SqliteStorage::SqliteStorage(const string& dbPath)
32 : m_size(0)
33{
34 if (dbPath.empty()) {
35 std::cerr << "Create db file in local location [" << dbPath << "]. " << std::endl
36 << "You can assign the path using -d option" << std::endl;
37 m_dbPath = string("ndn_repo.db");
38 }
39 else {
40 boost::filesystem::path fsPath(dbPath);
41 boost::filesystem::file_status fsPathStatus = boost::filesystem::status(fsPath);
42 if (!boost::filesystem::is_directory(fsPathStatus)) {
43 if (!boost::filesystem::create_directory(boost::filesystem::path(fsPath))) {
Alexander Afanasyev42290b22017-03-09 12:58:29 -080044 BOOST_THROW_EXCEPTION(Error("Folder '" + dbPath + "' does not exists and cannot be created"));
Weiqi Shif0330d52014-07-09 10:54:27 -070045 }
46 }
47
48 m_dbPath = dbPath + "/ndn_repo.db";
49 }
50 initializeRepo();
51}
52
53
54void
55SqliteStorage::initializeRepo()
56{
57 char* errMsg = 0;
58
59 int rc = sqlite3_open_v2(m_dbPath.c_str(), &m_db,
60 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
61#ifdef DISABLE_SQLITE3_FS_LOCKING
62 "unix-dotfile"
63#else
64 0
65#endif
66 );
67
68 if (rc == SQLITE_OK) {
69 sqlite3_exec(m_db, "CREATE TABLE NDN_REPO ("
70 "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
71 "name BLOB, "
72 "data BLOB, "
73 "keylocatorHash BLOB);\n "
74 , 0, 0, &errMsg);
75 // Ignore errors (when database already exists, errors are expected)
76 }
77 else {
78 std::cerr << "Database file open failure rc:" << rc << std::endl;
Alexander Afanasyev42290b22017-03-09 12:58:29 -080079 BOOST_THROW_EXCEPTION(Error("Database file open failure"));
Weiqi Shif0330d52014-07-09 10:54:27 -070080 }
81 sqlite3_exec(m_db, "PRAGMA synchronous = OFF", 0, 0, &errMsg);
82 sqlite3_exec(m_db, "PRAGMA journal_mode = WAL", 0, 0, &errMsg);
83}
84
85SqliteStorage::~SqliteStorage()
86{
87 sqlite3_close(m_db);
88}
89
90void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080091SqliteStorage::fullEnumerate(const std::function<void(const Storage::ItemMeta)>& f)
Weiqi Shif0330d52014-07-09 10:54:27 -070092{
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)
Alexander Afanasyev42290b22017-03-09 12:58:29 -080098 BOOST_THROW_EXCEPTION(Error("Initiation Read Entries from Database Prepare error"));
Weiqi Shif0330d52014-07-09 10:54:27 -070099 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);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800127 BOOST_THROW_EXCEPTION(Error("Initiation Read Entries error"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700128 }
129 }
130 m_size = entryNumber;
131}
132
133int64_t
134SqliteStorage::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);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800171 BOOST_THROW_EXCEPTION(Error("Insert failed"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700172 }
173 sqlite3_reset(insertStmt);
174 m_size++;
175 id = sqlite3_last_insert_rowid(m_db);
176 }
177 else {
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800178 BOOST_THROW_EXCEPTION(Error("Some error with insert"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700179 }
180
181 sqlite3_finalize(insertStmt);
182 return id;
183}
184
185
186bool
187SqliteStorage::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;
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800196 BOOST_THROW_EXCEPTION(Error("delete statement prepared failed"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700197 }
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);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800204 BOOST_THROW_EXCEPTION(Error(" node delete error"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700205 }
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);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800213 BOOST_THROW_EXCEPTION(Error("delete bind error"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700214 }
215 sqlite3_finalize(deleteStmt);
216 return true;
217}
218
219
220shared_ptr<Data>
221SqliteStorage::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);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800242 BOOST_THROW_EXCEPTION(Error("Database query failure"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700243 }
244 }
245 else {
246 std::cerr << "select bind error" << std::endl;
247 sqlite3_finalize(queryStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800248 BOOST_THROW_EXCEPTION(Error("select bind error"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700249 }
250 sqlite3_finalize(queryStmt);
251 }
252 else {
253 sqlite3_finalize(queryStmt);
254 std::cerr << "select statement prepared failed" << std::endl;
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800255 BOOST_THROW_EXCEPTION(Error("select statement prepared failed"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700256 }
257 return shared_ptr<Data>();
258}
259
260int64_t
261SqliteStorage::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);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800270 BOOST_THROW_EXCEPTION(Error("Database query failure"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700271 }
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);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800278 BOOST_THROW_EXCEPTION(Error("Database query failure"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700279 }
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
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800288} // namespace repo