annotate async_multi_threads/transaction.py @ 69:551d9fc0a2ba

Updated wrong names.
author June Park <parkjune1995@gmail.com>
date Thu, 25 Dec 2025 20:07:46 -0800
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
69
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
1 # Question: In-Memory Key-Value Store with Nested Transactions
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
2 #
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
3 # Design an in-memory key-value database that supports the following commands and fully handles nested transactions.
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
4 # Core Operations:
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
5
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
6 # SET <key> <value>: Sets the value for a key.
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
7
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
8 # GET <key>: Returns the value for a key.
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
9
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
10 # UNSET <key>: Removes the key and its value.
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
11 #
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
12 # Transaction Operations:
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
13
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
14 # BEGIN: Starts a new transaction scope. If a transaction is already active, this starts a nested transaction.
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
15
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
16 # COMMIT: Applies all changes made in the current transaction scope and all its active nested transactions to the parent scope (or to the main database state if no parent exists). After a successful commit, the transaction scope is closed.
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
17
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
18 # ROLLBACK: Discards all changes made in the current transaction scope and all its active nested transactions, restoring the state to what it was when the BEGIN command was issued for the current scope. After a successful rollback, the transaction scope is closed.
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
19
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
20 # Implementation Goal:
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
21 # Design the primary data structures and outline the logic for SET, COMMIT, and ROLLBACK to ensure nested transactions operate correctly. Explain how the state of the database is managed across multiple transaction layers.
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
22
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
23
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
24 class Database:
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
25 def __init__(self):
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
26 self.global_db = {}
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
27 self.queue = []
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
28
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
29 def commit(self):
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
30 copy_db = self.queue.pop()
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
31 if self.queue:
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
32 self.queue[-1].merge(copy_db)
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
33 else:
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
34 self.global_db |= copy_db
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
35
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
36 def set(self, key, value):
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
37 if self.queue:
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
38 curr_db = self.queue[-1]
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
39 curr_db[key] = value
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
40 else:
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
41 self.global_db[key] = value
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
42
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
43 def get(self, key):
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
44 if self.queue:
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
45 for copy_db in reversed(self.queue):
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
46 if key in copy_db:
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
47 return copy_db[key]
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
48
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
49 return self.global_db[key]
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
50
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
51 def unset(self, key):
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
52 if self.queue:
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
53 curr_db = self.queue[-1]
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
54 del curr_db[key]
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
55 else:
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
56 del self.global_db[key]
551d9fc0a2ba Updated wrong names.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
57