Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 125 additions & 7 deletions veadk/database/database_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,26 @@ def __init__(self, client):

self.client: RedisDatabase = client

def add(self, data: list[str], index: str):
def index_exists(self, index: str) -> bool:
"""
Check if the index (key) exists in Redis.

Args:
index: The Redis key to check

Returns:
bool: True if the key exists, False otherwise
"""
try:
# Use Redis EXISTS command to check if key exists
return bool(self.client._client.exists(index))
except Exception as e:
logger.error(
f"Failed to check if key exists in Redis: index={index} error={e}"
)
return False

def add(self, data: list[str], index: str, **kwargs):
logger.debug(f"Adding documents to Redis database: index={index}")

try:
Expand Down Expand Up @@ -78,7 +97,7 @@ def delete_doc(self, index: str, id: str) -> bool:
)
return False

def list_docs(self, index: str, offset: int = 0, limit: int = 100) -> list[dict]:
def list_chunks(self, index: str, offset: int = 0, limit: int = 100) -> list[dict]:
logger.debug(f"Listing documents from Redis database: index={index}")
try:
# Get all documents from Redis
Expand All @@ -99,6 +118,24 @@ def __init__(self, client):

self.client: MysqlDatabase = client

def index_exists(self, index: str) -> bool:
"""
Check if the table (index) exists in MySQL database.

Args:
index: The table name to check

Returns:
bool: True if the table exists, False otherwise
"""
try:
return self.client.table_exists(index)
except Exception as e:
logger.error(
f"Failed to check if table exists in MySQL: index={index} error={e}"
)
return False

def create_table(self, table_name: str):
logger.debug(f"Creating table for SQL database: table_name={table_name}")

Expand All @@ -111,7 +148,7 @@ def create_table(self, table_name: str):
"""
self.client.add(sql)

def add(self, data: list[str], index: str):
def add(self, data: list[str], index: str, **kwargs):
logger.debug(
f"Adding documents to SQL database: table_name={index} data_len={len(data)}"
)
Expand Down Expand Up @@ -188,6 +225,25 @@ def __init__(self, client):

self.client: OpenSearchVectorDatabase = client

def index_exists(self, index: str) -> bool:
"""
Check if the collection (index) exists in OpenSearch.

Args:
index: The collection name to check

Returns:
bool: True if the collection exists, False otherwise
"""
try:
self._validate_index(index)
return self.client.collection_exists(index)
except Exception as e:
logger.error(
f"Failed to check if collection exists in OpenSearch: index={index} error={e}"
)
return False

def _validate_index(self, index: str):
"""
Verify whether the string conforms to the naming rules of index_name in OpenSearch.
Expand All @@ -203,7 +259,7 @@ def _validate_index(self, index: str):
"The index name does not conform to the naming rules of OpenSearch"
)

def add(self, data: list[str], index: str):
def add(self, data: list[str], index: str, **kwargs):
self._validate_index(index)

logger.debug(
Expand Down Expand Up @@ -247,7 +303,7 @@ def delete_doc(self, index: str, id: str) -> bool:
)
return False

def list_docs(self, index: str, offset: int = 0, limit: int = 1000) -> list[dict]:
def list_chunks(self, index: str, offset: int = 0, limit: int = 1000) -> list[dict]:
self._validate_index(index)
logger.debug(f"Listing documents from vector database: index={index}")
return self.client.list_docs(collection_name=index, offset=offset, limit=limit)
Expand All @@ -259,6 +315,25 @@ def __init__(self, client):

self.client: VikingDatabase = client

def index_exists(self, index: str) -> bool:
"""
Check if the collection (index) exists in VikingDB.

Args:
index: The collection name to check

Returns:
bool: True if the collection exists, False otherwise
"""
try:
self._validate_index(index)
return self.client.collection_exists(index)
except Exception as e:
logger.error(
f"Failed to check if collection exists in VikingDB: index={index} error={e}"
)
return False

def _validate_index(self, index: str):
"""
Only English letters, numbers, and underscores (_) are allowed.
Expand Down Expand Up @@ -322,6 +397,13 @@ def delete_doc(self, index: str, id: str) -> bool:
logger.debug(f"Deleting documents from vector database: index={index} id={id}")
return self.client.delete_by_id(collection_name=index, id=id)

def list_chunks(self, index: str, offset: int, limit: int) -> list[dict]:
self._validate_index(index)
logger.debug(f"Listing documents from vector database: index={index}")
return self.client.list_chunks(
collection_name=index, offset=offset, limit=limit
)

def list_docs(self, index: str, offset: int, limit: int) -> list[dict]:
self._validate_index(index)
logger.debug(f"Listing documents from vector database: index={index}")
Expand All @@ -334,6 +416,25 @@ def __init__(self, client):

self.client: VikingMemoryDatabase = client

def index_exists(self, index: str) -> bool:
"""
Check if the collection (index) exists in VikingMemoryDB.

Note:
VikingMemoryDatabase does not support checking if a collection exists.
This method always returns False.

Args:
index: The collection name to check

Returns:
bool: Always returns False as VikingMemoryDatabase does not support this functionality
"""
logger.warning(
"VikingMemoryDatabase does not support checking if a collection exists"
)
raise NotImplementedError("VikingMemoryDatabase does not support index_exists")

def _validate_index(self, index: str):
if not (
isinstance(index, str)
Expand Down Expand Up @@ -371,7 +472,7 @@ def delete(self, index: str) -> bool:
def delete_docs(self, index: str, ids: list[int]):
raise NotImplementedError("VikingMemoryDatabase does not support delete_docs")

def list_docs(self, index: str):
def list_chunks(self, index: str):
raise NotImplementedError("VikingMemoryDatabase does not support list_docs")


Expand All @@ -381,6 +482,23 @@ def __init__(self, client):

self.client: LocalDataBase = client

def index_exists(self, index: str) -> bool:
"""
Check if the index exists in LocalDataBase.

Note:
LocalDataBase does not support checking if an index exists.
This method always returns False.

Args:
index: The index name to check (not used in LocalDataBase)

Returns:
bool: Always returns False as LocalDataBase does not support this functionality
"""
logger.warning("LocalDataBase does not support checking if an index exists")
return True

def add(self, data: list[str], **kwargs):
self.client.add(data)

Expand All @@ -393,7 +511,7 @@ def delete(self, index: str) -> bool:
def delete_doc(self, index: str, id: str) -> bool:
return self.client.delete_doc(id)

def list_docs(self, index: str, offset: int = 0, limit: int = 100) -> list[dict]:
def list_chunks(self, index: str, offset: int = 0, limit: int = 100) -> list[dict]:
return self.client.list_docs(offset=offset, limit=limit)


Expand Down
Loading