zone_writer_local.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. state_(ZW_UNUSED)
  32. {}
  33. ZoneWriterLocal::~ZoneWriterLocal() {
  34. // Clean up everything there might be left if someone forgot, just
  35. // in case. Or should we assert instead?
  36. cleanup();
  37. }
  38. void
  39. ZoneWriterLocal::load() {
  40. if (state_ != ZW_UNUSED) {
  41. isc_throw(isc::InvalidOperation, "Trying to load twice");
  42. }
  43. zone_data_ = load_action_(segment_->getMemorySegment());
  44. if (zone_data_ == NULL) {
  45. // Bug inside load_action_.
  46. isc_throw(isc::InvalidOperation, "No data returned from load action");
  47. }
  48. state_ = ZW_LOADED;
  49. }
  50. void
  51. ZoneWriterLocal::install() {
  52. if (state_ != ZW_LOADED) {
  53. isc_throw(isc::InvalidOperation, "No data to install");
  54. }
  55. ZoneTable* table(segment_->getHeader().getTable());
  56. if (table == NULL) {
  57. isc_throw(isc::InvalidOperation, "No zone table present");
  58. }
  59. const ZoneTable::AddResult result(table->addZone(
  60. segment_->getMemorySegment(),
  61. rrclass_, origin_, zone_data_));
  62. state_ = ZW_INSTALLED;
  63. zone_data_ = result.zone_data;
  64. }
  65. void
  66. ZoneWriterLocal::cleanup() {
  67. // We eat the data (if any) now.
  68. if (zone_data_ != NULL) {
  69. ZoneData::destroy(segment_->getMemorySegment(), zone_data_, rrclass_);
  70. zone_data_ = NULL;
  71. state_ = ZW_CLEANED;
  72. }
  73. }
  74. }
  75. }
  76. }