Question: Valid Locking Schedule Write a function, named validate _ locking _ schedule that takes a list of Actions ( see below ) as its only

Valid Locking Schedule
Write a function, named validate_locking_schedule that takes a list of Actions (see below) as its only parameter. This function doesnt return anything, but instead raises exceptions if the schedule is invalid (according to legality of schedules, two-phased locking, and consistency of transactions). Be sure to validate the actions in that order. The exception classes are already defined for you in your starter solution.
The Action class is a simple class with attributes denoting the id of the database object (region), the id of the transaction the action takes place in, and the type of action (if the action was a read, write, lock, or unlock).
You can assume the action classs object_ and transaction attributes are strings or integers (types which support equality) and the type is an UPPERCASE string.
class Action:
"""
This is the Action class. It is already in the test cases, so please don't
put it in your solution.
"""
def __init__(self, object_, transaction, type_):
self.object_= object_
self.transaction = transaction
assert type_ in ("READ", "WRITE", "LOCK", "UNLOCK")
self.type_= type_
def __str__(self):
return f"Action({self.object_},{self.transaction},{self.type_})"
class LegalityOfScheduleViolation(Exception): pass
class TwoPhasedLockingViolation(Exception): pass
class ConsistencyOfTransactionViolation(Exception): pass
# Do not modify code above this line
def validate_locking_schedule(actions):
locks = set()
transactions ={}
for action in actions:
if action.type_== "LOCK":
if action.object_ in locks:
raise LegalityOfScheduleViolation(f"Locking violation: Object {action.object_} is already locked.")
locks.add(action.object_)
elif action.type_== "UNLOCK":
if action.object_ not in locks:
raise LegalityOfScheduleViolation(f"Unlocking violation: Object {action.object_} is not locked.")
locks.remove(action.object_)
elif action.type_== "READ" or action.type_== "WRITE":
if action.transaction in transactions:
if action.object_ in transactions[action.transaction]:
raise ConsistencyOfTransactionViolation(f"Consistency violation: Transaction {action.transaction} performs multiple operations on object {action.object_}.")
else:
transactions[action.transaction].add(action.object_)
else:
transactions[action.transaction]= set([action.object_])
if locks:
raise TwoPhasedLockingViolation("Two-phased locking violation: Some objects are still locked at the end of the schedule.")
fail:
def test_visible_3(self):
# (Two-Phased Locking)
actions =[
Action(object_="A", transaction="T2", type_="LOCK"),
Action(object_="B", transaction="T2", type_="LOCK"),
Action(object_="C", transaction="T2", type_="LOCK"),
Action(object_="D", transaction="T2", type_="LOCK"),
Action(object_="D", transaction="T2", type_="UNLOCK"),
Action(object_="E", transaction="T2", type_="LOCK"),
Action(object_="E", transaction="T2", type_="UNLOCK"),
Action(object_="A", transaction="T2", type_="UNLOCK"),
Action(object_="C", transaction="T2", type_="UNLOCK"),
Action(object_="B", transaction="T2", type_="UNLOCK"),
]
print("
Actions:")
pprint(actions)
with self.assertRaises(TwoPhasedLockingViolation):
validate_locking_schedule(actions)
print("Correctly raised TwoPhasedLockingViolation")
actions =[
Action(object_="A", transaction="T2", type_="LOCK"),
Action(object_="B", transaction="T2", type_="LOCK"),
Action(object_="A", transaction="T2", type_="UNLOCK"),
Action(object_="A", transaction="T1", type_="LOCK"),
Action(object_="B", transaction="T2", type_="UNLOCK"),
Action(object_="A", transaction="T1", type_="UNLOCK"),
]
print("
Actions:")
pprint(actions)
validate_locking_schedule(actions)
print("Correctly didn't raise exception")
error:
def test_visible_2(self):
# Legality of Schedules: locks block other locks)
actions =[
Action(object_="A", transaction="T2", type_="LOCK"),
Action(object_="A", transaction="T2", type_="READ"),
Action(object_="A", transaction="T2", type_="UNLOCK"),
Action(object_="A", transaction="T1", type_="LOCK"),
Action(object_="A", transaction="T1", type_="UNLOCK"),
]
print("
Actions:")
pprint(actions)
validate_locking_schedule(actions)
print("Correctly didn't raise exception")
actions =[
Action(object_="A", transaction="T2", type_="LOCK"),
Action(object_="B", transaction="T1", type_="LOCK"),
Action(object_="A", transaction="T2", type_="READ"),
Action(object_="A", transaction="T2", type_="UNLOCK"),
Action(object_="B", transaction="T1", type_="UNLOCK"),
]
print("Actions:")
pprint(actions)
validate_locking_schedule(actions)
print("Okay to repeatedly request to lock and unlock")
the code I gave is a basis please give code that will pass the above test cases

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!