-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring with a number of improvements:
* can use the regular #save or #save! instead of #save_optimistic! * can now use locking with #destroy * can use #unlock to persist without locking
- Loading branch information
Showing
21 changed files
with
372 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
Gemfile.lock | ||
pkg/* | ||
.idea | ||
*.iml | ||
*.iml | ||
.rvmrc |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
require "bundler/gem_tasks" | ||
|
||
require 'rspec/core/rake_task' | ||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
task :default => :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'mongoid/errors/mongoid_error' | ||
|
||
module Mongoid | ||
module Errors | ||
|
||
# Raised when trying to update a document that has been updated by | ||
# another process. | ||
# | ||
# @example Create the error. | ||
# StaleDocument.new('update', document) | ||
class StaleDocument < MongoidError | ||
|
||
attr_reader :action, :document | ||
|
||
def initialize(action, document) | ||
@action = action | ||
@document = document | ||
|
||
super( | ||
translate( | ||
"stale_document.#{action}", | ||
{ :klass => document.class.name } | ||
) | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Mongoid | ||
|
||
module Lockable | ||
|
||
extend ActiveSupport::Concern | ||
|
||
included do | ||
ActiveSupport::Deprecation.warn 'Mongoid::Lockable is deprecated and will be removed. Use Mongoid::OptimisticLocking instead.', caller | ||
include Mongoid::OptimisticLocking | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'mongoid' | ||
require 'mongoid/errors/stale_document' | ||
require 'mongoid/lockable' | ||
require 'mongoid/optimistic_locking/deprecated' | ||
require 'mongoid/optimistic_locking/lock_version' | ||
require 'mongoid/optimistic_locking/operations' | ||
require 'mongoid/optimistic_locking/threaded_with_unlocked' | ||
require 'mongoid/optimistic_locking/unlocked' | ||
require 'mongoid/optimistic_locking/version' | ||
|
||
# monkey patch Threaded | ||
Mongoid::Threaded.send :include, Mongoid::OptimisticLocking::ThreadedWithUnlocked | ||
|
||
# add english load path to translations | ||
I18n.load_path << File.expand_path('../../config/locales/en.yml', __FILE__) | ||
|
||
module Mongoid | ||
# == What is Optimistic Locking | ||
# | ||
# See <http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html>. | ||
# | ||
# == Usage | ||
# | ||
# TODO ... | ||
module OptimisticLocking | ||
|
||
extend ActiveSupport::Concern | ||
|
||
include Deprecated | ||
include LockVersion | ||
include Operations | ||
include Unlocked | ||
|
||
included do | ||
field LOCKING_FIELD, :type => Integer, :default => 0 | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Mongoid | ||
module OptimisticLocking | ||
module Deprecated | ||
|
||
def save_optimistic!(*args) | ||
ActiveSupport::Deprecation.warn 'save_optimistic! is deprecated and will be removed. Use save or save! instead', caller | ||
save! *args | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Mongoid | ||
module OptimisticLocking | ||
module LockVersion | ||
|
||
LOCKING_FIELD = :_lock_version | ||
|
||
private | ||
|
||
attr_reader :lock_version_for_selector | ||
|
||
def set_lock_version_for_selector | ||
@lock_version_for_selector = self[LOCKING_FIELD] | ||
yield | ||
rescue Exception | ||
@lock_version_for_selector = nil | ||
raise | ||
end | ||
|
||
def increment_lock_version | ||
self[LOCKING_FIELD] = self[LOCKING_FIELD] ? self[LOCKING_FIELD] + 1 : 1 | ||
yield | ||
rescue Exception | ||
self[LOCKING_FIELD] = self[LOCKING_FIELD] ? self[LOCKING_FIELD] - 1 : 0 | ||
raise | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module Mongoid | ||
module OptimisticLocking | ||
module Operations | ||
|
||
def insert(*args) | ||
return super unless optimistic_locking? | ||
increment_lock_version do | ||
super | ||
end | ||
end | ||
|
||
def update(*args) | ||
return super unless optimistic_locking? | ||
set_lock_version_for_selector do | ||
increment_lock_version do | ||
result = super | ||
unless Mongoid.database.command({:getlasterror => 1})['updatedExisting'] | ||
raise Mongoid::Errors::StaleDocument.new('update', self) | ||
end | ||
result | ||
end | ||
end | ||
end | ||
|
||
def remove(*args) | ||
return super unless optimistic_locking? | ||
set_lock_version_for_selector do | ||
result = super | ||
unless Mongoid.database.command({:getlasterror => 1})['updatedExisting'] | ||
raise Mongoid::Errors::StaleDocument.new('destroy', self) | ||
end | ||
result | ||
end | ||
end | ||
|
||
def atomic_selector | ||
result = super | ||
if optimistic_locking? && lock_version_for_selector | ||
key = | ||
if metadata && metadata.embedded? | ||
path = metadata.path(self) | ||
"#{path.path}._lock_version" | ||
else | ||
'_lock_version' | ||
end | ||
result[key] = lock_version_for_selector | ||
end | ||
result | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Mongoid | ||
module OptimisticLocking | ||
module ThreadedWithUnlocked | ||
|
||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
|
||
def optimistic_locking? | ||
!unlocked | ||
end | ||
|
||
def unlocked | ||
!!Thread.current["[mongoid]:unlocked"] | ||
end | ||
|
||
def unlocked=(value) | ||
Thread.current["[mongoid]:unlocked"] = value | ||
end | ||
|
||
def clear_options! | ||
self.unlocked = false | ||
super | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Mongoid | ||
module OptimisticLocking | ||
module Unlocked | ||
|
||
extend ActiveSupport::Concern | ||
|
||
def unlocked | ||
Threaded.unlocked = true | ||
self | ||
end | ||
|
||
def optimistic_locking? | ||
Threaded.optimistic_locking? | ||
end | ||
|
||
module ClassMethods | ||
|
||
def unlocked | ||
Threaded.unlocked = true | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Mongoid | ||
module OptimisticLocking | ||
VERSION = '0.0.1' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
require "mongoid_optimistic_locking/version" | ||
require File.join(File.dirname(__FILE__), 'mongoid_optimistic_locking/lockable') | ||
I18n.load_path << File.join(File.dirname(__FILE__), "config", "locales", "en.yml") | ||
require 'mongoid/optimistic_locking' |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.