-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_blocks.py
75 lines (58 loc) · 2.1 KB
/
build_blocks.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
import create_tables
import sqlite3
import math
from pathlib import Path
block_size=70
create_index_sql = """CREATE INDEX
idx_simulations_fk_blocks_id
ON simulations (fk_blocks_id);"""
find_max_simulation_id_sql = """SELECT max(id) FROM simulations;"""
update_simulations_fk_block_id_sql = """update simulations set fk_blocks_id=? where id>=? and id<?;"""
create_new_block_sql = """INSERT into blocks (id,name) Values (?,?);"""
drop_index_sql ="""DROP INDEX IF EXISTS idx_simulations_fk_blocks_id ;"""
def find_highest_id(conn):
cur = conn.cursor()
cur.execute(find_max_simulation_id_sql)
rows = cur.fetchall()
row = rows[0][0]
return row
def drop_index(conn):
cur = conn.cursor()
cur.execute(drop_index_sql)
conn.commit()
def update_simulation_rows(conn,data):
"""
Query tasks by priority
:param conn: the Connection object
:param priority:
:return:
"""
cur = conn.cursor()
cur.execute(update_simulations_fk_block_id_sql,data)
conn.commit()
def create_block(conn, id):
cur = conn.cursor()
cur.execute(create_new_block_sql,(id, str(f'block_{id}')))
conn.commit()
def create_index_on_fk_blocks_id(conn):
cur = conn.cursor()
cur.execute(create_index_sql)
conn.commit()
if __name__=="__main__":
conn = create_tables.create_connection(create_tables.db_file)
number_of_rows = find_highest_id(conn)
#block_size=10
#Delete index on start
drop_index(conn)
mapping_between_block_and_simulation = []
block_max= int(math.ceil(number_of_rows/block_size))
current_simulation=0
for block_id in range(1,block_max+1):
end_simulation=current_simulation+block_size
mapping_between_block_and_simulation.append((block_id,current_simulation,end_simulation))
current_simulation = end_simulation
for mapping in mapping_between_block_and_simulation:
block_id=mapping[0]
create_block(conn,block_id)
update_simulation_rows(conn,mapping)
create_index_on_fk_blocks_id(conn)