MCPcopy Create free account
hub / github.com/su-kaka/gcli2api / StorageAdapter

Class StorageAdapter

src/storage_adapter.py:75–319  ·  view source on GitHub ↗

存储适配器,根据配置选择存储后端

Source from the content-addressed store, hash-verified

73
74
75class StorageAdapter:
76 """存储适配器,根据配置选择存储后端"""
77
78 def __init__(self):
79 self._backend: Optional["StorageBackend"] = None
80 self._initialized = False
81 self._lock = asyncio.Lock()
82
83 async def initialize(self) -> None:
84 """初始化存储适配器"""
85 async with self._lock:
86 if self._initialized:
87 return
88
89 # 按优先级检查存储后端:PostgreSQL > MongoDB > SQLite
90 postgresql_uri = os.getenv("POSTGRESQL_URI", "")
91 mongodb_uri = os.getenv("MONGODB_URI", "")
92
93 if postgresql_uri:
94 # 使用 PostgreSQL
95 try:
96 from .storage.psql_manager import PSQLManager
97
98 self._backend = PSQLManager()
99 await self._backend.initialize()
100 log.info("Using PostgreSQL storage backend")
101 except Exception as e:
102 log.error(f"Failed to initialize PostgreSQL backend: {e}")
103 # 尝试降级到 SQLite
104 log.info("Falling back to SQLite storage backend")
105 try:
106 from .storage.sqlite_manager import SQLiteManager
107
108 self._backend = SQLiteManager()
109 await self._backend.initialize()
110 log.info("Using SQLite storage backend (fallback)")
111 except Exception as e2:
112 log.error(f"Failed to initialize SQLite backend: {e2}")
113 raise RuntimeError("No storage backend available") from e2
114 elif not mongodb_uri:
115 # 优先使用 SQLite(默认启用,无需环境变量)
116 try:
117 from .storage.sqlite_manager import SQLiteManager
118
119 self._backend = SQLiteManager()
120 await self._backend.initialize()
121 log.info("Using SQLite storage backend")
122 except Exception as e:
123 log.error(f"Failed to initialize SQLite backend: {e}")
124 raise RuntimeError("No storage backend available") from e
125 else:
126 # 使用 MongoDB
127 try:
128 from .storage.mongodb_manager import MongoDBManager
129
130 self._backend = MongoDBManager()
131 await self._backend.initialize()
132 log.info("Using MongoDB storage backend")

Callers 1

get_storage_adapterFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected