Created
September 23, 2013 18:57
-
-
Save denzilc/6675190 to your computer and use it in GitHub Desktop.
Very Simple In-Memory Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db = dict() | |
trans_history = [] #function structure | |
committed_trans_history = [] #outside block commands | |
trans_block_index = -1 #block index | |
''' SET [name] [value]: Set a variable [name] to the value [value]. Neither variable names nor values will ever contain spaces''' | |
def set_db(name, value): | |
db[name] = value | |
''' GET [name]: Print out the value stored under the variable [name]. Print NULL if that variable name hasn't been set. ''' | |
def get_db(name): | |
if name in db: | |
print db[name] | |
else: | |
print "NULL" | |
''' UNSET [name]: Unset the variable [name] ''' | |
def unset_db(name): | |
try: | |
val = db[name] | |
del db[name] | |
return val | |
except: | |
print "ENTRY NOT FOUND" | |
''' NUMEQUALTO [value]: Return the number of variables equal to [value]. If no values are equal, this should output 0. ''' | |
def numequalto_db(value): | |
print db.values().count(value) | |
''' COMMIT: Permanently store all of the operations from all presently open transactional blocks.''' | |
def commit_db(): | |
global trans_history | |
if len(trans_history) == 0: | |
print "NO TRANSACTION" | |
return | |
del trans_history[:] | |
''' ROLLBACK: Rollback all of the commands from the most recent transactional block. ''' | |
def rollback_db(): | |
global trans_history | |
global trans_block_index | |
if len(trans_history) == 0: | |
print "NO TRANSACTION" | |
else: | |
for command in reversed(trans_history[trans_block_index]): | |
ins = command[0] | |
if ins == set_db: | |
unset_db(str(command[1])) | |
elif ins == unset_db: | |
set_db(str(command[1]), int(command[2])) | |
del trans_history[-1] | |
trans_block_index = trans_block_index-1 | |
for command in committed_trans_history: | |
ins = command[0] | |
if ins == set_db: | |
set_db(str(command[1]), int(command[2])) | |
if ins == unset_db: | |
unset_db(str(command[1])) | |
if trans_block_index >= 0: | |
for i in range(0, trans_block_index+1): | |
for command in trans_history[i]: | |
ins = command[0] | |
if ins == set_db: | |
set_db(str(command[1]), int(command[2])) | |
if ins == unset_db: | |
unset_db(str(command[1])) | |
if __name__ == '__main__': | |
while(True): | |
cmd = raw_input() | |
if cmd == 'END': # END: Exit the program | |
exit(0) | |
if cmd == 'COMMIT': | |
commit_db() | |
if cmd == 'ROLLBACK': | |
rollback_db() | |
if cmd == 'BEGIN': | |
trans_block_index += 1 | |
if len(trans_history) <= trans_block_index: | |
trans_history.append([]) | |
else: | |
attrs = cmd.split(' ') | |
ins = attrs[0] | |
if ins == 'GET': | |
get_db(str(attrs[1])) | |
if ins == 'SET': | |
set_db(str(attrs[1]), int(attrs[2])) | |
if trans_block_index != -1: | |
trans_history[trans_block_index].append((set_db, attrs[1], attrs[2])) | |
else: | |
committed_trans_history.append((set_db, attrs[1], attrs[2])) | |
if ins == 'UNSET': | |
del_val = unset_db(str(attrs[1])) | |
if trans_block_index != -1: | |
trans_history[trans_block_index].append((unset_db, attrs[1], del_val)) | |
else: | |
committed_trans_history.append((unset_db, attrs[1], del_val)) | |
if ins == 'NUMEQUALTO': | |
numequalto_db(int(attrs[1])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment