]> git.openstreetmap.org Git - rails.git/commitdiff
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 edc3675e58382fce0b8b5801a2e7180ab280cec2..9f8f4a38bdf48e200dae71ce99e14cbf9ded46c3 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 2b1ba6c753c70df6579d381facebc7bd451be754..f4e938176c214a3d78bb83571d2cfd0400a331e8 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 3b6491cf0b3ceda5ed90ee7e1da5e49579c6af21..a7f74e50c7e66e465581c3deb575a582df5670b2 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 5a8e03ef170ca49880524c3541f6598f47d29137..17521428ea260cc9bab6defcdcc9457958968f70 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 4f10e65a5767ed87fa9515a9fe74441d0f0ed0ec..2f960d8866a3239066a14f1198ddac08a06646ff 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 6da7814c28a40d442e856239e09149b2ca8727c5..076c03eec72dad790f09a3857038a071ca99d96a 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 a2b165e42716f5cfa7f095aa76654616274f95b5..cdc0c4717c1df571f8a289c7907a71586e69a9e4 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 61344bdfb62021fad69b67c72cf4e97d0b59219f..3a9c0d9d5ff4b881b895b9c2492167814ee7a690 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 f1dc76eb4c6b65053214e1e80f6665f70ad10052..56c0717a7ac1ffc1ff713703b55d7d0320877880 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