-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_index_flow_lancedb.py
More file actions
398 lines (356 loc) · 11.8 KB
/
java_index_flow_lancedb.py
File metadata and controls
398 lines (356 loc) · 11.8 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"""
CocoIndex 1.0 app: index Java, Flyway SQL, and YAML into LanceDB.
LanceDB requires a single primary key per table; each chunk gets a UUID `id`.
Environment:
JAVA_CODEBASE_RAG_INDEX_DIR — Lance tables + Kuzu + cocoindex state (default: ./.java-codebase-rag)
JAVA_CODEBASE_RAG_SOURCE_ROOT — Java repo root for indexing (optional; else cocoindex cwd)
SBERT_MODEL / SBERT_DEVICE — embedding (optional; YAML also supported via java-codebase-rag CLI)
Dependencies:
pip install "cocoindex[lancedb]" sentence-transformers
Usage:
cocoindex update java_index_flow_lancedb.py:JavaCodeIndexLance --full-reprocess
"""
from __future__ import annotations
import inspect
import os
import uuid
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from dataclasses import dataclass
from pathlib import Path
from typing import Annotated, Any
import cocoindex as coco
import numpy as np
import numpy.typing as npt
import pyarrow as pa
from cocoindex.connectors import lancedb, localfs
from cocoindex.connectors.lancedb import LanceType
from cocoindex.ops.sentence_transformers import SentenceTransformerEmbedder
from cocoindex.ops.text import RecursiveSplitter, detect_code_language
from cocoindex.resources.file import PatternFilePathMatcher
from java_codebase_rag.config import resolved_sbert_model_for_process_env
from java_index_v1_common import (
JAVA_CHUNK,
SBERT_MODEL,
SQL_CHUNK,
YAML_CHUNK,
chunk_key_range,
position_to_json,
)
from path_filtering import LayeredIgnore
from ast_java import ONTOLOGY_VERSION, parse_java
from graph_enrich import enrich_chunk
# Older cocoindex (e.g. 1.0.0a43) uses ``tracked=False``; newer releases renamed
# the flag to ``detect_change`` (default False) and reject ``tracked``.
_ck_params = inspect.signature(coco.ContextKey.__init__).parameters
if "detect_change" in _ck_params:
PROJECT_ROOT = coco.ContextKey[Path]("java_lance_project_root")
LANCE_DB = coco.ContextKey("java_lance_async_conn")
EMBEDDER = coco.ContextKey[SentenceTransformerEmbedder]("java_lance_embedder")
elif "tracked" in _ck_params:
PROJECT_ROOT = coco.ContextKey[Path]("java_lance_project_root", tracked=False)
LANCE_DB = coco.ContextKey("java_lance_async_conn", tracked=False)
EMBEDDER = coco.ContextKey[SentenceTransformerEmbedder](
"java_lance_embedder", tracked=False
)
else:
PROJECT_ROOT = coco.ContextKey[Path]("java_lance_project_root")
LANCE_DB = coco.ContextKey("java_lance_async_conn")
EMBEDDER = coco.ContextKey[SentenceTransformerEmbedder]("java_lance_embedder")
splitter = RecursiveSplitter()
@dataclass
class JavaLanceChunk:
id: str
filename: str
language: str
text: str
range_start: int
range_end: int
start: dict[str, Any]
end: dict[str, Any]
embedding: Annotated[npt.NDArray[np.float32], EMBEDDER]
package: str
module: str
microservice: str
primary_type_fqn: str
primary_type_kind: str
role: str
# Native PyArrow lists: without the LanceType override CocoIndex would JSON-encode
# `list[str]` into a STRING column, which caller code then iterates character-by-character.
capabilities: Annotated[list[str], LanceType(pa.list_(pa.string()))]
annotations_on_type: Annotated[list[str], LanceType(pa.list_(pa.string()))]
symbols: Annotated[list[str], LanceType(pa.list_(pa.string()))]
ontology_version: int
@dataclass
class SqlLanceChunk:
id: str
filename: str
text: str
range_start: int
range_end: int
start: dict[str, Any]
end: dict[str, Any]
embedding: Annotated[npt.NDArray[np.float32], EMBEDDER]
@dataclass
class YamlLanceChunk:
id: str
filename: str
text: str
range_start: int
range_end: int
start: dict[str, Any]
end: dict[str, Any]
embedding: Annotated[npt.NDArray[np.float32], EMBEDDER]
@coco.lifespan
async def coco_lifespan(builder: coco.EnvironmentBuilder) -> AsyncIterator[None]:
idx_raw = os.environ.get("JAVA_CODEBASE_RAG_INDEX_DIR", "").strip()
if idx_raw and not idx_raw.startswith(("s3://", "gs://", "az://")):
index_dir = Path(idx_raw).expanduser().resolve()
else:
index_dir = (Path(".").resolve() / ".java-codebase-rag").resolve()
index_dir.mkdir(parents=True, exist_ok=True)
builder.settings.db_path = index_dir / "cocoindex.db"
env_root = os.environ.get("JAVA_CODEBASE_RAG_SOURCE_ROOT", "").strip()
if env_root:
root = Path(env_root).expanduser().resolve()
else:
root = Path(".").resolve()
builder.provide(PROJECT_ROOT, root)
embedder = SentenceTransformerEmbedder(
resolved_sbert_model_for_process_env(SBERT_MODEL),
device=os.environ.get("SBERT_DEVICE") or None,
trust_remote_code=True,
)
builder.provide(EMBEDDER, embedder)
uri = str(index_dir)
@asynccontextmanager
async def _lance_cm() -> AsyncIterator[Any]:
conn = await lancedb.connect_async(uri)
try:
yield conn
finally:
conn.close()
await builder.provide_async_with(LANCE_DB, _lance_cm())
yield
@coco.fn(memo=True)
async def process_java_file(
file: localfs.File,
table: lancedb.TableTarget[JavaLanceChunk],
) -> None:
embedder = coco.use_context(EMBEDDER)
project_root = coco.use_context(PROJECT_ROOT)
if LayeredIgnore(project_root).is_ignored((project_root / file.file_path.path).resolve())[0]:
return
try:
content = await file.read_text()
except UnicodeDecodeError:
return
if not content.strip():
return
language = detect_code_language(filename=file.file_path.path.name) or "text"
cs, mn, ov = JAVA_CHUNK
chunks = splitter.split(
content,
cs,
min_chunk_size=mn,
chunk_overlap=ov,
language=language,
)
rel = file.file_path.path.as_posix()
content_bytes = content.encode("utf-8", errors="replace")
ast = parse_java(content_bytes)
for ch in chunks:
rs, re = chunk_key_range(ch)
enrich = enrich_chunk(
ast,
chunk_start_byte=ch.start.byte_offset,
chunk_end_byte=ch.end.byte_offset,
file_path=rel,
project_root=project_root,
)
emb = await embedder.embed(ch.text)
table.declare_row(
row=JavaLanceChunk(
id=str(uuid.uuid4()),
filename=rel,
language=language,
text=ch.text,
range_start=rs,
range_end=re,
start=position_to_json(ch.start),
end=position_to_json(ch.end),
embedding=emb,
package=enrich.package,
module=enrich.module,
microservice=enrich.microservice,
primary_type_fqn=enrich.primary_type_fqn,
primary_type_kind=enrich.primary_type_kind,
role=enrich.role,
capabilities=list(enrich.capabilities),
annotations_on_type=enrich.annotations_on_type,
symbols=enrich.symbols,
ontology_version=ONTOLOGY_VERSION,
)
)
@coco.fn(memo=True)
async def process_sql_file(
file: localfs.File,
table: lancedb.TableTarget[SqlLanceChunk],
) -> None:
embedder = coco.use_context(EMBEDDER)
project_root = coco.use_context(PROJECT_ROOT)
if LayeredIgnore(project_root).is_ignored((project_root / file.file_path.path).resolve())[0]:
return
try:
content = await file.read_text()
except UnicodeDecodeError:
return
if not content.strip():
return
language = "sql"
cs, mn, ov = SQL_CHUNK
chunks = splitter.split(
content,
cs,
min_chunk_size=mn,
chunk_overlap=ov,
language=language,
)
rel = file.file_path.path.as_posix()
for ch in chunks:
rs, re = chunk_key_range(ch)
emb = await embedder.embed(ch.text)
table.declare_row(
row=SqlLanceChunk(
id=str(uuid.uuid4()),
filename=rel,
text=ch.text,
range_start=rs,
range_end=re,
start=position_to_json(ch.start),
end=position_to_json(ch.end),
embedding=emb,
)
)
@coco.fn(memo=True)
async def process_yaml_file(
file: localfs.File,
table: lancedb.TableTarget[YamlLanceChunk],
) -> None:
embedder = coco.use_context(EMBEDDER)
project_root = coco.use_context(PROJECT_ROOT)
if LayeredIgnore(project_root).is_ignored((project_root / file.file_path.path).resolve())[0]:
return
try:
content = await file.read_text()
except UnicodeDecodeError:
return
if not content.strip():
return
ext = file.file_path.path.suffix.lower()
language = "yaml" if ext in (".yml", ".yaml") else "text"
cs, mn, ov = YAML_CHUNK
chunks = splitter.split(
content,
cs,
min_chunk_size=mn,
chunk_overlap=ov,
language=language,
)
rel = file.file_path.path.as_posix()
for ch in chunks:
rs, re = chunk_key_range(ch)
emb = await embedder.embed(ch.text)
table.declare_row(
row=YamlLanceChunk(
id=str(uuid.uuid4()),
filename=rel,
text=ch.text,
range_start=rs,
range_end=re,
start=position_to_json(ch.start),
end=position_to_json(ch.end),
embedding=emb,
)
)
@coco.fn
async def app_main() -> None:
java_schema = await lancedb.TableSchema.from_class(
JavaLanceChunk,
primary_key=["id"],
)
java_table = await lancedb.mount_table_target(
LANCE_DB,
"javacodeindex_java_code",
java_schema,
)
sql_schema = await lancedb.TableSchema.from_class(
SqlLanceChunk,
primary_key=["id"],
)
sql_table = await lancedb.mount_table_target(
LANCE_DB,
"sqlschemaindex_sql_schema",
sql_schema,
)
yaml_schema = await lancedb.TableSchema.from_class(
YamlLanceChunk,
primary_key=["id"],
)
yaml_table = await lancedb.mount_table_target(
LANCE_DB,
"yamlconfigindex_yaml_config",
yaml_schema,
)
project_root = coco.use_context(PROJECT_ROOT)
_ignore = LayeredIgnore(project_root)
_walk_excludes = _ignore.cocoindex_excluded_patterns()
java_files = localfs.walk_dir(
PROJECT_ROOT,
recursive=True,
path_matcher=PatternFilePathMatcher(
included_patterns=["**/*.java"],
excluded_patterns=_walk_excludes,
),
)
sql_files = localfs.walk_dir(
PROJECT_ROOT,
recursive=True,
path_matcher=PatternFilePathMatcher(
included_patterns=["**/src/main/resources/db/migration/*.sql"],
excluded_patterns=_walk_excludes,
),
)
yaml_files = localfs.walk_dir(
PROJECT_ROOT,
recursive=True,
path_matcher=PatternFilePathMatcher(
included_patterns=[
"**/src/main/resources/application*.yml",
"**/src/main/resources/application*.yaml",
],
excluded_patterns=_walk_excludes,
),
)
await coco.mount_each(
coco.component_subpath(coco.Symbol("java_files")),
process_java_file,
java_files.items(),
java_table,
)
await coco.mount_each(
coco.component_subpath(coco.Symbol("sql_files")),
process_sql_file,
sql_files.items(),
sql_table,
)
await coco.mount_each(
coco.component_subpath(coco.Symbol("yaml_files")),
process_yaml_file,
yaml_files.items(),
yaml_table,
)
app = coco.App(
coco.AppConfig(name="JavaCodeIndexLance"),
app_main,
)