-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternalMergeSort.py
327 lines (269 loc) · 8.83 KB
/
externalMergeSort.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import os
import sys
from operator import itemgetter
import heapq
import threading
import time
start_time = time.time()
MEMORY_LIMIT = 0
THREAD_COUNT = 0
INPUT_FILE = 'input.txt'
OUTPUT_FILE = 'output.txt'
TUPLE_SIZE = 0
TOTAL_TUPLES = 0
TOTAL_FILESIZE = 0
TOTAL_SUBFILES = 0
TUPLES_PER_SUBFILE = 0
ROW_LEN = 0
TUPLES_PER_THREAD = 0
cols_to_sort = []
col_index_to_sort = []
thread_reqd = False
asc = False
col_details = dict()
file_counter = 0
class heap_object(object):
def __init__(self, val, filename):
self.val = val
self.filename = filename
def __lt__(self, other):
for i in col_index_to_sort:
if(asc == True):
if(self.val[i] < other.val[i]):
return True
elif(self.val[i] > other.val[i]):
return False
else:
if(self.val[i] > other.val[i]):
return True
elif(self.val[i] < other.val[i]):
return False
return False
def check_args(args):
print('[CHECKING] Arguments')
if(len(args) < 6):
print('[ERROR] Invalid Argument Count')
sys.exit(1)
global MEMORY_LIMIT, thread_reqd, cols_to_sort, asc, THREAD_COUNT, INPUT_FILE, OUTPUT_FILE
MEMORY_LIMIT = int(args[3])*1000*1000
try:
THREAD_COUNT = int(args[4])
thread_reqd = True
except:
thread_reqd = False
start = 4
if(thread_reqd == True):
start = 5
asc = True if args[start].lower() == 'asc' else False
for i in range(start+1, len(args)):
cols_to_sort.append(args[i])
INPUT_FILE = args[1]
OUTPUT_FILE = args[2]
def read_metadata():
print('[READING] Metadata')
global TUPLE_SIZE, col_details
with open('metadata.txt') as f:
index = 0
for line in f:
line = line.split(',')
size = int(line[1].rstrip())
col_details[line[0]] = [index, size]
TUPLE_SIZE += size
index += 1
f.close()
for i in cols_to_sort:
col_index_to_sort.append(col_details[i][0])
def set_details():
global TOTAL_TUPLES, TOTAL_FILESIZE, TOTAL_SUBFILES, TUPLES_PER_SUBFILE, ROW_LEN
with open(INPUT_FILE) as f:
for i, l in enumerate(f):
pass
TOTAL_TUPLES = i + 1
TOTAL_FILESIZE = TOTAL_TUPLES*TUPLE_SIZE
TOTAL_SUBFILES = (TOTAL_FILESIZE+MEMORY_LIMIT-1)//MEMORY_LIMIT
TUPLES_PER_SUBFILE = MEMORY_LIMIT//TUPLE_SIZE
with open(INPUT_FILE, 'r') as f:
ROW_LEN = len(f.readline())+1
f.close()
print('[DETAILS] Memory Limit = ' + str(MEMORY_LIMIT) + ' B')
print('[DETAILS] Tuple Size = ' + str(TUPLE_SIZE) + ' B')
print('[DETAILS] Total tuples = ' + str(TOTAL_TUPLES))
if(thread_reqd == False):
print('[DETAILS] Total File Size = ' + str(TOTAL_FILESIZE) + ' B')
print('[DETAILS] Sub Files Required = ' + str(TOTAL_SUBFILES))
# if(TOTAL_FILESIZE < MEMORY_LIMIT):
# print('[ERROR] Two Phase Merge Sort is not required as memory limit is more than file size')
# sys.exit(1)
if(TOTAL_SUBFILES * TUPLE_SIZE > MEMORY_LIMIT):
print('[ERROR] Memory Limit is less than what is required to hold the tuples during second phase')
sys.exit(1)
def line_to_tuple(line):
res = []
for i in col_details.values():
res.append(line[:i[1]])
line = line[i[1]+2:]
return res
def create_subfiles():
print('[CREATING] Subfiles')
count = 0
filenum = 0
f = open(str(filenum)+'.txt', 'w')
f.close()
with open(INPUT_FILE) as input:
for line in input:
if(count == 0):
f = open(str(filenum)+'.txt', 'w')
filenum += 1
f.write(line)
count += 1
if(count == TUPLES_PER_SUBFILE):
count = 0
f.close()
if(f.closed == False):
f.close()
input.close()
def sort_subfiles():
for i in range(TOTAL_SUBFILES):
cur = str(i)+'.txt'
table= []
with open(cur, 'r+') as f:
print('[SORTING] Subfile #' + str(i))
for line in f:
row = line_to_tuple(line)
table.append(row)
if(asc == True):
table.sort(key = itemgetter(*col_index_to_sort))
else:
table.sort(key = itemgetter(*col_index_to_sort), reverse=True)
f.truncate(0)
f.seek(0)
print('[WRITING] Subfile #' + str(i))
for row in table:
for cell in range(len(row)):
f.write(row[cell])
if(cell != len(row)-1):
f.write(' ')
else:
f.write(' ')
f.write('\n')
f.close()
def thread_handler(start, end, filenum):
print('[CREATING] Subfiles #' + str(filenum))
count = 0
f = open(str(filenum)+'.txt', 'w')
start_read = False
with open(INPUT_FILE) as input:
for line in input:
if(count == start):
start_read = True
if(count == end):
f.write(line)
f.close()
break
if(start_read):
f.write(line)
count += 1
if(f.closed == False):
f.close()
input.close()
table = []
with open(str(filenum) + '.txt', 'r+') as f:
print('[SORTING] Subfile #' + str(filenum))
for line in f:
row = line_to_tuple(line)
table.append(row)
if(asc == True):
table.sort(key = itemgetter(*col_index_to_sort))
else:
table.sort(key = itemgetter(*col_index_to_sort), reverse=True)
f.truncate(0)
f.seek(0)
print('[WRITING] Subfile #' + str(filenum))
for row in table:
for cell in range(len(row)):
f.write(row[cell])
if(cell != len(row)-1):
f.write(' ')
else:
f.write(' ')
f.write('\n')
f.close()
def threaded_phase1():
global TOTAL_SUBFILES, TUPLES_PER_THREAD, file_counter
PARTITIONS = TOTAL_SUBFILES
TOTAL_SUBFILES = TOTAL_SUBFILES * THREAD_COUNT
TUPLES_PER_THREAD = TUPLES_PER_SUBFILE // THREAD_COUNT
for i in range(PARTITIONS):
threads = []
for j in range(THREAD_COUNT):
start = i*TUPLES_PER_SUBFILE + j*TUPLES_PER_THREAD
if(start > TOTAL_TUPLES):
TOTAL_SUBFILES = file_counter
break
end = i*TUPLES_PER_SUBFILE + (j+1)*TUPLES_PER_THREAD-1
if(j==THREAD_COUNT-1):
end = ((i+1)*TUPLES_PER_SUBFILE)-1
if(end > TOTAL_TUPLES):
TOTAL_SUBFILES = file_counter+1
end = TOTAL_TUPLES
threads.append(threading.Thread(target=thread_handler, args=(start, end, file_counter)))
file_counter += 1
if(TOTAL_SUBFILES * TUPLE_SIZE > MEMORY_LIMIT):
print('[ERROR] Memory Limit is less than what is required to hold the tuples during second phase')
sys.exit(1)
for j in threads:
j.start()
for j in threads:
j.join()
def phase1():
print('[STARTED] Phase 1')
if(thread_reqd == False):
create_subfiles()
sort_subfiles()
else:
threaded_phase1()
print('[COMPLETED] Phase 1 after ' + str(time.time() - start_time) )
def phase2():
print('[STARTED] Phase 2')
filenames = [str(x)+'.txt' for x in range(TOTAL_SUBFILES)]
fp = {filename: open(filename, 'r+') for filename in filenames}
out = open(OUTPUT_FILE, 'w')
heap = []
for i in fp:
line = fp[i].readline()
if(len(line) == 0):
fp[i].close()
del fp[i]
else:
heapq.heappush(heap, heap_object(line_to_tuple(line), i))
files_done = 0
while(files_done < TOTAL_SUBFILES):
obj = heapq.heappop(heap)
row = obj.val
filename = obj.filename
for i in range(len(row)):
out.write(row[i])
if(i != len(row)-1):
out.write(' ')
out.write('\n')
line = fp[filename].readline()
if(len(line) == 0):
files_done += 1
else:
heapq.heappush(heap, heap_object(line_to_tuple(line), filename))
for i in fp:
fp[i].close()
out.close()
print('[COMPLETED] Phase 2')
def del_subfiles():
print('[DELETING] Subfiles')
for i in range(TOTAL_SUBFILES):
os.remove(os.getcwd()+'/'+str(i)+'.txt')
if __name__ == '__main__':
check_args(sys.argv)
read_metadata()
set_details()
phase1()
phase2()
del_subfiles()
print("--- %s seconds ---" % (time.time() - start_time))