-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleDB.py
More file actions
133 lines (97 loc) · 3.12 KB
/
Copy pathsimpleDB.py
File metadata and controls
133 lines (97 loc) · 3.12 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
###############
# Calvin Chan #
###############
# Thumbtack coding challenge
# Stack ADT is to hold the DB and Values (for NUMEQUALTO)
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
# db holds all the values from SET
db = {}
# This holds the frequency of each variable, for NUMEQUALTO
valDict = {}
# These stacks hold the dbs and valDicts during the transactions
db_Stack = Stack()
val_Stack = Stack()
# Switch for loops
currentInput = ""
def databaseCommands(dataBase, valuesDict, dbStack, valuesStack):
global currentInput
while currentInput != None:
currentInput = raw_input()
# Splits the commands into parts
dbCommand = currentInput.split(" ")
if dbCommand[0] == "SET":
name = dbCommand[1]
value = dbCommand[2]
# If the item is already in the database,
# set is mutating this value - thus decrementing
# the value in valuesDict is necessary
if dataBase.get(name, 0) != 0:
currentValue = dataBase[name]
valuesDict[currentValue] -= 1
dataBase[name] = value
if value in valuesDict:
valuesDict[value] += 1
else:
valuesDict[value] = 1
elif dbCommand[0] == "GET":
name = dbCommand[1]
if name in dataBase:
print "%s" % dataBase[name]
else:
print "NULL"
elif dbCommand[0] == "UNSET":
name = dbCommand[1]
value = dataBase[name]
del dataBase[name]
if value in valuesDict:
valuesDict[value] -= 1
elif dbCommand[0] == "NUMEQUALTO":
value = dbCommand[1]
if value in valuesDict:
print "%d" % valuesDict[value]
else:
print "0"
elif dbCommand[0] == "END":
currentInput = None
else:
# input doesn't correspond to data commands,
# it can either be transactional or invalid
transactionCommands(dbCommand[0], dataBase, valuesDict, dbStack, valuesStack)
def transactionCommands(transactionInput, dataBase, valuesDict, dbStack, valuesStack):
if transactionInput == "BEGIN":
# Existing database & valueDict is pushed onto
# the stack for future ROLLBACK reference
dbStack.push(dataBase.copy())
valuesStack.push(valuesDict.copy())
databaseCommands(dataBase, valuesDict, dbStack, valuesStack)
elif transactionInput == "ROLLBACK":
if dbStack.isEmpty() or valuesStack.isEmpty():
print "NO TRANSACTION"
else:
# Pops off the top of the stack and reinstates the
# dataBase and valuesDict as the previous db/valDict
dataBase = dbStack.pop()
valuesDict = valuesStack.pop()
databaseCommands(dataBase, valuesDict, dbStack, valuesStack)
elif transactionInput == "COMMIT":
# Empties the stack, thus committing all the
# changes by erasing the previous dbs and
# valDicts if they are non-empty
if dbStack.isEmpty() or valuesStack.isEmpty():
print "NO TRANSACTION"
databaseCommands(dataBase, valuesDict, dbStack, valuesStack)
else:
dbStack = Stack()
valuesStack = Stack()
databaseCommands(dataBase, valuesDict, dbStack, valuesStack)
else:
print "INVALID INPUT"
databaseCommands(db, valDict, db_Stack, val_Stack)