memory_client.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 RRset is NULL exception.
  127. ///
  128. /// This is thrown if the provided RRset parameter is NULL.
  129. struct NullRRset : public InvalidParameter {
  130. NullRRset(const char* file, size_t line, const char* what) :
  131. InvalidParameter(file, line, what)
  132. { }
  133. };
  134. /// \brief Zone is empty exception.
  135. ///
  136. /// This is thrown if we have an empty zone created as a result of
  137. /// load().
  138. struct EmptyZone : public InvalidParameter {
  139. EmptyZone(const char* file, size_t line, const char* what) :
  140. InvalidParameter(file, line, what)
  141. { }
  142. };
  143. /// \brief General failure exception for \c add().
  144. ///
  145. /// This is thrown against general error cases in adding an RRset
  146. /// to the zone.
  147. ///
  148. /// Note: this exception would cover cases for \c OutOfZone or
  149. /// \c NullRRset. We'll need to clarify and unify the granularity
  150. /// of exceptions eventually. For now, exceptions are added as
  151. /// developers see the need for it.
  152. struct AddError : public InvalidParameter {
  153. AddError(const char* file, size_t line, const char* what) :
  154. InvalidParameter(file, line, what)
  155. { }
  156. };
  157. /// Returns a \c ZoneFinder result that best matches the given name.
  158. ///
  159. /// This derived version of the method never throws an exception.
  160. /// For other details see \c DataSourceClient::findZone().
  161. virtual isc::datasrc::DataSourceClient::FindResult
  162. findZone(const isc::dns::Name& name) const;
  163. /// Returns a \c ZoneData in the result that best matches the given
  164. /// name.
  165. ///
  166. /// This is mainly intended for use in unit tests and should not be
  167. /// used in other code.
  168. ///
  169. /// \throws none
  170. const ZoneData* findZoneData(const isc::dns::Name& name);
  171. /// \brief Implementation of the getIterator method
  172. virtual isc::datasrc::ZoneIteratorPtr
  173. getIterator(const isc::dns::Name& name, bool separate_rrs = false) const;
  174. /// In-memory data source doesn't write back persistently, so this
  175. /// derived method will result in a NotImplemented exception.
  176. ///
  177. /// \note We plan to use a database-based data source as a backend
  178. /// persistent storage for an in-memory data source. When it's
  179. /// implemented we may also want to allow the user of the in-memory client
  180. /// to update via its updater (this may or may not be a good idea and
  181. /// is subject to further discussions).
  182. virtual ZoneUpdaterPtr getUpdater(const isc::dns::Name& name,
  183. bool replace, bool journaling = false)
  184. const;
  185. virtual std::pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>
  186. getJournalReader(const isc::dns::Name& zone, uint32_t begin_serial,
  187. uint32_t end_serial) const;
  188. private:
  189. // TODO: Do we still need the PImpl if nobody should manipulate this class
  190. // directly any more (it should be handled through DataSourceClient)?
  191. class InMemoryClientImpl;
  192. InMemoryClientImpl* impl_;
  193. // A helper internal class used by load(). It maintains some intermediate
  194. // states while loading RRs of the zone.
  195. class Loader;
  196. };
  197. } // namespace memory
  198. } // namespace datasrc
  199. } // namespace isc
  200. #endif // DATASRC_MEMORY_CLIENT_H
  201. // Local Variables:
  202. // mode: c++
  203. // End: