-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjixia_db.py
More file actions
210 lines (190 loc) · 7.96 KB
/
Copy pathjixia_db.py
File metadata and controls
210 lines (190 loc) · 7.96 KB
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
import logging
import os
from collections.abc import Iterable
from pathlib import Path
from jixia import LeanProject
from jixia.structs import LeanName, Symbol, Declaration, is_internal
from psycopg import Connection
from psycopg.types.json import Jsonb
from psycopg.types.range import Range
logger = logging.getLogger(__name__)
def _get_signature(declaration: Declaration, module_content):
if declaration.signature.pp is not None:
return declaration.signature.pp
elif declaration.signature.range is not None:
# A range may not land on a UTF-8 boundary; don't let it abort the load.
return module_content[declaration.signature.range.as_slice()].decode(errors="replace")
else:
return ""
def _get_value(declaration: Declaration, module_content):
if declaration.value is not None and declaration.value.range is not None:
return module_content[declaration.value.range.as_slice()].decode(errors="replace")
else:
return None
def _get_range(declaration: Declaration):
r = declaration.ref.range
if r is not None:
return Range(r.start, r.stop)
else:
return None
def run_analysis(project: LeanProject, prefixes: list[LeanName]) -> list[tuple[Path, list[LeanName]]]:
# Pure CPU analysis, no DB connection involved; this can take 30-90+ min.
lean_sysroot = Path(os.environ["LEAN_SYSROOT"])
lean_src = lean_sysroot / "src" / "lean"
# Each jixia worker loads the full Mathlib environment (~2-3 GB), so the
# default thread count (CPUs + 4) can exhaust memory and get the process
# OOM-killed. Cap it via JIXIA_MAX_WORKERS in memory-constrained CI.
max_workers_env = os.environ.get("JIXIA_MAX_WORKERS")
max_workers = int(max_workers_env) if max_workers_env else None
module_lists = []
for d in project.root, lean_src:
results = project.batch_run_jixia(
base_dir=d,
prefixes=prefixes,
plugins=["module", "declaration", "symbol"],
max_workers=max_workers,
)
module_lists.append((d, [r[0] for r in results]))
return module_lists
def load_data(project: LeanProject, analysis: list[tuple[Path, list[LeanName]]], conn: Connection):
def load_module(data: Iterable[LeanName], base_dir: Path):
values = []
for m in data:
try:
content = project.path_of_module(m, base_dir).read_bytes()
docstring = project.load_module_info(m).docstring
except FileNotFoundError:
logger.warning("skipping module %s: jixia output not found", m)
continue
values.append((Jsonb(m), content, docstring))
cursor.executemany(
"""
INSERT INTO module (name, content, docstring) VALUES (%s, %s, %s) ON CONFLICT DO NOTHING
""",
values,
)
def load_symbol(module: LeanName):
try:
symbols = [s for s in project.load_info(module, Symbol) if not is_internal(s.name)]
except FileNotFoundError:
logger.warning("skipping module %s: jixia output not found", module)
return
values = ((Jsonb(s.name), Jsonb(module), s.kind, s.type_readable if s.type_readable is not None else s.type, s.is_prop) for s in symbols)
cursor.executemany(
"""
INSERT INTO symbol (name, module_name, kind, type, is_prop) VALUES (%s, %s, %s, %s, %s) ON CONFLICT DO NOTHING
""",
values,
)
for s in symbols:
values = (
{
"source": Jsonb(s.name),
"target": Jsonb(t),
}
for t in s.type_references
if not is_internal(t)
)
cursor.executemany(
"""
INSERT INTO dependency (source, target, on_type)
SELECT %(source)s, %(target)s, TRUE
WHERE EXISTS(SELECT 1 FROM symbol WHERE name = %(target)s)
ON CONFLICT DO NOTHING
""",
values,
)
if s.value_references is not None:
values = (
{
"source": Jsonb(s.name),
"target": Jsonb(t),
}
for t in s.value_references
if not is_internal(t)
)
cursor.executemany(
"""
INSERT INTO dependency (source, target, on_type)
SELECT %(source)s, %(target)s, FALSE
WHERE EXISTS(SELECT 1 FROM symbol WHERE name = %(target)s)
ON CONFLICT DO NOTHING
""",
values,
)
def load_declaration(module_name: LeanName):
try:
declarations = project.load_info(module_name, Declaration)
except FileNotFoundError:
logger.warning("skipping module %s: jixia output not found", module_name)
return
cursor.execute(
"""
SELECT content FROM module WHERE name = %s
""",
(Jsonb(module_name),),
)
module = cursor.fetchone()
if module is None:
logger.warn("couldn't find a module with name '%s'", Jsonb(module_name))
return
(module_content,) = module
db_declarations = []
for index, decl in enumerate(declarations):
if is_internal(decl.name) or decl.kind == "proofWanted":
continue
db_declarations.append(
{
"module_name": Jsonb(module_name),
"index": index,
"name": Jsonb(decl.name) if decl.kind != "example" else None,
"visible": decl.modifiers.visibility != "private" and decl.kind != "example",
"docstring": decl.modifiers.docstring,
"kind": decl.kind,
"signature": _get_signature(decl, module_content),
"value": _get_value(decl, module_content),
"range": _get_range(decl),
}
)
cursor.executemany(
"""
INSERT INTO declaration (module_name, index, name, visible, docstring, kind, signature, value)
SELECT %(module_name)s, %(index)s, %(name)s, %(visible)s, %(docstring)s, %(kind)s, %(signature)s, %(value)s
WHERE EXISTS(SELECT 1 FROM symbol WHERE name = %(name)s)
ON CONFLICT DO NOTHING
""",
db_declarations,
)
def topological_sort():
logger.info("performing topological sort")
cursor.execute("""
INSERT INTO level (symbol_name, level)
SELECT name, 0
FROM symbol v
WHERE NOT EXISTS (SELECT 1 FROM dependency e WHERE e.source = v.name)
ON CONFLICT DO NOTHING
""")
while cursor.rowcount:
logger.info("topological sort: %d rows affected", cursor.rowcount)
# Find all nodes whose direct predecessors have already been assigned a level
cursor.execute("""
INSERT INTO level (symbol_name, level)
SELECT e.source AS symbol_name, MAX(l.level) + 1 AS level
FROM
dependency e LEFT JOIN level l ON e.target = l.symbol_name
WHERE NOT EXISTS(SELECT 1 FROM level l WHERE l.symbol_name = e.source)
GROUP BY e.source
HAVING
EVERY(l.level IS NOT NULL) = TRUE
ON CONFLICT DO NOTHING
""")
with conn.cursor() as cursor:
all_modules = []
for d, modules in analysis:
load_module(modules, d)
all_modules += modules
for m in all_modules:
load_symbol(m)
for m in all_modules:
load_declaration(m)
topological_sort()