-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
529 lines (497 loc) · 21.9 KB
/
main.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import os
import time
import math
import sys
class MergeJoin:
def __init__(self, m, left_relation, right_relation, tuples):
self.m = m # main memory buffers
self.tuples = tuples # number of tuples in one block
self.left_tuple_size = 0 # size of each tuple in bytes for left relation
self.right_tuple_size = 0 # size of each tuple in bytes for right relation
self.left_relation = left_relation # name of left relation
self.right_relation = right_relation # name of right relation
self.left_sublist = 0
self.right_sublist = 0
self.left_memory = [] # stores the left sorted sublist first blocks
self.right_memory = [] # stores the right sorted sublist first blocks
self.buffer = [] # one extra buffer for output purpose, list of strings
self.left_sublist_offsets = []
self.right_sublist_offsets = []
self.left_file_list = []
self.right_file_list = []
self.left_sublist_read = [] # how much data we have actually read from each left sublist
self.right_sublist_read = [] # how much data we have actually read from each right sublist
self.get_next_index = 0
self.output_file = self.give_output_file_name()
self.read_file = open(self.output_file, 'r')
self.FOUND = True
self.find_tuple_size()
def give_output_file_name(self):
left = os.path.basename(self.left_relation)
right = os.path.basename(self.right_relation)
return left + '_' + right + '_join.txt'
def find_tuple_size(self):
x = open(self.left_relation, 'r')
self.left_tuple_size = len(x.readline())
x.close()
x = open(self.right_relation, 'r')
self.right_tuple_size = len(x.readline())
x.close()
def write_out(self):
out_file = open(self.output_file, 'a')
for line in self.buffer:
out_file.write(line)
self.buffer = []
def open(self):
left_file_size = os.stat(self.left_relation).st_size
right_file_size = os.stat(self.right_relation).st_size
block_left = (left_file_size + self.tuples * self.left_tuple_size - 1) // (self.tuples * self.left_tuple_size)
block_right = (right_file_size + self.tuples * self.right_tuple_size - 1) // (self.tuples * self.right_tuple_size)
if block_left + block_right < (self.m * self.m):
self.phase_one()
else:
raise NotImplementedError('Not possible as B(R) + B(S) >= M^2')
def get_next(self):
if self.get_next_index == self.tuples:
arr = self.read_file.readlines(self.tuples * self.left_tuple_size - 1) # we can choose either left or right
if len(arr) == 0:
print('All data has been read')
self.FOUND = False
else:
self.buffer = arr
self.get_next_index = 1
return self.buffer[self.get_next_index - 1]
else:
self.get_next_index += 1
return self.buffer[self.get_next_index - 1]
def close(self):
self.read_file.close()
for i in range(self.left_sublist):
self.left_file_list[i].close()
for i in range(self.right_sublist):
self.right_file_list[i].close()
def phase_one(self):
left_file_size = os.stat(self.left_relation).st_size
read_till = self.m * self.tuples * self.left_tuple_size
files_to_be_created = math.ceil(left_file_size / read_till)
self.left_sublist = files_to_be_created
print(files_to_be_created)
self.left_sublist_offsets = [0] * files_to_be_created # initially all the sublist start from first block
left = open(self.left_relation, 'r')
for i in range(files_to_be_created):
arr = left.readlines(read_till-1) # need to subtract one otherwise one more line will be read
dump_file = open(self.left_relation + str(i), 'w')
brr = []
for line in arr:
temp = line[:-1]
x, y = temp.split(' ')
brr.append((x, y))
brr.sort(key=lambda k: k[1])
for line in brr:
x, y = line
to_write = x + ' ' + y
dump_file.write(to_write + '\n')
dump_file.close()
left.close()
read_till = self.m * self.tuples * self.right_tuple_size
right_file_size = os.stat(self.right_relation).st_size
files_to_be_created = math.ceil(right_file_size / read_till)
self.right_sublist_offsets = [0] * files_to_be_created # initially all the sublist start from first block
self.right_sublist_read = [0] * files_to_be_created
self.right_sublist = files_to_be_created
right = open(self.right_relation, 'r')
for i in range(files_to_be_created):
arr = right.readlines(read_till-1)
dump_file = open(self.right_relation + str(i), 'w')
brr = []
for line in arr:
temp = line[:-1]
x, y = temp.split(' ')
brr.append((x, y))
brr.sort(key=lambda k: k[0])
for line in brr:
x, y = line
to_write = x + ' ' + y
dump_file.write(to_write + '\n')
dump_file.close()
right.close()
def initialise_list(self):
# read one block from each of the sorted file
to_read = self.tuples * self.left_tuple_size
for i in range(self.left_sublist):
file = open(self.left_relation + str(i), 'r')
arr = file.readlines(to_read - 1)
self.left_memory.append(arr)
self.left_file_list.append(file)
to_read = self.tuples * self.right_tuple_size
for i in range(self.right_sublist):
file = open(self.right_relation + str(i), 'r')
arr = file.readlines(to_read - 1)
self.right_memory.append(arr)
self.right_file_list.append(file)
def join(self):
# find the minimum from the above
self.initialise_list()
left_not_processed = [1] * self.left_sublist
right_not_processed = [1] * self.right_sublist
while any(left_not_processed) and any(right_not_processed):
xx = ""
left_min = "~" # since it is the largest character
for i in range(self.left_sublist):
if left_not_processed[i] == 0:
continue # continue if the sublist has been completely read
ind = self.left_sublist_offsets[i]
if ind == len(self.left_memory[i]):
# re fill the block
to_read = self.tuples * self.left_tuple_size
arr = self.left_file_list[i].readlines(to_read - 1)
if len(arr) == 0:
left_not_processed[i] = 0
continue
ind = 0
self.left_sublist_offsets[i] = ind
self.left_memory[i] = arr
temp = self.left_memory[i][ind]
temp = temp[:-1]
x, y = temp.split(' ')
if y < left_min:
left_min = y
xx = x
right_min = "~"
for i in range(self.right_sublist):
if right_not_processed[i] == 0:
continue
ind = self.right_sublist_offsets[i]
if ind == len(self.right_memory[i]):
# re fill the block
to_read = self.tuples * self.right_tuple_size
arr = self.right_file_list[i].readlines(to_read - 1)
if len(arr) == 0:
right_not_processed[i] = 0
continue
ind = 0
self.right_sublist_offsets[i] = ind
self.right_memory[i] = arr
temp = self.right_memory[i][ind]
temp = temp[:-1]
y, z = temp.split(' ')
if y < right_min:
right_min = y
if left_min < right_min:
for i in range(self.left_sublist):
ind = self.left_sublist_offsets[i]
# remove all the tuples with Y = left_min
while ind < len(self.left_memory[i]):
temp = self.left_memory[i][ind]
temp = temp[:-1]
x, y = temp.split(' ')
if y == left_min:
self.left_sublist_offsets[i] += 1
ind += 1
else:
break
elif right_min < left_min:
for i in range(self.right_sublist):
ind = self.right_sublist_offsets[i]
# remove all the tuple with Y = right_min
while ind < len(self.right_memory[i]):
temp = self.right_memory[i][ind]
temp = temp[:-1]
y, z = temp.split(' ')
if y == right_min:
self.right_sublist_offsets[i] += 1
self.right_sublist_read[i] += self.right_tuple_size
ind += 1
else:
break
else:
# the left min will join with all the right ones
# just remove only one tuple with Y = left_min
self.join_right(xx, left_min)
for i in range(self.left_sublist):
ind = self.left_sublist_offsets[i]
if ind == len(self.left_memory[i]):
to_read = self.tuples * self.left_tuple_size
arr = self.left_file_list[i].readlines(to_read - 1)
self.left_sublist_offsets[i] = 0
ind = 0
if len(arr) == 0:
left_not_processed[i] = 0
else:
self.left_memory[i] = arr
temp = self.left_memory[i][ind]
temp = temp[:-1]
x, y = temp.split(' ')
if y == left_min:
ind += 1
self.left_sublist_offsets[i] += 1
if ind == len(self.left_memory[i]):
self.left_sublist_offsets[i] = 0
to_read = self.tuples * self.left_tuple_size
arr = self.left_file_list[i].readlines(to_read - 1)
if len(arr) == 0:
left_not_processed[i] = 0
else:
self.left_memory[i] = arr
break
# collect all the tuples with Y = the above found minimum
def join_right(self, xx, left_y):
"""
join x, y with the right table
:return: nothing
"""
files = []
for i in range(self.right_sublist):
a = open(self.right_relation + str(i), 'r')
a.seek(self.right_sublist_read[i])
files.append(a)
# needs to maintain the pointers for right file as well
for i in range(self.right_sublist):
ind = self.right_sublist_offsets[i]
temp_mem = self.right_memory[i]
if ind == len(temp_mem):
to_read = self.tuples * self.right_tuple_size
arr = files[i].readlines(to_read - 1)
if len(arr) == 0:
continue
else:
temp_mem = arr
ind = 0
temp = temp_mem[ind]
y, z = temp.split(' ')
while y == left_y:
to_write = xx + ' ' + y + ' ' + z
self.buffer.append(to_write)
if len(self.buffer) == self.tuples:
self.write_out()
ind += 1
if ind == len(temp_mem):
# need to re fill the block
to_read = self.tuples * self.right_tuple_size
arr = files[i].readlines(to_read - 1)
if len(arr) == 0:
break
else:
temp_mem = arr
ind = 0
temp = temp_mem[ind]
y, z = temp.split(' ')
self.write_out() # write whatever is left
class HashJoin:
def __init__(self, m, left_relation, right_relation, tuples):
self.m = m # main memory buffers
self.tuples = tuples # number of tuples in one block
self.left_tuple_size = 0 # size of each tuple in bytes for left relation
self.right_tuple_size = 0 # size of each tuple in bytes for right relation
self.left_relation = left_relation # name of left relation
self.right_relation = right_relation # name of right relation
self.get_next_index = 0
self.output_file = self.give_output_file_name()
self.read_file = open(self.output_file, 'r')
self.buffer = [] # one extra buffer for output purpose, list of strings
self.FOUND = True
self.find_tuple_size()
def find_tuple_size(self):
x = open(self.left_relation, 'r')
self.left_tuple_size = len(x.readline())
x.close()
x = open(self.right_relation, 'r')
self.right_tuple_size = len(x.readline())
x.close()
def give_output_file_name(self):
left = os.path.basename(self.left_relation)
right = os.path.basename(self.right_relation)
return left + '_' + right + '_join.txt'
def write_out(self):
out_file = open(self.output_file, 'a')
for line in self.buffer:
out_file.write(line)
self.buffer = []
def get_next(self):
if self.get_next_index == self.tuples:
arr = self.read_file.readlines(self.tuples * self.left_tuple_size - 1) # we can choose either left or right
if len(arr) == 0:
print('All data has been read')
self.FOUND = False
else:
self.buffer = arr
self.get_next_index = 1
return self.buffer[self.get_next_index - 1]
else:
self.get_next_index += 1
return self.buffer[self.get_next_index - 1]
def close(self):
self.read_file.close()
def open(self):
file = open(self.left_relation, 'r')
hashed_list = dict()
while True:
to_read = self.tuples * self.left_tuple_size
arr = file.readlines(to_read-1)
if len(arr) == 0:
break
for line in arr:
temp = line[:-1]
x, y = temp.split(' ')
num = self.give_hash(y)
# for each line put it into the corresponding bucket
if num in hashed_list:
hashed_list[num].append(line)
else:
hashed_list[num] = [line]
# if the main memory is filled, empty it and reuse it.
if len(hashed_list[num]) == self.tuples:
append_file = open(self.left_relation + str(num), 'a')
for data in hashed_list[num]:
append_file.write(data)
hashed_list[num] = []
append_file.close() # close the file
for i in range(self.m):
if i not in hashed_list: # if there was nothing hashed in this bucket
continue
append_file = open(self.left_relation + str(i), 'a')
for data in hashed_list[i]:
append_file.write(data)
hashed_list[i] = []
append_file.close()
file.close()
file = open(self.right_relation, 'r')
while True:
to_read = self.tuples * self.right_tuple_size
arr = file.readlines(to_read - 1)
if len(arr) == 0:
break
for line in arr:
y, z = line.split(' ')
num = self.give_hash(y)
if num in hashed_list:
hashed_list[num].append(line)
else:
hashed_list[num] = [line]
if len(hashed_list[num]) == self.tuples:
append_file = open(self.right_relation + str(num), 'a')
for data in hashed_list[num]:
append_file.write(data)
hashed_list[num] = []
append_file.close() # close the file
for i in range(self.m):
if i not in hashed_list:
continue
append_file = open(self.right_relation + str(i), 'a')
for data in hashed_list[i]:
append_file.write(data)
hashed_list[i] = []
append_file.close()
file.close()
def join(self):
# we can assume that the R_i or S_i will fill in the main memory (This is the assumption)
# read page 294
# since we have hashed into self.m buckets, we need to iterate over each of them
for i in range(self.m):
# get the sizes of R_i and S_i and load the smaller one into the memory
if not os.path.isfile(self.left_relation + str(i)):
continue
if not os.path.isfile(self.right_relation + str(i)):
continue
left_file_size = os.stat(self.left_relation + str(i)).st_size
right_file_size = os.stat(self.right_relation + str(i)).st_size
if left_file_size < right_file_size:
left_f = open(self.left_relation + str(i), 'r')
lines = left_f.readlines()
# create a search structure over the common column i.e Y
# a dict with key = Y and a list of all the X values
search_structure = dict()
for line in lines:
temp = line[:-1]
x, y = temp.split(' ')
if y in search_structure:
search_structure[y].append(x)
else:
search_structure[y] = [x] # initialise a list if not present
# read each block of right relation and for each tuple find all tuples which can join
right_f = open(self.right_relation + str(i), 'r')
while True:
right_buffer = right_f.readlines(self.tuples * self.right_tuple_size - 1)
if len(right_buffer) > 0:
for line in right_buffer:
# use the search_structure
y, z = line.split(' ')
if y in search_structure:
for x in search_structure[y]:
to_write = x + ' ' + y + ' ' + z
self.buffer.append(to_write)
if len(self.buffer) > self.tuples:
self.write_out()
else:
pass
self.write_out() # write the remaining data in buffer to the disk
else:
break
else:
right_f = open(self.right_relation + str(i), 'r')
lines = right_f.readlines()
search_structure = dict()
for line in lines:
y, z = line.split(' ')
if y in search_structure:
search_structure[y].append(z)
else:
search_structure[y] = [z]
left_f = open(self.left_relation + str(i), 'r')
while True:
left_buffer = left_f.readlines(self.tuples * self.left_tuple_size - 1)
if len(left_buffer) > 0:
for line in left_buffer:
# use search_structure
temp = line[:-1]
x, y = temp.split(' ')
if y in search_structure:
for z in search_structure[y]:
to_write = x + ' ' + y + ' ' + z
self.buffer.append(to_write)
if len(self.buffer) > self.tuples:
self.write_out()
else:
pass
self.write_out() # write the remaining data in buffer to the disk
else:
break
def give_hash(self, y):
# returns an integer
power_p = 1
p = 31
mod = self.m
hash_val = 0
for char in y:
hash_val = (hash_val + (ord(char) - 96)*power_p) % mod
power_p = (power_p * p) % mod
return hash_val
if __name__ == '__main__':
left_file = sys.argv[1]
right_file = sys.argv[2]
which = sys.argv[3]
memory_buffers = int(sys.argv[4])
left = os.path.basename(left_file)
right = os.path.basename(right_file)
out_file = left + '_' + right + '_join.txt'
if os.path.isfile(out_file):
file = open(out_file, 'w')
file.truncate()
else:
file = open(out_file, 'w+')
file.close()
# to get the tuple size we need to read one line from the file
st_time = time.time()
if which == 'sort':
merge_join = MergeJoin(memory_buffers, left_file, right_file, 100)
merge_join.open()
merge_join.join()
merge_join.close()
else:
hash_join = HashJoin(memory_buffers, left_file, right_file, 100)
hash_join.open()
hash_join.join()
hash_join.close()
end_time = time.time()
time_spent = end_time - st_time
print(time_spent)