From 6ae23bed90f249ed943cc43f1bad0e40852f04bb Mon Sep 17 00:00:00 2001 From: Gabriel Ebner Date: Sat, 3 May 2008 16:27:49 +0000 Subject: [PATCH] split_node_tags: - Use InnoDB. - Put version column on the current_* tables. - Use transactions (untested). --- app/controllers/node_controller.rb | 1 + app/controllers/relation_controller.rb | 1 + app/controllers/way_controller.rb | 1 + app/models/node.rb | 34 ++++++++++---------------- app/models/old_node.rb | 4 +-- app/models/old_relation.rb | 1 + app/models/old_way.rb | 1 + app/models/relation.rb | 5 +--- app/models/way.rb | 17 ++++--------- db/migrate/014_move_to_innodb.rb | 29 ++++++++++++++++++++++ 10 files changed, 54 insertions(+), 40 deletions(-) create mode 100644 db/migrate/014_move_to_innodb.rb diff --git a/app/controllers/node_controller.rb b/app/controllers/node_controller.rb index edc3675e5..9f8f4a38b 100644 --- a/app/controllers/node_controller.rb +++ b/app/controllers/node_controller.rb @@ -15,6 +15,7 @@ class NodeController < ApplicationController node = Node.from_xml(request.raw_post, true) if node + node.version = 0 node.user_id = @user.id node.visible = true node.save_with_history! diff --git a/app/controllers/relation_controller.rb b/app/controllers/relation_controller.rb index 2b1ba6c75..f4e938176 100644 --- a/app/controllers/relation_controller.rb +++ b/app/controllers/relation_controller.rb @@ -15,6 +15,7 @@ class RelationController < ApplicationController if !relation.preconditions_ok? render :text => "", :status => :precondition_failed else + relation.version = 0 relation.user_id = @user.id relation.save_with_history! diff --git a/app/controllers/way_controller.rb b/app/controllers/way_controller.rb index 3b6491cf0..a7f74e50c 100644 --- a/app/controllers/way_controller.rb +++ b/app/controllers/way_controller.rb @@ -15,6 +15,7 @@ class WayController < ApplicationController if !way.preconditions_ok? render :text => "", :status => :precondition_failed else + way.version = 0 way.user_id = @user.id way.save_with_history! diff --git a/app/models/node.rb b/app/models/node.rb index 5a8e03ef1..17521428e 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -96,35 +96,27 @@ class Node < GeoRecord def save_with_history! t = Time.now - - Node.transaction do - # apply timestamp to the new node + Node.transaction do + self.version += 1 self.timestamp = t self.save! - end - # Create a NodeTag - NodeTag.transaction do + # Create a NodeTag tags = self.tags - NodeTag.delete_all(['id = ?', self.id]) - - sequence_id = 1 tags.each do |k,v| - tag = NodeTag.new - tag.k = k - tag.v = v - tag.id = self.id - tag.sequence_id = sequence_id - tag.save! - sequence_id += 1 + tag = NodeTag.new + tag.k = k + tag.v = v + tag.id = self.id + tag.save! end - end - # Create an OldNode - old_node = OldNode.from_node(self) - old_node.timestamp = t - old_node.save_with_dependencies! + # Create an OldNode + old_node = OldNode.from_node(self) + old_node.timestamp = t + old_node.save_with_dependencies! + end end def to_xml diff --git a/app/models/old_node.rb b/app/models/old_node.rb index 4f10e65a5..2f960d886 100644 --- a/app/models/old_node.rb +++ b/app/models/old_node.rb @@ -27,6 +27,7 @@ class OldNode < GeoRecord old_node.timestamp = node.timestamp old_node.user_id = node.user_id old_node.id = node.id + old_node.version = node.version return old_node end @@ -57,16 +58,13 @@ class OldNode < GeoRecord #ok from here @attributes.update(OldNode.find(:first, :conditions => ['id = ? AND timestamp = ?', self.id, self.timestamp]).instance_variable_get('@attributes')) - sequence_id = 1 self.tags.each do |k,v| tag = OldNodeTag.new tag.k = k tag.v = v tag.id = self.id tag.version = self.version - tag.sequence_id = sequence_id tag.save! - sequence_id += 1 end end diff --git a/app/models/old_relation.rb b/app/models/old_relation.rb index 6da7814c2..076c03eec 100644 --- a/app/models/old_relation.rb +++ b/app/models/old_relation.rb @@ -9,6 +9,7 @@ class OldRelation < ActiveRecord::Base old_relation.user_id = relation.user_id old_relation.timestamp = relation.timestamp old_relation.id = relation.id + old_relation.version = relation.version old_relation.members = relation.members old_relation.tags = relation.tags return old_relation diff --git a/app/models/old_way.rb b/app/models/old_way.rb index a2b165e42..cdc0c4717 100644 --- a/app/models/old_way.rb +++ b/app/models/old_way.rb @@ -9,6 +9,7 @@ class OldWay < ActiveRecord::Base old_way.user_id = way.user_id old_way.timestamp = way.timestamp old_way.id = way.id + old_way.version = way.version old_way.nds = way.nds old_way.tags = way.tags return old_way diff --git a/app/models/relation.rb b/app/models/relation.rb index 61344bdfb..3a9c0d9d5 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -167,13 +167,12 @@ class Relation < ActiveRecord::Base def save_with_history! Relation.transaction do t = Time.now + self.version += 1 self.timestamp = t self.save! tags = self.tags - RelationTag.delete_all(['id = ?', self.id]) - tags.each do |k,v| tag = RelationTag.new tag.k = k @@ -183,9 +182,7 @@ class Relation < ActiveRecord::Base end members = self.members - RelationMember.delete_all(['id = ?', self.id]) - members.each do |n| mem = RelationMember.new mem.id = self.id diff --git a/app/models/way.rb b/app/models/way.rb index f1dc76eb4..56c0717a7 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -158,15 +158,12 @@ class Way < ActiveRecord::Base t = Time.now Way.transaction do + self.version += 1 self.timestamp = t self.save! - end - WayTag.transaction do tags = self.tags - WayTag.delete_all(['id = ?', self.id]) - tags.each do |k,v| tag = WayTag.new tag.k = k @@ -174,13 +171,9 @@ class Way < ActiveRecord::Base tag.id = self.id tag.save! end - end - WayNode.transaction do nds = self.nds - WayNode.delete_all(['id = ?', self.id]) - sequence = 1 nds.each do |n| nd = WayNode.new @@ -189,11 +182,11 @@ class Way < ActiveRecord::Base nd.save! sequence += 1 end - end - old_way = OldWay.from_way(self) - old_way.timestamp = t - old_way.save_with_dependencies! + old_way = OldWay.from_way(self) + old_way.timestamp = t + old_way.save_with_dependencies! + end end def preconditions_ok? diff --git a/db/migrate/014_move_to_innodb.rb b/db/migrate/014_move_to_innodb.rb new file mode 100644 index 000000000..38c741cca --- /dev/null +++ b/db/migrate/014_move_to_innodb.rb @@ -0,0 +1,29 @@ +class MoveToInnodb < ActiveRecord::Migration + @@conv_tables = ['nodes', 'ways', 'way_tags', 'way_nodes', + 'current_way_nodes', 'relation_members', 'relations', + 'relation_tags', 'current_relation_tags'] + + @@ver_tbl = ['nodes', 'ways', 'relations'] + + def self.up + execute 'DROP INDEX current_way_tags_v_idx ON current_way_tags' + execute 'DROP INDEX current_relation_tags_v_idx ON current_relation_tags' + + @@ver_tbl.each { |tbl| + change_column tbl, "version", :bigint, :limit => 20, :null => false + } + + @@conv_tables.each { |tbl| + execute "ALTER TABLE #{tbl} ENGINE = InnoDB" + } + + @@ver_tbl.each { |tbl| + add_column "current_#{tbl}", "version", :bigint, :limit => 20, :null => false + execute "UPDATE current_#{tbl} SET version = " + + "(SELECT max(version)+1 FROM #{tbl} WHERE #{tbl}.id = current_#{tbl}.id)" + } + end + + def self.down + end +end -- 2.39.5