blob: e753f956503643afcff32f115866d6a79d32963d [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Lijing Wanga697cf22016-12-25 14:44:22 -08003 * Copyright (c) 2013-2017, Regents of the University of California.
Zhenkai Zhuda686882013-01-29 22:32:24 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Zhenkai Zhuda686882013-01-29 22:32:24 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * 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 Zhuda686882013-01-29 22:32:24 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * 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 Zhuda686882013-01-29 22:32:24 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * 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 Zhuda686882013-01-29 22:32:24 -080019 */
Lijing Wanga697cf22016-12-25 14:44:22 -080020
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080021#include "fetch-task-db.hpp"
22#include "db-helper.hpp"
Zhenkai Zhuda686882013-01-29 22:32:24 -080023
Lijing Wanga697cf22016-12-25 14:44:22 -080024namespace ndn {
25namespace chronoshare {
26
Zhenkai Zhuda686882013-01-29 22:32:24 -080027namespace fs = boost::filesystem;
28
Lijing Wanga697cf22016-12-25 14:44:22 -080029const std::string INIT_DATABASE = "\
Zhenkai Zhuda686882013-01-29 22:32:24 -080030CREATE 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\
39CREATE INDEX identifier ON Task (deviceName, baseName); \n\
40";
41
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080042FetchTaskDb::FetchTaskDb(const boost::filesystem::path& folder, const std::string& tag)
Zhenkai Zhuda686882013-01-29 22:32:24 -080043{
44 fs::path actualFolder = folder / ".chronoshare" / "fetch_tasks";
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080045 fs::create_directories(actualFolder);
Zhenkai Zhuda686882013-01-29 22:32:24 -080046
47 int res = sqlite3_open((actualFolder / tag).c_str(), &m_db);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080048 if (res != SQLITE_OK) {
Lijing Wanga697cf22016-12-25 14:44:22 -080049 BOOST_THROW_EXCEPTION(Error("Cannot open database: " + (actualFolder / tag).string()));
Zhenkai Zhuda686882013-01-29 22:32:24 -080050 }
51
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080052 char* errmsg = 0;
Zhenkai Zhuda686882013-01-29 22:32:24 -080053 res = sqlite3_exec(m_db, INIT_DATABASE.c_str(), NULL, NULL, &errmsg);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080054 if (res != SQLITE_OK && errmsg != 0) {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080055 sqlite3_free(errmsg);
Zhenkai Zhuda686882013-01-29 22:32:24 -080056 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080057 else {
Zhenkai Zhuda686882013-01-29 22:32:24 -080058 }
59}
60
61FetchTaskDb::~FetchTaskDb()
62{
63 int res = sqlite3_close(m_db);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080064 if (res != SQLITE_OK) {
Zhenkai Zhuda686882013-01-29 22:32:24 -080065 // _LOG_ERROR
66 }
67}
68
69void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080070FetchTaskDb::addTask(const Name& deviceName, const Name& baseName, uint64_t minSeqNo,
71 uint64_t maxSeqNo, int priority)
Zhenkai Zhuda686882013-01-29 22:32:24 -080072{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080073 sqlite3_stmt* stmt;
74 sqlite3_prepare_v2(m_db,
Lijing Wanga697cf22016-12-25 14:44:22 -080075 "INSERT OR IGNORE INTO Task(deviceName, baseName, minSeqNo, maxSeqNo, priority) VALUES(?, ?, ?, ?, ?)",
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080076 -1, &stmt, 0);
Lijing Wanga697cf22016-12-25 14:44:22 -080077
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 Zhuda686882013-01-29 22:32:24 -080082 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 Afanasyeveda3b7a2016-12-25 11:26:40 -080087 if (res == SQLITE_OK) {
Zhenkai Zhuda686882013-01-29 22:32:24 -080088 }
89 sqlite3_finalize(stmt);
90}
91
92void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080093FetchTaskDb::deleteTask(const Name& deviceName, const Name& baseName)
Zhenkai Zhuda686882013-01-29 22:32:24 -080094{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080095 sqlite3_stmt* stmt;
Zhenkai Zhuda686882013-01-29 22:32:24 -080096 sqlite3_prepare_v2(m_db, "DELETE FROM Task WHERE deviceName = ? AND baseName = ?;", -1, &stmt, 0);
Lijing Wanga697cf22016-12-25 14:44:22 -080097
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 Zhuda686882013-01-29 22:32:24 -0800103 int res = sqlite3_step(stmt);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800104 if (res == SQLITE_OK) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800105 }
106 sqlite3_finalize(stmt);
107}
108
109void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800110FetchTaskDb::foreachTask(const FetchTaskCallback& callback)
Zhenkai Zhuda686882013-01-29 22:32:24 -0800111{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800112 sqlite3_stmt* stmt;
Zhenkai Zhuda686882013-01-29 22:32:24 -0800113 sqlite3_prepare_v2(m_db, "SELECT * FROM Task;", -1, &stmt, 0);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800114 while (sqlite3_step(stmt) == SQLITE_ROW) {
Lijing Wanga697cf22016-12-25 14:44:22 -0800115 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 Afanasyeveda3b7a2016-12-25 11:26:40 -0800119 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 Zhuda686882013-01-29 22:32:24 -0800123 }
124
125 sqlite3_finalize(stmt);
126}
Lijing Wanga697cf22016-12-25 14:44:22 -0800127
128} // namespace chronoshare
129} // namespace ndn