class Synapse::Repository::OptimisticLock

Lock that keeps track of an aggregate's version @api private

Attributes

closed[R]

@return [Boolean] True if this lock can be disposed

closed?[R]

@return [Boolean] True if this lock can be disposed

threads[R]

@return [Hash] Hash of threads to the number of times they hold the lock

Public Class Methods

new() click to toggle source
# File lib/synapse/repository/optimistic_lock_manager.rb, line 82
def initialize
  @closed = false
  @threads = Hash.new 0
end

Public Instance Methods

lock() click to toggle source

@return [Boolean] Returns false if lock is closed

# 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() click to toggle source

@return [undefined]

# 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) click to toggle source

@param [AggregateRoot] aggregate @return [Boolean]

# 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