-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
334 lines (223 loc) · 7.69 KB
/
Copy pathmain.py
File metadata and controls
334 lines (223 loc) · 7.69 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
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
import os
import argparse
from tqdm import tqdm
from sentence_transformers import SentenceTransformer
from parser import extract_classes_and_methods
from qdrant_utils import (
init_collections,
batch_insert_fragments,
is_file_unchanged,
update_file_hash,
calculate_file_hash,
)
from neo4j_utils import (
insert_method_call,
check_neo4j_connection,
count_methods_and_calls,
)
# -----------------------------------
# CONFIG
# -----------------------------------
REPO_FOLDER = os.getenv("REPO_FOLDER", "/app/repo_to_index")
MODEL_NAME = os.getenv(
"EMBEDDING_MODEL",
"all-MiniLM-L6-v2"
)
BATCH_SIZE = int(os.getenv("EMBEDDING_BATCH_SIZE", "32"))
EMBEDDING_INSERT_CHUNK_SIZE = int(
os.getenv("EMBEDDING_INSERT_CHUNK_SIZE", "512")
)
MIN_CODE_CHARS = int(os.getenv("MIN_CODE_CHARS", "80"))
EMBED_METHODS_ONLY = (
os.getenv("EMBED_METHODS_ONLY", "true").lower()
not in {"0", "false", "no"}
)
# -----------------------------------
# LOAD MODEL
# -----------------------------------
print(f"Loading embedding model: {MODEL_NAME}")
model = SentenceTransformer(MODEL_NAME)
# -----------------------------------
# HELPERS
# -----------------------------------
def should_embed_fragment(fragment: dict) -> bool:
"""
Decide whether a fragment should be embedded.
"""
if EMBED_METHODS_ONLY and fragment.get("type") != "method":
return False
code = fragment.get("code", "")
if not code:
return False
if len(code.strip()) < MIN_CODE_CHARS:
return False
return True
def batch_insert_embeddings(fragments: list[dict]):
"""
Generate embeddings in batches and insert into Qdrant.
"""
if not fragments:
return
inserted = 0
for start in range(0, len(fragments), EMBEDDING_INSERT_CHUNK_SIZE):
chunk = fragments[start:start + EMBEDDING_INSERT_CHUNK_SIZE]
codes = [f["code"] for f in chunk]
embeddings = model.encode(
codes,
batch_size=BATCH_SIZE,
show_progress_bar=False,
)
for frag, emb in zip(chunk, embeddings):
try:
frag["embedding"] = emb.tolist()
except Exception as e:
print(f"Error processing fragment {frag.get('symbol')}: {e}")
batch_insert_fragments(chunk)
inserted += len(chunk)
print(f"Inserted embeddings into Qdrant: {inserted}/{len(fragments)}")
# -----------------------------------
# MAIN
# -----------------------------------
def main(full_rescan: bool = False):
# Initialize Qdrant collections
init_collections()
print(f"Indexing starting. REPO_FOLDER={REPO_FOLDER}")
print(
f"Mode: {'FULL RESCAN' if full_rescan else 'INCREMENTAL (MD5 cache)'}"
)
total_files = 0
skipped_files = 0
processed_files = 0
fragments_to_embed = []
# deduplicate graph edges
all_calls = set()
# -----------------------------------
# WALK REPOSITORY
# -----------------------------------
for root, _, files in os.walk(REPO_FOLDER):
for file in files:
if not file.lower().endswith(".java"):
continue
path = os.path.join(root, file)
total_files += 1
# -----------------------------------
# SKIP UNCHANGED FILES
# -----------------------------------
if not full_rescan and is_file_unchanged(path):
skipped_files += 1
print(f"Skipping unchanged file: {path}")
continue
print(f"Processing: {path}")
# -----------------------------------
# PARSE FILE
# -----------------------------------
try:
fragments = extract_classes_and_methods(path)
except Exception as e:
print(f"Failed parsing {path}: {e}")
continue
processed_files += 1
# -----------------------------------
# UPDATE FILE HASH
# -----------------------------------
try:
file_hash = calculate_file_hash(path)
update_file_hash(path, file_hash)
except Exception as e:
print(f"Warning: failed updating hash for {path}: {e}")
# -----------------------------------
# PROCESS FRAGMENTS
# -----------------------------------
for frag in fragments:
# collect graph relationships
if frag.get("type") == "method":
caller = frag.get("symbol")
for callee in frag.get("calls", []):
all_calls.add((caller, callee))
# collect embeddable fragments
if should_embed_fragment(frag):
fragments_to_embed.append(frag)
# -----------------------------------
# EMBEDDINGS
# -----------------------------------
print(
f"\nGenerating embeddings for "
f"{len(fragments_to_embed)} fragments..."
)
batch_insert_embeddings(fragments_to_embed)
# -----------------------------------
# NEO4J
# -----------------------------------
neo4j_enabled = (
os.getenv("NEO4J_ENABLED", "true").lower()
not in {"0", "false", "no"}
)
if not neo4j_enabled:
print("Skipping Neo4j insertion (disabled).")
else:
if not check_neo4j_connection():
print(
"Skipping Neo4j insertion "
"(connection unavailable)."
)
else:
print(
f"\nInserting {len(all_calls)} "
f"method-call relationships into Neo4j..."
)
errors = 0
for caller, callee in tqdm(
all_calls,
desc="Neo4j method calls"
):
try:
insert_method_call(caller, callee)
except Exception as e:
errors += 1
print(
f"Error inserting relationship "
f"{caller} -> {callee}: {e}"
)
if errors >= 5:
print(
"Too many Neo4j errors. "
"Stopping insertion."
)
break
# -----------------------------------
# SUMMARY
# -----------------------------------
print("\n=== INDEXING SUMMARY ===")
print(f"Files discovered (.java): {total_files}")
if total_files == 0:
print(
"Hint: Put Java files under REPO_FOLDER "
"or update the path."
)
if not full_rescan:
print(f"Skipped unchanged: {skipped_files}")
print(f"Processed files: {processed_files}")
print(f"Embedded fragments: {len(fragments_to_embed)}")
print(f"Unique graph edges: {len(all_calls)}")
# -----------------------------------
# GRAPH SUMMARY
# -----------------------------------
if neo4j_enabled and check_neo4j_connection():
n, r = count_methods_and_calls()
if n is not None:
print(f"Neo4j method nodes: {n}")
print(f"Neo4j CALLS edges: {r}")
# -----------------------------------
# ENTRYPOINT
# -----------------------------------
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Code Genius Indexer"
)
parser.add_argument(
"--full-rescan",
action="store_true",
help="Re-index all files regardless of MD5 cache",
)
args = parser.parse_args()
main(full_rescan=args.full_rescan)