Class InMemoryStore
- All Implemented Interfaces:
VDBStoreBase
This implementation stores vectors in memory using a ConcurrentHashMap for thread safety. It uses cosine similarity for vector search, which is the most common metric for embedding vectors.
This implementation is suitable for:
- Development and testing
- Small to medium-sized datasets
- Prototyping RAG systems
For production use with large datasets, consider using a dedicated vector database like Qdrant, ChromaDB, or Pinecone.
Example usage:
InMemoryStore store = InMemoryStore.builder()
.dimensions(1024)
.build();
// Add documents
store.add(documents).block();
// Search
List<Document> results = store.search(queryEmbedding, 5, 0.8).block();
Exception Handling:
IllegalArgumentException- for invalid input parameters (null documents, null embeddings, invalid limit)VectorStoreException- for vector-specific errors (dimension mismatch)
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionreactor.core.publisher.Mono<Void>Adds multiple documents to the store.static InMemoryStore.Builderbuilder()Creates a new builder for InMemoryStore.voidclear()Clears all documents from the store.reactor.core.publisher.Mono<Boolean>Deletes a document from the store (optional).intGets the dimension of vectors stored in this store.booleanisEmpty()Checks if the store is empty.reactor.core.publisher.Mono<List<io.agentscope.core.rag.model.Document>>search(SearchDocumentDto searchDocumentDto) Searches for similar documents.intsize()Gets the number of documents currently stored.
-
Method Details
-
add
public reactor.core.publisher.Mono<Void> add(List<io.agentscope.core.rag.model.Document> documentList) Description copied from interface:VDBStoreBaseAdds multiple documents to the store.All documents must have embedding vectors set. The store will persist both the vectors and the document metadata/payloads.
This method supports batch operations for efficiency. Implementations should handle batch insertion optimally.
- Specified by:
addin interfaceVDBStoreBase- Parameters:
documentList- the list of documents to store (all must have embeddings set)- Returns:
- a Mono that completes when all documents are stored
-
search
public reactor.core.publisher.Mono<List<io.agentscope.core.rag.model.Document>> search(SearchDocumentDto searchDocumentDto) Description copied from interface:VDBStoreBaseSearches for similar documents.This method performs vector similarity search and returns the most similar documents. Results include the document metadata and similarity scores. Optionally, embeddings may also be included in the results (implementation-dependent).
- Specified by:
searchin interfaceVDBStoreBase- Parameters:
searchDocumentDto- the search document DTO- Returns:
- a Mono that emits a list of Document objects with scores set, sorted by similarity (descending)
-
delete
Description copied from interface:VDBStoreBaseDeletes a document from the store (optional).Default implementation throws UnsupportedOperationException. Vector stores that support deletion should override this method.
- Specified by:
deletein interfaceVDBStoreBase- Parameters:
id- the document ID (UUID string) to delete- Returns:
- a Mono that emits true if the deletion was successful, false otherwise
-
size
public int size()Gets the number of documents currently stored.This method is thread-safe and returns the current snapshot of the store size.
- Returns:
- the number of stored documents (always non-negative)
-
isEmpty
public boolean isEmpty()Checks if the store is empty.Equivalent to
size() == 0. This method is thread-safe.- Returns:
- true if the store contains no documents, false otherwise
-
clear
public void clear() -
getDimensions
public int getDimensions()Gets the dimension of vectors stored in this store.- Returns:
- the vector dimension
-
builder
Creates a new builder for InMemoryStore.- Returns:
- a new Builder instance
-