-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.py
More file actions
84 lines (73 loc) · 1.91 KB
/
Database.py
File metadata and controls
84 lines (73 loc) · 1.91 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import copy
from HashDB import HashDB
from TreeDB import TreeDB, Node
class BlockHolder(object):
'''
A block object to handle commits and rollbacks
Attributes:
dbType: string selecting database algorithm
dbList: a list of each database block
Methods:
run: listen for and parse input
begin: begin new database block
rollback: discard all changes since last commit
commit: save all changes by merging into the first database block
parseInput: parses stdin and determines what function to call
'''
def __init__(self, dbType):
'''
dbType: string 'hash' or 'tree'
'''
self.dbType = dbType
if self.dbType == 'tree':
db = TreeDB()
self.dbList = [db]
elif self.dbType == 'hash':
db = HashDB()
self.dbList = [db]
def run(self):
x = str(raw_input()).split()
if not self.parseInput(x, self.dbList[-1]):
return False
return True
def begin(self):
if self.dbType == 'hash':
newDB = HashDB()
newDB.mainDB = copy.deepcopy(self.dbList[-1].mainDB)
self.dbList.append(newDB)
elif self.dbType == 'tree':
newDB = TreeDB()
newDB.mainDB = copy.deepcopy(self.dbList[-1].mainDB)
self.dbList.append(newDB)
def rollback(self):
self.dbList = [self.dbList[0],]
def commit(self):
if self.dbList[0] == self.dbList[-1]:
print "NO TRANSACTION"
else:
self.dbList = [self.dbList[-1],]
def parseInput(self, x, block):
'''
x: stdin
block: database block to update
'''
if x[0] == 'END':
return False
elif x[0] == 'ROLLBACK':
self.rollback()
elif x[0] == 'BEGIN':
self.begin()
elif x[0] == 'COMMIT':
self.commit()
else:
block.parseInput(x)
return True
if __name__ == '__main__':
# db = TreeDB()
# bh = BlockHolder('tree')
# while bh.run():
# pass
db = HashDB()
bh = BlockHolder('hash')
while bh.run():
pass