Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 19 | */ |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 20 | #include "fetch-task-db.hpp" |
| 21 | #include "db-helper.hpp" |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 22 | |
| 23 | using namespace std; |
| 24 | using namespace boost; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 25 | using namespace Ndnx; |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 26 | namespace fs = boost::filesystem; |
| 27 | |
| 28 | const string INIT_DATABASE = "\ |
| 29 | CREATE TABLE IF NOT EXISTS \n\ |
| 30 | Task( \n\ |
| 31 | deviceName BLOB NOT NULL, \n\ |
| 32 | baseName BLOB NOT NULL, \n\ |
| 33 | minSeqNo INTEGER, \n\ |
| 34 | maxSeqNo INTEGER, \n\ |
| 35 | priority INTEGER, \n\ |
| 36 | PRIMARY KEY (deviceName, baseName) \n\ |
| 37 | ); \n\ |
| 38 | CREATE INDEX identifier ON Task (deviceName, baseName); \n\ |
| 39 | "; |
| 40 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 41 | FetchTaskDb::FetchTaskDb(const boost::filesystem::path& folder, const std::string& tag) |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 42 | { |
| 43 | fs::path actualFolder = folder / ".chronoshare" / "fetch_tasks"; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 44 | fs::create_directories(actualFolder); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 45 | |
| 46 | int res = sqlite3_open((actualFolder / tag).c_str(), &m_db); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 47 | if (res != SQLITE_OK) { |
| 48 | BOOST_THROW_EXCEPTION( |
| 49 | Error::Db() << errmsg_info_str("Cannot open database: " + (actualFolder / tag).string())); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 52 | char* errmsg = 0; |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 53 | res = sqlite3_exec(m_db, INIT_DATABASE.c_str(), NULL, NULL, &errmsg); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 54 | if (res != SQLITE_OK && errmsg != 0) { |
| 55 | // _LOG_TRACE ("Init \"error\": " << errmsg); |
| 56 | sqlite3_free(errmsg); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 57 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 58 | else { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | FetchTaskDb::~FetchTaskDb() |
| 63 | { |
| 64 | int res = sqlite3_close(m_db); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 65 | if (res != SQLITE_OK) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 66 | // _LOG_ERROR |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 71 | FetchTaskDb::addTask(const Name& deviceName, const Name& baseName, uint64_t minSeqNo, |
| 72 | uint64_t maxSeqNo, int priority) |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 73 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 74 | sqlite3_stmt* stmt; |
| 75 | sqlite3_prepare_v2(m_db, |
| 76 | "INSERT OR IGNORE INTO Task (deviceName, baseName, minSeqNo, maxSeqNo, priority) VALUES (?, ?, ?, ?, ?)", |
| 77 | -1, &stmt, 0); |
| 78 | CcnxCharbufPtr deviceBuf = CcnxCharbufPtr(deviceName); |
| 79 | CcnxCharbufPtr baseBuf = CcnxCharbufPtr(baseName); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 80 | sqlite3_bind_blob(stmt, 1, deviceBuf->buf(), deviceBuf->length(), SQLITE_STATIC); |
| 81 | sqlite3_bind_blob(stmt, 2, baseBuf->buf(), baseBuf->length(), SQLITE_STATIC); |
| 82 | sqlite3_bind_int64(stmt, 3, minSeqNo); |
| 83 | sqlite3_bind_int64(stmt, 4, maxSeqNo); |
| 84 | sqlite3_bind_int(stmt, 5, priority); |
| 85 | int res = sqlite3_step(stmt); |
| 86 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 87 | if (res == SQLITE_OK) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 88 | } |
| 89 | sqlite3_finalize(stmt); |
| 90 | } |
| 91 | |
| 92 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 93 | FetchTaskDb::deleteTask(const Name& deviceName, const Name& baseName) |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 94 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 95 | sqlite3_stmt* stmt; |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 96 | sqlite3_prepare_v2(m_db, "DELETE FROM Task WHERE deviceName = ? AND baseName = ?;", -1, &stmt, 0); |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 97 | NdnxCharbufPtr deviceBuf = NdnxCharbufPtr(deviceName); |
| 98 | NdnxCharbufPtr baseBuf = NdnxCharbufPtr(baseName); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 99 | sqlite3_bind_blob(stmt, 1, deviceBuf->buf(), deviceBuf->length(), SQLITE_STATIC); |
| 100 | sqlite3_bind_blob(stmt, 2, baseBuf->buf(), baseBuf->length(), SQLITE_STATIC); |
| 101 | int res = sqlite3_step(stmt); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 102 | if (res == SQLITE_OK) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 103 | } |
| 104 | sqlite3_finalize(stmt); |
| 105 | } |
| 106 | |
| 107 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 108 | FetchTaskDb::foreachTask(const FetchTaskCallback& callback) |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 109 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 110 | sqlite3_stmt* stmt; |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 111 | sqlite3_prepare_v2(m_db, "SELECT * FROM Task;", -1, &stmt, 0); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 112 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 113 | Name deviceName(sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0)); |
| 114 | Name baseName(sqlite3_column_blob(stmt, 1), sqlite3_column_bytes(stmt, 1)); |
| 115 | uint64_t minSeqNo = sqlite3_column_int64(stmt, 2); |
| 116 | uint64_t maxSeqNo = sqlite3_column_int64(stmt, 3); |
| 117 | int priority = sqlite3_column_int(stmt, 4); |
| 118 | callback(deviceName, baseName, minSeqNo, maxSeqNo, priority); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | sqlite3_finalize(stmt); |
| 122 | } |