blob: 912f063167ca2eb43528e2c24e74d151a7cea82d [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, 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 */
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080020#include "fetch-task-db.hpp"
21#include "db-helper.hpp"
Zhenkai Zhuda686882013-01-29 22:32:24 -080022
23using namespace std;
24using namespace boost;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070025using namespace Ndnx;
Zhenkai Zhuda686882013-01-29 22:32:24 -080026namespace fs = boost::filesystem;
27
28const string INIT_DATABASE = "\
29CREATE 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\
38CREATE INDEX identifier ON Task (deviceName, baseName); \n\
39";
40
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080041FetchTaskDb::FetchTaskDb(const boost::filesystem::path& folder, const std::string& tag)
Zhenkai Zhuda686882013-01-29 22:32:24 -080042{
43 fs::path actualFolder = folder / ".chronoshare" / "fetch_tasks";
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080044 fs::create_directories(actualFolder);
Zhenkai Zhuda686882013-01-29 22:32:24 -080045
46 int res = sqlite3_open((actualFolder / tag).c_str(), &m_db);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080047 if (res != SQLITE_OK) {
48 BOOST_THROW_EXCEPTION(
49 Error::Db() << errmsg_info_str("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) {
55 // _LOG_TRACE ("Init \"error\": " << errmsg);
56 sqlite3_free(errmsg);
Zhenkai Zhuda686882013-01-29 22:32:24 -080057 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080058 else {
Zhenkai Zhuda686882013-01-29 22:32:24 -080059 }
60}
61
62FetchTaskDb::~FetchTaskDb()
63{
64 int res = sqlite3_close(m_db);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080065 if (res != SQLITE_OK) {
Zhenkai Zhuda686882013-01-29 22:32:24 -080066 // _LOG_ERROR
67 }
68}
69
70void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080071FetchTaskDb::addTask(const Name& deviceName, const Name& baseName, uint64_t minSeqNo,
72 uint64_t maxSeqNo, int priority)
Zhenkai Zhuda686882013-01-29 22:32:24 -080073{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080074 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 Zhuda686882013-01-29 22:32:24 -080080 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 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);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070097 NdnxCharbufPtr deviceBuf = NdnxCharbufPtr(deviceName);
98 NdnxCharbufPtr baseBuf = NdnxCharbufPtr(baseName);
Zhenkai Zhuda686882013-01-29 22:32:24 -080099 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 Afanasyeveda3b7a2016-12-25 11:26:40 -0800102 if (res == SQLITE_OK) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800103 }
104 sqlite3_finalize(stmt);
105}
106
107void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800108FetchTaskDb::foreachTask(const FetchTaskCallback& callback)
Zhenkai Zhuda686882013-01-29 22:32:24 -0800109{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800110 sqlite3_stmt* stmt;
Zhenkai Zhuda686882013-01-29 22:32:24 -0800111 sqlite3_prepare_v2(m_db, "SELECT * FROM Task;", -1, &stmt, 0);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800112 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 Zhuda686882013-01-29 22:32:24 -0800119 }
120
121 sqlite3_finalize(stmt);
122}