java-codebase-rag 0.6.3__py3-none-any.whl → 0.6.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ast_java.py CHANGED
@@ -14,8 +14,8 @@ from __future__ import annotations
14
14
 
15
15
  import posixpath
16
16
  import sys
17
+ import threading
17
18
  from dataclasses import dataclass, field
18
- from functools import lru_cache
19
19
  from typing import Iterable
20
20
 
21
21
  import tree_sitter_java as _ts_java
@@ -215,10 +215,23 @@ _ANON_SUPER_AS_INTERFACE: frozenset[str] = frozenset({
215
215
  })
216
216
 
217
217
 
218
- @lru_cache(maxsize=1)
218
+ # tree-sitter's ``Parser`` mutates internal state during ``parse()`` and is NOT
219
+ # thread-safe, so each OS thread gets its own instance. ``parse_java`` is called
220
+ # concurrently from worker threads when indexing runs with cocoindex's inflight
221
+ # parallelism — both directly (java_index_flow_lancedb.py: process_java_file
222
+ # offloads parse+enrich to asyncio.to_thread) and transitively
223
+ # (graph_enrich.collect_annotation_meta_chain -> _collect_annotation_decl_index
224
+ # -> parse_java, reached from enrich_chunk). The ``Language`` is immutable and
225
+ # shared; per-thread ``Parser`` construction is lazy and cheap (once per thread),
226
+ # which also preserves parse parallelism instead of serializing it.
227
+ _parser_tls = threading.local()
228
+
229
+
219
230
  def _parser() -> Parser:
220
- lang = Language(_ts_java.language())
221
- return Parser(lang)
231
+ p = getattr(_parser_tls, "parser", None)
232
+ if p is None:
233
+ _parser_tls.parser = p = Parser(Language(_ts_java.language()))
234
+ return p
222
235
 
223
236
 
224
237
  # ---------- dataclasses ----------
@@ -1565,62 +1578,20 @@ def _parse_codebase_http_route_inner_annotation(
1565
1578
  return out
1566
1579
 
1567
1580
 
1568
- def _codebase_route_inner_annotation_nodes(container_ann: Node, src: bytes) -> list[Node]:
1569
- found: list[Node] = []
1570
-
1571
- def visit(n: Node) -> None:
1572
- if n.type == "annotation":
1573
- name_node = n.child_by_field_name("name")
1574
- n_simple = _txt(name_node, src).rsplit(".", 1)[-1] if name_node is not None else ""
1575
- if n_simple == "CodebaseHttpRoute":
1576
- found.append(n)
1577
- for c in n.children:
1578
- visit(c)
1581
+ def _inner_annotation_nodes(container_ann: Node, src: bytes, target_simple: str) -> list[Node]:
1582
+ """Collect nested ``@<target_simple>`` annotations anywhere under ``container_ann``.
1579
1583
 
1580
- visit(container_ann)
1581
- return found
1582
-
1583
-
1584
- def _codebase_async_route_inner_annotation_nodes(container_ann: Node, src: bytes) -> list[Node]:
1585
- found: list[Node] = []
1586
-
1587
- def visit(n: Node) -> None:
1588
- if n.type == "annotation":
1589
- name_node = n.child_by_field_name("name")
1590
- n_simple = _txt(name_node, src).rsplit(".", 1)[-1] if name_node is not None else ""
1591
- if n_simple == "CodebaseAsyncRoute":
1592
- found.append(n)
1593
- for c in n.children:
1594
- visit(c)
1595
-
1596
- visit(container_ann)
1597
- return found
1598
-
1599
-
1600
- def _codebase_http_client_inner_annotation_nodes(container_ann: Node, src: bytes) -> list[Node]:
1601
- found: list[Node] = []
1602
-
1603
- def visit(n: Node) -> None:
1604
- if n.type == "annotation":
1605
- name_node = n.child_by_field_name("name")
1606
- n_simple = _txt(name_node, src).rsplit(".", 1)[-1] if name_node is not None else ""
1607
- if n_simple == "CodebaseHttpClient":
1608
- found.append(n)
1609
- for c in n.children:
1610
- visit(c)
1611
-
1612
- visit(container_ann)
1613
- return found
1614
-
1615
-
1616
- def _codebase_producer_inner_annotation_nodes(container_ann: Node, src: bytes) -> list[Node]:
1584
+ Shared by the four brownfield container walkers — ``CodebaseHttpRoute``,
1585
+ ``CodebaseAsyncRoute``, ``CodebaseHttpClient``, ``CodebaseProducer`` — which
1586
+ differ only by the target annotation simple name.
1587
+ """
1617
1588
  found: list[Node] = []
1618
1589
 
1619
1590
  def visit(n: Node) -> None:
1620
1591
  if n.type == "annotation":
1621
1592
  name_node = n.child_by_field_name("name")
1622
1593
  n_simple = _txt(name_node, src).rsplit(".", 1)[-1] if name_node is not None else ""
1623
- if n_simple == "CodebaseProducer":
1594
+ if n_simple == target_simple:
1624
1595
  found.append(n)
1625
1596
  for c in n.children:
1626
1597
  visit(c)
@@ -1842,7 +1813,7 @@ def _outgoing_calls_from_codebase_http_client_producer_annotations(
1842
1813
  ),
1843
1814
  )
1844
1815
  elif simple == "CodebaseHttpClients":
1845
- for inner in _codebase_http_client_inner_annotation_nodes(ann, src):
1816
+ for inner in _inner_annotation_nodes(ann, src, "CodebaseHttpClient"):
1846
1817
  out.append(
1847
1818
  _parse_codebase_http_client_annotation(
1848
1819
  inner,
@@ -1869,7 +1840,7 @@ def _outgoing_calls_from_codebase_http_client_producer_annotations(
1869
1840
  ),
1870
1841
  )
1871
1842
  elif simple == "CodebaseProducers":
1872
- for inner in _codebase_producer_inner_annotation_nodes(ann, src):
1843
+ for inner in _inner_annotation_nodes(ann, src, "CodebaseProducer"):
1873
1844
  out.append(
1874
1845
  _parse_codebase_producer_annotation(
1875
1846
  inner,
@@ -2343,7 +2314,7 @@ def _collect_routes(
2343
2314
  ),
2344
2315
  )
2345
2316
  elif simple == "CodebaseHttpRoutes":
2346
- for inner in _codebase_route_inner_annotation_nodes(node, src):
2317
+ for inner in _inner_annotation_nodes(node, src, "CodebaseHttpRoute"):
2347
2318
  routes.extend(
2348
2319
  _parse_codebase_http_route_inner_annotation(
2349
2320
  inner,
@@ -2359,7 +2330,7 @@ def _collect_routes(
2359
2330
  elif simple in ("CodebaseAsyncRoute", "CodebaseAsyncRoutes"):
2360
2331
  nodes = [node]
2361
2332
  if simple == "CodebaseAsyncRoutes":
2362
- nodes = list(_codebase_async_route_inner_annotation_nodes(node, src))
2333
+ nodes = list(_inner_annotation_nodes(node, src, "CodebaseAsyncRoute"))
2363
2334
  for ann in nodes:
2364
2335
  pairs, _ = _annotation_kv_nodes(ann, src)
2365
2336
  topic_node = pairs.get("topic")