use crate::vector_stores::{Document, SearchResult, VectorStore, VectorStoreError};
use std::sync::Arc;
#[derive(Debug, Clone)]
pub enum VectorStoreType {
InMemory,
FileBacked,
Qdrant {
url: String,
collection: String,
},
}
pub struct VectorStoreProvider;
impl VectorStoreProvider {
pub async fn create(store_type: VectorStoreType) -> Result<Arc<dyn VectorStore>, VectorStoreError> {
match store_type {
VectorStoreType::InMemory => {
use crate::vector_stores::InMemoryVectorStore;
Ok(Arc::new(InMemoryVectorStore::new()))
}
VectorStoreType::FileBacked => {
use crate::vector_stores::InMemoryVectorStore;
Ok(Arc::new(InMemoryVectorStore::new()))
}
VectorStoreType::Qdrant { url, collection } => {
Self::create_qdrant_store(url, collection).await
}
}
}
async fn create_qdrant_store(url: String, collection: String) -> Result<Arc<dyn VectorStore>, VectorStoreError> {
#[cfg(feature = "qdrant-integration")]
{
use crate::vector_stores::{QdrantVectorStore, QdrantConfig};
let config = QdrantConfig::new(url, collection);
let store = QdrantVectorStore::new(config).await?;
Ok(Arc::new(store))
}
#[cfg(not(feature = "qdrant-integration"))]
{
eprintln!("Warning: Qdrant requested but feature 'qdrant-integration' not enabled. Falling back to InMemory store.");
use crate::vector_stores::InMemoryVectorStore;
Ok(Arc::new(InMemoryVectorStore::new()))
}
}
}
pub struct VectorStoreBuilder {
store_type: VectorStoreType,
}
impl VectorStoreBuilder {
pub fn new() -> Self {
Self {
store_type: VectorStoreType::InMemory,
}
}
pub fn in_memory() -> Self {
Self {
store_type: VectorStoreType::InMemory,
}
}
pub fn file_backed() -> Self {
Self {
store_type: VectorStoreType::FileBacked,
}
}
pub fn qdrant(url: impl Into<String>, collection: impl Into<String>) -> Self {
Self {
store_type: VectorStoreType::Qdrant {
url: url.into(),
collection: collection.into(),
},
}
}
pub async fn build(self) -> Result<Arc<dyn VectorStore>, VectorStoreError> {
VectorStoreProvider::create(self.store_type).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_create_in_memory() {
let result = VectorStoreProvider::create(VectorStoreType::InMemory).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_builder_in_memory() {
let builder = VectorStoreBuilder::in_memory();
let store = builder.build().await;
assert!(store.is_ok());
}
#[tokio::test]
async fn test_builder_qdrant_fallback() {
let builder = VectorStoreBuilder::qdrant("http://localhost:6334", "test_collection");
let store = builder.build().await;
assert!(store.is_ok());
}
}