database.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __DATABASE_DATASRC_H
  15. #define __DATABASE_DATASRC_H
  16. #include <string>
  17. #include <boost/scoped_ptr.hpp>
  18. #include <dns/rrclass.h>
  19. #include <dns/rrclass.h>
  20. #include <dns/rrset.h>
  21. #include <datasrc/client.h>
  22. #include <dns/name.h>
  23. #include <exceptions/exceptions.h>
  24. #include <map>
  25. #include <set>
  26. namespace isc {
  27. namespace datasrc {
  28. /**
  29. * \brief Abstraction of lowlevel database with DNS data
  30. *
  31. * This class is defines interface to databases. Each supported database
  32. * will provide methods for accessing the data stored there in a generic
  33. * manner. The methods are meant to be low-level, without much or any knowledge
  34. * about DNS and should be possible to translate directly to queries.
  35. *
  36. * On the other hand, how the communication with database is done and in what
  37. * schema (in case of relational/SQL database) is up to the concrete classes.
  38. *
  39. * This class is non-copyable, as copying connections to database makes little
  40. * sense and will not be needed.
  41. *
  42. * \todo Is it true this does not need to be copied? For example the zone
  43. * iterator might need it's own copy. But a virtual clone() method might
  44. * be better for that than copy constructor.
  45. *
  46. * \note The same application may create multiple connections to the same
  47. * database, having multiple instances of this class. If the database
  48. * allows having multiple open queries at one connection, the connection
  49. * class may share it.
  50. */
  51. class DatabaseAccessor : boost::noncopyable {
  52. public:
  53. /**
  54. * Definitions of the fields as they are required to be filled in
  55. * by IteratorContext::getNext()
  56. *
  57. * When implementing getNext(), the columns array should
  58. * be filled with the values as described in this enumeration,
  59. * in this order, i.e. TYPE_COLUMN should be the first element
  60. * (index 0) of the array, TTL_COLUMN should be the second element
  61. * (index 1), etc.
  62. */
  63. enum RecordColumns {
  64. TYPE_COLUMN = 0, ///< The RRType of the record (A/NS/TXT etc.)
  65. TTL_COLUMN = 1, ///< The TTL of the record (a
  66. SIGTYPE_COLUMN = 2, ///< For RRSIG records, this contains the RRTYPE
  67. ///< the RRSIG covers. In the current implementation,
  68. ///< this field is ignored.
  69. RDATA_COLUMN = 3, ///< Full text representation of the record's RDATA
  70. NAME_COLUMN = 4, ///< The domain name of this RR
  71. COLUMN_COUNT = 5 ///< The total number of columns, MUST be value of
  72. ///< the largest other element in this enum plus 1.
  73. };
  74. /**
  75. * Definitions of the fields to be passed to addRecordToZone().
  76. *
  77. * Each derived implementation of addRecordToZone() should expect
  78. * the "columns" vector to be filled with the values as described in this
  79. * enumeration, in this order.
  80. */
  81. enum AddRecordColumns {
  82. ADD_NAME = 0, ///< The owner name of the record (a domain name)
  83. ADD_REV_NAME = 1, ///< Reversed name of NAME (used for DNSSEC)
  84. ADD_TTL = 2, ///< The TTL of the record (in numeric form)
  85. ADD_TYPE = 3, ///< The RRType of the record (A/NS/TXT etc.)
  86. ADD_SIGTYPE = 4, ///< For RRSIG records, this contains the RRTYPE
  87. ///< the RRSIG covers.
  88. ADD_RDATA = 5, ///< Full text representation of the record's RDATA
  89. ADD_COLUMN_COUNT = 6 ///< Number of columns
  90. };
  91. /**
  92. * Definitions of the fields to be passed to deleteRecordInZone().
  93. *
  94. * Each derived implementation of deleteRecordInZone() should expect
  95. * the "params" vector to be filled with the values as described in this
  96. * enumeration, in this order.
  97. */
  98. enum DeleteRecordParams {
  99. DEL_NAME = 0, ///< The owner name of the record (a domain name)
  100. DEL_TYPE = 1, ///< The RRType of the record (A/NS/TXT etc.)
  101. DEL_RDATA = 2, ///< Full text representation of the record's RDATA
  102. DEL_PARAM_COUNT = 3 ///< Number of parameters
  103. };
  104. /**
  105. * \brief Destructor
  106. *
  107. * It is empty, but needs a virtual one, since we will use the derived
  108. * classes in polymorphic way.
  109. */
  110. virtual ~DatabaseAccessor() { }
  111. /**
  112. * \brief Retrieve a zone identifier
  113. *
  114. * This method looks up a zone for the given name in the database. It
  115. * should match only exact zone name (eg. name is equal to the zone's
  116. * apex), as the DatabaseClient will loop trough the labels itself and
  117. * find the most suitable zone.
  118. *
  119. * It is not specified if and what implementation of this method may throw,
  120. * so code should expect anything.
  121. *
  122. * \param name The (fully qualified) domain name of the zone's apex to be
  123. * looked up.
  124. * \return The first part of the result indicates if a matching zone
  125. * was found. In case it was, the second part is internal zone ID.
  126. * This one will be passed to methods finding data in the zone.
  127. * It is not required to keep them, in which case whatever might
  128. * be returned - the ID is only passed back to the database as
  129. * an opaque handle.
  130. */
  131. virtual std::pair<bool, int> getZone(const std::string& name) const = 0;
  132. /**
  133. * \brief This holds the internal context of ZoneIterator for databases
  134. *
  135. * While the ZoneIterator implementation from DatabaseClient does all the
  136. * translation from strings to DNS classes and validation, this class
  137. * holds the pointer to where the database is at reading the data.
  138. *
  139. * It can either hold shared pointer to the connection which created it
  140. * and have some kind of statement inside (in case single database
  141. * connection can handle multiple concurrent SQL statements) or it can
  142. * create a new connection (or, if it is more convenient, the connection
  143. * itself can inherit both from DatabaseConnection and IteratorContext
  144. * and just clone itself).
  145. */
  146. class IteratorContext : public boost::noncopyable {
  147. public:
  148. /**
  149. * \brief Destructor
  150. *
  151. * Virtual destructor, so any descendand class is destroyed correctly.
  152. */
  153. virtual ~IteratorContext() { }
  154. /**
  155. * \brief Function to provide next resource record
  156. *
  157. * This function should provide data about the next resource record
  158. * from the data that is searched. The data is not converted yet.
  159. *
  160. * Depending on how the iterator was constructed, there is a difference
  161. * in behaviour; for a 'full zone iterator', created with
  162. * getAllRecords(), all COLUMN_COUNT elements of the array are
  163. * overwritten.
  164. * For a 'name iterator', created with getRecords(), the column
  165. * NAME_COLUMN is untouched, since what would be added here is by
  166. * definition already known to the caller (it already passes it as
  167. * an argument to getRecords()).
  168. *
  169. * Once this function returns false, any subsequent call to it should
  170. * result in false. The implementation of a derived class must ensure
  171. * it doesn't cause any disruption due to that such as a crash or
  172. * exception.
  173. *
  174. * \note The order of RRs is not strictly set, but the RRs for single
  175. * RRset must not be interleaved with any other RRs (eg. RRsets must be
  176. * "together").
  177. *
  178. * \param columns The data will be returned through here. The order
  179. * is specified by the RecordColumns enum, and the size must be
  180. * COLUMN_COUNT
  181. * \todo Do we consider databases where it is stored in binary blob
  182. * format?
  183. * \throw DataSourceError if there's database-related error. If the
  184. * exception (or any other in case of derived class) is thrown,
  185. * the iterator can't be safely used any more.
  186. * \return true if a record was found, and the columns array was
  187. * updated. false if there was no more data, in which case
  188. * the columns array is untouched.
  189. */
  190. virtual bool getNext(std::string (&columns)[COLUMN_COUNT]) = 0;
  191. };
  192. typedef boost::shared_ptr<IteratorContext> IteratorContextPtr;
  193. /**
  194. * \brief Creates an iterator context for a specific name.
  195. *
  196. * Returns an IteratorContextPtr that contains all records of the
  197. * given name from the given zone.
  198. *
  199. * The implementation of the iterator that is returned may leave the
  200. * NAME_COLUMN column of the array passed to getNext() untouched, as that
  201. * data is already known (it is the same as the name argument here)
  202. *
  203. * \exception any Since any implementation can be used, the caller should
  204. * expect any exception to be thrown.
  205. *
  206. * \param name The name to search for. This should be a FQDN.
  207. * \param id The ID of the zone, returned from getZone().
  208. * \param subdomains If set to true, match subdomains of name instead
  209. * of name itself. It is used to find empty domains and match
  210. * wildcards.
  211. * \return Newly created iterator context. Must not be NULL.
  212. */
  213. virtual IteratorContextPtr getRecords(const std::string& name,
  214. int id,
  215. bool subdomains = false) const = 0;
  216. /**
  217. * \brief Creates an iterator context for the whole zone.
  218. *
  219. * Returns an IteratorContextPtr that contains all records of the
  220. * zone with the given zone id.
  221. *
  222. * Each call to getNext() on the returned iterator should copy all
  223. * column fields of the array that is passed, as defined in the
  224. * RecordColumns enum.
  225. *
  226. * \exception any Since any implementation can be used, the caller should
  227. * expect any exception to be thrown.
  228. *
  229. * \param id The ID of the zone, returned from getZone().
  230. * \return Newly created iterator context. Must not be NULL.
  231. */
  232. virtual IteratorContextPtr getAllRecords(int id) const = 0;
  233. /// Start a transaction for updating a zone.
  234. ///
  235. /// Each derived class version of this method starts a database
  236. /// transaction to make updates to the given name of zone (whose class was
  237. /// specified at the construction of the class).
  238. ///
  239. /// If \c replace is true, any existing records of the zone will be
  240. /// deleted on successful completion of updates (after
  241. /// \c commitUpdateZone()); if it's false, the existing records will be
  242. /// intact unless explicitly deleted by \c deleteRecordInZone().
  243. ///
  244. /// A single \c DatabaseAccessor instance can perform at most one update
  245. /// transaction; a duplicate call to this method before
  246. /// \c commitUpdateZone() or \c rollbackUpdateZone() will result in
  247. /// a \c DataSourceError exception. If multiple update attempts need
  248. /// to be performed concurrently (and if the underlying database allows
  249. /// such operation), separate \c DatabaseAccessor instance must be
  250. /// created.
  251. ///
  252. /// \note The underlying database may not allow concurrent updates to
  253. /// the same database instance even if different "connections" (or
  254. /// something similar specific to the database implementation) are used
  255. /// for different sets of updates. For example, it doesn't seem to be
  256. /// possible for SQLite3 unless different databases are used. MySQL
  257. /// allows concurrent updates to different tables of the same database,
  258. /// but a specific operation may block others. As such, this interface
  259. /// doesn't require derived classes to allow concurrent updates with
  260. /// multiple \c DatabaseAccessor instances; however, the implementation
  261. /// is encouraged to do the best for making it more likely to succeed
  262. /// as long as the underlying database system allows concurrent updates.
  263. ///
  264. /// This method returns a pair of \c bool and \c int. Its first element
  265. /// indicates whether the given name of zone is found. If it's false,
  266. /// the transaction isn't considered to be started; a subsequent call to
  267. /// this method with an existing zone name should succeed. Likewise,
  268. /// if a call to this method results in an exception, the transaction
  269. /// isn't considered to be started. Note also that if the zone is not
  270. /// found this method doesn't try to create a new one in the database.
  271. /// It must have been created by some other means beforehand.
  272. ///
  273. /// The second element is the internal zone ID used for subsequent
  274. /// updates. Depending on implementation details of the actual derived
  275. /// class method, it may be different from the one returned by
  276. /// \c getZone(); for example, a specific implementation may use a
  277. /// completely new zone ID when \c replace is true.
  278. ///
  279. /// \exception DataSourceError Duplicate call to this method, or some
  280. /// internal database related error.
  281. ///
  282. /// \param zone_name A string representation of the zone name to be updated
  283. /// \param replace Whether to replace the entire zone (see above)
  284. ///
  285. /// \return A pair of bool and int, indicating whether the specified zone
  286. /// exists and (if so) the zone ID to be used for the update, respectively.
  287. virtual std::pair<bool, int> startUpdateZone(const std::string& zone_name,
  288. bool replace) = 0;
  289. /// Add a single record to the zone to be updated.
  290. ///
  291. /// This method provides a simple interface to insert a new record
  292. /// (a database "row") to the zone in the update context started by
  293. /// \c startUpdateZone(). The zone to which the record to be added
  294. /// is the one specified at the time of the \c startUpdateZone() call.
  295. ///
  296. /// A successful call to \c startUpdateZone() must have preceded to
  297. /// this call; otherwise a \c DataSourceError exception will be thrown.
  298. ///
  299. /// The row is defined as a vector of strings that has exactly
  300. /// ADD_COLUMN_COUNT number of elements. See AddRecordColumns for
  301. /// the semantics of each element.
  302. ///
  303. /// Derived class methods are not required to check whether the given
  304. /// values in \c columns are valid in terms of the expected semantics;
  305. /// in general, it's the caller's responsibility.
  306. /// For example, TTLs would normally be expected to be a textual
  307. /// representation of decimal numbers, but this interface doesn't require
  308. /// the implementation to perform this level of validation. It may check
  309. /// the values, however, and in that case if it detects an error it
  310. /// should throw a \c DataSourceError exception.
  311. ///
  312. /// Likewise, derived class methods are not required to detect any
  313. /// duplicate record that is already in the zone.
  314. ///
  315. /// \note The underlying database schema may not have a trivial mapping
  316. /// from this style of definition of rows to actual database records.
  317. /// It's the implementation's responsibility to implement the mapping
  318. /// in the actual derived method.
  319. ///
  320. /// \exception DataSourceError Invalid call without starting a transaction,
  321. /// or other internal database error.
  322. ///
  323. /// \param columns An array of strings that defines a record to be added
  324. /// to the zone.
  325. virtual void addRecordToZone(
  326. const std::string (&columns)[ADD_COLUMN_COUNT]) = 0;
  327. /// Delete a single record from the zone to be updated.
  328. ///
  329. /// This method provides a simple interface to delete a record
  330. /// (a database "row") from the zone in the update context started by
  331. /// \c startUpdateZone(). The zone from which the record to be deleted
  332. /// is the one specified at the time of the \c startUpdateZone() call.
  333. ///
  334. /// A successful call to \c startUpdateZone() must have preceded to
  335. /// this call; otherwise a \c DataSourceError exception will be thrown.
  336. ///
  337. /// The record to be deleted is specified by a vector of strings that has
  338. /// exactly DEL_PARAM_COUNT number of elements. See DeleteRecordParams
  339. /// for the semantics of each element.
  340. ///
  341. /// \note In IXFR, TTL may also be specified, but we intentionally
  342. /// ignore that in this interface, because it's not guaranteed
  343. /// that all records have the same TTL (unlike the RRset
  344. /// assumption) and there can even be multiple records for the
  345. /// same name, type and rdata with different TTLs. If we only
  346. /// delete one of them, subsequent lookup will still return a
  347. /// positive answer, which would be confusing. It's a higher
  348. /// layer's responsibility to check if there is at least one
  349. /// record in the database that has the given TTL.
  350. ///
  351. /// Like \c addRecordToZone, derived class methods are not required to
  352. /// validate the semantics of the given parameters or to check if there
  353. /// is a record that matches the specified parameter; if there isn't
  354. /// it simply ignores the result.
  355. ///
  356. /// \exception DataSourceError Invalid call without starting a transaction,
  357. /// or other internal database error.
  358. ///
  359. /// \param params An array of strings that defines a record to be deleted
  360. /// from the zone.
  361. virtual void deleteRecordInZone(
  362. const std::string (&params)[DEL_PARAM_COUNT]) = 0;
  363. /// Commit updates to the zone.
  364. ///
  365. /// This method completes a transaction of making updates to the zone
  366. /// in the context started by startUpdateZone.
  367. ///
  368. /// A successful call to \c startUpdateZone() must have preceded to
  369. /// this call; otherwise a \c DataSourceError exception will be thrown.
  370. /// Once this method successfully completes, the transaction isn't
  371. /// considered to exist any more. So a new transaction can now be
  372. /// started. On the other hand, a duplicate call to this method after
  373. /// a successful completion of it is invalid and should result in
  374. /// a \c DataSourceError exception.
  375. ///
  376. /// If some internal database error happens, a \c DataSourceError
  377. /// exception must be thrown. In that case the transaction is still
  378. /// considered to be valid; the caller must explicitly rollback it
  379. /// or (if it's confident that the error is temporary) try to commit it
  380. /// again.
  381. ///
  382. /// \exception DataSourceError Call without a transaction, duplicate call
  383. /// to the method or internal database error.
  384. virtual void commitUpdateZone() = 0;
  385. /// Rollback updates to the zone made so far.
  386. ///
  387. /// This method rollbacks a transaction of making updates to the zone
  388. /// in the context started by startUpdateZone. When it succeeds
  389. /// (it normally should, but see below), the underlying database should
  390. /// be reverted to the point before performing the corresponding
  391. /// \c startUpdateZone().
  392. ///
  393. /// A successful call to \c startUpdateZone() must have preceded to
  394. /// this call; otherwise a \c DataSourceError exception will be thrown.
  395. /// Once this method successfully completes, the transaction isn't
  396. /// considered to exist any more. So a new transaction can now be
  397. /// started. On the other hand, a duplicate call to this method after
  398. /// a successful completion of it is invalid and should result in
  399. /// a \c DataSourceError exception.
  400. ///
  401. /// Normally this method should not fail. But it may not always be
  402. /// possible to guarantee it depending on the characteristics of the
  403. /// underlying database system. So this interface doesn't require the
  404. /// actual implementation for the error free property. But if a specific
  405. /// implementation of this method can fail, it is encouraged to document
  406. /// when that can happen with its implication.
  407. ///
  408. /// \exception DataSourceError Call without a transaction, duplicate call
  409. /// to the method or internal database error.
  410. virtual void rollbackUpdateZone() = 0;
  411. /// Clone the accessor with the same configuration.
  412. ///
  413. /// Each derived class implementation of this method will create a new
  414. /// accessor of the same derived class with the same configuration
  415. /// (such as the database server address) as that of the caller object
  416. /// and return it.
  417. ///
  418. /// Note that other internal states won't be copied to the new accessor
  419. /// even though the name of "clone" may indicate so. For example, even
  420. /// if the calling accessor is in the middle of a update transaction,
  421. /// the new accessor will not start a transaction to trace the same
  422. /// updates.
  423. ///
  424. /// The intended use case of cloning is to create a separate context
  425. /// where a specific set of database operations can be performed
  426. /// independently from the original accessor. The updater will use it
  427. /// so that multiple updaters can be created concurrently even if the
  428. /// underlying database system doesn't allow running multiple transactions
  429. /// in a single database connection.
  430. ///
  431. /// The underlying database system may not support the functionality
  432. /// that would be needed to implement this method. For example, it
  433. /// may not allow a single thread (or process) to have more than one
  434. /// database connections. In such a case the derived class implementation
  435. /// should throw a \c DataSourceError exception.
  436. ///
  437. /// \return A shared pointer to the cloned accessor.
  438. virtual boost::shared_ptr<DatabaseAccessor> clone() = 0;
  439. /**
  440. * \brief Returns a string identifying this dabase backend
  441. *
  442. * The returned string is mainly intended to be used for
  443. * debugging/logging purposes.
  444. *
  445. * Any implementation is free to choose the exact string content,
  446. * but it is advisable to make it a name that is distinguishable
  447. * from the others.
  448. *
  449. * \return the name of the database
  450. */
  451. virtual const std::string& getDBName() const = 0;
  452. /**
  453. * \brief It returns the previous name in DNSSEC order.
  454. *
  455. * This is used in DatabaseClient::findPreviousName and does more
  456. * or less the real work, except for working on strings.
  457. *
  458. * \param rname The name to ask for previous of, in reversed form.
  459. * We use the reversed form (see isc::dns::Name::reverse),
  460. * because then the case insensitive order of string representation
  461. * and the DNSSEC order correspond (eg. org.example.a is followed
  462. * by org.example.a.b which is followed by org.example.b, etc).
  463. * \param zone_id The zone to look through.
  464. * \return The previous name.
  465. * \note This function must return previous name even in case
  466. * the queried rname does not exist in the zone.
  467. * \note This method must skip under-the-zone-cut data (glue data).
  468. * This might be implemented by looking for NSEC records (as glue
  469. * data don't have them) in the zone or in some other way.
  470. *
  471. * \throw DataSourceError if there's a problem with the database.
  472. * \throw NotImplemented if this database doesn't support DNSSEC
  473. * or there's no previous name for the queried one (the NSECs
  474. * might be missing or the queried name is less or equal the
  475. * apex of the zone).
  476. */
  477. virtual std::string findPreviousName(int zone_id,
  478. const std::string& rname) const = 0;
  479. };
  480. /**
  481. * \brief Concrete data source client oriented at database backends.
  482. *
  483. * This class (together with corresponding versions of ZoneFinder,
  484. * ZoneIterator, etc.) translates high-level data source queries to
  485. * low-level calls on DatabaseAccessor. It calls multiple queries
  486. * if necessary and validates data from the database, allowing the
  487. * DatabaseAccessor to be just simple translation to SQL/other
  488. * queries to database.
  489. *
  490. * While it is possible to subclass it for specific database in case
  491. * of special needs, it is not expected to be needed. This should just
  492. * work as it is with whatever DatabaseAccessor.
  493. */
  494. class DatabaseClient : public DataSourceClient {
  495. public:
  496. /**
  497. * \brief Constructor
  498. *
  499. * It initializes the client with a database via the given accessor.
  500. *
  501. * \exception isc::InvalidParameter if accessor is NULL. It might throw
  502. * standard allocation exception as well, but doesn't throw anything else.
  503. *
  504. * \param rrclass The RR class of the zones that this client will handle.
  505. * \param accessor The accessor to the database to use to get data.
  506. * As the parameter suggests, the client takes ownership of the accessor
  507. * and will delete it when itself deleted.
  508. */
  509. DatabaseClient(isc::dns::RRClass rrclass,
  510. boost::shared_ptr<DatabaseAccessor> accessor);
  511. /**
  512. * \brief Corresponding ZoneFinder implementation
  513. *
  514. * The zone finder implementation for database data sources. Similarly
  515. * to the DatabaseClient, it translates the queries to methods of the
  516. * database.
  517. *
  518. * Application should not come directly in contact with this class
  519. * (it should handle it trough generic ZoneFinder pointer), therefore
  520. * it could be completely hidden in the .cc file. But it is provided
  521. * to allow testing and for rare cases when a database needs slightly
  522. * different handling, so it can be subclassed.
  523. *
  524. * Methods directly corresponds to the ones in ZoneFinder.
  525. */
  526. class Finder : public ZoneFinder {
  527. public:
  528. /**
  529. * \brief Constructor
  530. *
  531. * \param database The database (shared with DatabaseClient) to
  532. * be used for queries (the one asked for ID before).
  533. * \param zone_id The zone ID which was returned from
  534. * DatabaseAccessor::getZone and which will be passed to further
  535. * calls to the database.
  536. * \param origin The name of the origin of this zone. It could query
  537. * it from database, but as the DatabaseClient just searched for
  538. * the zone using the name, it should have it.
  539. */
  540. Finder(boost::shared_ptr<DatabaseAccessor> database, int zone_id,
  541. const isc::dns::Name& origin);
  542. // The following three methods are just implementations of inherited
  543. // ZoneFinder's pure virtual methods.
  544. virtual isc::dns::Name getOrigin() const;
  545. virtual isc::dns::RRClass getClass() const;
  546. /**
  547. * \brief Find an RRset in the datasource
  548. *
  549. * Searches the datasource for an RRset of the given name and
  550. * type. If there is a CNAME at the given name, the CNAME rrset
  551. * is returned.
  552. * (this implementation is not complete, and currently only
  553. * does full matches, CNAMES, and the signatures for matches and
  554. * CNAMEs)
  555. * \note target was used in the original design to handle ANY
  556. * queries. This is not implemented yet, and may use
  557. * target again for that, but it might also use something
  558. * different. It is left in for compatibility at the moment.
  559. * \note options are ignored at this moment
  560. *
  561. * \note Maybe counter intuitively, this method is not a const member
  562. * function. This is intentional; some of the underlying implementations
  563. * are expected to use a database backend, and would internally contain
  564. * some abstraction of "database connection". In the most strict sense
  565. * any (even read only) operation might change the internal state of
  566. * such a connection, and in that sense the operation cannot be considered
  567. * "const". In order to avoid giving a false sense of safety to the
  568. * caller, we indicate a call to this method may have a surprising
  569. * side effect. That said, this view may be too strict and it may
  570. * make sense to say the internal database connection doesn't affect
  571. * external behavior in terms of the interface of this method. As
  572. * we gain more experiences with various kinds of backends we may
  573. * revisit the constness.
  574. *
  575. * \exception DataSourceError when there is a problem reading
  576. * the data from the dabase backend.
  577. * This can be a connection, code, or
  578. * data (parse) error.
  579. *
  580. * \param name The name to find
  581. * \param type The RRType to find
  582. * \param target Unused at this moment
  583. * \param options Options about how to search.
  584. * See ZoneFinder::FindOptions.
  585. */
  586. virtual FindResult find(const isc::dns::Name& name,
  587. const isc::dns::RRType& type,
  588. isc::dns::RRsetList* target = NULL,
  589. const FindOptions options = FIND_DEFAULT);
  590. /**
  591. * \brief Implementation of ZoneFinder::findPreviousName method.
  592. */
  593. virtual isc::dns::Name findPreviousName(const isc::dns::Name& query)
  594. const;
  595. /**
  596. * \brief The zone ID
  597. *
  598. * This function provides the stored zone ID as passed to the
  599. * constructor. This is meant for testing purposes and normal
  600. * applications shouldn't need it.
  601. */
  602. int zone_id() const { return (zone_id_); }
  603. /**
  604. * \brief The database accessor.
  605. *
  606. * This function provides the database accessor stored inside as
  607. * passed to the constructor. This is meant for testing purposes and
  608. * normal applications shouldn't need it.
  609. */
  610. const DatabaseAccessor& getAccessor() const {
  611. return (*accessor_);
  612. }
  613. private:
  614. boost::shared_ptr<DatabaseAccessor> accessor_;
  615. const int zone_id_;
  616. const isc::dns::Name origin_;
  617. //
  618. /// \brief Shortcut name for the result of getRRsets
  619. typedef std::pair<bool, std::map<dns::RRType, dns::RRsetPtr> >
  620. FoundRRsets;
  621. /// \brief Just shortcut for set of types
  622. typedef std::set<dns::RRType> WantedTypes;
  623. /**
  624. * \brief Searches database for RRsets of one domain.
  625. *
  626. * This method scans RRs of single domain specified by name and
  627. * extracts any RRsets found and requested by parameters.
  628. *
  629. * It is used internally by find(), because it is called multiple
  630. * times (usually with different domains).
  631. *
  632. * \param name Which domain name should be scanned.
  633. * \param types List of types the caller is interested in.
  634. * \param check_ns If this is set to true, it checks nothing lives
  635. * together with NS record (with few little exceptions, like RRSIG
  636. * or NSEC). This check is meant for non-apex NS records.
  637. * \param construct_name If this is NULL, the resulting RRsets have
  638. * their name set to name. If it is not NULL, it overrides the name
  639. * and uses this one (this can be used for wildcard synthesized
  640. * records).
  641. * \return A pair, where the first element indicates if the domain
  642. * contains any RRs at all (not only the requested, it may happen
  643. * this is set to true, but the second part is empty). The second
  644. * part is map from RRtypes to RRsets of the corresponding types.
  645. * If the RRset is not present in DB, the RRtype is not there at
  646. * all (so you'll not find NULL pointer in the result).
  647. * \throw DataSourceError If there's a low-level error with the
  648. * database or the database contains bad data.
  649. */
  650. FoundRRsets getRRsets(const std::string& name,
  651. const WantedTypes& types, bool check_ns,
  652. const std::string* construct_name = NULL);
  653. /**
  654. * \brief Checks if something lives below this domain.
  655. *
  656. * This looks if there's any subdomain of the given name. It can be
  657. * used to test if domain is empty non-terminal.
  658. *
  659. * \param name The domain to check.
  660. */
  661. bool hasSubdomains(const std::string& name);
  662. /**
  663. * \brief Get the NSEC covering a name.
  664. *
  665. * This one calls findPreviousName on the given name and extracts an NSEC
  666. * record on the result. It handles various error cases. The method exists
  667. * to share code present at more than one location.
  668. */
  669. dns::RRsetPtr findNSECCover(const dns::Name& name);
  670. /**
  671. * \brief Convenience type shortcut.
  672. *
  673. * To find stuff in the result of getRRsets.
  674. */
  675. typedef std::map<dns::RRType, dns::RRsetPtr>::const_iterator
  676. FoundIterator;
  677. };
  678. /**
  679. * \brief Find a zone in the database
  680. *
  681. * This queries database's getZone to find the best matching zone.
  682. * It will propagate whatever exceptions are thrown from that method
  683. * (which is not restricted in any way).
  684. *
  685. * \param name Name of the zone or data contained there.
  686. * \return FindResult containing the code and an instance of Finder, if
  687. * anything is found. However, application should not rely on the
  688. * ZoneFinder being instance of Finder (possible subclass of this class
  689. * may return something else and it may change in future versions), it
  690. * should use it as a ZoneFinder only.
  691. */
  692. virtual FindResult findZone(const isc::dns::Name& name) const;
  693. /**
  694. * \brief Get the zone iterator
  695. *
  696. * The iterator allows going through the whole zone content. If the
  697. * underlying DatabaseConnection is implemented correctly, it should
  698. * be possible to have multiple ZoneIterators at once and query data
  699. * at the same time.
  700. *
  701. * \exception DataSourceError if the zone doesn't exist.
  702. * \exception isc::NotImplemented if the underlying DatabaseConnection
  703. * doesn't implement iteration. But in case it is not implemented
  704. * and the zone doesn't exist, DataSourceError is thrown.
  705. * \exception Anything else the underlying DatabaseConnection might
  706. * want to throw.
  707. * \param name The origin of the zone to iterate.
  708. * \return Shared pointer to the iterator (it will never be NULL)
  709. */
  710. virtual ZoneIteratorPtr getIterator(const isc::dns::Name& name) const;
  711. /// This implementation internally clones the accessor from the one
  712. /// used in the client and starts a separate transaction using the cloned
  713. /// accessor. The returned updater will be able to work separately from
  714. /// the original client.
  715. virtual ZoneUpdaterPtr getUpdater(const isc::dns::Name& name,
  716. bool replace) const;
  717. private:
  718. /// \brief The RR class that this client handles.
  719. const isc::dns::RRClass rrclass_;
  720. /// \brief The accessor to our database.
  721. const boost::shared_ptr<DatabaseAccessor> accessor_;
  722. };
  723. }
  724. }
  725. #endif // __DATABASE_DATASRC_H
  726. // Local Variables:
  727. // mode: c++
  728. // End: