memory_datasrc.h 12 KB

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