split_node_tags:
authorGabriel Ebner <gabriel@svn.openstreetmap.org>
Sat, 3 May 2008 16:27:49 +0000 (16:27 +0000)
committerGabriel Ebner <gabriel@svn.openstreetmap.org>
Sat, 3 May 2008 16:27:49 +0000 (16:27 +0000)
  - Use InnoDB.
  - Put version column on the current_* tables.
  - Use transactions (untested).

app/controllers/node_controller.rb
app/controllers/relation_controller.rb
app/controllers/way_controller.rb
app/models/node.rb
app/models/old_node.rb
app/models/old_relation.rb
app/models/old_way.rb
app/models/relation.rb
app/models/way.rb
db/migrate/014_move_to_innodb.rb [new file with mode: 0644]

index edc3675..9f8f4a3 100644 (file)
@@ -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!
index 2b1ba6c..f4e9381 100644 (file)
@@ -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!
 
index 3b6491c..a7f74e5 100644 (file)
@@ -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!
 
index 5a8e03e..1752142 100644 (file)
@@ -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
index 4f10e65..2f960d8 100644 (file)
@@ -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
 
index 6da7814..076c03e 100644 (file)
@@ -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
index a2b165e..cdc0c47 100644 (file)
@@ -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
index 61344bd..3a9c0d9 100644 (file)
@@ -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
index f1dc76e..56c0717 100644 (file)
@@ -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 (file)
index 0000000..38c741c
--- /dev/null
@@ -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