Class: Synapse::Repository::OptimisticLock Private
- Inherits:
-
Object
- Object
- Synapse::Repository::OptimisticLock
- Defined in:
- lib/synapse/repository/optimistic_lock_manager.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Lock that keeps track of an aggregate’s version
Instance Attribute Summary collapse
-
#closed ⇒ Boolean
(also: #closed?)
readonly
private
True if this lock can be disposed.
-
#threads ⇒ Hash
readonly
private
Hash of threads to the number of times they hold the lock.
Instance Method Summary collapse
-
#initialize ⇒ OptimisticLock
constructor
private
A new instance of OptimisticLock.
-
#lock ⇒ Boolean
private
Returns false if lock is closed.
- #unlock ⇒ undefined private
- #validate(aggregate) ⇒ Boolean private
Constructor Details
#initialize ⇒ OptimisticLock
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of OptimisticLock.
82 83 84 85 |
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 82 def initialize @closed = false @threads = Hash.new 0 end |
Instance Attribute Details
#closed ⇒ Boolean (readonly) Also known as: closed?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns True if this lock can be disposed.
75 76 77 |
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 75 def closed @closed end |
#threads ⇒ Hash (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Hash of threads to the number of times they hold the lock.
80 81 82 |
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 80 def threads @threads end |
Instance Method Details
#lock ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns false if lock is closed
100 101 102 103 104 105 106 107 |
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 100 def lock if @closed false else @threads[Thread.current] = @threads[Thread.current] + 1 true end end |
#unlock ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 110 def unlock count = @threads[Thread.current] if count <= 1 @threads.delete Thread.current else @threads[Thread.current] = @threads[Thread.current] - 1 end if @threads.empty? @closed = true end end |
#validate(aggregate) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
89 90 91 92 93 94 95 96 97 |
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 89 def validate(aggregate) last_committed = aggregate.version if @version.nil? || @version == last_committed @version = (last_committed || 0) + aggregate.uncommitted_event_count true else false end end |