-
Notifications
You must be signed in to change notification settings - Fork 0
/
_sizes.py
executable file
·163 lines (134 loc) · 5.02 KB
/
_sizes.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
#!/usr/bin/python3
import sqlite3
import sys
import os
sql_schema="""SELECT * FROM sqlite_schema;"""
sql_tables_schema="""SELECT * FROM sqlite_schema WHERE type='table';"""
sql_indexes_schema="""SELECT * FROM sqlite_schema WHERE type='index';"""
sql_sizes="""SELECT name, SUM(pgsize) as size
FROM sqlite_schema AS s
LEFT JOIN dbstat AS c USING (name)
GROUP BY name;"""
sql_total_size="""SELECT SUM(size) FROM (
SELECT name, SUM(pgsize) as size
FROM sqlite_schema AS s
LEFT JOIN dbstat AS c USING (name)
GROUP BY name);"""
sql_total_table_size="""SELECT SUM(size) FROM (
SELECT name, SUM(pgsize) as size
FROM sqlite_schema AS s
LEFT JOIN dbstat AS c USING (name)
WHERE type='table'
GROUP BY name);"""
sql_total_nonfts_table_size="""SELECT SUM(size) FROM (
SELECT name, SUM(pgsize) as size
FROM sqlite_schema AS s
LEFT JOIN dbstat AS c USING (name)
WHERE type='table' AND name NOT LIKE '%fts4%'
GROUP BY name);"""
sql_total_fts_table_size="""SELECT SUM(size) FROM (
SELECT name, SUM(pgsize) as size
FROM sqlite_schema AS s
LEFT JOIN dbstat AS c USING (name)
WHERE type='table' AND name LIKE '%fts4%'
GROUP BY name);"""
sql_total_fts_extra_table_size="""SELECT SUM(size) FROM (
SELECT name, SUM(pgsize) as size
FROM sqlite_schema AS s
LEFT JOIN dbstat AS c USING (name)
WHERE type='table' AND name LIKE '%%fts4%%' AND %s
GROUP BY name);"""
sql_total_index_size="""SELECT SUM(size) FROM (
SELECT name, SUM(pgsize) as size
FROM sqlite_schema AS s
LEFT JOIN dbstat AS c USING (name)
WHERE type='index'
GROUP BY name);"""
sql_total_nonfts_index_size="""SELECT SUM(size) FROM (
SELECT name, SUM(pgsize) as size
FROM sqlite_schema AS s
LEFT JOIN dbstat AS c USING (name)
WHERE type='index'
GROUP BY name);"""
def hr(x):
if x == None:
return "NA"
f="%.0f %s"
for unit in ['B','KB','MB','GB']:
if x > -1024.0 and x < 1024.0:
return f % (round(x), unit)
x /= 1024.0
return f % (round(x), 'TB')
def out(rows):
for row in rows:
print("%-48s %10s %10s" % (row[0], row[1], hr(row[1])), file=sys. stderr)
print("\n", file=sys. stderr)
def out1(rows):
for row in rows:
print("%-48s %10s %10s" % ('',row[0], hr(row[0])), file=sys. stderr)
print("\n", file=sys. stderr)
collectedRaw=[]
collectedHR=[]
def outRes(rows,rawf,hrf):
for row in rows:
collectedRaw.append(rawf % row[0])
collectedHR.append(hrf % hr(row[0]))
def filesizeRes(f,rawf,hrf):
b = os.path.getsize(f)
collectedRaw.append(rawf % (b,os.path.basename(f)))
collectedHR.append(rawf % (hr(b),os.path.basename(f)))
def query(sql, consume):
cursor = connection.cursor()
cursor.execute(sql)
rows = cursor.fetchall()
consume(rows)
cursor.close()
where=sys.argv[1]
db=where+sys.argv[2]
dbf=where+sys.argv[3]
dbfz=where+sys.argv[4]
extra=sys.argv[5:]
connection = sqlite3.connect(db)
#print("table sizes", file=sys. stderr)
#print("-----------", file=sys. stderr)
#query(sql_sizes, out)
#print("total size")
query(sql_total_size, lambda r: outRes(r,
" <integer name='size_db_working_total' tools:keep='@string/size_db_working_total'>%s</integer>",
" <string name='hr_size_db_working_total' tools:keep='@string/hr_size_db_working_total'>%s</string>"))
#print("total table size")
#query(sql_total_table_size, lambda r: outRes(r,
# " <integer name='size_tables' tools:keep='@string/size_tables'>%s</integer>",
# " <string name='hr_size_tables' tools:keep='@string/hr_size_tables'>%s</string>"))
#print("total_core table size")
#query(sql_total_nonfts_table_size, lambda r: outRes(r,
# " <integer name='size_core' tools:keep='@string/size_core'>%s</integer>",
# " <string name='hr_size_core' tools:keep='@string/hr_size_core'>%s</string>"))
#print("total indexes size")
#query(sql_total_index_size, lambda r: outRes(r,
# " <integer name='size_indexes' tools:keep='@string/size_indexes'>%s</integer>",
# " <string name='hr_size_indexes' tools:keep='@string/hr_size_indexes'>%s</string>"))
#print("total fts table size")
query(sql_total_fts_table_size, lambda r: outRes(r,
" <integer name='size_searchtext' tools:keep='@string/size_searchtext'>%s</integer>",
" <string name='hr_size_searchtext' tools:keep='@string/hr_size_searchtext'>%s</string>"))
for ts in extra:
extra = "name LIKE '%%%s%%'" % ts
sql = sql_total_fts_extra_table_size % extra
#print(extra)
#print(sql)
query(sql_total_fts_extra_table_size % extra, lambda r: outRes(r,
" <integer name='size_searchtext_%s' tools:keep='@string/size_searchtext_%s'>%%s</integer>" % (ts,ts),
" <string name='hr_size_searchtext_%s' tools:keep='@string/hr_size_searchtext_%s'>%%s</string>" % (ts,ts),))
filesizeRes(dbf,
" <integer name='size_sqlunet_db' tools:keep='@string/size_sqlunet_db'>%s</integer> <!-- %s -->",
" <string name='hr_size_sqlunet_db' tools:keep='@string/hr_size_sqlunet_db'>%s</string> <!-- %s -->")
filesizeRes(dbfz,
" <integer name='size_sqlunet_db_zip' tools:keep='@string/size_sqlunet_db_zip'>%s</integer> <!-- %s -->",
" <string name='hr_size_sqlunet_db_zip' tools:keep='@string/hr_size_sqlunet_db_zip'>%s</string> <!-- %s -->")
if __name__ == "__main__":
for s in collectedRaw:
print(s)
#print()
#for s in collectedHR:
# print(s)