Class: Spinoza::LockManager
- Inherits:
-
Object
- Object
- Spinoza::LockManager
show all
- Defined in:
- lib/spinoza/system/lock-manager.rb
Overview
Manages concurrency in the spinoza system model, which explicitly schedules all database reads and writes. So all this does is check for concurrency violations; nothing actually blocks.
The txn
references in this class care only about identity, so they could all be ids or they could all be transaction objects. Similarly the resource being locked by a ReadLock or WriteLock can be anything whose identity is defined by hash equality, i.e. #eql?. Typically, we use ‘[table, key]` pairs, where `key` is a primary key reference like `…`.
Defined Under Namespace
Classes: ConcurrencyError, ReadLock, WriteLock
Instance Attribute Summary collapse
-
#locks ⇒ Object
readonly
{ resource => WriteLock | ReadLock | nil, … } typically, resource == [table, key].
Instance Method Summary
collapse
Constructor Details
Returns a new instance of LockManager.
88
89
90
|
# File 'lib/spinoza/system/lock-manager.rb', line 88
def initialize
@locks = {}
end
|
Instance Attribute Details
#locks ⇒ Object
{ resource => WriteLock | ReadLock | nil, … } typically, resource == [table, key]
86
87
88
|
# File 'lib/spinoza/system/lock-manager.rb', line 86
def locks
@locks
end
|
Instance Method Details
#has_read_lock?(resource, txn) ⇒ Boolean
159
160
161
162
|
# File 'lib/spinoza/system/lock-manager.rb', line 159
def has_read_lock? resource, txn
lock = locks[resource]
lock.kind_of?(ReadLock) && lock.includes?(txn)
end
|
#has_write_lock?(resource, txn) ⇒ Boolean
164
165
166
167
|
# File 'lib/spinoza/system/lock-manager.rb', line 164
def has_write_lock? resource, txn
lock = locks[resource]
lock.kind_of?(WriteLock) && lock.includes?(txn)
end
|
#lock_read(resource, txn) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/spinoza/system/lock-manager.rb', line 92
def lock_read resource, txn
case lock = locks[resource]
when nil
locks[resource] = ReadLock.new(txn)
when ReadLock, WriteLock
lock.add txn
else raise
end
end
|
#lock_write(resource, txn) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/spinoza/system/lock-manager.rb', line 104
def lock_write resource, txn
case lock = locks[resource]
when nil
locks[resource] = WriteLock.new(txn)
when WriteLock
lock.add txn
when ReadLock
raise ConcurrencyError, "#{resource} is locked: #{lock}"
else raise
end
end
|
#unlock_all(txn) ⇒ Object
150
151
152
153
154
155
156
157
|
# File 'lib/spinoza/system/lock-manager.rb', line 150
def unlock_all txn
locks.delete_if do |resource, lock|
if lock and lock.includes? txn
lock.remove txn
lock.unlocked?
end
end
end
|
#unlock_read(resource, txn) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/spinoza/system/lock-manager.rb', line 116
def unlock_read resource, txn
lock = locks[resource]
case lock
when nil
raise ConcurrencyError, "#{resource} is not locked"
when ReadLock, WriteLock
begin
lock.remove txn
locks.delete resource if lock.unlocked?
rescue ConcurrencyError => ex
raise ConcurrencyError "#{resource}: #{ex.message}"
end
else raise
end
end
|
#unlock_write(resource, txn) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/spinoza/system/lock-manager.rb', line 132
def unlock_write resource, txn
lock = locks[resource]
case lock
when ReadLock
raise ConcurrencyError, "#{resource} is read locked: #{lock}"
when nil
raise ConcurrencyError, "#{resource} is not locked"
when WriteLock
begin
lock.remove txn
locks.delete resource if lock.unlocked?
rescue ConcurrencyError => ex
raise ConcurrencyError "#{resource}: #{ex.message}"
end
else raise
end
end
|