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