lib/couchrest/model/design.rb in couchrest_model-2.1.0.rc1 vs lib/couchrest/model/design.rb in couchrest_model-2.2.0.beta1
- old
+ new
@@ -15,20 +15,22 @@
# Instantiate a new design document for this model
def initialize(model, prefix = nil)
self.model = model
self.method_name = self.class.method_name(prefix)
+ @lock = Mutex.new
suffix = prefix ? "_#{prefix}" : ''
self["_id"] = "_design/#{model.to_s}#{suffix}"
apply_defaults
end
def sync(db = nil)
if auto_update
db ||= database
if cache_checksum(db) != checksum
- sync!(db)
+ # Only allow one thread to update the design document at a time
+ @lock.synchronize { sync!(db) }
set_cache_checksum(db, checksum)
end
end
self
end
@@ -37,10 +39,11 @@
db ||= database
# Load up the last copy. We never blindly overwrite the remote copy
# as it may contain views that are not used or known about by
# our model.
+
doc = load_from_database(db)
if !doc || doc['couchrest-hash'] != checksum
# We need to save something
if doc
@@ -179,7 +182,5 @@
end
end
end
end
-
-