blob: c24bd92ef6f7203c12c8e918acd192a56c539fe1 [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
41FetchTaskDb::FetchTaskDb(const boost::filesystem::path &folder, const std::string &tag)
42{
43 fs::path actualFolder = folder / ".chronoshare" / "fetch_tasks";
44 fs::create_directories (actualFolder);
45
46 int res = sqlite3_open((actualFolder / tag).c_str(), &m_db);
47 if (res != SQLITE_OK)
48 {
49 BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Cannot open database: " + (actualFolder / tag).string()));
50 }
51
52 char *errmsg = 0;
53 res = sqlite3_exec(m_db, INIT_DATABASE.c_str(), NULL, NULL, &errmsg);
54 if (res != SQLITE_OK && errmsg != 0)
55 {
56 // _LOG_TRACE ("Init \"error\": " << errmsg);
57 sqlite3_free (errmsg);
58 }
59 else
60 {
61 }
62}
63
64FetchTaskDb::~FetchTaskDb()
65{
66 int res = sqlite3_close(m_db);
67 if (res != SQLITE_OK)
68 {
69 // _LOG_ERROR
70 }
71}
72
73void
74FetchTaskDb::addTask(const Name &deviceName, const Name &baseName, uint64_t minSeqNo, uint64_t maxSeqNo, int priority)
75{
76 sqlite3_stmt *stmt;
77 sqlite3_prepare_v2(m_db, "INSERT OR IGNORE INTO Task (deviceName, baseName, minSeqNo, maxSeqNo, priority) VALUES (?, ?, ?, ?, ?)", -1, &stmt, 0);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070078 NdnxCharbufPtr deviceBuf = NdnxCharbufPtr(deviceName);
79 NdnxCharbufPtr baseBuf = NdnxCharbufPtr(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
87 if (res == SQLITE_OK)
88 {
89 }
90 sqlite3_finalize(stmt);
91}
92
93void
94FetchTaskDb::deleteTask(const Name &deviceName, const Name &baseName)
95{
96 sqlite3_stmt *stmt;
97 sqlite3_prepare_v2(m_db, "DELETE FROM Task WHERE deviceName = ? AND baseName = ?;", -1, &stmt, 0);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070098 NdnxCharbufPtr deviceBuf = NdnxCharbufPtr(deviceName);
99 NdnxCharbufPtr baseBuf = NdnxCharbufPtr(baseName);
Zhenkai Zhuda686882013-01-29 22:32:24 -0800100 sqlite3_bind_blob(stmt, 1, deviceBuf->buf(), deviceBuf->length(), SQLITE_STATIC);
101 sqlite3_bind_blob(stmt, 2, baseBuf->buf(), baseBuf->length(), SQLITE_STATIC);
102 int res = sqlite3_step(stmt);
103 if (res == SQLITE_OK)
104 {
105 }
106 sqlite3_finalize(stmt);
107}
108
109void
110FetchTaskDb::foreachTask(const FetchTaskCallback &callback)
111{
112 sqlite3_stmt *stmt;
113 sqlite3_prepare_v2(m_db, "SELECT * FROM Task;", -1, &stmt, 0);
114 while (sqlite3_step(stmt) == SQLITE_ROW)
115 {
116 Name deviceName(sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0));
117 Name baseName(sqlite3_column_blob(stmt, 1), sqlite3_column_bytes(stmt, 1));
118 uint64_t minSeqNo = sqlite3_column_int64(stmt, 2);
119 uint64_t maxSeqNo = sqlite3_column_int64(stmt, 3);
120 int priority = sqlite3_column_int(stmt, 4);
121 callback(deviceName, baseName, minSeqNo, maxSeqNo, priority);
122 }
123
124 sqlite3_finalize(stmt);
125}