memory_datasrc.h 13 KB

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