Class InMemoryStore

java.lang.Object
io.agentscope.core.rag.store.InMemoryStore
All Implemented Interfaces:
VDBStoreBase

public class InMemoryStore extends Object implements VDBStoreBase
In-memory implementation of vector database storage.

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:

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Builder for InMemoryStore.
  • Method Summary

    Modifier and Type
    Method
    Description
    reactor.core.publisher.Mono<Void>
    add(List<io.agentscope.core.rag.model.Document> documentList)
    Adds multiple documents to the store.
    Creates a new builder for InMemoryStore.
    void
    Clears all documents from the store.
    reactor.core.publisher.Mono<Boolean>
    Deletes a document from the store (optional).
    int
    Gets the dimension of vectors stored in this store.
    boolean
    Checks if the store is empty.
    reactor.core.publisher.Mono<List<io.agentscope.core.rag.model.Document>>
    search(SearchDocumentDto searchDocumentDto)
    Searches for similar documents.
    int
    Gets the number of documents currently stored.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • add

      public reactor.core.publisher.Mono<Void> add(List<io.agentscope.core.rag.model.Document> documentList)
      Description copied from interface: VDBStoreBase
      Adds 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:
      add in interface VDBStoreBase
      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: VDBStoreBase
      Searches 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:
      search in interface VDBStoreBase
      Parameters:
      searchDocumentDto - the search document DTO
      Returns:
      a Mono that emits a list of Document objects with scores set, sorted by similarity (descending)
    • delete

      public reactor.core.publisher.Mono<Boolean> delete(String id)
      Description copied from interface: VDBStoreBase
      Deletes a document from the store (optional).

      Default implementation throws UnsupportedOperationException. Vector stores that support deletion should override this method.

      Specified by:
      delete in interface VDBStoreBase
      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()
      Clears all documents from the store.

      After this operation, size() returns 0 and isEmpty() returns true. This operation is thread-safe.

    • getDimensions

      public int getDimensions()
      Gets the dimension of vectors stored in this store.
      Returns:
      the vector dimension
    • builder

      public static InMemoryStore.Builder builder()
      Creates a new builder for InMemoryStore.
      Returns:
      a new Builder instance