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.
81 82 83 84 |
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 81 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.
74 75 76 |
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 74 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.
79 80 81 |
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 79 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
99 100 101 102 103 104 105 106 |
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 99 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.
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 109 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.
88 89 90 91 92 93 94 95 96 |
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 88 def validate(aggregate) last_committed = aggregate.version if @version.nil? or @version.eql? last_committed @version = (last_committed or 0) + aggregate.uncommitted_event_count true else false end end |