-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20171171_2.py
69 lines (58 loc) · 1.88 KB
/
20171171_2.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
import sys
disk = {}
OUT_FILE = '20171171_2.txt'
def get_var_str(d):
temp = ''
for key, value in sorted(d.items()):
temp += key + ' ' + str(value) + ' '
return temp.rstrip()
def recover(logs):
logs.reverse()
end_ckpt_seen = False
start_ckpt_seen = False
fin_trans = []
unfin_trans = []
for log in logs:
log = log.strip('<>')
if 'END CKPT' in log:
end_ckpt_seen = True
elif 'START CKPT' in log:
if end_ckpt_seen:
break
else:
start_ckpt_seen = True
active_trans = [x.strip() for x in log.split('START CKPT')[1].strip().strip('()').split(',')]
for trans_id in active_trans:
if trans_id not in fin_trans:
unfin_trans.append(trans_id)
elif 'START' in log:
if start_ckpt_seen:
trans_id = log.split()[1]
if trans_id in unfin_trans:
unfin_trans.remove(trans_id)
if not unfin_trans:
break
elif 'COMMIT' in log:
trans_id = log.split()[1]
fin_trans.append(trans_id)
else:
trans_id, attr, value = [x.strip() for x in log.split(',')]
if trans_id not in fin_trans:
disk[attr] = value
if __name__ == '__main__':
input_file = sys.argv[1]
logs = []
fill_disk = False
for line in open(input_file).readlines():
if line.isspace():
continue
if not fill_disk:
line = line.split()
for i in range(0, len(line), 2):
disk[line[i]] = int(line[i + 1])
fill_disk = True
continue
logs.append(line.strip())
recover(logs)
with open(OUT_FILE, 'w') as f:
f.writelines(get_var_str(disk) + '\n')