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.



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

#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



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

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



79
80
81
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 79

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



104
105
106
107
108
109
110
111
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 104

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)


114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 114

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)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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
    if last_committed.nil?
      last_committed = 0
    end

    @version = last_committed + aggregate.uncommitted_event_count

    true
  else
    false
  end
end