memory_datasrc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 <string>
  17. #include <boost/noncopyable.hpp>
  18. #include <datasrc/zonetable.h>
  19. #include <datasrc/client.h>
  20. namespace isc {
  21. namespace dns {
  22. class Name;
  23. class RRsetList;
  24. };
  25. namespace datasrc {
  26. /// A derived zone finder class intended to be used with the memory data source.
  27. ///
  28. /// Conceptually this "finder" maintains a local in-memory copy of all RRs
  29. /// of a single zone from some kind of source (right now it's a textual
  30. /// master file, but it could also be another data source with a database
  31. /// backend). This is why the class has methods like \c load() or \c add().
  32. ///
  33. /// This class is non copyable.
  34. class InMemoryZoneFinder : boost::noncopyable, public ZoneFinder {
  35. ///
  36. /// \name Constructors and Destructor.
  37. public:
  38. /// \brief Constructor from zone parameters.
  39. ///
  40. /// This constructor internally involves resource allocation, and if
  41. /// it fails, a corresponding standard exception will be thrown.
  42. /// It never throws an exception otherwise.
  43. ///
  44. /// \param rrclass The RR class of the zone.
  45. /// \param origin The origin name of the zone.
  46. InMemoryZoneFinder(const isc::dns::RRClass& rrclass,
  47. const isc::dns::Name& origin);
  48. /// The destructor.
  49. virtual ~InMemoryZoneFinder();
  50. //@}
  51. /// \brief Returns the origin of the zone.
  52. virtual isc::dns::Name getOrigin() const;
  53. /// \brief Returns the class of the zone.
  54. virtual isc::dns::RRClass getClass() const;
  55. /// \brief Looks up an RRset in the zone.
  56. ///
  57. /// See documentation in \c Zone.
  58. ///
  59. /// It returns NULL pointer in case of NXDOMAIN and NXRRSET,
  60. /// and also SUCCESS if target is not NULL(TYPE_ANY query).
  61. /// (the base class documentation does not seem to require that).
  62. virtual FindResult find(const isc::dns::Name& name,
  63. const isc::dns::RRType& type,
  64. isc::dns::RRsetList* target = NULL,
  65. const FindOptions options = FIND_DEFAULT);
  66. /// \brief Inserts an rrset into the zone.
  67. ///
  68. /// It puts another RRset into the zone.
  69. ///
  70. /// Except for NullRRset and OutOfZone, this method does not guarantee
  71. /// strong exception safety (it is currently not needed, if it is needed
  72. /// in future, it should be implemented).
  73. ///
  74. /// \throw NullRRset \c rrset is a NULL pointer.
  75. /// \throw OutOfZone The owner name of \c rrset is outside of the
  76. /// origin of the zone.
  77. /// \throw AddError Other general errors.
  78. /// \throw Others This method might throw standard allocation exceptions.
  79. ///
  80. /// \param rrset The set to add.
  81. /// \return SUCCESS or EXIST (if an rrset for given name and type already
  82. /// exists).
  83. result::Result add(const isc::dns::ConstRRsetPtr& rrset);
  84. /// \brief RRSet out of zone exception.
  85. ///
  86. /// This is thrown if addition of an RRset that doesn't belong under the
  87. /// zone's origin is requested.
  88. struct OutOfZone : public InvalidParameter {
  89. OutOfZone(const char* file, size_t line, const char* what) :
  90. InvalidParameter(file, line, what)
  91. { }
  92. };
  93. /// \brief RRset is NULL exception.
  94. ///
  95. /// This is thrown if the provided RRset parameter is NULL.
  96. struct NullRRset : public InvalidParameter {
  97. NullRRset(const char* file, size_t line, const char* what) :
  98. InvalidParameter(file, line, what)
  99. { }
  100. };
  101. /// \brief General failure exception for \c add().
  102. ///
  103. /// This is thrown against general error cases in adding an RRset
  104. /// to the zone.
  105. ///
  106. /// Note: this exception would cover cases for \c OutOfZone or
  107. /// \c NullRRset. We'll need to clarify and unify the granularity
  108. /// of exceptions eventually. For now, exceptions are added as
  109. /// developers see the need for it.
  110. struct AddError : public InvalidParameter {
  111. AddError(const char* file, size_t line, const char* what) :
  112. InvalidParameter(file, line, what)
  113. { }
  114. };
  115. /// Return the master file name of the zone
  116. ///
  117. /// This method returns the name of the zone's master file to be loaded.
  118. /// The returned string will be an empty unless the zone finder has
  119. /// successfully loaded a zone.
  120. ///
  121. /// This method should normally not throw an exception. But the creation
  122. /// of the return string may involve a resource allocation, and if it
  123. /// fails, the corresponding standard exception will be thrown.
  124. ///
  125. /// \return The name of the zone file loaded in the zone finder, or an empty
  126. /// string if the zone hasn't loaded any file.
  127. const std::string getFileName() const;
  128. /// \brief Load zone from masterfile.
  129. ///
  130. /// This loads data from masterfile specified by filename. It replaces
  131. /// current content. The masterfile parsing ability is kind of limited,
  132. /// see isc::dns::masterLoad.
  133. ///
  134. /// This throws isc::dns::MasterLoadError if there is problem with loading
  135. /// (missing file, malformed, it contains different zone, etc - see
  136. /// isc::dns::masterLoad for details).
  137. ///
  138. /// In case of internal problems, OutOfZone, NullRRset or AssertError could
  139. /// be thrown, but they should not be expected. Exceptions caused by
  140. /// allocation may be thrown as well.
  141. ///
  142. /// If anything is thrown, the previous content is preserved (so it can
  143. /// be used to update the data, but if user makes a typo, the old one
  144. /// is kept).
  145. ///
  146. /// \param filename The master file to load.
  147. ///
  148. /// \todo We may need to split it to some kind of build and commit/abort.
  149. /// This will probably be needed when a better implementation of
  150. /// configuration reloading is written.
  151. void load(const std::string& filename);
  152. /// Exchanges the content of \c this zone finder with that of the given
  153. /// \c zone_finder.
  154. ///
  155. /// This method never throws an exception.
  156. ///
  157. /// \param zone_finder Another \c InMemoryZone object which is to
  158. /// be swapped with \c this zone finder.
  159. void swap(InMemoryZoneFinder& zone_finder);
  160. private:
  161. /// \name Hidden private data
  162. //@{
  163. struct InMemoryZoneFinderImpl;
  164. InMemoryZoneFinderImpl* impl_;
  165. //@}
  166. // The friend here is for InMemoryClient::getIterator. The iterator
  167. // needs to access the data inside the zone, so the InMemoryClient
  168. // extracts the pointer to data and puts it into the iterator.
  169. // The access is read only.
  170. friend class InMemoryClient;
  171. };
  172. /// \brief A data source client that holds all necessary data in memory.
  173. ///
  174. /// The \c InMemoryClient class provides an access to a conceptual data
  175. /// source that maintains all necessary data in a memory image, thereby
  176. /// allowing much faster lookups. The in memory data is a copy of some
  177. /// real physical source - in the current implementation a list of zones
  178. /// are populated as a result of \c addZone() calls; zone data is given
  179. /// in a standard master file (but there's a plan to use database backends
  180. /// as a source of the in memory data).
  181. ///
  182. /// Although every data source client is assumed to be of the same RR class,
  183. /// the \c InMemoryClient class does not enforce the assumption through
  184. /// its interface.
  185. /// For example, the \c addZone() method does not check if the new zone is of
  186. /// the same RR class as that of the others already in memory.
  187. /// It is caller's responsibility to ensure this assumption.
  188. ///
  189. /// <b>Notes to developer:</b>
  190. ///
  191. /// The addZone() method takes a (Boost) shared pointer because it would be
  192. /// inconvenient to require the caller to maintain the ownership of zones,
  193. /// while it wouldn't be safe to delete unnecessary zones inside the dedicated
  194. /// backend.
  195. ///
  196. /// The findZone() method takes a domain name and returns the best matching
  197. /// \c InMemoryZoneFinder in the form of (Boost) shared pointer, so that it can
  198. /// provide the general interface for all data sources.
  199. class InMemoryClient : public DataSourceClient {
  200. public:
  201. ///
  202. /// \name Constructors and Destructor.
  203. ///
  204. //@{
  205. /// Default constructor.
  206. ///
  207. /// This constructor internally involves resource allocation, and if
  208. /// it fails, a corresponding standard exception will be thrown.
  209. /// It never throws an exception otherwise.
  210. InMemoryClient();
  211. /// The destructor.
  212. ~InMemoryClient();
  213. //@}
  214. /// Return the number of zones stored in the client.
  215. ///
  216. /// This method never throws an exception.
  217. ///
  218. /// \return The number of zones stored in the client.
  219. unsigned int getZoneCount() const;
  220. /// Add a zone (in the form of \c ZoneFinder) to the \c InMemoryClient.
  221. ///
  222. /// \c zone_finder must not be associated with a NULL pointer; otherwise
  223. /// an exception of class \c InvalidParameter will be thrown.
  224. /// If internal resource allocation fails, a corresponding standard
  225. /// exception will be thrown.
  226. /// This method never throws an exception otherwise.
  227. ///
  228. /// \param zone_finder A \c ZoneFinder object to be added.
  229. /// \return \c result::SUCCESS If the zone_finder is successfully
  230. /// added to the client.
  231. /// \return \c result::EXIST The memory data source already
  232. /// stores a zone that has the same origin.
  233. result::Result addZone(ZoneFinderPtr zone_finder);
  234. /// Returns a \c ZoneFinder for a zone_finder that best matches the given
  235. /// name.
  236. ///
  237. /// This derived version of the method never throws an exception.
  238. /// For other details see \c DataSourceClient::findZone().
  239. virtual FindResult findZone(const isc::dns::Name& name) const;
  240. /// \brief Implementation of the getIterator method
  241. virtual ZoneIteratorPtr getIterator(const isc::dns::Name& name) const;
  242. /// In-memory data source is read-only, so this derived method will
  243. /// result in a NotImplemented (once merged) exception.
  244. virtual ZoneUpdaterPtr startUpdateZone(const isc::dns::Name& name,
  245. bool replace) const;
  246. private:
  247. // TODO: Do we still need the PImpl if nobody should manipulate this class
  248. // directly any more (it should be handled through DataSourceClient)?
  249. class InMemoryClientImpl;
  250. InMemoryClientImpl* impl_;
  251. };
  252. }
  253. }
  254. #endif // __DATA_SOURCE_MEMORY_H
  255. // Local Variables:
  256. // mode: c++
  257. // End: