-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrencyControlManager.py
More file actions
62 lines (52 loc) · 2.49 KB
/
Copy pathConcurrencyControlManager.py
File metadata and controls
62 lines (52 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from models.TwoPhaseLocking import TwoPhaseLocking
from models.TimeStampOrdering import TimestampOrdering
from models.ControllerMethod import ControllerMethod
from models.Schedule import Schedule
from models.Transaction import Transaction
from models.Operation import Operation
from models.Response import Response
from models.CCManagerEnums import ResponseType, OperationStatus, TransactionStatus
class ConcurrencyControlManager:
"""Manages concurrency control for transactions."""
def __init__(self, controller="2PL"):
"""
Initialize the ConcurrencyControlManager.
:param controller: A string to specify the concurrency control method ("2PL" or "TSO").
Default is "2PL".
"""
if controller == "TSO":
self.controller = TimestampOrdering()
else:
self.controller = TwoPhaseLocking()
self.schedule = Schedule()
self.running = True
def begin_transaction(self) -> int:
"""
Start a new transaction. This will give the new Query (queries) transaction id
and construct a new Transaction object. When query processor send the transactional or non-transactional queries to be processed,
this procedure will be called to change the schedule state.
"""
new_transaction = Transaction() #Initialize new transaction
new_transaction.setTransactionStatus(TransactionStatus.ACTIVE)
self.schedule.addTransaction(new_transaction) #Adding the new transaction to transaction list
return new_transaction.getTransactionID()
def log_object(self, object: Operation):
"""
Query processor call this function after getting validating operation can be run
"""
return self.controller.log_object(object)
def validate_object(self, object: Operation) -> Response:
"""
Query processor call this function to get the information whether the operation can be run or not.
"""
return self.controller.validate_object(object)
def end_transaction(self, transaction_id: int):
"""
Flushes objects belonging to a particular transaction after it has successfully committed/aborted.
e.g. unlocking resources
Also terminates the transaction.
:param transaction_id: The ID of the transaction to end.
"""
self.controller.end_transaction(transaction_id)
def getWaitingTransactionsList(self):
return self.schedule.getTransactionWaitingList