-
Notifications
You must be signed in to change notification settings - Fork 0
/
ninjadb.py
78 lines (60 loc) · 1.86 KB
/
ninjadb.py
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
import sys
import signal
# This is the model for the database
from dbstore import DBStore
class NinjaDB:
def __init__(self):
self.exit = False
# SIGINT handler, exit gracefully
signal.signal(signal.SIGINT, lambda signal, frame: sys.exit(0))
print('Welcome to NinjaDB 0.0.3')
# Initialize DBStore object
self.db = DBStore()
def main(self):
# start main loop until exit is True
while not self.exit:
try:
#sys.stdout.write('> ')
rawCMD = raw_input()
args = rawCMD.strip().split(' ')
if len(args) > 0:
# make the command uppercase
args[0] = args[0].upper()
#exit if we've reached the end of the file
except EOFError:
self.exit = True
break
if args[0] == 'SET' and lengthIs(args, 3):
self.db.set(args[1], args[2])
elif args[0] == 'GET' and lengthIs(args, 2):
res = self.db.get(args[1])
# print either the value or NULL if it does not exist
print('NULL' if res is None else res)
elif args[0] == 'UNSET' and lengthIs(args, 2):
self.db.unset(args[1])
elif args[0] == 'NUMEQUALTO' and lengthIs(args, 2):
count = self.db.getCount(args[1])
print(count)
elif args[0] == 'BEGIN':
self.db.begin()
elif args[0] == 'ROLLBACK':
if self.db.delta == -1:
print('NO TRANSACTION')
else:
self.db.rollback()
elif args[0] == 'COMMIT':
if self.db.delta == -1:
print('NO TRANSACTION')
else:
self.db.commit()
elif args[0] == 'END':
self.exit = True
break
else:
print('Invalid command entered: %s' % args[0])
# Helper function used to check the arguments length
def lengthIs(array, length):
return len(array) == length
if __name__ == '__main__':
ninja = NinjaDB()
ninja.main()