blob: 336b3307d3607dbcfda9cd9bda05aef7460fab92 [file] [log] [blame]
Shock Jiang3016c982014-11-11 11:35:17 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
4 *
5 * This file is part of NDNS (Named Data Networking Domain Name Service).
6 * See AUTHORS.md for complete list of NDNS authors and contributors.
7 *
8 * NDNS 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 * NDNS 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 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef NDNS_DAEMON_DB_MGR_HPP
21#define NDNS_DAEMON_DB_MGR_HPP
22
23#include "config.hpp"
24#include "zone.hpp"
25#include "rrset.hpp"
26
27#include <ndn-cxx/common.hpp>
28#include <sqlite3.h>
29
30namespace ndn {
31namespace ndns {
32
33#define DEFINE_ERROR(ErrorName, Base) \
34class ErrorName : public Base \
35{ \
36 public: \
37 explicit \
38 ErrorName(const std::string& what) \
39 : Base(what) \
40 { \
41 } \
42};
43
44
45
46/**
47 * @brief Database Manager, provides CRUD operations on stored entities
48 *
49 * @note Method names follow MongoDB convention: insert/remove/find/update
50 */
51class DbMgr : noncopyable
52{
53public:
54
55 /**
56 * @brief The Database Status
57 */
58 enum DbStatus {
59 DB_CONNECTED,
60 DB_CLOSED,
61 DB_ERROR
62 };
63
64 DEFINE_ERROR(Error, std::runtime_error);
65 DEFINE_ERROR(PrepareError, Error);
66 DEFINE_ERROR(ExecuteError, Error);
67 DEFINE_ERROR(ConnectError, Error);
68
69public:
70 explicit
71 DbMgr(const std::string& dbFile = DEFAULT_DATABASE_PATH "/" "ndns.db");
72
73 ~DbMgr();
74
75 /**
76 * @brief connect to the database. If it's already opened, do nothing.
77 */
78 void
79 open();
80
81 /**
82 * @brief close the database connection. Do nothing if it's already closed.
83 * Destructor would automatically close the database connection as well.
84 */
85 void
86 close();
87
88 /**
89 * @brief clear all the data in the database
90 */
91 void
92 clearAllData();
93
94public: // Zone manipulation
95 DEFINE_ERROR(ZoneError, Error);
96
97 /**
98 * @brief insert the m_zone to the database, and set the zone's id.
99 * If the zone is already in the db, handle the exception without leaving it to upper level,
100 * meanwhile, set the zone's id too.
101 * @pre m_zone.getId() == 0
102 * @post m_zone.getId() > 0
103 */
104 void
105 insert(Zone& zone);
106
107 /**
108 * @brief lookup the zone by name, fill the m_id and m_ttl
109 * @post whatever the previous id is
110 * @return true if the record exist
111 */
112 bool
113 find(Zone& zone);
114
115 /**
Jiewen Tan870b29b2014-11-17 19:09:49 -0800116 * @brief get all zones in the database
117 */
118 std::vector<Zone>
119 listZones();
120
121 /**
Shock Jiang3016c982014-11-11 11:35:17 -0800122 * @brief remove the zone
123 * @pre m_zone.getId() > 0
124 * @post m_zone.getId() == 0
125 */
126 void
127 remove(Zone& zone);
128
129public: // Rrset manipulation
130 DEFINE_ERROR(RrsetError, Error);
131
132 /**
133 * @brief add the rrset
134 * @pre m_rrset.getId() == 0
135 * @post m_rrset.getId() > 0
136 */
137 void
138 insert(Rrset& rrset);
139
140 /**
141 * @brief get the data from db according to `m_zone`, `m_label`, `m_type`.
142 *
143 * If record exists, `m_ttl`, `m_version` and `m_data` is set
144 *
145 * @pre m_rrset.getZone().getId() > 0
146 * @post whatever the previous id is,
147 * m_rrset.getId() > 0 if record exists, otherwise m_rrset.getId() == 0
148 * @return true if the record exist
149 */
150 bool
151 find(Rrset& rrset);
152
153 /**
154 * @brief get all the rrsets which is stored at given zone
155 * @throw RrsetError() if zone does not exist in the database
156 * @note if zone.getId() == 0, the function setId for the zone automatically
157 * @note all returned rrsets' m_zone point to the memory of the param[in] zone
158 */
159 std::vector<Rrset>
160 findRrsets(Zone& zone);
161
162 /**
163 * @brief remove the rrset
164 * @pre m_rrset.getId() > 0
165 * @post m_rrset.getId() == 0
166 */
167 void
168 remove(Rrset& rrset);
169
170 /**
171 * @brief replace ttl, version, and Data with new values
172 * @pre m_rrset.getId() > 0
173 */
174 void
175 update(Rrset& rrset);
176
177 ////////////////////////////////
178 ////////getter and setter
179public:
180 const std::string&
181 getDbFile() const
182 {
183 return m_dbFile;
184 }
185
186private:
187 std::string m_dbFile;
188 sqlite3* m_conn;
189};
190
191} // namespace ndns
192} // namespace ndn
193
194#endif // NDNS_DAEMON_DB_MGR_HPP