rrcollator.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 RRCOLLATOR_H
  15. #define RRCOLLATOR_H 1
  16. #include <dns/master_loader_callbacks.h>
  17. #include <dns/rrset.h>
  18. #include <boost/noncopyable.hpp>
  19. #include <boost/function.hpp>
  20. namespace isc {
  21. namespace dns {
  22. /// \brief A converter from a stream of RRs to a stream of collated RRsets
  23. ///
  24. /// This class is mainly intended to be a helper used as an adaptor for
  25. /// user applications of the \c MasterLoader class; it works as a callback
  26. /// for \c MasterLoader, buffers given RRs from the loader, collating
  27. /// consecutive RRs that belong to the same RRset (ones having the same
  28. /// owner name, RR type and class), and produces a stream of RRsets through
  29. /// its own callback. RRSIGs are also separated if their type covered fields
  30. /// have different values even if the owner name and RR class are the same.
  31. ///
  32. /// It also "normalizes" TTLs of the RR; if collated RRs have different TTLs,
  33. /// this class guarantees that the TTL of the resulting RRsets has the
  34. /// smallest TTL among them.
  35. ///
  36. /// The conversion will be useful for applications of \c MasterLoader because
  37. /// many of this library have interfaces that take an RRset object (or
  38. /// a pointer to it). Note, however, that this class doesn't guarantee that
  39. /// all RRs that would belong to the same RRset are collated into the same
  40. /// single RRset. In fact, it can only collate RRs that are consecutive
  41. /// in the original stream; once it encounters an RR of a different RRset,
  42. /// any subsequent RRs of the previous RRset will form a separate RRset object.
  43. ///
  44. /// This class is non-copyable; it's partially for the convenience of internal
  45. /// implementation details, but it actually doesn't make sense to copy
  46. /// an object of this class, if not harmful, for the intended usage of
  47. /// the class.
  48. class RRCollator : boost::noncopyable {
  49. public:
  50. /// \brief
  51. typedef boost::function<void(const RRsetPtr& rrset)> AddRRsetCallback;
  52. /// \brief Constructor.
  53. ///
  54. /// \throw std::bad_alloc Internal memory allocation fails. This should
  55. /// be very rare.
  56. ///
  57. /// \param callback The callback functor to be called for each collated
  58. /// RRset.
  59. RRCollator(const AddRRsetCallback& callback);
  60. /// \brief Destructor.
  61. ///
  62. /// It only performs trivial internal cleanup. In particular, even if
  63. /// it still has a buffered RRset it will be simply discarded. This is
  64. /// because the given callback could throw an exception, and it's
  65. /// impossible to predict how this class is used (to see if it's a very
  66. /// rare case where propagating an exception from a destructor is
  67. /// justified). Instead, the application needs to make sure that
  68. /// \c finish() is called before the object of this class is destroyed.
  69. ///
  70. /// \throw None
  71. ~RRCollator();
  72. /// \brief Call the callback on the remaining RRset, if any.
  73. ///
  74. /// This method is expected to be called that it's supposed all RRs have
  75. /// been passed to this class object. Since there is no explicit
  76. /// indicator of the end of the stream, the user of this class needs to
  77. /// explicitly call this method to call the callback for the last buffered
  78. /// RRset (see also the destructor's description).
  79. ///
  80. /// If there is no buffered RRset, this method does nothing. It can happen
  81. /// if it's called without receiving any RRs, or called more than once.
  82. ///
  83. /// It propagates any exception thrown from the callback; otherwise it
  84. /// doesn't throw anything.
  85. void finish();
  86. /// \brief Return \c MasterLoader compatible callback.
  87. ///
  88. /// This method returns a functor in the form of \c AddRRCallback
  89. /// that works as an adaptor between \c MasterLoader and an application
  90. /// that needs to get a stream of RRsets. When the returned callback
  91. /// is called, this \c RRCollator object accepts the corresponding RR,
  92. /// and collates it with other RRs of the same RRset if necessary.
  93. /// Every time the \c RRCollator object encounters an RR of a different
  94. /// RRset, it calls the callback passed to the constructor with the RRset
  95. /// built so far.
  96. ///
  97. /// Like \c flush(), this \c AddRRCallback functor propagates any exception
  98. /// thrown from the callback.
  99. ///
  100. /// This method is expected to be called only once for a given
  101. /// \c RRCollator object. It doesn't prohibit duplicate calls, but
  102. /// returned functor objects internally refer to the same \c RRCollator
  103. /// object, and calling the both callbacks randomly will just cause
  104. /// confusion.
  105. AddRRCallback getCallback();
  106. private:
  107. class Impl;
  108. Impl* impl_;
  109. };
  110. } // namespace dns
  111. } // namespace isc
  112. #endif // RRCOLLATOR_H
  113. // Local Variables:
  114. // mode: c++
  115. // End: