blob: ca8369199f5f890fe06f35abe9148a7896fa8c18 [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 /**
116 * @brief remove the zone
117 * @pre m_zone.getId() > 0
118 * @post m_zone.getId() == 0
119 */
120 void
121 remove(Zone& zone);
122
123public: // Rrset manipulation
124 DEFINE_ERROR(RrsetError, Error);
125
126 /**
127 * @brief add the rrset
128 * @pre m_rrset.getId() == 0
129 * @post m_rrset.getId() > 0
130 */
131 void
132 insert(Rrset& rrset);
133
134 /**
135 * @brief get the data from db according to `m_zone`, `m_label`, `m_type`.
136 *
137 * If record exists, `m_ttl`, `m_version` and `m_data` is set
138 *
139 * @pre m_rrset.getZone().getId() > 0
140 * @post whatever the previous id is,
141 * m_rrset.getId() > 0 if record exists, otherwise m_rrset.getId() == 0
142 * @return true if the record exist
143 */
144 bool
145 find(Rrset& rrset);
146
147 /**
148 * @brief get all the rrsets which is stored at given zone
149 * @throw RrsetError() if zone does not exist in the database
150 * @note if zone.getId() == 0, the function setId for the zone automatically
151 * @note all returned rrsets' m_zone point to the memory of the param[in] zone
152 */
153 std::vector<Rrset>
154 findRrsets(Zone& zone);
155
156 /**
157 * @brief remove the rrset
158 * @pre m_rrset.getId() > 0
159 * @post m_rrset.getId() == 0
160 */
161 void
162 remove(Rrset& rrset);
163
164 /**
165 * @brief replace ttl, version, and Data with new values
166 * @pre m_rrset.getId() > 0
167 */
168 void
169 update(Rrset& rrset);
170
171 ////////////////////////////////
172 ////////getter and setter
173public:
174 const std::string&
175 getDbFile() const
176 {
177 return m_dbFile;
178 }
179
180private:
181 std::string m_dbFile;
182 sqlite3* m_conn;
183};
184
185} // namespace ndns
186} // namespace ndn
187
188#endif // NDNS_DAEMON_DB_MGR_HPP