Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | #include "fetch-task-db.h" |
| 22 | #include "db-helper.h" |
| 23 | |
| 24 | using namespace std; |
| 25 | using namespace boost; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 26 | using namespace Ndnx; |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 27 | namespace fs = boost::filesystem; |
| 28 | |
| 29 | const string INIT_DATABASE = "\ |
| 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 | |
| 42 | FetchTaskDb::FetchTaskDb(const boost::filesystem::path &folder, const std::string &tag) |
| 43 | { |
| 44 | fs::path actualFolder = folder / ".chronoshare" / "fetch_tasks"; |
| 45 | fs::create_directories (actualFolder); |
| 46 | |
| 47 | int res = sqlite3_open((actualFolder / tag).c_str(), &m_db); |
| 48 | if (res != SQLITE_OK) |
| 49 | { |
| 50 | BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Cannot open database: " + (actualFolder / tag).string())); |
| 51 | } |
| 52 | |
| 53 | char *errmsg = 0; |
| 54 | res = sqlite3_exec(m_db, INIT_DATABASE.c_str(), NULL, NULL, &errmsg); |
| 55 | if (res != SQLITE_OK && errmsg != 0) |
| 56 | { |
| 57 | // _LOG_TRACE ("Init \"error\": " << errmsg); |
| 58 | sqlite3_free (errmsg); |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | FetchTaskDb::~FetchTaskDb() |
| 66 | { |
| 67 | int res = sqlite3_close(m_db); |
| 68 | if (res != SQLITE_OK) |
| 69 | { |
| 70 | // _LOG_ERROR |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | FetchTaskDb::addTask(const Name &deviceName, const Name &baseName, uint64_t minSeqNo, uint64_t maxSeqNo, int priority) |
| 76 | { |
| 77 | sqlite3_stmt *stmt; |
| 78 | sqlite3_prepare_v2(m_db, "INSERT OR IGNORE INTO Task (deviceName, baseName, minSeqNo, maxSeqNo, priority) VALUES (?, ?, ?, ?, ?)", -1, &stmt, 0); |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 79 | NdnxCharbufPtr deviceBuf = NdnxCharbufPtr(deviceName); |
| 80 | NdnxCharbufPtr baseBuf = NdnxCharbufPtr(baseName); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 81 | sqlite3_bind_blob(stmt, 1, deviceBuf->buf(), deviceBuf->length(), SQLITE_STATIC); |
| 82 | sqlite3_bind_blob(stmt, 2, baseBuf->buf(), baseBuf->length(), SQLITE_STATIC); |
| 83 | sqlite3_bind_int64(stmt, 3, minSeqNo); |
| 84 | sqlite3_bind_int64(stmt, 4, maxSeqNo); |
| 85 | sqlite3_bind_int(stmt, 5, priority); |
| 86 | int res = sqlite3_step(stmt); |
| 87 | |
| 88 | if (res == SQLITE_OK) |
| 89 | { |
| 90 | } |
| 91 | sqlite3_finalize(stmt); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | FetchTaskDb::deleteTask(const Name &deviceName, const Name &baseName) |
| 96 | { |
| 97 | sqlite3_stmt *stmt; |
| 98 | 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] | 99 | NdnxCharbufPtr deviceBuf = NdnxCharbufPtr(deviceName); |
| 100 | NdnxCharbufPtr baseBuf = NdnxCharbufPtr(baseName); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 101 | sqlite3_bind_blob(stmt, 1, deviceBuf->buf(), deviceBuf->length(), SQLITE_STATIC); |
| 102 | sqlite3_bind_blob(stmt, 2, baseBuf->buf(), baseBuf->length(), SQLITE_STATIC); |
| 103 | int res = sqlite3_step(stmt); |
| 104 | if (res == SQLITE_OK) |
| 105 | { |
| 106 | } |
| 107 | sqlite3_finalize(stmt); |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | FetchTaskDb::foreachTask(const FetchTaskCallback &callback) |
| 112 | { |
| 113 | sqlite3_stmt *stmt; |
| 114 | sqlite3_prepare_v2(m_db, "SELECT * FROM Task;", -1, &stmt, 0); |
| 115 | while (sqlite3_step(stmt) == SQLITE_ROW) |
| 116 | { |
| 117 | Name deviceName(sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0)); |
| 118 | Name baseName(sqlite3_column_blob(stmt, 1), sqlite3_column_bytes(stmt, 1)); |
| 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); |
| 123 | } |
| 124 | |
| 125 | sqlite3_finalize(stmt); |
| 126 | } |