@@ -28,6 +28,25 @@ def __init__(self, client):
2828
2929 self .client : RedisDatabase = client
3030
31+ def index_exists (self , index : str ) -> bool :
32+ """
33+ Check if the index (key) exists in Redis.
34+
35+ Args:
36+ index: The Redis key to check
37+
38+ Returns:
39+ bool: True if the key exists, False otherwise
40+ """
41+ try :
42+ # Use Redis EXISTS command to check if key exists
43+ return bool (self .client ._client .exists (index ))
44+ except Exception as e :
45+ logger .error (
46+ f"Failed to check if key exists in Redis: index={ index } error={ e } "
47+ )
48+ return False
49+
3150 def add (self , data : list [str ], index : str , ** kwargs ):
3251 logger .debug (f"Adding documents to Redis database: index={ index } " )
3352
@@ -99,6 +118,24 @@ def __init__(self, client):
99118
100119 self .client : MysqlDatabase = client
101120
121+ def index_exists (self , index : str ) -> bool :
122+ """
123+ Check if the table (index) exists in MySQL database.
124+
125+ Args:
126+ index: The table name to check
127+
128+ Returns:
129+ bool: True if the table exists, False otherwise
130+ """
131+ try :
132+ return self .client .table_exists (index )
133+ except Exception as e :
134+ logger .error (
135+ f"Failed to check if table exists in MySQL: index={ index } error={ e } "
136+ )
137+ return False
138+
102139 def create_table (self , table_name : str ):
103140 logger .debug (f"Creating table for SQL database: table_name={ table_name } " )
104141
@@ -188,6 +225,25 @@ def __init__(self, client):
188225
189226 self .client : OpenSearchVectorDatabase = client
190227
228+ def index_exists (self , index : str ) -> bool :
229+ """
230+ Check if the collection (index) exists in OpenSearch.
231+
232+ Args:
233+ index: The collection name to check
234+
235+ Returns:
236+ bool: True if the collection exists, False otherwise
237+ """
238+ try :
239+ self ._validate_index (index )
240+ return self .client .collection_exists (index )
241+ except Exception as e :
242+ logger .error (
243+ f"Failed to check if collection exists in OpenSearch: index={ index } error={ e } "
244+ )
245+ return False
246+
191247 def _validate_index (self , index : str ):
192248 """
193249 Verify whether the string conforms to the naming rules of index_name in OpenSearch.
@@ -259,6 +315,25 @@ def __init__(self, client):
259315
260316 self .client : VikingDatabase = client
261317
318+ def index_exists (self , index : str ) -> bool :
319+ """
320+ Check if the collection (index) exists in VikingDB.
321+
322+ Args:
323+ index: The collection name to check
324+
325+ Returns:
326+ bool: True if the collection exists, False otherwise
327+ """
328+ try :
329+ self ._validate_index (index )
330+ return self .client .collection_exists (index )
331+ except Exception as e :
332+ logger .error (
333+ f"Failed to check if collection exists in VikingDB: index={ index } error={ e } "
334+ )
335+ return False
336+
262337 def _validate_index (self , index : str ):
263338 """
264339 Only English letters, numbers, and underscores (_) are allowed.
@@ -341,6 +416,25 @@ def __init__(self, client):
341416
342417 self .client : VikingMemoryDatabase = client
343418
419+ def index_exists (self , index : str ) -> bool :
420+ """
421+ Check if the collection (index) exists in VikingMemoryDB.
422+
423+ Note:
424+ VikingMemoryDatabase does not support checking if a collection exists.
425+ This method always returns False.
426+
427+ Args:
428+ index: The collection name to check
429+
430+ Returns:
431+ bool: Always returns False as VikingMemoryDatabase does not support this functionality
432+ """
433+ logger .warning (
434+ "VikingMemoryDatabase does not support checking if a collection exists"
435+ )
436+ raise NotImplementedError ("VikingMemoryDatabase does not support index_exists" )
437+
344438 def _validate_index (self , index : str ):
345439 if not (
346440 isinstance (index , str )
@@ -388,6 +482,23 @@ def __init__(self, client):
388482
389483 self .client : LocalDataBase = client
390484
485+ def index_exists (self , index : str ) -> bool :
486+ """
487+ Check if the index exists in LocalDataBase.
488+
489+ Note:
490+ LocalDataBase does not support checking if an index exists.
491+ This method always returns False.
492+
493+ Args:
494+ index: The index name to check (not used in LocalDataBase)
495+
496+ Returns:
497+ bool: Always returns False as LocalDataBase does not support this functionality
498+ """
499+ logger .warning ("LocalDataBase does not support checking if an index exists" )
500+ return True
501+
391502 def add (self , data : list [str ], ** kwargs ):
392503 self .client .add (data )
393504
0 commit comments