blob: 3f7579629dcc916dc09119d6ad1f55f83b25df02 [file] [log] [blame]
Prashanth Swaminathanb2105902015-08-20 14:28:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang19a11d22018-04-12 22:58:20 -07003 * Copyright (c) 2014-2018, Regents of the University of California
Prashanth Swaminathanb2105902015-08-20 14:28:54 -07004 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04005 * 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 Swaminathanb2105902015-08-20 14:28:54 -07007 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04008 * NAC is free software: you can redistribute it and/or modify it under the terms
Prashanth Swaminathanb2105902015-08-20 14:28:54 -07009 * 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 Afanasyev9091d832018-04-18 17:21:08 -040012 * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070013 * 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 Afanasyev9091d832018-04-18 17:21:08 -040017 * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070018 *
19 * @author Prashanth Swaminathan <prashanthsw@gmail.com>
20 */
21
22#include "producer-db.hpp"
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070023#include <ndn-cxx/util/sqlite3-statement.hpp>
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070024#include <iostream>
25#include <boost/filesystem.hpp>
26#include <sqlite3.h>
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070027
28namespace ndn {
Alexander Afanasyev9091d832018-04-18 17:21:08 -040029namespace nac {
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070030
31using util::Sqlite3Statement;
32using time::system_clock;
33
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070034static const std::string INITIALIZATION =R"_DBTEXT_(
35CREATE TABLE IF NOT EXISTS
36 contentkeys(
37 rowId INTEGER PRIMARY KEY,
38 timeslot INTEGER,
39 key BLOB NOT NULL
40 );
41CREATE UNIQUE INDEX IF NOT EXISTS
42 timeslotIndex ON contentkeys(timeslot);)_DBTEXT_";
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070043
44class ProducerDB::Impl
45{
46public:
47 Impl(const std::string& dbPath)
48 {
49 // open Database
50
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070051 int result = sqlite3_open_v2(dbPath.c_str(),
52 &m_database,
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070053 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070054 nullptr);
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070055
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
76public:
77 sqlite3* m_database;
78};
79
80ProducerDB::ProducerDB(const std::string& dbPath)
81 : m_impl(new Impl(dbPath))
82{
83}
84
85ProducerDB::~ProducerDB() = default;
86
87static int32_t
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070088getFixedTimeslot(const system_clock::TimePoint& timeslot)
89{
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070090 return (time::toUnixTimestamp(timeslot)).count() / 3600000;
91}
92
93bool
94ProducerDB::hasContentKey(const system_clock::TimePoint& timeslot) const
95{
96 int32_t fixedTimeslot = getFixedTimeslot(timeslot);
97 Sqlite3Statement statement(m_impl->m_database,
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070098 R"_DBTEXT_(SELECT key FROM contentkeys where timeslot=?)_DBTEXT_");
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070099 statement.bind(1, fixedTimeslot);
100 return (statement.step() == SQLITE_ROW);
101}
102
103
104Buffer
105ProducerDB::getContentKey(const system_clock::TimePoint& timeslot) const
106{
107 int32_t fixedTimeslot = getFixedTimeslot(timeslot);
108 Sqlite3Statement statement(m_impl->m_database,
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700109 R"_DBTEXT_(SELECT key FROM contentkeys where timeslot=?)_DBTEXT_");
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700110 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
122void
123ProducerDB::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 Zhang19a11d22018-04-12 22:58:20 -0700128 R"_DBTEXT_(INSERT INTO contentkeys (timeslot, key)
129 values (?, ?))_DBTEXT_");
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700130 statement.bind(1, fixedTimeslot);
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700131 statement.bind(2, key.data(), key.size(), SQLITE_TRANSIENT);
132 if (statement.step() != SQLITE_DONE) {
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700133 BOOST_THROW_EXCEPTION(Error("Cannot add the key to database"));
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700134 }
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700135}
136
137void
138ProducerDB::deleteContentKey(const system_clock::TimePoint& timeslot)
139{
140 int32_t fixedTimeslot = getFixedTimeslot(timeslot);
141 Sqlite3Statement statement(m_impl->m_database,
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700142 R"_DBTEXT_(DELETE FROM contentkeys WHERE timeslot=?)_DBTEXT_");
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700143 statement.bind(1, fixedTimeslot);
144 statement.step();
145}
146
Alexander Afanasyev9091d832018-04-18 17:21:08 -0400147} // namespace nac
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700148} // namespace ndn