2 # DiffReader reads OSM diffs and applies them to the database.
4 # Uses the streaming LibXML "Reader" interface to cut down on memory
5 # usage, so hopefully we can process fairly large diffs.
7 include ConsistencyValidations
9 # maps each element type to the model class which handles it
13 "relation" => Relation
17 # Construct a diff reader by giving it a bunch of XML +data+ to parse
18 # in OsmChange format. All diffs must be limited to a single changeset
19 # given in +changeset+.
20 def initialize(data, changeset)
21 @reader = XML::Reader.new data
22 @changeset = changeset
26 # An element-block mapping for using the LibXML reader interface.
28 # Since a lot of LibXML reader usage is boilerplate iteration through
29 # elements, it would be better to DRY and do this in a block. This
30 # could also help with error handling...?
32 # skip the first element, which is our opening element of the block
34 # loop over all elements.
35 # NOTE: XML::Reader#read returns 0 for EOF and -1 for error.
36 while @reader.read == 1
37 break if @reader.node_type == 15 # end element
38 next unless @reader.node_type == 1 # element
44 # An element-block mapping for using the LibXML reader interface.
46 # Since a lot of LibXML reader usage is boilerplate iteration through
47 # elements, it would be better to DRY and do this in a block. This
48 # could also help with error handling...?
50 with_element do |model_name|
51 model = MODELS[model_name]
52 raise "Unexpected element type #{model_name}, " +
53 "expected node, way, relation." if model.nil?
54 yield model, @reader.expand
60 # Checks a few invariants. Others are checked in the model methods
61 # such as save_ and delete_with_history.
62 def check(model, xml, new)
63 raise OSM::APIBadXMLError.new(model, xml) if new.nil?
64 unless new.changeset_id == @changeset.id
65 raise OSM::APIChangesetMismatchError.new(new.changeset_id, @changeset.id)
70 # Consume the XML diff and try to commit it to the database. This code
71 # is *not* transactional, so code which calls it should ensure that the
72 # appropriate transaction block is in place.
74 # On a failure to meet preconditions (e.g: optimistic locking fails)
75 # an exception subclassing OSM::APIError will be thrown.
78 node_ids, way_ids, rel_ids = {}, {}, {}
79 ids = { :node => node_ids, :way => way_ids, :relation => rel_ids}
81 result = OSM::API.new.get_xml_doc
83 # loop at the top level, within the <osmChange> element (although we
84 # don't actually check this...)
85 with_element do |action_name|
86 if action_name == 'create'
87 # create a new element. this code is agnostic of the element type
88 # because all the elements support the methods that we're using.
89 with_model do |model, xml|
90 new = model.from_xml_node(xml, true)
91 check(model, xml, new)
93 # when this element is saved it will get a new ID, so we save it
94 # to produce the mapping which is sent to other elements.
95 placeholder_id = xml['id'].to_i
96 raise OSM::APIBadXMLError.new(model, xml) if placeholder_id.nil?
98 # some elements may have placeholders for other elements in the
99 # diff, so we must fix these before saving the element.
100 new.fix_placeholders!(ids)
102 # create element given user
103 new.create_with_history(@changeset.user)
105 # save placeholder => allocated ID map
106 ids[model.to_s.downcase.to_sym][placeholder_id] = new.id
108 # add the result to the document we're building for return.
109 xml_result = XML::Node.new model.to_s.downcase
110 xml_result["old_id"] = placeholder_id.to_s
111 xml_result["new_id"] = new.id.to_s
112 xml_result["new_version"] = new.version.to_s
113 result.root << xml_result
116 elsif action_name == 'modify'
117 # modify an existing element. again, this code doesn't directly deal
118 # with types, but uses duck typing to handle them transparently.
119 with_model do |model, xml|
120 # get the new element from the XML payload
121 new = model.from_xml_node(xml, false)
122 check(model, xml, new)
124 # and the old one from the database
125 old = model.find(new.id)
127 new.fix_placeholders!(ids)
128 old.update_from(new, @changeset.user)
130 xml_result = XML::Node.new model.to_s.downcase
131 xml_result["old_id"] = old.id.to_s
132 xml_result["new_id"] = new.id.to_s
133 xml_result["new_version"] = new.version.to_s
134 result.root << xml_result
137 elsif action_name == 'delete'
138 # delete action. this takes a payload in API 0.6, so we need to do
139 # most of the same checks that are done for the modify.
140 with_model do |model, xml|
141 new = model.from_xml_node(xml, false)
142 check(model, xml, new)
144 old = model.find(new.id)
146 # can a delete have placeholders under any circumstances?
147 # if a way is modified, then deleted is that a valid diff?
148 new.fix_placeholders!(ids)
149 old.delete_with_history!(new, @changeset.user)
151 xml_result = XML::Node.new model.to_s.downcase
152 xml_result["old_id"] = old.id.to_s
153 result.root << xml_result
157 # no other actions to choose from, so it must be the users fault!
158 raise OSM::APIChangesetActionInvalid.new(action_name)
162 # return the XML document to be rendered back to the client