zone_loader.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #include <datasrc/zone_loader.h>
  15. #include <datasrc/master_loader_callbacks.h>
  16. #include <datasrc/client.h>
  17. #include <datasrc/data_source.h>
  18. #include <datasrc/iterator.h>
  19. #include <datasrc/zone.h>
  20. #include <dns/rrset.h>
  21. using isc::dns::Name;
  22. using isc::dns::ConstRRsetPtr;
  23. using isc::dns::MasterLoader;
  24. namespace isc {
  25. namespace datasrc {
  26. ZoneLoader::ZoneLoader(DataSourceClient& destination, const Name& zone_name,
  27. DataSourceClient& source) :
  28. // Separate the RRsets as that is possibly faster (the data source doesn't
  29. // have to aggregate them) and also because our limit semantics.
  30. iterator_(source.getIterator(zone_name, true)),
  31. updater_(destination.getUpdater(zone_name, true, false)),
  32. complete_(false), rr_count_(0)
  33. {
  34. // The getIterator should never return NULL. So we check it.
  35. // Or should we throw instead?
  36. assert(iterator_ != ZoneIteratorPtr());
  37. // In case the zone doesn't exist in the destination, throw
  38. if (updater_ == ZoneUpdaterPtr()) {
  39. isc_throw(DataSourceError, "Zone " << zone_name << " not found in "
  40. "destination data source, can't fill it with data");
  41. }
  42. // The dereference of zone_finder is safe, if we can get iterator, we can
  43. // get a finder.
  44. //
  45. // TODO: We probably need a getClass on the data source itself.
  46. if (source.findZone(zone_name).zone_finder->getClass() !=
  47. updater_->getFinder().getClass()) {
  48. isc_throw(isc::InvalidParameter,
  49. "Source and destination class mismatch");
  50. }
  51. }
  52. ZoneLoader::ZoneLoader(DataSourceClient& destination, const Name& zone_name,
  53. const char* filename) :
  54. updater_(destination.getUpdater(zone_name, true, false)),
  55. complete_(false), loaded_ok_(true), rr_count_(0)
  56. {
  57. if (updater_ == ZoneUpdaterPtr()) {
  58. isc_throw(DataSourceError, "Zone " << zone_name << " not found in "
  59. "destination data source, can't fill it with data");
  60. } else {
  61. loader_.reset(new
  62. MasterLoader(filename, zone_name,
  63. // TODO: Maybe we should have getClass()
  64. // on the data source?
  65. updater_->getFinder().getClass(),
  66. createMasterLoaderCallbacks(zone_name,
  67. updater_->getFinder().getClass(),
  68. &loaded_ok_),
  69. createMasterLoaderAddCallback(*updater_)));
  70. }
  71. }
  72. namespace {
  73. // Copy up to limit RRsets from source to destination
  74. bool
  75. copyRRsets(const ZoneUpdaterPtr& destination, const ZoneIteratorPtr& source,
  76. size_t limit, size_t& rr_count_)
  77. {
  78. size_t loaded = 0;
  79. while (loaded < limit) {
  80. const ConstRRsetPtr rrset(source->getNextRRset());
  81. if (rrset == ConstRRsetPtr()) {
  82. // Done loading, no more RRsets in the input.
  83. return (true);
  84. } else {
  85. destination->addRRset(*rrset);
  86. }
  87. ++loaded;
  88. rr_count_ += rrset->getRdataCount();
  89. }
  90. return (false); // Not yet, there may be more
  91. }
  92. } // end unnamed namespace
  93. bool
  94. ZoneLoader::loadIncremental(size_t limit) {
  95. if (complete_) {
  96. isc_throw(isc::InvalidOperation,
  97. "Loading has been completed previously");
  98. }
  99. if (iterator_ == ZoneIteratorPtr()) {
  100. assert(loader_.get() != NULL);
  101. try {
  102. complete_ = loader_->loadIncremental(limit);
  103. } catch (const isc::dns::MasterLoaderError& e) {
  104. isc_throw(MasterFileError, e.getMessage().c_str());
  105. }
  106. if (complete_ && !loaded_ok_) {
  107. isc_throw(MasterFileError, "Error while loading master file");
  108. }
  109. rr_count_ = loader_->getRRCount();
  110. } else {
  111. complete_ = copyRRsets(updater_, iterator_, limit, rr_count_);
  112. }
  113. if (complete_) {
  114. updater_->commit();
  115. }
  116. return (complete_);
  117. }
  118. size_t
  119. ZoneLoader::getRRCount() const {
  120. return (rr_count_);
  121. }
  122. size_t
  123. ZoneLoader::getSize() const {
  124. if (!loader_) {
  125. return (0);
  126. }
  127. return (loader_->getSize());
  128. }
  129. size_t
  130. ZoneLoader::getPosition() const {
  131. if (!loader_) {
  132. return (0);
  133. }
  134. return (loader_->getPosition());
  135. }
  136. } // end namespace datasrc
  137. } // end namespace isc