memory_datasrc.h 14 KB

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