Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, 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 | */ |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 20 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "fetch-task-db.hpp" |
| 22 | #include "db-helper.hpp" |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 23 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 24 | namespace ndn { |
| 25 | namespace chronoshare { |
| 26 | |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 27 | namespace fs = boost::filesystem; |
| 28 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 29 | const std::string INIT_DATABASE = "\ |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 30 | CREATE TABLE IF NOT EXISTS \n\ |
| 31 | Task( \n\ |
| 32 | deviceName BLOB NOT NULL, \n\ |
| 33 | baseName BLOB NOT NULL, \n\ |
| 34 | minSeqNo INTEGER, \n\ |
| 35 | maxSeqNo INTEGER, \n\ |
| 36 | priority INTEGER, \n\ |
| 37 | PRIMARY KEY (deviceName, baseName) \n\ |
| 38 | ); \n\ |
| 39 | CREATE INDEX identifier ON Task (deviceName, baseName); \n\ |
| 40 | "; |
| 41 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 42 | FetchTaskDb::FetchTaskDb(const boost::filesystem::path& folder, const std::string& tag) |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 43 | { |
| 44 | fs::path actualFolder = folder / ".chronoshare" / "fetch_tasks"; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 45 | fs::create_directories(actualFolder); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 46 | |
| 47 | int res = sqlite3_open((actualFolder / tag).c_str(), &m_db); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 48 | if (res != SQLITE_OK) { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 49 | BOOST_THROW_EXCEPTION(Error("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) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 55 | sqlite3_free(errmsg); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 56 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 57 | else { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | FetchTaskDb::~FetchTaskDb() |
| 62 | { |
| 63 | int res = sqlite3_close(m_db); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 64 | if (res != SQLITE_OK) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 65 | // _LOG_ERROR |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 70 | FetchTaskDb::addTask(const Name& deviceName, const Name& baseName, uint64_t minSeqNo, |
| 71 | uint64_t maxSeqNo, int priority) |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 72 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 73 | sqlite3_stmt* stmt; |
| 74 | sqlite3_prepare_v2(m_db, |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 75 | "INSERT OR IGNORE INTO Task(deviceName, baseName, minSeqNo, maxSeqNo, priority) VALUES(?, ?, ?, ?, ?)", |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 76 | -1, &stmt, 0); |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 77 | |
| 78 | sqlite3_bind_blob(stmt, 1, deviceName.wireEncode().wire(), deviceName.wireEncode().size(), |
| 79 | SQLITE_STATIC); |
| 80 | sqlite3_bind_blob(stmt, 2, baseName.wireEncode().wire(), baseName.wireEncode().size(), |
| 81 | SQLITE_STATIC); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 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); |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 97 | |
| 98 | sqlite3_bind_blob(stmt, 1, deviceName.wireEncode().wire(), deviceName.wireEncode().size(), |
| 99 | SQLITE_STATIC); |
| 100 | sqlite3_bind_blob(stmt, 2, baseName.wireEncode().wire(), baseName.wireEncode().size(), |
| 101 | SQLITE_STATIC); |
| 102 | |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 103 | int res = sqlite3_step(stmt); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 104 | if (res == SQLITE_OK) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 105 | } |
| 106 | sqlite3_finalize(stmt); |
| 107 | } |
| 108 | |
| 109 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 110 | FetchTaskDb::foreachTask(const FetchTaskCallback& callback) |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 111 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 112 | sqlite3_stmt* stmt; |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 113 | sqlite3_prepare_v2(m_db, "SELECT * FROM Task;", -1, &stmt, 0); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 114 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 115 | Name deviceName(Block(sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0))); |
| 116 | Name baseName(Block(sqlite3_column_blob(stmt, 1), sqlite3_column_bytes(stmt, 1))); |
| 117 | |
| 118 | std::cout << "deviceName: " << deviceName << " baseName: " << baseName << std::endl; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 119 | uint64_t minSeqNo = sqlite3_column_int64(stmt, 2); |
| 120 | uint64_t maxSeqNo = sqlite3_column_int64(stmt, 3); |
| 121 | int priority = sqlite3_column_int(stmt, 4); |
| 122 | callback(deviceName, baseName, minSeqNo, maxSeqNo, priority); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | sqlite3_finalize(stmt); |
| 126 | } |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 127 | |
| 128 | } // namespace chronoshare |
| 129 | } // namespace ndn |