zone_table_segment_mapped.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (C) 2013 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 ZONE_TABLE_SEGMENT_MAPPED_H
  15. #define ZONE_TABLE_SEGMENT_MAPPED_H
  16. #include <datasrc/memory/zone_table_segment.h>
  17. #include <util/memory_segment_mapped.h>
  18. #include <boost/scoped_ptr.hpp>
  19. namespace isc {
  20. namespace datasrc {
  21. namespace memory {
  22. /// \brief Mapped-file based implementation of ZoneTableSegment class
  23. ///
  24. /// This class specifies a concrete implementation for a memory-mapped
  25. /// ZoneTableSegment. Please see the ZoneTableSegment class
  26. /// documentation for usage.
  27. class ZoneTableSegmentMapped : public ZoneTableSegment {
  28. // This is so that ZoneTableSegmentMapped can be instantiated from
  29. // ZoneTableSegment::create().
  30. friend class ZoneTableSegment;
  31. protected:
  32. /// \brief Protected constructor
  33. ///
  34. /// Instances are expected to be created by the factory method
  35. /// (\c ZoneTableSegment::create()), so this constructor is
  36. /// protected.
  37. ZoneTableSegmentMapped(const isc::dns::RRClass& rrclass);
  38. public:
  39. /// \brief Return the ZoneTableHeader for the mapped zone table
  40. /// segment implementation.
  41. ///
  42. /// \throws isc::InvalidOperation if this method is called without a
  43. /// successful \c reset() call first.
  44. virtual ZoneTableHeader& getHeader();
  45. /// \brief const version of \c getHeader().
  46. ///
  47. /// \throws isc::InvalidOperation if this method is called without a
  48. /// successful \c reset() call first.
  49. virtual const ZoneTableHeader& getHeader() const;
  50. /// \brief Return the MemorySegment for the memory-mapped zone table
  51. /// segment implementation (a MemorySegmentMapped instance).
  52. ///
  53. /// \throws isc::InvalidOperation if this method is called without a
  54. /// successful \c reset() call first.
  55. virtual isc::util::MemorySegment& getMemorySegment();
  56. /// \brief Returns if the segment is writable.
  57. ///
  58. /// Segments successfully opened in CREATE or READ_WRITE modes are
  59. /// writable. Segments opened in READ_ONLY mode are not writable.
  60. /// If there was a failure in \c reset(), the segment is not
  61. /// writable.
  62. virtual bool isWritable() const;
  63. /// \brief Unmap the current file (if mapped) and map the specified
  64. /// one.
  65. ///
  66. /// In case of exceptions, the current existing mapped file may be
  67. /// left open, or may be cleared. Please see the \c ZoneTableSegment
  68. /// API documentation for the behavior.
  69. ///
  70. /// See the \c MemorySegmentOpenMode documentation (in
  71. /// \c ZoneTableSegment class) for the various modes in which a
  72. /// \c ZoneTableSegmentMapped can be created.
  73. ///
  74. /// \c params should be a map containing a "mapped-file" key that
  75. /// points to a string value containing the filename of a mapped
  76. /// file. E.g.,
  77. ///
  78. /// {"mapped-file": "/var/bind10/mapped-files/zone-sqlite3.mapped.0"}
  79. ///
  80. /// \throws isc::InvalidParameter if the configuration in \c params
  81. /// has incorrect syntax.
  82. /// \throws isc::Unexpected for a variety of cases where an
  83. /// unexpected condition occurs. These should not occur normally in
  84. /// correctly written code.
  85. ///
  86. /// \param mode The open mode (see the \c MemorySegmentOpenMode
  87. /// documentation in \c ZoneTableSegment class).
  88. /// \param params An element containing config for the mapped file
  89. /// (see the description).
  90. virtual void reset(MemorySegmentOpenMode mode,
  91. isc::data::ConstElementPtr params);
  92. /// \brief Unmap the current file (if mapped).
  93. virtual void clear();
  94. private:
  95. bool processChecksum(isc::util::MemorySegmentMapped& segment, bool create,
  96. std::string& error_msg);
  97. bool processHeader(isc::util::MemorySegmentMapped& segment, bool create,
  98. std::string& error_msg);
  99. void openReadWrite(const std::string& filename, bool create);
  100. void openReadOnly(const std::string& filename);
  101. private:
  102. isc::dns::RRClass rrclass_;
  103. MemorySegmentOpenMode current_mode_;
  104. std::string current_filename_;
  105. // Internally holds a MemorySegmentMapped. This is NULL on
  106. // construction, and is set by the \c reset() method.
  107. boost::scoped_ptr<isc::util::MemorySegmentMapped> mem_sgmt_;
  108. ZoneTableHeader* header_;
  109. };
  110. } // namespace memory
  111. } // namespace datasrc
  112. } // namespace isc
  113. #endif // ZONE_TABLE_SEGMENT_MAPPED_H