forked from CodeGraphContext/CodeGraphContext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
107 lines (91 loc) · 3.98 KB
/
Copy path__init__.py
File metadata and controls
107 lines (91 loc) · 3.98 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
# src/codegraphcontext/core/__init__.py
"""
Core database management module.
Supports both Neo4j and FalkorDB Lite backends.
Use DATABASE_TYPE environment variable to switch:
- DATABASE_TYPE=falkordb - Uses embedded FalkorDB Lite (recommended for lite-version)
- DATABASE_TYPE=neo4j - Uses Neo4j server
- If not set, auto-detects based on what's available
"""
import os
from typing import Union
import importlib.util
def _is_falkordb_available() -> bool:
"""Check if FalkorDB Lite is installed (without importing native modules)."""
import sys
if sys.version_info < (3, 12):
return False
try:
# Check for redislite/falkordb-client spec without loading it
return importlib.util.find_spec("redislite") is not None
except ImportError:
return False
def _is_neo4j_configured() -> bool:
"""Check if Neo4j is configured with credentials."""
return all([
os.getenv('NEO4J_URI'),
os.getenv('NEO4J_USERNAME'),
os.getenv('NEO4J_PASSWORD')
])
def get_database_manager() -> Union['DatabaseManager', 'FalkorDBManager']:
"""
Factory function to get the appropriate database manager based on configuration.
Selection logic:
1. Runtime Override: 'CGC_RUNTIME_DB_TYPE' (set via --database flag)
2. Configured Default: 'DEFAULT_DATABASE' (set via 'cgc default database')
3. Legacy Env Var: 'DATABASE_TYPE'
4. Implicit Default: FalkorDB (if available)
5. Fallback: Neo4j (if configured)
"""
from codegraphcontext.utils.debug_log import info_logger
# 1. Runtime Override (CLI flag) or Config/Env
db_type = os.getenv('CGC_RUNTIME_DB_TYPE')
if not db_type:
db_type = os.getenv('DEFAULT_DATABASE')
if not db_type:
db_type = os.getenv('DATABASE_TYPE')
if db_type:
db_type = db_type.lower()
if db_type == 'falkordb':
if not _is_falkordb_available():
raise ValueError("Database set to 'falkordb' but FalkorDB Lite is not installed.\nRun 'pip install falkordblite'")
from .database_falkordb import FalkorDBManager
info_logger("Using FalkorDB Lite (explicit)")
return FalkorDBManager()
elif db_type == 'neo4j':
if not _is_neo4j_configured():
raise ValueError("Database set to 'neo4j' but it is not configured.\nRun 'cgc neo4j setup' to configure Neo4j.")
from .database import DatabaseManager
info_logger("Using Neo4j Server (explicit)")
return DatabaseManager()
else:
raise ValueError(f"Unknown database type: '{db_type}'. Use 'falkordb' or 'neo4j'.")
# 4. Implicit Default -> FalkorDB (Zero Config)
if _is_falkordb_available():
from .database_falkordb import FalkorDBManager
info_logger("Using FalkorDB Lite (default)")
return FalkorDBManager()
# 5. Fallback if FalkorDB missing but Neo4j is ready
if _is_neo4j_configured():
from .database import DatabaseManager
info_logger("Using Neo4j Server (auto-detected)")
return DatabaseManager()
import sys
error_msg = "No database backend available.\n"
if sys.version_info < (3, 12):
error_msg += (
"FalkorDB Lite is not supported on Python < 3.12.\n"
"You are running Python " + str(sys.version_info.major) + "." + str(sys.version_info.minor) + ".\n"
"Please upgrade to Python 3.12+ to use the embedded database,\n"
"OR run 'cgc neo4j setup' to configure an external Neo4j database."
)
else:
error_msg += (
"Recommended: Install FalkorDB Lite ('pip install falkordblite')\n"
"Alternative: Run 'cgc neo4j setup' to configure Neo4j."
)
raise ValueError(error_msg)
# For backward compatibility, export DatabaseManager
from .database import DatabaseManager
from .database_falkordb import FalkorDBManager
__all__ = ['DatabaseManager', 'FalkorDBManager', 'get_database_manager']