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