memory_datasrc.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright (C) 2010 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 __MEMORY_DATA_SOURCE_H
  15. #define __MEMORY_DATA_SOURCE_H 1
  16. #include <datasrc/zonetable.h>
  17. namespace isc {
  18. namespace dns {
  19. class Name;
  20. };
  21. namespace datasrc {
  22. /// A derived zone class intended to be used with the memory data source.
  23. class MemoryZone : public Zone {
  24. ///
  25. /// \name Constructors and Destructor.
  26. ///
  27. /// \b Note:
  28. /// The copy constructor and the assignment operator are intentionally
  29. /// defined as private, making this class non copyable.
  30. //@{
  31. private:
  32. MemoryZone(const MemoryZone& source);
  33. MemoryZone& operator=(const MemoryZone& source);
  34. public:
  35. /// \brief Constructor from zone parameters.
  36. ///
  37. /// This constructor internally involves resource allocation, and if
  38. /// it fails, a corresponding standard exception will be thrown.
  39. /// It never throws an exception otherwise.
  40. ///
  41. /// \param rrclass The RR class of the zone.
  42. /// \param origin The origin name of the zone.
  43. MemoryZone(const isc::dns::RRClass& rrclass, const isc::dns::Name& origin);
  44. /// The destructor.
  45. virtual ~MemoryZone();
  46. //@}
  47. /// \brief Returns the origin of the zone.
  48. virtual const isc::dns::Name& getOrigin() const;
  49. /// \brief Returns the class of the zone.
  50. virtual const isc::dns::RRClass& getClass() const;
  51. /// \brief Looks up an RRset in the zone.
  52. ///
  53. /// See documentation in \c Zone.
  54. virtual FindResult find(const isc::dns::Name& name,
  55. const isc::dns::RRType& type) const;
  56. private:
  57. /// \name Hidden private data
  58. //@{
  59. struct MemoryZoneImpl;
  60. MemoryZoneImpl* impl_;
  61. //@}
  62. };
  63. /// \brief A data source that uses in memory dedicated backend.
  64. ///
  65. /// The \c MemoryDataSrc class represents a data source and provides a
  66. /// basic interface to help DNS lookup processing. For a given domain
  67. /// name, its \c findZone() method searches the in memory dedicated backend
  68. /// for the zone that gives a longest match against that name.
  69. ///
  70. /// The in memory dedicated backend are assumed to be of the same RR class,
  71. /// but the \c MemoryDataSrc class does not enforce the assumption through
  72. /// its interface.
  73. /// For example, the \c addZone() method does not check if the new zone is of
  74. /// the same RR class as that of the others already in the dedicated backend.
  75. /// It is caller's responsibility to ensure this assumption.
  76. ///
  77. /// <b>Notes to developer:</b>
  78. ///
  79. /// For now, we don't make it a derived class of AbstractDataSrc because the
  80. /// interface is so different (we'll eventually consider this as part of the
  81. /// generalization work).
  82. ///
  83. /// The addZone() method takes a (Boost) shared pointer because it would be
  84. /// inconvenient to require the caller to maintain the ownership of zones,
  85. /// while it wouldn't be safe to delete unnecessary zones inside the dedicated
  86. /// backend.
  87. ///
  88. /// The findZone() method takes a domain name and returns the best matching \c
  89. /// MemoryZone in the form of (Boost) shared pointer, so that it can provide
  90. /// the general interface for all data sources.
  91. ///
  92. /// Currently, \c FindResult::zone is immutable for safety.
  93. /// In future versions we may want to make it changeable. For example,
  94. /// we may want to allow configuration update on an existing zone.
  95. class MemoryDataSrc {
  96. public:
  97. /// \brief A helper structure to represent the search result of
  98. /// <code>MemoryDataSrc::find()</code>.
  99. ///
  100. /// This is a straightforward pair of the result code and a share pointer
  101. /// to the found zone to represent the result of \c find().
  102. /// We use this in order to avoid overloading the return value for both
  103. /// the result code ("success" or "not found") and the found object,
  104. /// i.e., avoid using \c NULL to mean "not found", etc.
  105. ///
  106. /// This is a simple value class with no internal state, so for
  107. /// convenience we allow the applications to refer to the members
  108. /// directly.
  109. ///
  110. /// See the description of \c find() for the semantics of the member
  111. /// variables.
  112. struct FindResult {
  113. FindResult(result::Result param_code, const ConstZonePtr param_zone) :
  114. code(param_code), zone(param_zone)
  115. {}
  116. const result::Result code;
  117. const ConstZonePtr zone;
  118. };
  119. ///
  120. /// \name Constructors and Destructor.
  121. ///
  122. /// \b Note:
  123. /// The copy constructor and the assignment operator are intentionally
  124. /// defined as private, making this class non copyable.
  125. //@{
  126. private:
  127. MemoryDataSrc(const MemoryDataSrc& source);
  128. MemoryDataSrc& operator=(const MemoryDataSrc& source);
  129. public:
  130. /// Default constructor.
  131. ///
  132. /// This constructor internally involves resource allocation, and if
  133. /// it fails, a corresponding standard exception will be thrown.
  134. /// It never throws an exception otherwise.
  135. MemoryDataSrc();
  136. /// The destructor.
  137. ~MemoryDataSrc();
  138. //@}
  139. /// Return the number of zones stored in the data source.
  140. ///
  141. /// This method never throws an exception.
  142. ///
  143. /// \return The number of zones stored in the data source.
  144. unsigned int getZoneCount() const;
  145. /// Add a \c Zone to the \c MemoryDataSrc.
  146. ///
  147. /// \c Zone must not be associated with a NULL pointer; otherwise
  148. /// an exception of class \c InvalidParameter will be thrown.
  149. /// If internal resource allocation fails, a corresponding standard
  150. /// exception will be thrown.
  151. /// This method never throws an exception otherwise.
  152. ///
  153. /// \param zone A \c Zone object to be added.
  154. /// \return \c result::SUCCESS If the zone is successfully
  155. /// added to the memory data source.
  156. /// \return \c result::EXIST The memory data source already
  157. /// stores a zone that has the same origin.
  158. result::Result addZone(ZonePtr zone);
  159. /// Find a \c Zone that best matches the given name in the \c MemoryDataSrc.
  160. ///
  161. /// It searches the internal storage for a \c Zone that gives the
  162. /// longest match against \c name, and returns the result in the
  163. /// form of a \c FindResult object as follows:
  164. /// - \c code: The result code of the operation.
  165. /// - \c result::SUCCESS: A zone that gives an exact match
  166. // is found
  167. /// - \c result::PARTIALMATCH: A zone whose origin is a
  168. // super domain of \c name is found (but there is no exact match)
  169. /// - \c result::NOTFOUND: For all other cases.
  170. /// - \c zone: A <Boost> shared pointer to the found \c Zone object if one
  171. // is found; otherwise \c NULL.
  172. ///
  173. /// This method never throws an exception.
  174. ///
  175. /// \param name A domain name for which the search is performed.
  176. /// \return A \c FindResult object enclosing the search result (see above).
  177. FindResult findZone(const isc::dns::Name& name) const;
  178. private:
  179. class MemoryDataSrcImpl;
  180. MemoryDataSrcImpl* impl_;
  181. };
  182. }
  183. }
  184. #endif // __DATA_SOURCE_MEMORY_H
  185. // Local Variables:
  186. // mode: c++
  187. // End: