memory_datasrc.h 14 KB

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