Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
5b1ff1a
feat(qdrant): implement Qdrant client connection manager
claude Nov 14, 2025
a881e1a
feat(qdrant): implement Qdrant repository base class
claude Nov 14, 2025
8f241d5
feat(qdrant): implement collection initialization and management
claude Nov 14, 2025
604f52d
feat(qdrant): define collection schema and configuration models
claude Nov 14, 2025
4570ab9
feat(qdrant): implement point models for vector storage
claude Nov 14, 2025
0233dea
feat(qdrant): implement vector storage operations
claude Nov 14, 2025
5cdf4d5
feat(qdrant): implement vector similarity search
claude Nov 14, 2025
c8b24d8
feat(similarity): implement similarity score calculator
claude Nov 14, 2025
3935394
feat(similarity): implement vector normalization utilities
claude Nov 14, 2025
21ab124
feat(qdrant): implement filter builder for advanced queries
claude Nov 14, 2025
9da8461
feat(qdrant): implement advanced batch upload operations
claude Nov 14, 2025
345f643
feat(qdrant): implement delete operations for points
claude Nov 14, 2025
ebf9778
feat(qdrant): implement point update operations
claude Nov 14, 2025
9a3695c
feat(qdrant): implement metadata handling utilities
claude Nov 14, 2025
6c4ae64
feat(qdrant): implement pagination for large result sets
claude Nov 14, 2025
1568cc0
feat(qdrant): implement comprehensive health check service
claude Nov 14, 2025
12b4d20
feat(qdrant): implement metrics collection models
claude Nov 14, 2025
9b23d56
style: apply black formatting to qdrant implementation
claude Nov 14, 2025
d26e1c0
style: fix isort import ordering in qdrant repository
claude Nov 14, 2025
fbf2833
style: remove unused imports for flake8 compliance
claude Nov 14, 2025
395c28d
fix(types): resolve mypy type errors across qdrant implementation
claude Nov 14, 2025
13c5804
feat(qdrant): implement comprehensive error handling system
claude Nov 14, 2025
228e8ff
feat(qdrant): implement advanced connection pooling system
claude Nov 14, 2025
68b2c2c
feat(qdrant): implement comprehensive index optimization system
claude Nov 14, 2025
3866fdb
test(qdrant): implement comprehensive unit test suite
claude Nov 14, 2025
26f8a56
feat(qdrant): implement collection backup and restore system
claude Nov 14, 2025
71d2102
style: fix isort import ordering in unit tests
claude Nov 14, 2025
4cc3f27
style: fix isort alphabetical ordering in test_vector_normalizer
claude Nov 14, 2025
0f80235
style: fix flake8 line length errors in qdrant_health
claude Nov 14, 2025
7535193
style: apply black formatting to qdrant_health
claude Nov 14, 2025
6f211c9
fix(types): resolve mypy type errors in qdrant modules
claude Nov 14, 2025
3172db2
fix(tests): add convenience aliases and functions for test compatibility
claude Nov 14, 2025
3440ae7
fix(tests): resolve failing unit tests
claude Nov 14, 2025
96a8733
fix(tests): adjust score interpretation thresholds
claude Nov 14, 2025
6981f68
feat(tests): implement Qdrant integration tests and fix CI workflow
claude Nov 14, 2025
e6c4387
feat(benchmarks): implement comprehensive Qdrant performance benchmarks
claude Nov 14, 2025
a96ecbe
feat(similarity): implement semantic similarity threshold tuning
claude Nov 14, 2025
44337fd
style: fix flake8 linting errors in benchmarks and threshold tuner
claude Nov 15, 2025
bcd6d94
fix(types): resolve mypy type errors in threshold tuner
claude Nov 15, 2025
f7cd741
fix: address PR review feedback from Gemini Code Assist
claude Nov 15, 2025
385aa8c
fix(types): resolve mypy type error in qdrant_point.py
claude Nov 15, 2025
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
118 changes: 118 additions & 0 deletions app/cache/qdrant_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"""
Qdrant client connection manager.

Sandi Metz Principles:
- Single Responsibility: Qdrant connection management
- Small methods: Each operation isolated
- Dependency Injection: Configuration injected
"""

from typing import Optional

from qdrant_client import AsyncQdrantClient
from qdrant_client.http.exceptions import UnexpectedResponse

from app.config import config
from app.utils.logger import get_logger

logger = get_logger(__name__)


async def create_qdrant_client() -> AsyncQdrantClient:
"""
Create Qdrant async client connection.

Returns:
Qdrant async client

Raises:
ConnectionError: If connection fails
"""
try:
client = AsyncQdrantClient(
host=config.qdrant_host,
port=config.qdrant_port,
timeout=30.0,
)

# Test connection
await client.get_collections()

logger.info(
"Qdrant client connected",
host=config.qdrant_host,
port=config.qdrant_port,
)

return client

except Exception as e:
logger.error("Qdrant connection failed", error=str(e))
raise ConnectionError(f"Failed to connect to Qdrant: {e}")
Comment on lines +49 to +51
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The except Exception is too broad and can mask programming errors. It's better to catch specific exceptions related to connection issues. You've already imported UnexpectedResponse, which is a good one to catch specifically. You could also consider catching network-related exceptions from httpx (which qdrant-client uses) if you want to be more precise.

