memory_datasrc.h 7.4 KB

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