blob: ae380eaa690e43935f69a0f805ed2c7aeedf5a29 [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"
23#include <boost/filesystem.hpp>
24#include <istream>
25
26namespace repo {
27
Wentao Shanga8f3c402014-10-30 14:03:27 -070028using std::string;
29
Weiqi Shif0330d52014-07-09 10:54:27 -070030SqliteStorage::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))) {
Alexander Afanasyev42290b22017-03-09 12:58:29 -080043 BOOST_THROW_EXCEPTION(Error("Folder '" + dbPath + "' does not exists and cannot be created"));
Weiqi Shif0330d52014-07-09 10:54:27 -070044 }
45 }
46
47 m_dbPath = dbPath + "/ndn_repo.db";
48 }
49 initializeRepo();
50}
51
52
53void
54SqliteStorage::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;
Alexander Afanasyev42290b22017-03-09 12:58:29 -080078 BOOST_THROW_EXCEPTION(Error("Database file open failure"));
Weiqi Shif0330d52014-07-09 10:54:27 -070079 }
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
84SqliteStorage::~SqliteStorage()
85{
86 sqlite3_close(m_db);
87}
88
89void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080090SqliteStorage::fullEnumerate(const std::function<void(const Storage::ItemMeta)>& f)
Weiqi Shif0330d52014-07-09 10:54:27 -070091{
92 sqlite3_stmt* m_stmt = 0;
93 int rc = SQLITE_DONE;
94 string sql = string("SELECT id, name, keylocatorHash FROM NDN_REPO;");
95 rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &m_stmt, 0);
96 if (rc != SQLITE_OK)
Alexander Afanasyev42290b22017-03-09 12:58:29 -080097 BOOST_THROW_EXCEPTION(Error("Initiation Read Entries from Database Prepare error"));
Weiqi Shif0330d52014-07-09 10:54:27 -070098 int entryNumber = 0;
99 while (true) {
100 rc = sqlite3_step(m_stmt);
101 if (rc == SQLITE_ROW) {
102
103 ItemMeta item;
104 item.fullName.wireDecode(Block(sqlite3_column_blob(m_stmt, 1),
105 sqlite3_column_bytes(m_stmt, 1)));
106 item.id = sqlite3_column_int(m_stmt, 0);
107 item.keyLocatorHash = make_shared<const ndn::Buffer>
108 (ndn::Buffer(sqlite3_column_blob(m_stmt, 3), sqlite3_column_bytes(m_stmt, 3)));
109
110 try {
111 f(item);
112 }
113 catch (...){
114 sqlite3_finalize(m_stmt);
115 throw;
116 }
117 entryNumber++;
118 }
119 else if (rc == SQLITE_DONE) {
120 sqlite3_finalize(m_stmt);
121 break;
122 }
123 else {
124 std::cerr << "Initiation Read Entries rc:" << rc << std::endl;
125 sqlite3_finalize(m_stmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800126 BOOST_THROW_EXCEPTION(Error("Initiation Read Entries error"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700127 }
128 }
129 m_size = entryNumber;
130}
131
132int64_t
133SqliteStorage::insert(const Data& data)
134{
135 Name name = data.getName();
136
137 Index::Entry entry(data, 0); //the id is not used
138 int64_t id = -1;
139 if (name.empty()) {
140 std::cerr << "name is empty" << std::endl;
141 return -1;
142 }
143
144 int rc = 0;
145
146 sqlite3_stmt* insertStmt = 0;
147
148 string insertSql = string("INSERT INTO NDN_REPO (id, name, data, keylocatorHash) "
149 "VALUES (?, ?, ?, ?)");
150
151 if (sqlite3_prepare_v2(m_db, insertSql.c_str(), -1, &insertStmt, 0) != SQLITE_OK) {
152 sqlite3_finalize(insertStmt);
153 std::cerr << "insert sql not prepared" << std::endl;
154 }
155 //Insert
156 if (sqlite3_bind_null(insertStmt, 1) == SQLITE_OK &&
157 sqlite3_bind_blob(insertStmt, 2,
158 entry.getName().wireEncode().wire(),
159 entry.getName().wireEncode().size(), 0) == SQLITE_OK &&
160 sqlite3_bind_blob(insertStmt, 3,
161 data.wireEncode().wire(),
162 data.wireEncode().size(),0 ) == SQLITE_OK &&
163 sqlite3_bind_blob(insertStmt, 4,
164 (const void*)&(*entry.getKeyLocatorHash()),
165 ndn::crypto::SHA256_DIGEST_SIZE,0) == SQLITE_OK) {
166 rc = sqlite3_step(insertStmt);
167 if (rc == SQLITE_CONSTRAINT) {
168 std::cerr << "Insert failed" << std::endl;
169 sqlite3_finalize(insertStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800170 BOOST_THROW_EXCEPTION(Error("Insert failed"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700171 }
172 sqlite3_reset(insertStmt);
173 m_size++;
174 id = sqlite3_last_insert_rowid(m_db);
175 }
176 else {
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800177 BOOST_THROW_EXCEPTION(Error("Some error with insert"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700178 }
179
180 sqlite3_finalize(insertStmt);
181 return id;
182}
183
184
185bool
186SqliteStorage::erase(const int64_t id)
187{
188 sqlite3_stmt* deleteStmt = 0;
189
190 string deleteSql = string("DELETE from NDN_REPO where id = ?;");
191
192 if (sqlite3_prepare_v2(m_db, deleteSql.c_str(), -1, &deleteStmt, 0) != SQLITE_OK) {
193 sqlite3_finalize(deleteStmt);
194 std::cerr << "delete statement prepared failed" << std::endl;
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800195 BOOST_THROW_EXCEPTION(Error("delete statement prepared failed"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700196 }
197
198 if (sqlite3_bind_int64(deleteStmt, 1, id) == SQLITE_OK) {
199 int rc = sqlite3_step(deleteStmt);
200 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
201 std::cerr << " node delete error rc:" << rc << std::endl;
202 sqlite3_finalize(deleteStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800203 BOOST_THROW_EXCEPTION(Error(" node delete error"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700204 }
205 if (sqlite3_changes(m_db) != 1)
206 return false;
207 m_size--;
208 }
209 else {
210 std::cerr << "delete bind error" << std::endl;
211 sqlite3_finalize(deleteStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800212 BOOST_THROW_EXCEPTION(Error("delete bind error"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700213 }
214 sqlite3_finalize(deleteStmt);
215 return true;
216}
217
218
219shared_ptr<Data>
220SqliteStorage::read(const int64_t id)
221{
222 sqlite3_stmt* queryStmt = 0;
223 string sql = string("SELECT * FROM NDN_REPO WHERE id = ? ;");
224 int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
225 if (rc == SQLITE_OK) {
226 if (sqlite3_bind_int64(queryStmt, 1, id) == SQLITE_OK) {
227 rc = sqlite3_step(queryStmt);
228 if (rc == SQLITE_ROW) {
229 shared_ptr<Data> data(new Data());
230 data->wireDecode(Block(sqlite3_column_blob(queryStmt, 2),
231 sqlite3_column_bytes(queryStmt, 2)));
232 sqlite3_finalize(queryStmt);
233 return data;
234 }
235 else if (rc == SQLITE_DONE) {
236 return shared_ptr<Data>();
237 }
238 else {
239 std::cerr << "Database query failure rc:" << rc << std::endl;
240 sqlite3_finalize(queryStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800241 BOOST_THROW_EXCEPTION(Error("Database query failure"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700242 }
243 }
244 else {
245 std::cerr << "select bind error" << std::endl;
246 sqlite3_finalize(queryStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800247 BOOST_THROW_EXCEPTION(Error("select bind error"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700248 }
249 sqlite3_finalize(queryStmt);
250 }
251 else {
252 sqlite3_finalize(queryStmt);
253 std::cerr << "select statement prepared failed" << std::endl;
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800254 BOOST_THROW_EXCEPTION(Error("select statement prepared failed"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700255 }
256 return shared_ptr<Data>();
257}
258
259int64_t
260SqliteStorage::size()
261{
262 sqlite3_stmt* queryStmt = 0;
263 string sql("SELECT count(*) FROM NDN_REPO ");
264 int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
265 if (rc != SQLITE_OK)
266 {
267 std::cerr << "Database query failure rc:" << rc << std::endl;
268 sqlite3_finalize(queryStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800269 BOOST_THROW_EXCEPTION(Error("Database query failure"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700270 }
271
272 rc = sqlite3_step(queryStmt);
273 if (rc != SQLITE_ROW)
274 {
275 std::cerr << "Database query failure rc:" << rc << std::endl;
276 sqlite3_finalize(queryStmt);
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800277 BOOST_THROW_EXCEPTION(Error("Database query failure"));
Weiqi Shif0330d52014-07-09 10:54:27 -0700278 }
279
280 int64_t nDatas = sqlite3_column_int64(queryStmt, 0);
281 if (m_size != nDatas) {
282 std::cerr << "The size of database is not correct! " << std::endl;
283 }
284 return nDatas;
285}
286
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800287} // namespace repo