Suggested change
except Exception as e:
logger.error("Qdrant connection failed", error=str(e))
raise ConnectionError(f"Failed to connect to Qdrant: {e}")
except UnexpectedResponse as e:
logger.error("Qdrant connection failed with unexpected response", error=str(e), status_code=e.status_code)
raise ConnectionError(f"Failed to connect to Qdrant: {e}") from e
except Exception as e:
logger.error("Qdrant connection failed", error=str(e))
raise ConnectionError(f"Failed to connect to Qdrant: {e}") from e



class QdrantConnectionManager:
"""
Manages Qdrant client connection lifecycle.

Handles connection pooling and health checks.
"""

def __init__(self):
"""Initialize connection manager."""
self._client: Optional[AsyncQdrantClient] = None

async def get_client(self) -> AsyncQdrantClient:
"""
Get or create Qdrant client.

Returns:
Qdrant async client

Raises:
ConnectionError: If connection fails
"""
if self._client is None:
self._client = await create_qdrant_client()
return self._client

async def close(self) -> None:
"""Close Qdrant client connection."""
if self._client is not None:
try:
await self._client.close()
logger.info("Qdrant client closed")
except Exception as e:
logger.error("Failed to close Qdrant client", error=str(e))
finally:
self._client = None

async def health_check(self) -> bool:
"""
Check Qdrant server health.

Returns:
True if healthy, False otherwise
"""
try:
client = await self.get_client()
await client.get_collections()
return True
except Exception as e:
logger.error("Qdrant health check failed", error=str(e))
return False

async def reconnect(self) -> bool:
"""
Reconnect to Qdrant server.

Returns:
True if reconnected successfully
"""
try:
await self.close()
self._client = await create_qdrant_client()
return True
except Exception as e:
logger.error("Qdrant reconnection failed", error=str(e))
return False
167 changes: 167 additions & 0 deletions app/cache/qdrant_collection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
"""
Qdrant collection initialization and management.

Sandi Metz Principles:
- Single Responsibility: Collection setup and validation
- Small methods: Each operation isolated
- Dependency Injection: Repository injected
"""

from typing import Optional

from qdrant_client.models import Distance

from app.repositories.qdrant_repository import QdrantRepository
from app.utils.logger import get_logger

logger = get_logger(__name__)


class QdrantCollectionManager:
"""
Manages Qdrant collection initialization.

Ensures collection exists and is properly configured.
"""

def __init__(self, repository: QdrantRepository):
"""
Initialize collection manager.

Args:
repository: Qdrant repository
"""
self._repository = repository

async def initialize(
self, distance: Distance = Distance.COSINE, recreate: bool = False
) -> bool:
"""
Initialize collection for vector storage.

Args:
distance: Distance metric for similarity
recreate: Whether to recreate existing collection

Returns:
True if initialized successfully
"""
try:
if recreate:
await self._recreate_collection(distance)
return True

return await self._ensure_collection_exists(distance)

except Exception as e:
logger.error("Collection initialization failed", error=str(e))
return False

async def _ensure_collection_exists(self, distance: Distance) -> bool:
"""
Ensure collection exists.

Args:
distance: Distance metric

Returns:
True if exists or created
"""
exists = await self._repository.collection_exists()

if exists:
logger.info("Collection verified")
return True

return await self._repository.create_collection(distance)

async def _recreate_collection(self, distance: Distance) -> bool:
"""
Recreate collection (delete and create).

Args:
distance: Distance metric

Returns:
True if recreated successfully
"""
logger.warning("Recreating collection - all data will be lost")

# Delete if exists
exists = await self._repository.collection_exists()
if exists:
await self._repository.delete_collection()

# Create new collection
return await self._repository.create_collection(distance)

async def validate_collection(self) -> dict[str, bool]:
"""
Validate collection configuration.

Returns:
Validation results dict
"""
results = {
"exists": False,
"accessible": False,
"configured": False,
}

try:
# Check existence
results["exists"] = await self._repository.collection_exists()
if not results["exists"]:
return results

# Check accessibility
results["accessible"] = await self._repository.ping()
if not results["accessible"]:
return results

# Check configuration
info = await self._repository.get_collection_info()
results["configured"] = info is not None

return results

except Exception as e:
logger.error("Collection validation failed", error=str(e))
return results

async def get_status(self) -> Optional[dict]:
"""
Get collection status and statistics.

Returns:
Status dict if successful
"""
try:
validation = await self.validate_collection()
if not validation["exists"]:
return {
"status": "not_initialized",
"message": "Collection does not exist",
}

info = await self._repository.get_collection_info()
if not info:
return {
"status": "error",
"message": "Failed to get collection info",
}

return {
"status": "ready",
"vectors_count": info["vectors_count"],
"points_count": info["points_count"],
"collection_status": info["status"],
"config": info["config"],
}

except Exception as e:
logger.error("Get status failed", error=str(e))
return {
"status": "error",
"message": str(e),
}
Loading
Loading