zone_writer_local.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "zone_writer_local.h"
  15. #include "zone_data.h"
  16. #include "zone_table_segment_local.h"
  17. #include <memory>
  18. using std::auto_ptr;
  19. namespace isc {
  20. namespace datasrc {
  21. namespace memory {
  22. ZoneWriterLocal::ZoneWriterLocal(ZoneTableSegmentLocal* segment,
  23. const LoadAction& load_action,
  24. const dns::Name& origin,
  25. const dns::RRClass& rrclass) :
  26. segment_(segment),
  27. load_action_(load_action),
  28. origin_(origin),
  29. rrclass_(rrclass),
  30. zone_data_(NULL),
  31. loaded_(false),
  32. data_ready_(false)
  33. {}
  34. ZoneWriterLocal::~ZoneWriterLocal() {
  35. // Clean up everything there might be left if someone forgot, just
  36. // in case. Or should we assert instead?
  37. cleanup();
  38. }
  39. void
  40. ZoneWriterLocal::load() {
  41. if (loaded_) {
  42. isc_throw(isc::Unexpected, "Trying to load twice");
  43. }
  44. zone_data_ = load_action_(segment_->getMemorySegment());
  45. if (zone_data_ == NULL) {
  46. // Bug inside load_action_.
  47. isc_throw(isc::Unexpected, "No data returned from load action");
  48. }
  49. loaded_ = true;
  50. data_ready_ = true;
  51. }
  52. void
  53. ZoneWriterLocal::install() {
  54. if (!data_ready_) {
  55. isc_throw(isc::Unexpected, "No data to install");
  56. }
  57. ZoneTable* table(segment_->getHeader().getTable());
  58. if (table == NULL) {
  59. isc_throw(isc::Unexpected, "No zone table present");
  60. }
  61. ZoneTable::AddResult result(table->addZone(segment_->getMemorySegment(),
  62. rrclass_, origin_, zone_data_));
  63. data_ready_ = false;
  64. zone_data_ = result.zone_data;
  65. }
  66. void
  67. ZoneWriterLocal::cleanup() {
  68. // We eat the data (if any) now.
  69. if (zone_data_ != NULL) {
  70. ZoneData::destroy(segment_->getMemorySegment(), zone_data_, rrclass_);
  71. zone_data_ = NULL;
  72. data_ready_ = false;
  73. }
  74. }
  75. }
  76. }
  77. }