zone_table_segment_mapped.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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::Unexpected 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::Unexpected 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::Unexpected if this method is called without a
  54. /// successful \c reset() call first.
  55. virtual isc::util::MemorySegment& getMemorySegment();
  56. /// \brief Return true if the segment is writable. For read-only
  57. /// segments, false is returned.
  58. ///
  59. /// \throws isc::Unexpected if this method is called without a
  60. /// successful \c reset() call first.
  61. virtual bool isWritable() const;
  62. /// \brief The mode using which to open a ZoneTableSegment around a
  63. /// mapped file.
  64. ///
  65. /// - CREATE: If the mapped file doesn't exist, create it. If it
  66. /// exists, overwrite it with a newly created mapped
  67. /// file. In both cases, open the newly created mapped
  68. /// file in read+write mode.
  69. ///
  70. /// - READ_WRITE: If the mapped file doesn't exist, create it. If it
  71. /// exists, use the existing mapped file. In both
  72. /// cases, open the mapped file in read+write mode.
  73. ///
  74. /// - READ_ONLY: If the mapped file doesn't exist, throw an
  75. /// exception. If it exists, open the existing mapped
  76. /// file in read-only mode.
  77. enum MemorySegmentOpenMode {
  78. CREATE,
  79. READ_WRITE,
  80. READ_ONLY
  81. };
  82. /// \brief Unmap the current file (if mapped) and map the specified
  83. /// one.
  84. ///
  85. /// See the \c MemorySegmentOpenMode documentation above for the
  86. /// various modes in which a ZoneTableSegment can be created.
  87. ///
  88. /// \c params should be a map containing a "mapped-file" key that
  89. /// points to a string value containing the filename of a mapped
  90. /// file. E.g.,
  91. ///
  92. /// {"mapped-file": "/var/bind10/mapped-files/zone-sqlite3.mapped.0"}
  93. ///
  94. /// \throws isc::InvalidParameter if the configuration in \c params
  95. /// has incorrect syntax.
  96. /// \throws isc::Unexpected for a variety of cases where an
  97. /// unexpected condition occurs. These should not occur normally in
  98. /// correctly written code.
  99. ///
  100. /// \param mode The open mode (see the MemorySegmentOpenMode
  101. /// documentation).
  102. /// \param params An element containing config for the mapped file
  103. /// (see the description).
  104. virtual void reset(MemorySegmentOpenMode mode,
  105. isc::data::ConstElementPtr params);
  106. private:
  107. // Internally holds a MemorySegmentMapped. This is NULL on
  108. // construction, and is set by the \c reset() method.
  109. isc::dns::RRClass rrclass_;
  110. MemorySegmentOpenMode current_mode_;
  111. boost::scoped_ptr<isc::util::MemorySegmentMapped> mem_sgmt_;
  112. ZoneTableHeader* header_;
  113. };
  114. } // namespace memory
  115. } // namespace datasrc
  116. } // namespace isc
  117. #endif // ZONE_TABLE_SEGMENT_MAPPED_H