diff.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright (C) 2011 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and 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 INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. """
  16. This helps the XFR in process with accumulating parts of diff and applying
  17. it to the datasource.
  18. """
  19. class NoSuchZone(Exception):
  20. """
  21. This is raised if a diff for non-existant zone is being created.
  22. """
  23. pass
  24. class Diff:
  25. """
  26. The class represents a diff against current state of datasource on
  27. one zone. The usual way of working with it is creating it, then putting
  28. bunch of changes in and commiting at the end.
  29. If you change your mind, you can just stop using the object without
  30. really commiting it. In that case no changes will happen in the data
  31. sounce.
  32. The class works as a kind of a buffer as well, it does not direct
  33. the changes to underlying data source right away, but keeps them for
  34. a while.
  35. """
  36. def __init__(self, datasource, zone):
  37. """
  38. Initializes the diff to a ready state. It checks the zone exists
  39. in the datasource and if not, NoSuchZone is raised. This also creates
  40. a transaction in the data source.
  41. The datasource is the one containing the zone. Zone is isc.dns.Name
  42. object representing the name of the zone (its apex).
  43. You can also expect isc.datasrc.Error or isc.datasrc.NotImplemented
  44. exceptions.
  45. """
  46. self.__updater = datasource.get_updater(zone, False)
  47. if self.__updater is None:
  48. # The no such zone case
  49. raise NoSuchZone("Zone " + str(zone) +
  50. " does not exist in the data source " +
  51. str(datasource))
  52. self.__buffer = []
  53. def add_data(self, rr):
  54. """
  55. Schedules addition of an RR into the zone in this diff.
  56. The rr is of isc.dns.RRset type and it must contain only one RR.
  57. If this is not the case or if the diff was already commited, this
  58. raises the ValueError exception.
  59. """
  60. pass
  61. def remove_data(self, rr):
  62. """
  63. Schedules removal of an RR from the zone in this diff.
  64. The rr is of isc.dns.RRset type and it must contain only one RR.
  65. If this is not the case or if the diff was already commited, this
  66. raises the ValueError exception.
  67. """
  68. pass
  69. def apply(self):
  70. """
  71. Push the buffered changes inside this diff down into the data source.
  72. This does not stop you from adding more changes later through this
  73. diff and it does not close the datasource transaction, so the changes
  74. will not be shown to others yet. It just means the internal memory
  75. buffer is flushed.
  76. This is called from time to time automatically, but you can call it
  77. manually if you really want to.
  78. This raises ValueError if the diff was already commited.
  79. It also can raise isc.datasrc.Error.
  80. """
  81. pass
  82. def commit(self):
  83. """
  84. Writes all the changes into the data source and makes them visible.
  85. This closes the diff, you may not use it any more. If you try to use
  86. it, you'll get ValueError.
  87. This might raise isc.datasrc.Error.
  88. """
  89. pass
  90. def get_buffer(self):
  91. """
  92. Returns the current buffer of changes not yet passed into the data
  93. source. It is in a form like [('add', rrset), ('remove', rrset),
  94. ('remove', rrset), ...].
  95. Probably useful only for testing and introspection purposes.
  96. """
  97. return self.__buffer