-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathdb.py
More file actions
157 lines (123 loc) · 5.23 KB
/
Copy pathdb.py
File metadata and controls
157 lines (123 loc) · 5.23 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
"""Database backend selection for CodeGraph.
The default backend is a regular FalkorDB server addressed by host/port.
Set ``CODE_GRAPH_DB_BACKEND=lite`` to use FalkorDBLite's embedded server.
"""
from __future__ import annotations
import os
from pathlib import Path
from typing import Any
LITE_BACKENDS = {"lite", "falkordblite"}
DEFAULT_LITE_PATH = "~/.cache/code-graph/falkordblite.rdb"
def is_lite_backend() -> bool:
"""Return whether CodeGraph should use the embedded FalkorDBLite backend."""
backend = os.getenv("CODE_GRAPH_DB_BACKEND", "falkordb").strip().lower()
return backend in LITE_BACKENDS
def _lite_db_path() -> str:
path = Path(os.getenv("FALKORDB_LITE_PATH", DEFAULT_LITE_PATH)).expanduser()
path.parent.mkdir(parents=True, exist_ok=True)
return str(path)
def _lite_serverconfig() -> dict[str, str]:
"""Return FalkorDBLite server config from env.
FalkorDBLite defaults to a private Unix socket. Supplying
``FALKORDB_LITE_PORT`` additionally exposes a local TCP port, which is
useful for libraries that only accept host/port connection settings.
"""
port = os.getenv("FALKORDB_LITE_PORT")
if not port:
return {}
return {
"bind": os.getenv("FALKORDB_LITE_HOST", "127.0.0.1"),
"port": port,
}
def create_falkordb() -> Any:
"""Create a sync FalkorDB client for the configured backend."""
if is_lite_backend():
try:
from redislite.falkordb_client import FalkorDB as LiteFalkorDB
except ImportError as e:
raise RuntimeError(
"CODE_GRAPH_DB_BACKEND=lite requires the optional "
"`falkordblite` dependency. Install with "
"`uv sync --extra light` or `pip install 'falkordb-code-graph[light]'`."
) from e
return LiteFalkorDB(_lite_db_path(), serverconfig=_lite_serverconfig())
from falkordb import FalkorDB
return FalkorDB(
host=os.getenv("FALKORDB_HOST", "localhost"),
port=os.getenv("FALKORDB_PORT", 6379),
username=os.getenv("FALKORDB_USERNAME", None),
password=os.getenv("FALKORDB_PASSWORD", None),
)
def create_async_falkordb() -> Any:
"""Create an async FalkorDB client for the configured backend."""
if is_lite_backend():
try:
from redislite.async_falkordb_client import AsyncFalkorDB as LiteAsyncFalkorDB
except ImportError as e:
raise RuntimeError(
"CODE_GRAPH_DB_BACKEND=lite requires the optional "
"`falkordblite` dependency. Install with "
"`uv sync --extra light` or `pip install 'falkordb-code-graph[light]'`."
) from e
client = LiteAsyncFalkorDB(_lite_db_path(), serverconfig=_lite_serverconfig())
if not hasattr(client, "aclose"):
client.aclose = client.close
return client
from falkordb.asyncio import FalkorDB as AsyncFalkorDB
return AsyncFalkorDB(
host=os.getenv("FALKORDB_HOST", "localhost"),
port=int(os.getenv("FALKORDB_PORT", 6379)),
username=os.getenv("FALKORDB_USERNAME", None),
password=os.getenv("FALKORDB_PASSWORD", None),
)
def create_redis_connection() -> Any:
"""Create a sync Redis-compatible connection for metadata operations."""
if is_lite_backend():
return create_falkordb().connection
import redis
return redis.Redis(
host=os.getenv("FALKORDB_HOST", "localhost"),
port=int(os.getenv("FALKORDB_PORT", "6379")),
username=os.getenv("FALKORDB_USERNAME"),
password=os.getenv("FALKORDB_PASSWORD"),
decode_responses=True,
)
def create_async_redis_connection() -> Any:
"""Create an async Redis-compatible connection for metadata operations."""
if is_lite_backend():
return create_async_falkordb().connection
import redis.asyncio as aioredis
return aioredis.Redis(
host=os.getenv("FALKORDB_HOST", "localhost"),
port=int(os.getenv("FALKORDB_PORT", "6379")),
username=os.getenv("FALKORDB_USERNAME"),
password=os.getenv("FALKORDB_PASSWORD"),
decode_responses=True,
)
def graphrag_connection_kwargs() -> dict[str, Any]:
"""Return host/port kwargs for GraphRAG SDK constructors.
GraphRAG SDK accepts host/port only. FalkorDBLite's private Unix socket is
therefore usable for structural tools but not for GraphRAG unless a local
TCP port is explicitly enabled with ``FALKORDB_LITE_PORT``.
"""
if is_lite_backend():
port = os.getenv("FALKORDB_LITE_PORT")
if not port:
raise RuntimeError(
"GraphRAG requires host/port access. When using "
"CODE_GRAPH_DB_BACKEND=lite, set FALKORDB_LITE_PORT to expose "
"the embedded FalkorDBLite instance on localhost."
)
create_falkordb()
return {
"host": os.getenv("FALKORDB_LITE_HOST", "127.0.0.1"),
"port": int(port),
"username": None,
"password": None,
}
return {
"host": os.getenv("FALKORDB_HOST", "localhost"),
"port": int(os.getenv("FALKORDB_PORT", 6379)),
"username": os.getenv("FALKORDB_USERNAME", None),
"password": os.getenv("FALKORDB_PASSWORD", None),
}