-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtrees.py
80 lines (68 loc) · 2.3 KB
/
btrees.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
79
80
import re
from BTrees.OOBTree import OOBTreePy
import timeit
datafile = "D:\\DB HW\\dataFull.txt" #data file path
f = open(datafile, "r")
searchFile = open('SearchFileBtree.txt','w') #Output Search file
btree = OOBTreePy()
dataTable = []
index = 0
for line in f:
key, value = line.strip().split('|')
dataTable.append([key,value])
index += 1
btree[key] = index
inputFile = "D:\\DB HW\\inputFile.txt" #input file path
commandFile = open(inputFile,'r')
startTime = timeit.timeit()
try:
def search(hkey):
searchIndex = btree[hkey]
returnValue = dataTable[searchIndex]
return returnValue[1]
def delete(hkey):
deleteIndex = btree[hkey]
dataTable[deleteIndex] = [hkey,'deleted']
del btree[hkey]
def insert(hkey,hvalue):
dataTable.append([hkey,hvalue])
global index
index += 1
btree[key] = index
for line in commandFile:
if line[0].lower() == 'i':
stringMatch = re.search('\(.+\)',line)
stringMatch = stringMatch.group(0)
stringMatch = stringMatch[1:-1].split(',')
key = stringMatch[0]
value = stringMatch[1]
insert(key,value)
if line[0].lower() == 's':
stringMatch = re.search('\(.+\)',line)
stringMatch = stringMatch.group(0)
key = stringMatch[1:-1]
if key in btree:
searchReturn = search(key)
searchFile.write(searchReturn + "\n")
else:
print("Key not present\n")
if line[0].lower() == 'd':
stringMatch = re.search('\(.+\)',line)
stringMatch = stringMatch.group(0)
key = stringMatch[1:-1]
if key in btree:
delete(key)
else:
print("Key not present\n")
except IndexError:
print("Please provide two values for Insert\n")
endTime = timeit.timeit()
timeTaken = endTime - startTime
printTable = open("FinalTableBtree.txt",'w')
for val in dataTable:
val = str(val)+"\n"
printTable.write(val)
timeStampFile = open("TimeTakenBtree.txt","w")
timeStampFile.write(str(startTime)+"\n")
timeStampFile.write(str(endTime)+"\n")
timeStampFile.write(str(timeTaken))