some optimisations

This commit is contained in:
Andrew Williams 2011-03-01 22:06:42 +10:30
parent 4e8e5ee0d1
commit ce2e2fc9a6
4 changed files with 33 additions and 19 deletions

View file

@ -12,9 +12,9 @@ GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
actionpack (3.0.4)
activemodel (= 3.0.4)
activesupport (= 3.0.4)
actionpack (3.0.5)
activemodel (= 3.0.5)
activesupport (= 3.0.5)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.4)
@ -22,11 +22,11 @@ GEM
rack-mount (~> 0.6.13)
rack-test (~> 0.5.7)
tzinfo (~> 0.3.23)
activemodel (3.0.4)
activesupport (= 3.0.4)
activemodel (3.0.5)
activesupport (= 3.0.5)
builder (~> 2.1.2)
i18n (~> 0.4)
activesupport (3.0.4)
activesupport (3.0.5)
builder (2.1.2)
couchrest (1.0.1)
json (>= 1.4.6)
@ -43,9 +43,9 @@ GEM
rack (>= 1.0.0)
rack-test (0.5.7)
rack (>= 1.0)
railties (3.0.4)
actionpack (= 3.0.4)
activesupport (= 3.0.4)
railties (3.0.5)
actionpack (= 3.0.5)
activesupport (= 3.0.5)
rake (>= 0.8.7)
thor (~> 0.14.4)
rake (0.8.7)

View file

@ -12,6 +12,8 @@ class BenchmarkCasted < Hash
end
class BenchmarkModel < CouchRest::Model::Base
use_database CouchRest.database!(ENV['BENCHMARK_DB'] || "http://localhost:5984/test")
property :string, String
property :number, Integer
property :casted, BenchmarkCasted
@ -60,5 +62,13 @@ begin
n.times { b.casted_list }
end
# db writing
if ENV['BENCHMARK_DB']
x.report("write record") do
# need to make change before it will save
n.times { b.string = "test#{n}"; b.save }
end
end
end
end

View file

@ -18,9 +18,7 @@ module CouchRest
def couchrest_attribute_will_change!(attr)
return if attr.nil?
self.send("#{attr}_will_change!")
if pkey = casted_by_attribute
@casted_by.couchrest_attribute_will_change!(pkey)
end
couchrest_parent_will_change!
end
def couchrest_parent_will_change!
@ -31,9 +29,9 @@ module CouchRest
# return the attribute name this object is referenced by in the parent
def casted_by_attribute
return nil unless @casted_by
return @casted_by_attribute if @casted_by_attribute_set
attr = @casted_by.attributes
attr.keys.detect { |k| attr[k] == self }
@casted_by_attribute = attr.keys.detect { |k| attr[k] == self }
end
end

View file

@ -53,16 +53,22 @@ module CouchRest
end
def []=(key,value)
old_id = get_unique_id if self.respond_to?(:get_unique_id)
has_changes = self.changed?
if !has_changes && self.respond_to?(:get_unique_id)
check_id_change = true
old_id = get_unique_id
end
super(key, value)
ret = super(key, value)
if self.respond_to?(:get_unique_id)
if check_id_change
# if we have set an attribute that results in the _id changing (unique_id),
# force changed? to return true so that the record can be saved
new_id = get_unique_id
changed_attributes["_id"] = new_id if old_id != new_id
end
ret
end
# Takes a hash as argument, and applies the values by using writer methods
@ -181,8 +187,8 @@ module CouchRest
property(:created_at, Time, :read_only => true, :protected => true, :auto_validation => false)
set_callback :save, :before do |object|
write_attribute_dirty('updated_at', Time.now)
write_attribute_dirty('created_at', Time.now) if object.new?
write_attribute('updated_at', Time.now)
write_attribute('created_at', Time.now) if object.new?
end
EOS
end