Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 5 | * This file is part of NAC (Name-Based Access Control for NDN). |
| 6 | * See AUTHORS.md for complete list of NAC authors and contributors. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 8 | * NAC is free software: you can redistribute it and/or modify it under the terms |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 12 | * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 17 | * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 18 | * |
| 19 | * @author Prashanth Swaminathan <prashanthsw@gmail.com> |
| 20 | */ |
| 21 | |
| 22 | #include "producer-db.hpp" |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 23 | #include <ndn-cxx/util/sqlite3-statement.hpp> |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 24 | #include <iostream> |
| 25 | #include <boost/filesystem.hpp> |
| 26 | #include <sqlite3.h> |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 29 | namespace nac { |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 30 | |
| 31 | using util::Sqlite3Statement; |
| 32 | using time::system_clock; |
| 33 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 34 | static const std::string INITIALIZATION =R"_DBTEXT_( |
| 35 | CREATE TABLE IF NOT EXISTS |
| 36 | contentkeys( |
| 37 | rowId INTEGER PRIMARY KEY, |
| 38 | timeslot INTEGER, |
| 39 | key BLOB NOT NULL |
| 40 | ); |
| 41 | CREATE UNIQUE INDEX IF NOT EXISTS |
| 42 | timeslotIndex ON contentkeys(timeslot);)_DBTEXT_"; |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 43 | |
| 44 | class ProducerDB::Impl |
| 45 | { |
| 46 | public: |
| 47 | Impl(const std::string& dbPath) |
| 48 | { |
| 49 | // open Database |
| 50 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 51 | int result = sqlite3_open_v2(dbPath.c_str(), |
| 52 | &m_database, |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 53 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 54 | nullptr); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 55 | |
| 56 | if (result != SQLITE_OK) |
| 57 | BOOST_THROW_EXCEPTION(Error("Producer DB cannot be opened/created: " + dbPath)); |
| 58 | |
| 59 | // enable foreign key |
| 60 | sqlite3_exec(m_database, "PRAGMA foreign_keys = ON", nullptr, nullptr, nullptr); |
| 61 | |
| 62 | // initialize database specific tables |
| 63 | char* errorMessage = nullptr; |
| 64 | result = sqlite3_exec(m_database, INITIALIZATION.c_str(), nullptr, nullptr, &errorMessage); |
| 65 | if (result != SQLITE_OK && errorMessage != nullptr) { |
| 66 | sqlite3_free(errorMessage); |
| 67 | BOOST_THROW_EXCEPTION(Error("Producer DB cannot be initialized")); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | ~Impl() |
| 72 | { |
| 73 | sqlite3_close(m_database); |
| 74 | } |
| 75 | |
| 76 | public: |
| 77 | sqlite3* m_database; |
| 78 | }; |
| 79 | |
| 80 | ProducerDB::ProducerDB(const std::string& dbPath) |
| 81 | : m_impl(new Impl(dbPath)) |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | ProducerDB::~ProducerDB() = default; |
| 86 | |
| 87 | static int32_t |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 88 | getFixedTimeslot(const system_clock::TimePoint& timeslot) |
| 89 | { |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 90 | return (time::toUnixTimestamp(timeslot)).count() / 3600000; |
| 91 | } |
| 92 | |
| 93 | bool |
| 94 | ProducerDB::hasContentKey(const system_clock::TimePoint& timeslot) const |
| 95 | { |
| 96 | int32_t fixedTimeslot = getFixedTimeslot(timeslot); |
| 97 | Sqlite3Statement statement(m_impl->m_database, |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 98 | R"_DBTEXT_(SELECT key FROM contentkeys where timeslot=?)_DBTEXT_"); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 99 | statement.bind(1, fixedTimeslot); |
| 100 | return (statement.step() == SQLITE_ROW); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | Buffer |
| 105 | ProducerDB::getContentKey(const system_clock::TimePoint& timeslot) const |
| 106 | { |
| 107 | int32_t fixedTimeslot = getFixedTimeslot(timeslot); |
| 108 | Sqlite3Statement statement(m_impl->m_database, |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 109 | R"_DBTEXT_(SELECT key FROM contentkeys where timeslot=?)_DBTEXT_"); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 110 | statement.bind(1, fixedTimeslot); |
| 111 | |
| 112 | Buffer result; |
| 113 | if (statement.step() == SQLITE_ROW) { |
| 114 | result = Buffer(statement.getBlob(0), statement.getSize(0)); |
| 115 | } |
| 116 | else { |
| 117 | BOOST_THROW_EXCEPTION(Error("Cannot get the key from database")); |
| 118 | } |
| 119 | return result; |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | ProducerDB::addContentKey(const system_clock::TimePoint& timeslot, const Buffer& key) |
| 124 | { |
| 125 | // BOOST_ASSERT(key.length() != 0); |
| 126 | int32_t fixedTimeslot = getFixedTimeslot(timeslot); |
| 127 | Sqlite3Statement statement(m_impl->m_database, |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 128 | R"_DBTEXT_(INSERT INTO contentkeys (timeslot, key) |
| 129 | values (?, ?))_DBTEXT_"); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 130 | statement.bind(1, fixedTimeslot); |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 131 | statement.bind(2, key.data(), key.size(), SQLITE_TRANSIENT); |
| 132 | if (statement.step() != SQLITE_DONE) { |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 133 | BOOST_THROW_EXCEPTION(Error("Cannot add the key to database")); |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 134 | } |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void |
| 138 | ProducerDB::deleteContentKey(const system_clock::TimePoint& timeslot) |
| 139 | { |
| 140 | int32_t fixedTimeslot = getFixedTimeslot(timeslot); |
| 141 | Sqlite3Statement statement(m_impl->m_database, |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 142 | R"_DBTEXT_(DELETE FROM contentkeys WHERE timeslot=?)_DBTEXT_"); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 143 | statement.bind(1, fixedTimeslot); |
| 144 | statement.step(); |
| 145 | } |
| 146 | |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 147 | } // namespace nac |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 148 | } // namespace ndn |