The writeup concept is on C++ syntax, but use Python.
Database
We assume each data item in the database is an floating point number. Each item has an ID from 0 onwards (to the # of items in the database - 1).
You should create a class Database to store the number. The specification of the class is as follows:
Constructor:
o Database(int size): size denote the number of data items in the database. Each data item is initialized to 0.0
o Database(int size, vector f): size denote the number of data items in the database. The data item are assigned to the value store in vector f. If f has fewer number than the database size the rest of the numbers are assigned 0.0. If f has more numbers that the database size, the extra numbers are ignored.
Methods:
o Float Read(int id): return the value item with that id
o Void Write(int id, float val): update the value of the data item with that id, where val is the value to be updated.
o Void Print(): you should provide a method to output the information of the database to the screen. The output should print the item of the database in order of the id. Each line should print 5 numbers. An example output for a database with 14 numbers are:
3.6 2 1.444 2.7 -13
6 12.3 44.33 11.5 -2.999
7.88 -0.25 4 1.02
(It is ok to print integer value with (say) 3 or 3.00000).
Transactions
Each transaction is a set of commands that read and write the database. It also has local memory to store numbers (will be labelled as 0..k, where k is provided by the user). The following is the list of commands for each transaction. (a, b, b1,b2 are integers and f is floating point numbers).
R a b : Read the a-th number in the database and store it in the b-th number of its local memory.
W a b : Write the b-th number in its local memory back to the a-th number to the database.
A b f : Add f to the b-th number in local memory.
M b f: Multiply f to the b-th number in local memory.
C a b : Copy the a-th number in local memory to the b-th location in local memory.
We assume each transaction is stored in a text file. The text file has the following format:
The first line have three numbers, the first number is the transaction ID, the second number is the number of local variables that transaction have, the third number is the number of commands in the transaction.
Each subsequent line is a command, in the format above.
Task
You are to write a program that read in a set of transactions, and then execute them. Also, you need to ensure serializability via 2-phase locking. Here is the detail:
Your program should first obtain the name of a text file (you should use command line parameter to pass it through). That file will have the following format
o The first line contain one integer, x, which denote the number of items in the database
o The next line contain x floating point numbers, denoting the initial value of each item in the database.
o The next line contain one integer, p, denoting the number of transactions
o The next p lines contains a list of file name (one filename per line), each of them is a transaction. (You should assume all files will be in the same directory as the one you run your program) You should then read in each file in the list, creating a transaction for each file. You should then “run” the transactions in the following way
o Pick a transaction in random
o Try executing the next instruction
o If the instruction require access to the database, you need to request the appropriate lock on that item of the database. (We assume locks are placed on each individual item).
o Notice that the lock request may not be granted. In such case, the same request is made the next time that transaction is picked.
o A transaction will commit immediately when the last operation of it executes successfully
o Once a transaction commits, it releases all the locks that it holds
o Repeat the steps above until all transaction has finished running (but see below)
If it turns out that every transaction is waiting for a lock and cannot proceed, you should declare that a deadlock has occurred and stop. (You do not need to implement a more sophisticated deadlock detection algorithm).
Output
You should output the following:
Whenever a lock is request, you should print out a statement:
“Transaction x, step q: request y-lock on z: k”
where x is the transaction id, q is the current step being executed (0 for first instruction, 1 for second instruction etc.), y is the type of lock requested (S or X), z is the database item that the transaction is trying to lock. k is either “granted” or “not granted”
Whenever a transaction executed a command, you should print out a statement:
“Transaction x, step q: execute command
Where x and q are the same as above,
If a transaction commits, you should print out a statement:
“Transaction x commits: locks released : ”
If a deadlock is formed, you should print out the statement:
“Deadlock occurs: transaction that are not finished: ”
If all transactions finished, you should print the content of the database
Implement the wait-die protocol for deadlock avoidance. Use the transaction ID, and the wait-die protocol to determine whether a transaction should wait or die if a lock cannot be obtained. If a transaction dies, you should rollback the operations (recall that a transaction may have issued a write command to update the database), and restart the transaction with the same transaction ID.
Hint: since you only release a lock when commit, you do not have to worry about cascade. Changes in the output; in the lock request statement, in addition to “granted” and “not granted”, if the transaction decided to abort, you should print “abort”.