zone_loader.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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)
  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. loader_(new MasterLoader(filename, zone_name,
  56. // TODO: Maybe we should have getClass() on the
  57. // data source?
  58. updater_->getFinder().getClass(),
  59. createMasterLoaderCallbacks(zone_name,
  60. updater_->getFinder().getClass(),
  61. &loaded_ok_),
  62. createMasterLoaderAddCallback(*updater_))),
  63. complete_(false),
  64. loaded_ok_(true)
  65. {
  66. }
  67. namespace {
  68. // Copy up to limit RRsets from source to destination
  69. bool
  70. copyRRsets(const ZoneUpdaterPtr& destination, const ZoneIteratorPtr& source,
  71. size_t limit)
  72. {
  73. size_t loaded = 0;
  74. while (loaded < limit) {
  75. const ConstRRsetPtr rrset(source->getNextRRset());
  76. if (rrset == ConstRRsetPtr()) {
  77. // Done loading, no more RRsets in the input.
  78. return (true);
  79. } else {
  80. destination->addRRset(*rrset);
  81. }
  82. ++loaded;
  83. }
  84. return (false); // Not yet, there may be more
  85. }
  86. }
  87. bool
  88. ZoneLoader::loadIncremental(size_t limit) {
  89. if (complete_) {
  90. isc_throw(isc::InvalidOperation,
  91. "Loading has been completed previously");
  92. }
  93. if (iterator_ == ZoneIteratorPtr()) {
  94. assert(loader_.get() != NULL);
  95. if (loader_->loadIncremental(limit)) {
  96. complete_ = true;
  97. if (!loaded_ok_) {
  98. isc_throw(MasterFileError, "Error while loading master file");
  99. } else {
  100. updater_->commit();
  101. }
  102. return (true);
  103. } else {
  104. return (false);
  105. }
  106. } else {
  107. if (copyRRsets(updater_, iterator_, limit)) {
  108. updater_->commit();
  109. complete_ = true;
  110. return (true);
  111. } else {
  112. return (false);
  113. }
  114. }
  115. }
  116. }
  117. }