memory_client.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright (C) 2012 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 DATASRC_MEMORY_CLIENT_H
  15. #define DATASRC_MEMORY_CLIENT_H 1
  16. #include <util/memory_segment.h>
  17. #include <datasrc/iterator.h>
  18. #include <datasrc/client.h>
  19. #include <datasrc/memory/zone_table.h>
  20. #include <datasrc/memory/zone_data.h>
  21. #include <string>
  22. namespace isc {
  23. namespace dns {
  24. class Name;
  25. class RRsetList;
  26. };
  27. namespace datasrc {
  28. namespace memory {
  29. /// \brief A data source client that holds all necessary data in memory.
  30. ///
  31. /// The \c InMemoryClient class provides an access to a conceptual data
  32. /// source that maintains all necessary data in a memory image, thereby
  33. /// allowing much faster lookups. The in memory data is a copy of some
  34. /// real physical source - in the current implementation a list of zones
  35. /// are populated as a result of \c load() calls; zone data is given in
  36. /// a standard master file, or as an iterator of some other datasource
  37. /// including database backed ones.
  38. ///
  39. /// The InMemoryClient enforces through its interface that all data
  40. /// loaded to the data source is of the same RR class. For example, the
  41. /// \c load() method assumes that the zone being loaded belongs to the
  42. /// same RR class as the memory::Client instance.
  43. class InMemoryClient : public DataSourceClient {
  44. public:
  45. ///
  46. /// \name Constructors and Destructor.
  47. ///
  48. //@{
  49. /// Default constructor.
  50. ///
  51. /// This constructor internally involves resource allocation, and if
  52. /// it fails, a corresponding standard exception will be thrown.
  53. /// It never throws an exception otherwise.
  54. InMemoryClient(util::MemorySegment& mem_sgmt,
  55. isc::dns::RRClass rrclass);
  56. /// The destructor.
  57. ~InMemoryClient();
  58. //@}
  59. /// \brief Returns the class of the data source client.
  60. virtual isc::dns::RRClass getClass() const;
  61. /// Return the number of zones stored in the client.
  62. ///
  63. /// This method never throws an exception.
  64. ///
  65. /// \return The number of zones stored in the client.
  66. virtual unsigned int getZoneCount() const;
  67. /// \brief Load zone from masterfile.
  68. ///
  69. /// This loads data from masterfile specified by filename. It replaces
  70. /// current content. The masterfile parsing ability is kind of limited,
  71. /// see isc::dns::masterLoad.
  72. ///
  73. /// This throws isc::dns::MasterLoadError if there is problem with loading
  74. /// (missing file, malformed, it contains different zone, etc - see
  75. /// isc::dns::masterLoad for details).
  76. ///
  77. /// In case of internal problems, OutOfZone, NullRRset or AssertError could
  78. /// be thrown, but they should not be expected. Exceptions caused by
  79. /// allocation may be thrown as well.
  80. ///
  81. /// If anything is thrown, the previous content is preserved (so it can
  82. /// be used to update the data, but if user makes a typo, the old one
  83. /// is kept).
  84. ///
  85. /// \param filename The master file to load.
  86. ///
  87. /// \todo We may need to split it to some kind of build and commit/abort.
  88. /// This will probably be needed when a better implementation of
  89. /// configuration reloading is written.
  90. result::Result load(const isc::dns::Name& zone_name,
  91. const std::string& filename);
  92. /// \brief Load zone from another data source.
  93. ///
  94. /// This is similar to the other version, but zone's RRsets are provided
  95. /// by an iterator of another data source. On successful load, the
  96. /// internal filename will be cleared.
  97. ///
  98. /// This implementation assumes the iterator produces combined RRsets,
  99. /// that is, there should exactly one RRset for the same owner name and
  100. /// RR type. This means the caller is expected to create the iterator
  101. /// with \c separate_rrs being \c false. This implementation also assumes
  102. /// RRsets of different names are not mixed; so if the iterator produces
  103. /// an RRset of a different name than that of the previous RRset, that
  104. /// previous name must never appear in the subsequent sequence of RRsets.
  105. /// Note that the iterator API does not ensure this. If the underlying
  106. /// implementation does not follow it, load() will fail. Note, however,
  107. /// that this whole interface is tentative. in-memory zone loading will
  108. /// have to be revisited fundamentally, and at that point this restriction
  109. /// probably won't matter.
  110. result::Result load(const isc::dns::Name& zone_name,
  111. ZoneIterator& iterator);
  112. /// Return the master file name of the zone
  113. ///
  114. /// This method returns the name of the zone's master file to be loaded.
  115. /// The returned string will be an empty unless the data source client has
  116. /// successfully loaded the \c zone_name zone from a file before.
  117. ///
  118. /// This method should normally not throw an exception. But the creation
  119. /// of the return string may involve a resource allocation, and if it
  120. /// fails, the corresponding standard exception will be thrown.
  121. ///
  122. /// \return The name of the zone file corresponding to the zone, or
  123. /// an empty string if the client hasn't loaded the \c zone_name
  124. /// zone from a file before.
  125. const std::string getFileName(const isc::dns::Name& zone_name) const;
  126. /// \brief Inserts an rrset into the zone.
  127. ///
  128. /// It puts another RRset into the zone.
  129. ///
  130. /// In the current implementation, this method doesn't allow an existing
  131. /// RRset to be updated or overridden. So the caller must make sure that
  132. /// all RRs of the same type and name must be given in the form of a
  133. /// single RRset. The current implementation will also require that
  134. /// when an RRSIG is added, the RRset to be covered has already been
  135. /// added. These restrictions are probably too strict when this data
  136. /// source accepts various forms of input, so they should be revisited
  137. /// later.
  138. ///
  139. /// Except for NullRRset and OutOfZone, this method does not guarantee
  140. /// strong exception safety (it is currently not needed, if it is needed
  141. /// in future, it should be implemented).
  142. ///
  143. /// \throw NullRRset \c rrset is a NULL pointer.
  144. /// \throw OutOfZone The owner name of \c rrset is outside of the
  145. /// origin of the zone.
  146. /// \throw AddError Other general errors.
  147. /// \throw Others This method might throw standard allocation exceptions.
  148. ///
  149. /// \param rrset The set to add.
  150. /// \return SUCCESS or EXIST (if an rrset for given name and type already
  151. /// exists).
  152. result::Result add(const isc::dns::Name& zone_name,
  153. const isc::dns::ConstRRsetPtr& rrset);
  154. /// \brief RRset is NULL exception.
  155. ///
  156. /// This is thrown if the provided RRset parameter is NULL.
  157. struct NullRRset : public InvalidParameter {
  158. NullRRset(const char* file, size_t line, const char* what) :
  159. InvalidParameter(file, line, what)
  160. { }
  161. };
  162. /// \brief Zone is empty exception.
  163. ///
  164. /// This is thrown if we have an empty zone created as a result of
  165. /// load().
  166. struct EmptyZone : public InvalidParameter {
  167. EmptyZone(const char* file, size_t line, const char* what) :
  168. InvalidParameter(file, line, what)
  169. { }
  170. };
  171. /// \brief General failure exception for \c add().
  172. ///
  173. /// This is thrown against general error cases in adding an RRset
  174. /// to the zone.
  175. ///
  176. /// Note: this exception would cover cases for \c OutOfZone or
  177. /// \c NullRRset. We'll need to clarify and unify the granularity
  178. /// of exceptions eventually. For now, exceptions are added as
  179. /// developers see the need for it.
  180. struct AddError : public InvalidParameter {
  181. AddError(const char* file, size_t line, const char* what) :
  182. InvalidParameter(file, line, what)
  183. { }
  184. };
  185. /// Returns a \c ZoneFinder result that best matches the given name.
  186. ///
  187. /// This derived version of the method never throws an exception.
  188. /// For other details see \c DataSourceClient::findZone().
  189. virtual isc::datasrc::DataSourceClient::FindResult
  190. findZone(const isc::dns::Name& name) const;
  191. /// Returns a \c ZoneData in the result that best matches the given
  192. /// name.
  193. ///
  194. /// This is mainly intended for use in unit tests and should not be
  195. /// used in other code.
  196. ///
  197. /// \throws none
  198. const ZoneData* findZoneData(const isc::dns::Name& name);
  199. /// \brief Implementation of the getIterator method
  200. virtual isc::datasrc::ZoneIteratorPtr
  201. getIterator(const isc::dns::Name& name, bool separate_rrs = false) const;
  202. /// In-memory data source doesn't write back persistently, so this
  203. /// derived method will result in a NotImplemented exception.
  204. ///
  205. /// \note We plan to use a database-based data source as a backend
  206. /// persistent storage for an in-memory data source. When it's
  207. /// implemented we may also want to allow the user of the in-memory client
  208. /// to update via its updater (this may or may not be a good idea and
  209. /// is subject to further discussions).
  210. virtual ZoneUpdaterPtr getUpdater(const isc::dns::Name& name,
  211. bool replace, bool journaling = false)
  212. const;
  213. virtual std::pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>
  214. getJournalReader(const isc::dns::Name& zone, uint32_t begin_serial,
  215. uint32_t end_serial) const;
  216. private:
  217. // TODO: Do we still need the PImpl if nobody should manipulate this class
  218. // directly any more (it should be handled through DataSourceClient)?
  219. class InMemoryClientImpl;
  220. InMemoryClientImpl* impl_;
  221. // A helper internal class used by load(). It maintains some intermediate
  222. // states while loading RRs of the zone.
  223. class Loader;
  224. };
  225. } // namespace memory
  226. } // namespace datasrc
  227. } // namespace isc
  228. #endif // DATASRC_MEMORY_CLIENT_H
  229. // Local Variables:
  230. // mode: c++
  231. // End: