Class: Synapse::Repository::OptimisticLock Private

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeOptimisticLock

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

#closedBoolean (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.

Returns:

  • (Boolean)

    True if this lock can be disposed



75
76
77
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 75

def closed
  @closed
end

#threadsHash (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.

Returns:

  • (Hash)

    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

#lockBoolean

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

Returns:

  • (Boolean)

    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

#unlockundefined

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:

  • (undefined)


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.

Parameters:

  • aggregate (AggregateRoot)

Returns:

  • (Boolean)


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