New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add bulk_upload #1995
add bulk_upload #1995
Changes from 1 commit
2c6b751
01df071
f62d067
358f6f3
c4f7d31
7620c61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -228,10 +228,12 @@ def self.delete_with_history_bulk!(relations, changeset, if_unused = false) | |
relation_ids = relation_hash.keys | ||
old_relations = Relation.select("id, version, visible").where(:id => relation_ids).lock | ||
raise OSM::APIBadUserInput, "Relation not exist. id: " + (relation_ids - old_relations.collect(&:id)).join(", ") unless relation_ids.length == old_relations.length | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-existing object should return HTTP 404 (Not found) instead of HTTP 400 (Bad Request) |
||
|
||
old_relations.each do |old| | ||
unless old.visible | ||
# already deleted | ||
raise OSM::APIAlreadyDeletedError.new("relation", old.id) unless if_unused | ||
|
||
# if-unused: ignore and do next. | ||
# return old db version to client. | ||
relation_hash[old.id].version = old.version | ||
|
@@ -247,6 +249,7 @@ def self.delete_with_history_bulk!(relations, changeset, if_unused = false) | |
# discover relations referred by relations | ||
rel_members = RelationMember.select("member_id, relation_id").where(:member_type => "Relation", :member_id => relation_hash.keys).where.not(:relation_id => relation_hash.keys).group_by(&:member_id) | ||
raise OSM::APIPreconditionFailedError, "Relation #{rel_members.first[0]} is still used by relations #{rel_members.first[1].collect(&:relation_id).join(',')}." unless rel_members.empty? || if_unused | ||
|
||
rel_members.each_key do |id| | ||
skipped[id] = relation_hash[id] | ||
relation_hash.delete id | ||
|
@@ -291,6 +294,7 @@ def self.update_from_bulk(relations, changeset) | |
old_members = RelationMember.select("member_type, member_id").where(:relation_id => relation_ids) | ||
.collect { |m| [m.member_type, m.member_id] } | ||
raise OSM::APIPreconditionFailedError, "Cannot update relations: data or member data is invalid." unless preconditions_bulk_ok?(relations, old_members) | ||
|
||
save_with_history_bulk!(relations, changeset) | ||
end | ||
end | ||
|
@@ -358,17 +362,20 @@ def self.preconditions_bulk_ok?(relations, good_members = []) | |
all_members.each do |m| | ||
model_sym = m[0].downcase.to_sym | ||
return false unless ids.key? model_sym | ||
|
||
ids[model_sym].append(m[1]) | ||
end | ||
ids.each do |model_sym, member_ids| | ||
next if member_ids.empty? | ||
|
||
# use reflection to look up the appropriate class | ||
model = Kernel.const_get(model_sym.to_s.capitalize) | ||
elements = model.select("id").where(:id => member_ids, :visible => true).lock("for share") | ||
|
||
# model.preconditions_bulk_ok?(elements) | ||
missing_elements = member_ids - elements.collect(&:id) | ||
next if missing_elements.empty? | ||
|
||
# generate error message | ||
relations.each do |r| | ||
r.members.each do |m| | ||
|
@@ -381,6 +388,7 @@ def self.preconditions_bulk_ok?(relations, good_members = []) | |
|
||
def self.create_with_history_bulk(relations, changeset) | ||
raise OSM::APIPreconditionFailedError, "Cannot create relation: data or member data is invalid." unless preconditions_bulk_ok?(relations) | ||
|
||
relations.each do |relation| | ||
relation.version = 0 | ||
relation.visible = true | ||
|
@@ -413,9 +421,10 @@ def fix_placeholders!(id_map, placeholder_id = nil) | |
def fix_placeholders(id_map) | ||
members.map! do |type, id, role| | ||
old_id = id.to_i | ||
if old_id < 0 | ||
if old_id.negative? | ||
new_id = id_map[type.downcase.to_sym][old_id] | ||
return type, old_id if new_id.nil? | ||
|
||
[type, new_id, role] | ||
else | ||
[type, id, role] | ||
|
@@ -473,7 +482,7 @@ def self.get_changed_members(old_members, new_members) | |
|
||
def self.any_relations?(changed_members) | ||
changed_members.collect { |type, _id, _role| type == "Relation" } | ||
.inject(false) { |acc, elem| acc || elem } | ||
.inject(false) { |acc, elem| acc || elem } | ||
end | ||
|
||
private | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,6 +208,7 @@ def self.update_from_bulk(ways, changeset) | |
end | ||
old_nodes = WayNode.select("node_id").where(:way_id => way_ids).collect(&:node_id) | ||
raise OSM::APIPreconditionFailedError, "Cannot update ways: data is invalid." unless preconditions_bulk_ok?(ways, old_nodes) | ||
|
||
save_with_history_bulk!(ways, changeset) | ||
end | ||
end | ||
|
@@ -223,6 +224,7 @@ def create_with_history(user) | |
|
||
def self.create_with_history_bulk(ways, changeset) | ||
raise OSM::APIPreconditionFailedError, "Cannot create way: data is invalid." unless preconditions_bulk_ok?(ways) | ||
|
||
ways.each do |way| | ||
way.version = 0 | ||
way.visible = true | ||
|
@@ -256,6 +258,7 @@ def self.preconditions_bulk_ok?(ways, old_nodes = []) | |
all_nds = ways.flat_map do |way| | ||
raise OSM::APIPreconditionFailedError, "Way #{way.id} has an empty node list." if way.nds.empty? | ||
raise OSM::APITooManyWayNodesError.new(way.id, way.nds.length, MAX_NUMBER_OF_WAY_NODES) if way.nds.length > MAX_NUMBER_OF_WAY_NODES | ||
|
||
way.nds | ||
end.sort.uniq | ||
all_nds -= old_nodes | ||
|
@@ -307,10 +310,12 @@ def self.delete_with_history_bulk!(ways, changeset, if_unused = false) | |
way_ids = way_hash.keys | ||
old_ways = Way.select("id, version, visible").where(:id => way_ids).lock | ||
raise OSM::APIBadUserInput, "Way not exist. id: " + (way_ids - old_ways.collect(&:id)).join(", ") unless way_ids.length == old_ways.length | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-existing object should return HTTP 404 (Not found) instead of HTTP 400 (Bad Request) |
||
|
||
old_ways.each do |old| | ||
unless old.visible | ||
# already deleted | ||
raise OSM::APIAlreadyDeletedError.new("way", old.id) unless if_unused | ||
|
||
# if-unused: ignore and do next. | ||
# return old db version to client. | ||
way_hash[old.id].version = old.version | ||
|
@@ -326,6 +331,7 @@ def self.delete_with_history_bulk!(ways, changeset, if_unused = false) | |
# discover ways referred by relations | ||
rel_members = RelationMember.select("member_id, relation_id").where(:member_type => "Way", :member_id => way_hash.keys).group_by(&:member_id) | ||
raise OSM::APIPreconditionFailedError, "Way #{rel_members.first[0]} is still used by relations #{rel_members.first[1].collect(&:relation_id).join(',')}." unless rel_members.empty? || if_unused | ||
|
||
rel_members.each_key do |id| | ||
skipped[id] = way_hash[id] | ||
way_hash.delete id | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-existing object should return HTTP 404 (Not found) instead of HTTP 400 (Bad Request)