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 validatelockingschedule 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, twophased 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 initself object transaction, type:
self.object object
self.transaction transaction
assert type in READ "WRITE", "LOCK", "UNLOCK"
self.type type
def strself:
return fActionselfobjectselftransactionselftype
class LegalityOfScheduleViolationException: pass
class TwoPhasedLockingViolationException: pass
class ConsistencyOfTransactionViolationException: pass
# Do not modify code above this line
def validatelockingscheduleactions:
locks set
transactions
for action in actions:
if action.type "LOCK":
if action.object in locks:
raise LegalityOfScheduleViolationfLocking violation: Object actionobject is already locked."
locks.addactionobject
elif action.type "UNLOCK":
if action.object not in locks:
raise LegalityOfScheduleViolationfUnlocking violation: Object actionobject is not locked."
locks.removeactionobject
elif action.type "READ" or action.type "WRITE":
if action.transaction in transactions:
if action.object in transactionsactiontransaction:
raise ConsistencyOfTransactionViolationfConsistency violation: Transaction actiontransaction performs multiple operations on object actionobject
else:
transactionsactiontransactionaddactionobject
else:
transactionsactiontransaction setactionobject
if locks:
raise TwoPhasedLockingViolationTwophased locking violation: Some objects are still locked at the end of the schedule."
fail:
def testvisibleself:
# TwoPhased Locking
actions
ActionobjectA transactionT type"LOCK"
ActionobjectB transactionT type"LOCK"
ActionobjectC transactionT type"LOCK"
ActionobjectD transactionT type"LOCK"
ActionobjectD transactionT type"UNLOCK"
ActionobjectE transactionT type"LOCK"
ActionobjectE transactionT type"UNLOCK"
ActionobjectA transactionT type"UNLOCK"
ActionobjectC transactionT type"UNLOCK"
ActionobjectB transactionT type"UNLOCK"
print
Actions:"
pprintactions
with self.assertRaisesTwoPhasedLockingViolation:
validatelockingscheduleactions
printCorrectly raised TwoPhasedLockingViolation"
actions
ActionobjectA transactionT type"LOCK"
ActionobjectB transactionT type"LOCK"
ActionobjectA transactionT type"UNLOCK"
ActionobjectA transactionT type"LOCK"
ActionobjectB transactionT type"UNLOCK"
ActionobjectA transactionT type"UNLOCK"
print
Actions:"
pprintactions
validatelockingscheduleactions
printCorrectly didn't raise exception"
error:
def testvisibleself:
# Legality of Schedules: locks block other locks
actions
ActionobjectA transactionT type"LOCK"
ActionobjectA transactionT type"READ"
ActionobjectA transactionT type"UNLOCK"
ActionobjectA transactionT type"LOCK"
ActionobjectA transactionT type"UNLOCK"
print
Actions:"
pprintactions
validatelockingscheduleactions
printCorrectly didn't raise exception"
actions
ActionobjectA transactionT type"LOCK"
ActionobjectB transactionT type"LOCK"
ActionobjectA transactionT type"READ"
ActionobjectA transactionT type"UNLOCK"
ActionobjectB transactionT type"UNLOCK"
printActions:
pprintactions
validatelockingscheduleactions
printOkay to repeatedly request to lock and unlock"
give code
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
