We have learned the transactions in the database class, and we also know what the isolation level is. In this article, we would implement it all by our own.
Terminology
Record
The single entity in the database, anything that encapsulates something. for example file, json object
Collection
The set of Record
Transaction ID
The unique number of each transaction.
Implmentation
We implement our code all in python.
Framework
1 | next_xid = 1 |
Visibility and Locking
Then to express the visibility and the locking function of each record, we need to add tow extra pieces of information to the record, the detailed code is shown below:
1 | class Transaction: |
1 | class Transaction: |
Add a record
1 | class Transaction: |
Deleting a record
1 | class Transaction: |
Updating a record
Updating can be splited into two procedures, deletion and appendix.
1 | class Transaction: |
Committing Changes
1 | class Transaction: |
Rollback Changes
1 | class Transaction: |
Implement some isolation level
In this part I will implement two kinds of isolation level: repeatable read and seriealiztion
Extend transaction class
1 | class Transaction: |
Based on the work above, we introduce the most simple isolation level ——— Read Uncommited
1 | class ReadUncommittedTransaction(Transaction): |
Then the case of read committed is a little complicated
1 | class ReadCommittedTransaction(Transaction): |
Then the next level is Repeatable Read
1 | class RepeatableReadTransaction(ReadCommittedTransaction): |
Next step is to add a new class to manage all the lock
1 | class LockManager: |
Based on which, we can implement serializable
1 | class SerializableTransaction(RepeatableReadTransaction): |