@@ -187,6 +187,19 @@ def _get_or_create_shared_client(cls, cfg: NebulaGraphDBConfig) -> tuple[str, "N
187187 client = cls ._CLIENT_CACHE .get (key )
188188 if client is None :
189189 # Connection setting
190+
191+ tmp_client = NebulaClient (
192+ hosts = cfg .uri ,
193+ username = cfg .user ,
194+ password = cfg .password ,
195+ session_config = SessionConfig (graph = None ),
196+ session_pool_config = SessionPoolConfig (size = 1 , wait_timeout = 3000 ),
197+ )
198+ try :
199+ cls ._ensure_space_exists (tmp_client , cfg )
200+ finally :
201+ tmp_client .close ()
202+
190203 conn_conf : ConnectionConfig | None = getattr (cfg , "conn_config" , None )
191204 if conn_conf is None :
192205 conn_conf = ConnectionConfig .from_defults (
@@ -317,6 +330,7 @@ def __init__(self, config: NebulaGraphDBConfig):
317330 }
318331 """
319332
333+ assert config .use_multi_db is False , "Multi-DB MODE IS NOT SUPPORTED"
320334 self .config = config
321335 self .db_name = config .space
322336 self .user_name = config .user_name
@@ -349,7 +363,7 @@ def __init__(self, config: NebulaGraphDBConfig):
349363 if (str (self .embedding_dimension ) != str (self .default_memory_dimension ))
350364 else "embedding"
351365 )
352- self .system_db_name = "system" if config . use_multi_db else config .space
366+ self .system_db_name = config .space
353367
354368 # ---- NEW: pool acquisition strategy
355369 # Get or create a shared pool from the class-level cache
@@ -436,7 +450,7 @@ def remove_oldest_memory(
436450 WHERE n.memory_type = '{ memory_type } '
437451 { optional_condition }
438452 ORDER BY n.updated_at DESC
439- OFFSET { keep_latest }
453+ OFFSET { int ( keep_latest ) }
440454 DETACH DELETE n
441455 """
442456 self .execute_query (query )
@@ -481,7 +495,7 @@ def node_not_exist(self, scope: str, user_name: str | None = None) -> int:
481495 user_name = user_name if user_name else self .config .user_name
482496 filter_clause = f'n.memory_type = "{ scope } " AND n.user_name = "{ user_name } "'
483497 query = f"""
484- MATCH (n@Memory)
498+ MATCH (n@Memory /*+ INDEX(idx_memory_user_name) */ )
485499 WHERE { filter_clause }
486500 RETURN n.id AS id
487501 LIMIT 1
@@ -838,7 +852,7 @@ def get_neighbors_by_tag(
838852 query = f"""
839853 LET tag_list = { tag_list_literal }
840854
841- MATCH (n@Memory)
855+ MATCH (n@Memory /*+ INDEX(idx_memory_user_name) */ )
842856 WHERE { where_clause }
843857 RETURN { return_fields } ,
844858 size( filter( n.tags, t -> t IN tag_list ) ) AS overlap_count
@@ -1392,6 +1406,17 @@ def get_structure_optimization_candidates(
13921406 logger .error (f"Failed : { e } , traceback: { traceback .format_exc ()} " )
13931407 return candidates
13941408
1409+ @timed
1410+ def drop_database (self ) -> None :
1411+ """
1412+ Permanently delete the entire database this instance is using.
1413+ WARNING: This operation is destructive and cannot be undone.
1414+ """
1415+ raise ValueError (
1416+ f"Refusing to drop protected database: `{ self .db_name } ` in "
1417+ f"Shared Database Multi-Tenant mode"
1418+ )
1419+
13951420 @timed
13961421 def detect_conflicts (self ) -> list [tuple [str , str ]]:
13971422 """
@@ -1462,6 +1487,25 @@ def merge_nodes(self, id1: str, id2: str) -> str:
14621487 """
14631488 raise NotImplementedError
14641489
1490+ @classmethod
1491+ def _ensure_space_exists (cls , tmp_client , cfg ):
1492+ """Lightweight check to ensure target graph (space) exists."""
1493+ db_name = getattr (cfg , "space" , None )
1494+ if not db_name :
1495+ logger .warning ("[NebulaGraphDBSync] No `space` specified in cfg." )
1496+ return
1497+
1498+ try :
1499+ res = tmp_client .execute ("SHOW GRAPHS;" )
1500+ existing = {row .values ()[0 ].as_string () for row in res }
1501+ if db_name not in existing :
1502+ tmp_client .execute (f"CREATE GRAPH IF NOT EXISTS `{ db_name } ` TYPED MemOSBgeM3Type;" )
1503+ logger .info (f"✅ Graph `{ db_name } ` created before session binding." )
1504+ else :
1505+ logger .debug (f"Graph `{ db_name } ` already exists." )
1506+ except Exception :
1507+ logger .exception ("[NebulaGraphDBSync] Failed to ensure space exists" )
1508+
14651509 @timed
14661510 def _ensure_database_exists (self ):
14671511 graph_type_name = "MemOSBgeM3Type"
0 commit comments